Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / chrome / renderer / media / cast_ipc_dispatcher.cc
bloba88103db8cc523aeaafc2487d8d66c383a60e44c
1 // Copyright 2014 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/cast_ipc_dispatcher.h"
7 #include "chrome/common/cast_messages.h"
8 #include "chrome/renderer/media/cast_transport_sender_ipc.h"
9 #include "ipc/ipc_message_macros.h"
11 CastIPCDispatcher* CastIPCDispatcher::global_instance_ = NULL;
13 CastIPCDispatcher::CastIPCDispatcher(
14 const scoped_refptr<base::MessageLoopProxy>& io_message_loop)
15 : sender_(NULL),
16 io_message_loop_(io_message_loop) {
17 DCHECK(io_message_loop_.get());
18 DCHECK(!global_instance_);
21 CastIPCDispatcher::~CastIPCDispatcher() {
22 DCHECK(io_message_loop_->BelongsToCurrentThread());
23 DCHECK(!global_instance_);
26 CastIPCDispatcher* CastIPCDispatcher::Get() {
27 return global_instance_;
30 void CastIPCDispatcher::Send(IPC::Message* message) {
31 DCHECK(io_message_loop_->BelongsToCurrentThread());
32 if (sender_) {
33 sender_->Send(message);
34 } else {
35 delete message;
39 int32 CastIPCDispatcher::AddSender(CastTransportSenderIPC* sender) {
40 return id_map_.Add(sender);
43 void CastIPCDispatcher::RemoveSender(int32 channel_id) {
44 return id_map_.Remove(channel_id);
47 bool CastIPCDispatcher::OnMessageReceived(const IPC::Message& message) {
48 DCHECK(io_message_loop_->BelongsToCurrentThread());
49 bool handled = true;
50 IPC_BEGIN_MESSAGE_MAP(CastIPCDispatcher, message)
51 IPC_MESSAGE_HANDLER(CastMsg_NotifyStatusChange, OnNotifyStatusChange)
52 IPC_MESSAGE_HANDLER(CastMsg_RawEvents, OnRawEvents)
53 IPC_MESSAGE_HANDLER(CastMsg_Rtt, OnRtt)
54 IPC_MESSAGE_HANDLER(CastMsg_RtcpCastMessage, OnRtcpCastMessage)
55 IPC_MESSAGE_HANDLER(CastMsg_ReceivedPacket, OnReceivedPacket)
56 IPC_MESSAGE_UNHANDLED(handled = false);
57 IPC_END_MESSAGE_MAP();
58 return handled;
61 void CastIPCDispatcher::OnFilterAdded(IPC::Sender* sender) {
62 DCHECK(io_message_loop_->BelongsToCurrentThread());
63 DCHECK(!global_instance_);
64 global_instance_ = this;
65 sender_ = sender;
68 void CastIPCDispatcher::OnFilterRemoved() {
69 DCHECK(io_message_loop_->BelongsToCurrentThread());
70 DCHECK_EQ(this, global_instance_);
71 global_instance_ = NULL;
72 sender_ = NULL;
75 void CastIPCDispatcher::OnChannelClosing() {
76 DCHECK(io_message_loop_->BelongsToCurrentThread());
77 DCHECK_EQ(this, global_instance_);
80 void CastIPCDispatcher::OnNotifyStatusChange(
81 int32 channel_id,
82 media::cast::CastTransportStatus status) {
83 CastTransportSenderIPC* sender = id_map_.Lookup(channel_id);
84 if (sender) {
85 sender->OnNotifyStatusChange(status);
86 } else {
87 DVLOG(1)
88 << "CastIPCDispatcher::OnNotifystatusChange on non-existing channel.";
92 void CastIPCDispatcher::OnRawEvents(
93 int32 channel_id,
94 const std::vector<media::cast::PacketEvent>& packet_events,
95 const std::vector<media::cast::FrameEvent>& frame_events) {
96 CastTransportSenderIPC* sender = id_map_.Lookup(channel_id);
97 if (sender) {
98 sender->OnRawEvents(packet_events, frame_events);
99 } else {
100 DVLOG(1) << "CastIPCDispatcher::OnRawEvents on non-existing channel.";
104 void CastIPCDispatcher::OnRtt(int32 channel_id,
105 uint32 ssrc,
106 base::TimeDelta rtt) {
107 CastTransportSenderIPC* sender = id_map_.Lookup(channel_id);
108 if (sender) {
109 sender->OnRtt(ssrc, rtt);
110 } else {
111 DVLOG(1) << "CastIPCDispatcher::OnRtt on non-existing channel.";
115 void CastIPCDispatcher::OnRtcpCastMessage(
116 int32 channel_id,
117 uint32 ssrc,
118 const media::cast::RtcpCastMessage& cast_message) {
119 CastTransportSenderIPC* sender = id_map_.Lookup(channel_id);
120 if (sender) {
121 sender->OnRtcpCastMessage(ssrc, cast_message);
122 } else {
123 DVLOG(1) << "CastIPCDispatcher::OnRtt on non-existing channel.";
127 void CastIPCDispatcher::OnReceivedPacket(
128 int32 channel_id,
129 const media::cast::Packet& packet) {
130 CastTransportSenderIPC* sender = id_map_.Lookup(channel_id);
131 if (sender) {
132 sender->OnReceivedPacket(packet);
133 } else {
134 DVLOG(1) << "CastIPCDispatcher::OnReceievdPacket on non-existing channel.";