Bug 1842773 - Part 18: Update TypedArray length, byteLength, and byteOffset accesses...
[gecko.git] / dom / serviceworkers / ServiceWorkerRegisterJob.cpp
blob9d5f6db0986747e06b6b8d3ac9df0702ebebe762
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/. */
7 #include "ServiceWorkerRegisterJob.h"
9 #include "mozilla/dom/WorkerCommon.h"
10 #include "ServiceWorkerManager.h"
12 namespace mozilla::dom {
14 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob(
15 nsIPrincipal* aPrincipal, const nsACString& aScope,
16 const nsACString& aScriptSpec, ServiceWorkerUpdateViaCache aUpdateViaCache)
17 : ServiceWorkerUpdateJob(Type::Register, aPrincipal, aScope,
18 nsCString(aScriptSpec), aUpdateViaCache) {}
20 void ServiceWorkerRegisterJob::AsyncExecute() {
21 MOZ_ASSERT(NS_IsMainThread());
23 RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
24 if (Canceled() || !swm) {
25 FailUpdateJob(NS_ERROR_DOM_ABORT_ERR);
26 return;
29 RefPtr<ServiceWorkerRegistrationInfo> registration =
30 swm->GetRegistration(mPrincipal, mScope);
32 if (registration) {
33 bool sameUVC = GetUpdateViaCache() == registration->GetUpdateViaCache();
34 registration->SetUpdateViaCache(GetUpdateViaCache());
36 RefPtr<ServiceWorkerInfo> newest = registration->Newest();
37 if (newest && mScriptSpec.Equals(newest->ScriptSpec()) && sameUVC) {
38 SetRegistration(registration);
39 Finish(NS_OK);
40 return;
42 } else {
43 registration =
44 swm->CreateNewRegistration(mScope, mPrincipal, GetUpdateViaCache());
45 if (!registration) {
46 FailUpdateJob(NS_ERROR_DOM_ABORT_ERR);
47 return;
51 SetRegistration(registration);
52 Update();
55 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() = default;
57 } // namespace mozilla::dom