ServiceWorker.h (2629B)
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_serviceworker_h__ 8 #define mozilla_dom_serviceworker_h__ 9 10 #include "mozilla/DOMEventTargetHelper.h" 11 #include "mozilla/dom/BindingDeclarations.h" 12 #include "mozilla/dom/ServiceWorkerDescriptor.h" 13 #include "mozilla/dom/ServiceWorkerUtils.h" 14 15 #ifdef XP_WIN 16 # undef PostMessage 17 #endif 18 19 class nsIGlobalObject; 20 21 namespace mozilla::dom { 22 23 class ServiceWorkerChild; 24 class ServiceWorkerCloneData; 25 struct StructuredSerializeOptions; 26 27 #define NS_DOM_SERVICEWORKER_IID \ 28 {0xd42e0611, 0x3647, 0x4319, {0xae, 0x05, 0x19, 0x89, 0x59, 0xba, 0x99, 0x5e}} 29 30 class ServiceWorker final : public DOMEventTargetHelper { 31 public: 32 NS_INLINE_DECL_STATIC_IID(NS_DOM_SERVICEWORKER_IID) 33 NS_DECL_ISUPPORTS_INHERITED 34 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorker, DOMEventTargetHelper) 35 36 IMPL_EVENT_HANDLER(statechange) 37 IMPL_EVENT_HANDLER(error) 38 39 static already_AddRefed<ServiceWorker> Create( 40 nsIGlobalObject* aOwner, const ServiceWorkerDescriptor& aDescriptor); 41 42 virtual JSObject* WrapObject(JSContext* aCx, 43 JS::Handle<JSObject*> aGivenProto) override; 44 45 ServiceWorkerState State() const; 46 47 void SetState(ServiceWorkerState aState); 48 49 void MaybeDispatchStateChangeEvent(); 50 51 void GetScriptURL(nsString& aURL) const; 52 53 void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage, 54 const Sequence<JSObject*>& aTransferable, ErrorResult& aRv); 55 56 void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage, 57 const StructuredSerializeOptions& aOptions, 58 ErrorResult& aRv); 59 60 const ServiceWorkerDescriptor& Descriptor() const; 61 62 void DisconnectFromOwner() override; 63 64 void RevokeActor(ServiceWorkerChild* aActor); 65 66 private: 67 ServiceWorker(nsIGlobalObject* aGlobal, 68 const ServiceWorkerDescriptor& aDescriptor); 69 70 // This class is reference-counted and will be destroyed from Release(). 71 ~ServiceWorker(); 72 73 void MaybeAttachToRegistration(ServiceWorkerRegistration* aRegistration); 74 75 void Shutdown(); 76 77 ServiceWorkerDescriptor mDescriptor; 78 79 RefPtr<ServiceWorkerChild> mActor; 80 bool mShutdown; 81 82 RefPtr<ServiceWorkerRegistration> mRegistration; 83 ServiceWorkerState mLastNotifiedState; 84 }; 85 86 } // namespace mozilla::dom 87 88 #endif // mozilla_dom_serviceworker_h__