/src/mozilla-central/dom/serviceworkers/ServiceWorkerUpdateJob.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "ServiceWorkerUpdateJob.h" |
8 | | |
9 | | #include "nsIScriptError.h" |
10 | | #include "nsIURL.h" |
11 | | #include "nsNetUtil.h" |
12 | | #include "nsProxyRelease.h" |
13 | | #include "ServiceWorkerManager.h" |
14 | | #include "ServiceWorkerPrivate.h" |
15 | | #include "ServiceWorkerRegistrationInfo.h" |
16 | | #include "ServiceWorkerScriptCache.h" |
17 | | #include "mozilla/dom/WorkerCommon.h" |
18 | | |
19 | | namespace mozilla { |
20 | | namespace dom { |
21 | | |
22 | | using serviceWorkerScriptCache::OnFailure; |
23 | | |
24 | | namespace { |
25 | | |
26 | | /** |
27 | | * The spec mandates slightly different behaviors for computing the scope |
28 | | * prefix string in case a Service-Worker-Allowed header is specified versus |
29 | | * when it's not available. |
30 | | * |
31 | | * With the header: |
32 | | * "Set maxScopeString to "/" concatenated with the strings in maxScope's |
33 | | * path (including empty strings), separated from each other by "/"." |
34 | | * Without the header: |
35 | | * "Set maxScopeString to "/" concatenated with the strings, except the last |
36 | | * string that denotes the script's file name, in registration's registering |
37 | | * script url's path (including empty strings), separated from each other by |
38 | | * "/"." |
39 | | * |
40 | | * In simpler terms, if the header is not present, we should only use the |
41 | | * "directory" part of the pathname, and otherwise the entire pathname should be |
42 | | * used. ScopeStringPrefixMode allows the caller to specify the desired |
43 | | * behavior. |
44 | | */ |
45 | | enum ScopeStringPrefixMode { |
46 | | eUseDirectory, |
47 | | eUsePath |
48 | | }; |
49 | | |
50 | | nsresult |
51 | | GetRequiredScopeStringPrefix(nsIURI* aScriptURI, nsACString& aPrefix, |
52 | | ScopeStringPrefixMode aPrefixMode) |
53 | 0 | { |
54 | 0 | nsresult rv = aScriptURI->GetPrePath(aPrefix); |
55 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
56 | 0 | return rv; |
57 | 0 | } |
58 | 0 | |
59 | 0 | if (aPrefixMode == eUseDirectory) { |
60 | 0 | nsCOMPtr<nsIURL> scriptURL(do_QueryInterface(aScriptURI)); |
61 | 0 | if (NS_WARN_IF(!scriptURL)) { |
62 | 0 | return NS_ERROR_FAILURE; |
63 | 0 | } |
64 | 0 | |
65 | 0 | nsAutoCString dir; |
66 | 0 | rv = scriptURL->GetDirectory(dir); |
67 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
68 | 0 | return rv; |
69 | 0 | } |
70 | 0 | |
71 | 0 | aPrefix.Append(dir); |
72 | 0 | } else if (aPrefixMode == eUsePath) { |
73 | 0 | nsAutoCString path; |
74 | 0 | rv = aScriptURI->GetPathQueryRef(path); |
75 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
76 | 0 | return rv; |
77 | 0 | } |
78 | 0 | |
79 | 0 | aPrefix.Append(path); |
80 | 0 | } else { |
81 | 0 | MOZ_ASSERT_UNREACHABLE("Invalid value for aPrefixMode"); |
82 | 0 | } |
83 | 0 | return NS_OK; |
84 | 0 | } |
85 | | |
86 | | } // anonymous namespace |
87 | | |
88 | | class ServiceWorkerUpdateJob::CompareCallback final : public serviceWorkerScriptCache::CompareCallback |
89 | | { |
90 | | RefPtr<ServiceWorkerUpdateJob> mJob; |
91 | | |
92 | | ~CompareCallback() |
93 | 0 | { |
94 | 0 | } |
95 | | |
96 | | public: |
97 | | explicit CompareCallback(ServiceWorkerUpdateJob* aJob) |
98 | | : mJob(aJob) |
99 | 0 | { |
100 | 0 | MOZ_ASSERT(mJob); |
101 | 0 | } |
102 | | |
103 | | virtual void |
104 | | ComparisonResult(nsresult aStatus, |
105 | | bool aInCacheAndEqual, |
106 | | OnFailure aOnFailure, |
107 | | const nsAString& aNewCacheName, |
108 | | const nsACString& aMaxScope, |
109 | | nsLoadFlags aLoadFlags) override |
110 | 0 | { |
111 | 0 | mJob->ComparisonResult(aStatus, |
112 | 0 | aInCacheAndEqual, |
113 | 0 | aOnFailure, |
114 | 0 | aNewCacheName, |
115 | 0 | aMaxScope, |
116 | 0 | aLoadFlags); |
117 | 0 | } |
118 | | |
119 | | NS_INLINE_DECL_REFCOUNTING(ServiceWorkerUpdateJob::CompareCallback, override) |
120 | | }; |
121 | | |
122 | | class ServiceWorkerUpdateJob::ContinueUpdateRunnable final : public LifeCycleEventCallback |
123 | | { |
124 | | nsMainThreadPtrHandle<ServiceWorkerUpdateJob> mJob; |
125 | | bool mSuccess; |
126 | | |
127 | | public: |
128 | | explicit ContinueUpdateRunnable(const nsMainThreadPtrHandle<ServiceWorkerUpdateJob>& aJob) |
129 | | : mJob(aJob) |
130 | | , mSuccess(false) |
131 | 0 | { |
132 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
133 | 0 | } |
134 | | |
135 | | void |
136 | | SetResult(bool aResult) override |
137 | 0 | { |
138 | 0 | mSuccess = aResult; |
139 | 0 | } |
140 | | |
141 | | NS_IMETHOD |
142 | | Run() override |
143 | 0 | { |
144 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
145 | 0 | mJob->ContinueUpdateAfterScriptEval(mSuccess); |
146 | 0 | mJob = nullptr; |
147 | 0 | return NS_OK; |
148 | 0 | } |
149 | | }; |
150 | | |
151 | | class ServiceWorkerUpdateJob::ContinueInstallRunnable final : public LifeCycleEventCallback |
152 | | { |
153 | | nsMainThreadPtrHandle<ServiceWorkerUpdateJob> mJob; |
154 | | bool mSuccess; |
155 | | |
156 | | public: |
157 | | explicit ContinueInstallRunnable(const nsMainThreadPtrHandle<ServiceWorkerUpdateJob>& aJob) |
158 | | : mJob(aJob) |
159 | | , mSuccess(false) |
160 | 0 | { |
161 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
162 | 0 | } |
163 | | |
164 | | void |
165 | | SetResult(bool aResult) override |
166 | 0 | { |
167 | 0 | mSuccess = aResult; |
168 | 0 | } |
169 | | |
170 | | NS_IMETHOD |
171 | | Run() override |
172 | 0 | { |
173 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
174 | 0 | mJob->ContinueAfterInstallEvent(mSuccess); |
175 | 0 | mJob = nullptr; |
176 | 0 | return NS_OK; |
177 | 0 | } |
178 | | }; |
179 | | |
180 | | ServiceWorkerUpdateJob::ServiceWorkerUpdateJob( |
181 | | nsIPrincipal* aPrincipal, |
182 | | const nsACString& aScope, |
183 | | const nsACString& aScriptSpec, |
184 | | nsILoadGroup* aLoadGroup, |
185 | | ServiceWorkerUpdateViaCache aUpdateViaCache) |
186 | | : ServiceWorkerJob(Type::Update, aPrincipal, aScope, aScriptSpec) |
187 | | , mLoadGroup(aLoadGroup) |
188 | | , mUpdateViaCache(aUpdateViaCache) |
189 | | , mOnFailure(OnFailure::DoNothing) |
190 | 0 | { |
191 | 0 | } |
192 | | |
193 | | already_AddRefed<ServiceWorkerRegistrationInfo> |
194 | | ServiceWorkerUpdateJob::GetRegistration() const |
195 | 0 | { |
196 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
197 | 0 | RefPtr<ServiceWorkerRegistrationInfo> ref = mRegistration; |
198 | 0 | return ref.forget(); |
199 | 0 | } |
200 | | |
201 | | ServiceWorkerUpdateJob::ServiceWorkerUpdateJob( |
202 | | Type aType, |
203 | | nsIPrincipal* aPrincipal, |
204 | | const nsACString& aScope, |
205 | | const nsACString& aScriptSpec, |
206 | | nsILoadGroup* aLoadGroup, |
207 | | ServiceWorkerUpdateViaCache aUpdateViaCache) |
208 | | : ServiceWorkerJob(aType, aPrincipal, aScope, aScriptSpec) |
209 | | , mLoadGroup(aLoadGroup) |
210 | | , mUpdateViaCache(aUpdateViaCache) |
211 | | , mOnFailure(serviceWorkerScriptCache::OnFailure::DoNothing) |
212 | 0 | { |
213 | 0 | } |
214 | | |
215 | | ServiceWorkerUpdateJob::~ServiceWorkerUpdateJob() |
216 | 0 | { |
217 | 0 | } |
218 | | |
219 | | void |
220 | | ServiceWorkerUpdateJob::FailUpdateJob(ErrorResult& aRv) |
221 | 0 | { |
222 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
223 | 0 | MOZ_ASSERT(aRv.Failed()); |
224 | 0 |
|
225 | 0 | // Cleanup after a failed installation. This essentially implements |
226 | 0 | // step 13 of the Install algorithm. |
227 | 0 | // |
228 | 0 | // https://w3c.github.io/ServiceWorker/#installation-algorithm |
229 | 0 | // |
230 | 0 | // The spec currently only runs this after an install event fails, |
231 | 0 | // but we must handle many more internal errors. So we check for |
232 | 0 | // cleanup on every non-successful exit. |
233 | 0 | if (mRegistration) { |
234 | 0 | // Some kinds of failures indicate there is something broken in the currently |
235 | 0 | // installed registration. In these cases we want to fully unregister. |
236 | 0 | if (mOnFailure == OnFailure::Uninstall) { |
237 | 0 | mRegistration->ClearAsCorrupt(); |
238 | 0 | } |
239 | 0 | |
240 | 0 | // Otherwise just clear the workers we may have created as part of the |
241 | 0 | // update process. |
242 | 0 | else { |
243 | 0 | mRegistration->ClearEvaluating(); |
244 | 0 | mRegistration->ClearInstalling(); |
245 | 0 | } |
246 | 0 |
|
247 | 0 | RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance(); |
248 | 0 | if (swm) { |
249 | 0 | swm->MaybeRemoveRegistration(mRegistration); |
250 | 0 |
|
251 | 0 | // Also clear the registration on disk if we are forcing uninstall |
252 | 0 | // due to a particularly bad failure. |
253 | 0 | if (mOnFailure == OnFailure::Uninstall) { |
254 | 0 | swm->MaybeSendUnregister(mRegistration->Principal(), |
255 | 0 | mRegistration->Scope()); |
256 | 0 | } |
257 | 0 | } |
258 | 0 | } |
259 | 0 |
|
260 | 0 | mRegistration = nullptr; |
261 | 0 |
|
262 | 0 | Finish(aRv); |
263 | 0 | } |
264 | | |
265 | | void |
266 | | ServiceWorkerUpdateJob::FailUpdateJob(nsresult aRv) |
267 | 0 | { |
268 | 0 | ErrorResult rv(aRv); |
269 | 0 | FailUpdateJob(rv); |
270 | 0 | } |
271 | | |
272 | | void |
273 | | ServiceWorkerUpdateJob::AsyncExecute() |
274 | 0 | { |
275 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
276 | 0 | MOZ_ASSERT(GetType() == Type::Update); |
277 | 0 |
|
278 | 0 | RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance(); |
279 | 0 | if (Canceled() || !swm) { |
280 | 0 | FailUpdateJob(NS_ERROR_DOM_ABORT_ERR); |
281 | 0 | return; |
282 | 0 | } |
283 | 0 | |
284 | 0 | // Begin step 1 of the Update algorithm. |
285 | 0 | // |
286 | 0 | // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#update-algorithm |
287 | 0 | |
288 | 0 | RefPtr<ServiceWorkerRegistrationInfo> registration = |
289 | 0 | swm->GetRegistration(mPrincipal, mScope); |
290 | 0 |
|
291 | 0 | if (!registration || registration->IsPendingUninstall()) { |
292 | 0 | ErrorResult rv; |
293 | 0 | rv.ThrowTypeError<MSG_SW_UPDATE_BAD_REGISTRATION>(NS_ConvertUTF8toUTF16(mScope), |
294 | 0 | NS_LITERAL_STRING("uninstalled")); |
295 | 0 | FailUpdateJob(rv); |
296 | 0 | return; |
297 | 0 | } |
298 | 0 |
|
299 | 0 | // If a Register job with a new script executed ahead of us in the job queue, |
300 | 0 | // then our update for the old script no longer makes sense. Simply abort |
301 | 0 | // in this case. |
302 | 0 | RefPtr<ServiceWorkerInfo> newest = registration->Newest(); |
303 | 0 | if (newest && !mScriptSpec.Equals(newest->ScriptSpec())) { |
304 | 0 | ErrorResult rv; |
305 | 0 | rv.ThrowTypeError<MSG_SW_UPDATE_BAD_REGISTRATION>(NS_ConvertUTF8toUTF16(mScope), |
306 | 0 | NS_LITERAL_STRING("changed")); |
307 | 0 | FailUpdateJob(rv); |
308 | 0 | return; |
309 | 0 | } |
310 | 0 |
|
311 | 0 | SetRegistration(registration); |
312 | 0 | Update(); |
313 | 0 | } |
314 | | |
315 | | void |
316 | | ServiceWorkerUpdateJob::SetRegistration(ServiceWorkerRegistrationInfo* aRegistration) |
317 | 0 | { |
318 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
319 | 0 |
|
320 | 0 | MOZ_ASSERT(!mRegistration); |
321 | 0 | MOZ_ASSERT(aRegistration); |
322 | 0 | mRegistration = aRegistration; |
323 | 0 | } |
324 | | |
325 | | void |
326 | | ServiceWorkerUpdateJob::Update() |
327 | 0 | { |
328 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
329 | 0 | MOZ_ASSERT(!Canceled()); |
330 | 0 |
|
331 | 0 | // SetRegistration() must be called before Update(). |
332 | 0 | MOZ_ASSERT(mRegistration); |
333 | 0 | MOZ_ASSERT(!mRegistration->GetInstalling()); |
334 | 0 |
|
335 | 0 | // Begin the script download and comparison steps starting at step 5 |
336 | 0 | // of the Update algorithm. |
337 | 0 |
|
338 | 0 | RefPtr<ServiceWorkerInfo> workerInfo = mRegistration->Newest(); |
339 | 0 | nsAutoString cacheName; |
340 | 0 |
|
341 | 0 | // If the script has not changed, we need to perform a byte-for-byte |
342 | 0 | // comparison. |
343 | 0 | if (workerInfo && workerInfo->ScriptSpec().Equals(mScriptSpec)) { |
344 | 0 | cacheName = workerInfo->CacheName(); |
345 | 0 | } |
346 | 0 |
|
347 | 0 | RefPtr<CompareCallback> callback = new CompareCallback(this); |
348 | 0 |
|
349 | 0 | nsresult rv = |
350 | 0 | serviceWorkerScriptCache::Compare(mRegistration, mPrincipal, cacheName, |
351 | 0 | NS_ConvertUTF8toUTF16(mScriptSpec), |
352 | 0 | callback, mLoadGroup); |
353 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
354 | 0 | FailUpdateJob(rv); |
355 | 0 | return; |
356 | 0 | } |
357 | 0 | } |
358 | | |
359 | | ServiceWorkerUpdateViaCache |
360 | | ServiceWorkerUpdateJob::GetUpdateViaCache() const |
361 | 0 | { |
362 | 0 | return mUpdateViaCache; |
363 | 0 | } |
364 | | |
365 | | void |
366 | | ServiceWorkerUpdateJob::ComparisonResult(nsresult aStatus, |
367 | | bool aInCacheAndEqual, |
368 | | OnFailure aOnFailure, |
369 | | const nsAString& aNewCacheName, |
370 | | const nsACString& aMaxScope, |
371 | | nsLoadFlags aLoadFlags) |
372 | 0 | { |
373 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
374 | 0 |
|
375 | 0 | mOnFailure = aOnFailure; |
376 | 0 |
|
377 | 0 | RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance(); |
378 | 0 | if (NS_WARN_IF(Canceled() || !swm)) { |
379 | 0 | FailUpdateJob(NS_ERROR_DOM_ABORT_ERR); |
380 | 0 | return; |
381 | 0 | } |
382 | 0 | |
383 | 0 | // Handle failure of the download or comparison. This is part of Update |
384 | 0 | // step 5 as "If the algorithm asynchronously completes with null, then:". |
385 | 0 | if (NS_WARN_IF(NS_FAILED(aStatus))) { |
386 | 0 | FailUpdateJob(aStatus); |
387 | 0 | return; |
388 | 0 | } |
389 | 0 | |
390 | 0 | // The spec validates the response before performing the byte-for-byte check. |
391 | 0 | // Here we perform the comparison in another module and then validate the |
392 | 0 | // script URL and scope. Make sure to do this validation before accepting |
393 | 0 | // an byte-for-byte match since the service-worker-allowed header might have |
394 | 0 | // changed since the last time it was installed. |
395 | 0 | |
396 | 0 | // This is step 2 the "validate response" section of Update algorithm step 5. |
397 | 0 | // Step 1 is performed in the serviceWorkerScriptCache code. |
398 | 0 | |
399 | 0 | nsCOMPtr<nsIURI> scriptURI; |
400 | 0 | nsresult rv = NS_NewURI(getter_AddRefs(scriptURI), mScriptSpec); |
401 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
402 | 0 | FailUpdateJob(NS_ERROR_DOM_SECURITY_ERR); |
403 | 0 | return; |
404 | 0 | } |
405 | 0 | |
406 | 0 | nsCOMPtr<nsIURI> maxScopeURI; |
407 | 0 | if (!aMaxScope.IsEmpty()) { |
408 | 0 | rv = NS_NewURI(getter_AddRefs(maxScopeURI), aMaxScope, |
409 | 0 | nullptr, scriptURI); |
410 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
411 | 0 | FailUpdateJob(NS_ERROR_DOM_SECURITY_ERR); |
412 | 0 | return; |
413 | 0 | } |
414 | 0 | } |
415 | 0 | |
416 | 0 | nsAutoCString defaultAllowedPrefix; |
417 | 0 | rv = GetRequiredScopeStringPrefix(scriptURI, defaultAllowedPrefix, |
418 | 0 | eUseDirectory); |
419 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
420 | 0 | FailUpdateJob(NS_ERROR_DOM_SECURITY_ERR); |
421 | 0 | return; |
422 | 0 | } |
423 | 0 | |
424 | 0 | nsAutoCString maxPrefix(defaultAllowedPrefix); |
425 | 0 | if (maxScopeURI) { |
426 | 0 | rv = GetRequiredScopeStringPrefix(maxScopeURI, maxPrefix, eUsePath); |
427 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
428 | 0 | FailUpdateJob(NS_ERROR_DOM_SECURITY_ERR); |
429 | 0 | return; |
430 | 0 | } |
431 | 0 | } |
432 | 0 | |
433 | 0 | if (!StringBeginsWith(mRegistration->Scope(), maxPrefix)) { |
434 | 0 | nsAutoString message; |
435 | 0 | NS_ConvertUTF8toUTF16 reportScope(mRegistration->Scope()); |
436 | 0 | NS_ConvertUTF8toUTF16 reportMaxPrefix(maxPrefix); |
437 | 0 | const char16_t* params[] = { reportScope.get(), reportMaxPrefix.get() }; |
438 | 0 |
|
439 | 0 | rv = nsContentUtils::FormatLocalizedString(nsContentUtils::eDOM_PROPERTIES, |
440 | 0 | "ServiceWorkerScopePathMismatch", |
441 | 0 | params, message); |
442 | 0 | NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to format localized string"); |
443 | 0 | swm->ReportToAllClients(mScope, |
444 | 0 | message, |
445 | 0 | EmptyString(), |
446 | 0 | EmptyString(), 0, 0, |
447 | 0 | nsIScriptError::errorFlag); |
448 | 0 | FailUpdateJob(NS_ERROR_DOM_SECURITY_ERR); |
449 | 0 | return; |
450 | 0 | } |
451 | 0 |
|
452 | 0 | // The response has been validated, so now we can consider if its a |
453 | 0 | // byte-for-byte match. This is step 6 of the Update algorithm. |
454 | 0 | if (aInCacheAndEqual) { |
455 | 0 | Finish(NS_OK); |
456 | 0 | return; |
457 | 0 | } |
458 | 0 | |
459 | 0 | Telemetry::Accumulate(Telemetry::SERVICE_WORKER_UPDATED, 1); |
460 | 0 |
|
461 | 0 | // Begin step 7 of the Update algorithm to evaluate the new script. |
462 | 0 | nsLoadFlags flags = aLoadFlags; |
463 | 0 | if (GetUpdateViaCache() == ServiceWorkerUpdateViaCache::None) { |
464 | 0 | flags |= nsIRequest::VALIDATE_ALWAYS; |
465 | 0 | } |
466 | 0 |
|
467 | 0 | RefPtr<ServiceWorkerInfo> sw = |
468 | 0 | new ServiceWorkerInfo(mRegistration->Principal(), |
469 | 0 | mRegistration->Scope(), |
470 | 0 | mRegistration->Id(), |
471 | 0 | mRegistration->Version(), |
472 | 0 | mScriptSpec, |
473 | 0 | aNewCacheName, |
474 | 0 | flags); |
475 | 0 |
|
476 | 0 | // If the registration is corrupt enough to force an uninstall if the |
477 | 0 | // upgrade fails, then we want to make sure the upgrade takes effect |
478 | 0 | // if it succeeds. Therefore force the skip-waiting flag on to replace |
479 | 0 | // the broken worker after install. |
480 | 0 | if (aOnFailure == OnFailure::Uninstall) { |
481 | 0 | sw->SetSkipWaitingFlag(); |
482 | 0 | } |
483 | 0 |
|
484 | 0 | mRegistration->SetEvaluating(sw); |
485 | 0 |
|
486 | 0 | nsMainThreadPtrHandle<ServiceWorkerUpdateJob> handle( |
487 | 0 | new nsMainThreadPtrHolder<ServiceWorkerUpdateJob>( |
488 | 0 | "ServiceWorkerUpdateJob", this)); |
489 | 0 | RefPtr<LifeCycleEventCallback> callback = new ContinueUpdateRunnable(handle); |
490 | 0 |
|
491 | 0 | ServiceWorkerPrivate* workerPrivate = sw->WorkerPrivate(); |
492 | 0 | MOZ_ASSERT(workerPrivate); |
493 | 0 | rv = workerPrivate->CheckScriptEvaluation(callback); |
494 | 0 |
|
495 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
496 | 0 | FailUpdateJob(NS_ERROR_DOM_ABORT_ERR); |
497 | 0 | return; |
498 | 0 | } |
499 | 0 | } |
500 | | |
501 | | void |
502 | | ServiceWorkerUpdateJob::ContinueUpdateAfterScriptEval(bool aScriptEvaluationResult) |
503 | 0 | { |
504 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
505 | 0 |
|
506 | 0 | RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance(); |
507 | 0 | if (Canceled() || !swm) { |
508 | 0 | FailUpdateJob(NS_ERROR_DOM_ABORT_ERR); |
509 | 0 | return; |
510 | 0 | } |
511 | 0 | |
512 | 0 | // Step 7.5 of the Update algorithm verifying that the script evaluated |
513 | 0 | // successfully. |
514 | 0 | |
515 | 0 | if (NS_WARN_IF(!aScriptEvaluationResult)) { |
516 | 0 | ErrorResult error; |
517 | 0 |
|
518 | 0 | NS_ConvertUTF8toUTF16 scriptSpec(mScriptSpec); |
519 | 0 | NS_ConvertUTF8toUTF16 scope(mRegistration->Scope()); |
520 | 0 | error.ThrowTypeError<MSG_SW_SCRIPT_THREW>(scriptSpec, scope); |
521 | 0 | FailUpdateJob(error); |
522 | 0 | return; |
523 | 0 | } |
524 | 0 | |
525 | 0 | Install(); |
526 | 0 | } |
527 | | |
528 | | void |
529 | | ServiceWorkerUpdateJob::Install() |
530 | 0 | { |
531 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
532 | 0 | MOZ_DIAGNOSTIC_ASSERT(!Canceled()); |
533 | 0 |
|
534 | 0 | MOZ_ASSERT(!mRegistration->GetInstalling()); |
535 | 0 |
|
536 | 0 | // Begin step 2 of the Install algorithm. |
537 | 0 | // |
538 | 0 | // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#installation-algorithm |
539 | 0 |
|
540 | 0 | mRegistration->TransitionEvaluatingToInstalling(); |
541 | 0 |
|
542 | 0 | // Step 6 of the Install algorithm resolving the job promise. |
543 | 0 | InvokeResultCallbacks(NS_OK); |
544 | 0 |
|
545 | 0 | // The job promise cannot be rejected after this point, but the job can |
546 | 0 | // still fail; e.g. if the install event handler throws, etc. |
547 | 0 |
|
548 | 0 | // Note, the updatefound event is fired automatically when the installing |
549 | 0 | // property is set on the ServiceWorkerRegistration binding object. This |
550 | 0 | // happens via the TransitionEvaluatingToInstalling() call above. |
551 | 0 |
|
552 | 0 | nsMainThreadPtrHandle<ServiceWorkerUpdateJob> handle( |
553 | 0 | new nsMainThreadPtrHolder<ServiceWorkerUpdateJob>( |
554 | 0 | "ServiceWorkerUpdateJob", this)); |
555 | 0 | RefPtr<LifeCycleEventCallback> callback = new ContinueInstallRunnable(handle); |
556 | 0 |
|
557 | 0 | // Send the install event to the worker thread |
558 | 0 | ServiceWorkerPrivate* workerPrivate = |
559 | 0 | mRegistration->GetInstalling()->WorkerPrivate(); |
560 | 0 | nsresult rv = workerPrivate->SendLifeCycleEvent(NS_LITERAL_STRING("install"), |
561 | 0 | callback); |
562 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
563 | 0 | ContinueAfterInstallEvent(false /* aSuccess */); |
564 | 0 | } |
565 | 0 | } |
566 | | |
567 | | void |
568 | | ServiceWorkerUpdateJob::ContinueAfterInstallEvent(bool aInstallEventSuccess) |
569 | 0 | { |
570 | 0 | if (Canceled()) { |
571 | 0 | return FailUpdateJob(NS_ERROR_DOM_ABORT_ERR); |
572 | 0 | } |
573 | 0 | |
574 | 0 | // If we haven't been canceled we should have a registration. There appears |
575 | 0 | // to be a path where it gets cleared before we call into here. Assert |
576 | 0 | // to try to catch this condition, but don't crash in release. |
577 | 0 | MOZ_DIAGNOSTIC_ASSERT(mRegistration); |
578 | 0 | if (!mRegistration) { |
579 | 0 | return FailUpdateJob(NS_ERROR_DOM_ABORT_ERR); |
580 | 0 | } |
581 | 0 | |
582 | 0 | // Continue executing the Install algorithm at step 12. |
583 | 0 | |
584 | 0 | // "If installFailed is true" |
585 | 0 | if (NS_WARN_IF(!aInstallEventSuccess)) { |
586 | 0 | // The installing worker is cleaned up by FailUpdateJob(). |
587 | 0 | FailUpdateJob(NS_ERROR_DOM_ABORT_ERR); |
588 | 0 | return; |
589 | 0 | } |
590 | 0 | |
591 | 0 | MOZ_DIAGNOSTIC_ASSERT(mRegistration->GetInstalling()); |
592 | 0 | mRegistration->TransitionInstallingToWaiting(); |
593 | 0 |
|
594 | 0 | Finish(NS_OK); |
595 | 0 |
|
596 | 0 | // Step 20 calls for explicitly waiting for queued event tasks to fire. Instead, |
597 | 0 | // we simply queue a runnable to execute Activate. This ensures the events are |
598 | 0 | // flushed from the queue before proceeding. |
599 | 0 |
|
600 | 0 | // Step 22 of the Install algorithm. Activate is executed after the completion |
601 | 0 | // of this job. The controlling client and skipWaiting checks are performed |
602 | 0 | // in TryToActivate(). |
603 | 0 | mRegistration->TryToActivateAsync(); |
604 | 0 | } |
605 | | |
606 | | } // namespace dom |
607 | | } // namespace mozilla |