sbProxiedComponentManager.cpp
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 
26 
27 #include <nsAutoPtr.h>
28 
30 
31 NS_IMETHODIMP
33 {
34  NS_ASSERTION(NS_IsMainThread(), "Not main thread!");
35 
36  nsCOMPtr<nsIProxyObjectManager> pom =
37  do_GetService(NS_XPCOMPROXY_CONTRACTID, &mResult);
38  if (NS_FAILED(mResult)) {
39  return NS_OK;
40  }
41 
42  nsCOMPtr<nsISupports> supports;
43  if (mIsService) {
44  if (mContractID) {
45  supports = do_GetService(mContractID, &mResult);
46  }
47  else {
48  supports = do_GetService(mCID, &mResult);
49  }
50  }
51  else {
52  if (mContractID) {
53  supports = do_CreateInstance(mContractID, &mResult);
54  }
55  else {
56  supports = do_CreateInstance(mCID, &mResult);
57  }
58  }
59 
60  if (NS_FAILED(mResult)) {
61  return NS_OK;
62  }
63 
64  mResult = pom->GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
65  mIID,
66  supports,
67  NS_PROXY_SYNC | NS_PROXY_ALWAYS,
68  getter_AddRefs(mSupports));
69 
70  return NS_OK;
71 }
72 
73 nsresult
74 sbCreateProxiedComponent::operator()(const nsIID& aIID, void** aInstancePtr) const
75 {
76  nsRefPtr<sbProxiedComponentManagerRunnable> runnable =
77  new sbProxiedComponentManagerRunnable(mIsService, mCID, mContractID, aIID);
78  if (!runnable) {
79  *aInstancePtr = 0;
80  if (mErrorPtr)
81  *mErrorPtr = NS_ERROR_OUT_OF_MEMORY;
82  return NS_ERROR_OUT_OF_MEMORY;
83  }
84 
85  nsresult rv = NS_DispatchToMainThread(runnable, NS_DISPATCH_SYNC);
86  if (NS_FAILED(rv)) {
87  *aInstancePtr = 0;
88  if (mErrorPtr)
89  *mErrorPtr = rv;
90  return rv;
91  }
92 
93  if (NS_FAILED(runnable->mResult)) {
94  *aInstancePtr = 0;
95  if (mErrorPtr)
96  *mErrorPtr = runnable->mResult;
97  return runnable->mResult;
98  }
99 
100  runnable->mSupports.forget(reinterpret_cast<nsISupports**>(aInstancePtr));
101 
102  if (mErrorPtr)
103  *mErrorPtr = runnable->mResult;
104 
105  return NS_OK;
106 }
107 
108 nsresult
110  void** aInstancePtr) const
111 {
112  nsresult rv;
113 
114  // Get the main thread QI'd object.
115  if (NS_IsMainThread()) {
116  // Already on main thread. Just QI.
117  rv = mSupports->QueryInterface(aIID, aInstancePtr);
118  }
119  else {
120  // Not on main thread. Get a proxy.
121  nsCOMPtr<nsIThread> mainThread;
122  rv = NS_GetMainThread(getter_AddRefs(mainThread));
123  if (NS_SUCCEEDED(rv)) {
124  rv = do_GetProxyForObject(mainThread,
125  aIID,
126  mSupports,
127  NS_PROXY_SYNC | NS_PROXY_ALWAYS,
128  aInstancePtr);
129  }
130  }
131 
132  // Return null on failure.
133  if (NS_FAILED(rv))
134  *aInstancePtr = nsnull;
135 
136  // Return result.
137  if (mResult)
138  *mResult = rv;
139 
140  return rv;
141 }
142 
return NS_OK
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
virtual nsresult NS_FASTCALL operator()(const nsIID &, void **) const
virtual nsresult NS_FASTCALL operator()(const nsIID &, void **) const
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility