ServiceWorkerRegistration.h (5407B)
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_ServiceWorkerRegistration_h 8 #define mozilla_dom_ServiceWorkerRegistration_h 9 10 #include "mozilla/DOMEventTargetHelper.h" 11 #include "mozilla/dom/ServiceWorkerBinding.h" 12 #include "mozilla/dom/ServiceWorkerRegistrationBinding.h" 13 #include "mozilla/dom/ServiceWorkerRegistrationDescriptor.h" 14 #include "mozilla/dom/ServiceWorkerUtils.h" 15 16 // Support for Notification API extension. 17 #include "mozilla/dom/NotificationBinding.h" 18 19 class nsIGlobalObject; 20 21 namespace mozilla::dom { 22 23 class CookieStoreManager; 24 class NavigationPreloadManager; 25 class Promise; 26 class PushManager; 27 class WorkerPrivate; 28 class ServiceWorker; 29 class ServiceWorkerRegistrationChild; 30 31 #define NS_DOM_SERVICEWORKERREGISTRATION_IID \ 32 {0x4578a90e, 0xa427, 0x4237, {0x98, 0x4a, 0xbd, 0x98, 0xe4, 0xcd, 0x5f, 0x3a}} 33 34 class ServiceWorkerRegistration final : public DOMEventTargetHelper { 35 public: 36 NS_INLINE_DECL_STATIC_IID(NS_DOM_SERVICEWORKERREGISTRATION_IID) 37 NS_DECL_ISUPPORTS_INHERITED 38 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerRegistration, 39 DOMEventTargetHelper) 40 41 IMPL_EVENT_HANDLER(updatefound) 42 43 static already_AddRefed<ServiceWorkerRegistration> CreateForMainThread( 44 nsPIDOMWindowInner* aWindow, 45 const ServiceWorkerRegistrationDescriptor& aDescriptor); 46 47 static already_AddRefed<ServiceWorkerRegistration> CreateForWorker( 48 WorkerPrivate* aWorkerPrivate, nsIGlobalObject* aGlobal, 49 const ServiceWorkerRegistrationDescriptor& aDescriptor); 50 51 JSObject* WrapObject(JSContext* aCx, 52 JS::Handle<JSObject*> aGivenProto) override; 53 54 void DisconnectFromOwner() override; 55 56 void RegistrationCleared(); 57 58 already_AddRefed<ServiceWorker> GetInstalling() const; 59 60 already_AddRefed<ServiceWorker> GetWaiting() const; 61 62 already_AddRefed<ServiceWorker> GetActive() const; 63 64 already_AddRefed<NavigationPreloadManager> NavigationPreload(); 65 66 void UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor); 67 68 bool MatchesDescriptor( 69 const ServiceWorkerRegistrationDescriptor& aDescriptor) const; 70 71 void GetScope(nsAString& aScope) const; 72 73 ServiceWorkerUpdateViaCache GetUpdateViaCache(ErrorResult& aRv) const; 74 75 already_AddRefed<Promise> Update(ErrorResult& aRv); 76 77 already_AddRefed<Promise> Unregister(ErrorResult& aRv); 78 79 already_AddRefed<PushManager> GetPushManager(JSContext* aCx, 80 ErrorResult& aRv); 81 82 already_AddRefed<Promise> ShowNotification( 83 JSContext* aCx, const nsAString& aTitle, 84 const NotificationOptions& aOptions, ErrorResult& aRv); 85 86 already_AddRefed<Promise> GetNotifications( 87 const GetNotificationOptions& aOptions, ErrorResult& aRv); 88 89 void SetNavigationPreloadEnabled(bool aEnabled, 90 ServiceWorkerBoolCallback&& aSuccessCB, 91 ServiceWorkerFailureCallback&& aFailureCB); 92 93 void SetNavigationPreloadHeader(const nsCString& aHeader, 94 ServiceWorkerBoolCallback&& aSuccessCB, 95 ServiceWorkerFailureCallback&& aFailureCB); 96 97 void GetNavigationPreloadState(NavigationPreloadGetStateCallback&& aSuccessCB, 98 ServiceWorkerFailureCallback&& aFailureCB); 99 100 const ServiceWorkerRegistrationDescriptor& Descriptor() const; 101 102 void WhenVersionReached(uint64_t aVersion, 103 ServiceWorkerBoolCallback&& aCallback); 104 105 void RevokeActor(ServiceWorkerRegistrationChild* aActor); 106 107 void FireUpdateFound() { MaybeDispatchUpdateFound(); } 108 109 CookieStoreManager* GetCookies(ErrorResult& aRv); 110 111 private: 112 ServiceWorkerRegistration( 113 nsIGlobalObject* aGlobal, 114 const ServiceWorkerRegistrationDescriptor& aDescriptor); 115 116 ~ServiceWorkerRegistration(); 117 118 void UpdateStateInternal(const Maybe<ServiceWorkerDescriptor>& aInstalling, 119 const Maybe<ServiceWorkerDescriptor>& aWaiting, 120 const Maybe<ServiceWorkerDescriptor>& aActive); 121 122 void MaybeScheduleUpdateFound( 123 const Maybe<ServiceWorkerDescriptor>& aInstallingDescriptor); 124 125 void MaybeDispatchUpdateFound(); 126 127 void Shutdown(); 128 129 ServiceWorkerRegistrationDescriptor mDescriptor; 130 RefPtr<ServiceWorkerRegistrationChild> mActor; 131 bool mShutdown; 132 133 RefPtr<ServiceWorker> mInstallingWorker; 134 RefPtr<ServiceWorker> mWaitingWorker; 135 RefPtr<ServiceWorker> mActiveWorker; 136 RefPtr<NavigationPreloadManager> mNavigationPreloadManager; 137 RefPtr<PushManager> mPushManager; 138 RefPtr<CookieStoreManager> mCookieStoreManager; 139 140 struct VersionCallback { 141 uint64_t mVersion; 142 ServiceWorkerBoolCallback mFunc; 143 144 VersionCallback(uint64_t aVersion, ServiceWorkerBoolCallback&& aFunc) 145 : mVersion(aVersion), mFunc(std::move(aFunc)) { 146 MOZ_DIAGNOSTIC_ASSERT(mFunc); 147 } 148 }; 149 nsTArray<UniquePtr<VersionCallback>> mVersionCallbackList; 150 151 uint64_t mScheduledUpdateFoundId; 152 uint64_t mDispatchedUpdateFoundId; 153 }; 154 155 } // namespace mozilla::dom 156 157 #endif /* mozilla_dom_ServiceWorkerRegistration_h */