Back to index

lightning-sunbird  0.9+nobinonly
XPCDispParamPropJSClass.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the IDispatch implementation for XPConnect.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * David Bradley.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00043 #include "xpcprivate.h"
00044 
00052 inline
00053 XPCDispParamPropJSClass* GetParamProp(JSContext* cx, JSObject* obj)
00054 {
00055     return NS_REINTERPRET_CAST(XPCDispParamPropJSClass*, 
00056                                JS_GetPrivate(cx, obj));
00057 }
00058 
00070 JS_STATIC_DLL_CALLBACK(JSBool)
00071 XPC_PP_GetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
00072 {
00073     XPCDispParamPropJSClass* paramProp = GetParamProp(cx, obj);
00074     JSObject* originalObj = paramProp->GetWrapper()->GetFlatJSObject();
00075     XPCCallContext ccx(JS_CALLER, cx, originalObj, nsnull, id, 
00076                        paramProp->GetParams()->GetParamCount(), nsnull, vp);
00077     return paramProp->Invoke(ccx, XPCDispObject::CALL_GETTER, vp);
00078 }
00079 
00091 JS_STATIC_DLL_CALLBACK(JSBool)
00092 XPC_PP_SetProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
00093 {
00094     XPCDispParamPropJSClass* paramProp = GetParamProp(cx, obj);
00095     JSObject* originalObj = paramProp->GetWrapper()->GetFlatJSObject();
00096     XPCCallContext ccx(JS_CALLER, cx, originalObj, nsnull, id, 
00097                        paramProp->GetParams()->GetParamCount(), nsnull, vp);
00098     _variant_t var;
00099     uintN err;
00100     if(!XPCDispConvert::JSToCOM(ccx, *vp, var, err))
00101         return JS_FALSE;
00102     XPCDispParams* params = paramProp->GetParams();
00103     params->SetNamedPropID();
00104     // params will own var
00105     params->InsertParam(var);
00106     // Save off the value passed in so we can return it.
00107     // Invoke resets the value to what ever IDispatch returns, in this
00108     // case empty, converted to undefined
00109     jsval retJSVal = *vp;
00110     AUTO_MARK_JSVAL(ccx, retJSVal);
00111     if (paramProp->Invoke(ccx, XPCDispObject::CALL_SETTER, vp))
00112     {
00113         *vp = retJSVal;
00114         return JS_TRUE;
00115     }
00116     return JS_FALSE;
00117 }
00118 
00130 JS_STATIC_DLL_CALLBACK(void)
00131 XPC_PP_Finalize(JSContext *cx, JSObject *obj)
00132 {
00133     delete GetParamProp(cx, obj);
00134 }
00135 
00143 JS_STATIC_DLL_CALLBACK(uint32)
00144 XPC_PP_Mark(JSContext *cx, JSObject *obj, void *arg)
00145 {
00146     XPCDispParamPropJSClass* paramProp = GetParamProp(cx, obj);
00147     if(paramProp)
00148     {
00149         XPCWrappedNative* wrapper = paramProp->GetWrapper();
00150         if(wrapper && wrapper->IsValid())
00151             xpc_MarkForValidWrapper(cx, wrapper, arg);
00152     }
00153     return 0;
00154 }
00155 
00160 static JSClass ParamPropClass = {
00161     "XPCDispParamPropJSCass",   // Name 
00162     JSCLASS_HAS_PRIVATE,        // flags  
00163 
00164     /* Mandatory non-null function pointer members. */
00165     JS_PropertyStub,            // addProperty
00166     JS_PropertyStub,            // delProperty
00167     XPC_PP_GetProperty,         // getProperty
00168     XPC_PP_SetProperty,         // setProperty
00169     JS_EnumerateStub,           // enumerate
00170     JS_ResolveStub,             // resolve
00171     JS_ConvertStub,             // convert
00172     XPC_PP_Finalize,            // finalize
00173 
00174     /* Optionally non-null members start here. */
00175     nsnull,                     // getObjectOps;
00176     nsnull,                     // checkAccess;
00177     nsnull,                     // call;
00178     nsnull,                     // construct;
00179     nsnull,                     // xdrObject;
00180     nsnull,                     // hasInstance;
00181     XPC_PP_Mark,                // mark;
00182     nsnull                      // spare;
00183 };
00184 
00185 // static
00186 JSBool XPCDispParamPropJSClass::NewInstance(XPCCallContext& ccx,
00187                                              XPCWrappedNative* wrapper, 
00188                                              PRUint32 dispID, 
00189                                              XPCDispParams* dispParams, 
00190                                              jsval* paramPropObj)
00191 {
00192     XPCDispParamPropJSClass* pDispParam =
00193         new XPCDispParamPropJSClass(wrapper, ccx.GetTearOff()->GetNative(), 
00194                                     dispID, dispParams);
00195     if(!pDispParam)
00196         return JS_FALSE;
00197     JSObject * obj = JS_NewObject(ccx, &ParamPropClass, nsnull, nsnull);
00198     if(!obj)
00199         return JS_FALSE;
00200     if(!JS_SetPrivate(ccx, obj, pDispParam))
00201         return JS_FALSE;
00202     *paramPropObj = OBJECT_TO_JSVAL(obj);
00203     return JS_TRUE;
00204 }
00205 
00206 XPCDispParamPropJSClass::XPCDispParamPropJSClass(XPCWrappedNative* wrapper, 
00207                                                  nsISupports * dispObj, 
00208                                                  PRUint32 dispID,
00209                                                  XPCDispParams* dispParams) :
00210     mWrapper(wrapper),
00211     mDispID(dispID),
00212     mDispParams(dispParams),
00213     mDispObj(nsnull)
00214 {
00215     NS_ADDREF(mWrapper);
00216     dispObj->QueryInterface(NSID_IDISPATCH, 
00217                                               NS_REINTERPRET_CAST(void**,
00218                                               &mDispObj));
00219 }
00220 
00221 XPCDispParamPropJSClass::~XPCDispParamPropJSClass()
00222 {
00223     delete mDispParams;
00224     // release our members
00225     NS_IF_RELEASE(mWrapper);
00226     NS_IF_RELEASE(mDispObj);
00227 }