ServiceWorkerRegisterJob.cpp (2318B)
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 "ServiceWorkerManager.h" 10 #include "mozilla/ProfilerMarkers.h" 11 #include "mozilla/dom/WorkerCommon.h" 12 13 namespace mozilla::dom { 14 15 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( 16 nsIPrincipal* aPrincipal, const nsACString& aScope, const WorkerType& aType, 17 const nsACString& aScriptSpec, ServiceWorkerUpdateViaCache aUpdateViaCache, 18 const ServiceWorkerLifetimeExtension& aLifetimeExtension) 19 : ServiceWorkerUpdateJob(Type::Register, aPrincipal, aScope, 20 nsCString(aScriptSpec), aUpdateViaCache, 21 aLifetimeExtension), 22 mType(aType) {} 23 24 void ServiceWorkerRegisterJob::AsyncExecute() { 25 MOZ_ASSERT(NS_IsMainThread()); 26 27 AUTO_PROFILER_MARKER_UNTYPED("SWRJ AsyncExecute", DOM, {}); 28 29 RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance(); 30 if (Canceled() || !swm) { 31 FailUpdateJob(NS_ERROR_DOM_ABORT_ERR); 32 return; 33 } 34 35 RefPtr<ServiceWorkerRegistrationInfo> registration = 36 swm->GetRegistration(mPrincipal, mScope); 37 38 if (registration) { 39 // if registration already exists, comparing it's options to see if 40 // they have been changed 41 bool sameOptions = 42 GetUpdateViaCache() == registration->GetUpdateViaCache() && 43 mType == registration->Type(); 44 45 registration->SetOptions(GetUpdateViaCache(), mType); 46 47 RefPtr<ServiceWorkerInfo> newest = registration->Newest(); 48 if (newest && mScriptSpec.Equals(newest->ScriptSpec()) && sameOptions) { 49 SetRegistration(registration); 50 Finish(NS_OK); 51 return; 52 } 53 } else { 54 registration = swm->CreateNewRegistration(mScope, mType, mPrincipal, 55 GetUpdateViaCache()); 56 if (!registration) { 57 FailUpdateJob(NS_ERROR_DOM_ABORT_ERR); 58 return; 59 } 60 } 61 62 SetRegistration(registration); 63 Update(); 64 } 65 66 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() = default; 67 68 } // namespace mozilla::dom