WorkletImpl.h (4299B)
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 https://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_worklet_WorkletImpl_h 8 #define mozilla_dom_worklet_WorkletImpl_h 9 10 #include "MainThreadUtils.h" 11 #include "mozilla/BasePrincipal.h" 12 #include "mozilla/Maybe.h" 13 #include "mozilla/OriginAttributes.h" 14 #include "mozilla/OriginTrials.h" 15 #include "mozilla/ipc/PBackgroundSharedTypes.h" 16 #include "nsRFPService.h" 17 18 class nsPIDOMWindowInner; 19 class nsIPrincipal; 20 class nsIRunnable; 21 22 namespace mozilla { 23 24 namespace dom { 25 26 class Worklet; 27 class WorkletGlobalScope; 28 class WorkletThread; 29 30 } // namespace dom 31 32 class WorkletLoadInfo { 33 public: 34 explicit WorkletLoadInfo(nsPIDOMWindowInner* aWindow); 35 36 uint64_t OuterWindowID() const { return mOuterWindowID; } 37 uint64_t InnerWindowID() const { return mInnerWindowID; } 38 39 private: 40 // Modified only in constructor. 41 uint64_t mOuterWindowID; 42 const uint64_t mInnerWindowID; 43 }; 44 45 /** 46 * WorkletImpl is accessed from both the worklet's parent thread (on which the 47 * Worklet object lives) and the worklet's execution thread. It is owned by 48 * Worklet and WorkletGlobalScope. No parent thread cycle collected objects 49 * are owned indefinitely by WorkletImpl because WorkletImpl is not cycle 50 * collected. 51 */ 52 class WorkletImpl { 53 using RFPTarget = mozilla::RFPTarget; 54 55 public: 56 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WorkletImpl); 57 58 // Methods for parent thread only: 59 60 virtual JSObject* WrapWorklet(JSContext* aCx, dom::Worklet* aWorklet, 61 JS::Handle<JSObject*> aGivenProto); 62 63 virtual nsresult SendControlMessage(already_AddRefed<nsIRunnable> aRunnable); 64 65 void NotifyWorkletFinished(); 66 67 virtual nsContentPolicyType ContentPolicyType() const = 0; 68 69 // Execution thread only. 70 dom::WorkletGlobalScope* GetGlobalScope(); 71 72 // Any thread. 73 74 const OriginTrials& Trials() const { return mTrials; } 75 const WorkletLoadInfo& LoadInfo() const { return mWorkletLoadInfo; } 76 const OriginAttributes& OriginAttributesRef() const { 77 return mPrincipal->OriginAttributesRef(); 78 } 79 nsIPrincipal* Principal() const { return mPrincipal; } 80 const ipc::PrincipalInfo& PrincipalInfo() const { return mPrincipalInfo; } 81 82 const Maybe<nsID>& GetAgentClusterId() const { return mAgentClusterId; } 83 84 bool IsSharedMemoryAllowed() const { return mSharedMemoryAllowed; } 85 bool IsSystemPrincipal() const { return mPrincipal->IsSystemPrincipal(); } 86 bool ShouldResistFingerprinting(RFPTarget aTarget) const { 87 return mShouldResistFingerprinting && 88 nsRFPService::IsRFPEnabledFor(mIsPrivateBrowsing, aTarget, 89 mOverriddenFingerprintingSettings); 90 } 91 92 virtual void OnAddModuleStarted() const { 93 MOZ_ASSERT(NS_IsMainThread()); 94 // empty base impl 95 } 96 97 virtual void OnAddModulePromiseSettled() const { 98 MOZ_ASSERT(NS_IsMainThread()); 99 // empty base impl 100 } 101 102 protected: 103 WorkletImpl(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal); 104 virtual ~WorkletImpl(); 105 106 virtual already_AddRefed<dom::WorkletGlobalScope> ConstructGlobalScope( 107 JSContext* aCx) = 0; 108 109 // Modified only in constructor. 110 ipc::PrincipalInfo mPrincipalInfo; 111 nsCOMPtr<nsIPrincipal> mPrincipal; 112 113 const WorkletLoadInfo mWorkletLoadInfo; 114 115 // Parent thread only. 116 RefPtr<dom::WorkletThread> mWorkletThread; 117 bool mTerminated : 1; 118 119 // Execution thread only. 120 RefPtr<dom::WorkletGlobalScope> mGlobalScope; 121 bool mFinishedOnExecutionThread : 1; 122 123 Maybe<nsID> mAgentClusterId; 124 125 bool mSharedMemoryAllowed : 1; 126 bool mShouldResistFingerprinting : 1; 127 bool mIsPrivateBrowsing : 1; 128 // The granular fingerprinting protection overrides applied to the worklet. 129 // This will only get populated if these is one that comes from the local 130 // granular override pref or WebCompat. Otherwise, a value of Nothing() 131 // indicates no granular overrides are present for this workerlet. 132 Maybe<RFPTargetSet> mOverriddenFingerprintingSettings; 133 134 const OriginTrials mTrials; 135 }; 136 137 } // namespace mozilla 138 139 #endif // mozilla_dom_worklet_WorkletImpl_h