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
11 namespace mozilla {
12 namespace dom {
13
ServiceWorkerRegisterJob(nsIPrincipal * aPrincipal,const nsACString & aScope,const nsACString & aScriptSpec,nsILoadGroup * aLoadGroup,ServiceWorkerUpdateViaCache aUpdateViaCache)14 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob(
15 nsIPrincipal* aPrincipal, const nsACString& aScope,
16 const nsACString& aScriptSpec, nsILoadGroup* aLoadGroup,
17 ServiceWorkerUpdateViaCache aUpdateViaCache)
18 : ServiceWorkerUpdateJob(Type::Register, aPrincipal, aScope, aScriptSpec,
19 aLoadGroup, 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 // If we are resurrecting an uninstalling registration, then persist
38 // it to disk again. We preemptively removed it earlier during
39 // unregister so that closing the window by shutting down the browser
40 // results in the registration being gone on restart.
41 if (registration->IsPendingUninstall()) {
42 registration->ClearPendingUninstall();
43 swm->StoreRegistration(mPrincipal, registration);
44 // Its possible that a ready promise is created between when the
45 // uninstalling flag is set and when we resurrect the registration
46 // here. In that case we might need to fire the ready promise
47 // now.
48 swm->CheckPendingReadyPromises();
49 }
50 RefPtr<ServiceWorkerInfo> newest = registration->Newest();
51 if (newest && mScriptSpec.Equals(newest->ScriptSpec()) && sameUVC) {
52 SetRegistration(registration);
53 Finish(NS_OK);
54 return;
55 }
56 } else {
57 registration =
58 swm->CreateNewRegistration(mScope, mPrincipal, GetUpdateViaCache());
59 if (!registration) {
60 FailUpdateJob(NS_ERROR_DOM_ABORT_ERR);
61 return;
62 }
63 }
64
65 SetRegistration(registration);
66 Update();
67 }
68
~ServiceWorkerRegisterJob()69 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() {}
70
71 } // namespace dom
72 } // namespace mozilla
73