1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_COMMON_MESSAGE_ROUTER_H_
6 #define CONTENT_COMMON_MESSAGE_ROUTER_H_
8 #include "base/id_map.h"
9 #include "content/common/content_export.h"
10 #include "ipc/ipc_listener.h"
11 #include "ipc/ipc_sender.h"
13 // The MessageRouter handles all incoming messages sent to it by routing them
14 // to the correct listener. Routing is based on the Message's routing ID.
15 // Since routing IDs are typically assigned asynchronously by the browser
16 // process, the MessageRouter has the notion of pending IDs for listeners that
17 // have not yet been assigned a routing ID.
19 // When a message arrives, the routing ID is used to index the set of routes to
20 // find a listener. If a listener is found, then the message is passed to it.
21 // Otherwise, the message is ignored if its routing ID is not equal to
22 // MSG_ROUTING_CONTROL.
24 // The MessageRouter supports the IPC::Sender interface for outgoing messages,
25 // but does not define a meaningful implementation of it. The subclass of
26 // MessageRouter is intended to provide that if appropriate.
28 // The MessageRouter can be used as a concrete class provided its Send method
29 // is not called and it does not receive any control messages.
33 class CONTENT_EXPORT MessageRouter
: public IPC::Listener
, public IPC::Sender
{
36 ~MessageRouter() override
;
38 // Implemented by subclasses to handle control messages
39 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
41 // IPC::Listener implementation:
42 bool OnMessageReceived(const IPC::Message
& msg
) override
;
44 // Like OnMessageReceived, except it only handles routed messages. Returns
45 // true if the message was dispatched, or false if there was no listener for
47 virtual bool RouteMessage(const IPC::Message
& msg
);
49 // IPC::Sender implementation:
50 bool Send(IPC::Message
* msg
) override
;
52 // Called to add a listener for a particular message routing ID.
53 // Returns true if succeeded.
54 bool AddRoute(int32 routing_id
, IPC::Listener
* listener
);
56 // Called to remove a listener for a particular message routing ID.
57 void RemoveRoute(int32 routing_id
);
60 // A list of all listeners with assigned routing IDs.
61 IDMap
<IPC::Listener
> routes_
;
63 DISALLOW_COPY_AND_ASSIGN(MessageRouter
);
66 } // namespace content
68 #endif // CONTENT_COMMON_MESSAGE_ROUTER_H_