- add sources.
[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     : log_message_delegate_(NULL),
16       io_message_loop_(io_message_loop),
17       channel_(NULL) {
18   // May be null in a browsertest using MockRenderThread.
19   if (io_message_loop_) {
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::Channel* channel) {
43   DCHECK(!io_message_loop_ || io_message_loop_->BelongsToCurrentThread());
44   channel_ = channel;
45 }
46
47 void WebRtcLoggingMessageFilter::OnFilterRemoved() {
48   DCHECK(!io_message_loop_ || io_message_loop_->BelongsToCurrentThread());
49   channel_ = NULL;
50   log_message_delegate_->OnFilterRemoved();
51 }
52
53 void WebRtcLoggingMessageFilter::OnChannelClosing() {
54   DCHECK(!io_message_loop_ || io_message_loop_->BelongsToCurrentThread());
55   channel_ = NULL;
56   log_message_delegate_->OnFilterRemoved();
57 }
58
59 void WebRtcLoggingMessageFilter::LoggingStopped() {
60   DCHECK(io_message_loop_->BelongsToCurrentThread());
61   Send(new WebRtcLoggingMsg_LoggingStopped());
62 }
63
64 void WebRtcLoggingMessageFilter::CreateLoggingHandler() {
65   DCHECK(!io_message_loop_ || io_message_loop_->BelongsToCurrentThread());
66   log_message_delegate_ =
67       new ChromeWebRtcLogMessageDelegate(io_message_loop_, this);
68 }
69
70 void WebRtcLoggingMessageFilter::OnStartLogging(
71     base::SharedMemoryHandle handle,
72     uint32 length) {
73   DCHECK(!io_message_loop_ || io_message_loop_->BelongsToCurrentThread());
74   log_message_delegate_->OnStartLogging(handle, length);
75 }
76
77 void WebRtcLoggingMessageFilter::OnStopLogging() {
78   DCHECK(io_message_loop_->BelongsToCurrentThread());
79   log_message_delegate_->OnStopLogging();
80 }
81
82 void WebRtcLoggingMessageFilter::Send(IPC::Message* message) {
83   DCHECK(!io_message_loop_ || io_message_loop_->BelongsToCurrentThread());
84   if (!channel_) {
85     DLOG(ERROR) << "IPC channel not available.";
86     delete message;
87   } else {
88     channel_->Send(message);
89   }
90 }