[email protected] | f786717 | 2012-07-11 07:04:07 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
markdittmer | 638a149 | 2016-02-19 01:11:50 | [diff] [blame] | 5 | #ifndef IPC_MESSAGE_ROUTER_H_ |
6 | #define IPC_MESSAGE_ROUTER_H_ | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 8 | #include <stdint.h> |
9 | |||||
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 10 | #include "base/component_export.h" |
Brett Wilson | f976d3f | 2017-08-18 17:23:39 | [diff] [blame] | 11 | #include "base/containers/id_map.h" |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | d84effeb | 2012-06-25 17:03:10 | [diff] [blame] | 13 | #include "ipc/ipc_listener.h" |
14 | #include "ipc/ipc_sender.h" | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | |
16 | // The MessageRouter handles all incoming messages sent to it by routing them | ||||
17 | // to the correct listener. Routing is based on the Message's routing ID. | ||||
18 | // Since routing IDs are typically assigned asynchronously by the browser | ||||
19 | // process, the MessageRouter has the notion of pending IDs for listeners that | ||||
20 | // have not yet been assigned a routing ID. | ||||
21 | // | ||||
22 | // When a message arrives, the routing ID is used to index the set of routes to | ||||
23 | // find a listener. If a listener is found, then the message is passed to it. | ||||
24 | // Otherwise, the message is ignored if its routing ID is not equal to | ||||
25 | // MSG_ROUTING_CONTROL. | ||||
26 | // | ||||
[email protected] | d84effeb | 2012-06-25 17:03:10 | [diff] [blame] | 27 | // The MessageRouter supports the IPC::Sender interface for outgoing messages, |
28 | // but does not define a meaningful implementation of it. The subclass of | ||||
29 | // MessageRouter is intended to provide that if appropriate. | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | // |
31 | // The MessageRouter can be used as a concrete class provided its Send method | ||||
32 | // is not called and it does not receive any control messages. | ||||
33 | |||||
markdittmer | 638a149 | 2016-02-19 01:11:50 | [diff] [blame] | 34 | namespace IPC { |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 35 | |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 36 | class COMPONENT_EXPORT(IPC) MessageRouter : public Listener, public Sender { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | public: |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 38 | MessageRouter(); |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 39 | ~MessageRouter() override; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | |
41 | // Implemented by subclasses to handle control messages | ||||
markdittmer | 638a149 | 2016-02-19 01:11:50 | [diff] [blame] | 42 | virtual bool OnControlMessageReceived(const Message& msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | |
markdittmer | 638a149 | 2016-02-19 01:11:50 | [diff] [blame] | 44 | // Listener implementation: |
45 | bool OnMessageReceived(const Message& msg) override; | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | |
47 | // Like OnMessageReceived, except it only handles routed messages. Returns | ||||
48 | // true if the message was dispatched, or false if there was no listener for | ||||
49 | // that route id. | ||||
markdittmer | 638a149 | 2016-02-19 01:11:50 | [diff] [blame] | 50 | virtual bool RouteMessage(const Message& msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | |
markdittmer | 638a149 | 2016-02-19 01:11:50 | [diff] [blame] | 52 | // Sender implementation: |
53 | bool Send(Message* msg) override; | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | |
[email protected] | 0d78ec0e | 2014-04-08 23:35:23 | [diff] [blame] | 55 | // Called to add a listener for a particular message routing ID. |
56 | // Returns true if succeeded. | ||||
markdittmer | 638a149 | 2016-02-19 01:11:50 | [diff] [blame] | 57 | bool AddRoute(int32_t routing_id, Listener* listener); |
[email protected] | 0d78ec0e | 2014-04-08 23:35:23 | [diff] [blame] | 58 | |
59 | // Called to remove a listener for a particular message routing ID. | ||||
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 60 | void RemoveRoute(int32_t routing_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | |
rockot | f62002a | 2016-09-15 00:08:59 | [diff] [blame] | 62 | // Returns the Listener associated with |routing_id|. |
63 | Listener* GetRoute(int32_t routing_id); | ||||
64 | |||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | private: |
66 | // A list of all listeners with assigned routing IDs. | ||||
Brett Wilson | f976d3f | 2017-08-18 17:23:39 | [diff] [blame] | 67 | base::IDMap<Listener*> routes_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | |
[email protected] | 312ef32 | 2009-08-28 19:33:29 | [diff] [blame] | 69 | DISALLOW_COPY_AND_ASSIGN(MessageRouter); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | }; |
71 | |||||
markdittmer | 638a149 | 2016-02-19 01:11:50 | [diff] [blame] | 72 | } // namespace IPC |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 73 | |
markdittmer | 638a149 | 2016-02-19 01:11:50 | [diff] [blame] | 74 | #endif // IPC_MESSAGE_ROUTER_H_ |