OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_COMMON_MESSAGE_ROUTER_H_ | 5 #ifndef CONTENT_COMMON_MESSAGE_ROUTER_H_ |
6 #define CONTENT_COMMON_MESSAGE_ROUTER_H_ | 6 #define CONTENT_COMMON_MESSAGE_ROUTER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/id_map.h" | 9 #include "base/id_map.h" |
10 #include "ipc/ipc_channel.h" | 10 #include "ipc/ipc_listener.h" |
| 11 #include "ipc/ipc_sender.h" |
11 | 12 |
12 // The MessageRouter handles all incoming messages sent to it by routing them | 13 // 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 // 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 // Since routing IDs are typically assigned asynchronously by the browser |
15 // process, the MessageRouter has the notion of pending IDs for listeners that | 16 // process, the MessageRouter has the notion of pending IDs for listeners that |
16 // have not yet been assigned a routing ID. | 17 // have not yet been assigned a routing ID. |
17 // | 18 // |
18 // When a message arrives, the routing ID is used to index the set of routes to | 19 // 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 // 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 // Otherwise, the message is ignored if its routing ID is not equal to |
21 // MSG_ROUTING_CONTROL. | 22 // MSG_ROUTING_CONTROL. |
22 // | 23 // |
23 // The MessageRouter supports the IPC::Message::Sender interface for outgoing | 24 // The MessageRouter supports the IPC::Sender interface for outgoing messages, |
24 // messages, but does not define a meaningful implementation of it. The | 25 // but does not define a meaningful implementation of it. The subclass of |
25 // subclass of MessageRouter is intended to provide that if appropriate. | 26 // MessageRouter is intended to provide that if appropriate. |
26 // | 27 // |
27 // The MessageRouter can be used as a concrete class provided its Send method | 28 // 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. | 29 // is not called and it does not receive any control messages. |
29 | 30 |
30 class MessageRouter : public IPC::Channel::Listener, | 31 class MessageRouter : public IPC::Listener, public IPC::Sender { |
31 public IPC::Message::Sender { | |
32 public: | 32 public: |
33 MessageRouter(); | 33 MessageRouter(); |
34 virtual ~MessageRouter(); | 34 virtual ~MessageRouter(); |
35 | 35 |
36 // Implemented by subclasses to handle control messages | 36 // Implemented by subclasses to handle control messages |
37 virtual bool OnControlMessageReceived(const IPC::Message& msg); | 37 virtual bool OnControlMessageReceived(const IPC::Message& msg); |
38 | 38 |
39 // IPC::Channel::Listener implementation: | 39 // IPC::Listener implementation: |
40 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; | 40 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; |
41 | 41 |
42 // Like OnMessageReceived, except it only handles routed messages. Returns | 42 // Like OnMessageReceived, except it only handles routed messages. Returns |
43 // true if the message was dispatched, or false if there was no listener for | 43 // true if the message was dispatched, or false if there was no listener for |
44 // that route id. | 44 // that route id. |
45 virtual bool RouteMessage(const IPC::Message& msg); | 45 virtual bool RouteMessage(const IPC::Message& msg); |
46 | 46 |
47 // IPC::Message::Sender implementation: | 47 // IPC::Sender implementation: |
48 virtual bool Send(IPC::Message* msg) OVERRIDE; | 48 virtual bool Send(IPC::Message* msg) OVERRIDE; |
49 | 49 |
50 // Called to add/remove a listener for a particular message routing ID. | 50 // Called to add/remove a listener for a particular message routing ID. |
51 void AddRoute(int32 routing_id, IPC::Channel::Listener* listener); | 51 void AddRoute(int32 routing_id, IPC::Listener* listener); |
52 void RemoveRoute(int32 routing_id); | 52 void RemoveRoute(int32 routing_id); |
53 | 53 |
54 IPC::Channel::Listener* ResolveRoute(int32 routing_id); | 54 IPC::Listener* ResolveRoute(int32 routing_id); |
55 | 55 |
56 private: | 56 private: |
57 // A list of all listeners with assigned routing IDs. | 57 // A list of all listeners with assigned routing IDs. |
58 IDMap<IPC::Channel::Listener> routes_; | 58 IDMap<IPC::Listener> routes_; |
59 | 59 |
60 DISALLOW_COPY_AND_ASSIGN(MessageRouter); | 60 DISALLOW_COPY_AND_ASSIGN(MessageRouter); |
61 }; | 61 }; |
62 | 62 |
63 #endif // CONTENT_COMMON_MESSAGE_ROUTER_H_ | 63 #endif // CONTENT_COMMON_MESSAGE_ROUTER_H_ |
OLD | NEW |