|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 #ifndef CHROME_COMMON_IPC_LOGGING_H_ |
|
6 #define CHROME_COMMON_IPC_LOGGING_H_ |
|
7 |
|
8 #include "chrome/common/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED. |
|
9 |
|
10 #ifdef IPC_MESSAGE_LOG_ENABLED |
|
11 |
|
12 #include "base/lock.h" |
|
13 #include "base/message_loop.h" |
|
14 #include "base/singleton.h" |
|
15 #include "base/waitable_event_watcher.h" |
|
16 #include "chrome/common/ipc_message_utils.h" |
|
17 |
|
18 class MessageLoop; |
|
19 |
|
20 namespace IPC { |
|
21 |
|
22 class Message; |
|
23 |
|
24 // One instance per process. Needs to be created on the main thread (the UI |
|
25 // thread in the browser) but OnPreDispatchMessage/OnPostDispatchMessage |
|
26 // can be called on other threads. |
|
27 class Logging : public base::WaitableEventWatcher::Delegate, |
|
28 public MessageLoop::DestructionObserver { |
|
29 public: |
|
30 // Implemented by consumers of log messages. |
|
31 class Consumer { |
|
32 public: |
|
33 virtual void Log(const LogData& data) = 0; |
|
34 }; |
|
35 |
|
36 void SetConsumer(Consumer* consumer); |
|
37 |
|
38 ~Logging(); |
|
39 static Logging* current(); |
|
40 |
|
41 void Enable(); |
|
42 void Disable(); |
|
43 bool Enabled() const { return enabled_; } |
|
44 |
|
45 // Called by child processes to give the logger object the channel to send |
|
46 // logging data to the browser process. |
|
47 void SetIPCSender(Message::Sender* sender); |
|
48 |
|
49 // Called in the browser process when logging data from a child process is |
|
50 // received. |
|
51 void OnReceivedLoggingMessage(const Message& message); |
|
52 |
|
53 void OnSendMessage(Message* message, const std::wstring& channel_id); |
|
54 void OnPreDispatchMessage(const Message& message); |
|
55 void OnPostDispatchMessage(const Message& message, |
|
56 const std::wstring& channel_id); |
|
57 |
|
58 // Returns the name of the logging enabled/disabled events so that the |
|
59 // sandbox can add them to to the policy. If true, gets the name of the |
|
60 // enabled event, if false, gets the name of the disabled event. |
|
61 static std::wstring GetEventName(bool enabled); |
|
62 |
|
63 // Like the *MsgLog functions declared for each message class, except this |
|
64 // calls the correct one based on the message type automatically. Defined in |
|
65 // ipc_logging.cc. |
|
66 static void GetMessageText(uint16_t type, std::wstring* name, |
|
67 const Message* message, std::wstring* params); |
|
68 |
|
69 // WaitableEventWatcher::Delegate implementation |
|
70 void OnWaitableEventSignaled(base::WaitableEvent* event); |
|
71 |
|
72 // MessageLoop::DestructionObserver implementation |
|
73 void WillDestroyCurrentMessageLoop(); |
|
74 |
|
75 typedef void (*LogFunction)(uint16_t type, |
|
76 std::wstring* name, |
|
77 const Message* msg, |
|
78 std::wstring* params); |
|
79 |
|
80 static void SetLoggerFunctions(LogFunction *functions); |
|
81 |
|
82 private: |
|
83 friend struct DefaultSingletonTraits<Logging>; |
|
84 Logging(); |
|
85 |
|
86 std::wstring GetEventName(int browser_pid, bool enabled); |
|
87 void OnSendLogs(); |
|
88 void Log(const LogData& data); |
|
89 |
|
90 void RegisterWaitForEvent(bool enabled); |
|
91 |
|
92 base::WaitableEventWatcher watcher_; |
|
93 |
|
94 scoped_ptr<base::WaitableEvent> logging_event_on_; |
|
95 scoped_ptr<base::WaitableEvent> logging_event_off_; |
|
96 bool enabled_; |
|
97 |
|
98 std::vector<LogData> queued_logs_; |
|
99 bool queue_invoke_later_pending_; |
|
100 |
|
101 Message::Sender* sender_; |
|
102 MessageLoop* main_thread_; |
|
103 |
|
104 Consumer* consumer_; |
|
105 |
|
106 static LogFunction *log_function_mapping_; |
|
107 }; |
|
108 |
|
109 } // namespace IPC |
|
110 |
|
111 #endif // IPC_MESSAGE_LOG_ENABLED |
|
112 |
|
113 #endif // CHROME_COMMON_IPC_LOGGING_H_ |