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