Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / media / webrtc_logging_handler_host.h
blobe6007773ea084268dee0a755e2f5dedfcd0a1a4b
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_
6 #define CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_
8 #include "base/basictypes.h"
9 #include "base/memory/shared_memory.h"
10 #include "chrome/browser/media/rtp_dump_type.h"
11 #include "chrome/browser/media/webrtc_rtp_dump_handler.h"
12 #include "chrome/common/media/webrtc_logging_message_data.h"
13 #include "chrome/common/partial_circular_buffer.h"
14 #include "content/public/browser/browser_message_filter.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "net/base/net_util.h"
18 namespace net {
19 class URLRequestContextGetter;
20 } // namespace net
22 class Profile;
24 #if defined(OS_ANDROID)
25 const size_t kWebRtcLogSize = 1 * 1024 * 1024; // 1 MB
26 #else
27 const size_t kWebRtcLogSize = 6 * 1024 * 1024; // 6 MB
28 #endif
30 typedef std::map<std::string, std::string> MetaDataMap;
32 struct WebRtcLogPaths {
33 base::FilePath log_path; // todo: rename to directory.
34 base::FilePath incoming_rtp_dump;
35 base::FilePath outgoing_rtp_dump;
38 class WebRtcLogBuffer {
39 public:
40 WebRtcLogBuffer();
41 ~WebRtcLogBuffer();
43 void Log(const std::string& message);
45 // Returns a circular buffer instance for reading the internal log buffer.
46 // Must only be called after the log has been marked as complete
47 // (see SetComplete) and the caller must ensure that the WebRtcLogBuffer
48 // instance remains in scope for the lifetime of the returned circular buffer.
49 PartialCircularBuffer Read();
51 // Switches the buffer to read-only mode, where access to the internal
52 // buffer is allowed from different threads than were used to contribute
53 // to the log. Calls to Log() won't be allowed after calling
54 // SetComplete() and the call to SetComplete() must be done on the same
55 // thread as constructed the buffer and calls Log().
56 void SetComplete();
58 private:
59 base::ThreadChecker thread_checker_;
60 uint8 buffer_[kWebRtcLogSize];
61 PartialCircularBuffer circular_;
62 bool read_only_;
65 // WebRtcLoggingHandlerHost handles operations regarding the WebRTC logging:
66 // - Opens a shared memory buffer that the handler in the render process
67 // writes to.
68 // - Writes basic machine info to the log.
69 // - Informs the handler in the render process when to stop logging.
70 // - Closes the shared memory (and thereby discarding it) or triggers uploading
71 // of the log.
72 // - Detects when channel, i.e. renderer, is going away and possibly triggers
73 // uploading the log.
74 class WebRtcLoggingHandlerHost : public content::BrowserMessageFilter {
75 public:
76 typedef base::Callback<void(bool, const std::string&)> GenericDoneCallback;
77 typedef base::Callback<void(bool, const std::string&, const std::string&)>
78 UploadDoneCallback;
80 explicit WebRtcLoggingHandlerHost(Profile* profile);
82 // Sets meta data that will be uploaded along with the log and also written
83 // in the beginning of the log. Must be called on the IO thread before calling
84 // StartLogging.
85 void SetMetaData(scoped_ptr<MetaDataMap> meta_data,
86 const GenericDoneCallback& callback);
88 // Opens a log and starts logging. Must be called on the IO thread.
89 void StartLogging(const GenericDoneCallback& callback);
91 // Stops logging. Log will remain open until UploadLog or DiscardLog is
92 // called. Must be called on the IO thread.
93 void StopLogging(const GenericDoneCallback& callback);
95 // Uploads the log and the RTP dumps. Discards the local copy. May only be
96 // called after logging has stopped. Must be called on the IO thread.
97 void UploadLog(const UploadDoneCallback& callback);
99 // Uploads a log that was previously saved via a call to StoreLog().
100 // Otherwise operates in the same way as UploadLog.
101 void UploadStoredLog(const std::string& log_id,
102 const UploadDoneCallback& callback);
104 // Called by WebRtcLogUploader when uploading has finished. Must be called on
105 // the IO thread.
106 void UploadLogDone();
108 // Discards the log and the RTP dumps. May only be called after logging has
109 // stopped. Must be called on the IO thread.
110 void DiscardLog(const GenericDoneCallback& callback);
112 // Stores the log locally using a hash of log_id + security origin.
113 void StoreLog(const std::string& log_id, const GenericDoneCallback& callback);
115 // Adds a message to the log.
116 void LogMessage(const std::string& message);
118 // May be called on any thread. |upload_log_on_render_close_| is used
119 // for decision making and it's OK if it changes before the execution based
120 // on that decision has finished.
121 void set_upload_log_on_render_close(bool should_upload) {
122 upload_log_on_render_close_ = should_upload;
125 // Starts dumping the RTP headers for the specified direction. Must be called
126 // on the IO thread. |type| specifies which direction(s) of RTP packets should
127 // be dumped. |callback| will be called when starting the dump is done.
128 // |stop_callback| will be called when StopRtpDump is called.
129 void StartRtpDump(RtpDumpType type,
130 const GenericDoneCallback& callback,
131 const content::RenderProcessHost::WebRtcStopRtpDumpCallback&
132 stop_callback);
134 // Stops dumping the RTP headers for the specified direction. Must be called
135 // on the IO thread. |type| specifies which direction(s) of RTP packet dumping
136 // should be stopped. |callback| will be called when stopping the dump is
137 // done.
138 void StopRtpDump(RtpDumpType type, const GenericDoneCallback& callback);
140 // Called when an RTP packet is sent or received. Must be called on the UI
141 // thread.
142 void OnRtpPacket(scoped_ptr<uint8[]> packet_header,
143 size_t header_length,
144 size_t packet_length,
145 bool incoming);
147 private:
148 // States used for protecting from function calls made at non-allowed points
149 // in time. For example, StartLogging() is only allowed in CLOSED state.
150 // Transitions: SetMetaData(): CLOSED -> CLOSED.
151 // StartLogging(): CLOSED -> STARTING.
152 // Start done: STARTING -> STARTED.
153 // StopLogging(): STARTED -> STOPPING.
154 // Stop done: STOPPING -> STOPPED.
155 // UploadLog(): STOPPED -> UPLOADING.
156 // Upload done: UPLOADING -> CLOSED.
157 // DiscardLog(): STOPPED -> CLOSED.
158 enum LoggingState {
159 CLOSED, // Logging not started, no log in memory.
160 STARTING, // Start logging is in progress.
161 STARTED, // Logging started.
162 STOPPING, // Stop logging is in progress.
163 STOPPED, // Logging has been stopped, log still open in memory.
166 friend class content::BrowserThread;
167 friend class base::DeleteHelper<WebRtcLoggingHandlerHost>;
169 ~WebRtcLoggingHandlerHost() override;
171 // BrowserMessageFilter implementation.
172 void OnChannelClosing() override;
173 void OnDestruct() const override;
174 bool OnMessageReceived(const IPC::Message& message) override;
176 // Handles log message requests from renderer process.
177 void OnAddLogMessages(const std::vector<WebRtcLoggingMessageData>& messages);
178 void OnLoggingStoppedInRenderer();
180 // Handles log message requests from browser process.
181 void AddLogMessageFromBrowser(const WebRtcLoggingMessageData& message);
183 void StartLoggingIfAllowed(const GenericDoneCallback& callback);
184 void DoStartLogging(bool permissions_granted,
185 const GenericDoneCallback& callback);
186 void LogInitialInfoOnFileThread(const GenericDoneCallback& callback);
187 void LogInitialInfoOnIOThread(const net::NetworkInterfaceList& network_list,
188 const GenericDoneCallback& callback);
189 void NotifyLoggingStarted(const GenericDoneCallback& callback);
191 // Called after stopping RTP dumps.
192 void StoreLogContinue(const std::string& log_id,
193 const GenericDoneCallback& callback);
195 // Writes a formatted log |message| to the |circular_buffer_|.
196 void LogToCircularBuffer(const std::string& message);
198 // Gets the log directory path for |profile_| and ensure it exists. Must be
199 // called on the FILE thread.
200 base::FilePath GetLogDirectoryAndEnsureExists();
202 void TriggerUpload(const UploadDoneCallback& callback,
203 const base::FilePath& log_directory);
205 void StoreLogInDirectory(const std::string& log_id,
206 scoped_ptr<WebRtcLogPaths> log_paths,
207 const GenericDoneCallback& done_callback,
208 const base::FilePath& directory);
210 void UploadStoredLogOnFileThread(const std::string& log_id,
211 const UploadDoneCallback& callback);
213 // A helper for TriggerUpload to do the real work.
214 void DoUploadLogAndRtpDumps(const base::FilePath& log_directory,
215 const UploadDoneCallback& callback);
217 // Create the RTP dump handler and start dumping. Must be called after making
218 // sure the log directory exists.
219 void CreateRtpDumpHandlerAndStart(RtpDumpType type,
220 const GenericDoneCallback& callback,
221 const base::FilePath& dump_dir);
223 // A helper for starting RTP dump assuming the RTP dump handler has been
224 // created.
225 void DoStartRtpDump(RtpDumpType type, const GenericDoneCallback& callback);
227 // Adds the packet to the dump on IO thread.
228 void DumpRtpPacketOnIOThread(scoped_ptr<uint8[]> packet_header,
229 size_t header_length,
230 size_t packet_length,
231 bool incoming);
233 bool ReleaseRtpDumps(WebRtcLogPaths* log_paths);
235 scoped_ptr<WebRtcLogBuffer> log_buffer_;
237 // The profile associated with our renderer process.
238 Profile* const profile_;
240 // These are only accessed on the IO thread, except when in STARTING state. In
241 // this state we are protected since entering any function that alters the
242 // state is not allowed.
243 scoped_ptr<MetaDataMap> meta_data_;
245 // These are only accessed on the IO thread.
246 GenericDoneCallback stop_callback_;
248 // Only accessed on the IO thread, except when in STARTING, STOPPING or
249 // UPLOADING state if the action fails and the state must be reset. In these
250 // states however, we are protected since entering any function that alters
251 // the state is not allowed.
252 LoggingState logging_state_;
254 // Only accessed on the IO thread.
255 bool upload_log_on_render_close_;
257 // This is the handle to be passed to the render process. It's stored so that
258 // it doesn't have to be passed on when posting messages between threads.
259 // It's only accessed on the IO thread.
260 base::SharedMemoryHandle foreign_memory_handle_;
262 // The system time in ms when logging is started. Reset when logging_state_
263 // changes to STOPPED.
264 base::Time logging_started_time_;
266 // The RTP dump handler responsible for creating the RTP header dump files.
267 scoped_ptr<WebRtcRtpDumpHandler> rtp_dump_handler_;
269 // The callback to call when StopRtpDump is called.
270 content::RenderProcessHost::WebRtcStopRtpDumpCallback stop_rtp_dump_callback_;
272 DISALLOW_COPY_AND_ASSIGN(WebRtcLoggingHandlerHost);
275 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_