Back to index

lightning-sunbird  0.9+nobinonly
xpcwrappednativeproto.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   John Bandhauer <jband@netscape.com> (original author)
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 /* Possibly shared proto object for XPCWrappedNative. */
00042 
00043 #include "xpcprivate.h"
00044 
00045 #if defined(DEBUG_xpc_hacker) || defined(DEBUG)
00046 PRInt32 XPCWrappedNativeProto::gDEBUG_LiveProtoCount = 0;
00047 #endif
00048 
00049 MOZ_DECL_CTOR_COUNTER(XPCWrappedNativeProto)
00050 
00051 XPCWrappedNativeProto::XPCWrappedNativeProto(XPCWrappedNativeScope* Scope,
00052                                              nsIClassInfo* ClassInfo,
00053                                              PRUint32 ClassInfoFlags,
00054                                              XPCNativeSet* Set)
00055     : mScope(Scope),
00056       mJSProtoObject(nsnull),
00057       mClassInfo(ClassInfo),
00058       mClassInfoFlags(ClassInfoFlags),
00059       mSet(Set),
00060       mSecurityInfo(nsnull),
00061       mScriptableInfo(nsnull)
00062 {
00063     // This native object lives as long as its associated JSObject - killed
00064     // by finalization of the JSObject (or explicitly if Init fails).
00065 
00066     MOZ_COUNT_CTOR(XPCWrappedNativeProto);
00067 
00068 #ifdef DEBUG
00069     PR_AtomicIncrement(&gDEBUG_LiveProtoCount);
00070 #endif
00071 }
00072 
00073 XPCWrappedNativeProto::~XPCWrappedNativeProto()
00074 {
00075     NS_ASSERTION(!mJSProtoObject, "JSProtoObject still alive");
00076 
00077     MOZ_COUNT_DTOR(XPCWrappedNativeProto);
00078 
00079 #ifdef DEBUG
00080     PR_AtomicDecrement(&gDEBUG_LiveProtoCount);
00081 #endif
00082     
00083     // Note that our weak ref to mScope is not to be trusted at this point.
00084 
00085     XPCNativeSet::ClearCacheEntryForClassInfo(mClassInfo);
00086 
00087     delete mScriptableInfo;
00088 }
00089 
00090 JSBool
00091 XPCWrappedNativeProto::Init(
00092                 XPCCallContext& ccx,
00093                 JSBool isGlobal,
00094                 const XPCNativeScriptableCreateInfo* scriptableCreateInfo)
00095 {
00096     if(scriptableCreateInfo && scriptableCreateInfo->GetCallback())
00097     {
00098         mScriptableInfo =
00099             XPCNativeScriptableInfo::Construct(ccx, isGlobal, scriptableCreateInfo);
00100         if(!mScriptableInfo)
00101             return JS_FALSE;
00102     }
00103 
00104     JSClass* jsclazz = mScriptableInfo &&
00105                        mScriptableInfo->GetFlags().AllowPropModsToPrototype() ?
00106                             &XPC_WN_ModsAllowed_Proto_JSClass :
00107                             &XPC_WN_NoMods_Proto_JSClass;
00108 
00109     JSObject *parent = mScope->GetGlobalJSObject();
00110 
00111     mJSProtoObject = JS_NewObject(ccx, jsclazz,
00112                                   mScope->GetPrototypeJSObject(),
00113                                   parent);
00114 
00115     JSBool ok = mJSProtoObject && JS_SetPrivate(ccx, mJSProtoObject, this);
00116 
00117     // Propagate the system flag from parent to child.
00118     if(ok && JS_IsSystemObject(ccx, parent))
00119         JS_FlagSystemObject(ccx, mJSProtoObject);
00120 
00121     DEBUG_ReportShadowedMembers(mSet, nsnull, this);
00122 
00123     return ok;
00124 }
00125 
00126 void
00127 XPCWrappedNativeProto::JSProtoObjectFinalized(JSContext *cx, JSObject *obj)
00128 {
00129     NS_ASSERTION(obj == mJSProtoObject, "huh?");
00130 
00131     // Map locking is not necessary since we are running gc.
00132 
00133     if(IsShared())
00134     {
00135         // Only remove this proto from the map if it is the one in the map.
00136         ClassInfo2WrappedNativeProtoMap* map = 
00137             GetScope()->GetWrappedNativeProtoMap();
00138         if(map->Find(mClassInfo) == this)
00139             map->Remove(mClassInfo);
00140     }
00141 
00142     GetRuntime()->GetDetachedWrappedNativeProtoMap()->Remove(this);
00143     GetRuntime()->GetDyingWrappedNativeProtoMap()->Add(this);
00144 
00145     mJSProtoObject = nsnull;
00146 }
00147 
00148 void
00149 XPCWrappedNativeProto::SystemIsBeingShutDown(XPCCallContext& ccx)
00150 {
00151     // Note that the instance might receive this call multiple times
00152     // as we walk to here from various places.
00153 
00154 #ifdef XPC_TRACK_PROTO_STATS
00155     static PRBool DEBUG_DumpedStats = PR_FALSE;
00156     if(!DEBUG_DumpedStats)
00157     {
00158         printf("%d XPCWrappedNativeProto(s) alive at shutdown\n",
00159                gDEBUG_LiveProtoCount);
00160         DEBUG_DumpedStats = PR_TRUE;
00161     }
00162 #endif
00163 
00164     if(mJSProtoObject)
00165     {
00166         // short circuit future finalization
00167         JS_SetPrivate(ccx, mJSProtoObject, nsnull);
00168         mJSProtoObject = nsnull;
00169     }
00170 }
00171 
00172 // static
00173 XPCWrappedNativeProto*
00174 XPCWrappedNativeProto::GetNewOrUsed(XPCCallContext& ccx,
00175                                     XPCWrappedNativeScope* Scope,
00176                                     nsIClassInfo* ClassInfo,
00177                                     const XPCNativeScriptableCreateInfo* ScriptableCreateInfo,
00178                                     JSBool ForceNoSharing,
00179                                     JSBool isGlobal)
00180 {
00181     NS_ASSERTION(Scope, "bad param");
00182     NS_ASSERTION(ClassInfo, "bad param");
00183 
00184     AutoMarkingWrappedNativeProtoPtr proto(ccx);
00185     ClassInfo2WrappedNativeProtoMap* map;
00186     XPCLock* lock;
00187     JSBool shared;
00188 
00189     JSUint32 ciFlags;
00190     if(NS_FAILED(ClassInfo->GetFlags(&ciFlags)))
00191         ciFlags = 0;
00192 
00193     if(ciFlags & XPC_PROTO_DONT_SHARE)
00194     {
00195         NS_ERROR("reserved flag set!");
00196         ciFlags &= ~XPC_PROTO_DONT_SHARE;
00197     }
00198 
00199     if(ForceNoSharing || (ciFlags & nsIClassInfo::PLUGIN_OBJECT) ||
00200        (ScriptableCreateInfo &&
00201         ScriptableCreateInfo->GetFlags().DontSharePrototype()))
00202     {
00203         ciFlags |= XPC_PROTO_DONT_SHARE;
00204         shared = JS_FALSE;
00205     }
00206     else
00207     {
00208         shared = JS_TRUE;
00209     }
00210 
00211     if(shared)
00212     {
00213         map = Scope->GetWrappedNativeProtoMap();
00214         lock = Scope->GetRuntime()->GetMapLock();
00215         {   // scoped lock
00216             XPCAutoLock al(lock);
00217             proto = map->Find(ClassInfo);
00218             if(proto)
00219                 return proto;
00220         }
00221     }
00222 
00223     AutoMarkingNativeSetPtr set(ccx);
00224     set = XPCNativeSet::GetNewOrUsed(ccx, ClassInfo);
00225     if(!set)
00226         return nsnull;
00227 
00228     proto = new XPCWrappedNativeProto(Scope, ClassInfo, ciFlags, set);
00229 
00230     if(!proto || !proto->Init(ccx, isGlobal, ScriptableCreateInfo))
00231     {
00232         delete proto.get();
00233         return nsnull;
00234     }
00235 
00236     if(shared)
00237     {   // scoped lock
00238         XPCAutoLock al(lock);
00239         map->Add(ClassInfo, proto);
00240     }
00241 
00242     return proto;
00243 }
00244 
00245 void
00246 XPCWrappedNativeProto::DebugDump(PRInt16 depth)
00247 {
00248 #ifdef DEBUG
00249     depth-- ;
00250     XPC_LOG_ALWAYS(("XPCWrappedNativeProto @ %x", this));
00251     XPC_LOG_INDENT();
00252         XPC_LOG_ALWAYS(("gDEBUG_LiveProtoCount is %d", gDEBUG_LiveProtoCount));
00253         XPC_LOG_ALWAYS(("mScope @ %x", mScope));
00254         XPC_LOG_ALWAYS(("mJSProtoObject @ %x", mJSProtoObject));
00255         XPC_LOG_ALWAYS(("mSet @ %x", mSet));
00256         XPC_LOG_ALWAYS(("mSecurityInfo of %x", mSecurityInfo));
00257         XPC_LOG_ALWAYS(("mScriptableInfo @ %x", mScriptableInfo));
00258         if(depth && mScriptableInfo)
00259         {
00260             XPC_LOG_INDENT();
00261             XPC_LOG_ALWAYS(("mScriptable @ %x", mScriptableInfo->GetCallback()));
00262             XPC_LOG_ALWAYS(("mFlags of %x", (PRUint32)mScriptableInfo->GetFlags()));
00263             XPC_LOG_ALWAYS(("mJSClass @ %x", mScriptableInfo->GetJSClass()));
00264             XPC_LOG_OUTDENT();
00265         }
00266     XPC_LOG_OUTDENT();
00267 #endif
00268 }
00269 
00270