• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 "ipc/message_router.h"
6 
7 #include "ipc/ipc_message.h"
8 
9 namespace IPC {
10 
11 MessageRouter::MessageRouter() = default;
12 
13 MessageRouter::~MessageRouter() = default;
14 
OnControlMessageReceived(const IPC::Message & msg)15 bool MessageRouter::OnControlMessageReceived(const IPC::Message& msg) {
16   NOTREACHED()
17       << "should override in subclass if you care about control messages";
18   return false;
19 }
20 
Send(IPC::Message * msg)21 bool MessageRouter::Send(IPC::Message* msg) {
22   NOTREACHED()
23       << "should override in subclass if you care about sending messages";
24   return false;
25 }
26 
AddRoute(int32_t routing_id,IPC::Listener * listener)27 bool MessageRouter::AddRoute(int32_t routing_id, IPC::Listener* listener) {
28   if (routes_.Lookup(routing_id)) {
29     DLOG(ERROR) << "duplicate routing ID";
30     return false;
31   }
32   routes_.AddWithID(listener, routing_id);
33   return true;
34 }
35 
RemoveRoute(int32_t routing_id)36 void MessageRouter::RemoveRoute(int32_t routing_id) {
37   routes_.Remove(routing_id);
38 }
39 
GetRoute(int32_t routing_id)40 Listener* MessageRouter::GetRoute(int32_t routing_id) {
41   return routes_.Lookup(routing_id);
42 }
43 
OnMessageReceived(const IPC::Message & msg)44 bool MessageRouter::OnMessageReceived(const IPC::Message& msg) {
45   if (msg.routing_id() == MSG_ROUTING_CONTROL)
46     return OnControlMessageReceived(msg);
47 
48   return RouteMessage(msg);
49 }
50 
RouteMessage(const IPC::Message & msg)51 bool MessageRouter::RouteMessage(const IPC::Message& msg) {
52   IPC::Listener* listener = routes_.Lookup(msg.routing_id());
53   if (!listener)
54     return false;
55 
56   return listener->OnMessageReceived(msg);
57 }
58 
59 }  // namespace IPC
60