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 "ServiceWorkerRegisterJob.h"
8 
9 #include "mozilla/dom/WorkerCommon.h"
10 #include "ServiceWorkerManager.h"
11 
12 namespace mozilla {
13 namespace dom {
14 
ServiceWorkerRegisterJob(nsIPrincipal * aPrincipal,const nsACString & aScope,const nsACString & aScriptSpec,ServiceWorkerUpdateViaCache aUpdateViaCache)15 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob(
16     nsIPrincipal* aPrincipal, const nsACString& aScope,
17     const nsACString& aScriptSpec, ServiceWorkerUpdateViaCache aUpdateViaCache)
18     : ServiceWorkerUpdateJob(Type::Register, aPrincipal, aScope,
19                              nsCString(aScriptSpec), aUpdateViaCache) {}
20 
AsyncExecute()21 void ServiceWorkerRegisterJob::AsyncExecute() {
22   MOZ_ASSERT(NS_IsMainThread());
23 
24   RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
25   if (Canceled() || !swm) {
26     FailUpdateJob(NS_ERROR_DOM_ABORT_ERR);
27     return;
28   }
29 
30   RefPtr<ServiceWorkerRegistrationInfo> registration =
31       swm->GetRegistration(mPrincipal, mScope);
32 
33   if (registration) {
34     bool sameUVC = GetUpdateViaCache() == registration->GetUpdateViaCache();
35     registration->SetUpdateViaCache(GetUpdateViaCache());
36 
37     RefPtr<ServiceWorkerInfo> newest = registration->Newest();
38     if (newest && mScriptSpec.Equals(newest->ScriptSpec()) && sameUVC) {
39       SetRegistration(registration);
40       Finish(NS_OK);
41       return;
42     }
43   } else {
44     registration =
45         swm->CreateNewRegistration(mScope, mPrincipal, GetUpdateViaCache());
46     if (!registration) {
47       FailUpdateJob(NS_ERROR_DOM_ABORT_ERR);
48       return;
49     }
50   }
51 
52   SetRegistration(registration);
53   Update();
54 }
55 
56 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() = default;
57 
58 }  // namespace dom
59 }  // namespace mozilla
60