File: service_worker_context_adapter.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 (462 lines) | stat: -rw-r--r-- 14,768 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/performance_manager/service_worker_context_adapter.h"

#include "base/check_op.h"
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/observer_list.h"
#include "base/scoped_observation.h"
#include "base/task/single_thread_task_runner.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_process_host_observer.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"

namespace performance_manager {

// ServiceWorkerContextAdapter::RunningServiceWorker ---------------------------

// Observes when the render process of a running service worker exits and
// notifies its owner.
class ServiceWorkerContextAdapter::RunningServiceWorker
    : content::RenderProcessHostObserver {
 public:
  RunningServiceWorker(int64_t version_id,
                       ServiceWorkerContextAdapter* adapter);
  ~RunningServiceWorker() override;

  void Subscribe(content::RenderProcessHost* worker_process_host);
  void Unsubscribe();

  // content::RenderProcessHostObserver:
  void RenderProcessExited(
      content::RenderProcessHost* host,
      const content::ChildProcessTerminationInfo& info) override;
  void InProcessRendererExiting(content::RenderProcessHost* host) override;
  void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;

 private:
  // The version ID of the service worker.
  int const version_id_;

  // The adapter that owns |this|. Notified when RenderProcessExited() is
  // called.
  const raw_ptr<ServiceWorkerContextAdapter> adapter_;

  base::ScopedObservation<content::RenderProcessHost,
                          content::RenderProcessHostObserver>
      scoped_observation_{this};
};

ServiceWorkerContextAdapter::RunningServiceWorker::RunningServiceWorker(
    int64_t version_id,
    ServiceWorkerContextAdapter* adapter)
    : version_id_(version_id), adapter_(adapter) {}

ServiceWorkerContextAdapter::RunningServiceWorker::~RunningServiceWorker() {
  DCHECK(!scoped_observation_.IsObserving());
}

void ServiceWorkerContextAdapter::RunningServiceWorker::Subscribe(
    content::RenderProcessHost* worker_process_host) {
  DCHECK(!scoped_observation_.IsObserving());
  scoped_observation_.Observe(worker_process_host);
}

void ServiceWorkerContextAdapter::RunningServiceWorker::Unsubscribe() {
  DCHECK(scoped_observation_.IsObserving());

  scoped_observation_.Reset();
}

void ServiceWorkerContextAdapter::RunningServiceWorker::RenderProcessExited(
    content::RenderProcessHost* host,
    const content::ChildProcessTerminationInfo& info) {
  adapter_->OnRenderProcessExited(version_id_);

  /* This object is deleted inside the above, don't touch "this". */
}

void ServiceWorkerContextAdapter::RunningServiceWorker::
    InProcessRendererExiting(content::RenderProcessHost* host) {
  CHECK(content::RenderProcessHost::run_renderer_in_process());
  adapter_->OnRenderProcessExited(version_id_);

  /* This object is deleted inside the above, don't touch "this". */
}

void ServiceWorkerContextAdapter::RunningServiceWorker::
    RenderProcessHostDestroyed(content::RenderProcessHost* host) {
  NOTREACHED_NORETURN();
}

// ServiceWorkerContextAdapter::RunningServiceWorker ---------------------------

ServiceWorkerContextAdapter::ServiceWorkerContextAdapter(
    content::ServiceWorkerContext* underlying_context) {
  scoped_underlying_context_observation_.Observe(underlying_context);
}

ServiceWorkerContextAdapter::~ServiceWorkerContextAdapter() {
  // Clean up any outstanding running service worker process subscriptions.
  for (const auto& item : running_service_workers_)
    item.second->Unsubscribe();
  running_service_workers_.clear();
}

void ServiceWorkerContextAdapter::AddObserver(
    content::ServiceWorkerContextObserver* observer) {
  observer_list_.AddObserver(observer);
}

void ServiceWorkerContextAdapter::RemoveObserver(
    content::ServiceWorkerContextObserver* observer) {
  observer_list_.RemoveObserver(observer);
}

void ServiceWorkerContextAdapter::RegisterServiceWorker(
    const GURL& script_url,
    const blink::StorageKey& key,
    const blink::mojom::ServiceWorkerRegistrationOptions& options,
    StatusCodeCallback callback) {
  NOTIMPLEMENTED();
}

void ServiceWorkerContextAdapter::UnregisterServiceWorker(
    const GURL& scope,
    const blink::StorageKey& key,
    ResultCallback callback) {
  NOTIMPLEMENTED();
}

void ServiceWorkerContextAdapter::UnregisterServiceWorkerImmediately(
    const GURL& scope,
    const blink::StorageKey& key,
    ResultCallback callback) {
  NOTIMPLEMENTED();
}

content::ServiceWorkerExternalRequestResult
ServiceWorkerContextAdapter::StartingExternalRequest(
    int64_t service_worker_version_id,
    content::ServiceWorkerExternalRequestTimeoutType timeout_type,
    const base::Uuid& request_uuid) {
  NOTIMPLEMENTED();
  return content::ServiceWorkerExternalRequestResult::kOk;
}

content::ServiceWorkerExternalRequestResult
ServiceWorkerContextAdapter::FinishedExternalRequest(
    int64_t service_worker_version_id,
    const base::Uuid& request_uuid) {
  NOTIMPLEMENTED();
  return content::ServiceWorkerExternalRequestResult::kOk;
}

size_t ServiceWorkerContextAdapter::CountExternalRequestsForTest(
    const blink::StorageKey& key) {
  NOTIMPLEMENTED();
  return 0u;
}

bool ServiceWorkerContextAdapter::ExecuteScriptForTest(
    const std::string& script,
    int64_t version_id,
    content::ServiceWorkerScriptExecutionCallback callback) {
  NOTIMPLEMENTED();
  return false;
}

bool ServiceWorkerContextAdapter::MaybeHasRegistrationForStorageKey(
    const blink::StorageKey& key) {
  NOTIMPLEMENTED();
  return false;
}

void ServiceWorkerContextAdapter::GetAllStorageKeysInfo(
    GetUsageInfoCallback callback) {
  NOTIMPLEMENTED();
}

void ServiceWorkerContextAdapter::DeleteForStorageKey(
    const blink::StorageKey& key,
    ResultCallback callback) {
  NOTIMPLEMENTED();
}

void ServiceWorkerContextAdapter::CheckHasServiceWorker(
    const GURL& url,
    const blink::StorageKey& key,
    CheckHasServiceWorkerCallback callback) {
  NOTIMPLEMENTED();
}

void ServiceWorkerContextAdapter::CheckOfflineCapability(
    const GURL& url,
    const blink::StorageKey& key,
    CheckOfflineCapabilityCallback callback) {
  NOTIMPLEMENTED();
}

void ServiceWorkerContextAdapter::ClearAllServiceWorkersForTest(
    base::OnceClosure callback) {
  NOTIMPLEMENTED();
}

void ServiceWorkerContextAdapter::StartWorkerForScope(
    const GURL& scope,
    const blink::StorageKey& key,
    StartWorkerCallback info_callback,
    StatusCodeCallback status_callback) {
  NOTIMPLEMENTED();
}

bool ServiceWorkerContextAdapter::IsLiveStartingServiceWorker(
    int64_t service_worker_version_id) {
  NOTIMPLEMENTED();
  return false;
}

bool ServiceWorkerContextAdapter::IsLiveRunningServiceWorker(
    int64_t service_worker_version_id) {
  NOTIMPLEMENTED();
  return false;
}

service_manager::InterfaceProvider&
ServiceWorkerContextAdapter::GetRemoteInterfaces(
    int64_t service_worker_version_id) {
  NOTREACHED_NORETURN();
}

blink::AssociatedInterfaceProvider&
ServiceWorkerContextAdapter::GetRemoteAssociatedInterfaces(
    int64_t service_worker_version_id) {
  NOTREACHED_NORETURN();
}

void ServiceWorkerContextAdapter::StartServiceWorkerAndDispatchMessage(
    const GURL& scope,
    const blink::StorageKey& key,
    blink::TransferableMessage message,
    ResultCallback result_callback) {
  NOTIMPLEMENTED();
}

void ServiceWorkerContextAdapter::StartServiceWorkerForNavigationHint(
    const GURL& document_url,
    const blink::StorageKey& key,
    StartServiceWorkerForNavigationHintCallback callback) {
  NOTIMPLEMENTED();
}

void ServiceWorkerContextAdapter::StopAllServiceWorkersForStorageKey(
    const blink::StorageKey& key) {
  NOTIMPLEMENTED();
}

void ServiceWorkerContextAdapter::StopAllServiceWorkers(
    base::OnceClosure callback) {
  NOTIMPLEMENTED();
}

const base::flat_map<int64_t /* version_id */,
                     content::ServiceWorkerRunningInfo>&
ServiceWorkerContextAdapter::GetRunningServiceWorkerInfos() {
  NOTIMPLEMENTED();
  static const base::NoDestructor<
      base::flat_map<int64_t, content::ServiceWorkerRunningInfo>>
      unused;
  return *unused;
}

void ServiceWorkerContextAdapter::OnRegistrationCompleted(const GURL& scope) {
  for (auto& observer : observer_list_)
    observer.OnRegistrationCompleted(scope);
}

void ServiceWorkerContextAdapter::OnRegistrationStored(int64_t registration_id,
                                                       const GURL& scope) {
  for (auto& observer : observer_list_)
    observer.OnRegistrationStored(registration_id, scope);
}

void ServiceWorkerContextAdapter::OnVersionActivated(int64_t version_id,
                                                     const GURL& scope) {
  for (auto& observer : observer_list_)
    observer.OnVersionActivated(version_id, scope);
}

void ServiceWorkerContextAdapter::OnVersionRedundant(int64_t version_id,
                                                     const GURL& scope) {
  for (auto& observer : observer_list_)
    observer.OnVersionRedundant(version_id, scope);
}

void ServiceWorkerContextAdapter::OnVersionStartedRunning(
    int64_t version_id,
    const content::ServiceWorkerRunningInfo& running_info) {
  content::RenderProcessHost* worker_process_host =
      content::RenderProcessHost::FromID(running_info.render_process_id);

  // It's possible that the renderer is already gone since the notification
  // comes asynchronously. Ignore this service worker.
  if (!worker_process_host || !worker_process_host->IsInitializedAndNotDead()) {
#if DCHECK_IS_ON()
    // A OnVersionStoppedRunning() notification is still expected to be sent.
    bool inserted = stopped_service_workers_.insert(version_id).second;
    DCHECK(inserted);
#endif  // DCHECK_IS_ON()
    return;
  }

  AddRunningServiceWorker(version_id, worker_process_host);
  for (auto& observer : observer_list_)
    observer.OnVersionStartedRunning(version_id, running_info);
}

void ServiceWorkerContextAdapter::OnVersionStoppedRunning(int64_t version_id) {
  bool removed = MaybeRemoveRunningServiceWorker(version_id);
  if (!removed) {
#if DCHECK_IS_ON()
    // If this service worker could not be found, then it must be because its
    // render process exited early.
    size_t removed_count = stopped_service_workers_.erase(version_id);
    DCHECK_EQ(removed_count, 1u);
#endif  // DCHECK_IS_ON()
    return;
  }

  for (auto& observer : observer_list_)
    observer.OnVersionStoppedRunning(version_id);
}

void ServiceWorkerContextAdapter::OnControlleeAdded(
    int64_t version_id,
    const std::string& client_uuid,
    const content::ServiceWorkerClientInfo& client_info) {
  // If |client_uuid| is already marked as a client of |version_id|, the
  // notification is dropped.
  bool inserted =
      service_worker_clients_[version_id].insert(client_uuid).second;
  if (!inserted) {
    NOTREACHED();
    return;
  }

  for (auto& observer : observer_list_)
    observer.OnControlleeAdded(version_id, client_uuid, client_info);
}

void ServiceWorkerContextAdapter::OnControlleeRemoved(
    int64_t version_id,
    const std::string& client_uuid) {
  // If |client_uuid| is not already marked as a client of |version_id|, the
  // notification is dropped.
  auto it = service_worker_clients_.find(version_id);
  if (it == service_worker_clients_.end()) {
    NOTREACHED();
    return;
  }

  size_t removed = it->second.erase(client_uuid);
  if (!removed) {
    NOTREACHED();
    return;
  }

  // If a service worker no longer has any clients, it is removed entirely from
  // |service_worker_clients_|.
  if (it->second.empty())
    service_worker_clients_.erase(it);

  for (auto& observer : observer_list_)
    observer.OnControlleeRemoved(version_id, client_uuid);
}

void ServiceWorkerContextAdapter::OnNoControllees(int64_t version_id,
                                                  const GURL& scope) {
  for (auto& observer : observer_list_)
    observer.OnNoControllees(version_id, scope);
}

void ServiceWorkerContextAdapter::OnControlleeNavigationCommitted(
    int64_t version_id,
    const std::string& client_uuid,
    content::GlobalRenderFrameHostId render_frame_host_id) {
  // The navigation committed notification should not be sent if the frame is
  // not already a client of |version_id|.
  auto it = service_worker_clients_.find(version_id);
  if (it == service_worker_clients_.end()) {
    NOTREACHED();
    return;
  }

  if (it->second.find(client_uuid) == it->second.end()) {
    NOTREACHED();
    return;
  }

  for (auto& observer : observer_list_)
    observer.OnControlleeNavigationCommitted(version_id, client_uuid,
                                             render_frame_host_id);
}

void ServiceWorkerContextAdapter::OnReportConsoleMessage(
    int64_t version_id,
    const GURL& scope,
    const content::ConsoleMessage& message) {
  for (auto& observer : observer_list_)
    observer.OnReportConsoleMessage(version_id, scope, message);
}

void ServiceWorkerContextAdapter::OnDestruct(ServiceWorkerContext* context) {
  for (auto& observer : observer_list_)
    observer.OnDestruct(context);
}

void ServiceWorkerContextAdapter::OnRenderProcessExited(int64_t version_id) {
  bool removed = MaybeRemoveRunningServiceWorker(version_id);
  DCHECK(removed);

  for (auto& observer : observer_list_)
    observer.OnVersionStoppedRunning(version_id);

#if DCHECK_IS_ON()
  // Now expect that OnVersionStoppedRunning() will be called for that
  // version_id.
  bool inserted = stopped_service_workers_.insert(version_id).second;
  DCHECK(inserted);
#endif  // DCHECK_IS_ON()
}

void ServiceWorkerContextAdapter::AddRunningServiceWorker(
    int64_t version_id,
    content::RenderProcessHost* worker_process_host) {
  std::unique_ptr<ServiceWorkerContextAdapter::RunningServiceWorker>
      running_service_worker =
          std::make_unique<RunningServiceWorker>(version_id, this);

  running_service_worker->Subscribe(worker_process_host);
  bool inserted = running_service_workers_
                      .emplace(version_id, std::move(running_service_worker))
                      .second;
  DCHECK(inserted);
}

bool ServiceWorkerContextAdapter::MaybeRemoveRunningServiceWorker(
    int64_t version_id) {
  auto it = running_service_workers_.find(version_id);
  if (it == running_service_workers_.end())
    return false;

  it->second->Unsubscribe();
  running_service_workers_.erase(it);

  return true;
}

}  // namespace performance_manager