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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/SimpleGlobalObject.h"
11 #include "js/Object.h" // JS::GetClass, JS::GetObjectISupports, JS::SetObjectISupports
13 #include "nsJSPrincipals.h"
14 #include "nsThreadUtils.h"
15 #include "nsContentUtils.h"
17 #include "xpcprivate.h"
19 #include "mozilla/dom/ScriptSettings.h"
20 #include "mozilla/NullPrincipal.h"
22 namespace mozilla::dom
{
24 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(SimpleGlobalObject
)
26 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(SimpleGlobalObject
)
27 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
28 tmp
->UnlinkObjectsInGlobal();
29 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
31 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(SimpleGlobalObject
)
32 tmp
->TraverseObjectsInGlobal(cb
);
33 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
35 NS_IMPL_CYCLE_COLLECTING_ADDREF(SimpleGlobalObject
)
36 NS_IMPL_CYCLE_COLLECTING_RELEASE(SimpleGlobalObject
)
37 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SimpleGlobalObject
)
38 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
39 NS_INTERFACE_MAP_ENTRY(nsIGlobalObject
)
42 static SimpleGlobalObject
* GetSimpleGlobal(JSObject
* global
);
44 static void SimpleGlobal_finalize(JS::GCContext
* gcx
, JSObject
* obj
) {
45 SimpleGlobalObject
* globalObject
= GetSimpleGlobal(obj
);
47 globalObject
->ClearWrapper(obj
);
48 NS_RELEASE(globalObject
);
52 static size_t SimpleGlobal_moved(JSObject
* obj
, JSObject
* old
) {
53 SimpleGlobalObject
* globalObject
= GetSimpleGlobal(obj
);
55 globalObject
->UpdateWrapper(obj
, old
);
60 static const JSClassOps SimpleGlobalClassOps
= {
64 JS_NewEnumerateStandardClasses
,
65 JS_ResolveStandardClass
,
66 JS_MayResolveStandardClass
,
67 SimpleGlobal_finalize
,
70 JS_GlobalObjectTraceHook
,
73 static const js::ClassExtension SimpleGlobalClassExtension
= {
76 static_assert(JSCLASS_GLOBAL_APPLICATION_SLOTS
> 0,
77 "Need at least one slot for JSCLASS_SLOT0_IS_NSISUPPORTS");
79 const JSClass SimpleGlobalClass
= {"",
80 JSCLASS_GLOBAL_FLAGS
|
81 JSCLASS_SLOT0_IS_NSISUPPORTS
|
82 JSCLASS_FOREGROUND_FINALIZE
,
83 &SimpleGlobalClassOps
,
85 &SimpleGlobalClassExtension
,
88 static SimpleGlobalObject
* GetSimpleGlobal(JSObject
* global
) {
89 MOZ_ASSERT(JS::GetClass(global
) == &SimpleGlobalClass
);
91 return JS::GetObjectISupports
<SimpleGlobalObject
>(global
);
95 JSObject
* SimpleGlobalObject::Create(GlobalType globalType
,
96 JS::Handle
<JS::Value
> proto
) {
97 // We can't root our return value with our AutoJSAPI because the rooting
98 // analysis thinks ~AutoJSAPI can GC. So we need to root in a scope outside
99 // the lifetime of the AutoJSAPI.
100 JS::Rooted
<JSObject
*> global(RootingCx());
102 { // Scope to ensure the AutoJSAPI destructor runs before we end up returning
105 JSContext
* cx
= jsapi
.cx();
107 JS::RealmOptions options
;
108 options
.creationOptions()
109 .setInvisibleToDebugger(true)
110 // Put our SimpleGlobalObjects in the system zone, so we won't create
111 // lots of zones for what are probably very short-lived
112 // compartments. This should help them be GCed quicker and take up
113 // less memory before they're GCed.
114 .setNewCompartmentInSystemZone();
116 if (NS_IsMainThread()) {
117 nsCOMPtr
<nsIPrincipal
> principal
=
118 NullPrincipal::CreateWithoutOriginAttributes();
119 options
.creationOptions().setTrace(xpc::TraceXPCGlobal
);
120 global
= xpc::CreateGlobalObject(cx
, &SimpleGlobalClass
,
121 nsJSPrincipals::get(principal
), options
);
123 global
= JS_NewGlobalObject(cx
, &SimpleGlobalClass
, nullptr,
124 JS::DontFireOnNewGlobalHook
, options
);
128 jsapi
.ClearException();
132 JSAutoRealm
ar(cx
, global
);
134 // It's important to create the nsIGlobalObject for our new global before we
135 // start trying to wrap things like the prototype into its compartment,
136 // because the wrap operation relies on the global having its
137 // nsIGlobalObject already.
138 RefPtr
<SimpleGlobalObject
> globalObject
=
139 new SimpleGlobalObject(global
, globalType
);
141 JS::SetRealmReduceTimerPrecisionCallerType(
142 js::GetNonCCWObjectRealm(global
),
143 RTPCallerTypeToToken(globalObject
->GetRTPCallerType()));
145 // Pass on ownership of globalObject to |global|.
146 JS::SetObjectISupports(global
, globalObject
.forget().take());
148 if (proto
.isObjectOrNull()) {
149 JS::Rooted
<JSObject
*> protoObj(cx
, proto
.toObjectOrNull());
150 if (!JS_WrapObject(cx
, &protoObj
)) {
151 jsapi
.ClearException();
155 if (!JS_SetPrototype(cx
, global
, protoObj
)) {
156 jsapi
.ClearException();
159 } else if (!proto
.isUndefined()) {
164 JS_FireOnNewGlobalObject(cx
, global
);
171 SimpleGlobalObject::GlobalType
SimpleGlobalObject::SimpleGlobalType(
173 if (JS::GetClass(obj
) != &SimpleGlobalClass
) {
174 return SimpleGlobalObject::GlobalType::NotSimpleGlobal
;
177 SimpleGlobalObject
* globalObject
= GetSimpleGlobal(obj
);
178 return globalObject
->Type();
181 } // namespace mozilla::dom