diff options
author | markdittmer <markdittmer@chromium.org> | 2016-02-18 17:11:50 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-19 01:13:45 +0000 |
commit | 638a149aec3475147a3a7f31bfdb1ed4985298d5 (patch) | |
tree | 5a5122a155fc73a98935d0545aed2853b11dbcc5 /ipc | |
parent | 3d6b5d3df0a988475328dc5a4aabce5c4f4aa211 (diff) | |
download | chromium_src-638a149aec3475147a3a7f31bfdb1ed4985298d5.zip chromium_src-638a149aec3475147a3a7f31bfdb1ed4985298d5.tar.gz chromium_src-638a149aec3475147a3a7f31bfdb1ed4985298d5.tar.bz2 |
Refactor content/common/message_router to ipc/message_router
BUG=586366
Review URL: https://codereview.chromium.org/1710973002
Cr-Commit-Position: refs/heads/master@{#376326}
Diffstat (limited to 'ipc')
-rw-r--r-- | ipc/BUILD.gn | 2 | ||||
-rw-r--r-- | ipc/ipc.gypi | 2 | ||||
-rw-r--r-- | ipc/message_router.cc | 55 | ||||
-rw-r--r-- | ipc/message_router.h | 71 |
4 files changed, 130 insertions, 0 deletions
diff --git a/ipc/BUILD.gn b/ipc/BUILD.gn index 2451d6f..c3d3bb4 100644 --- a/ipc/BUILD.gn +++ b/ipc/BUILD.gn @@ -91,6 +91,8 @@ component("ipc") { "message_filter.h", "message_filter_router.cc", "message_filter_router.h", + "message_router.cc", + "message_router.h", "param_traits_log_macros.h", "param_traits_macros.h", "param_traits_read_macros.h", diff --git a/ipc/ipc.gypi b/ipc/ipc.gypi index 6b7cf93..da570d2 100644 --- a/ipc/ipc.gypi +++ b/ipc/ipc.gypi @@ -96,6 +96,8 @@ 'message_filter.h', 'message_filter_router.cc', 'message_filter_router.h', + 'message_router.cc', + 'message_router.h', 'param_traits_log_macros.h', 'param_traits_macros.h', 'param_traits_read_macros.h', diff --git a/ipc/message_router.cc b/ipc/message_router.cc new file mode 100644 index 0000000..aaf4a1f --- /dev/null +++ b/ipc/message_router.cc @@ -0,0 +1,55 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ipc/message_router.h" + +#include "ipc/ipc_message.h" + +namespace IPC { + +MessageRouter::MessageRouter() {} + +MessageRouter::~MessageRouter() {} + +bool MessageRouter::OnControlMessageReceived(const IPC::Message& msg) { + NOTREACHED() + << "should override in subclass if you care about control messages"; + return false; +} + +bool MessageRouter::Send(IPC::Message* msg) { + NOTREACHED() + << "should override in subclass if you care about sending messages"; + return false; +} + +bool MessageRouter::AddRoute(int32_t routing_id, IPC::Listener* listener) { + if (routes_.Lookup(routing_id)) { + DLOG(ERROR) << "duplicate routing ID"; + return false; + } + routes_.AddWithID(listener, routing_id); + return true; +} + +void MessageRouter::RemoveRoute(int32_t routing_id) { + routes_.Remove(routing_id); +} + +bool MessageRouter::OnMessageReceived(const IPC::Message& msg) { + if (msg.routing_id() == MSG_ROUTING_CONTROL) + return OnControlMessageReceived(msg); + + return RouteMessage(msg); +} + +bool MessageRouter::RouteMessage(const IPC::Message& msg) { + IPC::Listener* listener = routes_.Lookup(msg.routing_id()); + if (!listener) + return false; + + return listener->OnMessageReceived(msg); +} + +} // namespace IPC diff --git a/ipc/message_router.h b/ipc/message_router.h new file mode 100644 index 0000000..db4bc27 --- /dev/null +++ b/ipc/message_router.h @@ -0,0 +1,71 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IPC_MESSAGE_ROUTER_H_ +#define IPC_MESSAGE_ROUTER_H_ + +#include <stdint.h> + +#include "base/id_map.h" +#include "base/macros.h" +#include "ipc/ipc_export.h" +#include "ipc/ipc_listener.h" +#include "ipc/ipc_sender.h" + +// The MessageRouter handles all incoming messages sent to it by routing them +// to the correct listener. Routing is based on the Message's routing ID. +// Since routing IDs are typically assigned asynchronously by the browser +// process, the MessageRouter has the notion of pending IDs for listeners that +// have not yet been assigned a routing ID. +// +// When a message arrives, the routing ID is used to index the set of routes to +// find a listener. If a listener is found, then the message is passed to it. +// Otherwise, the message is ignored if its routing ID is not equal to +// MSG_ROUTING_CONTROL. +// +// The MessageRouter supports the IPC::Sender interface for outgoing messages, +// but does not define a meaningful implementation of it. The subclass of +// MessageRouter is intended to provide that if appropriate. +// +// The MessageRouter can be used as a concrete class provided its Send method +// is not called and it does not receive any control messages. + +namespace IPC { + +class IPC_EXPORT MessageRouter : public Listener, public Sender { + public: + MessageRouter(); + ~MessageRouter() override; + + // Implemented by subclasses to handle control messages + virtual bool OnControlMessageReceived(const Message& msg); + + // Listener implementation: + bool OnMessageReceived(const Message& msg) override; + + // Like OnMessageReceived, except it only handles routed messages. Returns + // true if the message was dispatched, or false if there was no listener for + // that route id. + virtual bool RouteMessage(const Message& msg); + + // Sender implementation: + bool Send(Message* msg) override; + + // Called to add a listener for a particular message routing ID. + // Returns true if succeeded. + bool AddRoute(int32_t routing_id, Listener* listener); + + // Called to remove a listener for a particular message routing ID. + void RemoveRoute(int32_t routing_id); + + private: + // A list of all listeners with assigned routing IDs. + IDMap<Listener> routes_; + + DISALLOW_COPY_AND_ASSIGN(MessageRouter); +}; + +} // namespace IPC + +#endif // IPC_MESSAGE_ROUTER_H_ |