27 #include <nsILocalFile.h>
28 #include <nsIMutableArray.h>
29 #include <nsIObserverService.h>
30 #include <nsISupportsPrimitives.h>
31 #include <nsIVariant.h>
33 #include <sbIDevice.h>
34 #include <sbIDeviceEvent.h>
35 #include <sbIDeviceEventTarget.h>
36 #include <sbIDeviceFirmwareHandler.h>
37 #include <sbILibraryManager.h>
39 #include <nsAutoLock.h>
40 #include <nsAutoPtr.h>
41 #include <nsArrayUtils.h>
42 #include <nsComponentManagerUtils.h>
53 static PRLogModuleInfo* gDeviceFirmwareUpdater = nsnull;
54 #define TRACE(args) PR_LOG(gDeviceFirmwareUpdater, PR_LOG_DEBUG, args)
55 #define LOG(args) PR_LOG(gDeviceFirmwareUpdater, PR_LOG_WARN, args)
61 #define MIN_RUNNING_HANDLERS (2)
63 #define FIRMWARE_FILE_PREF "firmware.cache.file"
64 #define FIRMWARE_VERSION_PREF "firmware.cache.version"
65 #define FIRMWARE_READABLE_PREF "firmware.cache.readableVersion"
74 , mIsShutdown(PR_FALSE)
77 if(!gDeviceFirmwareUpdater) {
78 gDeviceFirmwareUpdater = PR_NewLogModule(
"sbDeviceFirmwareUpdater");
83 sbDeviceFirmwareUpdater::~sbDeviceFirmwareUpdater()
86 nsAutoMonitor::DestroyMonitor(
mMonitor);
93 LOG((
"[sbDeviceFirmwareUpdater] - Init"));
95 mMonitor = nsAutoMonitor::NewMonitor(
"sbDeviceFirmwareUpdater::mMonitor");
96 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_OUT_OF_MEMORY);
98 nsresult rv = NS_ERROR_UNEXPECTED;
99 nsCOMPtr<nsISimpleEnumerator> categoryEnum;
101 nsCOMPtr<nsICategoryManager>
cm =
102 do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
103 NS_ENSURE_SUCCESS(rv, rv);
106 getter_AddRefs(categoryEnum));
107 NS_ENSURE_SUCCESS(rv, rv);
110 while (NS_SUCCEEDED(categoryEnum->HasMoreElements(&hasMore)) &&
113 nsCOMPtr<nsISupports> ptr;
114 if (NS_SUCCEEDED(categoryEnum->GetNext(getter_AddRefs(ptr))) &&
117 nsCOMPtr<nsISupportsCString> stringValue(do_QueryInterface(ptr));
119 nsCString factoryName;
121 NS_SUCCEEDED(stringValue->GetData(factoryName))) {
123 nsCString contractId;
125 factoryName.get(), getter_Copies(contractId));
126 NS_ENSURE_SUCCESS(rv, rv);
132 NS_ENSURE_TRUE(element, NS_ERROR_OUT_OF_MEMORY);
134 LOG((
"[sbDeviceFirmwareUpdater] - Init - Adding %s as a firmware handler",
135 contractId.BeginReading()));
142 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
145 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
148 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
151 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
153 nsCOMPtr<nsIEventTarget> threadPool =
154 do_GetService(
"@songbirdnest.com/Songbird/ThreadPoolService;1", &rv);
155 NS_ENSURE_SUCCESS(rv, rv);
160 do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
161 NS_ENSURE_SUCCESS(rv, rv);
165 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to add library manager observer");
173 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
174 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
183 nsresult rv = NS_ERROR_UNEXPECTED;
184 nsCOMPtr<nsIMutableArray> mutableArray =
185 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
186 NS_ENSURE_SUCCESS(rv, rv);
188 mRunningHandlers.EnumerateRead(sbDeviceFirmwareUpdater::EnumerateIntoArrayISupportsKey,
192 rv = mutableArray->GetLength(&length);
193 NS_ENSURE_SUCCESS(rv, rv);
195 for(PRUint32 current = 0; current < length; ++current) {
196 nsCOMPtr<sbIDeviceFirmwareHandler>
handler =
197 do_QueryElementAt(mutableArray, current, &rv);
198 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Bad object in hashtable!");
204 NS_ENSURE_TRUE(handlerStatus, NS_ERROR_OUT_OF_MEMORY);
209 NS_ENSURE_SUCCESS(rv, rv);
214 rv = handler->Cancel();
215 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
216 "Failed to cancel, may not shutdown cleanly!");
228 already_AddRefed<sbIDeviceFirmwareHandler>
231 NS_ENSURE_TRUE(aDevice, nsnull);
235 nsCOMPtr<sbIDeviceFirmwareHandler>
handler;
237 handler.forget(&_retval);
243 already_AddRefed<sbIDeviceFirmwareHandler>
250 NS_ENSURE_TRUE(aDevice, nsnull);
254 nsCOMPtr<sbIDeviceFirmwareHandler>
handler;
256 nsresult rv = GetHandler(aDevice,
259 getter_AddRefs(handler));
260 NS_ENSURE_SUCCESS(rv, nsnull);
262 rv = handler->Bind(aDevice, aListener);
263 NS_ENSURE_SUCCESS(rv, nsnull);
266 NS_ENSURE_SUCCESS(rv, nsnull);
270 handler.forget(&_retval);
280 NS_ENSURE_ARG_POINTER(aDevice);
281 NS_ENSURE_ARG_POINTER(aHandler);
283 nsCOMPtr<sbIDeviceFirmwareHandler>
handler;
286 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
288 #if defined PR_LOGGING
290 NS_WARN_IF_FALSE(handler == aHandler,
291 "Attempting to replace a running firmware handler!");
302 NS_ENSURE_TRUE(aHandler, nsnull);
310 nsresult rv = status->Init();
311 NS_ENSURE_SUCCESS(rv, nsnull);
314 NS_ENSURE_TRUE(success, nsnull);
316 _retval = status.forget();
326 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
327 NS_ENSURE_ARG_POINTER(aDevice);
328 NS_ENSURE_ARG_POINTER(aHandler);
330 PRBool needsRecoveryMode = PR_FALSE;
332 nsresult rv = aHandler->GetNeedsRecoveryMode(&needsRecoveryMode);
333 NS_ENSURE_SUCCESS(rv, rv);
337 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
347 nsCOMPtr<nsIVariant> firmwareVersion;
350 getter_AddRefs(firmwareVersion));
351 NS_ENSURE_SUCCESS(rv, rv);
353 PRUint32 prefVersion = 0;
354 rv = firmwareVersion->GetAsUint32(&prefVersion);
355 NS_ENSURE_SUCCESS(rv, rv);
358 getter_AddRefs(firmwareVersion));
360 nsString prefReadableVersion;
361 rv = firmwareVersion->GetAsAString(prefReadableVersion);
362 NS_ENSURE_SUCCESS(rv, rv);
364 nsCOMPtr<nsIVariant> firmwareFilePath;
366 getter_AddRefs(firmwareFilePath));
367 NS_ENSURE_SUCCESS(rv, rv);
370 rv = firmwareFilePath->GetAsAString(filePath);
371 NS_ENSURE_SUCCESS(rv, rv);
373 nsCOMPtr<nsILocalFile> localFile;
374 rv = NS_NewLocalFile(filePath, PR_FALSE, getter_AddRefs(localFile));
376 PRBool exists = PR_FALSE;
377 rv = localFile->Exists(&exists);
378 NS_ENSURE_SUCCESS(rv, rv);
385 nsCOMPtr<sbIDeviceFirmwareUpdate> firmwareUpdate =
387 NS_ENSURE_SUCCESS(rv, rv);
389 rv = firmwareUpdate->Init(localFile,
392 NS_ENSURE_SUCCESS(rv, rv);
394 firmwareUpdate.forget(aUpdate);
400 sbDeviceFirmwareUpdater::CheckForUpdate(
sbIDevice *aDevice,
401 PRUint32 aDeviceVendorID,
402 PRUint32 aDeviceProductID,
405 LOG((
"[sbDeviceFirmwareUpdater] - CheckForUpdate"));
407 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
408 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
409 NS_ENSURE_ARG_POINTER(aDevice);
411 nsresult rv = NS_ERROR_UNEXPECTED;
413 nsCOMPtr<sbIDeviceFirmwareHandler>
handler =
420 NS_ENSURE_TRUE(handler, NS_ERROR_UNEXPECTED);
423 rv = handler->CanUpdate(aDevice,
427 NS_ENSURE_SUCCESS(rv, rv);
428 NS_ENSURE_TRUE(canUpdate, NS_ERROR_NOT_IMPLEMENTED);
433 NS_ENSURE_TRUE(handlerStatus, NS_ERROR_OUT_OF_MEMORY);
438 NS_ENSURE_SUCCESS(rv, rv);
442 return NS_ERROR_FAILURE;
446 nsCOMPtr<sbIDeviceEventTarget>
eventTarget = do_QueryInterface(aDevice, &rv);
447 NS_ENSURE_SUCCESS(rv, rv);
449 rv = eventTarget->AddEventListener(
this);
450 NS_ENSURE_SUCCESS(rv, rv);
453 NS_ENSURE_SUCCESS(rv, rv);
456 NS_ENSURE_SUCCESS(rv, rv);
459 NS_ENSURE_SUCCESS(rv, rv);
463 rv = handler->RefreshInfo();
464 NS_ENSURE_SUCCESS(rv, rv);
470 sbDeviceFirmwareUpdater::DownloadUpdate(
sbIDevice *aDevice,
471 PRBool aVerifyFirmwareUpdate,
474 LOG((
"[sbDeviceFirmwareUpdater] - DownloadUpdate"));
476 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
477 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
478 NS_ENSURE_ARG_POINTER(aDevice);
480 nsresult rv = NS_ERROR_UNEXPECTED;
482 nsCOMPtr<sbIDeviceFirmwareHandler> handler =
485 nsString deviceModel;
486 rv = handler->GetDeviceModelNumber(deviceModel);
487 NS_ENSURE_SUCCESS(rv, rv);
489 nsString deviceVendor;
490 rv = handler->GetDeviceVendor(deviceVendor);
491 NS_ENSURE_SUCCESS(rv, rv);
496 NS_ENSURE_TRUE(handlerStatus, NS_ERROR_OUT_OF_MEMORY);
501 NS_ENSURE_SUCCESS(rv, rv);
505 return NS_ERROR_FAILURE;
508 nsCOMPtr<sbIDeviceEventTarget> eventTarget = do_QueryInterface(aDevice, &rv);
509 NS_ENSURE_SUCCESS(rv, rv);
511 rv = eventTarget->AddEventListener(
this);
512 NS_ENSURE_SUCCESS(rv, rv);
515 NS_ENSURE_SUCCESS(rv, rv);
518 NS_ENSURE_SUCCESS(rv, rv);
521 NS_ENSURE_SUCCESS(rv, rv);
525 nsRefPtr<sbDeviceFirmwareDownloader> downloader;
527 NS_ENSURE_TRUE(downloader, NS_ERROR_OUT_OF_MEMORY);
529 if(deviceModel.IsVoid() || deviceVendor.IsVoid()) {
530 rv = downloader->Init(aDevice,
533 NS_ENSURE_SUCCESS(rv, rv);
536 nsString deviceCacheDirName(deviceVendor);
537 deviceCacheDirName.AppendLiteral(
" ");
538 deviceCacheDirName += deviceModel;
540 rv = downloader->Init(aDevice,
544 NS_ENSURE_SUCCESS(rv, rv);
548 rv = downloader->Start();
549 NS_ENSURE_SUCCESS(rv, rv);
551 nsCOMPtr<sbIFileDownloaderListener>
listener;
552 if(
mDownloaders.Get(aDevice, getter_AddRefs(listener))) {
556 rv = downloader->
Cancel();
557 NS_ENSURE_SUCCESS(rv, rv);
563 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
569 sbDeviceFirmwareUpdater::VerifyUpdate(
sbIDevice *aDevice,
573 LOG((
"[sbDeviceFirmwareUpdater] - VerifyUpdate"));
575 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
576 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
577 NS_ENSURE_ARG_POINTER(aDevice);
578 NS_ENSURE_ARG_POINTER(aFirmwareUpdate);
580 return NS_ERROR_NOT_IMPLEMENTED;
584 sbDeviceFirmwareUpdater::ApplyUpdate(
sbIDevice *aDevice,
588 LOG((
"[sbDeviceFirmwareUpdater] - ApplyUpdate"));
590 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
591 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
592 NS_ENSURE_ARG_POINTER(aDevice);
593 NS_ENSURE_ARG_POINTER(aFirmwareUpdate);
595 nsresult rv = NS_ERROR_UNEXPECTED;
597 nsCOMPtr<sbIDeviceFirmwareHandler> handler =
603 NS_ENSURE_TRUE(handlerStatus, NS_ERROR_OUT_OF_MEMORY);
608 NS_ENSURE_SUCCESS(rv, rv);
612 return NS_ERROR_FAILURE;
616 nsCOMPtr<sbIDeviceEventTarget> eventTarget = do_QueryInterface(aDevice, &rv);
617 NS_ENSURE_SUCCESS(rv, rv);
619 rv = eventTarget->AddEventListener(
this);
620 NS_ENSURE_SUCCESS(rv, rv);
623 NS_ENSURE_SUCCESS(rv, rv);
626 NS_ENSURE_SUCCESS(rv, rv);
629 NS_ENSURE_SUCCESS(rv, rv);
633 nsRefPtr<sbDeviceFirmwareUpdaterRunner> runner;
635 NS_ENSURE_TRUE(runner, NS_ERROR_OUT_OF_MEMORY);
637 rv = runner->Init(aDevice, aFirmwareUpdate, handler);
638 NS_ENSURE_SUCCESS(rv, rv);
640 rv =
mThreadPool->Dispatch(runner, NS_DISPATCH_NORMAL);
641 NS_ENSURE_SUCCESS(rv, rv);
647 sbDeviceFirmwareUpdater::RecoveryUpdate(
sbIDevice *aDevice,
649 PRUint32 aDeviceVendorID,
650 PRUint32 aDeviceProductID,
653 LOG((
"[sbDeviceFirmwareUpdater] - RecoveryUpdate"));
655 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
656 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
657 NS_ENSURE_ARG_POINTER(aDevice);
659 nsresult rv = NS_ERROR_UNEXPECTED;
661 nsCOMPtr<sbIDeviceFirmwareHandler> handler =
671 NS_ENSURE_TRUE(handlerStatus, NS_ERROR_OUT_OF_MEMORY);
676 NS_ENSURE_SUCCESS(rv, rv);
680 return NS_ERROR_FAILURE;
683 nsCOMPtr<sbIDeviceEventTarget> eventTarget = do_QueryInterface(aDevice, &rv);
684 NS_ENSURE_SUCCESS(rv, rv);
686 rv = eventTarget->AddEventListener(
this);
687 NS_ENSURE_SUCCESS(rv, rv);
690 NS_ENSURE_SUCCESS(rv, rv);
693 NS_ENSURE_SUCCESS(rv, rv);
696 NS_ENSURE_SUCCESS(rv, rv);
702 PRBool needsCaching = PR_FALSE;
705 nsCOMPtr<sbIDeviceFirmwareUpdate> firmwareUpdate;
709 nsCOMPtr<sbIDeviceFirmwareUpdate> defaultFirmwareUpdate;
712 nsCOMPtr<sbIDeviceFirmwareUpdate> cachedFirmwareUpdate;
714 LOG((
"Using cached firmware update? %s",
715 NS_SUCCEEDED(rv) ?
"true" :
"false"));
718 if(!aFirmwareUpdate) {
719 rv = handler->GetDefaultFirmwareUpdate(getter_AddRefs(defaultFirmwareUpdate));
720 NS_ENSURE_SUCCESS(rv, rv);
733 firmwareUpdate = aFirmwareUpdate;
734 needsCaching = PR_FALSE;
741 if(cachedFirmwareUpdate && defaultFirmwareUpdate) {
742 nsCOMPtr<nsIFile> cachedFile;
743 rv = cachedFirmwareUpdate->GetFirmwareImageFile(getter_AddRefs(cachedFile));
744 NS_ENSURE_SUCCESS(rv, rv);
746 nsCOMPtr<nsIFile> defaultFile;
747 rv = defaultFirmwareUpdate->GetFirmwareImageFile(getter_AddRefs(defaultFile));
748 NS_ENSURE_SUCCESS(rv, rv);
750 nsString cachedFileName, defaultFileName;
751 rv = cachedFile->GetLeafName(cachedFileName);
752 NS_ENSURE_SUCCESS(rv, rv);
754 rv = defaultFile->GetLeafName(defaultFileName);
755 NS_ENSURE_SUCCESS(rv, rv);
757 PRInt64 cachedFileSize = 0;
758 rv = cachedFile->GetFileSize(&cachedFileSize);
759 NS_ENSURE_SUCCESS(rv, rv);
761 PRInt64 defaultFileSize = 0;
762 rv = defaultFile->GetFileSize(&defaultFileSize);
763 NS_ENSURE_SUCCESS(rv, rv);
765 if((cachedFileName != defaultFileName) ||
766 (cachedFileName == defaultFileName && cachedFileSize != defaultFileSize)) {
767 nsCOMPtr<nsIFile> cacheDir;
768 rv = cachedFile->GetParent(getter_AddRefs(cacheDir));
769 NS_ENSURE_SUCCESS(rv, rv);
771 rv = cachedFile->Remove(PR_FALSE);
772 NS_ENSURE_SUCCESS(rv, rv);
774 nsCOMPtr<nsISimpleEnumerator> cacheDirEntries;
775 rv = cacheDir->GetDirectoryEntries(getter_AddRefs(cacheDirEntries));
776 NS_ENSURE_SUCCESS(rv, rv);
779 rv = cacheDirEntries->HasMoreElements(&hasMore);
780 NS_ENSURE_SUCCESS(rv, rv);
783 nsCOMPtr<nsIFile>
file;
784 rv = cacheDirEntries->GetNext(getter_AddRefs(file));
785 NS_ENSURE_SUCCESS(rv, rv);
787 PRBool isFile = PR_FALSE;
788 rv = file->IsFile(&isFile);
789 NS_ENSURE_SUCCESS(rv, rv);
792 rv = file->Remove(PR_FALSE);
793 NS_ENSURE_SUCCESS(rv, rv);
796 rv = cacheDirEntries->HasMoreElements(&hasMore);
797 NS_ENSURE_SUCCESS(rv, rv);
800 firmwareUpdate = defaultFirmwareUpdate;
801 needsCaching = PR_TRUE;
804 firmwareUpdate = cachedFirmwareUpdate;
805 needsCaching = PR_FALSE;
808 else if(cachedFirmwareUpdate) {
809 firmwareUpdate = cachedFirmwareUpdate;
811 else if(defaultFirmwareUpdate) {
812 firmwareUpdate = defaultFirmwareUpdate;
813 needsCaching = PR_TRUE;
816 return NS_ERROR_UNEXPECTED;
833 nsString deviceModel;
834 rv = handler->GetDeviceModelNumber(deviceModel);
835 NS_ENSURE_SUCCESS(rv, rv);
837 nsString deviceVendor;
838 rv = handler->GetDeviceVendor(deviceVendor);
839 NS_ENSURE_SUCCESS(rv, rv);
841 if(deviceModel.IsVoid() || deviceVendor.IsVoid()) {
842 nsCOMPtr<sbIDeviceFirmwareUpdate> cachedFirmwareUpdate;
845 getter_AddRefs(cachedFirmwareUpdate));
846 NS_ENSURE_SUCCESS(rv, rv);
849 nsString deviceCacheDirName(deviceVendor);
850 deviceCacheDirName.AppendLiteral(
" ");
851 deviceCacheDirName += deviceModel;
853 nsCOMPtr<sbIDeviceFirmwareUpdate> cachedFirmwareUpdate;
857 getter_AddRefs(cachedFirmwareUpdate));
858 NS_ENSURE_SUCCESS(rv, rv);
861 cachedFirmwareUpdate.swap(firmwareUpdate);
864 nsRefPtr<sbDeviceFirmwareUpdaterRunner> runner;
866 NS_ENSURE_TRUE(runner, NS_ERROR_OUT_OF_MEMORY);
868 rv = runner->Init(aDevice, firmwareUpdate, handler, PR_TRUE);
869 NS_ENSURE_SUCCESS(rv, rv);
871 rv =
mThreadPool->Dispatch(runner, NS_DISPATCH_NORMAL);
872 NS_ENSURE_SUCCESS(rv, rv);
878 sbDeviceFirmwareUpdater::ContinueUpdate(
sbIDevice *aDevice,
882 LOG((
"[sbDeviceFirmwareUpdater] - ContinueUpdate"));
884 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
885 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
886 NS_ENSURE_ARG_POINTER(aDevice);
887 NS_ENSURE_ARG_POINTER(_retval);
889 nsresult rv = NS_ERROR_UNEXPECTED;
893 nsCOMPtr<nsIMutableArray> mutableArray =
894 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
895 NS_ENSURE_SUCCESS(rv, rv);
898 sbDeviceFirmwareUpdater::EnumerateIntoArrayISupportsKey,
902 rv = mutableArray->GetLength(&length);
903 NS_ENSURE_SUCCESS(rv, rv);
905 for(PRUint32 current = 0; current < length; ++current) {
906 nsCOMPtr<sbIDeviceFirmwareHandler> handler =
907 do_QueryElementAt(mutableArray, current, &rv);
908 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Bad object in hashtable!");
913 nsCOMPtr<sbIDevice> oldDevice;
914 rv = handler->GetBoundDevice(getter_AddRefs(oldDevice));
915 NS_ENSURE_SUCCESS(rv, rv);
917 PRBool success = PR_FALSE;
918 rv = handler->Rebind(aDevice, aListener, &success);
919 NS_ENSURE_SUCCESS(rv, rv);
928 NS_ENSURE_SUCCESS(rv, rv);
933 rv = handler->GetContractId(contractId);
934 NS_ENSURE_SUCCESS(rv, rv);
936 LOG((
"[%s] continueUpdate successful", __FUNCTION__));
937 LOG((
"Handler with contract id %s will continue the update",
938 NS_ConvertUTF16toUTF8(contractId).BeginReading()));
948 sbDeviceFirmwareUpdater::FinalizeUpdate(
sbIDevice *aDevice)
950 LOG((
"[sbDeviceFirmwareUpdater] - FinalizeUpdate"));
952 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
953 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
954 NS_ENSURE_ARG_POINTER(aDevice);
957 NS_ENSURE_TRUE(handler,
NS_OK);
965 nsCOMPtr<sbIFileDownloaderListener>
listener;
966 if(
mDownloaders.Get(aDevice, getter_AddRefs(listener))) {
970 nsresult rv = downloader->
Cancel();
971 NS_ENSURE_SUCCESS(rv, rv);
980 sbDeviceFirmwareUpdater::VerifyDevice(
sbIDevice *aDevice,
983 LOG((
"[sbDeviceFirmwareUpdater] - VerifyDevice"));
985 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
986 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
987 NS_ENSURE_ARG_POINTER(aDevice);
989 return NS_ERROR_NOT_IMPLEMENTED;
995 LOG((
"[sbDeviceFirmwareUpdater] - RegisterHandler"));
997 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
998 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
999 NS_ENSURE_ARG_POINTER(aFirmwareHandler);
1001 nsString contractId;
1002 nsresult rv = aFirmwareHandler->GetContractId(contractId);
1003 NS_ENSURE_SUCCESS(rv, rv);
1005 NS_ConvertUTF16toUTF8 contractId8(contractId);
1010 NS_ENSURE_TRUE(element, NS_ERROR_OUT_OF_MEMORY);
1019 LOG((
"[sbDeviceFirmwareUpdater] - UnregisterHandler"));
1021 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
1022 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
1023 NS_ENSURE_ARG_POINTER(aFirmwareHandler);
1025 nsString contractId;
1026 nsresult rv = aFirmwareHandler->GetContractId(contractId);
1027 NS_ENSURE_SUCCESS(rv, rv);
1029 NS_ConvertUTF16toUTF8 contractId8(contractId);
1032 firmwarehandlers_t::index_type index =
1035 if(index != firmwarehandlers_t::NoIndex) {
1043 sbDeviceFirmwareUpdater::HasHandler(
sbIDevice *aDevice,
1044 PRUint32 aDeviceVendorID,
1045 PRUint32 aDeviceProductID,
1048 LOG((
"[sbDeviceFirmwareUpdater] - HasHandler"));
1050 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
1051 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
1052 NS_ENSURE_ARG_POINTER(aDevice);
1053 NS_ENSURE_ARG_POINTER(_retval);
1055 nsCOMPtr<sbIDeviceFirmwareHandler>
handler;
1056 nsresult rv = GetHandler(aDevice,
1059 getter_AddRefs(handler));
1061 *_retval = PR_FALSE;
1062 if(NS_SUCCEEDED(rv)) {
1070 sbDeviceFirmwareUpdater::GetHandler(
sbIDevice *aDevice,
1071 PRUint32 aDeviceVendorID,
1072 PRUint32 aDeviceProductID,
1075 LOG((
"[sbDeviceFirmwareUpdater] - GetHandler"));
1077 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
1078 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
1079 NS_ENSURE_ARG_POINTER(_retval);
1089 NS_ENSURE_TRUE(element, NS_ERROR_OUT_OF_MEMORY);
1092 nsresult rv = NS_ERROR_UNEXPECTED;
1093 firmwarehandlers_t::size_type length = firmwareHandlers.Length();
1094 for(firmwarehandlers_t::size_type
i = 0;
i < length; ++
i) {
1095 nsCOMPtr<sbIDeviceFirmwareHandler> handler =
1096 do_CreateInstance(firmwareHandlers[
i].BeginReading(), &rv);
1101 PRBool canHandleDevice = PR_FALSE;
1102 rv = handler->CanUpdate(aDevice,
1115 if(canHandleDevice) {
1116 handler.forget(_retval);
1121 return NS_ERROR_NOT_AVAILABLE;
1125 sbDeviceFirmwareUpdater::GetActiveHandler(
sbIDevice *aDevice,
1128 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
1129 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
1130 NS_ENSURE_ARG_POINTER(aDevice);
1131 NS_ENSURE_ARG_POINTER(_retval);
1135 handler.forget(_retval);
1139 return NS_ERROR_NOT_AVAILABLE;
1143 sbDeviceFirmwareUpdater::Cancel(
sbIDevice *aDevice)
1145 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
1146 NS_ENSURE_FALSE(
mIsShutdown, NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
1147 NS_ENSURE_ARG_POINTER(aDevice);
1154 nsresult rv = handler->Cancel();
1155 NS_ENSURE_SUCCESS(rv, rv);
1157 rv = handler->Unbind();
1158 NS_ENSURE_SUCCESS(rv, rv);
1168 sbDeviceFirmwareUpdater::RequireRecovery(
sbIDevice *aDevice)
1170 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_NOT_INITIALIZED);
1171 NS_ENSURE_ARG_POINTER(aDevice);
1176 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
1184 LOG((
"[sbDeviceFirmwareUpdater] - OnDeviceEvent"));
1186 NS_ENSURE_ARG_POINTER(aEvent);
1188 nsCOMPtr<sbIDevice> device;
1189 nsresult rv = aEvent->GetOrigin(getter_AddRefs(device));
1190 NS_ENSURE_SUCCESS(rv, rv);
1192 LOG((
"[sbDeviceFirmwareUpdater] - Origin Device 0x%X", device.get()));
1195 NS_ENSURE_TRUE(handler,
NS_OK);
1197 PRUint32 eventType = 0;
1198 rv = aEvent->GetType(&eventType);
1199 NS_ENSURE_SUCCESS(rv, rv);
1203 NS_ENSURE_TRUE(handlerStatus, NS_ERROR_UNEXPECTED);
1208 NS_ENSURE_SUCCESS(rv, rv);
1213 NS_ENSURE_SUCCESS(rv, rv);
1215 PRBool removeListener = PR_FALSE;
1219 LOG((
"[sbDeviceFirmwareUpdater] - OP_REFRESH"));
1224 NS_ENSURE_SUCCESS(rv, rv);
1230 NS_ENSURE_SUCCESS(rv, rv);
1232 removeListener = PR_TRUE;
1236 NS_ENSURE_SUCCESS(rv, rv);
1242 LOG((
"[sbDeviceFirmwareUpdater] - OP_DOWNLOAD"));
1247 NS_ENSURE_SUCCESS(rv, rv);
1252 NS_ENSURE_SUCCESS(rv, rv);
1254 removeListener = PR_TRUE;
1261 LOG((
"[sbDeviceFirmwareUpdater] - OP_UPDATE or OP_RECOVERY"));
1266 NS_ENSURE_SUCCESS(rv, rv);
1271 NS_ENSURE_SUCCESS(rv, rv);
1273 removeListener = PR_TRUE;
1279 NS_ENSURE_SUCCESS(rv, rv);
1285 NS_WARNING(
"Unsupported operation");
1290 if(removeListener) {
1291 nsCOMPtr<sbIDeviceEventTarget> eventTarget =
1292 do_QueryInterface(device, &rv);
1293 NS_ENSURE_SUCCESS(rv, rv);
1295 rv = eventTarget->RemoveEventListener(
this);
1296 NS_ENSURE_SUCCESS(rv, rv);
1305 nsIMutableArray *
array = (nsIMutableArray*)aArray;
1307 nsCOMPtr<nsISupports> supports = do_QueryInterface(aData, &rv);
1308 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
1310 rv = array->AppendElement(aData,
false);
1311 NS_ENSURE_SUCCESS(rv, PL_DHASH_STOP);
1313 return PL_DHASH_NEXT;
1317 PLDHashOperator sbDeviceFirmwareUpdater::EnumerateIntoArrayISupportsKey(
1322 TRACE((
"sbDeviceFirmwareUpdater[0x%x] - EnumerateIntoArray (nsISupports)"));
1330 sbDeviceFirmwareUpdater::Observe(
nsISupports* aSubject,
1332 const PRUnichar* aData)
1334 LOG((
"[sbDeviceFirmwareUpdater] - Observe: %s",
this, aTopic));
1338 do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
1339 NS_ENSURE_SUCCESS(rv, rv);
1343 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Failed to remove shutdown observer");
1346 NS_ENSURE_SUCCESS(rv, rv);
1359 , mStatus(STATUS_NONE)
1366 nsAutoMonitor::DestroyMonitor(mMonitor);
1373 mMonitor = nsAutoMonitor::NewMonitor(
"sbDeviceFirmwareHandlerStatus::mMonitor");
1374 NS_ENSURE_TRUE(mMonitor, NS_ERROR_OUT_OF_MEMORY);
1382 NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
1383 NS_ENSURE_ARG_POINTER(aOperation);
1385 nsAutoMonitor mon(mMonitor);
1386 *aOperation = mOperation;
1394 NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
1396 nsAutoMonitor mon(mMonitor);
1397 mOperation = aOperation;
1405 NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
1406 NS_ENSURE_ARG_POINTER(aStatus);
1408 nsAutoMonitor mon(mMonitor);
1417 NS_ENSURE_TRUE(mMonitor, NS_ERROR_NOT_INITIALIZED);
1419 nsAutoMonitor mon(mMonitor);
1432 : mRecovery(PR_FALSE)
1437 sbDeviceFirmwareUpdaterRunner::~sbDeviceFirmwareUpdaterRunner()
1447 NS_ENSURE_ARG_POINTER(aDevice);
1448 NS_ENSURE_ARG_POINTER(aUpdate);
1449 NS_ENSURE_ARG_POINTER(aHandler);
1461 sbDeviceFirmwareUpdaterRunner::Run()
1466 nsresult rv = NS_ERROR_UNEXPECTED;
1470 NS_ENSURE_SUCCESS(rv, rv);
1474 NS_ENSURE_SUCCESS(rv, rv);
nsresult GetOperation(handleroperation_t *aOperation)
nsCOMPtr< nsIEventTarget > mThreadPool
const unsigned long EVENT_FIRMWARE_DOWNLOAD_END
Firmware download end.
nsresult RequiresRecoveryMode(sbIDevice *aDevice, sbIDeviceFirmwareHandler *aHandler)
handlerstatus_t mHandlerStatus
static nsCOMPtr< nsIObserverService > observerService
static nsresult CacheFirmwareUpdate(sbIDevice *aDevice, sbIDeviceFirmwareUpdate *aFirmwareUpdate, sbIDeviceFirmwareUpdate **aCachedFirmwareUpdate)
#define SB_LIBRARY_MANAGER_SHUTDOWN_TOPIC
const unsigned long EVENT_FIRMWARE_NEEDREC_ERROR
Firmware update requires recovery mode to be put on the device.
NS_DECL_ISUPPORTS NS_DECL_NSIRUNNABLE sbDeviceFirmwareUpdaterRunner()
#define MIN_RUNNING_HANDLERS
downloaders_t mDownloaders
nsresult PutRunningHandler(sbIDevice *aDevice, sbIDeviceFirmwareHandler *aHandler)
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceFirmwareUpdaterRunner, nsIRunnable)
#define SB_DEVICEFIRMWAREUPDATE_CONTRACTID
const unsigned long EVENT_FIRMWARE_CFU_END
Check for update end.
nsCOMPtr< sbIDevice > mDevice
const unsigned long EVENT_FIRMWARE_UPDATE_END
Firmware update end.
nsCOMPtr< sbIDeviceFirmwareHandler > mHandler
#define FIRMWARE_READABLE_PREF
const unsigned long EVENT_FIRMWARE_CFU_START
Check for update start.
nsresult Init(sbIDevice *aDevice, sbIDeviceFirmwareUpdate *aUpdate, sbIDeviceFirmwareHandler *aHandler, PRBool aRecovery=PR_FALSE)
const unsigned long EVENT_FIRMWARE_CFU_ERROR
Check for update error.
nsresult SetStatus(handlerstatus_t aStatus)
nsresult GetStatus(handlerstatus_t *aStatus)
runninghandlers_t mRunningHandlers
nsCOMPtr< sbIDeviceFirmwareUpdate > mFirmwareUpdate
~sbDeviceFirmwareHandlerStatus()
runninghandlers_t mRecoveryModeHandlers
NS_IMPL_THREADSAFE_ISUPPORTS3(sbDeviceFirmwareUpdater, sbIDeviceFirmwareUpdater, sbIDeviceEventListener, nsIObserver) sbDeviceFirmwareUpdater
nsresult SetOperation(handleroperation_t aOperation)
#define FIRMWARE_VERSION_PREF
StringArrayEnumerator prototype hasMore
sbDeviceFirmwareHandlerStatus()
#define FIRMWARE_FILE_PREF
#define SB_DEVICE_FIRMWARE_HANDLER_CATEGORY
nsTArray< nsCString > firmwarehandlers_t
nsresult GetCachedFirmwareUpdate(sbIDevice *aDevice, sbIDeviceFirmwareUpdate **aUpdate)
PLDHashOperator appendElementToArray(T *aData, void *aArray)
_getSelectedPageStyle s i
already_AddRefed< sbIDeviceFirmwareHandler > GetRunningHandler(sbIDevice *aDevice)
sbDeviceFirmwareHandlerStatus * GetHandlerStatus(sbIDeviceFirmwareHandler *aHandler)
const unsigned long EVENT_FIRMWARE_UPDATE_START
Firmware update start.
firmwarehandlers_t mFirmwareHandlers
_updateTextAndScrollDataForFrame aData
const unsigned long EVENT_FIRMWARE_DOWNLOAD_START
Firmware download start.