// Copyright 2017 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_ONE_TIME_MESSAGE_HANDLER_H_ #define EXTENSIONS_RENDERER_API_MESSAGING_ONE_TIME_MESSAGE_HANDLER_H_ #include <stdint.h> #include <memory> #include <optional> #include <string> #include "base/memory/raw_ptr.h" #include "base/memory/weak_ptr.h" #include "extensions/common/mojom/message_port.mojom.h" #include "extensions/renderer/bindings/api_binding_types.h" #include "v8/include/v8-forward.h" namespace base { class Value; } namespace gin { class Arguments; } namespace extensions { namespace mojom { enum class ChannelType; } class NativeExtensionBindingsSystem; class ScriptContext; struct Message; struct MessageTarget; struct PortId; // A class for handling one-time message communication, including // runtime.sendMessage and extension.sendRequest. These methods use the same // underlying architecture as long-lived port-based communications (like // runtime.connect), but are exposed through a simpler API. // A basic flow will be from an "opener" (the original sender) and a "receiver" // (the event listener), which will be in two separate contexts (and potentially // renderer processes). The flow is outlined below: // // chrome.runtime.sendMessage( // initiates the sendMessage flow, triggering // // SendMessage(). // {foo: bar}, // The data sent with SendMessage(). // function() { ... }); // The response callback in SendMessage(). // // This creates a new opener port in the context, and posts a message to it // with the data. The browser then dispatches this to other renderers. // // In another context, we have: // chrome.runtime.onMessage.addListener(function(message, sender, reply) { // ... // reply(...); // }); // // When the renderer receives the connection message, we will create a // new receiver port in this context via AddReceiver(). // When the message comes in, we reply with DeliverMessage() to the receiver's // port ID. // If the receiver replies via the reply callback, it will send a new message // back along the port to the browser. The browser then sends this message back // to the opener's renderer, where it is delivered via DeliverMessage(). // // This concludes the one-time message flow. class OneTimeMessageHandler { … }; } // namespace extensions #endif // EXTENSIONS_RENDERER_API_MESSAGING_ONE_TIME_MESSAGE_HANDLER_H_