blob: 731c4ebce532bf2f5b54caa5d00dae5534a80182 [file] [log] [blame]
markdittmer085d0802016-02-19 10:11:50 +09001// 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 IPC_MESSAGE_ROUTER_H_
6#define IPC_MESSAGE_ROUTER_H_
7
8#include <stdint.h>
9
Ken Rockote6f96f82018-01-23 11:44:39 +090010#include "base/component_export.h"
Brett Wilsonf0d64c62017-08-19 02:23:39 +090011#include "base/containers/id_map.h"
markdittmer085d0802016-02-19 10:11:50 +090012#include "base/macros.h"
markdittmer085d0802016-02-19 10:11:50 +090013#include "ipc/ipc_listener.h"
14#include "ipc/ipc_sender.h"
15
16// The MessageRouter handles all incoming messages sent to it by routing them
17// to the correct listener. Routing is based on the Message's routing ID.
18// Since routing IDs are typically assigned asynchronously by the browser
19// process, the MessageRouter has the notion of pending IDs for listeners that
20// have not yet been assigned a routing ID.
21//
22// When a message arrives, the routing ID is used to index the set of routes to
23// find a listener. If a listener is found, then the message is passed to it.
24// Otherwise, the message is ignored if its routing ID is not equal to
25// MSG_ROUTING_CONTROL.
26//
27// The MessageRouter supports the IPC::Sender interface for outgoing messages,
28// but does not define a meaningful implementation of it. The subclass of
29// MessageRouter is intended to provide that if appropriate.
30//
31// The MessageRouter can be used as a concrete class provided its Send method
32// is not called and it does not receive any control messages.
33
34namespace IPC {
35
Ken Rockote6f96f82018-01-23 11:44:39 +090036class COMPONENT_EXPORT(IPC) MessageRouter : public Listener, public Sender {
markdittmer085d0802016-02-19 10:11:50 +090037 public:
38 MessageRouter();
39 ~MessageRouter() override;
40
41 // Implemented by subclasses to handle control messages
42 virtual bool OnControlMessageReceived(const Message& msg);
43
44 // Listener implementation:
45 bool OnMessageReceived(const Message& msg) override;
46
47 // Like OnMessageReceived, except it only handles routed messages. Returns
48 // true if the message was dispatched, or false if there was no listener for
49 // that route id.
50 virtual bool RouteMessage(const Message& msg);
51
52 // Sender implementation:
53 bool Send(Message* msg) override;
54
55 // Called to add a listener for a particular message routing ID.
56 // Returns true if succeeded.
57 bool AddRoute(int32_t routing_id, Listener* listener);
58
59 // Called to remove a listener for a particular message routing ID.
60 void RemoveRoute(int32_t routing_id);
61
rockot065bc8f2016-09-15 09:08:59 +090062 // Returns the Listener associated with |routing_id|.
63 Listener* GetRoute(int32_t routing_id);
64
markdittmer085d0802016-02-19 10:11:50 +090065 private:
66 // A list of all listeners with assigned routing IDs.
Brett Wilsonf0d64c62017-08-19 02:23:39 +090067 base::IDMap<Listener*> routes_;
markdittmer085d0802016-02-19 10:11:50 +090068
69 DISALLOW_COPY_AND_ASSIGN(MessageRouter);
70};
71
72} // namespace IPC
73
74#endif // IPC_MESSAGE_ROUTER_H_