29 #include "sbICDDevice.h"
30 #include "sbICDDeviceService.h"
32 #include <sbIDevice.h>
33 #include <sbIDeviceController.h>
34 #include <sbIDeviceRegistrar.h>
35 #include <sbIDeviceControllerRegistrar.h>
36 #include <sbIDeviceManager.h>
37 #include <sbIDeviceEvent.h>
38 #include <sbIDeviceEventTarget.h>
42 #include <nsComponentManagerUtils.h>
43 #include <nsIClassInfoImpl.h>
44 #include <nsIProgrammingLanguage.h>
45 #include <nsIPropertyBag2.h>
46 #include <nsISupportsPrimitives.h>
47 #include <nsIThreadManager.h>
48 #include <nsIThreadPool.h>
49 #include <nsServiceManagerUtils.h>
69 sbCDDeviceMarshall::sbCDDeviceMarshall()
71 , mKnownDevicesLock(nsAutoMonitor::NewMonitor("sbCDDeviceMarshall::mKnownDevicesLock"))
73 mKnownDevices.Init(8);
78 nsAutoMonitor mon(mKnownDevicesLock);
81 nsAutoMonitor::DestroyMonitor(mKnownDevicesLock);
88 nsCOMPtr<sbIDeviceManager2> deviceMgr =
89 do_GetService(
"@songbirdnest.com/Songbird/DeviceManager;2", &rv);
90 NS_ENSURE_SUCCESS(rv, rv);
92 mCDDeviceService = nsnull;
93 PRInt32 selectedWeight = -1;
96 nsCOMPtr<nsICategoryManager> catman =
97 do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
98 NS_ENSURE_SUCCESS(rv, rv);
100 nsCOMPtr<nsISimpleEnumerator> categoryEnum;
101 rv = catman->EnumerateCategory(
"cdrip-engine", getter_AddRefs(categoryEnum));
102 NS_ENSURE_SUCCESS(rv, rv);
104 while (NS_SUCCEEDED(categoryEnum->HasMoreElements(&hasMore)) && hasMore) {
105 nsCOMPtr<nsISupports> ptr;
106 if (NS_SUCCEEDED(categoryEnum->GetNext(getter_AddRefs(ptr))) && ptr) {
107 nsCOMPtr<nsISupportsCString> stringValue(do_QueryInterface(ptr));
108 nsCString factoryName;
109 if (stringValue && NS_SUCCEEDED(stringValue->GetData(factoryName))) {
110 nsCString contractId;
111 rv = catman->GetCategoryEntry(
"cdrip-engine", factoryName.get(),
112 getter_Copies(contractId));
113 NS_ENSURE_SUCCESS(rv, rv);
116 nsCOMPtr<sbICDDeviceService> cdDevSvc =
117 do_GetService(contractId.get(), &rv);
118 NS_ENSURE_SUCCESS(rv, rv);
120 PRInt32 currentWeight;
121 rv = cdDevSvc->GetWeight(¤tWeight);
122 NS_ENSURE_SUCCESS(rv, rv);
127 if (selectedWeight == -1 || currentWeight >= selectedWeight) {
128 mCDDeviceService = cdDevSvc;
129 selectedWeight = currentWeight;
141 NS_ENSURE_ARG_POINTER(aCDDevice);
146 rv = aCDDevice->GetName(deviceName);
147 NS_ENSURE_SUCCESS(rv, rv);
151 PRBool hasDevice = PR_FALSE;
153 if (NS_FAILED(rv) || hasDevice) {
158 nsCOMPtr<nsIWritablePropertyBag> propBag =
159 do_CreateInstance(
"@mozilla.org/hash-property-bag;1", &rv);
160 NS_ENSURE_SUCCESS(rv, rv);
162 nsCOMPtr<nsIWritableVariant> deviceType =
163 do_CreateInstance(
"@songbirdnest.com/Songbird/Variant;1", &rv);
164 NS_ENSURE_SUCCESS(rv, rv);
166 rv = deviceType->SetAsAString(NS_LITERAL_STRING(
"CD"));
167 NS_ENSURE_SUCCESS(rv, rv);
169 rv = propBag->SetProperty(NS_LITERAL_STRING(
"DeviceType"),
171 NS_ENSURE_SUCCESS(rv, rv);
174 NS_ENSURE_TRUE(controller, NS_ERROR_UNEXPECTED);
177 nsCOMPtr<nsIWritableVariant> deviceVar =
178 do_CreateInstance(
"@songbirdnest.com/Songbird/Variant;1", &rv);
179 NS_ENSURE_SUCCESS(rv, rv);
181 rv = deviceVar->SetAsISupports(aCDDevice);
182 NS_ENSURE_SUCCESS(rv, rv);
184 rv = propBag->SetProperty(NS_LITERAL_STRING(
"sbICDDevice"), deviceVar);
185 NS_ENSURE_SUCCESS(rv, rv);
188 nsCOMPtr<sbIDevice> sbDevice;
189 rv = controller->CreateDevice(propBag, getter_AddRefs(sbDevice));
190 NS_ENSURE_SUCCESS(rv, rv);
193 PRBool hasDisc = PR_FALSE;
194 rv = aCDDevice->GetIsDiscInserted(&hasDisc);
195 if (NS_FAILED(rv) || !hasDisc) {
201 rv = aCDDevice->GetDiscType(&discType);
206 nsCOMPtr<sbIDeviceManager2> deviceManager =
207 do_GetService(
"@songbirdnest.com/Songbird/DeviceManager;2", &rv);
208 NS_ENSURE_SUCCESS(rv, rv);
210 nsCOMPtr<sbIDeviceRegistrar> deviceRegistrar =
211 do_QueryInterface(deviceManager, &rv);
212 NS_ENSURE_SUCCESS(rv, rv);
215 rv = deviceRegistrar->RegisterDevice(sbDevice);
216 NS_ASSERTION(NS_SUCCEEDED(rv),
"Failed to register device!");
221 static_cast<sbIDeviceMarshall *>(
this));
224 nsAutoMonitor mon(mKnownDevicesLock);
225 mKnownDevices.Put(deviceName, sbDevice);
236 nsCOMPtr<nsIPropertyBag2> parameters;
237 nsCOMPtr<nsIVariant> var;
238 nsCOMPtr<nsISupports> supports;
239 rv = aDevice->GetParameters(getter_AddRefs(parameters));
240 NS_ENSURE_SUCCESS(rv, rv);
241 rv = parameters->GetProperty(NS_LITERAL_STRING(
"sbICDDevice"),
242 getter_AddRefs(var));
243 NS_ENSURE_SUCCESS(rv, rv);
244 rv = var->GetAsISupports(getter_AddRefs(supports));
245 NS_ENSURE_SUCCESS(rv, rv);
246 nsCOMPtr<sbICDDevice> cdDevice = do_QueryInterface(supports, &rv);
247 NS_ENSURE_SUCCESS(rv, rv);
248 rv = cdDevice->GetName(deviceName);
249 NS_ENSURE_SUCCESS(rv, rv);
254 NS_ENSURE_SUCCESS(rv, rv);
260 nsAutoMonitor mon(mKnownDevicesLock);
261 mKnownDevices.Remove(deviceName);
264 nsCOMPtr<sbIDeviceRegistrar> deviceRegistrar =
265 do_GetService(
"@songbirdnest.com/Songbird/DeviceManager;2", &rv);
266 NS_ENSURE_SUCCESS(rv, rv);
268 nsCOMPtr<sbIDeviceControllerRegistrar> deviceControllerRegistrar =
269 do_GetService(
"@songbirdnest.com/Songbird/DeviceManager;2", &rv);
270 NS_ENSURE_SUCCESS(rv, rv);
273 nsCOMPtr<sbIDeviceController> deviceController;
274 nsID *controllerId = nsnull;
275 rv = aDevice->GetControllerId(&controllerId);
276 if (NS_SUCCEEDED(rv)) {
277 rv = deviceControllerRegistrar->GetController(
279 getter_AddRefs(deviceController));
283 NS_WARNING(
"Failed to get device controller.");
284 deviceController = nsnull;
288 NS_Free(controllerId);
292 if (deviceController) {
293 rv = deviceController->ReleaseDevice(aDevice);
294 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to release the device");
298 rv = deviceRegistrar->UnregisterDevice(aDevice);
299 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to unregister device");
309 nsCOMPtr<sbIDevice> device;
310 rv =
GetDevice(aName, getter_AddRefs(device));
311 if (NS_FAILED(rv) || !device) {
317 NS_ENSURE_SUCCESS(rv, rv);
325 NS_ENSURE_ARG_POINTER(aOutDevice);
328 nsCOMPtr<nsISupports> supports;
329 rv = mKnownDevices.Get(aName, getter_AddRefs(supports));
330 NS_ENSURE_SUCCESS(rv, rv);
332 nsCOMPtr<sbIDevice> device = do_QueryInterface(supports, &rv);
333 if (NS_FAILED(rv) || !device) {
334 return NS_ERROR_FAILURE;
337 device.forget(aOutDevice);
344 NS_ENSURE_ARG_POINTER(aOutHasDevice);
345 *aOutHasDevice = PR_FALSE;
348 nsAutoMonitor mon(mKnownDevicesLock);
351 nsCOMPtr<sbIDevice> deviceRef;
352 rv =
GetDevice(aName, getter_AddRefs(deviceRef));
353 if (NS_SUCCEEDED(rv) && deviceRef) {
354 *aOutHasDevice = PR_TRUE;
367 NS_ENSURE_STATE(mCDDeviceService);
369 nsCOMPtr<nsIThreadPool> threadPoolService =
370 do_GetService(
"@songbirdnest.com/Songbird/ThreadPoolService;1", &rv);
371 NS_ENSURE_SUCCESS(rv, rv);
373 nsCOMPtr<nsIThreadManager> threadMgr =
374 do_GetService(
"@mozilla.org/thread-manager;1", &rv);
375 NS_ENSURE_SUCCESS(rv, rv);
379 rv = threadMgr->GetCurrentThread(getter_AddRefs(mOwnerContextThread));
380 NS_ENSURE_SUCCESS(rv, rv);
382 nsCOMPtr<nsIRunnable> runnable =
384 NS_ENSURE_TRUE(runnable, NS_ERROR_FAILURE);
386 rv = threadPoolService->Dispatch(runnable, NS_DISPATCH_NORMAL);
387 NS_ENSURE_SUCCESS(rv, rv);
396 PRInt32 deviceCount = 0;
397 nsresult rv = mCDDeviceService->GetNbDevices(&deviceCount);
398 NS_ENSURE_SUCCESS(rv, );
401 nsCOMPtr<nsIRunnable> runnable =
403 NS_ENSURE_TRUE(runnable, );
404 rv = mOwnerContextThread->Dispatch(runnable, NS_DISPATCH_SYNC);
405 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
406 "WARNING: Could not notify of device start scan!");
409 for (PRInt32
i = 0;
i < deviceCount;
i++) {
410 nsCOMPtr<sbICDDevice> curDevice;
411 rv = mCDDeviceService->GetDevice(
i, getter_AddRefs(curDevice));
412 if (NS_FAILED(rv) || !curDevice) {
413 NS_WARNING(
"Could not get the current device!");
422 mOwnerContextThread);
424 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Could not add a CD Device!");
432 NS_ENSURE_TRUE(runnable, );
433 rv = mOwnerContextThread->Dispatch(runnable, NS_DISPATCH_SYNC);
434 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
435 "WARNING: Could not notify of device start scan!");
444 static_cast<sbIDeviceMarshall *>(
this));
453 static_cast<sbIDeviceMarshall *>(
this));
465 nsCOMPtr<sbIDeviceManager2> manager =
466 do_GetService(
"@songbirdnest.com/Songbird/DeviceManager;2", &rv);
467 NS_ENSURE_SUCCESS(rv, rv);
470 nsCOMPtr<sbIDeviceEventTarget>
eventTarget = do_QueryInterface(manager, &rv);
471 NS_ENSURE_SUCCESS(rv, rv);
474 nsCOMPtr<sbIDeviceEvent>
event;
475 rv = manager->CreateEvent(aType,
480 getter_AddRefs(event));
481 NS_ENSURE_SUCCESS(rv, rv);
485 rv = eventTarget->DispatchEvent(event, aAsync, &dispatched);
486 NS_ENSURE_SUCCESS(rv, rv);
497 NS_ENSURE_ARG_POINTER(aRegistrar);
505 sbCDDeviceMarshall::BeginMonitoring()
509 NS_ENSURE_STATE(mCDDeviceService);
510 NS_ASSERTION(
IsMonitoring(),
"BeginMonitoring() called after StopMonitoring()!");
512 rv = mCDDeviceService->RegisterListener(
this);
513 NS_ENSURE_SUCCESS(rv, rv);
516 NS_ENSURE_SUCCESS(rv, rv);
522 sbCDDeviceMarshall::StopMonitoring()
526 if (mCDDeviceService) {
527 rv = mCDDeviceService->RemoveListener(
this);
528 NS_ENSURE_SUCCESS(rv, rv);
535 sbCDDeviceMarshall::GetId(nsID **aId)
537 NS_ENSURE_ARG_POINTER(aId);
540 *aId =
static_cast<nsID *
>(NS_Alloc(
sizeof(nsID)));
547 sbCDDeviceMarshall::GetName(nsAString & aName)
557 sbCDDeviceMarshall::OnDeviceRemoved(
sbICDDevice *aDevice)
559 NS_ENSURE_ARG_POINTER(aDevice);
563 rv = aDevice->GetName(deviceName);
564 NS_ENSURE_SUCCESS(rv, rv);
567 nsCOMPtr<sbIDevice> device;
568 rv =
GetDevice(deviceName, getter_AddRefs(device));
569 if (NS_SUCCEEDED(rv) && device) {
571 NS_ENSURE_SUCCESS(rv, rv);
578 sbCDDeviceMarshall::OnMediaInserted(
sbICDDevice *aDevice)
580 NS_ENSURE_ARG_POINTER(aDevice);
583 NS_ENSURE_SUCCESS(rv, rv);
589 sbCDDeviceMarshall::OnMediaEjected(
sbICDDevice *aDevice)
591 NS_ENSURE_ARG_POINTER(aDevice);
595 rv = aDevice->GetName(deviceName);
596 NS_ENSURE_SUCCESS(rv, rv);
599 NS_ENSURE_SUCCESS(rv, rv);
nsresult AddDevice(sbICDDevice *aCDDevice)
virtual ~sbCDDeviceMarshall()
nsresult GetHasDevice(nsAString const &aName, PRBool *aOutHasDevice)
NS_IMPL_QUERY_INTERFACE2_CI(sbCDDeviceMarshall, sbIDeviceMarshall, sbICDDeviceListener) NS_IMPL_CI_INTERFACE_GETTER2(sbCDDeviceMarshall
nsresult RemoveDevice(nsAString const &aName)
const unsigned long EVENT_DEVICE_SCAN_END
Songbird Variant Utility Definitions.
const unsigned long EVENT_DEVICE_ADDED
#define SB_CDDEVICE_MARSHALL_IID
NS_IMPL_THREADSAFE_RELEASE(sbRequestItem)
NS_IMPL_THREADSAFE_ADDREF(sbRequestItem)
PRBool IsMonitoring() const
sbIDeviceController * FindCompatibleControllers(nsIPropertyBag *deviceParams)
void RunNotifyDeviceStartScan()
void RegisterControllers(sbIDeviceControllerRegistrar *registrar)
const unsigned long EVENT_DEVICE_SCAN_START
const unsigned long STATE_IDLE
#define SB_CDDEVICE_MARSHALL_NAME
Songbird Thread Utilities Definitions.
RT sbInvokeOnThread1(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1, TH aThread)
nsresult DiscoverDevices()
void RunNotifyDeviceStopScan()
NS_DECL_ISUPPORTS NS_DECL_NSICLASSINFO NS_DECL_SBIDEVICEMARSHALL NS_DECL_SBICDDEVICELISTENER nsresult Init()
void RunDiscoverDevices()
[UNIMPLEMENTED UNTIL AFTER 0.3]
nsresult GetDevice(nsAString const &aName, sbIDevice **aOutDevice)
StringArrayEnumerator prototype hasMore
#define SB_CDDEVICE_MARSHALL_CID
NS_IMPL_CI_INTERFACE_GETTER2(sbDataRemoteWrapper, sbIDataRemote, nsIClassInfo) sbDataRemoteWrapper
const unsigned long AUDIO_DISC_TYPE
#define SB_DEVICE_CONTROLLER_CATEGORY
readonly attribute nsIDPtr id
_getSelectedPageStyle s i
nsresult CreateAndDispatchDeviceManagerEvent(PRUint32 aType, nsIVariant *aData=nsnull, nsISupports *aOrigin=nsnull, PRBool aAsync=PR_FALSE)
void ClearMonitoringFlag()
_updateTextAndScrollDataForFrame aData