Chromium Code Reviews| Index: chrome/browser/media/webrtc_text_log_handler.h |
| diff --git a/chrome/browser/media/webrtc_text_log_handler.h b/chrome/browser/media/webrtc_text_log_handler.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..511bf86740de1c4a1a0112f4ace6423d17c5e416 |
| --- /dev/null |
| +++ b/chrome/browser/media/webrtc_text_log_handler.h |
| @@ -0,0 +1,159 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_MEDIA_WEBRTC_TEXT_LOG_HANDLER_H_ |
| +#define CHROME_BROWSER_MEDIA_WEBRTC_TEXT_LOG_HANDLER_H_ |
| + |
| +#include <map> |
| +#include <string> |
| + |
| +#include "base/callback.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "chrome/common/media/webrtc_logging_message_data.h" |
| +#include "chrome/common/partial_circular_buffer.h" |
| +#include "net/base/network_interfaces.h" |
| + |
| +#if defined(OS_ANDROID) |
| +const size_t kWebRtcLogSize = 1 * 1024 * 1024; // 1 MB |
| +#else |
| +const size_t kWebRtcLogSize = 6 * 1024 * 1024; // 6 MB |
| +#endif |
| + |
| +namespace net { |
| +// class URLRequestContextGetter; // TODO: Needed? |
| +// class NetworkInterfaceList; |
| +} // namespace net |
| + |
| +typedef std::map<std::string, std::string> MetaDataMap; |
| + |
| +// class WebRtcLoggingHandlerHost; |
| + |
| +class WebRtcLogBuffer { |
| + public: |
| + WebRtcLogBuffer(); |
| + ~WebRtcLogBuffer(); |
| + |
| + void Log(const std::string& message); |
| + |
| + // Returns a circular buffer instance for reading the internal log buffer. |
| + // Must only be called after the log has been marked as complete |
| + // (see SetComplete) and the caller must ensure that the WebRtcLogBuffer |
| + // instance remains in scope for the lifetime of the returned circular buffer. |
| + PartialCircularBuffer Read(); |
| + |
| + // Switches the buffer to read-only mode, where access to the internal |
| + // buffer is allowed from different threads than were used to contribute |
| + // to the log. Calls to Log() won't be allowed after calling |
| + // SetComplete() and the call to SetComplete() must be done on the same |
| + // thread as constructed the buffer and calls Log(). |
| + void SetComplete(); |
| + |
| + private: |
| + base::ThreadChecker thread_checker_; |
| + uint8_t buffer_[kWebRtcLogSize]; |
| + PartialCircularBuffer circular_; |
| + bool read_only_; |
| +}; |
| + |
| +class WebRtcTextLogHandler |
|
Henrik Grunell
2016/05/17 07:29:17
Great to move this out!
terelius-chromium
2016/06/17 11:39:43
Acknowledged.
|
| + : public base::RefCountedThreadSafe<WebRtcTextLogHandler> { |
| + public: |
| + // States used for protecting from function calls made at non-allowed points |
| + // in time. For example, StartLogging() is only allowed in CLOSED state. |
| + // Transitions: SetMetaData(): CLOSED -> CLOSED. |
| + // StartLogging(): CLOSED -> STARTING. |
| + // StartDone(): STARTING -> STARTED. |
| + // StopLogging(): STARTED -> STOPPING. |
| + // StopDone(): STOPPING -> STOPPED. |
| + // DiscardLog(): STOPPED -> CLOSED. |
| + // ReleaseLog(): STOPPED -> CLOSED. |
| + enum LoggingState { |
| + CLOSED, // Logging not started, no log in memory. |
| + STARTING, // Start logging is in progress. |
| + STARTED, // Logging started. |
| + STOPPING, // Stop logging is in progress. |
| + STOPPED, // Logging has been stopped, log still open in memory. |
| + }; |
| + |
| + public: |
| + typedef base::Callback<void(bool, const std::string&)> GenericDoneCallback; |
| + |
| + explicit WebRtcTextLogHandler(int render_process_id); |
| + |
| + // Returns the current state of the log. Must be called on the IO thread. |
| + LoggingState GetState() const { return logging_state_; } |
| + |
| + void SetMetaData(std::unique_ptr<MetaDataMap> meta_data, |
| + const GenericDoneCallback& callback); |
| + |
| + // Opens a log and starts logging. Must be called on the IO thread. |
| + void StartLogging(const GenericDoneCallback& callback); |
| + |
| + // Called automatically after StartLogging(). Do not call directly. |
| + // Must be called on the IO thread. |
| + void StartDone(const GenericDoneCallback& callback); |
| + |
| + // Stops logging. Log will remain open until UploadLog or DiscardLog is |
| + // called. Must be called on the IO thread. |
| + void StopLogging(const GenericDoneCallback& callback); |
| + |
| + // Called by the WebRtcLoggingHandlerHost when logging has stopped in the |
| + // renderer. Do not call directly. Must be called on the IO thread. |
| + void StopDone(); |
| + |
| + // Discards a stopped log. Must be called on the IO thread. |
| + void DiscardLog(); |
| + |
| + // Releases a stopped log to the caller. Must be called on the IO thread. |
| + void ReleaseLog(std::unique_ptr<WebRtcLogBuffer>* log_buffer, |
| + std::unique_ptr<MetaDataMap>* meta_data); |
| + |
| + // Adds a message to the log. Must be called on the IO thread. |
| + void LogMessage(const std::string& message); |
| + |
| + void LogToCircularBuffer(const std::string& message); |
| + |
| + void LogWebRtcLoggingMessageData(const WebRtcLoggingMessageData& message); |
| + |
| + void FireGenericDoneCallback(const GenericDoneCallback& callback, |
| + bool success, |
| + const std::string& error_message); |
| + |
| + private: |
| + friend class RefCountedThreadSafe<WebRtcTextLogHandler>; |
| + ~WebRtcTextLogHandler(); |
| + |
| + void LogInitialInfoOnFileThread(const GenericDoneCallback& callback); |
| + void LogInitialInfoOnIOThread(const net::NetworkInterfaceList& network_list, |
| + const GenericDoneCallback& callback); |
| + void EnableBrowserProcessLoggingOnUIThread(); |
| + void DisableBrowserProcessLoggingOnUIThread(); |
| + |
| + // The render process ID this object belongs to. |
| + int render_process_id_; |
| + |
| + std::unique_ptr<WebRtcLogBuffer> log_buffer_; |
| + |
| + // These are only accessed on the IO thread, except when in STARTING state. In |
| + // this state we are protected since entering any function that alters the |
| + // state is not allowed. |
| + std::unique_ptr<MetaDataMap> meta_data_; |
| + |
| + // These are only accessed on the IO thread. |
| + GenericDoneCallback stop_callback_; |
| + |
| + // Only accessed on the IO thread, except when in STARTING, STOPPING or |
| + // UPLOADING state if the action fails and the state must be reset. In these |
| + // states however, we are protected since entering any function that alters |
| + // the state is not allowed. TODO: Check this! |
| + LoggingState logging_state_; |
| + |
| + // The system time in ms when logging is started. Reset when logging_state_ |
| + // changes to STOPPED. |
| + base::Time logging_started_time_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WebRtcTextLogHandler); |
| +}; |
| + |
| +#endif // CHROME_BROWSER_MEDIA_WEBRTC_TEXT_LOG_HANDLER_H_ |