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
content / browser / service_worker / service_worker_device_delegate_observer.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/service_worker/service_worker_device_delegate_observer.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "third_party/blink/public/common/service_worker/embedded_worker_status.h"
#include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
namespace content {
namespace {
void DidFindRegistration(
ServiceWorkerDeviceDelegateObserver::ServiceWorkerStartedCallback callback,
blink::ServiceWorkerStatusCode service_worker_status,
scoped_refptr<ServiceWorkerRegistration> service_worker_registration) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (service_worker_status != blink::ServiceWorkerStatusCode::kOk) {
std::move(callback).Run(nullptr, service_worker_status);
return;
}
ServiceWorkerVersion* active_version =
service_worker_registration->active_version();
CHECK(active_version);
active_version->RunAfterStartWorker(
ServiceWorkerMetrics::EventType::EXTERNAL_REQUEST,
base::BindOnce(std::move(callback),
base::WrapRefCounted(active_version)));
}
} // namespace
ServiceWorkerDeviceDelegateObserver::ServiceWorkerDeviceDelegateObserver(
ServiceWorkerContextCore* context)
: context_(raw_ref<ServiceWorkerContextCore>::from_ptr(context)) {
observation_.Observe(context_->wrapper());
}
ServiceWorkerDeviceDelegateObserver::~ServiceWorkerDeviceDelegateObserver() =
default;
void ServiceWorkerDeviceDelegateObserver::OnRegistrationDeleted(
int64_t registration_id,
const GURL& scope,
const blink::StorageKey& key) {
if (registration_id_map_.erase(registration_id) > 0) {
RegistrationRemoved(registration_id);
}
}
void ServiceWorkerDeviceDelegateObserver::OnStopped(int64_t version_id) {
pending_callbacks_.erase(version_id);
}
void ServiceWorkerDeviceDelegateObserver::Register(int64_t registration_id) {
auto registration = context_->GetLiveRegistration(registration_id);
// We should have a live registration to reach this point.
CHECK(registration);
if (registration_id_map_.contains(registration_id)) {
return;
}
// By default, the
// `registration_id_map_[registration_id].has_hid_event_handlers` is set to
// false. It relies on the activated ServiceWorkerVersion to use
// `UpdateHasEventHandlers` for updating it.
registration_id_map_[registration_id] = {registration->key(), false};
RegistrationAdded(registration_id);
}
void ServiceWorkerDeviceDelegateObserver::DispatchEventToWorker(
int64_t registration_id,
ServiceWorkerStartedCallback callback) {
auto it = registration_id_map_.find(registration_id);
CHECK(it != registration_id_map_.end());
context_->wrapper()->FindReadyRegistrationForId(
registration_id, it->second.key,
base::BindOnce(&DidFindRegistration, std::move(callback)));
}
void ServiceWorkerDeviceDelegateObserver::UpdateHasEventHandlers(
int64_t registration_id,
bool has_event_handlers) {
auto it = registration_id_map_.find(registration_id);
// This can happen when an installed service worker registration with device
// event handlers is loaded after browser restart. In this case, it only
// registers the service worker that has device event handlers.
if (it == registration_id_map_.end()) {
if (!has_event_handlers) {
return;
}
Register(registration_id);
it = registration_id_map_.find(registration_id);
}
it->second.has_event_handlers = has_event_handlers;
}
void ServiceWorkerDeviceDelegateObserver::ProcessPendingCallbacks(
ServiceWorkerVersion* version) {
CHECK(version);
auto it = pending_callbacks_.find(version->version_id());
if (it == pending_callbacks_.end()) {
return;
}
auto callbacks = std::move(it->second);
pending_callbacks_.erase(it);
for (auto& cb : callbacks) {
std::move(cb).Run();
}
}
void ServiceWorkerDeviceDelegateObserver::AddPendingCallback(
ServiceWorkerVersion* version,
base::OnceClosure callback) {
// This should only be used when the worker is running since it is meant to be
// used in the case of the worker is running and caller is waiting other
// states to be ready for executing `callback`.
CHECK(version);
CHECK_EQ(version->running_status(), blink::EmbeddedWorkerStatus::kRunning);
pending_callbacks_[version->version_id()].push_back(std::move(callback));
}
BrowserContext* ServiceWorkerDeviceDelegateObserver::GetBrowserContext() {
return context_->wrapper()->browser_context();
}
} // namespace content