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