Back to index

lightning-sunbird  0.9+nobinonly
nsProxyEvent.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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 mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
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 of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or 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 
00038 #ifndef __nsProxyEvent_h_
00039 #define __nsProxyEvent_h_
00040 
00041 #include "nsCOMPtr.h"
00042 #include "nsAutoPtr.h"
00043 #include "nscore.h"
00044 #include "nsISupports.h"
00045 #include "nsIFactory.h"
00046 
00047 #include "nsIEventQueueService.h"
00048 #include "nsIEventQueue.h"
00049 #include "plevent.h"
00050 #include "prtypes.h"
00051 #include "xptcall.h"
00052 #include "xptinfo.h"
00053     
00054 class nsProxyObjectCallInfo;
00055 
00056 #define PROXY_SYNC    0x0001  // acts just like a function call.
00057 #define PROXY_ASYNC   0x0002  // fire and forget.  This will return immediately and you will lose all return information.
00058 #define PROXY_ALWAYS  0x0004   // ignore check to see if the eventQ is on the same thread as the caller, and alway return a proxied object.
00059 
00060 //#define AUTOPROXIFICATION
00061 
00062 // WARNING about PROXY_ASYNC:  
00063 //
00064 // If the calling thread goes away, any function which accesses the calling stack 
00065 // will blow up.
00066 //
00067 //  example:
00068 //
00069 //     myFoo->bar(&x)
00070 //
00071 //     ... thread goes away ...
00072 //
00073 //    bar(PRInt32 *x)
00074 //    {
00075 //         *x = 0;   <-----  You will blow up here.
00076 //
00077 //   
00078 //  So what gets saved?  
00079 //
00080 //  You can safely pass base types by value.  You can also pass interface pointers.
00081 //  I will make sure that the interface pointers are addrefed while they are being 
00082 //  proxied.  You can also pass string and wstring.  These I will copy and free.
00083 //
00084 //  I do **NOT** copy arrays or strings with size.  If you are using these either
00085 //  change your interface, or contact me about this feature request.
00086 
00087 
00088 
00089 
00090 class nsProxyObject
00091 {
00092 public:
00093     nsProxyObject(nsIEventQueue *destQueue, PRInt32 proxyType, nsISupports *realObject,
00094               nsIEventQueueService* eventQService);
00095     nsProxyObject(nsIEventQueue *destQueue, PRInt32 proxyType, const nsCID &aClass,  
00096               nsISupports *aDelegate,  const nsIID &aIID, nsIEventQueueService* eventQService);
00097 
00098     void AddRef();
00099     void Release();
00100 
00101                         ~nsProxyObject();
00102 
00103     nsresult            Post( PRUint32            methodIndex, 
00104                               nsXPTMethodInfo   * info, 
00105                               nsXPTCMiniVariant * params, 
00106                               nsIInterfaceInfo  * interfaceInfo);
00107     
00108     nsresult            PostAndWait(nsProxyObjectCallInfo *proxyInfo);
00109     nsISupports*        GetRealObject() const { return mRealObject; }
00110     nsIEventQueue*      GetQueue() const { return mDestQueue; }
00111     PRInt32             GetProxyType() const { return mProxyType; }
00112 
00113     friend class nsProxyEventObject;
00114 private:
00115     
00116     nsAutoRefCnt              mRefCnt;
00117 
00118     PRInt32                   mProxyType;
00119     
00120     nsCOMPtr<nsIEventQueue>   mDestQueue;        /* destination queue */
00121     
00122     nsCOMPtr<nsISupports>     mRealObject;       /* the non-proxy object that this event is referring to. 
00123                                                     This is a strong ref. */
00124     nsCOMPtr<nsIEventQueueService> mEventQService;
00125 
00126     nsresult convertMiniVariantToVariant(nsXPTMethodInfo   * methodInfo, 
00127                                          nsXPTCMiniVariant * params, 
00128                                          nsXPTCVariant     **fullParam, 
00129                                          uint8 *paramCount);
00130 
00131 };
00132 
00133 
00134 class nsProxyObjectCallInfo
00135 {
00136 public:
00137     
00138     nsProxyObjectCallInfo(nsProxyObject* owner,
00139                           nsXPTMethodInfo *methodInfo,
00140                           PRUint32 methodIndex, 
00141                           nsXPTCVariant* parameterList, 
00142                           PRUint32 parameterCount, 
00143                           PLEvent *event);
00144 
00145     ~nsProxyObjectCallInfo();
00146 
00147     PRUint32            GetMethodIndex() const { return mMethodIndex; }
00148     nsXPTCVariant*      GetParameterList() const { return mParameterList; }
00149     PRUint32            GetParameterCount() const { return mParameterCount; }
00150     PLEvent*            GetPLEvent() const { return mEvent; }
00151     nsresult            GetResult() const { return mResult; }
00152     nsProxyObject*      GetProxyObject() const { return mOwner; }
00153     
00154     PRBool              GetCompleted();
00155     void                SetCompleted();
00156     void                PostCompleted();
00157 
00158     void                SetResult(nsresult rv) {mResult = rv; }
00159     
00160     nsIEventQueue*      GetCallersQueue();
00161     void                SetCallersQueue(nsIEventQueue* queue);
00162 
00163 private:
00164     
00165     nsresult         mResult;                    /* this is the return result of the called function */
00166     nsXPTMethodInfo *mMethodInfo;
00167     PRUint32         mMethodIndex;               /* which method to be called? */
00168     nsXPTCVariant   *mParameterList;             /* marshalled in parameter buffer */
00169     PRUint32         mParameterCount;            /* number of params */
00170     PLEvent         *mEvent;                     /* the current plevent */       
00171     PRInt32          mCompleted;                 /* is true when the method has been called. */
00172        
00173     nsCOMPtr<nsIEventQueue>  mCallersEventQ;     /* this is the eventQ that we must post a message back to 
00174                                                     when we are done invoking the method (only PROXY_SYNC). 
00175                                                   */
00176 
00177     nsRefPtr<nsProxyObject>   mOwner;            /* this is the strong referenced nsProxyObject */
00178    
00179     void RefCountInInterfacePointers(PRBool addRef);
00180     void CopyStrings(PRBool copy);
00181 
00182 };
00183 
00184 
00185 #endif