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
3 // found in the LICENSE file.
5 #include "chrome/renderer/media/chrome_webrtc_log_message_delegate.h"
7 #include "base/logging.h"
8 #include "base/single_thread_task_runner.h"
9 #include "chrome/common/partial_circular_buffer.h"
10 #include "chrome/renderer/media/webrtc_logging_message_filter.h"
12 ChromeWebRtcLogMessageDelegate::ChromeWebRtcLogMessageDelegate(
13 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_task_runner
,
14 WebRtcLoggingMessageFilter
* message_filter
)
15 : io_task_runner_(io_task_runner
),
16 logging_started_(false),
17 message_filter_(message_filter
) {
18 content::InitWebRtcLoggingDelegate(this);
21 ChromeWebRtcLogMessageDelegate::~ChromeWebRtcLogMessageDelegate() {
22 DCHECK(CalledOnValidThread());
25 void ChromeWebRtcLogMessageDelegate::LogMessage(const std::string
& message
) {
26 WebRtcLoggingMessageData
data(base::Time::Now(), message
);
28 io_task_runner_
->PostTask(
29 FROM_HERE
, base::Bind(
30 &ChromeWebRtcLogMessageDelegate::LogMessageOnIOThread
,
31 base::Unretained(this),
35 void ChromeWebRtcLogMessageDelegate::LogMessageOnIOThread(
36 const WebRtcLoggingMessageData
& message
) {
37 DCHECK(CalledOnValidThread());
39 if (logging_started_
&& message_filter_
) {
40 if (!log_buffer_
.empty()) {
41 // A delayed task has already been posted for sending the buffer contents.
42 // Just add the message to the buffer.
43 log_buffer_
.push_back(message
);
47 log_buffer_
.push_back(message
);
49 if (base::TimeTicks::Now() - last_log_buffer_send_
>
50 base::TimeDelta::FromMilliseconds(100)) {
53 io_task_runner_
->PostDelayedTask(
55 base::Bind(&ChromeWebRtcLogMessageDelegate::SendLogBuffer
,
56 base::Unretained(this)),
57 base::TimeDelta::FromMilliseconds(200));
62 void ChromeWebRtcLogMessageDelegate::OnFilterRemoved() {
63 DCHECK(CalledOnValidThread());
64 message_filter_
= NULL
;
67 void ChromeWebRtcLogMessageDelegate::OnStartLogging() {
68 DCHECK(CalledOnValidThread());
69 logging_started_
= true;
70 content::InitWebRtcLogging();
73 void ChromeWebRtcLogMessageDelegate::OnStopLogging() {
74 DCHECK(CalledOnValidThread());
75 if (!log_buffer_
.empty())
78 message_filter_
->LoggingStopped();
79 logging_started_
= false;
82 void ChromeWebRtcLogMessageDelegate::SendLogBuffer() {
83 DCHECK(CalledOnValidThread());
84 if (logging_started_
&& message_filter_
) {
85 message_filter_
->AddLogMessages(log_buffer_
);
86 last_log_buffer_send_
= base::TimeTicks::Now();