Worklet.h (2376B)
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_Worklet_h 8 #define mozilla_dom_Worklet_h 9 10 #include "nsCOMPtr.h" 11 #include "nsRefPtrHashtable.h" 12 #include "nsWrapperCache.h" 13 14 class nsPIDOMWindowInner; 15 16 namespace mozilla { 17 18 class ErrorResult; 19 class WorkletImpl; 20 21 namespace dom { 22 23 class Promise; 24 class WorkletFetchHandler; 25 class WorkletScriptHandler; 26 struct WorkletOptions; 27 enum class CallerType : uint32_t; 28 29 class Worklet : public nsISupports, public nsWrapperCache { 30 public: 31 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 32 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Worklet) 33 34 // |aOwnedObject| may be provided by the WorkletImpl as a parent thread 35 // object to keep alive and traverse for CC as long as the Worklet has 36 // references remaining. 37 Worklet(nsPIDOMWindowInner* aWindow, RefPtr<WorkletImpl> aImpl, 38 nsISupports* aOwnedObject = nullptr); 39 40 nsPIDOMWindowInner* GetParentObject() const { return mWindow; } 41 42 virtual JSObject* WrapObject(JSContext* aCx, 43 JS::Handle<JSObject*> aGivenProto) override; 44 45 already_AddRefed<Promise> AddModule(JSContext* aCx, 46 const nsAString& aModuleURL, 47 const WorkletOptions& aOptions, 48 CallerType aCallerType, ErrorResult& aRv); 49 50 WorkletImpl* Impl() const { return mImpl; } 51 52 const nsTArray<nsString>& GetLocalizedStrings() const { 53 return mLocalizedStrings; 54 } 55 56 protected: 57 virtual ~Worklet(); 58 59 private: 60 WorkletFetchHandler* GetImportFetchHandler(const nsACString& aURI); 61 62 void AddImportFetchHandler(const nsACString& aURI, 63 WorkletFetchHandler* aHandler); 64 65 nsCOMPtr<nsPIDOMWindowInner> mWindow; 66 nsCOMPtr<nsISupports> mOwnedObject; 67 68 nsRefPtrHashtable<nsCStringHashKey, WorkletFetchHandler> mImportHandlers; 69 70 const RefPtr<WorkletImpl> mImpl; 71 72 nsTArray<nsString> mLocalizedStrings; 73 74 friend class WorkletFetchHandler; 75 friend class WorkletScriptHandler; 76 }; 77 78 } // namespace dom 79 } // namespace mozilla 80 81 #endif // mozilla_dom_Worklet_h