message_router.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #ifndef IPC_MESSAGE_ROUTER_H_
  5. #define IPC_MESSAGE_ROUTER_H_
  6. #include <stdint.h>
  7. #include "base/component_export.h"
  8. #include "base/containers/id_map.h"
  9. #include "ipc/ipc_listener.h"
  10. #include "ipc/ipc_sender.h"
  11. // The MessageRouter handles all incoming messages sent to it by routing them
  12. // to the correct listener. Routing is based on the Message's routing ID.
  13. // Since routing IDs are typically assigned asynchronously by the browser
  14. // process, the MessageRouter has the notion of pending IDs for listeners that
  15. // have not yet been assigned a routing ID.
  16. //
  17. // When a message arrives, the routing ID is used to index the set of routes to
  18. // find a listener. If a listener is found, then the message is passed to it.
  19. // Otherwise, the message is ignored if its routing ID is not equal to
  20. // MSG_ROUTING_CONTROL.
  21. //
  22. // The MessageRouter supports the IPC::Sender interface for outgoing messages,
  23. // but does not define a meaningful implementation of it. The subclass of
  24. // MessageRouter is intended to provide that if appropriate.
  25. //
  26. // The MessageRouter can be used as a concrete class provided its Send method
  27. // is not called and it does not receive any control messages.
  28. namespace IPC {
  29. class COMPONENT_EXPORT(IPC) MessageRouter : public Listener, public Sender {
  30. public:
  31. MessageRouter();
  32. MessageRouter(const MessageRouter&) = delete;
  33. MessageRouter& operator=(const MessageRouter&) = delete;
  34. ~MessageRouter() override;
  35. // Implemented by subclasses to handle control messages
  36. virtual bool OnControlMessageReceived(const Message& msg);
  37. // Listener implementation:
  38. bool OnMessageReceived(const Message& msg) override;
  39. // Like OnMessageReceived, except it only handles routed messages. Returns
  40. // true if the message was dispatched, or false if there was no listener for
  41. // that route id.
  42. virtual bool RouteMessage(const Message& msg);
  43. // Sender implementation:
  44. bool Send(Message* msg) override;
  45. // Called to add a listener for a particular message routing ID.
  46. // Returns true if succeeded.
  47. bool AddRoute(int32_t routing_id, Listener* listener);
  48. // Called to remove a listener for a particular message routing ID.
  49. void RemoveRoute(int32_t routing_id);
  50. // Returns the Listener associated with |routing_id|.
  51. Listener* GetRoute(int32_t routing_id);
  52. private:
  53. // A list of all listeners with assigned routing IDs.
  54. base::IDMap<Listener*> routes_;
  55. };
  56. } // namespace IPC
  57. #endif // IPC_MESSAGE_ROUTER_H_