Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / renderer / media / webrtc_logging_message_filter.cc
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.
4
5 #include "chrome/renderer/media/webrtc_logging_message_filter.h"
6
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "chrome/common/media/webrtc_logging_messages.h"
10 #include "chrome/renderer/media/chrome_webrtc_log_message_delegate.h"
11 #include "ipc/ipc_logging.h"
12
13 WebRtcLoggingMessageFilter::WebRtcLoggingMessageFilter(
14     const scoped_refptr<base::MessageLoopProxy>& io_message_loop)
15     : io_message_loop_(io_message_loop),
16       log_message_delegate_(NULL),
17       sender_(NULL) {
18   // May be null in a browsertest using MockRenderThread.
19   if (io_message_loop_.get()) {
20     io_message_loop_->PostTask(
21         FROM_HERE, base::Bind(
22             &WebRtcLoggingMessageFilter::CreateLoggingHandler,
23             base::Unretained(this)));
24   }
25 }
26
27 WebRtcLoggingMessageFilter::~WebRtcLoggingMessageFilter() {
28 }
29
30 bool WebRtcLoggingMessageFilter::OnMessageReceived(
31     const IPC::Message& message) {
32   DCHECK(io_message_loop_->BelongsToCurrentThread());
33   bool handled = true;
34   IPC_BEGIN_MESSAGE_MAP(WebRtcLoggingMessageFilter, message)
35     IPC_MESSAGE_HANDLER(WebRtcLoggingMsg_StartLogging, OnStartLogging)
36     IPC_MESSAGE_HANDLER(WebRtcLoggingMsg_StopLogging, OnStopLogging)
37     IPC_MESSAGE_UNHANDLED(handled = false)
38   IPC_END_MESSAGE_MAP()
39   return handled;
40 }
41
42 void WebRtcLoggingMessageFilter::OnFilterAdded(IPC::Sender* sender) {
43   DCHECK(!io_message_loop_.get() || io_message_loop_->BelongsToCurrentThread());
44   sender_ = sender;
45 }
46
47 void WebRtcLoggingMessageFilter::OnFilterRemoved() {
48   DCHECK(!io_message_loop_.get() || io_message_loop_->BelongsToCurrentThread());
49   sender_ = NULL;
50   log_message_delegate_->OnFilterRemoved();
51 }
52
53 void WebRtcLoggingMessageFilter::OnChannelClosing() {
54   DCHECK(!io_message_loop_.get() || io_message_loop_->BelongsToCurrentThread());
55   sender_ = NULL;
56   log_message_delegate_->OnFilterRemoved();
57 }
58
59 void WebRtcLoggingMessageFilter::AddLogMessages(
60     const std::vector<WebRtcLoggingMessageData>& messages) {
61   DCHECK(io_message_loop_->BelongsToCurrentThread());
62   Send(new WebRtcLoggingMsg_AddLogMessages(messages));
63 }
64
65 void WebRtcLoggingMessageFilter::LoggingStopped() {
66   DCHECK(!io_message_loop_.get() || io_message_loop_->BelongsToCurrentThread());
67   Send(new WebRtcLoggingMsg_LoggingStopped());
68 }
69
70 void WebRtcLoggingMessageFilter::CreateLoggingHandler() {
71   DCHECK(!io_message_loop_.get() || io_message_loop_->BelongsToCurrentThread());
72   log_message_delegate_ =
73       new ChromeWebRtcLogMessageDelegate(io_message_loop_, this);
74 }
75
76 void WebRtcLoggingMessageFilter::OnStartLogging() {
77   DCHECK(!io_message_loop_.get() || io_message_loop_->BelongsToCurrentThread());
78   log_message_delegate_->OnStartLogging();
79 }
80
81 void WebRtcLoggingMessageFilter::OnStopLogging() {
82   DCHECK(!io_message_loop_.get() || io_message_loop_->BelongsToCurrentThread());
83   log_message_delegate_->OnStopLogging();
84 }
85
86 void WebRtcLoggingMessageFilter::Send(IPC::Message* message) {
87   DCHECK(!io_message_loop_.get() || io_message_loop_->BelongsToCurrentThread());
88   if (!sender_) {
89     DLOG(ERROR) << "IPC sender not available.";
90     delete message;
91   } else {
92     sender_->Send(message);
93   }
94 }