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 "content/public/browser/browser_message_filter.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "net/base/net_util.h"
18 class URLRequestContextGetter
;
21 class PartialCircularBuffer
;
24 typedef std::map
<std::string
, std::string
> MetaDataMap
;
26 // WebRtcLoggingHandlerHost handles operations regarding the WebRTC logging:
27 // - Opens a shared memory buffer that the handler in the render process
29 // - Writes basic machine info to the log.
30 // - Informs the handler in the render process when to stop logging.
31 // - Closes the shared memory (and thereby discarding it) or triggers uploading
33 // - Detects when channel, i.e. renderer, is going away and possibly triggers
35 class WebRtcLoggingHandlerHost
: public content::BrowserMessageFilter
{
37 typedef base::Callback
<void(bool, const std::string
&)> GenericDoneCallback
;
38 typedef base::Callback
<void(bool, const std::string
&, const std::string
&)>
41 explicit WebRtcLoggingHandlerHost(Profile
* profile
);
43 // Sets meta data that will be uploaded along with the log and also written
44 // in the beginning of the log. Must be called on the IO thread before calling
46 void SetMetaData(const MetaDataMap
& meta_data
,
47 const GenericDoneCallback
& callback
);
49 // Opens a log and starts logging. Must be called on the IO thread.
50 void StartLogging(const GenericDoneCallback
& callback
);
52 // Stops logging. Log will remain open until UploadLog or DiscardLog is
53 // called. Must be called on the IO thread.
54 void StopLogging(const GenericDoneCallback
& callback
);
56 // Uploads the log and the RTP dumps. Discards the local copy. May only be
57 // called after logging has stopped. Must be called on the IO thread.
58 void UploadLog(const UploadDoneCallback
& callback
);
60 // Called by WebRtcLogUploader when uploading has finished. Must be called on
64 // Discards the log and the RTP dumps. May only be called after logging has
65 // stopped. Must be called on the IO thread.
66 void DiscardLog(const GenericDoneCallback
& callback
);
68 // Adds a message to the log.
69 void LogMessage(const std::string
& message
);
71 // May be called on any thread. |upload_log_on_render_close_| is used
72 // for decision making and it's OK if it changes before the execution based
73 // on that decision has finished.
74 void set_upload_log_on_render_close(bool should_upload
) {
75 upload_log_on_render_close_
= should_upload
;
78 // Starts dumping the RTP headers for the specified direction. Must be called
79 // on the IO thread. |type| specifies which direction(s) of RTP packets should
80 // be dumped. |callback| will be called when starting the dump is done.
81 // |stop_callback| will be called when StopRtpDump is called.
82 void StartRtpDump(RtpDumpType type
,
83 const GenericDoneCallback
& callback
,
84 const content::RenderProcessHost::WebRtcStopRtpDumpCallback
&
87 // Stops dumping the RTP headers for the specified direction. Must be called
88 // on the IO thread. |type| specifies which direction(s) of RTP packet dumping
89 // should be stopped. |callback| will be called when stopping the dump is
91 void StopRtpDump(RtpDumpType type
, const GenericDoneCallback
& callback
);
93 // Called when an RTP packet is sent or received. Must be called on the UI
95 void OnRtpPacket(scoped_ptr
<uint8
[]> packet_header
,
101 // States used for protecting from function calls made at non-allowed points
102 // in time. For example, StartLogging() is only allowed in CLOSED state.
103 // Transitions: SetMetaData(): CLOSED -> CLOSED.
104 // StartLogging(): CLOSED -> STARTING.
105 // Start done: STARTING -> STARTED.
106 // StopLogging(): STARTED -> STOPPING.
107 // Stop done: STOPPING -> STOPPED.
108 // UploadLog(): STOPPED -> UPLOADING.
109 // Upload done: UPLOADING -> CLOSED.
110 // DiscardLog(): STOPPED -> CLOSED.
112 CLOSED
, // Logging not started, no log in memory.
113 STARTING
, // Start logging is in progress.
114 STARTED
, // Logging started.
115 STOPPING
, // Stop logging is in progress.
116 STOPPED
, // Logging has been stopped, log still open in memory.
117 UPLOADING
// Uploading log is in progress.
120 friend class content::BrowserThread
;
121 friend class base::DeleteHelper
<WebRtcLoggingHandlerHost
>;
123 ~WebRtcLoggingHandlerHost() override
;
125 // BrowserMessageFilter implementation.
126 void OnChannelClosing() override
;
127 void OnDestruct() const override
;
128 bool OnMessageReceived(const IPC::Message
& message
) override
;
130 // Handles log message requests from renderer process.
131 void OnAddLogMessages(const std::vector
<WebRtcLoggingMessageData
>& messages
);
132 void OnLoggingStoppedInRenderer();
134 // Handles log message requests from browser process.
135 void AddLogMessageFromBrowser(const WebRtcLoggingMessageData
& message
);
137 void StartLoggingIfAllowed();
138 void DoStartLogging();
139 void LogInitialInfoOnFileThread();
140 void LogInitialInfoOnIOThread(const net::NetworkInterfaceList
& network_list
);
141 void NotifyLoggingStarted();
143 // Writes a formatted log |message| to the |circular_buffer_|.
144 void LogToCircularBuffer(const std::string
& message
);
146 // Gets the log directory path for |profile_| and ensure it exists. Must be
147 // called on the FILE thread.
148 base::FilePath
GetLogDirectoryAndEnsureExists();
150 void TriggerUpload(const base::FilePath
& log_directory
);
152 // A helper for TriggerUpload to do the real work.
153 void DoUploadLogAndRtpDumps(const base::FilePath
& log_directory
);
155 void FireGenericDoneCallback(GenericDoneCallback
* callback
,
157 const std::string
& error_message
);
159 // Create the RTP dump handler and start dumping. Must be called after making
160 // sure the log directory exists.
161 void CreateRtpDumpHandlerAndStart(RtpDumpType type
,
162 GenericDoneCallback callback
,
163 const base::FilePath
& dump_dir
);
165 // A helper for starting RTP dump assuming the RTP dump handler has been
167 void DoStartRtpDump(RtpDumpType type
, GenericDoneCallback
* callback
);
169 // Adds the packet to the dump on IO thread.
170 void DumpRtpPacketOnIOThread(scoped_ptr
<uint8
[]> packet_header
,
171 size_t header_length
,
172 size_t packet_length
,
175 scoped_ptr
<unsigned char[]> log_buffer_
;
176 scoped_ptr
<PartialCircularBuffer
> circular_buffer_
;
178 // The profile associated with our renderer process.
181 // These are only accessed on the IO thread, except when in STARTING state. In
182 // this state we are protected since entering any function that alters the
183 // state is not allowed.
184 MetaDataMap meta_data_
;
186 // These are only accessed on the IO thread.
187 GenericDoneCallback start_callback_
;
188 GenericDoneCallback stop_callback_
;
189 UploadDoneCallback upload_callback_
;
191 // Only accessed on the IO thread, except when in STARTING, STOPPING or
192 // UPLOADING state if the action fails and the state must be reset. In these
193 // states however, we are protected since entering any function that alters
194 // the state is not allowed.
195 LoggingState logging_state_
;
197 // Only accessed on the IO thread.
198 bool upload_log_on_render_close_
;
200 // This is the handle to be passed to the render process. It's stored so that
201 // it doesn't have to be passed on when posting messages between threads.
202 // It's only accessed on the IO thread.
203 base::SharedMemoryHandle foreign_memory_handle_
;
205 // The system time in ms when logging is started. Reset when logging_state_
206 // changes to STOPPED.
207 base::Time logging_started_time_
;
209 // The RTP dump handler responsible for creating the RTP header dump files.
210 scoped_ptr
<WebRtcRtpDumpHandler
> rtp_dump_handler_
;
212 // The callback to call when StopRtpDump is called.
213 content::RenderProcessHost::WebRtcStopRtpDumpCallback stop_rtp_dump_callback_
;
215 DISALLOW_COPY_AND_ASSIGN(WebRtcLoggingHandlerHost
);
218 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_