ServiceWorkerUpdateJob.h (4362B)
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 #ifndef mozilla_dom_serviceworkerupdatejob_h 8 #define mozilla_dom_serviceworkerupdatejob_h 9 10 #include "ServiceWorkerJob.h" 11 #include "ServiceWorkerRegistration.h" 12 #include "mozilla/dom/ServiceWorkerLifetimeExtension.h" 13 #include "nsIRequest.h" 14 15 namespace mozilla::dom { 16 17 namespace serviceWorkerScriptCache { 18 enum class OnFailure : uint8_t; 19 } // namespace serviceWorkerScriptCache 20 21 class ServiceWorkerManager; 22 class ServiceWorkerRegistrationInfo; 23 24 // A job class that performs the Update and Install algorithms from the 25 // service worker spec. This class is designed to be inherited and customized 26 // as a different job type. This is necessary because the register job 27 // performs largely the same operations as the update job, but has a few 28 // different starting steps. 29 class ServiceWorkerUpdateJob : public ServiceWorkerJob { 30 public: 31 // Construct an update job to be used only for updates. 32 ServiceWorkerUpdateJob( 33 nsIPrincipal* aPrincipal, const nsACString& aScope, nsCString aScriptSpec, 34 ServiceWorkerUpdateViaCache aUpdateViaCache, 35 const ServiceWorkerLifetimeExtension& aLifetimeExtension); 36 37 already_AddRefed<ServiceWorkerRegistrationInfo> GetRegistration() const; 38 39 protected: 40 // Construct an update job that is overriden as another job type. 41 ServiceWorkerUpdateJob( 42 Type aType, nsIPrincipal* aPrincipal, const nsACString& aScope, 43 nsCString aScriptSpec, ServiceWorkerUpdateViaCache aUpdateViaCache, 44 const ServiceWorkerLifetimeExtension& aLifetimeExtension); 45 46 virtual ~ServiceWorkerUpdateJob(); 47 48 // FailUpdateJob() must be called if an update job needs Finish() with 49 // an error. 50 void FailUpdateJob(ErrorResult& aRv); 51 52 void FailUpdateJob(nsresult aRv); 53 54 // The entry point when the update job is being used directly. Job 55 // types overriding this class should override this method to 56 // customize behavior. 57 virtual void AsyncExecute() override; 58 59 // Set the registration to be operated on by Update() or to be immediately 60 // returned as a result of the job. This must be called before Update(). 61 void SetRegistration(ServiceWorkerRegistrationInfo* aRegistration); 62 63 // Execute the bulk of the update job logic using the registration defined 64 // by a previous SetRegistration() call. This can be called by the overriden 65 // AsyncExecute() to complete the job. The Update() method will always call 66 // Finish(). This method corresponds to the spec Update algorithm. 67 void Update(); 68 69 ServiceWorkerUpdateViaCache GetUpdateViaCache() const; 70 71 private: 72 class CompareCallback; 73 class ContinueUpdateRunnable; 74 class ContinueInstallRunnable; 75 76 // Utility method called after a script is loaded and compared to 77 // our current cached script. 78 void ComparisonResult(nsresult aStatus, bool aInCacheAndEqual, 79 serviceWorkerScriptCache::OnFailure aOnFailure, 80 const nsAString& aNewCacheName, 81 const nsACString& aMaxScope, nsLoadFlags aLoadFlags); 82 83 // Utility method called after evaluating the worker script. 84 void ContinueUpdateAfterScriptEval(bool aScriptEvaluationResult); 85 86 // Utility method corresponding to the spec Install algorithm. 87 void Install(); 88 89 // Utility method called after the install event is handled. 90 void ContinueAfterInstallEvent(bool aInstallEventSuccess); 91 92 RefPtr<ServiceWorkerRegistrationInfo> mRegistration; 93 ServiceWorkerUpdateViaCache mUpdateViaCache; 94 // The lifetime extension to be applied each time we interact with the 95 // potentially new installing ServiceWorker. A `FullLifetimeExtension` value 96 // here (as used by/on behalf of window clients) means a fresh extension will 97 // be granted with every lifecycle event, whereas a 98 // `PropagatedLifetimeExtension` for updates initiated via ServiceWorkers will 99 // have the same deadline applied each time. 100 ServiceWorkerLifetimeExtension mLifetimeExtension; 101 serviceWorkerScriptCache::OnFailure mOnFailure; 102 }; 103 104 } // namespace mozilla::dom 105 106 #endif // mozilla_dom_serviceworkerupdatejob_h