1 // Copyright 2016 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_WEBRTC_TEXT_LOG_HANDLER_H_ 6 #define CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_TEXT_LOG_HANDLER_H_ 7 8 #include <map> 9 #include <memory> 10 #include <string> 11 12 #include "base/callback.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/sequence_checker.h" 15 #include "chrome/browser/media/webrtc/webrtc_log_uploader.h" 16 #include "net/base/network_interfaces.h" 17 18 namespace chrome { 19 namespace mojom { 20 class WebRtcLoggingMessage; 21 } // namespace mojom 22 } // namespace chrome 23 24 class WebRtcLogBuffer; 25 26 class WebRtcTextLogHandler { 27 public: 28 // States used for protecting from function calls made at non-allowed points 29 // in time. For example, StartLogging() is only allowed in CLOSED state. 30 // See also comment on |channel_is_closing_| below. 31 // Transitions: SetMetaData(): CLOSED -> CLOSED, or 32 // STARTED -> STARTED 33 // StartLogging(): CLOSED -> STARTING. 34 // StartDone(): STARTING -> STARTED. 35 // StopLogging(): STARTED -> STOPPING. 36 // StopDone(): STOPPING -> STOPPED. 37 // DiscardLog(): STOPPED -> CLOSED. 38 // ReleaseLog(): STOPPED -> CLOSED. 39 enum LoggingState { 40 CLOSED, // Logging not started, no log in memory. 41 STARTING, // Start logging is in progress. 42 STARTED, // Logging started. 43 STOPPING, // Stop logging is in progress. 44 STOPPED, // Logging has been stopped, log still open in memory. 45 }; 46 47 typedef base::OnceCallback<void(bool, const std::string&)> 48 GenericDoneCallback; 49 50 explicit WebRtcTextLogHandler(int render_process_id); 51 ~WebRtcTextLogHandler(); 52 53 // Returns the current state of the log. 54 LoggingState GetState() const; 55 56 // Returns true if channel is closing. 57 bool GetChannelIsClosing() const; 58 59 // Sets meta data for log uploading. Merged with any already set meta data. 60 // Values for existing keys are overwritten. The meta data already set at log 61 // start is written to the beginning of the log. Meta data set after log start 62 // is written to the log at that time. 63 void SetMetaData(std::unique_ptr<WebRtcLogMetaDataMap> meta_data, 64 GenericDoneCallback callback); 65 66 // Opens a log and starts logging if allowed by the LogUploader. 67 // Returns false if logging could not be started. 68 bool StartLogging(WebRtcLogUploader* log_uploader, 69 GenericDoneCallback callback); 70 71 // Stops logging. Log will remain open until UploadLog or DiscardLog is 72 // called. 73 bool StopLogging(GenericDoneCallback callback); 74 75 // Called by the WebRtcLoggingHandlerHost when logging has stopped in the 76 // renderer. Should only be called in response to a 77 // WebRtcLoggingMsg_LoggingStopped IPC message. 78 void StopDone(); 79 80 // Signals that the renderer is closing, which de facto stops logging but 81 // keeps the log in memory. 82 // Can be called in any state except CLOSED. 83 void ChannelClosing(); 84 85 // Discards a stopped log. 86 void DiscardLog(); 87 88 // Releases a stopped log to the caller. 89 void ReleaseLog(std::unique_ptr<WebRtcLogBuffer>* log_buffer, 90 std::unique_ptr<WebRtcLogMetaDataMap>* meta_data); 91 92 // Adds a message to the log. 93 void LogMessage(const std::string& message); 94 95 // Adds a message to the log. 96 void LogWebRtcLoggingMessage( 97 const chrome::mojom::WebRtcLoggingMessage* message); 98 99 // Returns true if the logging state is CLOSED and fires an the callback 100 // with an error message otherwise. 101 bool ExpectLoggingStateStopped(GenericDoneCallback* callback); 102 103 void FireGenericDoneCallback(GenericDoneCallback callback, 104 bool success, 105 const std::string& error_message); 106 107 void SetWebAppId(int web_app_id); 108 109 private: 110 void StartDone(GenericDoneCallback callback); 111 112 void LogToCircularBuffer(const std::string& message); 113 114 void OnGetNetworkInterfaceList( 115 GenericDoneCallback callback, 116 const base::Optional<net::NetworkInterfaceList>& networks); 117 void OnGetNetworkInterfaceListFinish( 118 GenericDoneCallback callback, 119 const base::Optional<net::NetworkInterfaceList>& networks, 120 const std::string& linux_distro); 121 122 SEQUENCE_CHECKER(sequence_checker_); 123 124 // The render process ID this object belongs to. 125 const int render_process_id_; 126 127 // Should be created by StartLogging(). 128 std::unique_ptr<WebRtcLogBuffer> log_buffer_; 129 130 // Should be created by StartLogging(). 131 std::unique_ptr<WebRtcLogMetaDataMap> meta_data_; 132 133 GenericDoneCallback stop_callback_; 134 LoggingState logging_state_; 135 136 // True if renderer is closing. The log (if there is one) can still be 137 // released or discarded (i.e. closed). No new logs can be created. The only 138 // state change possible when channel is closing is from any state to CLOSED. 139 bool channel_is_closing_ = false; 140 141 // The system time in ms when logging is started. Reset when logging_state_ 142 // changes to STOPPED. 143 base::Time logging_started_time_; 144 145 // Web app id used for statistics. See 146 // |WebRtcLoggingHandlerHost::web_app_id_|. 147 int web_app_id_ = 0; 148 149 base::WeakPtrFactory<WebRtcTextLogHandler> weak_factory_{this}; 150 151 DISALLOW_COPY_AND_ASSIGN(WebRtcTextLogHandler); 152 }; 153 154 #endif // CHROME_BROWSER_MEDIA_WEBRTC_WEBRTC_TEXT_LOG_HANDLER_H_ 155