Bug 452317 - FeedConverter.js: QueryInterface should throw NS_ERROR_NO_INTERFACE...
[wine-gecko.git] / xpcom / proxy / src / nsProxyEventPrivate.h
blob9a2c63739ed37d564746dfb00e21f4b229892c72
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsProxyEventPrivate_h__
40 #define nsProxyEventPrivate_h__
42 #include "nsISupports.h"
43 #include "nsIFactory.h"
44 #include "nsIEventTarget.h"
45 #include "nsIInterfaceInfo.h"
46 #include "nsIProxyObjectManager.h"
48 #include "nsXPTCUtils.h"
50 #include "nsAutoPtr.h"
51 #include "nsCOMPtr.h"
52 #include "nsThreadUtils.h"
54 #include "nsClassHashtable.h"
55 #include "nsHashtable.h"
57 #include "prmon.h"
58 #include "prlog.h"
60 class nsProxyEventObject;
62 /**
63 * To make types clearer, we distinguish between a canonical nsISupports* and
64 * a proxied interface pointer which represents an arbitrary interface known
65 * at runtime.
67 typedef nsISupports nsISomeInterface;
69 #define NS_PROXYOBJECT_CLASS_IID \
70 { 0xeea90d45, 0xb059, 0x11d2, \
71 { 0x91, 0x5e, 0xc1, 0x2b, 0x69, 0x6c, 0x93, 0x33 } }
73 // This IID is used to filter runnables during synchronous event handling.
74 // The returned pointer is type nsProxyObjectCallInfo
76 #define NS_PROXYEVENT_FILTER_IID \
77 { 0xec373590, 0x9164, 0x11d3, \
78 {0x8c, 0x73, 0x00, 0x00, 0x64, 0x65, 0x73, 0x74} }
80 /**
81 * An object representing an IID and its associated interfaceinfo. Instances
82 * of this class are obtained via nsProxyObjectManager::GetClass.
84 class nsProxyEventClass
86 public:
87 nsIInterfaceInfo* GetInterfaceInfo() const {return mInfo;}
88 const nsIID& GetProxiedIID() const {return mIID; }
90 nsProxyEventClass(REFNSIID aIID, nsIInterfaceInfo* aInfo);
91 ~nsProxyEventClass();
93 nsIID mIID;
94 nsCOMPtr<nsIInterfaceInfo> mInfo;
95 uint32* mDescriptors;
98 /**
99 * A class which provides the XPCOM identity for a proxied object.
100 * Instances of this class are obtained from the POM, and are uniquely
101 * hashed on a proxytype/eventtarget/realobject key.
103 class nsProxyObject : public nsISupports
105 public:
106 NS_DECL_ISUPPORTS
108 NS_DECLARE_STATIC_IID_ACCESSOR(NS_PROXYOBJECT_CLASS_IID)
110 nsProxyObject(nsIEventTarget *destQueue, PRInt32 proxyType,
111 nsISupports *realObject);
113 nsISupports* GetRealObject() const { return mRealObject; }
114 nsIEventTarget* GetTarget() const { return mTarget; }
115 PRInt32 GetProxyType() const { return mProxyType; }
117 // these are the equivalents of AddRef/Release, but must be called
118 // while holding the global POM lock
119 nsrefcnt LockedAddRef();
120 nsrefcnt LockedRelease();
122 // LockedFind should be called holding the POM lock. It will
123 // temporarily unlock the lock during execution.
124 nsresult LockedFind(REFNSIID iid, void **aResult);
126 void LockedRemove(nsProxyEventObject* aObject);
128 friend class nsProxyObjectManager;
129 private:
130 ~nsProxyObject();
132 PRInt32 mProxyType;
133 nsCOMPtr<nsIEventTarget> mTarget; /* event target */
134 nsCOMPtr<nsISupports> mRealObject; /* the non-proxy object that this object is proxying
135 This is a strong ref. */
136 nsProxyEventObject *mFirst;
138 class nsProxyObjectDestructorEvent : public nsRunnable
140 nsProxyObjectDestructorEvent(nsProxyObject *doomed) :
141 mDoomed(doomed)
144 NS_DECL_NSIRUNNABLE
146 friend class nsProxyObject;
147 private:
148 nsProxyObject *mDoomed;
151 friend class nsProxyObjectDestructorEvent;
154 NS_DEFINE_STATIC_IID_ACCESSOR(nsProxyObject, NS_PROXYOBJECT_CLASS_IID)
157 * Object representing a single interface implemented on a proxied object.
158 * This object is maintained in a singly-linked list from the associated
159 * "parent" nsProxyObject.
161 class nsProxyEventObject : protected nsAutoXPTCStub
163 public:
165 NS_DECL_ISUPPORTS
167 // call this method and return result
168 NS_IMETHOD CallMethod(PRUint16 methodIndex,
169 const XPTMethodDescriptor* info,
170 nsXPTCMiniVariant* params);
172 nsProxyEventClass* GetClass() const { return mClass; }
173 nsISomeInterface* GetProxiedInterface() const { return mRealInterface; }
174 nsIEventTarget* GetTarget() const { return mProxyObject->GetTarget(); }
175 PRInt32 GetProxyType() const { return mProxyObject->GetProxyType(); }
177 nsresult convertMiniVariantToVariant(const XPTMethodDescriptor *methodInfo,
178 nsXPTCMiniVariant *params,
179 nsXPTCVariant **fullParam,
180 uint8 *outParamCount);
182 nsProxyEventObject(nsProxyObject *aParent,
183 nsProxyEventClass *aClass,
184 already_AddRefed<nsISomeInterface> aRealInterface,
185 nsresult *rv);
187 // AddRef, but you must be holding the global POM lock
188 nsrefcnt LockedAddRef();
189 friend class nsProxyObject;
191 private:
192 ~nsProxyEventObject();
194 // Member ordering is important: See note in the destructor.
195 nsProxyEventClass *mClass;
196 nsCOMPtr<nsProxyObject> mProxyObject;
197 nsCOMPtr<nsISomeInterface> mRealInterface;
199 // Weak reference, maintained by the parent nsProxyObject
200 nsProxyEventObject *mNext;
203 #define NS_PROXYEVENT_IID \
204 { /* 9a24dc5e-2b42-4a5a-aeca-37b8c8fd8ccd */ \
205 0x9a24dc5e, \
206 0x2b42, \
207 0x4a5a, \
208 {0xae, 0xca, 0x37, 0xb8, 0xc8, 0xfd, 0x8c, 0xcd} \
212 * A class representing a particular proxied method call.
214 class nsProxyObjectCallInfo : public nsRunnable
216 public:
218 NS_DECL_NSIRUNNABLE
220 NS_IMETHOD QueryInterface(REFNSIID aIID, void **aResult);
222 NS_DECLARE_STATIC_IID_ACCESSOR(NS_PROXYEVENT_IID)
224 nsProxyObjectCallInfo(nsProxyEventObject* owner,
225 const XPTMethodDescriptor *methodInfo,
226 PRUint32 methodIndex,
227 nsXPTCVariant* parameterList,
228 PRUint32 parameterCount);
230 ~nsProxyObjectCallInfo();
232 PRUint32 GetMethodIndex() const { return mMethodIndex; }
233 nsXPTCVariant* GetParameterList() const { return mParameterList; }
234 PRUint32 GetParameterCount() const { return mParameterCount; }
235 nsresult GetResult() const { return mResult; }
237 PRBool GetCompleted();
238 void SetCompleted();
239 void PostCompleted();
241 void SetResult(nsresult rv) { mResult = rv; }
243 nsIEventTarget* GetCallersTarget();
244 void SetCallersTarget(nsIEventTarget* target);
245 PRBool IsSync() const
247 return !!(mOwner->GetProxyType() & NS_PROXY_SYNC);
250 private:
252 nsresult mResult; /* this is the return result of the called function */
253 const XPTMethodDescriptor *mMethodInfo;
254 PRUint32 mMethodIndex; /* which method to be called? */
255 nsXPTCVariant *mParameterList; /* marshalled in parameter buffer */
256 PRUint32 mParameterCount; /* number of params */
257 PRInt32 mCompleted; /* is true when the method has been called. */
259 nsCOMPtr<nsIEventTarget> mCallersTarget; /* this is the dispatch target that we must post a message back to
260 when we are done invoking the method (only NS_PROXY_SYNC). */
262 nsRefPtr<nsProxyEventObject> mOwner; /* this is the strong referenced nsProxyObject */
264 void RefCountInInterfacePointers(PRBool addRef);
265 void CopyStrings(PRBool copy);
268 NS_DEFINE_STATIC_IID_ACCESSOR(nsProxyObjectCallInfo, NS_PROXYEVENT_IID)
270 ////////////////////////////////////////////////////////////////////////////////
271 // nsProxyObjectManager
272 ////////////////////////////////////////////////////////////////////////////////
274 class nsProxyObjectManager: public nsIProxyObjectManager
276 public:
277 NS_DECL_ISUPPORTS
278 NS_DECL_NSIPROXYOBJECTMANAGER
280 static NS_METHOD Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr);
282 nsProxyObjectManager();
284 static nsProxyObjectManager *GetInstance();
285 static PRBool IsManagerShutdown();
287 static void Shutdown();
289 nsresult GetClass(REFNSIID aIID, nsProxyEventClass **aResult);
291 void LockedRemove(nsProxyObject* aProxy);
293 PRLock* GetLock() const { return mProxyCreationLock; }
295 #ifdef PR_LOGGING
296 static PRLogModuleInfo *sLog;
297 #endif
299 private:
300 ~nsProxyObjectManager();
302 static nsProxyObjectManager* mInstance;
303 nsHashtable mProxyObjectMap;
304 nsClassHashtable<nsIDHashKey, nsProxyEventClass> mProxyClassMap;
305 PRLock *mProxyCreationLock;
308 #define NS_XPCOMPROXY_CLASSNAME "nsProxyObjectManager"
309 #define NS_PROXYEVENT_MANAGER_CID \
310 { 0xeea90d41, \
311 0xb059, \
312 0x11d2, \
313 {0x91, 0x5e, 0xc1, 0x2b, 0x69, 0x6c, 0x93, 0x33} \
316 #define PROXY_LOG(args) PR_LOG(nsProxyObjectManager::sLog, PR_LOG_DEBUG, args)
318 #endif // nsProxyEventPrivate_h__