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/. */
7 #ifndef mozilla_dom_worklet_WorkletImpl_h
8 #define mozilla_dom_worklet_WorkletImpl_h
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"
18 class nsPIDOMWindowInner
;
27 class WorkletGlobalScope
;
32 class WorkletLoadInfo
{
34 explicit WorkletLoadInfo(nsPIDOMWindowInner
* aWindow
);
36 uint64_t OuterWindowID() const { return mOuterWindowID
; }
37 uint64_t InnerWindowID() const { return mInnerWindowID
; }
40 // Modified only in constructor.
41 uint64_t mOuterWindowID
;
42 const uint64_t mInnerWindowID
;
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
53 using RFPTarget
= mozilla::RFPTarget
;
56 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WorkletImpl
);
58 // Methods for parent thread only:
60 virtual JSObject
* WrapWorklet(JSContext
* aCx
, dom::Worklet
* aWorklet
,
61 JS::Handle
<JSObject
*> aGivenProto
);
63 virtual nsresult
SendControlMessage(already_AddRefed
<nsIRunnable
> aRunnable
);
65 void NotifyWorkletFinished();
67 virtual nsContentPolicyType
ContentPolicyType() const = 0;
69 // Execution thread only.
70 dom::WorkletGlobalScope
* GetGlobalScope();
74 const OriginTrials
& Trials() const { return mTrials
; }
75 const WorkletLoadInfo
& LoadInfo() const { return mWorkletLoadInfo
; }
76 const OriginAttributes
& OriginAttributesRef() const {
77 return mPrincipal
->OriginAttributesRef();
79 nsIPrincipal
* Principal() const { return mPrincipal
; }
80 const ipc::PrincipalInfo
& PrincipalInfo() const { return mPrincipalInfo
; }
82 const Maybe
<nsID
>& GetAgentClusterId() const { return mAgentClusterId
; }
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
);
92 virtual void OnAddModuleStarted() const {
93 MOZ_ASSERT(NS_IsMainThread());
97 virtual void OnAddModulePromiseSettled() const {
98 MOZ_ASSERT(NS_IsMainThread());
103 WorkletImpl(nsPIDOMWindowInner
* aWindow
, nsIPrincipal
* aPrincipal
);
104 virtual ~WorkletImpl();
106 virtual already_AddRefed
<dom::WorkletGlobalScope
> ConstructGlobalScope() = 0;
108 // Modified only in constructor.
109 ipc::PrincipalInfo mPrincipalInfo
;
110 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
112 const WorkletLoadInfo mWorkletLoadInfo
;
114 // Parent thread only.
115 RefPtr
<dom::WorkletThread
> mWorkletThread
;
116 bool mTerminated
: 1;
118 // Execution thread only.
119 RefPtr
<dom::WorkletGlobalScope
> mGlobalScope
;
120 bool mFinishedOnExecutionThread
: 1;
122 Maybe
<nsID
> mAgentClusterId
;
124 bool mSharedMemoryAllowed
: 1;
125 bool mShouldResistFingerprinting
: 1;
126 bool mIsPrivateBrowsing
: 1;
127 // The granular fingerprinting protection overrides applied to the worklet.
128 // This will only get populated if these is one that comes from the local
129 // granular override pref or WebCompat. Otherwise, a value of Nothing()
130 // indicates no granular overrides are present for this workerlet.
131 Maybe
<RFPTarget
> mOverriddenFingerprintingSettings
;
133 const OriginTrials mTrials
;
136 } // namespace mozilla
138 #endif // mozilla_dom_worklet_WorkletImpl_h