blob: 731c4ebce532bf2f5b54caa5d00dae5534a80182 [file] [log] [blame]
[email protected]f7867172012-07-11 07:04:071// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
markdittmer638a1492016-02-19 01:11:505#ifndef IPC_MESSAGE_ROUTER_H_
6#define IPC_MESSAGE_ROUTER_H_
initial.commit09911bf2008-07-26 23:55:297
avia9aa7a82015-12-25 03:06:318#include <stdint.h>
9
Ken Rockot3044d212018-01-23 02:44:3910#include "base/component_export.h"
Brett Wilsonf976d3f2017-08-18 17:23:3911#include "base/containers/id_map.h"
avia9aa7a82015-12-25 03:06:3112#include "base/macros.h"
[email protected]d84effeb2012-06-25 17:03:1013#include "ipc/ipc_listener.h"
14#include "ipc/ipc_sender.h"
initial.commit09911bf2008-07-26 23:55:2915
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//
[email protected]d84effeb2012-06-25 17:03:1027// 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.
initial.commit09911bf2008-07-26 23:55:2930//
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
markdittmer638a1492016-02-19 01:11:5034namespace IPC {
[email protected]130757672012-10-24 00:26:1935
Ken Rockot3044d212018-01-23 02:44:3936class COMPONENT_EXPORT(IPC) MessageRouter : public Listener, public Sender {
initial.commit09911bf2008-07-26 23:55:2937 public:
[email protected]d4799a32010-09-28 22:54:5838 MessageRouter();
dchenge933b3e2014-10-21 11:44:0939 ~MessageRouter() override;
initial.commit09911bf2008-07-26 23:55:2940
41 // Implemented by subclasses to handle control messages
markdittmer638a1492016-02-19 01:11:5042 virtual bool OnControlMessageReceived(const Message& msg);
initial.commit09911bf2008-07-26 23:55:2943
markdittmer638a1492016-02-19 01:11:5044 // Listener implementation:
45 bool OnMessageReceived(const Message& msg) override;
initial.commit09911bf2008-07-26 23:55:2946
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.
markdittmer638a1492016-02-19 01:11:5050 virtual bool RouteMessage(const Message& msg);
initial.commit09911bf2008-07-26 23:55:2951
markdittmer638a1492016-02-19 01:11:5052 // Sender implementation:
53 bool Send(Message* msg) override;
initial.commit09911bf2008-07-26 23:55:2954
[email protected]0d78ec0e2014-04-08 23:35:2355 // Called to add a listener for a particular message routing ID.
56 // Returns true if succeeded.
markdittmer638a1492016-02-19 01:11:5057 bool AddRoute(int32_t routing_id, Listener* listener);
[email protected]0d78ec0e2014-04-08 23:35:2358
59 // Called to remove a listener for a particular message routing ID.
avia9aa7a82015-12-25 03:06:3160 void RemoveRoute(int32_t routing_id);
initial.commit09911bf2008-07-26 23:55:2961
rockotf62002a2016-09-15 00:08:5962 // Returns the Listener associated with |routing_id|.
63 Listener* GetRoute(int32_t routing_id);
64
initial.commit09911bf2008-07-26 23:55:2965 private:
66 // A list of all listeners with assigned routing IDs.
Brett Wilsonf976d3f2017-08-18 17:23:3967 base::IDMap<Listener*> routes_;
initial.commit09911bf2008-07-26 23:55:2968
[email protected]312ef322009-08-28 19:33:2969 DISALLOW_COPY_AND_ASSIGN(MessageRouter);
initial.commit09911bf2008-07-26 23:55:2970};
71
markdittmer638a1492016-02-19 01:11:5072} // namespace IPC
[email protected]130757672012-10-24 00:26:1973
markdittmer638a1492016-02-19 01:11:5074#endif // IPC_MESSAGE_ROUTER_H_