File: service_worker_data.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (171 lines) | stat: -rw-r--r-- 6,868 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/service_worker_data.h"

#include "extensions/common/extension.h"
#include "extensions/common/extension_messages.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/renderer/dispatcher.h"
#include "extensions/renderer/extension_interaction_provider.h"
#include "extensions/renderer/native_extension_bindings_system.h"
#include "extensions/renderer/renderer_extension_registry.h"
#include "extensions/renderer/worker_script_context_set.h"
#include "extensions/renderer/worker_thread_dispatcher.h"
#include "extensions/renderer/worker_thread_util.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"

namespace extensions {

ServiceWorkerData::ServiceWorkerData(
    blink::WebServiceWorkerContextProxy* proxy,
    int64_t service_worker_version_id,
    const absl::optional<base::UnguessableToken>& activation_sequence,
    ScriptContext* context,
    std::unique_ptr<NativeExtensionBindingsSystem> bindings_system)
    : proxy_(proxy),
      service_worker_version_id_(service_worker_version_id),
      activation_sequence_(std::move(activation_sequence)),
      context_(context),
      v8_schema_registry_(new V8SchemaRegistry),
      bindings_system_(std::move(bindings_system)) {
#if !BUILDFLAG(ENABLE_EXTENSIONS_LEGACY_IPC)
  // `bindings_system_` is null if `ExtensionAPIEnabledForServiceWorkerScript`
  // returns false. That means we aren't exposing any bindings to the service
  // worker, but we will have ServiceWorkerData for it so that the
  // WakeEventPage and logging can communicate back to the browser via the
  // `mojom::RendererHost`.
  if (bindings_system_) {
    proxy_->GetAssociatedInterfaceRegistry().AddInterface<mojom::ServiceWorker>(
        base::BindRepeating(&ServiceWorkerData::OnServiceWorkerRequest,
                            weak_ptr_factory_.GetWeakPtr()));
  }
#endif
}

ServiceWorkerData::~ServiceWorkerData() = default;

#if !BUILDFLAG(ENABLE_EXTENSIONS_LEGACY_IPC)
void ServiceWorkerData::OnServiceWorkerRequest(
    mojo::PendingAssociatedReceiver<mojom::ServiceWorker> receiver) {
  CHECK(bindings_system_);
  receiver_.reset();
  receiver_.Bind(std::move(receiver));
}

void ServiceWorkerData::UpdatePermissions(PermissionSet active_permissions,
                                          PermissionSet withheld_permissions) {
  DCHECK(worker_thread_util::IsWorkerThread());
  CHECK(bindings_system_);

  const ExtensionId& extension_id = context_->GetExtensionID();
  const Extension* extension =
      RendererExtensionRegistry::Get()->GetByID(extension_id);
  if (!extension) {
    return;
  }
  extension->permissions_data()->SetPermissions(
      std::make_unique<const PermissionSet>(std::move(active_permissions)),
      std::make_unique<const PermissionSet>(std::move(withheld_permissions)));

  bindings_system_->UpdateBindings(extension_id, /*permissions_changed=*/true,
                                   Dispatcher::GetWorkerScriptContextSet());
}

mojom::ServiceWorkerHost* ServiceWorkerData::GetServiceWorkerHost() {
  CHECK(bindings_system_);
  if (!service_worker_host_.is_bound()) {
    proxy_->GetRemoteAssociatedInterface(
        service_worker_host_.BindNewEndpointAndPassReceiver());
  }
  return service_worker_host_.get();
}

mojom::EventRouter* ServiceWorkerData::GetEventRouter() {
  CHECK(bindings_system_);
  if (!event_router_remote_.is_bound()) {
    proxy_->GetRemoteAssociatedInterface(
        event_router_remote_.BindNewEndpointAndPassReceiver());
  }
  return event_router_remote_.get();
}

mojom::RendererAutomationRegistry* ServiceWorkerData::GetAutomationRegistry() {
  CHECK(bindings_system_);
  if (!renderer_automation_registry_remote_.is_bound()) {
    proxy_->GetRemoteAssociatedInterface(
        renderer_automation_registry_remote_.BindNewEndpointAndPassReceiver());
  }
  return renderer_automation_registry_remote_.get();
}
#endif

mojom::RendererHost* ServiceWorkerData::GetRendererHost() {
  // We allow access to mojom::RendererHost without a `bindings_system_`.
  if (!renderer_host_.is_bound()) {
    proxy_->GetRemoteAssociatedInterface(
        renderer_host_.BindNewEndpointAndPassReceiver());
  }
  return renderer_host_.get();
}

void ServiceWorkerData::Init() {
  // If we do not have bindings there is no additional init necessary
  if (!bindings_system_) {
    return;
  }
#if BUILDFLAG(ENABLE_EXTENSIONS_LEGACY_IPC)
  WorkerThreadDispatcher::Get()->DidInitializeContext(
      service_worker_version_id_);
#else
  const int thread_id = content::WorkerThread::GetCurrentId();
  GetServiceWorkerHost()->DidInitializeServiceWorkerContext(
      context_->GetExtensionID(), service_worker_version_id_, thread_id,
      event_dispatcher_receiver_.BindNewEndpointAndPassRemote());
#endif
}

#if !BUILDFLAG(ENABLE_EXTENSIONS_LEGACY_IPC)
void ServiceWorkerData::DispatchEvent(mojom::DispatchEventParamsPtr params,
                                      base::Value::List event_args,
                                      DispatchEventCallback callback) {
  ScriptContext* script_context = context();
  // Note |scoped_extension_interaction| requires a HandleScope.
  v8::Isolate* isolate = script_context->isolate();
  v8::HandleScope handle_scope(isolate);
  std::unique_ptr<InteractionProvider::Scope> scoped_extension_interaction;
  if (params->is_user_gesture) {
    scoped_extension_interaction =
        ExtensionInteractionProvider::Scope::ForWorker(
            script_context->v8_context());
  }

  bindings_system()->DispatchEventInContext(params->event_name, event_args,
                                            std::move(params->filtering_info),
                                            context());
  std::move(callback).Run();
}

void ServiceWorkerData::DispatchOnConnect(
    const PortId& port_id,
    extensions::mojom::ChannelType channel_type,
    const std::string& channel_name,
    extensions::mojom::TabConnectionInfoPtr tab_info,
    extensions::mojom::ExternalConnectionInfoPtr external_connection_info,
    mojo::PendingAssociatedReceiver<extensions::mojom::MessagePort> port,
    mojo::PendingAssociatedRemote<extensions::mojom::MessagePortHost> port_host,
    DispatchOnConnectCallback callback) {
  WorkerThreadDispatcher::GetBindingsSystem()
      ->messaging_service()
      ->DispatchOnConnect(Dispatcher::GetWorkerScriptContextSet(), port_id,
                          channel_type, channel_name, *tab_info,
                          *external_connection_info, std::move(port),
                          std::move(port_host),
                          // Render frames do not matter.
                          nullptr, std::move(callback));
}

#endif

}  // namespace extensions