OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_LOGGING_HANDLER_HOST_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_LOGGING_HANDLER_HOST_H_ |
6 #define CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_LOGGING_HANDLER_HOST_H_ | 6 #define CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_LOGGING_HANDLER_HOST_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <map> | 11 #include <map> |
| 12 #include <memory> |
12 #include <string> | 13 #include <string> |
13 #include <vector> | 14 #include <vector> |
14 | 15 |
| 16 #include "base/callback.h" |
15 #include "base/macros.h" | 17 #include "base/macros.h" |
16 #include "base/memory/shared_memory.h" | |
17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
18 #include "chrome/browser/media/webrtc/rtp_dump_type.h" | 19 #include "chrome/browser/media/webrtc/rtp_dump_type.h" |
19 #include "chrome/browser/media/webrtc/webrtc_rtp_dump_handler.h" | 20 #include "chrome/browser/media/webrtc/webrtc_rtp_dump_handler.h" |
20 #include "chrome/common/media/webrtc_logging_message_data.h" | 21 #include "chrome/browser/media/webrtc/webrtc_text_log_handler.h" |
21 #include "chrome/common/partial_circular_buffer.h" | |
22 #include "content/public/browser/browser_message_filter.h" | 22 #include "content/public/browser/browser_message_filter.h" |
23 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
24 #include "net/base/network_interfaces.h" | |
25 | |
26 namespace net { | |
27 class URLRequestContextGetter; | |
28 } // namespace net | |
29 | 24 |
30 class Profile; | 25 class Profile; |
31 class WebRtcLogUploader; | 26 class WebRtcLogUploader; |
32 | 27 |
33 #if defined(OS_ANDROID) | |
34 const size_t kWebRtcLogSize = 1 * 1024 * 1024; // 1 MB | |
35 #else | |
36 const size_t kWebRtcLogSize = 6 * 1024 * 1024; // 6 MB | |
37 #endif | |
38 | |
39 typedef std::map<std::string, std::string> MetaDataMap; | |
40 | 28 |
41 struct WebRtcLogPaths { | 29 struct WebRtcLogPaths { |
42 base::FilePath log_path; // todo: rename to directory. | 30 base::FilePath log_path; // todo: rename to directory. |
43 base::FilePath incoming_rtp_dump; | 31 base::FilePath incoming_rtp_dump; |
44 base::FilePath outgoing_rtp_dump; | 32 base::FilePath outgoing_rtp_dump; |
45 }; | 33 }; |
46 | 34 |
47 class WebRtcLogBuffer { | 35 typedef std::map<std::string, std::string> MetaDataMap; |
48 public: | |
49 WebRtcLogBuffer(); | |
50 ~WebRtcLogBuffer(); | |
51 | |
52 void Log(const std::string& message); | |
53 | |
54 // Returns a circular buffer instance for reading the internal log buffer. | |
55 // Must only be called after the log has been marked as complete | |
56 // (see SetComplete) and the caller must ensure that the WebRtcLogBuffer | |
57 // instance remains in scope for the lifetime of the returned circular buffer. | |
58 PartialCircularBuffer Read(); | |
59 | |
60 // Switches the buffer to read-only mode, where access to the internal | |
61 // buffer is allowed from different threads than were used to contribute | |
62 // to the log. Calls to Log() won't be allowed after calling | |
63 // SetComplete() and the call to SetComplete() must be done on the same | |
64 // thread as constructed the buffer and calls Log(). | |
65 void SetComplete(); | |
66 | |
67 private: | |
68 base::ThreadChecker thread_checker_; | |
69 uint8_t buffer_[kWebRtcLogSize]; | |
70 PartialCircularBuffer circular_; | |
71 bool read_only_; | |
72 }; | |
73 | 36 |
74 // WebRtcLoggingHandlerHost handles operations regarding the WebRTC logging: | 37 // WebRtcLoggingHandlerHost handles operations regarding the WebRTC logging: |
75 // - Opens a shared memory buffer that the handler in the render process | 38 // - Opens a shared memory buffer that the handler in the render process |
76 // writes to. | 39 // writes to. |
77 // - Writes basic machine info to the log. | 40 // - Writes basic machine info to the log. |
78 // - Informs the handler in the render process when to stop logging. | 41 // - Informs the handler in the render process when to stop logging. |
79 // - Closes the shared memory (and thereby discarding it) or triggers uploading | 42 // - Closes the shared memory (and thereby discarding it) or triggers uploading |
80 // of the log. | 43 // of the log. |
81 // - Detects when channel, i.e. renderer, is going away and possibly triggers | 44 // - Detects when channel, i.e. renderer, is going away and possibly triggers |
82 // uploading the log. | 45 // uploading the log. |
(...skipping 16 matching lines...) Expand all Loading... |
99 void SetMetaData(std::unique_ptr<MetaDataMap> meta_data, | 62 void SetMetaData(std::unique_ptr<MetaDataMap> meta_data, |
100 const GenericDoneCallback& callback); | 63 const GenericDoneCallback& callback); |
101 | 64 |
102 // Opens a log and starts logging. Must be called on the IO thread. | 65 // Opens a log and starts logging. Must be called on the IO thread. |
103 void StartLogging(const GenericDoneCallback& callback); | 66 void StartLogging(const GenericDoneCallback& callback); |
104 | 67 |
105 // Stops logging. Log will remain open until UploadLog or DiscardLog is | 68 // Stops logging. Log will remain open until UploadLog or DiscardLog is |
106 // called. Must be called on the IO thread. | 69 // called. Must be called on the IO thread. |
107 void StopLogging(const GenericDoneCallback& callback); | 70 void StopLogging(const GenericDoneCallback& callback); |
108 | 71 |
109 // Uploads the log and the RTP dumps. Discards the local copy. May only be | 72 // Uploads the text log and the RTP dumps. Discards the local copy. May only |
110 // called after logging has stopped. Must be called on the IO thread. | 73 // be called after text logging has stopped. Must be called on the IO thread. |
111 void UploadLog(const UploadDoneCallback& callback); | 74 void UploadLog(const UploadDoneCallback& callback); |
112 | 75 |
113 // Uploads a log that was previously saved via a call to StoreLog(). | 76 // Uploads a log that was previously saved via a call to StoreLog(). |
114 // Otherwise operates in the same way as UploadLog. | 77 // Otherwise operates in the same way as UploadLog. |
115 void UploadStoredLog(const std::string& log_id, | 78 void UploadStoredLog(const std::string& log_id, |
116 const UploadDoneCallback& callback); | 79 const UploadDoneCallback& callback); |
117 | 80 |
118 // Called by WebRtcLogUploader when uploading has finished. Must be called on | 81 // Called by WebRtcLogUploader when uploading has finished. Must be called on |
119 // the IO thread. | 82 // the IO thread. |
120 void UploadLogDone(); | 83 void UploadLogDone(); |
121 | 84 |
122 // Discards the log and the RTP dumps. May only be called after logging has | 85 // Discards the log and the RTP dumps. May only be called after logging has |
123 // stopped. Must be called on the IO thread. | 86 // stopped. Must be called on the IO thread. |
124 void DiscardLog(const GenericDoneCallback& callback); | 87 void DiscardLog(const GenericDoneCallback& callback); |
125 | 88 |
126 // Stores the log locally using a hash of log_id + security origin. | 89 // Stores the log locally using a hash of log_id + security origin. |
127 void StoreLog(const std::string& log_id, const GenericDoneCallback& callback); | 90 void StoreLog(const std::string& log_id, const GenericDoneCallback& callback); |
128 | 91 |
129 // Adds a message to the log. | |
130 // This method must be called on the IO thread. | |
131 void LogMessage(const std::string& message); | |
132 | |
133 // May be called on any thread. |upload_log_on_render_close_| is used | 92 // May be called on any thread. |upload_log_on_render_close_| is used |
134 // for decision making and it's OK if it changes before the execution based | 93 // for decision making and it's OK if it changes before the execution based |
135 // on that decision has finished. | 94 // on that decision has finished. |
136 void set_upload_log_on_render_close(bool should_upload) { | 95 void set_upload_log_on_render_close(bool should_upload) { |
137 upload_log_on_render_close_ = should_upload; | 96 upload_log_on_render_close_ = should_upload; |
138 } | 97 } |
139 | 98 |
140 // Starts dumping the RTP headers for the specified direction. Must be called | 99 // Starts dumping the RTP headers for the specified direction. Must be called |
141 // on the IO thread. |type| specifies which direction(s) of RTP packets should | 100 // on the IO thread. |type| specifies which direction(s) of RTP packets should |
142 // be dumped. |callback| will be called when starting the dump is done. | 101 // be dumped. |callback| will be called when starting the dump is done. |
(...skipping 10 matching lines...) Expand all Loading... |
153 void StopRtpDump(RtpDumpType type, const GenericDoneCallback& callback); | 112 void StopRtpDump(RtpDumpType type, const GenericDoneCallback& callback); |
154 | 113 |
155 // Called when an RTP packet is sent or received. Must be called on the UI | 114 // Called when an RTP packet is sent or received. Must be called on the UI |
156 // thread. | 115 // thread. |
157 void OnRtpPacket(std::unique_ptr<uint8_t[]> packet_header, | 116 void OnRtpPacket(std::unique_ptr<uint8_t[]> packet_header, |
158 size_t header_length, | 117 size_t header_length, |
159 size_t packet_length, | 118 size_t packet_length, |
160 bool incoming); | 119 bool incoming); |
161 | 120 |
162 private: | 121 private: |
163 // States used for protecting from function calls made at non-allowed points | |
164 // in time. For example, StartLogging() is only allowed in CLOSED state. | |
165 // Transitions: SetMetaData(): CLOSED -> CLOSED. | |
166 // StartLogging(): CLOSED -> STARTING. | |
167 // Start done: STARTING -> STARTED. | |
168 // StopLogging(): STARTED -> STOPPING. | |
169 // Stop done: STOPPING -> STOPPED. | |
170 // UploadLog(): STOPPED -> UPLOADING. | |
171 // Upload done: UPLOADING -> CLOSED. | |
172 // DiscardLog(): STOPPED -> CLOSED. | |
173 enum LoggingState { | |
174 CLOSED, // Logging not started, no log in memory. | |
175 STARTING, // Start logging is in progress. | |
176 STARTED, // Logging started. | |
177 STOPPING, // Stop logging is in progress. | |
178 STOPPED, // Logging has been stopped, log still open in memory. | |
179 }; | |
180 | |
181 friend class content::BrowserThread; | 122 friend class content::BrowserThread; |
182 friend class base::DeleteHelper<WebRtcLoggingHandlerHost>; | 123 friend class base::DeleteHelper<WebRtcLoggingHandlerHost>; |
183 | 124 |
184 ~WebRtcLoggingHandlerHost() override; | 125 ~WebRtcLoggingHandlerHost() override; |
185 | 126 |
186 // BrowserMessageFilter implementation. | 127 // BrowserMessageFilter implementation. |
187 void OnChannelClosing() override; | 128 void OnChannelClosing() override; |
188 void OnDestruct() const override; | 129 void OnDestruct() const override; |
189 bool OnMessageReceived(const IPC::Message& message) override; | 130 bool OnMessageReceived(const IPC::Message& message) override; |
190 | 131 |
191 // Handles log message requests from renderer process. | 132 // Handles log message requests from renderer process. |
192 void OnAddLogMessages(const std::vector<WebRtcLoggingMessageData>& messages); | 133 void OnAddLogMessages(const std::vector<WebRtcLoggingMessageData>& messages); |
193 void OnLoggingStoppedInRenderer(); | 134 void OnLoggingStoppedInRenderer(); |
194 | 135 |
195 void LogInitialInfoOnFileThread(const GenericDoneCallback& callback); | |
196 void LogInitialInfoOnIOThread(const net::NetworkInterfaceList& network_list, | |
197 const GenericDoneCallback& callback); | |
198 | |
199 void EnableBrowserProcessLoggingOnUIThread(); | |
200 void DisableBrowserProcessLoggingOnUIThread(); | |
201 | |
202 // Called after stopping RTP dumps. | 136 // Called after stopping RTP dumps. |
203 void StoreLogContinue(const std::string& log_id, | 137 void StoreLogContinue(const std::string& log_id, |
204 const GenericDoneCallback& callback); | 138 const GenericDoneCallback& callback); |
205 | 139 |
206 // Writes a formatted log |message| to the |circular_buffer_|. | 140 // Writes a formatted log |message| to the |circular_buffer_|. |
207 void LogToCircularBuffer(const std::string& message); | 141 void LogToCircularBuffer(const std::string& message); |
208 | 142 |
209 // Gets the log directory path for |profile_| and ensure it exists. Must be | 143 // Gets the log directory path for |profile_| and ensure it exists. Must be |
210 // called on the FILE thread. | 144 // called on the FILE thread. |
211 base::FilePath GetLogDirectoryAndEnsureExists(); | 145 base::FilePath GetLogDirectoryAndEnsureExists(); |
(...skipping 29 matching lines...) Expand all Loading... |
241 size_t packet_length, | 175 size_t packet_length, |
242 bool incoming); | 176 bool incoming); |
243 | 177 |
244 bool ReleaseRtpDumps(WebRtcLogPaths* log_paths); | 178 bool ReleaseRtpDumps(WebRtcLogPaths* log_paths); |
245 | 179 |
246 void FireGenericDoneCallback( | 180 void FireGenericDoneCallback( |
247 const WebRtcLoggingHandlerHost::GenericDoneCallback& callback, | 181 const WebRtcLoggingHandlerHost::GenericDoneCallback& callback, |
248 bool success, | 182 bool success, |
249 const std::string& error_message); | 183 const std::string& error_message); |
250 | 184 |
251 std::unique_ptr<WebRtcLogBuffer> log_buffer_; | 185 // The render process ID this object belongs to. |
| 186 int render_process_id_; |
252 | 187 |
253 // The profile associated with our renderer process. | 188 // The profile associated with our renderer process. |
254 Profile* const profile_; | 189 Profile* const profile_; |
255 | 190 |
256 // These are only accessed on the IO thread, except when in STARTING state. In | |
257 // this state we are protected since entering any function that alters the | |
258 // state is not allowed. | |
259 std::unique_ptr<MetaDataMap> meta_data_; | |
260 | |
261 // These are only accessed on the IO thread. | |
262 GenericDoneCallback stop_callback_; | |
263 | |
264 // Only accessed on the IO thread, except when in STARTING, STOPPING or | |
265 // UPLOADING state if the action fails and the state must be reset. In these | |
266 // states however, we are protected since entering any function that alters | |
267 // the state is not allowed. | |
268 LoggingState logging_state_; | |
269 | |
270 // Only accessed on the IO thread. | 191 // Only accessed on the IO thread. |
271 bool upload_log_on_render_close_; | 192 bool upload_log_on_render_close_; |
272 | 193 |
273 // This is the handle to be passed to the render process. It's stored so that | 194 // The text log handler owns the WebRtcLogBuffer object and keeps track of |
274 // it doesn't have to be passed on when posting messages between threads. | 195 // the logging state. It is a scoped_refptr to allow posting tasks. |
275 // It's only accessed on the IO thread. | 196 scoped_refptr<WebRtcTextLogHandler> text_log_handler_; |
276 base::SharedMemoryHandle foreign_memory_handle_; | |
277 | |
278 // The system time in ms when logging is started. Reset when logging_state_ | |
279 // changes to STOPPED. | |
280 base::Time logging_started_time_; | |
281 | 197 |
282 // The RTP dump handler responsible for creating the RTP header dump files. | 198 // The RTP dump handler responsible for creating the RTP header dump files. |
283 std::unique_ptr<WebRtcRtpDumpHandler> rtp_dump_handler_; | 199 std::unique_ptr<WebRtcRtpDumpHandler> rtp_dump_handler_; |
284 | 200 |
285 // The callback to call when StopRtpDump is called. | 201 // The callback to call when StopRtpDump is called. |
286 content::RenderProcessHost::WebRtcStopRtpDumpCallback stop_rtp_dump_callback_; | 202 content::RenderProcessHost::WebRtcStopRtpDumpCallback stop_rtp_dump_callback_; |
287 | 203 |
288 // A pointer to the log uploader that's shared for all profiles. | 204 // A pointer to the log uploader that's shared for all profiles. |
289 // Ownership lies with the browser process. | 205 // Ownership lies with the browser process. |
290 WebRtcLogUploader* const log_uploader_; | 206 WebRtcLogUploader* const log_uploader_; |
291 | 207 |
292 // The render process ID this object belongs to. | |
293 int render_process_id_; | |
294 | 208 |
295 DISALLOW_COPY_AND_ASSIGN(WebRtcLoggingHandlerHost); | 209 DISALLOW_COPY_AND_ASSIGN(WebRtcLoggingHandlerHost); |
296 }; | 210 }; |
297 | 211 |
298 #endif // CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_LOGGING_HANDLER_HOST_H_ | 212 #endif // CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_LOGGING_HANDLER_HOST_H_ |
OLD | NEW |