/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef mozilla_dom_workers_serviceworkerevents_h__ #define mozilla_dom_workers_serviceworkerevents_h__ #include "mozilla/dom/Event.h" #include "mozilla/dom/ExtendableEventBinding.h" #include "mozilla/dom/FetchEventBinding.h" #include "mozilla/dom/Promise.h" #include "mozilla/dom/Response.h" #include "mozilla/dom/workers/bindings/ServiceWorker.h" #ifndef MOZ_SIMPLEPUSH #include "mozilla/dom/PushEventBinding.h" #include "mozilla/dom/PushMessageDataBinding.h" #endif #include "nsProxyRelease.h" class nsIInterceptedChannel; namespace mozilla { namespace dom { class Blob; class Request; class ResponseOrPromise; } // namespace dom } // namespace mozilla BEGIN_WORKERS_NAMESPACE class ServiceWorkerClient; class FetchEvent final : public Event { nsMainThreadPtrHandle mChannel; nsMainThreadPtrHandle mServiceWorker; nsRefPtr mClient; nsRefPtr mRequest; nsAutoPtr mClientInfo; bool mIsReload; bool mWaitToRespond; protected: explicit FetchEvent(EventTarget* aOwner); ~FetchEvent(); public: NS_DECL_ISUPPORTS_INHERITED NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FetchEvent, Event) NS_FORWARD_TO_EVENT virtual JSObject* WrapObjectInternal(JSContext* aCx, JS::Handle aGivenProto) override { return FetchEventBinding::Wrap(aCx, this, aGivenProto); } void PostInit(nsMainThreadPtrHandle& aChannel, nsMainThreadPtrHandle& aServiceWorker, nsAutoPtr& aClientInfo); static already_AddRefed Constructor(const GlobalObject& aGlobal, const nsAString& aType, const FetchEventInit& aOptions, ErrorResult& aRv); bool WaitToRespond() const { return mWaitToRespond; } Request* Request_() const { return mRequest; } already_AddRefed GetClient(); bool IsReload() const { return mIsReload; } void RespondWith(const ResponseOrPromise& aArg, ErrorResult& aRv); already_AddRefed ForwardTo(const nsAString& aUrl); already_AddRefed Default(); }; class ExtendableEvent : public Event { nsRefPtr mPromise; protected: explicit ExtendableEvent(mozilla::dom::EventTarget* aOwner); ~ExtendableEvent() {} public: NS_DECL_ISUPPORTS_INHERITED NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ExtendableEvent, Event) NS_FORWARD_TO_EVENT virtual JSObject* WrapObjectInternal(JSContext* aCx, JS::Handle aGivenProto) override { return mozilla::dom::ExtendableEventBinding::Wrap(aCx, this, aGivenProto); } static already_AddRefed Constructor(mozilla::dom::EventTarget* aOwner, const nsAString& aType, const EventInit& aOptions) { nsRefPtr e = new ExtendableEvent(aOwner); bool trusted = e->Init(aOwner); e->InitEvent(aType, aOptions.mBubbles, aOptions.mCancelable); e->SetTrusted(trusted); return e.forget(); } static already_AddRefed Constructor(const GlobalObject& aGlobal, const nsAString& aType, const EventInit& aOptions, ErrorResult& aRv) { nsCOMPtr target = do_QueryInterface(aGlobal.GetAsSupports()); return Constructor(target, aType, aOptions); } void WaitUntil(Promise& aPromise); already_AddRefed GetPromise() const { nsRefPtr p = mPromise; return p.forget(); } virtual ExtendableEvent* AsExtendableEvent() override { return this; } }; #ifndef MOZ_SIMPLEPUSH class PushMessageData final : public nsISupports, public nsWrapperCache { nsString mData; public: NS_DECL_ISUPPORTS virtual JSObject* WrapObject(JSContext* aCx, JS::Handle aGivenProto) override { return mozilla::dom::PushMessageDataBinding_workers::Wrap(aCx, this, aGivenProto); } nsISupports* GetParentObject() const { return nullptr; } void Json(JSContext* cx, JS::MutableHandle aRetval); void Text(nsAString& aData); void ArrayBuffer(JSContext* cx, JS::MutableHandle aRetval); mozilla::dom::Blob* Blob(); explicit PushMessageData(const nsAString& aData); private: ~PushMessageData(); }; class PushEvent final : public ExtendableEvent { nsString mData; nsMainThreadPtrHandle mServiceWorker; protected: explicit PushEvent(mozilla::dom::EventTarget* aOwner); ~PushEvent() {} public: NS_DECL_ISUPPORTS_INHERITED NS_FORWARD_TO_EVENT virtual JSObject* WrapObjectInternal(JSContext* aCx, JS::Handle aGivenProto) override { return mozilla::dom::PushEventBinding_workers::Wrap(aCx, this, aGivenProto); } static already_AddRefed Constructor(mozilla::dom::EventTarget* aOwner, const nsAString& aType, const PushEventInit& aOptions) { nsRefPtr e = new PushEvent(aOwner); bool trusted = e->Init(aOwner); e->InitEvent(aType, aOptions.mBubbles, aOptions.mCancelable); e->SetTrusted(trusted); if(aOptions.mData.WasPassed()){ e->mData = aOptions.mData.Value(); } return e.forget(); } static already_AddRefed Constructor(const GlobalObject& aGlobal, const nsAString& aType, const PushEventInit& aOptions, ErrorResult& aRv) { nsCOMPtr owner = do_QueryInterface(aGlobal.GetAsSupports()); return Constructor(owner, aType, aOptions); } void PostInit(nsMainThreadPtrHandle& aServiceWorker) { mServiceWorker = aServiceWorker; } already_AddRefed Data() { nsRefPtr data = new PushMessageData(mData); return data.forget(); } }; #endif /* ! MOZ_SIMPLEPUSH */ END_WORKERS_NAMESPACE #endif /* mozilla_dom_workers_serviceworkerevents_h__ */