| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_ | |
| 6 #define CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include <map> | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/shared_memory.h" | |
| 17 #include "build/build_config.h" | |
| 18 #include "chrome/browser/media/rtp_dump_type.h" | |
| 19 #include "chrome/browser/media/webrtc_rtp_dump_handler.h" | |
| 20 #include "chrome/common/media/webrtc_logging_message_data.h" | |
| 21 #include "chrome/common/partial_circular_buffer.h" | |
| 22 #include "content/public/browser/browser_message_filter.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 | |
| 30 class Profile; | |
| 31 class WebRtcLogUploader; | |
| 32 | |
| 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 | |
| 41 struct WebRtcLogPaths { | |
| 42 base::FilePath log_path; // todo: rename to directory. | |
| 43 base::FilePath incoming_rtp_dump; | |
| 44 base::FilePath outgoing_rtp_dump; | |
| 45 }; | |
| 46 | |
| 47 class WebRtcLogBuffer { | |
| 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 | |
| 74 // WebRtcLoggingHandlerHost handles operations regarding the WebRTC logging: | |
| 75 // - Opens a shared memory buffer that the handler in the render process | |
| 76 // writes to. | |
| 77 // - Writes basic machine info to the log. | |
| 78 // - Informs the handler in the render process when to stop logging. | |
| 79 // - Closes the shared memory (and thereby discarding it) or triggers uploading | |
| 80 // of the log. | |
| 81 // - Detects when channel, i.e. renderer, is going away and possibly triggers | |
| 82 // uploading the log. | |
| 83 class WebRtcLoggingHandlerHost : public content::BrowserMessageFilter { | |
| 84 public: | |
| 85 typedef base::Callback<void(bool, const std::string&)> GenericDoneCallback; | |
| 86 typedef base::Callback<void(bool, const std::string&, const std::string&)> | |
| 87 UploadDoneCallback; | |
| 88 | |
| 89 // Key used to attach the handler to the RenderProcessHost. | |
| 90 static const char kWebRtcLoggingHandlerHostKey[]; | |
| 91 | |
| 92 WebRtcLoggingHandlerHost(int render_process_id, | |
| 93 Profile* profile, | |
| 94 WebRtcLogUploader* log_uploader); | |
| 95 | |
| 96 // Sets meta data that will be uploaded along with the log and also written | |
| 97 // in the beginning of the log. Must be called on the IO thread before calling | |
| 98 // StartLogging. | |
| 99 void SetMetaData(std::unique_ptr<MetaDataMap> meta_data, | |
| 100 const GenericDoneCallback& callback); | |
| 101 | |
| 102 // Opens a log and starts logging. Must be called on the IO thread. | |
| 103 void StartLogging(const GenericDoneCallback& callback); | |
| 104 | |
| 105 // Stops logging. Log will remain open until UploadLog or DiscardLog is | |
| 106 // called. Must be called on the IO thread. | |
| 107 void StopLogging(const GenericDoneCallback& callback); | |
| 108 | |
| 109 // Uploads the log and the RTP dumps. Discards the local copy. May only be | |
| 110 // called after logging has stopped. Must be called on the IO thread. | |
| 111 void UploadLog(const UploadDoneCallback& callback); | |
| 112 | |
| 113 // Uploads a log that was previously saved via a call to StoreLog(). | |
| 114 // Otherwise operates in the same way as UploadLog. | |
| 115 void UploadStoredLog(const std::string& log_id, | |
| 116 const UploadDoneCallback& callback); | |
| 117 | |
| 118 // Called by WebRtcLogUploader when uploading has finished. Must be called on | |
| 119 // the IO thread. | |
| 120 void UploadLogDone(); | |
| 121 | |
| 122 // Discards the log and the RTP dumps. May only be called after logging has | |
| 123 // stopped. Must be called on the IO thread. | |
| 124 void DiscardLog(const GenericDoneCallback& callback); | |
| 125 | |
| 126 // Stores the log locally using a hash of log_id + security origin. | |
| 127 void StoreLog(const std::string& log_id, const GenericDoneCallback& callback); | |
| 128 | |
| 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 | |
| 134 // for decision making and it's OK if it changes before the execution based | |
| 135 // on that decision has finished. | |
| 136 void set_upload_log_on_render_close(bool should_upload) { | |
| 137 upload_log_on_render_close_ = should_upload; | |
| 138 } | |
| 139 | |
| 140 // 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 | |
| 142 // be dumped. |callback| will be called when starting the dump is done. | |
| 143 // |stop_callback| will be called when StopRtpDump is called. | |
| 144 void StartRtpDump(RtpDumpType type, | |
| 145 const GenericDoneCallback& callback, | |
| 146 const content::RenderProcessHost::WebRtcStopRtpDumpCallback& | |
| 147 stop_callback); | |
| 148 | |
| 149 // Stops dumping the RTP headers for the specified direction. Must be called | |
| 150 // on the IO thread. |type| specifies which direction(s) of RTP packet dumping | |
| 151 // should be stopped. |callback| will be called when stopping the dump is | |
| 152 // done. | |
| 153 void StopRtpDump(RtpDumpType type, const GenericDoneCallback& callback); | |
| 154 | |
| 155 // Called when an RTP packet is sent or received. Must be called on the UI | |
| 156 // thread. | |
| 157 void OnRtpPacket(std::unique_ptr<uint8_t[]> packet_header, | |
| 158 size_t header_length, | |
| 159 size_t packet_length, | |
| 160 bool incoming); | |
| 161 | |
| 162 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; | |
| 182 friend class base::DeleteHelper<WebRtcLoggingHandlerHost>; | |
| 183 | |
| 184 ~WebRtcLoggingHandlerHost() override; | |
| 185 | |
| 186 // BrowserMessageFilter implementation. | |
| 187 void OnChannelClosing() override; | |
| 188 void OnDestruct() const override; | |
| 189 bool OnMessageReceived(const IPC::Message& message) override; | |
| 190 | |
| 191 // Handles log message requests from renderer process. | |
| 192 void OnAddLogMessages(const std::vector<WebRtcLoggingMessageData>& messages); | |
| 193 void OnLoggingStoppedInRenderer(); | |
| 194 | |
| 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. | |
| 203 void StoreLogContinue(const std::string& log_id, | |
| 204 const GenericDoneCallback& callback); | |
| 205 | |
| 206 // Writes a formatted log |message| to the |circular_buffer_|. | |
| 207 void LogToCircularBuffer(const std::string& message); | |
| 208 | |
| 209 // Gets the log directory path for |profile_| and ensure it exists. Must be | |
| 210 // called on the FILE thread. | |
| 211 base::FilePath GetLogDirectoryAndEnsureExists(); | |
| 212 | |
| 213 void TriggerUpload(const UploadDoneCallback& callback, | |
| 214 const base::FilePath& log_directory); | |
| 215 | |
| 216 void StoreLogInDirectory(const std::string& log_id, | |
| 217 std::unique_ptr<WebRtcLogPaths> log_paths, | |
| 218 const GenericDoneCallback& done_callback, | |
| 219 const base::FilePath& directory); | |
| 220 | |
| 221 void UploadStoredLogOnFileThread(const std::string& log_id, | |
| 222 const UploadDoneCallback& callback); | |
| 223 | |
| 224 // A helper for TriggerUpload to do the real work. | |
| 225 void DoUploadLogAndRtpDumps(const base::FilePath& log_directory, | |
| 226 const UploadDoneCallback& callback); | |
| 227 | |
| 228 // Create the RTP dump handler and start dumping. Must be called after making | |
| 229 // sure the log directory exists. | |
| 230 void CreateRtpDumpHandlerAndStart(RtpDumpType type, | |
| 231 const GenericDoneCallback& callback, | |
| 232 const base::FilePath& dump_dir); | |
| 233 | |
| 234 // A helper for starting RTP dump assuming the RTP dump handler has been | |
| 235 // created. | |
| 236 void DoStartRtpDump(RtpDumpType type, const GenericDoneCallback& callback); | |
| 237 | |
| 238 // Adds the packet to the dump on IO thread. | |
| 239 void DumpRtpPacketOnIOThread(std::unique_ptr<uint8_t[]> packet_header, | |
| 240 size_t header_length, | |
| 241 size_t packet_length, | |
| 242 bool incoming); | |
| 243 | |
| 244 bool ReleaseRtpDumps(WebRtcLogPaths* log_paths); | |
| 245 | |
| 246 void FireGenericDoneCallback( | |
| 247 const WebRtcLoggingHandlerHost::GenericDoneCallback& callback, | |
| 248 bool success, | |
| 249 const std::string& error_message); | |
| 250 | |
| 251 std::unique_ptr<WebRtcLogBuffer> log_buffer_; | |
| 252 | |
| 253 // The profile associated with our renderer process. | |
| 254 Profile* const profile_; | |
| 255 | |
| 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. | |
| 271 bool upload_log_on_render_close_; | |
| 272 | |
| 273 // This is the handle to be passed to the render process. It's stored so that | |
| 274 // it doesn't have to be passed on when posting messages between threads. | |
| 275 // It's only accessed on the IO thread. | |
| 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 | |
| 282 // The RTP dump handler responsible for creating the RTP header dump files. | |
| 283 std::unique_ptr<WebRtcRtpDumpHandler> rtp_dump_handler_; | |
| 284 | |
| 285 // The callback to call when StopRtpDump is called. | |
| 286 content::RenderProcessHost::WebRtcStopRtpDumpCallback stop_rtp_dump_callback_; | |
| 287 | |
| 288 // A pointer to the log uploader that's shared for all profiles. | |
| 289 // Ownership lies with the browser process. | |
| 290 WebRtcLogUploader* const log_uploader_; | |
| 291 | |
| 292 // The render process ID this object belongs to. | |
| 293 int render_process_id_; | |
| 294 | |
| 295 DISALLOW_COPY_AND_ASSIGN(WebRtcLoggingHandlerHost); | |
| 296 }; | |
| 297 | |
| 298 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_ | |
| OLD | NEW |