OLD | NEW |
(Empty) | |
| 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_TEXT_LOG_HANDLER_H_ |
| 6 #define CHROME_BROWSER_MEDIA_WEBRTC_TEXT_LOG_HANDLER_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <memory> |
| 10 #include <string> |
| 11 |
| 12 #include "base/callback.h" |
| 13 #include "base/threading/thread_checker.h" |
| 14 #include "chrome/common/media/webrtc_logging_message_data.h" |
| 15 #include "chrome/common/partial_circular_buffer.h" |
| 16 #include "net/base/network_interfaces.h" |
| 17 |
| 18 #if defined(OS_ANDROID) |
| 19 const size_t kWebRtcLogSize = 1 * 1024 * 1024; // 1 MB |
| 20 #else |
| 21 const size_t kWebRtcLogSize = 6 * 1024 * 1024; // 6 MB |
| 22 #endif |
| 23 |
| 24 typedef std::map<std::string, std::string> MetaDataMap; |
| 25 |
| 26 class WebRtcLogUploader; |
| 27 |
| 28 class WebRtcLogBuffer { |
| 29 public: |
| 30 WebRtcLogBuffer(); |
| 31 ~WebRtcLogBuffer(); |
| 32 |
| 33 void Log(const std::string& message); |
| 34 |
| 35 // Returns a circular buffer instance for reading the internal log buffer. |
| 36 // Must only be called after the log has been marked as complete |
| 37 // (see SetComplete) and the caller must ensure that the WebRtcLogBuffer |
| 38 // instance remains in scope for the lifetime of the returned circular buffer. |
| 39 PartialCircularBuffer Read(); |
| 40 |
| 41 // Switches the buffer to read-only mode, where access to the internal |
| 42 // buffer is allowed from different threads than were used to contribute |
| 43 // to the log. Calls to Log() won't be allowed after calling |
| 44 // SetComplete() and the call to SetComplete() must be done on the same |
| 45 // thread as constructed the buffer and calls Log(). |
| 46 void SetComplete(); |
| 47 |
| 48 private: |
| 49 base::ThreadChecker thread_checker_; |
| 50 uint8_t buffer_[kWebRtcLogSize]; |
| 51 PartialCircularBuffer circular_; |
| 52 bool read_only_; |
| 53 }; |
| 54 |
| 55 class WebRtcTextLogHandler |
| 56 : public base::RefCountedThreadSafe<WebRtcTextLogHandler> { |
| 57 public: |
| 58 // States used for protecting from function calls made at non-allowed points |
| 59 // in time. For example, StartLogging() is only allowed in CLOSED state. |
| 60 // Transitions: SetMetaData(): CLOSED -> CLOSED. |
| 61 // StartLogging(): CLOSED -> STARTING. |
| 62 // StartDone(): STARTING -> STARTED. |
| 63 // StopLogging(): STARTED -> STOPPING. |
| 64 // StopDone(): STOPPING -> STOPPED. |
| 65 // DiscardLog(): STOPPED -> CLOSED or |
| 66 // CHANNEL_CLOSING -> CHANNEL_CLOSING. |
| 67 // ReleaseLog(): STOPPED -> CLOSED. or |
| 68 // CHANNEL_CLOSING -> CHANNEL_CLOSING. |
| 69 // ChannelClosing(): ANY -> CHANNEL_CLOSING. |
| 70 enum LoggingState { |
| 71 CLOSED, // Logging not started, no log in memory. |
| 72 STARTING, // Start logging is in progress. |
| 73 STARTED, // Logging started. |
| 74 STOPPING, // Stop logging is in progress. |
| 75 STOPPED, // Logging has been stopped, log still open in memory. |
| 76 CHANNEL_CLOSING, // Renderer is closing. The log (if there is one) can |
| 77 // still be uploaded, but no new logs can be created and |
| 78 // and no state transitions can be made. |
| 79 }; |
| 80 |
| 81 typedef base::Callback<void(bool, const std::string&)> GenericDoneCallback; |
| 82 |
| 83 explicit WebRtcTextLogHandler(int render_process_id); |
| 84 |
| 85 // Returns the current state of the log. Must be called on the IO thread. |
| 86 LoggingState GetState() const { return logging_state_; } |
| 87 |
| 88 void SetMetaData(std::unique_ptr<MetaDataMap> meta_data, |
| 89 const GenericDoneCallback& callback); |
| 90 |
| 91 // Opens a log and starts logging if allowed by the LogUploader. |
| 92 // Returns false if logging could not be started. |
| 93 // Must be called on the IO thread. |
| 94 bool StartLogging(WebRtcLogUploader* log_uploader, |
| 95 const GenericDoneCallback& callback); |
| 96 |
| 97 // Stops logging. Log will remain open until UploadLog or DiscardLog is |
| 98 // called. Must be called on the IO thread. |
| 99 bool StopLogging(const GenericDoneCallback& callback); |
| 100 |
| 101 // Called by the WebRtcLoggingHandlerHost when logging has stopped in the |
| 102 // renderer. Should only be called in response to a |
| 103 // WebRtcLoggingMsg_LoggingStopped IPC message. |
| 104 // Must be called on the IO thread. |
| 105 void StopDone(); |
| 106 |
| 107 // Signals that the renderer is closing, which de facto stops logging but |
| 108 // keeps the log in memory. |
| 109 // Can be called in any state except CLOSED. Must be called on the IO thread. |
| 110 void ChannelClosing(); |
| 111 |
| 112 // Discards a stopped log. Must be called on the IO thread. |
| 113 void DiscardLog(); |
| 114 |
| 115 // Releases a stopped log to the caller. Must be called on the IO thread. |
| 116 void ReleaseLog(std::unique_ptr<WebRtcLogBuffer>* log_buffer, |
| 117 std::unique_ptr<MetaDataMap>* meta_data); |
| 118 |
| 119 // Adds a message to the log. Must be called on the IO thread. |
| 120 void LogMessage(const std::string& message); |
| 121 |
| 122 // Adds a message to the log. Must be called on the IO thread. |
| 123 void LogWebRtcLoggingMessageData(const WebRtcLoggingMessageData& message); |
| 124 |
| 125 // Returns true if the logging state is CLOSED and fires an the callback |
| 126 // with an error message otherwise. Must be called on the IO thread. |
| 127 bool ExpectLoggingStateStopped(const GenericDoneCallback& callback); |
| 128 |
| 129 void FireGenericDoneCallback(const GenericDoneCallback& callback, |
| 130 bool success, |
| 131 const std::string& error_message); |
| 132 |
| 133 private: |
| 134 friend class RefCountedThreadSafe<WebRtcTextLogHandler>; |
| 135 ~WebRtcTextLogHandler(); |
| 136 |
| 137 void StartDone(const GenericDoneCallback& callback); |
| 138 |
| 139 void LogToCircularBuffer(const std::string& message); |
| 140 |
| 141 void LogInitialInfoOnFileThread(const GenericDoneCallback& callback); |
| 142 void LogInitialInfoOnIOThread(const net::NetworkInterfaceList& network_list, |
| 143 const GenericDoneCallback& callback); |
| 144 void EnableBrowserProcessLoggingOnUIThread(); |
| 145 void DisableBrowserProcessLoggingOnUIThread(); |
| 146 |
| 147 // The render process ID this object belongs to. |
| 148 const int render_process_id_; |
| 149 |
| 150 std::unique_ptr<WebRtcLogBuffer> log_buffer_; |
| 151 |
| 152 // These are only accessed on the IO thread, except when in STARTING state. In |
| 153 // this state we are protected since entering any function that alters the |
| 154 // state is not allowed. |
| 155 std::unique_ptr<MetaDataMap> meta_data_; |
| 156 |
| 157 // These are only accessed on the IO thread. |
| 158 GenericDoneCallback stop_callback_; |
| 159 |
| 160 // Only accessed on the IO thread. |
| 161 LoggingState logging_state_; |
| 162 |
| 163 // The system time in ms when logging is started. Reset when logging_state_ |
| 164 // changes to STOPPED. |
| 165 base::Time logging_started_time_; |
| 166 |
| 167 DISALLOW_COPY_AND_ASSIGN(WebRtcTextLogHandler); |
| 168 }; |
| 169 |
| 170 #endif // CHROME_BROWSER_MEDIA_WEBRTC_TEXT_LOG_HANDLER_H_ |
OLD | NEW |