1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Mozilla Communicator client code, released
17 : * March 31, 1998.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Netscape Communications Corporation.
21 : * Portions created by the Initial Developer are Copyright (C) 1998
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * John Bandhauer <jband@netscape.com> (original author)
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either of the GNU General Public License Version 2 or later (the "GPL"),
29 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : /* Shared proto object for XPCWrappedNative. */
42 :
43 : #include "xpcprivate.h"
44 :
45 : #if defined(DEBUG_xpc_hacker) || defined(DEBUG)
46 : PRInt32 XPCWrappedNativeProto::gDEBUG_LiveProtoCount = 0;
47 : #endif
48 :
49 186922 : XPCWrappedNativeProto::XPCWrappedNativeProto(XPCWrappedNativeScope* Scope,
50 : nsIClassInfo* ClassInfo,
51 : PRUint32 ClassInfoFlags,
52 : XPCNativeSet* Set,
53 : QITableEntry* offsets)
54 : : mScope(Scope),
55 : mJSProtoObject(nsnull),
56 : mClassInfo(ClassInfo),
57 : mClassInfoFlags(ClassInfoFlags),
58 : mSet(Set),
59 : mSecurityInfo(nsnull),
60 : mScriptableInfo(nsnull),
61 186922 : mOffsets(offsets)
62 : {
63 : // This native object lives as long as its associated JSObject - killed
64 : // by finalization of the JSObject (or explicitly if Init fails).
65 :
66 186922 : MOZ_COUNT_CTOR(XPCWrappedNativeProto);
67 :
68 : #ifdef DEBUG
69 186922 : PR_ATOMIC_INCREMENT(&gDEBUG_LiveProtoCount);
70 : #endif
71 186922 : }
72 :
73 373562 : XPCWrappedNativeProto::~XPCWrappedNativeProto()
74 : {
75 186781 : NS_ASSERTION(!mJSProtoObject, "JSProtoObject still alive");
76 :
77 186781 : MOZ_COUNT_DTOR(XPCWrappedNativeProto);
78 :
79 : #ifdef DEBUG
80 186781 : PR_ATOMIC_DECREMENT(&gDEBUG_LiveProtoCount);
81 : #endif
82 :
83 : // Note that our weak ref to mScope is not to be trusted at this point.
84 :
85 186781 : XPCNativeSet::ClearCacheEntryForClassInfo(mClassInfo);
86 :
87 186781 : delete mScriptableInfo;
88 186781 : }
89 :
90 : JSBool
91 186922 : XPCWrappedNativeProto::Init(XPCCallContext& ccx,
92 : const XPCNativeScriptableCreateInfo* scriptableCreateInfo,
93 : bool callPostCreatePrototype)
94 : {
95 : nsIXPCScriptable *callback = scriptableCreateInfo ?
96 : scriptableCreateInfo->GetCallback() :
97 186922 : nsnull;
98 186922 : if (callback) {
99 : mScriptableInfo =
100 14667 : XPCNativeScriptableInfo::Construct(ccx, scriptableCreateInfo);
101 14667 : if (!mScriptableInfo)
102 0 : return false;
103 : }
104 :
105 : js::Class* jsclazz;
106 :
107 :
108 186922 : if (mScriptableInfo) {
109 14667 : const XPCNativeScriptableFlags& flags(mScriptableInfo->GetFlags());
110 :
111 14667 : if (flags.AllowPropModsToPrototype()) {
112 2781 : jsclazz = flags.WantCall() ?
113 : &XPC_WN_ModsAllowed_WithCall_Proto_JSClass :
114 2781 : &XPC_WN_ModsAllowed_NoCall_Proto_JSClass;
115 : } else {
116 11886 : jsclazz = flags.WantCall() ?
117 : &XPC_WN_NoMods_WithCall_Proto_JSClass :
118 11886 : &XPC_WN_NoMods_NoCall_Proto_JSClass;
119 : }
120 : } else {
121 172255 : jsclazz = &XPC_WN_NoMods_NoCall_Proto_JSClass;
122 : }
123 :
124 186922 : JSObject *parent = mScope->GetGlobalJSObject();
125 :
126 : mJSProtoObject =
127 : xpc_NewSystemInheritingJSObject(ccx, js::Jsvalify(jsclazz),
128 : mScope->GetPrototypeJSObject(),
129 186922 : true, parent);
130 :
131 186922 : bool success = !!mJSProtoObject;
132 186922 : if (success) {
133 186922 : JS_SetPrivate(mJSProtoObject, this);
134 186922 : if (callPostCreatePrototype)
135 173705 : success = CallPostCreatePrototype(ccx);
136 : }
137 :
138 : DEBUG_ReportShadowedMembers(mSet, nsnull, this);
139 :
140 186922 : return success;
141 : }
142 :
143 : bool
144 173705 : XPCWrappedNativeProto::CallPostCreatePrototype(XPCCallContext& ccx)
145 : {
146 : // Nothing to do if we don't have a scriptable callback.
147 14667 : nsIXPCScriptable *callback = mScriptableInfo ? mScriptableInfo->GetCallback()
148 188372 : : nsnull;
149 173705 : if (!callback)
150 159038 : return true;
151 :
152 : // Call the helper. This can handle being called if it's not implemented,
153 : // so we don't have to check any sort of "want" here. See xpc_map_end.h.
154 14667 : nsresult rv = callback->PostCreatePrototype(ccx, mJSProtoObject);
155 14667 : if (NS_FAILED(rv)) {
156 0 : JS_SetPrivate(mJSProtoObject, nsnull);
157 0 : mJSProtoObject = nsnull;
158 0 : XPCThrower::Throw(rv, ccx);
159 0 : return false;
160 : }
161 :
162 14667 : return true;
163 : }
164 :
165 : void
166 186781 : XPCWrappedNativeProto::JSProtoObjectFinalized(JSContext *cx, JSObject *obj)
167 : {
168 186781 : NS_ASSERTION(obj == mJSProtoObject, "huh?");
169 :
170 : // Map locking is not necessary since we are running gc.
171 :
172 : // Only remove this proto from the map if it is the one in the map.
173 : ClassInfo2WrappedNativeProtoMap* map =
174 186781 : GetScope()->GetWrappedNativeProtoMap(ClassIsMainThreadOnly());
175 186781 : if (map->Find(mClassInfo) == this)
176 186781 : map->Remove(mClassInfo);
177 :
178 186781 : GetRuntime()->GetDetachedWrappedNativeProtoMap()->Remove(this);
179 186781 : GetRuntime()->GetDyingWrappedNativeProtoMap()->Add(this);
180 :
181 186781 : mJSProtoObject.finalize(cx);
182 186781 : }
183 :
184 : void
185 861 : XPCWrappedNativeProto::SystemIsBeingShutDown()
186 : {
187 : // Note that the instance might receive this call multiple times
188 : // as we walk to here from various places.
189 :
190 : #ifdef XPC_TRACK_PROTO_STATS
191 : static bool DEBUG_DumpedStats = false;
192 : if (!DEBUG_DumpedStats) {
193 : printf("%d XPCWrappedNativeProto(s) alive at shutdown\n",
194 : gDEBUG_LiveProtoCount);
195 : DEBUG_DumpedStats = true;
196 : }
197 : #endif
198 :
199 861 : if (mJSProtoObject) {
200 : // short circuit future finalization
201 126 : JS_SetPrivate(mJSProtoObject, nsnull);
202 126 : mJSProtoObject = nsnull;
203 : }
204 861 : }
205 :
206 : // static
207 : XPCWrappedNativeProto*
208 383074 : XPCWrappedNativeProto::GetNewOrUsed(XPCCallContext& ccx,
209 : XPCWrappedNativeScope* scope,
210 : nsIClassInfo* classInfo,
211 : const XPCNativeScriptableCreateInfo* scriptableCreateInfo,
212 : QITableEntry* offsets,
213 : bool callPostCreatePrototype)
214 : {
215 383074 : NS_ASSERTION(scope, "bad param");
216 383074 : NS_ASSERTION(classInfo, "bad param");
217 :
218 766148 : AutoMarkingWrappedNativeProtoPtr proto(ccx);
219 383074 : ClassInfo2WrappedNativeProtoMap* map = nsnull;
220 383074 : XPCLock* lock = nsnull;
221 :
222 : uint32_t ciFlags;
223 383074 : if (NS_FAILED(classInfo->GetFlags(&ciFlags)))
224 0 : ciFlags = 0;
225 :
226 383074 : JSBool mainThreadOnly = !!(ciFlags & nsIClassInfo::MAIN_THREAD_ONLY);
227 383074 : map = scope->GetWrappedNativeProtoMap(mainThreadOnly);
228 383074 : lock = mainThreadOnly ? nsnull : scope->GetRuntime()->GetMapLock();
229 : { // scoped lock
230 766148 : XPCAutoLock al(lock);
231 383074 : proto = map->Find(classInfo);
232 383074 : if (proto)
233 196152 : return proto;
234 : }
235 :
236 373844 : AutoMarkingNativeSetPtr set(ccx);
237 186922 : set = XPCNativeSet::GetNewOrUsed(ccx, classInfo);
238 186922 : if (!set)
239 0 : return nsnull;
240 :
241 373844 : proto = new XPCWrappedNativeProto(scope, classInfo, ciFlags, set, offsets);
242 :
243 186922 : if (!proto || !proto->Init(ccx, scriptableCreateInfo, callPostCreatePrototype)) {
244 0 : delete proto.get();
245 0 : return nsnull;
246 : }
247 :
248 : { // scoped lock
249 373844 : XPCAutoLock al(lock);
250 186922 : map->Add(classInfo, proto);
251 : }
252 :
253 186922 : return proto;
254 : }
255 :
256 : void
257 0 : XPCWrappedNativeProto::DebugDump(PRInt16 depth)
258 : {
259 : #ifdef DEBUG
260 0 : depth-- ;
261 0 : XPC_LOG_ALWAYS(("XPCWrappedNativeProto @ %x", this));
262 0 : XPC_LOG_INDENT();
263 0 : XPC_LOG_ALWAYS(("gDEBUG_LiveProtoCount is %d", gDEBUG_LiveProtoCount));
264 0 : XPC_LOG_ALWAYS(("mScope @ %x", mScope));
265 0 : XPC_LOG_ALWAYS(("mJSProtoObject @ %x", mJSProtoObject.get()));
266 0 : XPC_LOG_ALWAYS(("mSet @ %x", mSet));
267 0 : XPC_LOG_ALWAYS(("mSecurityInfo of %x", mSecurityInfo));
268 0 : XPC_LOG_ALWAYS(("mScriptableInfo @ %x", mScriptableInfo));
269 0 : if (depth && mScriptableInfo) {
270 0 : XPC_LOG_INDENT();
271 0 : XPC_LOG_ALWAYS(("mScriptable @ %x", mScriptableInfo->GetCallback()));
272 0 : XPC_LOG_ALWAYS(("mFlags of %x", (PRUint32)mScriptableInfo->GetFlags()));
273 0 : XPC_LOG_ALWAYS(("mJSClass @ %x", mScriptableInfo->GetJSClass()));
274 0 : XPC_LOG_OUTDENT();
275 : }
276 0 : XPC_LOG_OUTDENT();
277 : #endif
278 0 : }
279 :
280 :
|