#include "extensions/renderer/api/messaging/native_renderer_messaging_service.h"
#include <map>
#include <memory>
#include <string>
#include <utility>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/supports_user_data.h"
#include "content/public/common/content_constants.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/v8_value_converter.h"
#include "extensions/common/api/messaging/message.h"
#include "extensions/common/api/messaging/messaging_endpoint.h"
#include "extensions/common/api/messaging/port_id.h"
#include "extensions/common/features/feature.h"
#include "extensions/common/manifest_handlers/externally_connectable.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/common/mojom/event_dispatcher.mojom.h"
#include "extensions/common/mojom/message_port.mojom-shared.h"
#include "extensions/common/utils/extension_utils.h"
#include "extensions/renderer/api/messaging/message_target.h"
#include "extensions/renderer/api/messaging/messaging_util.h"
#include "extensions/renderer/api_activity_logger.h"
#include "extensions/renderer/bindings/api_binding_util.h"
#include "extensions/renderer/bindings/get_per_context_data.h"
#include "extensions/renderer/extension_interaction_provider.h"
#include "extensions/renderer/get_script_context.h"
#include "extensions/renderer/ipc_message_sender.h"
#include "extensions/renderer/native_extension_bindings_system.h"
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/script_context_set.h"
#include "extensions/renderer/script_context_set_iterable.h"
#include "gin/data_object_builder.h"
#include "gin/handle.h"
#include "gin/per_context_data.h"
#include "ipc/ipc_mojo_bootstrap.h"
#include "third_party/blink/public/mojom/frame/user_activation_notification_type.mojom.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_scoped_window_focus_allowed_indicator.h"
#include "v8/include/v8-context.h"
#include "v8/include/v8-local-handle.h"
#include "v8/include/v8-persistent-handle.h"
UserActivationNotificationType;
namespace extensions {
namespace {
struct MessagingPerContextData : public base::SupportsUserData::Data { … };
constexpr char MessagingPerContextData::kPerContextDataKey[];
bool ScriptContextIsValid(ScriptContext* script_context) { … }
}
class NativeRendererMessagingService::MessagePortScope
: public mojom::MessagePort { … };
const void* const kRenderFrameMessagePortsKey = …;
class NativeRendererMessagingService::RenderFrameMessagePorts
: public base::SupportsUserData::Data,
public NativeRendererMessagingService::MessagePortScope { … };
NativeRendererMessagingService::NativeRendererMessagingService(
NativeExtensionBindingsSystem* bindings_system)
: … { … }
NativeRendererMessagingService::~NativeRendererMessagingService() = default;
void NativeRendererMessagingService::DispatchOnConnect(
ScriptContextSetIterable* context_set,
const PortId& target_port_id,
mojom::ChannelType channel_type,
const std::string& channel_name,
const TabConnectionInfo& source,
const ExternalConnectionInfo& info,
mojo::PendingAssociatedReceiver<extensions::mojom::MessagePort> port,
mojo::PendingAssociatedRemote<extensions::mojom::MessagePortHost> port_host,
content::RenderFrame* restrict_to_render_frame,
ConnectCallback callback) { … }
void NativeRendererMessagingService::DeliverMessage(
ScriptContextSetIterable* context_set,
const PortId& target_port_id,
const Message& message,
content::RenderFrame* restrict_to_render_frame) { … }
void NativeRendererMessagingService::DispatchOnDisconnect(
ScriptContextSetIterable* context_set,
const PortId& port_id,
const std::string& error_message,
content::RenderFrame* restrict_to_render_frame) { … }
gin::Handle<GinPort> NativeRendererMessagingService::Connect(
ScriptContext* script_context,
const MessageTarget& target,
const std::string& channel_name,
mojom::SerializationFormat format) { … }
v8::Local<v8::Promise> NativeRendererMessagingService::SendOneTimeMessage(
ScriptContext* script_context,
const MessageTarget& target,
mojom::ChannelType channel_type,
const Message& message,
binding::AsyncResponseType async_type,
v8::Local<v8::Function> response_callback) { … }
void NativeRendererMessagingService::PostMessageToPort(
v8::Local<v8::Context> context,
const PortId& port_id,
std::unique_ptr<Message> message) { … }
void NativeRendererMessagingService::ClosePort(v8::Local<v8::Context> context,
const PortId& port_id) { … }
gin::Handle<GinPort> NativeRendererMessagingService::CreatePortForTesting(
ScriptContext* script_context,
const std::string& channel_name,
const mojom::ChannelType channel_type,
const PortId& port_id,
mojo::PendingAssociatedRemote<mojom::MessagePort>& message_port_remote,
mojo::PendingAssociatedReceiver<mojom::MessagePortHost>&
message_port_host_receiver) { … }
gin::Handle<GinPort> NativeRendererMessagingService::GetPortForTesting(
ScriptContext* script_context,
const PortId& port_id) { … }
bool NativeRendererMessagingService::HasPortForTesting(
ScriptContext* script_context,
const PortId& port_id) { … }
void NativeRendererMessagingService::BindPortForTesting(
ScriptContext* script_context,
const PortId& port_id,
mojo::PendingAssociatedRemote<mojom::MessagePort>& message_port_remote,
mojo::PendingAssociatedReceiver<mojom::MessagePortHost>&
message_port_host_receiver) { … }
void NativeRendererMessagingService::DispatchOnConnectToScriptContext(
const PortId& target_port_id,
mojom::ChannelType channel_type,
const std::string& channel_name,
const TabConnectionInfo& source,
const ExternalConnectionInfo& info,
bool* port_created,
ScriptContext* script_context) { … }
void NativeRendererMessagingService::DeliverMessageToScriptContext(
const Message& message,
const PortId& target_port_id,
ScriptContext* script_context) { … }
void NativeRendererMessagingService::DeliverMessageToWorker(
const Message& message,
const PortId& target_port_id,
ScriptContext* script_context) { … }
void NativeRendererMessagingService::DeliverMessageToBackgroundPage(
const Message& message,
const PortId& target_port_id,
ScriptContext* script_context) { … }
void NativeRendererMessagingService::DispatchOnDisconnectToScriptContext(
const PortId& port_id,
const std::string& error_message,
ScriptContext* script_context) { … }
bool NativeRendererMessagingService::ContextHasMessagePort(
ScriptContext* script_context,
const PortId& port_id) { … }
void NativeRendererMessagingService::DispatchOnConnectToListeners(
ScriptContext* script_context,
const PortId& target_port_id,
const ExtensionId& target_extension_id,
mojom::ChannelType channel_type,
const std::string& channel_name,
const TabConnectionInfo& source,
const ExternalConnectionInfo& info,
const std::string& event_name) { … }
void NativeRendererMessagingService::DispatchOnMessageToListeners(
ScriptContext* script_context,
const Message& message,
const PortId& target_port_id) { … }
void NativeRendererMessagingService::DispatchOnDisconnectToListeners(
ScriptContext* script_context,
const PortId& port_id,
const std::string& error_message) { … }
gin::Handle<GinPort> NativeRendererMessagingService::CreatePort(
ScriptContext* script_context,
const std::string& channel_name,
const mojom::ChannelType channel_type,
const PortId& port_id) { … }
gin::Handle<GinPort> NativeRendererMessagingService::GetPort(
ScriptContext* script_context,
const PortId& port_id) { … }
void NativeRendererMessagingService::CloseMessagePort(
ScriptContext* script_context,
const PortId& port_id,
bool close_channel) { … }
NativeRendererMessagingService::MessagePortScope*
NativeRendererMessagingService::GetMessagePortScope(
content::RenderFrame* render_frame) { … }
mojom::MessagePortHost* NativeRendererMessagingService::GetMessagePortHost(
ScriptContext* script_context,
const PortId& port_id) { … }
mojom::MessagePortHost*
NativeRendererMessagingService::GetMessagePortHostIfExists(
ScriptContext* script_context,
const PortId& port_id) { … }
base::SafeRef<NativeRendererMessagingService>
NativeRendererMessagingService::AsSafeRef() { … }
}