michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef CHROME_COMMON_MESSAGE_ROUTER_H__ michael@0: #define CHROME_COMMON_MESSAGE_ROUTER_H__ michael@0: michael@0: #include "base/id_map.h" michael@0: #include "chrome/common/ipc_channel.h" michael@0: michael@0: // The MessageRouter handles all incoming messages sent to it by routing them michael@0: // to the correct listener. Routing is based on the Message's routing ID. michael@0: // Since routing IDs are typically assigned asynchronously by the browser michael@0: // process, the MessageRouter has the notion of pending IDs for listeners that michael@0: // have not yet been assigned a routing ID. michael@0: // michael@0: // When a message arrives, the routing ID is used to index the set of routes to michael@0: // find a listener. If a listener is found, then the message is passed to it. michael@0: // Otherwise, the message is ignored if its routing ID is not equal to michael@0: // MSG_ROUTING_CONTROL. michael@0: // michael@0: // The MessageRouter supports the IPC::Message::Sender interface for outgoing michael@0: // messages, but does not define a meaningful implementation of it. The michael@0: // subclass of MessageRouter is intended to provide that if appropriate. michael@0: // michael@0: // The MessageRouter can be used as a concrete class provided its Send method michael@0: // is not called and it does not receive any control messages. michael@0: michael@0: class MessageRouter : public IPC::Channel::Listener, michael@0: public IPC::Message::Sender { michael@0: public: michael@0: MessageRouter() {} michael@0: virtual ~MessageRouter() {} michael@0: michael@0: // Implemented by subclasses to handle control messages michael@0: virtual void OnControlMessageReceived(const IPC::Message& msg); michael@0: michael@0: // IPC::Channel::Listener implementation: michael@0: virtual void OnMessageReceived(const IPC::Message& msg); michael@0: michael@0: // Like OnMessageReceived, except it only handles routed messages. Returns michael@0: // true if the message was dispatched, or false if there was no listener for michael@0: // that route id. michael@0: virtual bool RouteMessage(const IPC::Message& msg); michael@0: michael@0: // IPC::Message::Sender implementation: michael@0: virtual bool Send(IPC::Message* msg); michael@0: michael@0: // Called to add/remove a listener for a particular message routing ID. michael@0: void AddRoute(int32_t routing_id, IPC::Channel::Listener* listener); michael@0: void RemoveRoute(int32_t routing_id); michael@0: michael@0: private: michael@0: // A list of all listeners with assigned routing IDs. michael@0: IDMap routes_; michael@0: michael@0: DISALLOW_EVIL_CONSTRUCTORS(MessageRouter); michael@0: }; michael@0: michael@0: #endif // CHROME_COMMON_MESSAGE_ROUTER_H__