#include "extensions/renderer/dispatcher.h"
#include <stddef.h>
#include <algorithm>
#include <memory>
#include <string_view>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/debug/alias.h"
#include "base/debug/dump_without_crashing.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/lazy_instance.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/v8_value_converter.h"
#include "extensions/common/api/messaging/message.h"
#include "extensions/common/constants.h"
#include "extensions/common/cors_util.h"
#include "extensions/common/crash_keys.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_api.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/extension_urls.h"
#include "extensions/common/extensions_client.h"
#include "extensions/common/features/behavior_feature.h"
#include "extensions/common/features/feature.h"
#include "extensions/common/features/feature_channel.h"
#include "extensions/common/features/feature_developer_mode_only.h"
#include "extensions/common/features/feature_provider.h"
#include "extensions/common/features/feature_session_type.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/manifest_handlers/options_page_info.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/common/mojom/host_id.mojom.h"
#include "extensions/common/permissions/permission_set.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/switches.h"
#include "extensions/common/utils/extension_utils.h"
#include "extensions/grit/extensions_renderer_resources.h"
#include "extensions/renderer/api/messaging/native_renderer_messaging_service.h"
#include "extensions/renderer/content_watcher.h"
#include "extensions/renderer/dom_activity_logger.h"
#include "extensions/renderer/extension_frame_helper.h"
#include "extensions/renderer/extension_interaction_provider.h"
#include "extensions/renderer/extensions_renderer_api_provider.h"
#include "extensions/renderer/extensions_renderer_client.h"
#include "extensions/renderer/ipc_message_sender.h"
#include "extensions/renderer/isolated_world_manager.h"
#include "extensions/renderer/module_system.h"
#include "extensions/renderer/native_extension_bindings_system.h"
#include "extensions/renderer/renderer_extension_registry.h"
#include "extensions/renderer/safe_builtins.h"
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/script_context_set.h"
#include "extensions/renderer/script_injection_manager.h"
#include "extensions/renderer/service_worker_data.h"
#include "extensions/renderer/shared_l10n_map.h"
#include "extensions/renderer/static_v8_external_one_byte_string_resource.h"
#include "extensions/renderer/trace_util.h"
#include "extensions/renderer/v8_helpers.h"
#include "extensions/renderer/worker_script_context_set.h"
#include "extensions/renderer/worker_thread_dispatcher.h"
#include "extensions/renderer/worker_thread_util.h"
#include "gin/converter.h"
#include "services/network/public/mojom/cors.mojom.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/mojom/frame/user_activation_notification_type.mojom.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/public/web/modules/service_worker/web_service_worker_context_proxy.h"
#include "third_party/blink/public/web/web_custom_element.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_frame.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_script_controller.h"
#include "third_party/blink/public/web/web_security_policy.h"
#include "third_party/blink/public/web/web_settings.h"
#include "third_party/blink/public/web/web_v8_features.h"
#include "third_party/blink/public/web/web_view.h"
#include "ui/base/resource/resource_bundle.h"
#include "v8/include/v8-context.h"
#include "v8/include/v8-function.h"
#include "v8/include/v8-object.h"
#include "v8/include/v8-primitive.h"
WebDocument;
WebSecurityPolicy;
WebString;
WebView;
RenderThread;
namespace extensions {
namespace {
static const char kOnSuspendEvent[] = …;
static const char kOnSuspendCanceledEvent[] = …;
enum class ExtensionRendererLoadStatus { … };
bool ExtensionAPIEnabledForServiceWorkerScript(const GURL& scope,
const GURL& script_url) { … }
void CallModuleMethod(const std::string& module_name,
const std::string& method_name,
const base::Value::List* args,
ScriptContext* context) { … }
class HandleScopeHelper { … };
base::LazyInstance<WorkerScriptContextSet>::DestructorAtExit
g_worker_script_context_set = …;
scoped_refptr<Extension> ConvertToExtension(
mojom::ExtensionLoadedParamsPtr params,
int context_id,
std::string* error) { … }
}
Dispatcher::PendingServiceWorker::PendingServiceWorker(
blink::WebServiceWorkerContextProxy* context_proxy)
: … { … }
Dispatcher::PendingServiceWorker::~PendingServiceWorker() = default;
Dispatcher::Dispatcher(
std::vector<std::unique_ptr<const ExtensionsRendererAPIProvider>>
api_providers)
: … { … }
Dispatcher::~Dispatcher() { … }
WorkerScriptContextSet* Dispatcher::GetWorkerScriptContextSet() { … }
bool Dispatcher::ShouldNotifyServiceWorkerOnWebSocketActivity(
v8::Local<v8::Context> v8_context) { … }
void Dispatcher::OnRenderThreadStarted(content::RenderThread* thread) { … }
void Dispatcher::OnRenderFrameCreated(content::RenderFrame* render_frame) { … }
bool Dispatcher::IsExtensionActive(const ExtensionId& extension_id) const { … }
void Dispatcher::DidCreateScriptContext(
blink::WebLocalFrame* frame,
const v8::Local<v8::Context>& v8_context,
int32_t world_id) { … }
void Dispatcher::DidInitializeServiceWorkerContextOnWorkerThread(
blink::WebServiceWorkerContextProxy* context_proxy,
const GURL& service_worker_scope,
const GURL& script_url) { … }
void Dispatcher::WillEvaluateServiceWorkerOnWorkerThread(
blink::WebServiceWorkerContextProxy* context_proxy,
v8::Local<v8::Context> v8_context,
int64_t service_worker_version_id,
const GURL& service_worker_scope,
const GURL& script_url,
const blink::ServiceWorkerToken& service_worker_token) { … }
void Dispatcher::WillReleaseScriptContext(
blink::WebLocalFrame* frame,
const v8::Local<v8::Context>& v8_context,
int32_t world_id) { … }
void Dispatcher::DidStartServiceWorkerContextOnWorkerThread(
int64_t service_worker_version_id,
const GURL& service_worker_scope,
const GURL& script_url) { … }
void Dispatcher::WillDestroyServiceWorkerContextOnWorkerThread(
v8::Local<v8::Context> v8_context,
int64_t service_worker_version_id,
const GURL& service_worker_scope,
const GURL& script_url) { … }
void Dispatcher::DidCreateDocumentElement(blink::WebLocalFrame* frame) { … }
void Dispatcher::RunScriptsAtDocumentStart(content::RenderFrame* render_frame) { … }
void Dispatcher::RunScriptsAtDocumentEnd(content::RenderFrame* render_frame) { … }
void Dispatcher::RunScriptsAtDocumentIdle(content::RenderFrame* render_frame) { … }
void Dispatcher::DispatchEventHelper(
const mojom::HostID& host_id,
const std::string& event_name,
const base::Value::List& event_args,
mojom::EventFilteringInfoPtr filtering_info) const { … }
void Dispatcher::InvokeModuleSystemMethod(content::RenderFrame* render_frame,
const ExtensionId& extension_id,
const std::string& module_name,
const std::string& function_name,
const base::Value::List& args) { … }
void Dispatcher::ExecuteDeclarativeScript(content::RenderFrame* render_frame,
int tab_id,
const ExtensionId& extension_id,
const std::string& script_id,
const GURL& url) { … }
void Dispatcher::ExecuteCode(mojom::ExecuteCodeParamsPtr param,
mojom::LocalFrame::ExecuteCodeCallback callback,
content::RenderFrame* render_frame) { … }
void Dispatcher::RegisterMojoInterfaces(
blink::AssociatedInterfaceRegistry* associated_interfaces) { … }
void Dispatcher::UnregisterMojoInterfaces(
blink::AssociatedInterfaceRegistry* associated_interfaces) { … }
void Dispatcher::OnRendererAssociatedRequest(
mojo::PendingAssociatedReceiver<mojom::Renderer> receiver) { … }
void Dispatcher::OnEventDispatcherRequest(
mojo::PendingAssociatedReceiver<mojom::EventDispatcher> dispatcher) { … }
void Dispatcher::ActivateExtension(const ExtensionId& extension_id) { … }
void Dispatcher::LoadExtensions(
std::vector<mojom::ExtensionLoadedParamsPtr> loaded_extensions) { … }
void Dispatcher::UnloadExtension(const ExtensionId& extension_id) { … }
void Dispatcher::SuspendExtension(
const ExtensionId& extension_id,
mojom::Renderer::SuspendExtensionCallback callback) { … }
void Dispatcher::CancelSuspendExtension(const ExtensionId& extension_id) { … }
void Dispatcher::SetSystemFont(const std::string& font_family,
const std::string& font_size) { … }
void Dispatcher::SetWebViewPartitionID(const std::string& partition_id) { … }
void Dispatcher::SetScriptingAllowlist(
const std::vector<ExtensionId>& extension_ids) { … }
void Dispatcher::UpdateDefaultPolicyHostRestrictions(
URLPatternSet default_policy_blocked_hosts,
URLPatternSet default_policy_allowed_hosts) { … }
void Dispatcher::UpdateUserScriptWorlds(
std::vector<mojom::UserScriptWorldInfoPtr> infos) { … }
void Dispatcher::ClearUserScriptWorldConfig(
const ExtensionId& extension_id,
const std::optional<std::string>& world_id) { … }
void Dispatcher::UpdateUserHostRestrictions(URLPatternSet user_blocked_hosts,
URLPatternSet user_allowed_hosts) { … }
void Dispatcher::UpdateTabSpecificPermissions(const ExtensionId& extension_id,
URLPatternSet new_hosts,
int tab_id,
bool update_origin_allowlist) { … }
void Dispatcher::UpdateUserScripts(
base::ReadOnlySharedMemoryRegion shared_memory,
mojom::HostIDPtr host_id) { … }
void Dispatcher::ClearTabSpecificPermissions(
const std::vector<ExtensionId>& extension_ids,
int tab_id,
bool update_origin_allowlist) { … }
void Dispatcher::WatchPages(const std::vector<std::string>& css_selectors) { … }
void Dispatcher::DispatchEvent(mojom::DispatchEventParamsPtr params,
base::Value::List event_args,
DispatchEventCallback callback) { … }
void Dispatcher::SetDeveloperMode(bool current_developer_mode) { … }
void Dispatcher::SetSessionInfo(version_info::Channel channel,
mojom::FeatureSessionType session_type,
bool is_lock_screen_context) { … }
void Dispatcher::ShouldSuspend(ShouldSuspendCallback callback) { … }
void Dispatcher::TransferBlobs(TransferBlobsCallback callback) { … }
void Dispatcher::UpdatePermissions(const ExtensionId& extension_id,
PermissionSet active_permissions,
PermissionSet withheld_permissions,
URLPatternSet policy_blocked_hosts,
URLPatternSet policy_allowed_hosts,
bool uses_default_policy_host_restrictions) { … }
void Dispatcher::SetActivityLoggingEnabled(bool enabled) { … }
void Dispatcher::OnUserScriptsUpdated(const mojom::HostID& changed_host) { … }
ScriptContextSetIterable* Dispatcher::GetScriptContextSet() { … }
void Dispatcher::UpdateActiveExtensions() { … }
void Dispatcher::InitOriginPermissions(const Extension* extension) { … }
void Dispatcher::UpdateOriginPermissions(const Extension& extension) { … }
void Dispatcher::EnableCustomElementAllowlist() { … }
void Dispatcher::UpdateAllBindings(bool api_permissions_changed) { … }
void Dispatcher::UpdateBindingsForExtension(const Extension& extension) { … }
void Dispatcher::RegisterNativeHandlers(
ModuleSystem* module_system,
ScriptContext* context,
NativeExtensionBindingsSystem* bindings_system,
V8SchemaRegistry* v8_schema_registry) { … }
void Dispatcher::PopulateSourceMap() { … }
bool Dispatcher::IsWithinPlatformApp() { … }
void Dispatcher::RequireGuestViewModules(ScriptContext* context) { … }
std::unique_ptr<NativeExtensionBindingsSystem> Dispatcher::CreateBindingsSystem(
NativeExtensionBindingsSystem::Delegate* delegate,
std::unique_ptr<IPCMessageSender> ipc_sender) { … }
void Dispatcher::ResumeEvaluationOnWorkerThread(
const ExtensionId& extension_id) { … }
}