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 "ipc/ipc_listener.h"
10 #include "ipc/ipc_sender.h"
12 // The MessageRouter handles all incoming messages sent to it by routing them
13 // to the correct listener. Routing is based on the Message's routing ID.
14 // Since routing IDs are typically assigned asynchronously by the browser
15 // process, the MessageRouter has the notion of pending IDs for listeners that
16 // have not yet been assigned a routing ID.
18 // When a message arrives, the routing ID is used to index the set of routes to
19 // find a listener. If a listener is found, then the message is passed to it.
20 // Otherwise, the message is ignored if its routing ID is not equal to
21 // MSG_ROUTING_CONTROL.
23 // The MessageRouter supports the IPC::Sender interface for outgoing messages,
24 // but does not define a meaningful implementation of it. The subclass of
25 // MessageRouter is intended to provide that if appropriate.
27 // The MessageRouter can be used as a concrete class provided its Send method
28 // is not called and it does not receive any control messages.
32 class MessageRouter
: public IPC::Listener
, public IPC::Sender
{
35 virtual ~MessageRouter();
37 // Implemented by subclasses to handle control messages
38 virtual bool OnControlMessageReceived(const IPC::Message
& msg
);
40 // IPC::Listener implementation:
41 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
43 // Like OnMessageReceived, except it only handles routed messages. Returns
44 // true if the message was dispatched, or false if there was no listener for
46 virtual bool RouteMessage(const IPC::Message
& msg
);
48 // IPC::Sender implementation:
49 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
51 // Called to add/remove a listener for a particular message routing ID.
52 void AddRoute(int32 routing_id
, IPC::Listener
* listener
);
53 void RemoveRoute(int32 routing_id
);
56 // A list of all listeners with assigned routing IDs.
57 IDMap
<IPC::Listener
> routes_
;
59 DISALLOW_COPY_AND_ASSIGN(MessageRouter
);
62 } // namespace content
64 #endif // CONTENT_COMMON_MESSAGE_ROUTER_H_