blob: 3e6aac61d50d7c7e358bfe4cd724202549449d92 [file] [log] [blame]
markdittmer085d0802016-02-19 10:11:50 +09001// 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
9namespace IPC {
10
Chris Watkins31bfe1a2017-11-30 11:11:59 +090011MessageRouter::MessageRouter() = default;
markdittmer085d0802016-02-19 10:11:50 +090012
Chris Watkins31bfe1a2017-11-30 11:11:59 +090013MessageRouter::~MessageRouter() = default;
markdittmer085d0802016-02-19 10:11:50 +090014
15bool MessageRouter::OnControlMessageReceived(const IPC::Message& msg) {
16 NOTREACHED()
17 << "should override in subclass if you care about control messages";
18 return false;
19}
20
21bool MessageRouter::Send(IPC::Message* msg) {
22 NOTREACHED()
23 << "should override in subclass if you care about sending messages";
24 return false;
25}
26
27bool 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
36void MessageRouter::RemoveRoute(int32_t routing_id) {
37 routes_.Remove(routing_id);
38}
39
rockot065bc8f2016-09-15 09:08:59 +090040Listener* MessageRouter::GetRoute(int32_t routing_id) {
41 return routes_.Lookup(routing_id);
42}
43
markdittmer085d0802016-02-19 10:11:50 +090044bool 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
51bool 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