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