OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media/webrtc_event_log_handler.h" | 5 #include "chrome/browser/media/webrtc_event_log_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "chrome/browser/media/webrtc_log_list.h" | 15 #include "chrome/browser/media/webrtc_log_list.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
18 #include "content/common/media/peer_connection_tracker_messages.h" | |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
21 #include "content/public/browser/webrtc_callback_interface.h" | |
22 #include "ipc/ipc_platform_file.h" | |
20 | 23 |
21 using content::BrowserThread; | 24 using content::BrowserThread; |
22 | 25 |
23 // Keys used to attach handler to the RenderProcessHost | 26 // Keys used to attach handler to the RenderProcessHost |
24 const char WebRtcEventLogHandler::kWebRtcEventLogHandlerKey[] = | 27 const char WebRtcEventLogHandler::kWebRtcEventLogHandlerKey[] = |
25 "kWebRtcEventLogHandlerKey"; | 28 "kWebRtcEventLogHandlerKey"; |
26 | 29 |
27 namespace { | 30 namespace { |
28 | 31 |
29 // Returns a path name to be used as prefix for RTC event log files. | 32 // Returns a path name to be used as prefix for RTC event log files. |
30 base::FilePath GetWebRtcEventLogPrefixPath(const base::FilePath& directory, | 33 base::FilePath GetWebRtcEventLogPrefixPath(const base::FilePath& directory) { |
31 uint64_t rtc_event_log_id) { | |
32 static const char kWebRtcEventLogFilePrefix[] = "WebRtcEventLog."; | 34 static const char kWebRtcEventLogFilePrefix[] = "WebRtcEventLog."; |
33 return directory.AppendASCII(kWebRtcEventLogFilePrefix + | 35 return directory.AppendASCII(kWebRtcEventLogFilePrefix); |
34 base::Int64ToString(rtc_event_log_id)); | |
35 } | 36 } |
36 | 37 |
38 // Appends the IDs to the RTC event log file name. | |
39 base::FilePath GetWebRtcEventLogPath(const base::FilePath& base_file, | |
40 int render_process_id, | |
41 uint64_t rtc_event_log_id) { | |
42 return base_file.AddExtension(base::IntToString(render_process_id)) | |
43 .AddExtension(base::Int64ToString(rtc_event_log_id)); | |
dcheng
2016/05/13 06:50:11
Uint64ToString, since the input type is unsigned.
Ivo-OOO until feb 6
2016/05/18 16:26:54
Done.
| |
44 } | |
45 | |
46 // Opens a logfile to pass on to the renderer. | |
47 IPC::PlatformFileForTransit CreateFileForProcess( | |
48 const base::FilePath& base_path, | |
49 int render_process_id, | |
50 uint64_t rtc_event_log_id) { | |
51 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
52 base::FilePath file_path = | |
53 GetWebRtcEventLogPath(base_path, render_process_id, rtc_event_log_id); | |
54 base::File event_log_file( | |
55 file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_APPEND); | |
56 if (!event_log_file.IsValid()) { | |
57 VLOG(1) << "Could not open WebRTC event log file, error=" | |
58 << event_log_file.error_details(); | |
59 return IPC::InvalidPlatformFileForTransit(); | |
60 } | |
61 return IPC::TakePlatformFileForTransit(std::move(event_log_file)); | |
62 } | |
63 | |
64 void SendEventLogFileToRenderer(content::RenderProcessHost* rph, | |
65 int local_id, | |
66 IPC::PlatformFileForTransit file_for_transit) { | |
67 if (file_for_transit == IPC::InvalidPlatformFileForTransit()) { | |
68 return; | |
69 } | |
70 #if defined(OS_ANDROID) | |
71 const int64_t max_filesize_bytes = 10000000; | |
72 #else | |
73 const int64_t max_filesize_bytes = 30000000; | |
dcheng
2016/05/13 06:50:11
Out of curiosity... why do we need to send this ov
Henrik Grunell
2016/05/13 09:05:31
Agree.
Ivo-OOO until feb 6
2016/05/18 16:26:54
Ok, I will move it there, good point.
| |
74 #endif | |
75 | |
76 rph->Send(new PeerConnectionTracker_StartEventLog(local_id, file_for_transit, | |
77 max_filesize_bytes)); | |
78 } | |
37 } // namespace | 79 } // namespace |
38 | 80 |
39 WebRtcEventLogHandler::WebRtcEventLogHandler(Profile* profile) | 81 WebRtcEventLogHandler::WebRtcEventLogHandler(int process_id, Profile* profile) |
40 : profile_(profile), | 82 : profile_(profile), |
41 is_rtc_event_logging_in_progress_(false), | 83 is_rtc_event_logging_in_progress_(false), |
84 number_log_files_(0), | |
42 current_rtc_event_log_id_(0) { | 85 current_rtc_event_log_id_(0) { |
43 DCHECK(profile_); | 86 DCHECK(profile_); |
44 thread_checker_.DetachFromThread(); | 87 thread_checker_.DetachFromThread(); |
88 content::RenderProcessHost* host = | |
89 content::RenderProcessHost::FromID(process_id); | |
90 using StartEventLogFn = void (WebRtcEventLogHandler::*)( | |
91 content::RenderProcessHost* host, const base::FilePath& file_path); | |
dcheng
2016/05/13 06:50:11
I think it's probably shorter (and clearer) to jus
Ivo-OOO until feb 6
2016/05/18 16:26:54
You're right that it's both shorter and clearer, h
| |
92 using StopEventLogFn = | |
93 void (WebRtcEventLogHandler::*)(content::RenderProcessHost* host); | |
94 StartEventLogFn start_fn = &WebRtcEventLogHandler::StartWebRtcEventLogging; | |
95 StopEventLogFn stop_fn = &WebRtcEventLogHandler::StopWebRtcEventLogging; | |
96 content::WebRTCCallbackInterface::GetInstance()->RegisterEventLogHandler( | |
97 process_id, base::Bind(start_fn, this, host), | |
98 base::Bind(stop_fn, this, host)); | |
99 content::WebRTCCallbackInterface::GetInstance() | |
100 ->RegisterPeerConnectionCallbacks( | |
101 process_id, | |
102 base::Bind(&WebRtcEventLogHandler::OnPeerConnectionAdded, this), | |
103 base::Bind(&WebRtcEventLogHandler::OnPeerConnectionRemoved, this)); | |
45 } | 104 } |
46 | 105 |
47 WebRtcEventLogHandler::~WebRtcEventLogHandler() {} | 106 WebRtcEventLogHandler::~WebRtcEventLogHandler() {} |
48 | 107 |
49 void WebRtcEventLogHandler::StartWebRtcEventLogging( | 108 void WebRtcEventLogHandler::StartWebRtcEventLogging( |
50 content::RenderProcessHost* host, | 109 content::RenderProcessHost* host, |
51 base::TimeDelta delay, | 110 base::TimeDelta delay, |
52 const RecordingDoneCallback& callback, | 111 const RecordingDoneCallback& callback, |
53 const RecordingErrorCallback& error_callback) { | 112 const RecordingErrorCallback& error_callback) { |
54 DCHECK(thread_checker_.CalledOnValidThread()); | 113 DCHECK(thread_checker_.CalledOnValidThread()); |
55 | 114 |
56 BrowserThread::PostTaskAndReplyWithResult( | 115 BrowserThread::PostTaskAndReplyWithResult( |
57 BrowserThread::FILE, FROM_HERE, | 116 BrowserThread::FILE, FROM_HERE, |
58 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), | 117 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), |
59 base::Bind(&WebRtcEventLogHandler::DoStartWebRtcEventLogging, this, host, | 118 base::Bind(&WebRtcEventLogHandler::DoStartWebRtcEventLogging, this, host, |
60 delay, callback, error_callback)); | 119 delay, callback, error_callback)); |
61 } | 120 } |
62 | 121 |
122 void WebRtcEventLogHandler::StartWebRtcEventLogging( | |
123 content::RenderProcessHost* host, | |
124 const base::FilePath& file_path) { | |
125 DCHECK(thread_checker_.CalledOnValidThread()); | |
126 is_rtc_event_logging_in_progress_ = true; | |
dcheng
2016/05/13 06:50:11
This is triggered by a renderer IPC, right? What i
Ivo-OOO until feb 6
2016/05/18 16:26:54
This is not triggered by renderer IPC (it is calle
| |
127 base_file_path_ = file_path; | |
128 #if defined(OS_ANDROID) | |
129 number_log_files_ = 3; | |
Henrik Grunell
2016/05/13 09:05:31
Are these number based on something particular?
Ivo-OOO until feb 6
2016/05/18 16:26:54
Not really, just on what seems like a reasonably u
| |
130 #else | |
131 number_log_files_ = 10; | |
132 #endif | |
133 for (int local_id : active_peer_connection_local_id_) { | |
134 if (number_log_files_ > 0) { | |
135 BrowserThread::PostTaskAndReplyWithResult( | |
136 BrowserThread::FILE, FROM_HERE, | |
137 base::Bind(&CreateFileForProcess, file_path, host->GetID(), local_id), | |
138 base::Bind(&SendEventLogFileToRenderer, host, local_id)); | |
139 number_log_files_--; | |
140 } | |
141 } | |
142 } | |
143 | |
63 void WebRtcEventLogHandler::StopWebRtcEventLogging( | 144 void WebRtcEventLogHandler::StopWebRtcEventLogging( |
64 content::RenderProcessHost* host, | 145 content::RenderProcessHost* host, |
65 const RecordingDoneCallback& callback, | 146 const RecordingDoneCallback& callback, |
66 const RecordingErrorCallback& error_callback) { | 147 const RecordingErrorCallback& error_callback) { |
67 DCHECK(thread_checker_.CalledOnValidThread()); | 148 DCHECK(thread_checker_.CalledOnValidThread()); |
68 const bool is_manual_stop = true; | 149 const bool is_manual_stop = true; |
69 BrowserThread::PostTaskAndReplyWithResult( | 150 BrowserThread::PostTaskAndReplyWithResult( |
70 BrowserThread::FILE, FROM_HERE, | 151 BrowserThread::FILE, FROM_HERE, |
71 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), | 152 base::Bind(&WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists, this), |
72 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, | 153 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, |
73 is_manual_stop, current_rtc_event_log_id_, callback, | 154 is_manual_stop, current_rtc_event_log_id_, callback, |
74 error_callback)); | 155 error_callback)); |
75 } | 156 } |
76 | 157 |
158 void WebRtcEventLogHandler::StopWebRtcEventLogging( | |
159 content::RenderProcessHost* host) { | |
160 is_rtc_event_logging_in_progress_ = false; | |
161 for (int local_id : active_peer_connection_local_id_) { | |
162 host->Send(new PeerConnectionTracker_StopEventLog(local_id)); | |
163 } | |
164 } | |
165 | |
166 void WebRtcEventLogHandler::OnPeerConnectionAdded(int process_id, | |
167 int connection_id) { | |
168 active_peer_connection_local_id_.insert(connection_id); | |
169 if (is_rtc_event_logging_in_progress_ && number_log_files_ > 0) { | |
170 content::RenderProcessHost* host = | |
171 content::RenderProcessHost::FromID(process_id); | |
172 if (host) { | |
173 BrowserThread::PostTaskAndReplyWithResult( | |
174 BrowserThread::FILE, FROM_HERE, | |
175 base::Bind(&CreateFileForProcess, base_file_path_, process_id, | |
176 connection_id), | |
177 base::Bind(&SendEventLogFileToRenderer, host, connection_id)); | |
178 number_log_files_--; | |
179 } | |
180 } | |
181 } | |
182 | |
183 void WebRtcEventLogHandler::OnPeerConnectionRemoved(int process_id, | |
184 int connection_id) { | |
185 active_peer_connection_local_id_.erase(connection_id); | |
186 } | |
187 | |
77 base::FilePath WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists() { | 188 base::FilePath WebRtcEventLogHandler::GetLogDirectoryAndEnsureExists() { |
78 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 189 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
79 base::FilePath log_dir_path = | 190 base::FilePath log_dir_path = |
80 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()); | 191 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()); |
81 base::File::Error error; | 192 base::File::Error error; |
82 if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) { | 193 if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) { |
83 DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error; | 194 DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error; |
84 return base::FilePath(); | 195 return base::FilePath(); |
85 } | 196 } |
86 return log_dir_path; | 197 return log_dir_path; |
87 } | 198 } |
88 | 199 |
89 void WebRtcEventLogHandler::DoStartWebRtcEventLogging( | 200 void WebRtcEventLogHandler::DoStartWebRtcEventLogging( |
90 content::RenderProcessHost* host, | 201 content::RenderProcessHost* host, |
91 base::TimeDelta delay, | 202 base::TimeDelta delay, |
92 const RecordingDoneCallback& callback, | 203 const RecordingDoneCallback& callback, |
93 const RecordingErrorCallback& error_callback, | 204 const RecordingErrorCallback& error_callback, |
94 const base::FilePath& log_directory) { | 205 const base::FilePath& log_directory) { |
95 DCHECK(thread_checker_.CalledOnValidThread()); | 206 DCHECK(thread_checker_.CalledOnValidThread()); |
96 | 207 |
97 if (is_rtc_event_logging_in_progress_) { | 208 if (is_rtc_event_logging_in_progress_) { |
98 error_callback.Run("RTC event logging already in progress"); | 209 error_callback.Run("RTC event logging already in progress"); |
99 return; | 210 return; |
100 } | 211 } |
101 | 212 base::FilePath prefix_path = GetWebRtcEventLogPrefixPath(log_directory); |
102 is_rtc_event_logging_in_progress_ = true; | 213 StartWebRtcEventLogging(host, prefix_path); |
103 base::FilePath prefix_path = | |
104 GetWebRtcEventLogPrefixPath(log_directory, ++current_rtc_event_log_id_); | |
105 host->EnableEventLogRecordings(prefix_path); | |
106 | 214 |
107 if (delay.is_zero()) { | 215 if (delay.is_zero()) { |
108 const bool is_stopped = false, is_manual_stop = false; | 216 const bool is_stopped = false, is_manual_stop = false; |
109 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); | 217 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); |
110 return; | 218 return; |
111 } | 219 } |
112 | 220 |
113 const bool is_manual_stop = false; | 221 const bool is_manual_stop = false; |
114 BrowserThread::PostDelayedTask( | 222 BrowserThread::PostDelayedTask( |
115 BrowserThread::UI, FROM_HERE, | 223 BrowserThread::UI, FROM_HERE, |
116 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, | 224 base::Bind(&WebRtcEventLogHandler::DoStopWebRtcEventLogging, this, host, |
117 is_manual_stop, current_rtc_event_log_id_, callback, | 225 is_manual_stop, current_rtc_event_log_id_, callback, |
118 error_callback, log_directory), | 226 error_callback, log_directory), |
119 delay); | 227 delay); |
120 } | 228 } |
121 | 229 |
122 void WebRtcEventLogHandler::DoStopWebRtcEventLogging( | 230 void WebRtcEventLogHandler::DoStopWebRtcEventLogging( |
123 content::RenderProcessHost* host, | 231 content::RenderProcessHost* host, |
124 bool is_manual_stop, | 232 bool is_manual_stop, |
125 uint64_t rtc_event_log_id, | 233 uint64_t rtc_event_log_id, |
126 const RecordingDoneCallback& callback, | 234 const RecordingDoneCallback& callback, |
127 const RecordingErrorCallback& error_callback, | 235 const RecordingErrorCallback& error_callback, |
128 const base::FilePath& log_directory) { | 236 const base::FilePath& log_directory) { |
129 DCHECK(thread_checker_.CalledOnValidThread()); | 237 DCHECK(thread_checker_.CalledOnValidThread()); |
130 DCHECK_LE(rtc_event_log_id, current_rtc_event_log_id_); | 238 DCHECK_LE(rtc_event_log_id, current_rtc_event_log_id_); |
131 | 239 |
132 base::FilePath prefix_path = | 240 base::FilePath prefix_path = GetWebRtcEventLogPrefixPath(log_directory); |
133 GetWebRtcEventLogPrefixPath(log_directory, rtc_event_log_id); | 241 base::FilePath file_path = |
242 GetWebRtcEventLogPath(prefix_path, host->GetID(), rtc_event_log_id); | |
134 // Prevent an old posted DoStopWebRtcEventLogging() call to stop a newer dump. | 243 // Prevent an old posted DoStopWebRtcEventLogging() call to stop a newer dump. |
135 // This could happen in a sequence like: | 244 // This could happen in a sequence like: |
136 // Start(10); // Start dump 1. Post Stop() to run after 10 seconds. | 245 // Start(10); // Start dump 1. Post Stop() to run after 10 seconds. |
137 // Stop(); // Manually stop dump 1 before 10 seconds; | 246 // Stop(); // Manually stop dump 1 before 10 seconds; |
138 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. | 247 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. |
139 if (rtc_event_log_id < current_rtc_event_log_id_) { | 248 if (rtc_event_log_id < current_rtc_event_log_id_) { |
140 const bool is_stopped = false; | 249 const bool is_stopped = false; |
141 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); | 250 callback.Run(file_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); |
142 return; | 251 return; |
143 } | 252 } |
144 | 253 |
145 if (!is_rtc_event_logging_in_progress_) { | 254 if (!is_rtc_event_logging_in_progress_) { |
146 error_callback.Run("No RTC event logging in progress"); | 255 error_callback.Run("No RTC event logging in progress"); |
147 return; | 256 return; |
148 } | 257 } |
149 | 258 |
150 host->DisableEventLogRecordings(); | 259 StopWebRtcEventLogging(host); |
151 is_rtc_event_logging_in_progress_ = false; | |
152 const bool is_stopped = true; | 260 const bool is_stopped = true; |
153 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); | 261 callback.Run(file_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); |
154 } | 262 } |
OLD | NEW |