Line data Source code
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 "Workers.h"
10 :
11 : namespace mozilla {
12 : namespace dom {
13 : namespace workers {
14 :
15 0 : ServiceWorkerRegisterJob::ServiceWorkerRegisterJob(nsIPrincipal* aPrincipal,
16 : const nsACString& aScope,
17 : const nsACString& aScriptSpec,
18 : nsILoadGroup* aLoadGroup,
19 0 : nsLoadFlags aLoadFlags)
20 : : ServiceWorkerUpdateJob(Type::Register, aPrincipal, aScope, aScriptSpec,
21 0 : aLoadGroup, aLoadFlags)
22 : {
23 0 : }
24 :
25 : void
26 0 : ServiceWorkerRegisterJob::AsyncExecute()
27 : {
28 0 : AssertIsOnMainThread();
29 :
30 0 : RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
31 0 : if (Canceled() || !swm) {
32 0 : FailUpdateJob(NS_ERROR_DOM_ABORT_ERR);
33 0 : return;
34 : }
35 :
36 : RefPtr<ServiceWorkerRegistrationInfo> registration =
37 0 : swm->GetRegistration(mPrincipal, mScope);
38 :
39 0 : if (registration) {
40 0 : bool isSameLoadFlags = registration->GetLoadFlags() == GetLoadFlags();
41 0 : registration->SetLoadFlags(GetLoadFlags());
42 :
43 : // If we are resurrecting an uninstalling registration, then persist
44 : // it to disk again. We preemptively removed it earlier during
45 : // unregister so that closing the window by shutting down the browser
46 : // results in the registration being gone on restart.
47 0 : if (registration->mPendingUninstall) {
48 0 : swm->StoreRegistration(mPrincipal, registration);
49 : }
50 0 : registration->mPendingUninstall = false;
51 0 : RefPtr<ServiceWorkerInfo> newest = registration->Newest();
52 0 : if (newest && mScriptSpec.Equals(newest->ScriptSpec()) && isSameLoadFlags) {
53 0 : SetRegistration(registration);
54 0 : Finish(NS_OK);
55 0 : return;
56 : }
57 : } else {
58 0 : registration = swm->CreateNewRegistration(mScope, mPrincipal,
59 0 : GetLoadFlags());
60 0 : if (!registration) {
61 0 : FailUpdateJob(NS_ERROR_DOM_ABORT_ERR);
62 0 : return;
63 : }
64 : }
65 :
66 0 : SetRegistration(registration);
67 0 : Update();
68 : }
69 :
70 0 : ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob()
71 : {
72 0 : }
73 :
74 : } // namespace workers
75 : } // namespace dom
76 : } // namespace mozilla
|