Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5939)

Unified Diff: chrome/browser/media/webrtc_text_log_handler.h

Issue 1978183003: Refactor WebRtcLoggingHandlerHost in preparation of automatic upload of WebRtcEventLogs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix race when renderer is closing while the log is in STARTING or STOPPING state Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..bd612a071e50ebe738f27ec537ed9f7f818aa56e
--- /dev/null
+++ b/chrome/browser/media/webrtc_text_log_handler.h
@@ -0,0 +1,166 @@
+// 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
+
+typedef std::map<std::string, std::string> MetaDataMap;
+
+class WebRtcLogUploader;
+
+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
+ : 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.
+ // RendererDying(): ANY -> DYING.
magjed_chromium 2016/07/19 14:53:17 Is DYING == CHANNEL_CLOSING?
terelius-chromium 2016/07/21 16:25:14 Yes. Forgot to update the comment. Fixed now.
+ 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.
+ CHANNEL_CLOSING, // Renderer is closing. TODO
magjed_chromium 2016/07/19 14:53:16 TODO what?
terelius-chromium 2016/07/21 16:25:14 Improve comment. Done.
+ };
+
+ public:
magjed_chromium 2016/07/19 14:53:17 Remove this extra 'public:'.
terelius-chromium 2016/07/21 16:25:14 Done.
+ 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 if allowed by the LogUploader.
+ // Returns false if logging could not be started.
+ // Must be called on the IO thread.
+ bool StartLogging(WebRtcLogUploader* log_uploader,
+ const GenericDoneCallback& callback);
+
+ // Called automatically after StartLogging(). Do not call directly.
magjed_chromium 2016/07/19 14:53:17 Why not make it private if it shouldn't be called
terelius-chromium 2016/07/21 16:25:14 Done, but the symmetry with StopDone is lost.
+ // 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.
+ bool 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.
magjed_chromium 2016/07/19 14:53:17 What does "Do not call directly." mean? It is call
terelius-chromium 2016/07/21 16:25:14 Updated comment. Wdyt?
magjed_chromium 2016/07/22 09:30:31 Now I understand, thanks.
+ void StopDone();
+
+ // Signals that the renderer is closing, which de facto stops logging but
+ // keeps the log in memory.
+ // Can be called in any state except CLOSED. Must be called on the IO thread.
+ void ChannelClosing();
+
+ // 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);
+
+ // Adds a message to the log. Must be called on the IO thread.
+ void LogWebRtcLoggingMessageData(const WebRtcLoggingMessageData& message);
+
+ // Returns true if the logging state is CLOSED and fires an the callback
+ // with an error message otherwise. Must be called on the IO thread.
+ bool ExpectLoggingStateStopped(const GenericDoneCallback& callback);
+
+ void FireGenericDoneCallback(const GenericDoneCallback& callback,
+ bool success,
+ const std::string& error_message);
+
+ private:
+ friend class RefCountedThreadSafe<WebRtcTextLogHandler>;
+ ~WebRtcTextLogHandler();
+
+ void LogToCircularBuffer(const std::string& message);
+
+ 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_;
magjed_chromium 2016/07/19 14:53:17 Make const.
terelius-chromium 2016/07/21 16:25:14 Done.
+
+ 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.
+ 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_

Powered by Google App Engine
This is Rietveld 408576698