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"
17 #include "net/base/network_interfaces.h"
20 class URLRequestContextGetter
;
25 #if defined(OS_ANDROID)
26 const size_t kWebRtcLogSize
= 1 * 1024 * 1024; // 1 MB
28 const size_t kWebRtcLogSize
= 6 * 1024 * 1024; // 6 MB
31 typedef std::map
<std::string
, std::string
> MetaDataMap
;
33 struct WebRtcLogPaths
{
34 base::FilePath log_path
; // todo: rename to directory.
35 base::FilePath incoming_rtp_dump
;
36 base::FilePath outgoing_rtp_dump
;
39 class WebRtcLogBuffer
{
44 void Log(const std::string
& message
);
46 // Returns a circular buffer instance for reading the internal log buffer.
47 // Must only be called after the log has been marked as complete
48 // (see SetComplete) and the caller must ensure that the WebRtcLogBuffer
49 // instance remains in scope for the lifetime of the returned circular buffer.
50 PartialCircularBuffer
Read();
52 // Switches the buffer to read-only mode, where access to the internal
53 // buffer is allowed from different threads than were used to contribute
54 // to the log. Calls to Log() won't be allowed after calling
55 // SetComplete() and the call to SetComplete() must be done on the same
56 // thread as constructed the buffer and calls Log().
60 base::ThreadChecker thread_checker_
;
61 uint8 buffer_
[kWebRtcLogSize
];
62 PartialCircularBuffer circular_
;
66 // WebRtcLoggingHandlerHost handles operations regarding the WebRTC logging:
67 // - Opens a shared memory buffer that the handler in the render process
69 // - Writes basic machine info to the log.
70 // - Informs the handler in the render process when to stop logging.
71 // - Closes the shared memory (and thereby discarding it) or triggers uploading
73 // - Detects when channel, i.e. renderer, is going away and possibly triggers
75 class WebRtcLoggingHandlerHost
: public content::BrowserMessageFilter
{
77 typedef base::Callback
<void(bool, const std::string
&)> GenericDoneCallback
;
78 typedef base::Callback
<void(bool, const std::string
&, const std::string
&)>
81 explicit WebRtcLoggingHandlerHost(Profile
* profile
);
83 // Sets meta data that will be uploaded along with the log and also written
84 // in the beginning of the log. Must be called on the IO thread before calling
86 void SetMetaData(scoped_ptr
<MetaDataMap
> meta_data
,
87 const GenericDoneCallback
& callback
);
89 // Opens a log and starts logging. Must be called on the IO thread.
90 void StartLogging(const GenericDoneCallback
& callback
);
92 // Stops logging. Log will remain open until UploadLog or DiscardLog is
93 // called. Must be called on the IO thread.
94 void StopLogging(const GenericDoneCallback
& callback
);
96 // Uploads the log and the RTP dumps. Discards the local copy. May only be
97 // called after logging has stopped. Must be called on the IO thread.
98 void UploadLog(const UploadDoneCallback
& callback
);
100 // Uploads a log that was previously saved via a call to StoreLog().
101 // Otherwise operates in the same way as UploadLog.
102 void UploadStoredLog(const std::string
& log_id
,
103 const UploadDoneCallback
& callback
);
105 // Called by WebRtcLogUploader when uploading has finished. Must be called on
107 void UploadLogDone();
109 // Discards the log and the RTP dumps. May only be called after logging has
110 // stopped. Must be called on the IO thread.
111 void DiscardLog(const GenericDoneCallback
& callback
);
113 // Stores the log locally using a hash of log_id + security origin.
114 void StoreLog(const std::string
& log_id
, const GenericDoneCallback
& callback
);
116 // Adds a message to the log.
117 void LogMessage(const std::string
& message
);
119 // May be called on any thread. |upload_log_on_render_close_| is used
120 // for decision making and it's OK if it changes before the execution based
121 // on that decision has finished.
122 void set_upload_log_on_render_close(bool should_upload
) {
123 upload_log_on_render_close_
= should_upload
;
126 // Starts dumping the RTP headers for the specified direction. Must be called
127 // on the IO thread. |type| specifies which direction(s) of RTP packets should
128 // be dumped. |callback| will be called when starting the dump is done.
129 // |stop_callback| will be called when StopRtpDump is called.
130 void StartRtpDump(RtpDumpType type
,
131 const GenericDoneCallback
& callback
,
132 const content::RenderProcessHost::WebRtcStopRtpDumpCallback
&
135 // Stops dumping the RTP headers for the specified direction. Must be called
136 // on the IO thread. |type| specifies which direction(s) of RTP packet dumping
137 // should be stopped. |callback| will be called when stopping the dump is
139 void StopRtpDump(RtpDumpType type
, const GenericDoneCallback
& callback
);
141 // Called when an RTP packet is sent or received. Must be called on the UI
143 void OnRtpPacket(scoped_ptr
<uint8
[]> packet_header
,
144 size_t header_length
,
145 size_t packet_length
,
149 // States used for protecting from function calls made at non-allowed points
150 // in time. For example, StartLogging() is only allowed in CLOSED state.
151 // Transitions: SetMetaData(): CLOSED -> CLOSED.
152 // StartLogging(): CLOSED -> STARTING.
153 // Start done: STARTING -> STARTED.
154 // StopLogging(): STARTED -> STOPPING.
155 // Stop done: STOPPING -> STOPPED.
156 // UploadLog(): STOPPED -> UPLOADING.
157 // Upload done: UPLOADING -> CLOSED.
158 // DiscardLog(): STOPPED -> CLOSED.
160 CLOSED
, // Logging not started, no log in memory.
161 STARTING
, // Start logging is in progress.
162 STARTED
, // Logging started.
163 STOPPING
, // Stop logging is in progress.
164 STOPPED
, // Logging has been stopped, log still open in memory.
167 friend class content::BrowserThread
;
168 friend class base::DeleteHelper
<WebRtcLoggingHandlerHost
>;
170 ~WebRtcLoggingHandlerHost() override
;
172 // BrowserMessageFilter implementation.
173 void OnChannelClosing() override
;
174 void OnDestruct() const override
;
175 bool OnMessageReceived(const IPC::Message
& message
) override
;
177 // Handles log message requests from renderer process.
178 void OnAddLogMessages(const std::vector
<WebRtcLoggingMessageData
>& messages
);
179 void OnLoggingStoppedInRenderer();
181 // Handles log message requests from browser process.
182 void AddLogMessageFromBrowser(const WebRtcLoggingMessageData
& message
);
184 void StartLoggingIfAllowed(const GenericDoneCallback
& callback
);
185 void DoStartLogging(bool permissions_granted
,
186 const GenericDoneCallback
& callback
);
187 void LogInitialInfoOnFileThread(const GenericDoneCallback
& callback
);
188 void LogInitialInfoOnIOThread(const net::NetworkInterfaceList
& network_list
,
189 const GenericDoneCallback
& callback
);
190 void NotifyLoggingStarted(const GenericDoneCallback
& callback
);
192 // Called after stopping RTP dumps.
193 void StoreLogContinue(const std::string
& log_id
,
194 const GenericDoneCallback
& callback
);
196 // Writes a formatted log |message| to the |circular_buffer_|.
197 void LogToCircularBuffer(const std::string
& message
);
199 // Gets the log directory path for |profile_| and ensure it exists. Must be
200 // called on the FILE thread.
201 base::FilePath
GetLogDirectoryAndEnsureExists();
203 void TriggerUpload(const UploadDoneCallback
& callback
,
204 const base::FilePath
& log_directory
);
206 void StoreLogInDirectory(const std::string
& log_id
,
207 scoped_ptr
<WebRtcLogPaths
> log_paths
,
208 const GenericDoneCallback
& done_callback
,
209 const base::FilePath
& directory
);
211 void UploadStoredLogOnFileThread(const std::string
& log_id
,
212 const UploadDoneCallback
& callback
);
214 // A helper for TriggerUpload to do the real work.
215 void DoUploadLogAndRtpDumps(const base::FilePath
& log_directory
,
216 const UploadDoneCallback
& callback
);
218 // Create the RTP dump handler and start dumping. Must be called after making
219 // sure the log directory exists.
220 void CreateRtpDumpHandlerAndStart(RtpDumpType type
,
221 const GenericDoneCallback
& callback
,
222 const base::FilePath
& dump_dir
);
224 // A helper for starting RTP dump assuming the RTP dump handler has been
226 void DoStartRtpDump(RtpDumpType type
, const GenericDoneCallback
& callback
);
228 // Adds the packet to the dump on IO thread.
229 void DumpRtpPacketOnIOThread(scoped_ptr
<uint8
[]> packet_header
,
230 size_t header_length
,
231 size_t packet_length
,
234 bool ReleaseRtpDumps(WebRtcLogPaths
* log_paths
);
236 scoped_ptr
<WebRtcLogBuffer
> log_buffer_
;
238 // The profile associated with our renderer process.
239 Profile
* const profile_
;
241 // These are only accessed on the IO thread, except when in STARTING state. In
242 // this state we are protected since entering any function that alters the
243 // state is not allowed.
244 scoped_ptr
<MetaDataMap
> meta_data_
;
246 // These are only accessed on the IO thread.
247 GenericDoneCallback stop_callback_
;
249 // Only accessed on the IO thread, except when in STARTING, STOPPING or
250 // UPLOADING state if the action fails and the state must be reset. In these
251 // states however, we are protected since entering any function that alters
252 // the state is not allowed.
253 LoggingState logging_state_
;
255 // Only accessed on the IO thread.
256 bool upload_log_on_render_close_
;
258 // This is the handle to be passed to the render process. It's stored so that
259 // it doesn't have to be passed on when posting messages between threads.
260 // It's only accessed on the IO thread.
261 base::SharedMemoryHandle foreign_memory_handle_
;
263 // The system time in ms when logging is started. Reset when logging_state_
264 // changes to STOPPED.
265 base::Time logging_started_time_
;
267 // The RTP dump handler responsible for creating the RTP header dump files.
268 scoped_ptr
<WebRtcRtpDumpHandler
> rtp_dump_handler_
;
270 // The callback to call when StopRtpDump is called.
271 content::RenderProcessHost::WebRtcStopRtpDumpCallback stop_rtp_dump_callback_
;
273 DISALLOW_COPY_AND_ASSIGN(WebRtcLoggingHandlerHost
);
276 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_