sbProxiedComponentManager.h
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 #ifndef __SB_PROXIEDCOMPONENTMANAGER_H__
26 #define __SB_PROXIEDCOMPONENTMANAGER_H__
27 
28 #include <nsIRunnable.h>
29 #include <nsCOMPtr.h>
30 #include <nsComponentManagerUtils.h>
31 #include <nsServiceManagerUtils.h>
32 #include <nsThreadUtils.h>
33 #include <nsIProxyObjectManager.h>
34 #include <nsXPCOMCIDInternal.h>
35 #include <nsProxyRelease.h>
36 
37 class sbProxiedComponentManagerRunnable : public nsIRunnable
38 {
39 public:
41  NS_DECL_NSIRUNNABLE
42 
44  const nsCID& aCID,
45  const char* aContractID,
46  const nsIID& aIID) :
47  mIsService(aIsService),
48  mCID(aCID),
49  mContractID(aContractID),
50  mIID(aIID) {}
51 
52  PRBool mIsService;
53  const nsCID& mCID;
54  const char* mContractID;
55  const nsIID& mIID;
56  nsCOMPtr<nsISupports> mSupports;
57  nsresult mResult;
58 };
59 
60 class NS_COM_GLUE sbCreateProxiedComponent : public nsCOMPtr_helper
61 {
62 public:
63  sbCreateProxiedComponent(const nsCID& aCID,
64  PRBool aIsService,
65  nsresult* aErrorPtr)
66  : mCID(aCID),
67  mContractID(nsnull),
68  mIsService(aIsService),
69  mErrorPtr(aErrorPtr)
70  {
71  }
72 
73  sbCreateProxiedComponent(const char* aContractID,
74  PRBool aIsService,
75  nsresult* aErrorPtr)
76  : mCID(NS_GET_IID(nsISupports)),
77  mContractID(aContractID),
78  mIsService(aIsService),
79  mErrorPtr(aErrorPtr)
80  {
81  }
82 
83  virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const;
84 
85 private:
86  const nsCID& mCID;
87  const char* mContractID;
88  PRBool mIsService;
89  nsresult* mErrorPtr;
90 };
91 
92 inline
94 do_ProxiedCreateInstance(const nsCID& aCID, nsresult* error = 0)
95 {
96  return sbCreateProxiedComponent(aCID, PR_FALSE, error);
97 }
98 
99 inline
101 do_ProxiedGetService(const nsCID& aCID, nsresult* error = 0)
102 {
103  return sbCreateProxiedComponent(aCID, PR_TRUE, error);
104 }
105 
106 inline
108 do_ProxiedCreateInstance(const char* aContractID, nsresult* error = 0)
109 {
110  return sbCreateProxiedComponent(aContractID, PR_FALSE, error);
111 }
112 
113 inline
115 do_ProxiedGetService(const char* aContractID, nsresult* error = 0)
116 {
117  return sbCreateProxiedComponent(aContractID, PR_TRUE, error);
118 }
119 
120 /* Get a proxy using a proxied nsIProxyObjectManager (acquired with
121  do_ProxiedGetService()).
122  This is useful because though USING a proxy may spin the event loop,
123  acquiring one via this function does not (assuming you've acquired the
124  proxy object manager earlier, where spinning the event loop is acceptable).
125  */
126 inline nsresult
127 do_GetProxyForObjectWithManager(nsIProxyObjectManager *aProxyObjMgr,
128  nsIEventTarget *aTarget,
129  REFNSIID aIID,
130  nsISupports* aObj,
131  PRInt32 aProxyType,
132  void** aProxyObject)
133 {
134  nsresult rv;
135  /* The proxied aProxyObjMgr can only actually proxy real target objects, so
136  the special magic values available for 'target' must be resolved here.
137  */
138  nsCOMPtr<nsIThread> thread;
139  nsCOMPtr<nsIEventTarget> target;
140  if (aTarget == NS_PROXY_TO_CURRENT_THREAD) {
141  rv = NS_GetCurrentThread(getter_AddRefs(thread));
142  NS_ENSURE_SUCCESS(rv, rv);
143 
144  target = thread;
145  }
146  else if (aTarget == NS_PROXY_TO_MAIN_THREAD) {
147  rv = NS_GetMainThread(getter_AddRefs(thread));
148  NS_ENSURE_SUCCESS(rv, rv);
149 
150  target = thread;
151  }
152  else {
153  target = aTarget;
154  }
155 
156  rv = aProxyObjMgr->GetProxyForObject(target,
157  aIID,
158  aObj,
159  aProxyType,
160  aProxyObject);
161  return rv;
162 }
163 
164 inline nsresult
165 do_GetProxyForObject(nsIEventTarget *aTarget,
166  REFNSIID aIID,
167  nsISupports* aObj,
168  PRInt32 aProxyType,
169  void** aProxyObject)
170 {
171  nsresult rv;
172  nsCOMPtr<nsIProxyObjectManager> proxyObjMgr =
173  do_ProxiedGetService(NS_XPCOMPROXY_CONTRACTID, &rv);
174  NS_ENSURE_SUCCESS(rv, rv);
175 
176  rv = do_GetProxyForObjectWithManager(proxyObjMgr,
177  aTarget,
178  aIID,
179  aObj,
180  aProxyType,
181  aProxyObject);
182  NS_ENSURE_SUCCESS(rv, rv);
183 
184  return NS_OK;
185 }
186 
187 template <class T>
188 inline nsresult
189 do_GetProxyForObject(nsIEventTarget * aTarget,
190  T * aObj,
191  PRInt32 aProxyType,
192  void ** aProxyObject)
193 {
194  return do_GetProxyForObject(aTarget,
195  NS_GET_TEMPLATE_IID(T),
196  aObj,
197  aProxyType,
198  aProxyObject);
199 }
200 
201 
206 class NS_COM_GLUE sbMainThreadQueryInterface : public nsCOMPtr_helper
207 {
208 public:
210  nsresult* aResult) :
211  mSupports(aSupports),
212  mResult(aResult)
213  {
214  }
215 
216  virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const;
217 
218 private:
219  nsISupports* mSupports;
220  nsresult* mResult;
221 };
222 
223 
245  nsresult* aResult = nsnull)
246 {
247  return sbMainThreadQueryInterface(aSupports, aResult);
248 }
249 
250 
251 template <class T> inline void
252 do_MainThreadQueryInterface(already_AddRefed<T>& aSupports,
253  nsresult* aResult = nsnull)
254 {
255  // This signature exists solely to _stop_ you from doing the bad thing.
256  // Saying |do_MainThreadQueryInterface()| on a pointer that is not otherwise
257  // owned by someone else is an automatic leak. See
258  // <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
259 }
260 
261 
262 #endif /* __SB_PROXIEDCOMPONENTMANAGER_H__ */
263 
sbCreateProxiedComponent(const char *aContractID, PRBool aIsService, nsresult *aErrorPtr)
return NS_OK
sbMainThreadQueryInterface(nsISupports *aSupports, nsresult *aResult)
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
const sbCreateProxiedComponent do_ProxiedCreateInstance(const nsCID &aCID, nsresult *error=0)
const sbCreateProxiedComponent do_ProxiedGetService(const nsCID &aCID, nsresult *error=0)
sbMainThreadQueryInterface do_MainThreadQueryInterface(nsISupports *aSupports, nsresult *aResult=nsnull)
NS_DECL_ISUPPORTS NS_DECL_NSIRUNNABLE sbProxiedComponentManagerRunnable(PRBool aIsService, const nsCID &aCID, const char *aContractID, const nsIID &aIID)
nsresult do_GetProxyForObjectWithManager(nsIProxyObjectManager *aProxyObjMgr, nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
sbCreateProxiedComponent(const nsCID &aCID, PRBool aIsService, nsresult *aErrorPtr)