ipc_message_sender.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2017 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 EXTENSIONS_RENDERER_IPC_MESSAGE_SENDER_H_
  5. #define EXTENSIONS_RENDERER_IPC_MESSAGE_SENDER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "extensions/common/extension_id.h"
  9. #include "extensions/common/mojom/frame.mojom-forward.h"
  10. #include "extensions/renderer/bindings/api_binding_types.h"
  11. struct ExtensionHostMsg_APIActionOrEvent_Params;
  12. namespace base {
  13. class DictionaryValue;
  14. }
  15. namespace extensions {
  16. class ScriptContext;
  17. class WorkerThreadDispatcher;
  18. struct Message;
  19. struct MessageTarget;
  20. struct PortId;
  21. // A class to handle sending bindings-related messages to the browser. Different
  22. // versions handle main thread vs. service worker threads.
  23. class IPCMessageSender {
  24. public:
  25. IPCMessageSender(const IPCMessageSender&) = delete;
  26. IPCMessageSender& operator=(const IPCMessageSender&) = delete;
  27. virtual ~IPCMessageSender();
  28. // Used to distinguish API calls & events from each other in activity log.
  29. enum class ActivityLogCallType { APICALL, EVENT };
  30. // Sends a request message to the browser.
  31. virtual void SendRequestIPC(ScriptContext* context,
  32. mojom::RequestParamsPtr params) = 0;
  33. // Handles sending any additional messages required after receiving a response
  34. // to a request.
  35. virtual void SendOnRequestResponseReceivedIPC(int request_id) = 0;
  36. // Sends a message to add/remove an unfiltered listener.
  37. virtual void SendAddUnfilteredEventListenerIPC(
  38. ScriptContext* context,
  39. const std::string& event_name) = 0;
  40. virtual void SendRemoveUnfilteredEventListenerIPC(
  41. ScriptContext* context,
  42. const std::string& event_name) = 0;
  43. // Sends a message to add/remove a lazy unfiltered listener.
  44. virtual void SendAddUnfilteredLazyEventListenerIPC(
  45. ScriptContext* context,
  46. const std::string& event_name) = 0;
  47. virtual void SendRemoveUnfilteredLazyEventListenerIPC(
  48. ScriptContext* context,
  49. const std::string& event_name) = 0;
  50. // Sends a message to add/remove a filtered listener.
  51. virtual void SendAddFilteredEventListenerIPC(
  52. ScriptContext* context,
  53. const std::string& event_name,
  54. const base::DictionaryValue& filter,
  55. bool is_lazy) = 0;
  56. virtual void SendRemoveFilteredEventListenerIPC(
  57. ScriptContext* context,
  58. const std::string& event_name,
  59. const base::DictionaryValue& filter,
  60. bool remove_lazy_listener) = 0;
  61. // Opens a message channel to the specified target.
  62. virtual void SendOpenMessageChannel(ScriptContext* script_context,
  63. const PortId& port_id,
  64. const MessageTarget& target,
  65. const std::string& channel_name) = 0;
  66. // Sends a message to open/close a mesage port or send a message to an
  67. // existing port.
  68. virtual void SendOpenMessagePort(int routing_id, const PortId& port_id) = 0;
  69. virtual void SendCloseMessagePort(int routing_id,
  70. const PortId& port_id,
  71. bool close_channel) = 0;
  72. virtual void SendPostMessageToPort(const PortId& port_id,
  73. const Message& message) = 0;
  74. // Sends a message indicating that a receiver of a message indicated that it
  75. // plans to send a response later.
  76. virtual void SendMessageResponsePending(int routing_id,
  77. const PortId& port_id) = 0;
  78. // Sends activityLog IPC to the browser process.
  79. virtual void SendActivityLogIPC(
  80. const ExtensionId& extension_id,
  81. ActivityLogCallType call_type,
  82. const ExtensionHostMsg_APIActionOrEvent_Params& params) = 0;
  83. // Creates an IPCMessageSender for use on the main thread.
  84. static std::unique_ptr<IPCMessageSender> CreateMainThreadIPCMessageSender();
  85. // Creates an IPCMessageSender for use on a worker thread.
  86. static std::unique_ptr<IPCMessageSender> CreateWorkerThreadIPCMessageSender(
  87. WorkerThreadDispatcher* dispatcher,
  88. int64_t service_worker_version_id);
  89. protected:
  90. IPCMessageSender();
  91. };
  92. } // namespace extensions
  93. #endif // EXTENSIONS_RENDERER_IPC_MESSAGE_SENDER_H_