Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / common / message_router.h
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.
4
5 #ifndef CONTENT_COMMON_MESSAGE_ROUTER_H_
6 #define CONTENT_COMMON_MESSAGE_ROUTER_H_
7
8 #include "base/id_map.h"
9 #include "ipc/ipc_listener.h"
10 #include "ipc/ipc_sender.h"
11
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.
17 //
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.
22 //
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.
26 //
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.
29
30 namespace content {
31
32 class MessageRouter : public IPC::Listener, public IPC::Sender {
33  public:
34   MessageRouter();
35   ~MessageRouter() override;
36
37   // Implemented by subclasses to handle control messages
38   virtual bool OnControlMessageReceived(const IPC::Message& msg);
39
40   // IPC::Listener implementation:
41   bool OnMessageReceived(const IPC::Message& msg) override;
42
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
45   // that route id.
46   virtual bool RouteMessage(const IPC::Message& msg);
47
48   // IPC::Sender implementation:
49   bool Send(IPC::Message* msg) override;
50
51   // Called to add a listener for a particular message routing ID.
52   // Returns true if succeeded.
53   bool AddRoute(int32 routing_id, IPC::Listener* listener);
54
55   // Called to remove a listener for a particular message routing ID.
56   void RemoveRoute(int32 routing_id);
57
58  private:
59   // A list of all listeners with assigned routing IDs.
60   IDMap<IPC::Listener> routes_;
61
62   DISALLOW_COPY_AND_ASSIGN(MessageRouter);
63 };
64
65 }  // namespace content
66
67 #endif  // CONTENT_COMMON_MESSAGE_ROUTER_H_