1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef IPC_MESSAGE_ROUTER_H_
6 #define IPC_MESSAGE_ROUTER_H_
10 #include "base/component_export.h"
11 #include "base/containers/id_map.h"
12 #include "ipc/ipc_listener.h"
13 #include "ipc/ipc_sender.h"
15 // The MessageRouter handles all incoming messages sent to it by routing them
16 // to the correct listener. Routing is based on the Message's routing ID.
17 // Since routing IDs are typically assigned asynchronously by the browser
18 // process, the MessageRouter has the notion of pending IDs for listeners that
19 // have not yet been assigned a routing ID.
21 // When a message arrives, the routing ID is used to index the set of routes to
22 // find a listener. If a listener is found, then the message is passed to it.
23 // Otherwise, the message is ignored if its routing ID is not equal to
24 // MSG_ROUTING_CONTROL.
26 // The MessageRouter supports the IPC::Sender interface for outgoing messages,
27 // but does not define a meaningful implementation of it. The subclass of
28 // MessageRouter is intended to provide that if appropriate.
30 // The MessageRouter can be used as a concrete class provided its Send method
31 // is not called and it does not receive any control messages.
35 class COMPONENT_EXPORT(IPC) MessageRouter : public Listener, public Sender {
39 MessageRouter(const MessageRouter&) = delete;
40 MessageRouter& operator=(const MessageRouter&) = delete;
42 ~MessageRouter() override;
44 // Implemented by subclasses to handle control messages
45 virtual bool OnControlMessageReceived(const Message& msg);
47 // Listener implementation:
48 bool OnMessageReceived(const Message& msg) override;
50 // Like OnMessageReceived, except it only handles routed messages. Returns
51 // true if the message was dispatched, or false if there was no listener for
53 virtual bool RouteMessage(const Message& msg);
55 // Sender implementation:
56 bool Send(Message* msg) override;
58 // Called to add a listener for a particular message routing ID.
59 // Returns true if succeeded.
60 bool AddRoute(int32_t routing_id, Listener* listener);
62 // Called to remove a listener for a particular message routing ID.
63 void RemoveRoute(int32_t routing_id);
65 // Returns the Listener associated with |routing_id|.
66 Listener* GetRoute(int32_t routing_id);
69 // A list of all listeners with assigned routing IDs.
70 base::IDMap<Listener*> routes_;
75 #endif // IPC_MESSAGE_ROUTER_H_