1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3
* ***** BEGIN LICENSE BLOCK *****
4
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
16
* The Original Code is Mozilla Communicator client code, released
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.
25
* John Bandhauer <jband@netscape.com> (original author)
26
* Pierre Phaneuf <pp@ludusdesign.com>
28
* Alternatively, the contents of this file may be used under the terms of
29
* either of the GNU General Public License Version 2 or later (the "GPL"),
30
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31
* in which case the provisions of the GPL or the LGPL are applicable instead
32
* of those above. If you wish to allow use of your version of this file only
33
* under the terms of either the GPL or the LGPL, and not to allow others to
34
* use your version of this file under the terms of the MPL, indicate your
35
* decision by deleting the provisions above and replace them with the notice
36
* and other provisions required by the GPL or the LGPL. If you do not delete
37
* the provisions above, a recipient may use your version of this file under
38
* the terms of any one of the MPL, the GPL or the LGPL.
40
* ***** END LICENSE BLOCK ***** */
42
/* An xpcom implementation of the JavaScript nsIID and nsCID objects. */
44
#include "xpcprivate.h"
46
/***************************************************************************/
49
NS_IMPL_THREADSAFE_ISUPPORTS1(nsJSID, nsIJSID)
51
char nsJSID::gNoString[] = "";
54
: mID(GetInvalidIID()), mNumber(gNoString), mName(gNoString)
60
if(mNumber && mNumber != gNoString)
62
if(mName && mName != gNoString)
68
mID = GetInvalidIID();
70
if(mNumber && mNumber != gNoString)
72
if(mName && mName != gNoString)
75
mNumber = mName = nsnull;
79
nsJSID::SetName(const char* name)
81
NS_ASSERTION(!mName || mName == gNoString ,"name already set");
82
NS_ASSERTION(name,"null name");
83
int len = strlen(name)+1;
84
mName = (char*)PR_Malloc(len);
87
memcpy(mName, name, len);
92
nsJSID::GetName(char * *aName)
95
return NS_ERROR_NULL_POINTER;
99
NS_ASSERTION(mName, "name not set");
100
*aName = (char*) nsMemory::Clone(mName, strlen(mName)+1);
101
return *aName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
105
nsJSID::GetNumber(char * *aNumber)
108
return NS_ERROR_NULL_POINTER;
112
if(!(mNumber = mID.ToString()))
116
*aNumber = (char*) nsMemory::Clone(mNumber, strlen(mNumber)+1);
117
return *aNumber ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
121
nsJSID::GetId(nsID* *aId)
124
return NS_ERROR_NULL_POINTER;
126
*aId = (nsID*) nsMemory::Clone(&mID, sizeof(nsID));
127
return *aId ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
131
nsJSID::GetValid(PRBool *aValid)
134
return NS_ERROR_NULL_POINTER;
141
nsJSID::Equals(nsIJSID *other, PRBool *_retval)
144
return NS_ERROR_NULL_POINTER;
148
if(!other || mID.Equals(GetInvalidIID()))
152
if(NS_SUCCEEDED(other->GetId(&otherID)))
154
*_retval = mID.Equals(*otherID);
155
nsMemory::Free(otherID);
161
nsJSID::Initialize(const char *idString)
164
return NS_ERROR_NULL_POINTER;
166
PRBool success = PR_FALSE;
168
if(strlen(idString) && mID.Equals(GetInvalidIID()))
172
if(idString[0] == '{')
175
if(id.Parse((char*)idString))
182
return success ? NS_OK : NS_ERROR_FAILURE;
186
nsJSID::InitWithName(const nsID& id, const char *nameString)
188
NS_ASSERTION(nameString, "no name");
191
return SetName(nameString);
194
// try to use the name, if no name, then use the number
196
nsJSID::ToString(char **_retval)
198
if(mName != gNoString)
201
if(NS_SUCCEEDED(GetName(&str)))
203
if(mName != gNoString)
212
return GetNumber(_retval);
216
nsJSID::GetInvalidIID() const
218
// {BB1F47B0-D137-11d2-9841-006008962422}
219
static nsID invalid = {0xbb1f47b0, 0xd137, 0x11d2,
220
{0x98, 0x41, 0x0, 0x60, 0x8, 0x96, 0x24, 0x22}};
226
nsJSID::NewID(const char* str)
230
NS_ASSERTION(0,"no string");
234
nsJSID* idObj = new nsJSID();
238
if(NS_FAILED(idObj->Initialize(str)))
244
/***************************************************************************/
245
// Class object support so that we can share prototypes of wrapper
247
// This class exists just so we can have a shared scriptable helper for
248
// the nsJSIID class. The instances implement their own helpers. But we
249
// needed to be able to indicate to the shared prototypes this single flag:
250
// nsIXPCScriptable::DONT_ENUM_STATIC_PROPS. And having a class to do it is
251
// the only means we have. Setting this flag on any given instance scriptable
252
// helper is not sufficient to convey the information that we don't want
253
// static properties enumerated on the shared proto.
255
class SharedScriptableHelperForJSIID : public nsIXPCScriptable
259
NS_DECL_NSIXPCSCRIPTABLE
260
SharedScriptableHelperForJSIID() {}
263
NS_INTERFACE_MAP_BEGIN(SharedScriptableHelperForJSIID)
264
NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
265
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIXPCScriptable)
266
NS_INTERFACE_MAP_END_THREADSAFE
268
NS_IMPL_THREADSAFE_ADDREF(SharedScriptableHelperForJSIID)
269
NS_IMPL_THREADSAFE_RELEASE(SharedScriptableHelperForJSIID)
271
// The nsIXPCScriptable map declaration that will generate stubs for us...
272
#define XPC_MAP_CLASSNAME SharedScriptableHelperForJSIID
273
#define XPC_MAP_QUOTED_CLASSNAME "JSIID"
274
#define XPC_MAP_FLAGS nsIXPCScriptable::DONT_ENUM_STATIC_PROPS |\
275
nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
276
#include "xpc_map_end.h" /* This will #undef the above */
278
static nsIXPCScriptable* gSharedScriptableHelperForJSIID;
280
NS_METHOD GetSharedScriptableHelperForJSIID(PRUint32 language,
281
nsISupports **helper)
283
if(language == nsIProgrammingLanguage::JAVASCRIPT)
285
NS_IF_ADDREF(gSharedScriptableHelperForJSIID);
286
*helper = gSharedScriptableHelperForJSIID;
293
/******************************************************/
295
static JSBool gClassObjectsWereKilled = JS_FALSE;
296
static JSBool gClassObjectsWereInited = JS_FALSE;
298
NS_DECL_CI_INTERFACE_GETTER(nsJSIID)
299
// Can't make this static. http://bugzilla.mozilla.org/show_bug.cgi?id=81436
300
nsIClassInfo* NS_CLASSINFO_NAME(nsJSIID);
301
static const nsModuleComponentInfo CI_nsJSIID =
303
{0x26ecb8d0, 0x35c9, 0x11d5, { 0x90, 0xb2, 0x0, 0x10, 0xa4, 0xe7, 0x3d, 0x9a }},
304
nsnull, nsnull, nsnull,nsnull, nsnull,
305
NS_CI_INTERFACE_GETTER_NAME(nsJSIID),
306
GetSharedScriptableHelperForJSIID,
307
&NS_CLASSINFO_NAME(nsJSIID), 0};
309
NS_DECL_CI_INTERFACE_GETTER(nsJSCID)
310
// Can't make this static. http://bugzilla.mozilla.org/show_bug.cgi?id=81436
311
nsIClassInfo* NS_CLASSINFO_NAME(nsJSCID);
312
static const nsModuleComponentInfo CI_nsJSCID =
314
{0x9255b5b0, 0x35cf, 0x11d5, { 0x90, 0xb2, 0x0, 0x10, 0xa4, 0xe7, 0x3d, 0x9a }},
315
nsnull, nsnull, nsnull,nsnull, nsnull,
316
NS_CI_INTERFACE_GETTER_NAME(nsJSCID), nsnull,
317
&NS_CLASSINFO_NAME(nsJSCID), 0};
319
JSBool xpc_InitJSxIDClassObjects()
321
if(gClassObjectsWereKilled)
323
if(gClassObjectsWereInited)
328
if(!NS_CLASSINFO_NAME(nsJSIID))
330
nsCOMPtr<nsIGenericFactory> factory;
331
rv = NS_NewGenericFactory(getter_AddRefs(factory), &CI_nsJSIID);
334
rv = factory->QueryInterface(NS_GET_IID(nsIClassInfo),
335
(void**)&NS_CLASSINFO_NAME(nsJSIID));
340
if(!NS_CLASSINFO_NAME(nsJSCID))
342
nsCOMPtr<nsIGenericFactory> factory;
343
rv = NS_NewGenericFactory(getter_AddRefs(factory), &CI_nsJSCID);
346
rv = factory->QueryInterface(NS_GET_IID(nsIClassInfo),
347
(void**)&NS_CLASSINFO_NAME(nsJSCID));
352
gSharedScriptableHelperForJSIID = new SharedScriptableHelperForJSIID();
353
if(!gSharedScriptableHelperForJSIID)
355
NS_ADDREF(gSharedScriptableHelperForJSIID);
357
gClassObjectsWereInited = JS_TRUE;
363
void xpc_DestroyJSxIDClassObjects()
365
NS_IF_RELEASE(NS_CLASSINFO_NAME(nsJSIID));
366
NS_IF_RELEASE(NS_CLASSINFO_NAME(nsJSCID));
367
NS_IF_RELEASE(gSharedScriptableHelperForJSIID);
369
gClassObjectsWereKilled = JS_TRUE;
372
/***************************************************************************/
374
NS_INTERFACE_MAP_BEGIN(nsJSIID)
375
NS_INTERFACE_MAP_ENTRY(nsIJSID)
376
NS_INTERFACE_MAP_ENTRY(nsIJSIID)
377
NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
378
#ifdef XPC_USE_SECURITY_CHECKED_COMPONENT
379
NS_INTERFACE_MAP_ENTRY(nsISecurityCheckedComponent)
381
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIJSID)
382
NS_IMPL_QUERY_CLASSINFO(nsJSIID)
383
NS_INTERFACE_MAP_END_THREADSAFE
385
NS_IMPL_THREADSAFE_ADDREF(nsJSIID)
386
NS_IMPL_THREADSAFE_RELEASE(nsJSIID)
387
NS_IMPL_CI_INTERFACE_GETTER2(nsJSIID, nsIJSID, nsIJSIID)
389
// The nsIXPCScriptable map declaration that will generate stubs for us...
390
#define XPC_MAP_CLASSNAME nsJSIID
391
#define XPC_MAP_QUOTED_CLASSNAME "nsJSIID"
392
#define XPC_MAP_WANT_NEWRESOLVE
393
#define XPC_MAP_WANT_ENUMERATE
394
#define XPC_MAP_WANT_HASINSTANCE
395
#define XPC_MAP_FLAGS nsIXPCScriptable::DONT_ENUM_STATIC_PROPS |\
396
nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
397
#include "xpc_map_end.h" /* This will #undef the above */
400
nsJSIID::nsJSIID(nsIInterfaceInfo* aInfo)
405
nsJSIID::~nsJSIID() {}
407
// If mInfo is present we use it and ignore mDetails, else we use mDetails.
409
NS_IMETHODIMP nsJSIID::GetName(char * *aName)
411
return mInfo->GetName(aName);
414
NS_IMETHODIMP nsJSIID::GetNumber(char * *aNumber)
417
mInfo->GetIIDShared(&id);
418
char* str = id->ToString();
420
return NS_ERROR_OUT_OF_MEMORY;
421
*aNumber = (char*) nsMemory::Clone(str, strlen(str)+1);
423
return *aNumber ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
426
NS_IMETHODIMP nsJSIID::GetId(nsID* *aId)
428
return mInfo->GetInterfaceIID((nsIID**)aId);
431
NS_IMETHODIMP nsJSIID::GetValid(PRBool *aValid)
437
NS_IMETHODIMP nsJSIID::Equals(nsIJSID *other, PRBool *_retval)
440
if(NS_SUCCEEDED(other->GetId(&otherID)))
442
mInfo->IsIID((nsIID*)otherID, _retval);
443
nsMemory::Free(otherID);
448
NS_IMETHODIMP nsJSIID::Initialize(const char *idString)
450
return NS_ERROR_FAILURE;
453
NS_IMETHODIMP nsJSIID::ToString(char **_retval)
455
return mInfo->GetName(_retval);
460
nsJSIID::NewID(nsIInterfaceInfo* aInfo)
469
if(NS_FAILED(aInfo->IsScriptable(&canScript)) || !canScript)
472
nsJSIID* idObj = new nsJSIID(aInfo);
478
/* PRBool resolve (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id); */
480
nsJSIID::NewResolve(nsIXPConnectWrappedNative *wrapper,
481
JSContext * cx, JSObject * obj,
482
jsval id, PRUint32 flags,
483
JSObject * *objp, PRBool *_retval)
485
XPCCallContext ccx(JS_CALLER, cx);
487
AutoMarkingNativeInterfacePtr iface(ccx);
490
mInfo->GetIIDShared(&iid);
492
iface = XPCNativeInterface::GetNewOrUsed(ccx, iid);
497
XPCNativeMember* member = iface->FindMember(id);
498
if(member && member->IsConstant())
501
if(!member->GetValue(ccx, iface, &val))
502
return NS_ERROR_OUT_OF_MEMORY;
505
if(!JS_ValueToId(cx, id, &idid))
506
return NS_ERROR_OUT_OF_MEMORY;
509
*_retval = OBJ_DEFINE_PROPERTY(cx, obj, idid, val,
520
/* PRBool enumerate (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj); */
522
nsJSIID::Enumerate(nsIXPConnectWrappedNative *wrapper,
523
JSContext * cx, JSObject * obj, PRBool *_retval)
525
// In this case, let's just eagerly resolve...
527
XPCCallContext ccx(JS_CALLER, cx);
529
AutoMarkingNativeInterfacePtr iface(ccx);
532
mInfo->GetIIDShared(&iid);
534
iface = XPCNativeInterface::GetNewOrUsed(ccx, iid);
539
PRUint16 count = iface->GetMemberCount();
540
for(PRUint16 i = 0; i < count; i++)
542
XPCNativeMember* member = iface->GetMemberAt(i);
543
if(member && member->IsConstant() &&
544
!xpc_ForcePropertyResolve(cx, obj, member->GetName()))
546
return NS_ERROR_UNEXPECTED;
552
/* PRBool hasInstance (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal val, out PRBool bp); */
554
nsJSIID::HasInstance(nsIXPConnectWrappedNative *wrapper,
555
JSContext * cx, JSObject * obj,
556
jsval val, PRBool *bp, PRBool *_retval)
561
if(!JSVAL_IS_PRIMITIVE(val))
563
// we have a JSObject
564
JSObject* obj = JSVAL_TO_OBJECT(val);
566
NS_ASSERTION(obj, "when is an object not an object?");
568
// is this really a native xpcom object with a wrapper?
569
XPCWrappedNative* other_wrapper =
570
XPCWrappedNative::GetWrappedNativeOfJSObject(cx, obj);
576
mInfo->GetIIDShared(&iid);
578
// We'll trust the interface set of the wrapper if this is known
579
// to be an interface that the objects *expects* to be able to
581
if(other_wrapper->HasInterfaceNoQI(*iid))
587
// Otherwise, we'll end up Querying the native object to be sure.
588
XPCCallContext ccx(JS_CALLER, cx);
590
AutoMarkingNativeInterfacePtr iface(ccx);
591
iface = XPCNativeInterface::GetNewOrUsed(ccx, iid);
593
if(iface && other_wrapper->FindTearOff(ccx, iface))
599
#ifdef XPC_USE_SECURITY_CHECKED_COMPONENT
600
/* string canCreateWrapper (in nsIIDPtr iid); */
602
nsJSIID::CanCreateWrapper(const nsIID * iid, char **_retval)
604
// We let anyone do this...
605
*_retval = xpc_CloneAllAccess();
609
/* string canCallMethod (in nsIIDPtr iid, in wstring methodName); */
611
nsJSIID::CanCallMethod(const nsIID * iid, const PRUnichar *methodName, char **_retval)
613
static const char* allowed[] = {"equals", "toString", nsnull};
615
*_retval = xpc_CheckAccessList(methodName, allowed);
619
/* string canGetProperty (in nsIIDPtr iid, in wstring propertyName); */
621
nsJSIID::CanGetProperty(const nsIID * iid, const PRUnichar *propertyName, char **_retval)
623
static const char* allowed[] = {"name", "number", "valid", nsnull};
624
*_retval = xpc_CheckAccessList(propertyName, allowed);
628
/* string canSetProperty (in nsIIDPtr iid, in wstring propertyName); */
630
nsJSIID::CanSetProperty(const nsIID * iid, const PRUnichar *propertyName, char **_retval)
632
// If you have to ask, then the answer is NO
638
/***************************************************************************/
640
NS_INTERFACE_MAP_BEGIN(nsJSCID)
641
NS_INTERFACE_MAP_ENTRY(nsIJSID)
642
NS_INTERFACE_MAP_ENTRY(nsIJSCID)
643
NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
644
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIJSID)
645
NS_IMPL_QUERY_CLASSINFO(nsJSCID)
646
NS_INTERFACE_MAP_END_THREADSAFE
648
NS_IMPL_THREADSAFE_ADDREF(nsJSCID)
649
NS_IMPL_THREADSAFE_RELEASE(nsJSCID)
650
NS_IMPL_CI_INTERFACE_GETTER2(nsJSCID, nsIJSID, nsIJSCID)
652
// The nsIXPCScriptable map declaration that will generate stubs for us...
653
#define XPC_MAP_CLASSNAME nsJSCID
654
#define XPC_MAP_QUOTED_CLASSNAME "nsJSCID"
655
#define XPC_MAP_WANT_CONSTRUCT
656
#define XPC_MAP_WANT_HASINSTANCE
657
#define XPC_MAP_FLAGS 0
658
#include "xpc_map_end.h" /* This will #undef the above */
660
nsJSCID::nsJSCID() {}
661
nsJSCID::~nsJSCID() {}
663
NS_IMETHODIMP nsJSCID::GetName(char * *aName)
664
{ResolveName(); return mDetails.GetName(aName);}
666
NS_IMETHODIMP nsJSCID::GetNumber(char * *aNumber)
667
{return mDetails.GetNumber(aNumber);}
669
NS_IMETHODIMP nsJSCID::GetId(nsID* *aId)
670
{return mDetails.GetId(aId);}
672
NS_IMETHODIMP nsJSCID::GetValid(PRBool *aValid)
673
{return mDetails.GetValid(aValid);}
675
NS_IMETHODIMP nsJSCID::Equals(nsIJSID *other, PRBool *_retval)
676
{return mDetails.Equals(other, _retval);}
678
NS_IMETHODIMP nsJSCID::Initialize(const char *idString)
679
{return mDetails.Initialize(idString);}
681
NS_IMETHODIMP nsJSCID::ToString(char **_retval)
682
{ResolveName(); return mDetails.ToString(_retval);}
685
nsJSCID::ResolveName()
687
if(!mDetails.NameIsSet())
688
mDetails.SetNameToNoString();
693
nsJSCID::NewID(const char* str)
697
NS_ASSERTION(0,"no string");
701
nsJSCID* idObj = new nsJSCID();
704
PRBool success = PR_FALSE;
709
if(NS_SUCCEEDED(idObj->Initialize(str)))
715
if(NS_SUCCEEDED(nsComponentManager::ContractIDToClassID(str, &cid)))
717
success = idObj->mDetails.InitWithName(cid, str);
727
/* nsISupports createInstance (); */
729
nsJSCID::CreateInstance(nsISupports **_retval)
731
if(!mDetails.IsValid())
732
return NS_ERROR_XPC_BAD_CID;
734
nsXPConnect* xpc = nsXPConnect::GetXPConnect();
736
return NS_ERROR_UNEXPECTED;
738
nsCOMPtr<nsIXPCNativeCallContext> ccxp;
739
xpc->GetCurrentNativeCallContext(getter_AddRefs(ccxp));
741
return NS_ERROR_UNEXPECTED;
749
ccxp->GetJSContext(&cx);
750
ccxp->GetArgc(&argc);
751
ccxp->GetArgvPtr(&argv);
752
ccxp->GetRetValPtr(&vp);
754
nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
755
ccxp->GetCalleeWrapper(getter_AddRefs(wrapper));
756
wrapper->GetJSObject(&obj);
758
// Do the security check if necessary
760
XPCContext* xpcc = nsXPConnect::GetContext(cx);
762
nsIXPCSecurityManager* sm;
763
sm = xpcc->GetAppropriateSecurityManager(
764
nsIXPCSecurityManager::HOOK_CREATE_INSTANCE);
765
if(sm && NS_FAILED(sm->CanCreateInstance(cx, *mDetails.GetID())))
767
// the security manager vetoed. It should have set an exception.
768
ccxp->SetExceptionWasThrown(JS_TRUE);
774
// If an IID was passed in then use it
780
if(JSVAL_IS_PRIMITIVE(val) ||
781
!(iidobj = JSVAL_TO_OBJECT(val)) ||
782
!(piid = xpc_JSObjectToID(cx, iidobj)))
784
return NS_ERROR_XPC_BAD_IID;
787
nsMemory::Free(piid);
790
iid = NS_GET_IID(nsISupports);
792
nsCOMPtr<nsISupports> inst;
795
rv = nsComponentManager::CreateInstance(*mDetails.GetID(), nsnull, iid,
796
(void**) getter_AddRefs(inst));
797
NS_ASSERTION(NS_FAILED(rv) || inst, "component manager returned success, but instance is null!");
799
if(NS_FAILED(rv) || !inst)
800
return NS_ERROR_XPC_CI_RETURNED_FAILURE;
803
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
804
rv = xpc->WrapNative(cx, obj, inst, iid, getter_AddRefs(holder));
805
if(NS_FAILED(rv) || !holder || NS_FAILED(holder->GetJSObject(&instJSObj)))
806
return NS_ERROR_XPC_CANT_CREATE_WN;
808
*vp = OBJECT_TO_JSVAL(instJSObj);
809
ccxp->SetReturnValueWasSet(JS_TRUE);
813
/* nsISupports getService (); */
815
nsJSCID::GetService(nsISupports **_retval)
817
if(!mDetails.IsValid())
818
return NS_ERROR_XPC_BAD_CID;
820
nsXPConnect* xpc = nsXPConnect::GetXPConnect();
822
return NS_ERROR_UNEXPECTED;
824
nsCOMPtr<nsIXPCNativeCallContext> ccxp;
825
xpc->GetCurrentNativeCallContext(getter_AddRefs(ccxp));
827
return NS_ERROR_UNEXPECTED;
835
ccxp->GetJSContext(&cx);
836
ccxp->GetArgc(&argc);
837
ccxp->GetArgvPtr(&argv);
838
ccxp->GetRetValPtr(&vp);
840
nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
841
ccxp->GetCalleeWrapper(getter_AddRefs(wrapper));
842
wrapper->GetJSObject(&obj);
844
// Do the security check if necessary
846
XPCContext* xpcc = nsXPConnect::GetContext(cx);
848
nsIXPCSecurityManager* sm;
849
sm = xpcc->GetAppropriateSecurityManager(
850
nsIXPCSecurityManager::HOOK_GET_SERVICE);
851
if(sm && NS_FAILED(sm->CanCreateInstance(cx, *mDetails.GetID())))
853
// the security manager vetoed. It should have set an exception.
854
ccxp->SetExceptionWasThrown(JS_TRUE);
860
// If an IID was passed in then use it
866
if(JSVAL_IS_PRIMITIVE(val) ||
867
!(iidobj = JSVAL_TO_OBJECT(val)) ||
868
!(piid = xpc_JSObjectToID(cx, iidobj)))
870
return NS_ERROR_XPC_BAD_IID;
873
nsMemory::Free(piid);
876
iid = NS_GET_IID(nsISupports);
878
nsCOMPtr<nsISupports> srvc;
881
rv = nsServiceManager::GetService(*mDetails.GetID(), iid,
882
getter_AddRefs(srvc), nsnull);
883
NS_ASSERTION(NS_FAILED(rv) || srvc, "service manager returned success, but service is null!");
884
if(NS_FAILED(rv) || !srvc)
885
return NS_ERROR_XPC_GS_RETURNED_FAILURE;
888
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
889
rv = xpc->WrapNative(cx, obj, srvc, iid, getter_AddRefs(holder));
890
if(NS_FAILED(rv) || !holder || NS_FAILED(holder->GetJSObject(&instJSObj)))
891
return NS_ERROR_XPC_CANT_CREATE_WN;
893
*vp = OBJECT_TO_JSVAL(instJSObj);
894
ccxp->SetReturnValueWasSet(JS_TRUE);
898
/* PRBool construct (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in PRUint32 argc, in JSValPtr argv, in JSValPtr vp); */
900
nsJSCID::Construct(nsIXPConnectWrappedNative *wrapper,
901
JSContext * cx, JSObject * obj,
902
PRUint32 argc, jsval * argv, jsval * vp,
905
XPCJSRuntime* rt = nsXPConnect::GetRuntime();
907
return NS_ERROR_FAILURE;
909
// 'push' a call context and call on it
910
XPCCallContext ccx(JS_CALLER, cx, obj, nsnull,
911
rt->GetStringJSVal(XPCJSRuntime::IDX_CREATE_INSTANCE),
914
*_retval = XPCWrappedNative::CallMethod(ccx);
918
/* PRBool hasInstance (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal val, out PRBool bp); */
920
nsJSCID::HasInstance(nsIXPConnectWrappedNative *wrapper,
921
JSContext * cx, JSObject * obj,
922
jsval val, PRBool *bp, PRBool *_retval)
927
if(!JSVAL_IS_PRIMITIVE(val))
929
// we have a JSObject
930
JSObject* obj = JSVAL_TO_OBJECT(val);
932
NS_ASSERTION(obj, "when is an object not an object?");
934
// is this really a native xpcom object with a wrapper?
935
XPCWrappedNative* other_wrapper =
936
XPCWrappedNative::GetWrappedNativeOfJSObject(cx, obj);
941
// We consider CID equality to be the thing that matters here.
942
// This is perhaps debatable.
943
nsIClassInfo* ci = other_wrapper->GetClassInfo();
947
if(NS_SUCCEEDED(ci->GetClassIDNoAlloc(&cid)))
948
*bp = cid.Equals(*mDetails.GetID());
954
/***************************************************************************/
955
// additional utilities...
958
xpc_NewIDObject(JSContext *cx, JSObject* jsobj, const nsID& aID)
960
JSObject *obj = nsnull;
962
char* idString = aID.ToString();
965
nsCOMPtr<nsIJSID> iid =
966
dont_AddRef(NS_STATIC_CAST(nsIJSID*, nsJSID::NewID(idString)));
970
nsXPConnect* xpc = nsXPConnect::GetXPConnect();
973
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
974
nsresult rv = xpc->WrapNative(cx, jsobj,
975
NS_STATIC_CAST(nsISupports*,iid),
977
getter_AddRefs(holder));
978
if(NS_SUCCEEDED(rv) && holder)
980
holder->GetJSObject(&obj);
989
xpc_JSObjectToID(JSContext *cx, JSObject* obj)
995
// NOTE: this call does NOT addref
996
XPCWrappedNative* wrapper =
997
XPCWrappedNative::GetWrappedNativeOfJSObject(cx, obj);
999
(wrapper->HasInterfaceNoQI(NS_GET_IID(nsIJSID)) ||
1000
wrapper->HasInterfaceNoQI(NS_GET_IID(nsIJSIID)) ||
1001
wrapper->HasInterfaceNoQI(NS_GET_IID(nsIJSCID))))
1003
((nsIJSID*)wrapper->GetIdentityObject())->GetId(&id);
1009
xpc_JSObjectIsID(JSContext *cx, JSObject* obj)
1011
NS_ASSERTION(cx && obj, "bad param");
1012
// NOTE: this call does NOT addref
1013
XPCWrappedNative* wrapper =
1014
XPCWrappedNative::GetWrappedNativeOfJSObject(cx, obj);
1016
(wrapper->HasInterfaceNoQI(NS_GET_IID(nsIJSID)) ||
1017
wrapper->HasInterfaceNoQI(NS_GET_IID(nsIJSIID)) ||
1018
wrapper->HasInterfaceNoQI(NS_GET_IID(nsIJSCID)));