OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_logging_handler_host.h" | 5 #include "chrome/browser/media/webrtc_logging_handler_host.h" |
6 | 6 |
7 #include <iomanip> | |
8 #include <sstream> | |
7 #include <string> | 9 #include <string> |
8 | 10 |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "base/command_line.h" | 12 #include "base/command_line.h" |
11 #include "base/cpu.h" | 13 #include "base/cpu.h" |
12 #include "base/file_util.h" | 14 #include "base/file_util.h" |
13 #include "base/logging.h" | 15 #include "base/logging.h" |
14 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
15 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
16 #include "base/sys_info.h" | 18 #include "base/sys_info.h" |
17 #include "base/time/time.h" | 19 #include "base/time/time.h" |
18 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/chromeos/settings/cros_settings.h" | 21 #include "chrome/browser/chromeos/settings/cros_settings.h" |
20 #include "chrome/browser/media/webrtc_log_list.h" | 22 #include "chrome/browser/media/webrtc_log_list.h" |
21 #include "chrome/browser/media/webrtc_log_uploader.h" | 23 #include "chrome/browser/media/webrtc_log_uploader.h" |
22 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
24 #include "chrome/common/media/webrtc_logging_messages.h" | |
25 #include "chrome/common/partial_circular_buffer.h" | 26 #include "chrome/common/partial_circular_buffer.h" |
26 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
27 #include "chromeos/settings/cros_settings_names.h" | 28 #include "chromeos/settings/cros_settings_names.h" |
28 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
29 #include "content/public/browser/content_browser_client.h" | 30 #include "content/public/browser/content_browser_client.h" |
30 #include "content/public/browser/gpu_data_manager.h" | 31 #include "content/public/browser/gpu_data_manager.h" |
31 #include "content/public/browser/render_process_host.h" | 32 #include "content/public/browser/render_process_host.h" |
32 #include "gpu/config/gpu_info.h" | 33 #include "gpu/config/gpu_info.h" |
33 #include "net/base/address_family.h" | 34 #include "net/base/address_family.h" |
34 #include "net/url_request/url_request_context_getter.h" | 35 #include "net/url_request/url_request_context_getter.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 // Remove last '\n'. | 108 // Remove last '\n'. |
108 message->resize(message->size() - 1); | 109 message->resize(message->size() - 1); |
109 } | 110 } |
110 | 111 |
111 } // namespace | 112 } // namespace |
112 | 113 |
113 WebRtcLoggingHandlerHost::WebRtcLoggingHandlerHost(Profile* profile) | 114 WebRtcLoggingHandlerHost::WebRtcLoggingHandlerHost(Profile* profile) |
114 : BrowserMessageFilter(WebRtcLoggingMsgStart), | 115 : BrowserMessageFilter(WebRtcLoggingMsgStart), |
115 profile_(profile), | 116 profile_(profile), |
116 logging_state_(CLOSED), | 117 logging_state_(CLOSED), |
117 upload_log_on_render_close_(false) { | 118 upload_log_on_render_close_(false), |
119 logging_started_time_ms_(0) { | |
118 DCHECK(profile_); | 120 DCHECK(profile_); |
119 } | 121 } |
120 | 122 |
121 WebRtcLoggingHandlerHost::~WebRtcLoggingHandlerHost() {} | 123 WebRtcLoggingHandlerHost::~WebRtcLoggingHandlerHost() {} |
122 | 124 |
123 void WebRtcLoggingHandlerHost::SetMetaData( | 125 void WebRtcLoggingHandlerHost::SetMetaData( |
124 const MetaDataMap& meta_data, | 126 const MetaDataMap& meta_data, |
125 const GenericDoneCallback& callback) { | 127 const GenericDoneCallback& callback) { |
126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
127 DCHECK(!callback.is_null()); | 129 DCHECK(!callback.is_null()); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 log_buffer_.reset(); | 214 log_buffer_.reset(); |
213 logging_state_ = CLOSED; | 215 logging_state_ = CLOSED; |
214 FireGenericDoneCallback(&discard_callback, true, ""); | 216 FireGenericDoneCallback(&discard_callback, true, ""); |
215 } | 217 } |
216 | 218 |
217 void WebRtcLoggingHandlerHost::LogMessage(const std::string& message) { | 219 void WebRtcLoggingHandlerHost::LogMessage(const std::string& message) { |
218 BrowserThread::PostTask( | 220 BrowserThread::PostTask( |
219 BrowserThread::IO, | 221 BrowserThread::IO, |
220 FROM_HERE, | 222 FROM_HERE, |
221 base::Bind( | 223 base::Bind( |
222 &WebRtcLoggingHandlerHost::AddLogMessageFromBrowser, this, message)); | 224 &WebRtcLoggingHandlerHost::AddLogMessageFromBrowser, |
225 this, | |
226 base::Time::Now().ToInternalValue() / | |
dcheng
2014/04/01 23:35:19
1) Shouldn't we be using base::TimeTicks? base::Ti
jiayl
2014/04/01 23:50:06
We do want to use base::Time::Now(), because
1. we
jiayl
2014/04/02 00:38:06
uint64 changed to base::Time.
| |
227 base::Time::kMicrosecondsPerMillisecond, | |
228 message)); | |
223 } | 229 } |
224 | 230 |
225 void WebRtcLoggingHandlerHost::OnChannelClosing() { | 231 void WebRtcLoggingHandlerHost::OnChannelClosing() { |
226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
227 if (logging_state_ == STARTED || logging_state_ == STOPPED) { | 233 if (logging_state_ == STARTED || logging_state_ == STOPPED) { |
228 if (upload_log_on_render_close_) { | 234 if (upload_log_on_render_close_) { |
229 logging_state_ = STOPPED; | 235 logging_state_ = STOPPED; |
236 logging_started_time_ms_ = 0; | |
230 content::BrowserThread::PostTaskAndReplyWithResult( | 237 content::BrowserThread::PostTaskAndReplyWithResult( |
231 content::BrowserThread::FILE, | 238 content::BrowserThread::FILE, |
232 FROM_HERE, | 239 FROM_HERE, |
233 base::Bind(&WebRtcLoggingHandlerHost::GetLogDirectoryAndEnsureExists, | 240 base::Bind(&WebRtcLoggingHandlerHost::GetLogDirectoryAndEnsureExists, |
234 this), | 241 this), |
235 base::Bind(&WebRtcLoggingHandlerHost::TriggerUploadLog, this)); | 242 base::Bind(&WebRtcLoggingHandlerHost::TriggerUploadLog, this)); |
236 } else { | 243 } else { |
237 g_browser_process->webrtc_log_uploader()->LoggingStoppedDontUpload(); | 244 g_browser_process->webrtc_log_uploader()->LoggingStoppedDontUpload(); |
238 } | 245 } |
239 } | 246 } |
240 content::BrowserMessageFilter::OnChannelClosing(); | 247 content::BrowserMessageFilter::OnChannelClosing(); |
241 } | 248 } |
242 | 249 |
243 void WebRtcLoggingHandlerHost::OnDestruct() const { | 250 void WebRtcLoggingHandlerHost::OnDestruct() const { |
244 BrowserThread::DeleteOnIOThread::Destruct(this); | 251 BrowserThread::DeleteOnIOThread::Destruct(this); |
245 } | 252 } |
246 | 253 |
247 bool WebRtcLoggingHandlerHost::OnMessageReceived(const IPC::Message& message, | 254 bool WebRtcLoggingHandlerHost::OnMessageReceived(const IPC::Message& message, |
248 bool* message_was_ok) { | 255 bool* message_was_ok) { |
249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
250 bool handled = true; | 257 bool handled = true; |
251 IPC_BEGIN_MESSAGE_MAP_EX(WebRtcLoggingHandlerHost, message, *message_was_ok) | 258 IPC_BEGIN_MESSAGE_MAP_EX(WebRtcLoggingHandlerHost, message, *message_was_ok) |
252 IPC_MESSAGE_HANDLER(WebRtcLoggingMsg_AddLogMessage, OnAddLogMessage) | 259 IPC_MESSAGE_HANDLER(WebRtcLoggingMsg_AddLogMessages, OnAddLogMessages) |
253 IPC_MESSAGE_HANDLER(WebRtcLoggingMsg_LoggingStopped, | 260 IPC_MESSAGE_HANDLER(WebRtcLoggingMsg_LoggingStopped, |
254 OnLoggingStoppedInRenderer) | 261 OnLoggingStoppedInRenderer) |
255 IPC_MESSAGE_UNHANDLED(handled = false) | 262 IPC_MESSAGE_UNHANDLED(handled = false) |
256 IPC_END_MESSAGE_MAP_EX() | 263 IPC_END_MESSAGE_MAP_EX() |
257 | 264 |
258 return handled; | 265 return handled; |
259 } | 266 } |
260 | 267 |
261 void WebRtcLoggingHandlerHost::AddLogMessageFromBrowser( | 268 void WebRtcLoggingHandlerHost::AddLogMessageFromBrowser( |
262 const std::string& message) { | 269 uint64 timestamp_ms, const std::string& message) { |
263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
264 if (logging_state_ == STARTED) | 271 if (logging_state_ == STARTED) |
265 LogToCircularBuffer(message); | 272 LogToCircularBuffer(FormatMessageWithTimestamp(timestamp_ms, message)); |
266 } | 273 } |
267 | 274 |
268 void WebRtcLoggingHandlerHost::OnAddLogMessage(const std::string& message) { | 275 void WebRtcLoggingHandlerHost::OnAddLogMessages( |
276 const std::vector<WebRtcLoggingMessage>& messages) { | |
269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 277 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
270 if (logging_state_ == STARTED || logging_state_ == STOPPING) | 278 if (logging_state_ == STARTED || logging_state_ == STOPPING) { |
271 LogToCircularBuffer(message); | 279 for (size_t i = 0; i < messages.size(); ++i) { |
280 LogToCircularBuffer( | |
281 FormatMessageWithTimestamp(messages[i].timestamp_ms, | |
282 messages[i].message)); | |
283 } | |
284 } | |
272 } | 285 } |
273 | 286 |
274 void WebRtcLoggingHandlerHost::OnLoggingStoppedInRenderer() { | 287 void WebRtcLoggingHandlerHost::OnLoggingStoppedInRenderer() { |
275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
276 if (logging_state_ != STOPPING) { | 289 if (logging_state_ != STOPPING) { |
277 // If an out-of-order response is received, stop_callback_ may be invalid, | 290 // If an out-of-order response is received, stop_callback_ may be invalid, |
278 // and must not be invoked. | 291 // and must not be invoked. |
279 DLOG(ERROR) << "OnLoggingStoppedInRenderer invoked in state " | 292 DLOG(ERROR) << "OnLoggingStoppedInRenderer invoked in state " |
280 << logging_state_; | 293 << logging_state_; |
281 BadMessageReceived(); | 294 BadMessageReceived(); |
282 return; | 295 return; |
283 } | 296 } |
297 logging_started_time_ms_ = 0; | |
284 logging_state_ = STOPPED; | 298 logging_state_ = STOPPED; |
285 FireGenericDoneCallback(&stop_callback_, true, ""); | 299 FireGenericDoneCallback(&stop_callback_, true, ""); |
286 } | 300 } |
287 | 301 |
288 void WebRtcLoggingHandlerHost::StartLoggingIfAllowed() { | 302 void WebRtcLoggingHandlerHost::StartLoggingIfAllowed() { |
289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
290 if (!g_browser_process->webrtc_log_uploader()->ApplyForStartLogging()) { | 304 if (!g_browser_process->webrtc_log_uploader()->ApplyForStartLogging()) { |
291 logging_state_ = CLOSED; | 305 logging_state_ = CLOSED; |
292 FireGenericDoneCallback( | 306 FireGenericDoneCallback( |
293 &start_callback_, false, "Cannot start, maybe the maximum number of " | 307 &start_callback_, false, "Cannot start, maybe the maximum number of " |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
391 LogToCircularBuffer("Name: " + it->friendly_name + ", Address: " + | 405 LogToCircularBuffer("Name: " + it->friendly_name + ", Address: " + |
392 IPAddressToSensitiveString(it->address)); | 406 IPAddressToSensitiveString(it->address)); |
393 } | 407 } |
394 | 408 |
395 NotifyLoggingStarted(); | 409 NotifyLoggingStarted(); |
396 } | 410 } |
397 | 411 |
398 void WebRtcLoggingHandlerHost::NotifyLoggingStarted() { | 412 void WebRtcLoggingHandlerHost::NotifyLoggingStarted() { |
399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
400 Send(new WebRtcLoggingMsg_StartLogging()); | 414 Send(new WebRtcLoggingMsg_StartLogging()); |
415 logging_started_time_ms_ = base::Time::Now().ToInternalValue() / | |
416 base::Time::kMicrosecondsPerMillisecond;; | |
401 logging_state_ = STARTED; | 417 logging_state_ = STARTED; |
402 FireGenericDoneCallback(&start_callback_, true, ""); | 418 FireGenericDoneCallback(&start_callback_, true, ""); |
403 } | 419 } |
404 | 420 |
405 void WebRtcLoggingHandlerHost::LogToCircularBuffer(const std::string& message) { | 421 void WebRtcLoggingHandlerHost::LogToCircularBuffer(const std::string& message) { |
406 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 422 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
407 DCHECK(circular_buffer_.get()); | 423 DCHECK(circular_buffer_.get()); |
408 circular_buffer_->Write(message.c_str(), message.length()); | 424 circular_buffer_->Write(message.c_str(), message.length()); |
409 const char eol = '\n'; | 425 const char eol = '\n'; |
410 circular_buffer_->Write(&eol, 1); | 426 circular_buffer_->Write(&eol, 1); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
450 void WebRtcLoggingHandlerHost::FireGenericDoneCallback( | 466 void WebRtcLoggingHandlerHost::FireGenericDoneCallback( |
451 GenericDoneCallback* callback, bool success, | 467 GenericDoneCallback* callback, bool success, |
452 const std::string& error_message) { | 468 const std::string& error_message) { |
453 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 469 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
454 DCHECK(!(*callback).is_null()); | 470 DCHECK(!(*callback).is_null()); |
455 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 471 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
456 base::Bind(*callback, success, | 472 base::Bind(*callback, success, |
457 error_message)); | 473 error_message)); |
458 (*callback).Reset(); | 474 (*callback).Reset(); |
459 } | 475 } |
476 | |
477 std::string WebRtcLoggingHandlerHost::FormatMessageWithTimestamp( | |
478 uint64 msg_created_time_ms, const std::string& message) { | |
479 uint64 interval_ms = msg_created_time_ms - logging_started_time_ms_; | |
480 | |
481 std::ostringstream oss; | |
dcheng
2014/04/01 23:35:19
We don't generally use streams except for logging.
jiayl
2014/04/02 00:38:06
Done.
| |
482 oss << "[" << std::setfill('0') << std::setw(3) | |
483 << (interval_ms / 1000) << ":" << std::setw(3) | |
484 << (interval_ms % 1000) << std::setfill(' ') << "] " | |
485 << message; | |
486 return oss.str(); | |
487 } | |
OLD | NEW |