markdittmer | 085d080 | 2016-02-19 10:11:50 +0900 | [diff] [blame] | 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 | |
Chris Watkins | 31bfe1a | 2017-11-30 11:11:59 +0900 | [diff] [blame] | 11 | MessageRouter::MessageRouter() = default; |
markdittmer | 085d080 | 2016-02-19 10:11:50 +0900 | [diff] [blame] | 12 | |
Chris Watkins | 31bfe1a | 2017-11-30 11:11:59 +0900 | [diff] [blame] | 13 | MessageRouter::~MessageRouter() = default; |
markdittmer | 085d080 | 2016-02-19 10:11:50 +0900 | [diff] [blame] | 14 | |
| 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 | |
| 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 | |
| 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 | |
| 36 | void MessageRouter::RemoveRoute(int32_t routing_id) { |
| 37 | routes_.Remove(routing_id); |
| 38 | } |
| 39 | |
rockot | 065bc8f | 2016-09-15 09:08:59 +0900 | [diff] [blame] | 40 | Listener* MessageRouter::GetRoute(int32_t routing_id) { |
| 41 | return routes_.Lookup(routing_id); |
| 42 | } |
| 43 | |
markdittmer | 085d080 | 2016-02-19 10:11:50 +0900 | [diff] [blame] | 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 | |
| 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 |