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
);
29 RefPtr
<ServiceWorkerRegistrationInfo
> registration
=
30 swm
->GetRegistration(mPrincipal
, mScope
);
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
);
44 swm
->CreateNewRegistration(mScope
, mPrincipal
, GetUpdateViaCache());
46 FailUpdateJob(NS_ERROR_DOM_ABORT_ERR
);
51 SetRegistration(registration
);
55 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() = default;
57 } // namespace mozilla::dom