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
content / browser / service_worker / fake_service_worker.cc [blame]
// Copyright 2019 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/fake_service_worker.h"
#include <utility>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "content/browser/service_worker/embedded_worker_test_helper.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/network/public/mojom/fetch_api.mojom.h"
#include "third_party/blink/public/mojom/background_fetch/background_fetch.mojom.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_response.mojom.h"
#include "third_party/blink/public/mojom/payments/payment_app.mojom.h"
#include "third_party/blink/public/mojom/push_messaging/push_messaging.mojom.h"
#include "third_party/blink/public/mojom/service_worker/dispatch_fetch_event_params.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_fetch_response_callback.mojom.h"
namespace content {
FakeServiceWorker::FakeServiceWorker(EmbeddedWorkerTestHelper* helper)
: helper_(helper) {}
FakeServiceWorker::~FakeServiceWorker() = default;
void FakeServiceWorker::Bind(
mojo::PendingReceiver<blink::mojom::ServiceWorker> receiver) {
receiver_.Bind(std::move(receiver));
receiver_.set_disconnect_handler(base::BindOnce(
&FakeServiceWorker::CallOnConnectionError, base::Unretained(this)));
}
void FakeServiceWorker::RunUntilInitializeGlobalScope() {
if (host_) {
return;
}
base::RunLoop loop;
quit_closure_for_initialize_global_scope_ = loop.QuitClosure();
loop.Run();
}
void FakeServiceWorker::FlushForTesting() {
receiver_.FlushForTesting();
}
base::WeakPtr<FakeServiceWorker> FakeServiceWorker::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
void FakeServiceWorker::InitializeGlobalScope(
mojo::PendingAssociatedRemote<blink::mojom::ServiceWorkerHost>
service_worker_host,
mojo::PendingAssociatedRemote<blink::mojom::AssociatedInterfaceProvider>
associated_interfaces_from_browser,
mojo::PendingAssociatedReceiver<blink::mojom::AssociatedInterfaceProvider>
associated_interfaces_to_browser,
blink::mojom::ServiceWorkerRegistrationObjectInfoPtr registration_info,
blink::mojom::ServiceWorkerObjectInfoPtr service_worker_info,
blink::mojom::FetchHandlerExistence fetch_handler_existence,
mojo::PendingReceiver<blink::mojom::ReportingObserver>
reporting_observer_receiver,
blink::mojom::AncestorFrameType ancestor_frame_type,
const blink::StorageKey& storage_key) {
host_.Bind(std::move(service_worker_host));
// Enable callers to use these endpoints without us actually binding them
// to an implementation.
registration_info->receiver.EnableUnassociatedUsage();
if (registration_info->installing) {
registration_info->installing->receiver.EnableUnassociatedUsage();
}
if (registration_info->waiting) {
registration_info->waiting->receiver.EnableUnassociatedUsage();
}
if (registration_info->active) {
registration_info->active->receiver.EnableUnassociatedUsage();
}
if (service_worker_info) {
service_worker_info->receiver.EnableUnassociatedUsage();
}
registration_info_ = std::move(registration_info);
service_worker_info_ = std::move(service_worker_info);
if (quit_closure_for_initialize_global_scope_) {
std::move(quit_closure_for_initialize_global_scope_).Run();
}
fetch_handler_existence_ = fetch_handler_existence;
}
void FakeServiceWorker::DispatchInstallEvent(
DispatchInstallEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED,
/*fetch_count=*/0);
}
void FakeServiceWorker::DispatchActivateEvent(
DispatchActivateEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchBackgroundFetchAbortEvent(
blink::mojom::BackgroundFetchRegistrationPtr registration,
DispatchBackgroundFetchAbortEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchBackgroundFetchClickEvent(
blink::mojom::BackgroundFetchRegistrationPtr registration,
DispatchBackgroundFetchClickEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchBackgroundFetchFailEvent(
blink::mojom::BackgroundFetchRegistrationPtr registration,
DispatchBackgroundFetchFailEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchBackgroundFetchSuccessEvent(
blink::mojom::BackgroundFetchRegistrationPtr registration,
DispatchBackgroundFetchSuccessEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchCookieChangeEvent(
const net::CookieChangeInfo& change,
DispatchCookieChangeEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchFetchEventForMainResource(
blink::mojom::DispatchFetchEventParamsPtr params,
mojo::PendingRemote<blink::mojom::ServiceWorkerFetchResponseCallback>
pending_response_callback,
DispatchFetchEventForMainResourceCallback callback) {
auto response = blink::mojom::FetchAPIResponse::New();
response->status_code = 200;
response->status_text = "OK";
response->response_type = network::mojom::FetchResponseType::kDefault;
mojo::Remote<blink::mojom::ServiceWorkerFetchResponseCallback>
response_callback(std::move(pending_response_callback));
auto timing = blink::mojom::ServiceWorkerFetchEventTiming::New();
auto now = base::TimeTicks::Now();
timing->respond_with_settled_time = now;
timing->dispatch_event_time = now;
response_callback->OnResponse(std::move(response), std::move(timing));
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchNotificationClickEvent(
const std::string& notification_id,
const blink::PlatformNotificationData& notification_data,
int action_index,
const std::optional<std::u16string>& reply,
DispatchNotificationClickEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchNotificationCloseEvent(
const std::string& notification_id,
const blink::PlatformNotificationData& notification_data,
DispatchNotificationCloseEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchPushEvent(
const std::optional<std::string>& payload,
DispatchPushEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchPushSubscriptionChangeEvent(
blink::mojom::PushSubscriptionPtr old_subscription,
blink::mojom::PushSubscriptionPtr new_subscription,
DispatchPushSubscriptionChangeEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchSyncEvent(const std::string& tag,
bool last_chance,
base::TimeDelta timeout,
DispatchSyncEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchPeriodicSyncEvent(
const std::string& tag,
base::TimeDelta timeout,
DispatchPeriodicSyncEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchAbortPaymentEvent(
mojo::PendingRemote<payments::mojom::PaymentHandlerResponseCallback>
pending_response_callback,
DispatchAbortPaymentEventCallback callback) {
mojo::Remote<payments::mojom::PaymentHandlerResponseCallback>
response_callback(std::move(pending_response_callback));
response_callback->OnResponseForAbortPayment(true);
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchCanMakePaymentEvent(
payments::mojom::CanMakePaymentEventDataPtr event_data,
mojo::PendingRemote<payments::mojom::PaymentHandlerResponseCallback>
pending_response_callback,
DispatchCanMakePaymentEventCallback callback) {
mojo::Remote<payments::mojom::PaymentHandlerResponseCallback>
response_callback(std::move(pending_response_callback));
response_callback->OnResponseForCanMakePayment(
payments::mojom::CanMakePaymentResponse::New());
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchPaymentRequestEvent(
payments::mojom::PaymentRequestEventDataPtr event_data,
mojo::PendingRemote<payments::mojom::PaymentHandlerResponseCallback>
pending_response_callback,
DispatchPaymentRequestEventCallback callback) {
mojo::Remote<payments::mojom::PaymentHandlerResponseCallback>
response_callback(std::move(pending_response_callback));
response_callback->OnResponseForPaymentRequest(
payments::mojom::PaymentHandlerResponse::New());
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchExtendableMessageEvent(
blink::mojom::ExtendableMessageEventPtr event,
DispatchExtendableMessageEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::DispatchContentDeleteEvent(
const std::string& id,
DispatchContentDeleteEventCallback callback) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void FakeServiceWorker::Ping(PingCallback callback) {
std::move(callback).Run();
}
void FakeServiceWorker::SetIdleDelay(base::TimeDelta delay) {
idle_delay_ = delay;
}
void FakeServiceWorker::AddKeepAlive() {
idle_delay_.reset();
}
void FakeServiceWorker::ClearKeepAlive() {
idle_delay_ = base::Seconds(30);
}
void FakeServiceWorker::AddMessageToConsole(
blink::mojom::ConsoleMessageLevel level,
const std::string& message) {
NOTIMPLEMENTED();
}
void FakeServiceWorker::ExecuteScriptForTest(
const std::u16string& script,
bool wants_result,
ExecuteScriptForTestCallback callback) {
NOTIMPLEMENTED();
}
void FakeServiceWorker::OnConnectionError() {
// Destroys |this|.
helper_->RemoveServiceWorker(this);
}
void FakeServiceWorker::CallOnConnectionError() {
// Call OnConnectionError(), which subclasses can override.
OnConnectionError();
}
} // namespace content