// Copyright 2014 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef EXTENSIONS_RENDERER_API_MESSAGING_NATIVE_RENDERER_MESSAGING_SERVICE_H_ #define EXTENSIONS_RENDERER_API_MESSAGING_NATIVE_RENDERER_MESSAGING_SERVICE_H_ #include <string> #include "base/memory/raw_ptr.h" #include "base/memory/safe_ref.h" #include "extensions/common/extension_id.h" #include "extensions/common/mojom/message_port.mojom.h" #include "extensions/renderer/api/messaging/gin_port.h" #include "extensions/renderer/api/messaging/one_time_message_handler.h" #include "extensions/renderer/bindings/api_binding_types.h" #include "gin/handle.h" #include "mojo/public/cpp/bindings/associated_receiver_set.h" #include "v8/include/v8-forward.h" namespace content { class RenderFrame; } namespace extensions { namespace mojom { enum class ChannelType; enum class SerializationFormat; } // namespace mojom class NativeExtensionBindingsSystem; class ScriptContextSetIterable; struct Message; struct MessageTarget; struct PortId; // The messaging service to handle dispatching extension messages and connection // events to different contexts. // This primarily handles long-lived port-based communications (like // runtime.connect). A basic flow will create an "opener" port and one ore more // "receiver" ports in different contexts (and possibly processes). This class // manages the communication with the browser to forward these messages along. // From JavaScript, a basic flow would be: // // page1.js // var port = chrome.runtime.connect(); // port.onMessage.addListener(function() { <handle message> }); // port.postMessage('hi!'); // <eventually> port.disconnect(); // // page2.js // chrome.runtime.onConnect.addListener(function(port) { // port.onMessage.addListener(function() { <handle message> }); // port.postMessage('hey!'); // }); // This causes the following steps in the messaging service: // Connection: // * connect() triggers OpenChannelToExtension, which notifies the browser of // a new connection. // * The browser dispatches OnConnect messages to different renderers. If a // renderer has a listener, it will respond with an OpenMessagePort message. // If no renderer has a listener, the browser will close the port. // Message Posting // * Calls to postMessage() trigger a PostMessageToPort messge to the browser. // * The browser sends a DeliverMessage message to listening renderers. These // then dispatch the onMessage event to listeners. // Disconnecting // * disconnect() calls result in sending a CloseMessagePort message to the // browser. // * The browser then sends a DispatchOnDisconnect message to other renderers, // which triggers the onDisconnect() event. // TODO(devlin): This is a pretty large comment for a class, and it documents // browser/renderer interaction. I wonder if this would be better in a // messaging.md document? class NativeRendererMessagingService : public GinPort::Delegate { … }; } // namespace extensions #endif // EXTENSIONS_RENDERER_API_MESSAGING_NATIVE_RENDERER_MESSAGING_SERVICE_H_