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/. */
7 #ifndef mozilla_dom_ShadowRealmGlobalScope_h
8 #define mozilla_dom_ShadowRealmGlobalScope_h
10 #include "js/TypeDecls.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/Maybe.h"
13 #include "mozilla/OriginTrials.h"
14 #include "mozilla/SchedulerGroup.h"
15 #include "mozilla/dom/BindingDeclarations.h"
16 #include "nsContentUtils.h"
17 #include "nsIGlobalObject.h"
18 #include "nsWrapperCache.h"
20 #include "js/loader/ModuleLoaderBase.h"
22 namespace mozilla::dom
{
24 #define SHADOWREALMGLOBALSCOPE_IID \
25 { /* 1b0a59dd-c1cb-429a-bb90-cea17994dba2 */ \
26 0x1b0a59dd, 0xc1cb, 0x429a, { \
27 0xbb, 0x90, 0xce, 0xa1, 0x79, 0x94, 0xdb, 0xa2 \
31 // Required for providing the wrapper, as this is the global used inside a Gecko
32 // backed ShadowRealm, but also required to power module resolution.
33 class ShadowRealmGlobalScope final
: public nsIGlobalObject
,
34 public nsWrapperCache
{
36 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
37 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(ShadowRealmGlobalScope
)
39 NS_DECLARE_STATIC_IID_ACCESSOR(SHADOWREALMGLOBALSCOPE_IID
)
41 explicit ShadowRealmGlobalScope(nsIGlobalObject
* aCreatingGlobal
)
42 : mCreatingGlobal(aCreatingGlobal
) {};
44 nsIGlobalObject
* GetCreatingGlobal() const { return mCreatingGlobal
; }
45 OriginTrials
Trials() const override
{ return {}; }
47 JSObject
* GetGlobalJSObject() override
{ return GetWrapper(); }
48 JSObject
* GetGlobalJSObjectPreserveColor() const override
{
49 return GetWrapperPreserveColor();
52 virtual JSObject
* WrapObject(JSContext
* aCx
,
53 JS::Handle
<JSObject
*> aGivenProto
) override
{
54 MOZ_CRASH("Shouldn't be here");
58 JS::loader::ModuleLoaderBase
* GetModuleLoader(JSContext
* aCx
) override
;
60 bool ShouldResistFingerprinting(RFPTarget aTarget
) const override
{
61 return nsContentUtils::ShouldResistFingerprinting(
62 "Presently we don't have enough context to make an informed decision"
63 "on JS Sandboxes. See 1782853",
67 nsISerialEventTarget
* SerialEventTarget() const final
{
68 return mozilla::GetMainThreadSerialEventTarget();
70 nsresult
Dispatch(already_AddRefed
<nsIRunnable
>&& aRunnable
) const final
{
71 return mozilla::SchedulerGroup::Dispatch(std::move(aRunnable
));
75 virtual ~ShadowRealmGlobalScope() = default;
77 RefPtr
<JS::loader::ModuleLoaderBase
> mModuleLoader
;
79 // The global which created this ShadowRealm
80 nsCOMPtr
<nsIGlobalObject
> mCreatingGlobal
;
83 NS_DEFINE_STATIC_IID_ACCESSOR(ShadowRealmGlobalScope
,
84 SHADOWREALMGLOBALSCOPE_IID
)
86 JSObject
* NewShadowRealmGlobal(JSContext
* aCx
, JS::RealmOptions
& aOptions
,
87 JSPrincipals
* aPrincipals
,
88 JS::Handle
<JSObject
*> aGlobalObj
);
90 bool IsShadowRealmGlobal(JSObject
* aObject
);
92 } // namespace mozilla::dom