34 #include <nsAutoLock.h>
35 #include <nsComponentManagerUtils.h>
36 #include <nsIComponentRegistrar.h>
37 #include <nsIObserverService.h>
38 #include <nsISimpleEnumerator.h>
39 #include <nsServiceManagerUtils.h>
40 #include <nsISupportsPrimitives.h>
44 #include <sbILibraryManager.h>
51 static PRLogModuleInfo* gDevicemanagerLog = nsnull;
52 #define LOG(args) PR_LOG(gDevicemanagerLog, PR_LOG_DEBUG, args)
57 #define SB_DEVICE_PREFIX "@songbirdnest.com/Songbird/OldDeviceImpl/"
60 PRBool sbDeviceManagerObsolete::sServiceInitialized = PR_FALSE;
63 PRBool sbDeviceManagerObsolete::sDevicesLoaded = PR_FALSE;
66 PRBool sbDeviceManagerObsolete::sServiceFinalized = PR_FALSE;
74 mLastRequestedIndex(nsnull)
77 if (!gDevicemanagerLog)
78 gDevicemanagerLog = PR_NewLogModule(
"sbDeviceManagerObsolete");
81 LOG((
"DeviceManagerObsolete[0x%x] - Created",
this));
84 sbDeviceManagerObsolete::~sbDeviceManagerObsolete()
86 NS_ASSERTION(sbDeviceManagerObsolete::sServiceFinalized,
87 "DeviceManagerObsolete never finalized!");
89 nsAutoLock::DestroyLock(mLock);
91 LOG((
"DeviceManagerObsolete[0x%x] - Destroyed",
this));
97 LOG((
"DeviceManager[0x%x] - Initialize",
this));
103 NS_ENSURE_FALSE(sbDeviceManagerObsolete::sServiceInitialized,
104 NS_ERROR_ALREADY_INITIALIZED);
106 mLock = nsAutoLock::NewLock(
"sbDeviceManagerObsolete::mLock");
107 NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
109 mLastRequestedCategory = EmptyString();
116 do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
117 NS_ENSURE_SUCCESS(rv, rv);
122 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to add library manager observer");
127 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
128 "Failed to add library before shutdown observer");
131 rv = observerService->AddObserver(
this, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
133 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to add shutdown observer");
143 sbDeviceManagerObsolete::sServiceInitialized = PR_TRUE;
149 sbDeviceManagerObsolete::Finalize()
151 LOG((
"DeviceManagerObsolete[0x%x] - Finalize",
this));
154 NS_ENSURE_FALSE(sbDeviceManagerObsolete::sServiceFinalized, NS_ERROR_UNEXPECTED);
159 nsAutoLock autoLock(mLock);
161 PRInt32
count = mSupportedDevices.Count();
162 for (PRInt32 index = 0; index <
count; index++) {
163 nsCOMPtr<sbIDeviceBase> device = mSupportedDevices.ObjectAt(index);
164 NS_ASSERTION(device,
"Null pointer in mSupportedDevices");
166 rv = device->Finalize();
167 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"A device failed to finalize");
170 sbDeviceManagerObsolete::sServiceFinalized = PR_TRUE;
180 sbDeviceManagerObsolete::LoadSupportedDevices()
182 LOG((
"DeviceManagerObsolete[0x%x] - LoadSupportedDevices",
this));
185 NS_ENSURE_TRUE(sbDeviceManagerObsolete::sServiceInitialized,
186 NS_ERROR_ALREADY_INITIALIZED);
187 NS_ENSURE_FALSE(sbDeviceManagerObsolete::sDevicesLoaded, NS_ERROR_UNEXPECTED);
189 nsAutoLock autoLock(mLock);
193 nsCOMPtr<nsIComponentRegistrar>
registrar;
194 rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
195 NS_ENSURE_SUCCESS(rv, rv);
197 nsCOMPtr<nsISimpleEnumerator> simpleEnumerator;
198 rv = registrar->EnumerateContractIDs(getter_AddRefs(simpleEnumerator));
199 NS_ENSURE_SUCCESS(rv, rv);
203 PRBool more = PR_FALSE;
204 while(NS_SUCCEEDED(simpleEnumerator->HasMoreElements(&more)) && more) {
206 rv = simpleEnumerator->GetNext(getter_AddRefs(element));
207 NS_ENSURE_SUCCESS(rv, rv);
209 nsCOMPtr<nsISupportsCString> contractString =
210 do_QueryInterface(element, &rv);
212 NS_WARNING(
"QueryInterface failed");
217 rv = contractString->GetData(contractID);
219 NS_WARNING(
"GetData failed");
225 if (!StringBeginsWith(contractID, prefix))
229 nsCOMPtr<sbIDeviceBase> device =
230 do_CreateInstance(contractID.get(), &rv);
232 NS_WARNING(
"Failed to create device!");
237 rv = device->Initialize();
240 NS_WARNING(
"Device failed to initialize!");
245 PRBool ok = mSupportedDevices.AppendObject(device);
248 NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
251 sbDeviceManagerObsolete::sDevicesLoaded = PR_TRUE;
257 sbDeviceManagerObsolete::GetDeviceCount(PRUint32* aDeviceCount)
259 NS_ENSURE_ARG_POINTER(aDeviceCount);
260 NS_ENSURE_TRUE(sbDeviceManagerObsolete::sDevicesLoaded, NS_ERROR_UNEXPECTED);
262 nsAutoLock autoLock(mLock);
264 *aDeviceCount = (PRUint32)mSupportedDevices.Count();
269 sbDeviceManagerObsolete::GetCategoryByIndex(PRUint32 aIndex,
272 NS_ENSURE_TRUE(sbDeviceManagerObsolete::sDevicesLoaded, NS_ERROR_UNEXPECTED);
274 nsAutoLock autoLock(mLock);
276 NS_ENSURE_ARG_MAX(aIndex, (PRUint32)mSupportedDevices.Count());
278 nsCOMPtr<sbIDeviceBase> device = mSupportedDevices.ObjectAt(aIndex);
279 NS_ENSURE_TRUE(device, NS_ERROR_NULL_POINTER);
281 nsresult rv = device->GetDeviceCategory(_retval);
282 NS_ENSURE_SUCCESS(rv, rv);
288 sbDeviceManagerObsolete::GetDeviceByIndex(PRUint32 aIndex,
291 NS_ENSURE_ARG_POINTER(_retval);
292 NS_ENSURE_TRUE(sbDeviceManagerObsolete::sDevicesLoaded, NS_ERROR_UNEXPECTED);
294 nsAutoLock autoLock(mLock);
296 NS_ENSURE_ARG_MAX(aIndex, (PRUint32)mSupportedDevices.Count());
298 nsCOMPtr<sbIDeviceBase> device = mSupportedDevices.ObjectAt(aIndex);
299 NS_ENSURE_TRUE(device, NS_ERROR_UNEXPECTED);
301 NS_ADDREF(*_retval = device);
307 sbDeviceManagerObsolete::HasDeviceForCategory(
const nsAString& aCategory,
310 NS_ENSURE_ARG_POINTER(_retval);
311 NS_ENSURE_TRUE(sbDeviceManagerObsolete::sDevicesLoaded, NS_ERROR_UNEXPECTED);
313 nsAutoLock autoLock(mLock);
316 nsresult rv = GetIndexForCategory(aCategory, &dummy);
318 *_retval = NS_SUCCEEDED(rv);
323 sbDeviceManagerObsolete::GetDeviceByCategory(
const nsAString& aCategory,
326 NS_ENSURE_ARG_POINTER(_retval);
327 NS_ENSURE_TRUE(sbDeviceManagerObsolete::sDevicesLoaded, NS_ERROR_UNEXPECTED);
329 nsAutoLock autoLock(mLock);
332 nsresult rv = GetIndexForCategory(aCategory, &index);
335 NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_AVAILABLE);
337 nsCOMPtr<sbIDeviceBase> device =
338 do_QueryInterface(mSupportedDevices.ObjectAt(index));
339 NS_ENSURE_TRUE(device, NS_ERROR_UNEXPECTED);
341 NS_ADDREF(*_retval = device);
346 sbDeviceManagerObsolete::GetIndexForCategory(
const nsAString& aCategory,
353 if (!mLastRequestedCategory.IsEmpty() &&
354 aCategory.Equals(mLastRequestedCategory)) {
355 *_retval = mLastRequestedIndex;
360 PRInt32 count = mSupportedDevices.Count();
361 for (PRInt32 index = 0; index <
count; index++) {
362 nsCOMPtr<sbIDeviceBase> device = mSupportedDevices.ObjectAt(index);
364 NS_WARNING(
"Null pointer in mSupportedDevices");
368 nsAutoString category;
369 nsresult rv = device->GetDeviceCategory(category);
371 NS_WARNING(
"GetDeviceCategory Failed");
375 if (category.Equals(aCategory)) {
376 mLastRequestedCategory = category;
377 *_retval = mLastRequestedIndex = index;
383 mLastRequestedCategory = EmptyString();
384 return NS_ERROR_NOT_AVAILABLE;
388 sbDeviceManagerObsolete::Observe(
nsISupports* aSubject,
390 const PRUnichar*
aData)
392 LOG((
"DeviceManagerObsolete[0x%x] - Observe: %s",
this, aTopic));
396 do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
397 NS_ENSURE_SUCCESS(rv, rv);
401 rv = LoadSupportedDevices();
402 NS_ENSURE_SUCCESS(rv, rv);
405 rv = observerService->NotifyObservers(NS_ISUPPORTS_CAST(
sbIDeviceManager*,
this),
408 NS_ENSURE_SUCCESS(rv, rv);
413 NS_ENSURE_SUCCESS(rv, rv);
415 else if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
418 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
419 "Failed to remove library manager observer");
421 rv = observerService->RemoveObserver(
this,
423 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
424 "Failed to remove library manager before shutdown observer");
426 rv = observerService->RemoveObserver(
this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
427 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
428 "Failed to remove shutdown observer");
const SB_LIBRARY_MANAGER_READY_TOPIC
static nsCOMPtr< nsIObserverService > observerService
const SB_DEVICE_MANAGER_READY_TOPIC
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
NS_IMPL_THREADSAFE_ISUPPORTS2(sbDeviceManagerObsolete, sbIDeviceManager, nsIObserver) sbDeviceManagerObsolete
Songbird WMDevice Component Definition.
[SOON TO BE DEPRECATED AFTER 0.3] Base interface for all supported devices.
Songbird DeviceBase Component Definition.
const SB_LIBRARY_MANAGER_BEFORE_SHUTDOWN_TOPIC
_updateTextAndScrollDataForFrame aData