sbBaseDeviceController.cpp
Go to the documentation of this file.
1 /* vim: set sw=2 :miv */
2 /*
3 //
4 // BEGIN SONGBIRD GPL
5 //
6 // This file is part of the Songbird web player.
7 //
8 // Copyright(c) 2005-2008 POTI, Inc.
9 // http://songbirdnest.com
10 //
11 // This file may be licensed under the terms of of the
12 // GNU General Public License Version 2 (the "GPL").
13 //
14 // Software distributed under the License is distributed
15 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
16 // express or implied. See the GPL for the specific language
17 // governing rights and limitations.
18 //
19 // You should have received a copy of the GPL along with this
20 // program. If not, go to http://www.gnu.org/licenses/gpl.html
21 // or write to the Free Software Foundation, Inc.,
22 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 //
24 // END SONGBIRD GPL
25 //
26 */
27 
28 #include "sbBaseDeviceController.h"
29 
30 #include <nsAutoLock.h>
31 #include <nsComponentManagerUtils.h>
32 
33 #include <sbDebugUtils.h>
34 
35 template<class T>
36 PLDHashOperator sbBaseDeviceController::EnumerateIntoArray(const nsID& aKey,
37  T* aData,
38  void* aArray)
39 {
40  nsIMutableArray *array = (nsIMutableArray*)aArray;
41  nsresult rv;
42  nsCOMPtr<nsISupports> supports = do_QueryInterface(aData, &rv);
43  NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
44 
45  rv = array->AppendElement(aData, false);
46  NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
47 
48  return PL_DHASH_NEXT;
49 }
50 
51 template<class T>
52 PLDHashOperator sbBaseDeviceController::EnumerateConnectAll(const nsID& aKey,
53  T* aData,
54  void* aArray)
55 {
56  nsIMutableArray *array = (nsIMutableArray*)aArray;
57  nsresult rv;
58  nsCOMPtr<nsISupports> supports = do_QueryInterface(aData, &rv);
59  NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
60 
61  rv = aData->Connect();
62  NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
63 
64  rv = array->AppendElement(aData, false);
65  NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
66 
67  return PL_DHASH_NEXT;
68 }
69 
70 template<class T>
71 PLDHashOperator sbBaseDeviceController::EnumerateDisconnectAll(const nsID& aKey,
72  T* aData,
73  void* aArray)
74 {
75  nsIMutableArray *array = (nsIMutableArray*)aArray;
76  nsresult rv;
77  nsCOMPtr<nsISupports> supports = do_QueryInterface(aData, &rv);
78  NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
79 
80  rv = aData->Disconnect();
81  NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
82 
83  rv = array->AppendElement(aData, false);
84  NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
85 
86  return PL_DHASH_NEXT;
87 }
88 
90 : mMonitor(nsnull) {
91  mMonitor =
92  nsAutoMonitor::NewMonitor("sbBaseDeviceController.mMonitor");
93  NS_ASSERTION(mMonitor, "Failed to create monitor");
94 
95  PRBool SB_UNUSED_IN_RELEASE(succeeded) = mDevices.Init();
96  NS_ASSERTION(succeeded, "Failed to initialize hashtable");
97 }
98 
100  if(mMonitor) {
101  nsAutoMonitor::DestroyMonitor(mMonitor);
102  }
103 }
104 
105 nsresult
107  nsAutoMonitor mon(mMonitor);
108  aID = mControllerID;
109  return NS_OK;
110 }
111 nsresult
113  nsAutoMonitor mon(mMonitor);
114  mControllerID = aID;
115  return NS_OK;
116 }
117 
118 nsresult
120  nsAutoMonitor mon(mMonitor);
121  aName = mControllerName;
122  return NS_OK;
123 }
124 nsresult
126  nsAutoMonitor mon(mMonitor);
127  mControllerName = aName;
128  return NS_OK;
129 }
130 
131 nsresult
133  nsAutoMonitor mon(mMonitor);
134  aID = mMarshallID;
135  return NS_OK;
136 }
137 nsresult
139  nsAutoMonitor mon(mMonitor);
140  mMarshallID = aID;
141  return NS_OK;
142 }
143 
144 nsresult
146  NS_ENSURE_ARG_POINTER(aDevice);
147 
148  nsresult rv;
149  nsID* id;
150  rv = aDevice->GetId(&id);
151  NS_ENSURE_SUCCESS(rv, rv);
152  NS_ENSURE_ARG_POINTER(id);
153 
154  nsAutoMonitor mon(mMonitor);
155 
156  PRBool succeeded = mDevices.Put(*id, aDevice);
157  mon.Exit();
158 
159  NS_Free(id);
160 
161  return succeeded ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
162 }
163 nsresult
165  NS_ENSURE_ARG_POINTER(aDevice);
166 
167  nsresult rv;
168  nsID* id;
169  rv = aDevice->GetId(&id);
170  NS_ENSURE_SUCCESS(rv, rv);
171  NS_ENSURE_ARG_POINTER(id);
172 
173  nsAutoMonitor mon(mMonitor);
174  mDevices.Remove(*id);
175  mon.Exit();
176 
177  NS_Free(id);
178 
179  return NS_OK;
180 }
181 
182 nsresult
184  sbIDevice* *aDevice) {
185  NS_ENSURE_ARG_POINTER(aID);
186  NS_ENSURE_ARG_POINTER(aDevice);
187 
188  nsAutoMonitor mon(mMonitor);
189 
190  PRBool succeeded = mDevices.Get(*aID, aDevice);
191  mon.Exit();
192 
193  return succeeded ? NS_OK : NS_ERROR_NOT_AVAILABLE;
194 }
195 
196 nsresult
198  NS_ENSURE_ARG_POINTER(aDevices);
199 
200  nsresult rv;
201  nsCOMPtr<nsIMutableArray> array =
202  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
203  NS_ENSURE_SUCCESS(rv, rv);
204 
205  nsAutoMonitor mon(mMonitor);
206 
207  PRUint32 count;
208  count = mDevices.EnumerateRead(sbBaseDeviceController::EnumerateIntoArray,
209  array.get());
210 
211  mon.Exit();
212 
213  // we can't trust the count returned from EnumerateRead because that won't
214  // tell us about erroring on the last element
215  rv = array->GetLength(&count);
216  NS_ENSURE_SUCCESS(rv, rv);
217  if (count < mDevices.Count()) {
218  return NS_ERROR_FAILURE;
219  }
220 
221  return CallQueryInterface(array, aDevices);
222 }
223 
224 nsresult
226  PRBool *_retval) {
227  NS_ENSURE_ARG_POINTER(aDevice);
228  NS_ENSURE_ARG_POINTER(_retval);
229 
230  nsID *id = nsnull;
231  nsresult rv = aDevice->GetId(&id);
232  NS_ENSURE_SUCCESS(rv, rv);
233 
234  nsCOMPtr<sbIDevice> device;
235  rv = GetDeviceInternal(id, getter_AddRefs(device));
236  NS_ENSURE_SUCCESS(rv, rv);
237 
238  NS_Free(id);
239  *_retval = (device != nsnull) ? PR_TRUE : PR_FALSE;
240 
241  return NS_OK;
242 }
243 
244 nsresult
246  nsAutoMonitor mon(mMonitor);
247 
248  nsresult rv;
249  nsCOMPtr<nsIMutableArray> array =
250  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
251  NS_ENSURE_SUCCESS(rv, rv);
252 
253  PRUint32 count;
254  count = mDevices.EnumerateRead(sbBaseDeviceController::EnumerateConnectAll,
255  array.get());
256 
257  mon.Exit();
258 
259  // we can't trust the count returned from EnumerateRead because that won't
260  // tell us about erroring on the last element
261  rv = array->GetLength(&count);
262  NS_ENSURE_SUCCESS(rv, rv);
263  if (count < mDevices.Count()) {
264  return NS_ERROR_FAILURE;
265  }
266 
267  return NS_OK;
268 }
269 
270 nsresult
272  nsAutoMonitor mon(mMonitor);
273 
274  nsresult rv;
275  nsCOMPtr<nsIMutableArray> array =
276  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
277  NS_ENSURE_SUCCESS(rv, rv);
278 
279  PRUint32 count;
280  count = mDevices.EnumerateRead(sbBaseDeviceController::EnumerateDisconnectAll,
281  array.get());
282 
283  mon.Exit();
284 
285  // we can't trust the count returned from EnumerateRead because that won't
286  // tell us about erroring on the last element
287  rv = array->GetLength(&count);
288  NS_ENSURE_SUCCESS(rv, rv);
289  if (count < mDevices.Count()) {
290  return NS_ERROR_FAILURE;
291  }
292 
293  return NS_OK;
294 }
295 
296 nsresult
298  NS_ENSURE_ARG_POINTER(aDevice);
299 
300  nsID *id = nsnull;
301  nsresult rv = aDevice->GetId(&id);
302  NS_ENSURE_SUCCESS(rv, rv);
303 
304  aDevice->Disconnect();
305 
306  nsAutoMonitor mon(mMonitor);
307  mDevices.Remove(*id);
308  mon.Exit();
309 
310  NS_Free(id);
311 
312  return NS_OK;
313 }
314 
315 nsresult
317  nsAutoMonitor mon(mMonitor);
318  mDevices.Clear();
319  return NS_OK;
320 }
nsresult GetDeviceInternal(const nsID *aID, sbIDevice **aDevice)
return NS_OK
menuItem id
Definition: FeedWriter.js:971
nsresult ReleaseDeviceInternal(sbIDevice *aDevice)
inArray array
function succeeded(ch, cx, status, data)
nsresult GetMarshallIdInternal(nsID &aID)
nsresult SetMarshallIdInternal(const nsID &aID)
var count
Definition: test_bug7406.js:32
nsresult SetControllerNameInternal(const nsAString &aName)
gWaitForCompletion mDevices
nsresult GetControllerNameInternal(nsAString &aName)
nsresult RemoveDeviceInternal(sbIDevice *aDevice)
nsresult GetDevicesInternal(nsIArray **aDevices)
nsresult AddDeviceInternal(sbIDevice *aDevice)
nsresult GetControllerIdInternal(nsID &aID)
_updateCookies aName
nsresult ControlsDeviceInternal(sbIDevice *aDevice, PRBool *_retval)
nsresult SetControllerIdInternal(const nsID &aID)
#define SB_UNUSED_IN_RELEASE(decl)
Definition: sbDebugUtils.h:55
_updateTextAndScrollDataForFrame aData