1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Shared proto object for XPCWrappedNative. */
#include "xpcprivate.h"
#include "pratom.h"
using namespace mozilla;
#ifdef DEBUG
int32_t XPCWrappedNativeProto::gDEBUG_LiveProtoCount = 0;
#endif
XPCWrappedNativeProto::XPCWrappedNativeProto(XPCWrappedNativeScope* Scope,
nsIClassInfo* ClassInfo,
already_AddRefed<XPCNativeSet>&& Set)
: mScope(Scope),
mJSProtoObject(nullptr),
mClassInfo(ClassInfo),
mSet(Set),
mScriptableInfo(nullptr)
{
// This native object lives as long as its associated JSObject - killed
// by finalization of the JSObject (or explicitly if Init fails).
MOZ_COUNT_CTOR(XPCWrappedNativeProto);
MOZ_ASSERT(mScope);
#ifdef DEBUG
gDEBUG_LiveProtoCount++;
#endif
}
XPCWrappedNativeProto::~XPCWrappedNativeProto()
{
MOZ_ASSERT(!mJSProtoObject, "JSProtoObject still alive");
MOZ_COUNT_DTOR(XPCWrappedNativeProto);
#ifdef DEBUG
gDEBUG_LiveProtoCount--;
#endif
// Note that our weak ref to mScope is not to be trusted at this point.
XPCNativeSet::ClearCacheEntryForClassInfo(mClassInfo);
delete mScriptableInfo;
}
bool
XPCWrappedNativeProto::Init(const XPCNativeScriptableCreateInfo* scriptableCreateInfo,
bool callPostCreatePrototype)
{
AutoJSContext cx;
nsIXPCScriptable* callback = scriptableCreateInfo ?
scriptableCreateInfo->GetCallback() :
nullptr;
if (callback) {
mScriptableInfo =
XPCNativeScriptableInfo::Construct(scriptableCreateInfo);
if (!mScriptableInfo)
return false;
}
const js::Class* jsclazz =
(mScriptableInfo &&
mScriptableInfo->GetFlags().AllowPropModsToPrototype())
? &XPC_WN_ModsAllowed_Proto_JSClass
: &XPC_WN_NoMods_Proto_JSClass;
JS::RootedObject global(cx, mScope->GetGlobalJSObject());
JS::RootedObject proto(cx, JS_GetObjectPrototype(cx, global));
mJSProtoObject = JS_NewObjectWithUniqueType(cx, js::Jsvalify(jsclazz),
proto);
bool success = !!mJSProtoObject;
if (success) {
JS_SetPrivate(mJSProtoObject, this);
if (callPostCreatePrototype)
success = CallPostCreatePrototype();
}
return success;
}
bool
XPCWrappedNativeProto::CallPostCreatePrototype()
{
AutoJSContext cx;
// Nothing to do if we don't have a scriptable callback.
nsIXPCScriptable* callback = mScriptableInfo ? mScriptableInfo->GetCallback()
: nullptr;
if (!callback)
return true;
// Call the helper. This can handle being called if it's not implemented,
// so we don't have to check any sort of "want" here. See xpc_map_end.h.
nsresult rv = callback->PostCreatePrototype(cx, mJSProtoObject);
if (NS_FAILED(rv)) {
JS_SetPrivate(mJSProtoObject, nullptr);
mJSProtoObject = nullptr;
XPCThrower::Throw(rv, cx);
return false;
}
return true;
}
void
XPCWrappedNativeProto::JSProtoObjectFinalized(js::FreeOp* fop, JSObject* obj)
{
MOZ_ASSERT(obj == mJSProtoObject.unbarrieredGet(), "huh?");
// Only remove this proto from the map if it is the one in the map.
ClassInfo2WrappedNativeProtoMap* map = GetScope()->GetWrappedNativeProtoMap();
if (map->Find(mClassInfo) == this)
map->Remove(mClassInfo);
GetContext()->GetDyingWrappedNativeProtoMap()->Add(this);
mJSProtoObject.finalize(js::CastToJSFreeOp(fop)->runtime());
}
void
XPCWrappedNativeProto::JSProtoObjectMoved(JSObject* obj, const JSObject* old)
{
MOZ_ASSERT(mJSProtoObject.unbarrieredGet() == old);
mJSProtoObject.init(obj); // Update without triggering barriers.
}
void
XPCWrappedNativeProto::SystemIsBeingShutDown()
{
// Note that the instance might receive this call multiple times
// as we walk to here from various places.
if (mJSProtoObject) {
// short circuit future finalization
JS_SetPrivate(mJSProtoObject, nullptr);
mJSProtoObject = nullptr;
}
}
// static
XPCWrappedNativeProto*
XPCWrappedNativeProto::GetNewOrUsed(XPCWrappedNativeScope* scope,
nsIClassInfo* classInfo,
const XPCNativeScriptableCreateInfo* scriptableCreateInfo,
bool callPostCreatePrototype)
{
AutoJSContext cx;
MOZ_ASSERT(scope, "bad param");
MOZ_ASSERT(classInfo, "bad param");
AutoMarkingWrappedNativeProtoPtr proto(cx);
ClassInfo2WrappedNativeProtoMap* map = nullptr;
map = scope->GetWrappedNativeProtoMap();
proto = map->Find(classInfo);
if (proto)
return proto;
RefPtr<XPCNativeSet> set = XPCNativeSet::GetNewOrUsed(classInfo);
if (!set)
return nullptr;
proto = new XPCWrappedNativeProto(scope, classInfo, set.forget());
if (!proto || !proto->Init(scriptableCreateInfo, callPostCreatePrototype)) {
delete proto.get();
return nullptr;
}
map->Add(classInfo, proto);
return proto;
}
void
XPCWrappedNativeProto::DebugDump(int16_t depth)
{
#ifdef DEBUG
depth-- ;
XPC_LOG_ALWAYS(("XPCWrappedNativeProto @ %x", this));
XPC_LOG_INDENT();
XPC_LOG_ALWAYS(("gDEBUG_LiveProtoCount is %d", gDEBUG_LiveProtoCount));
XPC_LOG_ALWAYS(("mScope @ %x", mScope));
XPC_LOG_ALWAYS(("mJSProtoObject @ %x", mJSProtoObject.get()));
XPC_LOG_ALWAYS(("mSet @ %x", mSet.get()));
XPC_LOG_ALWAYS(("mScriptableInfo @ %x", mScriptableInfo));
if (depth && mScriptableInfo) {
XPC_LOG_INDENT();
XPC_LOG_ALWAYS(("mScriptable @ %x", mScriptableInfo->GetCallback()));
XPC_LOG_ALWAYS(("mFlags of %x", (uint32_t)mScriptableInfo->GetFlags()));
XPC_LOG_ALWAYS(("mJSClass @ %x", mScriptableInfo->GetJSClass()));
XPC_LOG_OUTDENT();
}
XPC_LOG_OUTDENT();
#endif
}
|