michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef CHROME_COMMON_IPC_LOGGING_H_ michael@0: #define CHROME_COMMON_IPC_LOGGING_H_ michael@0: michael@0: #include "chrome/common/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED. michael@0: michael@0: #ifdef IPC_MESSAGE_LOG_ENABLED michael@0: michael@0: #include "base/lock.h" michael@0: #include "base/message_loop.h" michael@0: #include "base/singleton.h" michael@0: #include "base/waitable_event_watcher.h" michael@0: #include "chrome/common/ipc_message_utils.h" michael@0: michael@0: class MessageLoop; michael@0: michael@0: namespace IPC { michael@0: michael@0: class Message; michael@0: michael@0: // One instance per process. Needs to be created on the main thread (the UI michael@0: // thread in the browser) but OnPreDispatchMessage/OnPostDispatchMessage michael@0: // can be called on other threads. michael@0: class Logging : public base::WaitableEventWatcher::Delegate, michael@0: public MessageLoop::DestructionObserver { michael@0: public: michael@0: // Implemented by consumers of log messages. michael@0: class Consumer { michael@0: public: michael@0: virtual void Log(const LogData& data) = 0; michael@0: }; michael@0: michael@0: void SetConsumer(Consumer* consumer); michael@0: michael@0: ~Logging(); michael@0: static Logging* current(); michael@0: michael@0: void Enable(); michael@0: void Disable(); michael@0: bool Enabled() const { return enabled_; } michael@0: michael@0: // Called by child processes to give the logger object the channel to send michael@0: // logging data to the browser process. michael@0: void SetIPCSender(Message::Sender* sender); michael@0: michael@0: // Called in the browser process when logging data from a child process is michael@0: // received. michael@0: void OnReceivedLoggingMessage(const Message& message); michael@0: michael@0: void OnSendMessage(Message* message, const std::wstring& channel_id); michael@0: void OnPreDispatchMessage(const Message& message); michael@0: void OnPostDispatchMessage(const Message& message, michael@0: const std::wstring& channel_id); michael@0: michael@0: // Returns the name of the logging enabled/disabled events so that the michael@0: // sandbox can add them to to the policy. If true, gets the name of the michael@0: // enabled event, if false, gets the name of the disabled event. michael@0: static std::wstring GetEventName(bool enabled); michael@0: michael@0: // Like the *MsgLog functions declared for each message class, except this michael@0: // calls the correct one based on the message type automatically. Defined in michael@0: // ipc_logging.cc. michael@0: static void GetMessageText(uint16_t type, std::wstring* name, michael@0: const Message* message, std::wstring* params); michael@0: michael@0: // WaitableEventWatcher::Delegate implementation michael@0: void OnWaitableEventSignaled(base::WaitableEvent* event); michael@0: michael@0: // MessageLoop::DestructionObserver implementation michael@0: void WillDestroyCurrentMessageLoop(); michael@0: michael@0: typedef void (*LogFunction)(uint16_t type, michael@0: std::wstring* name, michael@0: const Message* msg, michael@0: std::wstring* params); michael@0: michael@0: static void SetLoggerFunctions(LogFunction *functions); michael@0: michael@0: private: michael@0: friend struct DefaultSingletonTraits; michael@0: Logging(); michael@0: michael@0: std::wstring GetEventName(int browser_pid, bool enabled); michael@0: void OnSendLogs(); michael@0: void Log(const LogData& data); michael@0: michael@0: void RegisterWaitForEvent(bool enabled); michael@0: michael@0: base::WaitableEventWatcher watcher_; michael@0: michael@0: scoped_ptr logging_event_on_; michael@0: scoped_ptr logging_event_off_; michael@0: bool enabled_; michael@0: michael@0: std::vector queued_logs_; michael@0: bool queue_invoke_later_pending_; michael@0: michael@0: Message::Sender* sender_; michael@0: MessageLoop* main_thread_; michael@0: michael@0: Consumer* consumer_; michael@0: michael@0: static LogFunction *log_function_mapping_; michael@0: }; michael@0: michael@0: } // namespace IPC michael@0: michael@0: #endif // IPC_MESSAGE_LOG_ENABLED michael@0: michael@0: #endif // CHROME_COMMON_IPC_LOGGING_H_