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 "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" 16 17 namespace net { 18 class URLRequestContextGetter; 19 } // namespace net 20 21 class PartialCircularBuffer; 22 class Profile; 23 24 typedef std::map<std::string, std::string> MetaDataMap; 25 26 // WebRtcLoggingHandlerHost handles operations regarding the WebRTC logging: 27 // - Opens a shared memory buffer that the handler in the render process 28 // writes to. 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 32 // of the log. 33 // - Detects when channel, i.e. renderer, is going away and possibly triggers 34 // uploading the log. 35 class WebRtcLoggingHandlerHost : public content::BrowserMessageFilter { 36 public: 37 typedef base::Callback<void(bool, const std::string&)> GenericDoneCallback; 38 typedef base::Callback<void(bool, const std::string&, const std::string&)> 39 UploadDoneCallback; 40 41 explicit WebRtcLoggingHandlerHost(Profile* profile); 42 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 45 // StartLogging. 46 void SetMetaData(const MetaDataMap& meta_data, 47 const GenericDoneCallback& callback); 48 49 // Opens a log and starts logging. Must be called on the IO thread. 50 void StartLogging(const GenericDoneCallback& callback); 51 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); 55 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); 59 60 // Called by WebRtcLogUploader when uploading has finished. Must be called on 61 // the IO thread. 62 void UploadLogDone(); 63 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); 67 68 // Adds a message to the log. 69 void LogMessage(const std::string& message); 70 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. set_upload_log_on_render_close(bool should_upload)74 void set_upload_log_on_render_close(bool should_upload) { 75 upload_log_on_render_close_ = should_upload; 76 } 77 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& 85 stop_callback); 86 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 90 // done. 91 void StopRtpDump(RtpDumpType type, const GenericDoneCallback& callback); 92 93 // Called when an RTP packet is sent or received. Must be called on the UI 94 // thread. 95 void OnRtpPacket(scoped_ptr<uint8[]> packet_header, 96 size_t header_length, 97 size_t packet_length, 98 bool incoming); 99 100 private: 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. 111 enum LoggingState { 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. 118 }; 119 120 friend class content::BrowserThread; 121 friend class base::DeleteHelper<WebRtcLoggingHandlerHost>; 122 123 virtual ~WebRtcLoggingHandlerHost(); 124 125 // BrowserMessageFilter implementation. 126 virtual void OnChannelClosing() OVERRIDE; 127 virtual void OnDestruct() const OVERRIDE; 128 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 129 130 // Handles log message requests from renderer process. 131 void OnAddLogMessages(const std::vector<WebRtcLoggingMessageData>& messages); 132 void OnLoggingStoppedInRenderer(); 133 134 // Handles log message requests from browser process. 135 void AddLogMessageFromBrowser(const WebRtcLoggingMessageData& message); 136 137 void StartLoggingIfAllowed(); 138 void DoStartLogging(); 139 void LogInitialInfoOnFileThread(); 140 void LogInitialInfoOnIOThread(const net::NetworkInterfaceList& network_list); 141 void NotifyLoggingStarted(); 142 143 // Writes a formatted log |message| to the |circular_buffer_|. 144 void LogToCircularBuffer(const std::string& message); 145 146 // Gets the log directory path for |profile_| and ensure it exists. Must be 147 // called on the FILE thread. 148 base::FilePath GetLogDirectoryAndEnsureExists(); 149 150 void TriggerUpload(const base::FilePath& log_directory); 151 152 // A helper for TriggerUpload to do the real work. 153 void DoUploadLogAndRtpDumps(const base::FilePath& log_directory); 154 155 void FireGenericDoneCallback(GenericDoneCallback* callback, 156 bool success, 157 const std::string& error_message); 158 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); 164 165 // A helper for starting RTP dump assuming the RTP dump handler has been 166 // created. 167 void DoStartRtpDump(RtpDumpType type, GenericDoneCallback* callback); 168 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, 173 bool incoming); 174 175 scoped_ptr<unsigned char[]> log_buffer_; 176 scoped_ptr<PartialCircularBuffer> circular_buffer_; 177 178 // The profile associated with our renderer process. 179 Profile* profile_; 180 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_; 185 186 // These are only accessed on the IO thread. 187 GenericDoneCallback start_callback_; 188 GenericDoneCallback stop_callback_; 189 UploadDoneCallback upload_callback_; 190 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_; 196 197 // Only accessed on the IO thread. 198 bool upload_log_on_render_close_; 199 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_; 204 205 // The system time in ms when logging is started. Reset when logging_state_ 206 // changes to STOPPED. 207 base::Time logging_started_time_; 208 209 // The RTP dump handler responsible for creating the RTP header dump files. 210 scoped_ptr<WebRtcRtpDumpHandler> rtp_dump_handler_; 211 212 // The callback to call when StopRtpDump is called. 213 content::RenderProcessHost::WebRtcStopRtpDumpCallback stop_rtp_dump_callback_; 214 215 DISALLOW_COPY_AND_ASSIGN(WebRtcLoggingHandlerHost); 216 }; 217 218 #endif // CHROME_BROWSER_MEDIA_WEBRTC_LOGGING_HANDLER_HOST_H_ 219