Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: chrome/browser/media/webrtc_logging_handler_host.h

Issue 1978183003: Refactor WebRtcLoggingHandlerHost in preparation of automatic upload of WebRtcEventLogs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix race when renderer is closing while the log is in STARTING or STOPPING state Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_LOGGING_HANDLER_HOST_H_ 5 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_
6 #define CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_ 6 #define CHROME_BROWSER_MEDIA_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 <string> 12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/callback.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
16 #include "base/memory/shared_memory.h"
17 #include "build/build_config.h" 17 #include "build/build_config.h"
18 #include "chrome/browser/media/rtp_dump_type.h" 18 #include "chrome/browser/media/rtp_dump_type.h"
19 #include "chrome/browser/media/webrtc_rtp_dump_handler.h" 19 #include "chrome/browser/media/webrtc_rtp_dump_handler.h"
20 #include "chrome/common/media/webrtc_logging_message_data.h" 20 #include "chrome/browser/media/webrtc_text_log_handler.h"
21 #include "chrome/common/partial_circular_buffer.h"
22 #include "content/public/browser/browser_message_filter.h" 21 #include "content/public/browser/browser_message_filter.h"
23 #include "content/public/browser/render_process_host.h" 22 #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 23
30 class Profile; 24 class Profile;
31 class WebRtcLogUploader; 25 class WebRtcLogUploader;
32 26
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 27
41 struct WebRtcLogPaths { 28 struct WebRtcLogPaths {
42 base::FilePath log_path; // todo: rename to directory. 29 base::FilePath log_path; // todo: rename to directory.
43 base::FilePath incoming_rtp_dump; 30 base::FilePath incoming_rtp_dump;
44 base::FilePath outgoing_rtp_dump; 31 base::FilePath outgoing_rtp_dump;
45 }; 32 };
46 33
47 class WebRtcLogBuffer { 34 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 35
74 // WebRtcLoggingHandlerHost handles operations regarding the WebRTC logging: 36 // WebRtcLoggingHandlerHost handles operations regarding the WebRTC logging:
75 // - Opens a shared memory buffer that the handler in the render process 37 // - Opens a shared memory buffer that the handler in the render process
76 // writes to. 38 // writes to.
77 // - Writes basic machine info to the log. 39 // - Writes basic machine info to the log.
78 // - Informs the handler in the render process when to stop logging. 40 // - Informs the handler in the render process when to stop logging.
79 // - Closes the shared memory (and thereby discarding it) or triggers uploading 41 // - Closes the shared memory (and thereby discarding it) or triggers uploading
80 // of the log. 42 // of the log.
81 // - Detects when channel, i.e. renderer, is going away and possibly triggers 43 // - Detects when channel, i.e. renderer, is going away and possibly triggers
82 // uploading the log. 44 // uploading the log.
(...skipping 16 matching lines...) Expand all
99 void SetMetaData(std::unique_ptr<MetaDataMap> meta_data, 61 void SetMetaData(std::unique_ptr<MetaDataMap> meta_data,
100 const GenericDoneCallback& callback); 62 const GenericDoneCallback& callback);
101 63
102 // Opens a log and starts logging. Must be called on the IO thread. 64 // Opens a log and starts logging. Must be called on the IO thread.
103 void StartLogging(const GenericDoneCallback& callback); 65 void StartLogging(const GenericDoneCallback& callback);
104 66
105 // Stops logging. Log will remain open until UploadLog or DiscardLog is 67 // Stops logging. Log will remain open until UploadLog or DiscardLog is
106 // called. Must be called on the IO thread. 68 // called. Must be called on the IO thread.
107 void StopLogging(const GenericDoneCallback& callback); 69 void StopLogging(const GenericDoneCallback& callback);
108 70
109 // Uploads the log and the RTP dumps. Discards the local copy. May only be 71 // 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. 72 // be called after text logging has stopped. Must be called on the IO thread.
111 void UploadLog(const UploadDoneCallback& callback); 73 void UploadLog(const UploadDoneCallback& callback);
112 74
113 // Uploads a log that was previously saved via a call to StoreLog(). 75 // Uploads a log that was previously saved via a call to StoreLog().
114 // Otherwise operates in the same way as UploadLog. 76 // Otherwise operates in the same way as UploadLog.
115 void UploadStoredLog(const std::string& log_id, 77 void UploadStoredLog(const std::string& log_id,
116 const UploadDoneCallback& callback); 78 const UploadDoneCallback& callback);
117 79
118 // Called by WebRtcLogUploader when uploading has finished. Must be called on 80 // Called by WebRtcLogUploader when uploading has finished. Must be called on
119 // the IO thread. 81 // the IO thread.
120 void UploadLogDone(); 82 void UploadLogDone();
121 83
122 // Discards the log and the RTP dumps. May only be called after logging has 84 // Discards the log and the RTP dumps. May only be called after logging has
123 // stopped. Must be called on the IO thread. 85 // stopped. Must be called on the IO thread.
124 void DiscardLog(const GenericDoneCallback& callback); 86 void DiscardLog(const GenericDoneCallback& callback);
125 87
126 // Stores the log locally using a hash of log_id + security origin. 88 // Stores the log locally using a hash of log_id + security origin.
127 void StoreLog(const std::string& log_id, const GenericDoneCallback& callback); 89 void StoreLog(const std::string& log_id, const GenericDoneCallback& callback);
128 90
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 91 // 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 92 // for decision making and it's OK if it changes before the execution based
135 // on that decision has finished. 93 // on that decision has finished.
136 void set_upload_log_on_render_close(bool should_upload) { 94 void set_upload_log_on_render_close(bool should_upload) {
137 upload_log_on_render_close_ = should_upload; 95 upload_log_on_render_close_ = should_upload;
138 } 96 }
139 97
140 // Starts dumping the RTP headers for the specified direction. Must be called 98 // 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 99 // 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. 100 // be dumped. |callback| will be called when starting the dump is done.
(...skipping 10 matching lines...) Expand all
153 void StopRtpDump(RtpDumpType type, const GenericDoneCallback& callback); 111 void StopRtpDump(RtpDumpType type, const GenericDoneCallback& callback);
154 112
155 // Called when an RTP packet is sent or received. Must be called on the UI 113 // Called when an RTP packet is sent or received. Must be called on the UI
156 // thread. 114 // thread.
157 void OnRtpPacket(std::unique_ptr<uint8_t[]> packet_header, 115 void OnRtpPacket(std::unique_ptr<uint8_t[]> packet_header,
158 size_t header_length, 116 size_t header_length,
159 size_t packet_length, 117 size_t packet_length,
160 bool incoming); 118 bool incoming);
161 119
162 private: 120 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; 121 friend class content::BrowserThread;
182 friend class base::DeleteHelper<WebRtcLoggingHandlerHost>; 122 friend class base::DeleteHelper<WebRtcLoggingHandlerHost>;
183 123
184 ~WebRtcLoggingHandlerHost() override; 124 ~WebRtcLoggingHandlerHost() override;
185 125
186 // BrowserMessageFilter implementation. 126 // BrowserMessageFilter implementation.
187 void OnChannelClosing() override; 127 void OnChannelClosing() override;
188 void OnDestruct() const override; 128 void OnDestruct() const override;
189 bool OnMessageReceived(const IPC::Message& message) override; 129 bool OnMessageReceived(const IPC::Message& message) override;
190 130
191 // Handles log message requests from renderer process. 131 // Handles log message requests from renderer process.
192 void OnAddLogMessages(const std::vector<WebRtcLoggingMessageData>& messages); 132 void OnAddLogMessages(const std::vector<WebRtcLoggingMessageData>& messages);
193 void OnLoggingStoppedInRenderer(); 133 void OnLoggingStoppedInRenderer();
194 134
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. 135 // Called after stopping RTP dumps.
203 void StoreLogContinue(const std::string& log_id, 136 void StoreLogContinue(const std::string& log_id,
204 const GenericDoneCallback& callback); 137 const GenericDoneCallback& callback);
205 138
206 // Writes a formatted log |message| to the |circular_buffer_|. 139 // Writes a formatted log |message| to the |circular_buffer_|.
207 void LogToCircularBuffer(const std::string& message); 140 void LogToCircularBuffer(const std::string& message);
208 141
209 // Gets the log directory path for |profile_| and ensure it exists. Must be 142 // Gets the log directory path for |profile_| and ensure it exists. Must be
210 // called on the FILE thread. 143 // called on the FILE thread.
211 base::FilePath GetLogDirectoryAndEnsureExists(); 144 base::FilePath GetLogDirectoryAndEnsureExists();
(...skipping 29 matching lines...) Expand all
241 size_t packet_length, 174 size_t packet_length,
242 bool incoming); 175 bool incoming);
243 176
244 bool ReleaseRtpDumps(WebRtcLogPaths* log_paths); 177 bool ReleaseRtpDumps(WebRtcLogPaths* log_paths);
245 178
246 void FireGenericDoneCallback( 179 void FireGenericDoneCallback(
247 const WebRtcLoggingHandlerHost::GenericDoneCallback& callback, 180 const WebRtcLoggingHandlerHost::GenericDoneCallback& callback,
248 bool success, 181 bool success,
249 const std::string& error_message); 182 const std::string& error_message);
250 183
251 std::unique_ptr<WebRtcLogBuffer> log_buffer_; 184 // The render process ID this object belongs to.
185 int render_process_id_;
252 186
253 // The profile associated with our renderer process. 187 // The profile associated with our renderer process.
254 Profile* const profile_; 188 Profile* const profile_;
255 189
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. 190 // Only accessed on the IO thread.
271 bool upload_log_on_render_close_; 191 bool upload_log_on_render_close_;
272 192
273 // This is the handle to be passed to the render process. It's stored so that 193 // 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. 194 // the logging state. It is a scoped_refptr to allow posting tasks.
275 // It's only accessed on the IO thread. 195 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 196
282 // The RTP dump handler responsible for creating the RTP header dump files. 197 // The RTP dump handler responsible for creating the RTP header dump files.
283 std::unique_ptr<WebRtcRtpDumpHandler> rtp_dump_handler_; 198 std::unique_ptr<WebRtcRtpDumpHandler> rtp_dump_handler_;
284 199
285 // The callback to call when StopRtpDump is called. 200 // The callback to call when StopRtpDump is called.
286 content::RenderProcessHost::WebRtcStopRtpDumpCallback stop_rtp_dump_callback_; 201 content::RenderProcessHost::WebRtcStopRtpDumpCallback stop_rtp_dump_callback_;
287 202
288 // A pointer to the log uploader that's shared for all profiles. 203 // A pointer to the log uploader that's shared for all profiles.
289 // Ownership lies with the browser process. 204 // Ownership lies with the browser process.
290 WebRtcLogUploader* const log_uploader_; 205 WebRtcLogUploader* const log_uploader_;
291 206
292 // The render process ID this object belongs to.
293 int render_process_id_;
294 207
295 DISALLOW_COPY_AND_ASSIGN(WebRtcLoggingHandlerHost); 208 DISALLOW_COPY_AND_ASSIGN(WebRtcLoggingHandlerHost);
296 }; 209 };
297 210
298 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_ 211 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698