30 #include <nsIDOMDocument.h>
31 #include <nsIDOMElement.h>
32 #include <nsIDOMNode.h>
33 #include <nsIDOMNodeList.h>
34 #include <nsIInputStream.h>
35 #include <nsIInputStreamPump.h>
36 #include <nsIPrefBranch.h>
37 #include <nsIPrefService.h>
38 #include <nsISupportsUtils.h>
39 #include <nsIVariant.h>
40 #include <nsIWritablePropertyBag2.h>
42 #include <nsAutoLock.h>
44 #include <nsComponentManagerUtils.h>
45 #include <nsNetUtil.h>
46 #include <nsServiceManagerUtils.h>
47 #include <nsThreadUtils.h>
48 #include <nsXPCOMCIDInternal.h>
50 #include <sbIDevice.h>
51 #include <sbIDeviceFirmwareUpdate.h>
52 #include <sbIDeviceProperties.h>
62 "Songbird
Device Firmware Tester - Mock
Device Firmware Handler")
64 sbMockDeviceFirmwareHandler::sbMockDeviceFirmwareHandler() : mComplete(0)
68 sbMockDeviceFirmwareHandler::~sbMockDeviceFirmwareHandler()
77 NS_LITERAL_STRING(
"@songbirdnest.com/Songbird/Device/Firmware/Handler/MockDevice;1");
80 do_GetService(
"@songbirdnest.com/mock-firmware-url-handler;1", &rv);
81 NS_ENSURE_SUCCESS(rv, rv);
85 NS_ENSURE_SUCCESS(rv, rv);
88 rv = NS_NewURI(getter_AddRefs(uri), resetURL);
89 NS_ENSURE_SUCCESS(rv, rv);
93 nsCString releaseNotesURL;
95 NS_ENSURE_SUCCESS(rv, rv);
97 rv = NS_NewURI(getter_AddRefs(uri), releaseNotesURL);
98 NS_ENSURE_SUCCESS(rv, rv);
108 *aCurrentFirmwareVersion = 0x01000000;
115 aCurrentFirmwareReadableVersion.AssignLiteral(
"1.0.0.0");
123 nsCOMPtr<nsIVariant> needsRecoveryModeVariant;
125 mDevice->GetPreference(NS_LITERAL_STRING(
"testing.firmware.needRecoveryMode"),
126 getter_AddRefs(needsRecoveryModeVariant));
127 NS_ENSURE_SUCCESS(rv, rv);
129 PRUint16 dataType = 0;
130 rv = needsRecoveryModeVariant->GetDataType(&dataType);
131 NS_ENSURE_SUCCESS(rv, rv);
134 if(dataType == nsIDataType::VTYPE_BOOL) {
135 rv = needsRecoveryModeVariant->GetAsBool(&needsRecoveryMode);
136 NS_ENSURE_SUCCESS(rv, rv);
150 aModelNumber.AssignLiteral(
"MOCKBOBMSC0001");
157 aModelVersion.AssignLiteral(
"MOCKBOBMSC0001/ZX");
164 aDeviceVendor.AssignLiteral(
"ACME Inc.");
170 PRUint32 aDeviceVendorID,
171 PRUint32 aDeviceProductID,
174 NS_ENSURE_ARG_POINTER(_retval);
177 nsCOMPtr<sbIDeviceProperties> properties;
178 nsresult rv = aDevice->GetProperties(getter_AddRefs(properties));
179 NS_ENSURE_SUCCESS(rv, rv);
182 rv = properties->GetVendorName(vendorName);
183 NS_ENSURE_SUCCESS(rv, rv);
187 if(!vendorName.EqualsLiteral(
"ACME Inc.")) {
202 PRBool canHandleDevice = PR_FALSE;
206 nsresult rv =
OnCanUpdate(aDevice, 0, 0, &canHandleDevice);
207 NS_ENSURE_SUCCESS(rv, rv);
209 if(canHandleDevice) {
211 NS_ENSURE_SUCCESS(rv, rv);
213 rv = Bind(aDevice, aListener);
214 NS_ENSURE_SUCCESS(rv, rv);
233 nsCString firmwareURL;
235 NS_ENSURE_SUCCESS(rv, rv);
238 NS_ENSURE_SUCCESS(rv, rv);
241 NS_ENSURE_SUCCESS(rv, rv);
245 NS_ENSURE_SUCCESS(rv, rv);
254 nsCOMPtr<nsIVariant> shouldFailVariant;
256 mDevice->GetPreference(NS_LITERAL_STRING(
"testing.firmware.update.fail"),
257 getter_AddRefs(shouldFailVariant));
258 NS_ENSURE_SUCCESS(rv, rv);
260 PRUint16 dataType = 0;
261 rv = shouldFailVariant->GetDataType(&dataType);
262 NS_ENSURE_SUCCESS(rv, rv);
264 PRBool shouldFail = PR_FALSE;
265 if(dataType == nsIDataType::VTYPE_BOOL) {
266 rv = shouldFailVariant->GetAsBool(&shouldFail);
267 NS_ENSURE_SUCCESS(rv, rv);
273 NS_ENSURE_SUCCESS(rv, rv);
279 nsCOMPtr<nsIFile> firmwareFile;
281 aFirmwareUpdate->GetFirmwareImageFile(getter_AddRefs(firmwareFile));
282 NS_ENSURE_SUCCESS(rv, rv);
284 nsCOMPtr<nsIInputStream> inputStream;
285 rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream), firmwareFile);
286 NS_ENSURE_SUCCESS(rv, rv);
288 nsCOMPtr<nsIInputStreamPump> inputStreamPump;
289 rv = NS_NewInputStreamPump(getter_AddRefs(inputStreamPump),
290 inputStream, -1, -1, 0, 0, PR_TRUE);
291 NS_ENSURE_SUCCESS(rv, rv);
294 NS_ENSURE_SUCCESS(rv, rv);
296 rv = inputStreamPump->AsyncRead(
this, firmwareFile);
297 NS_ENSURE_SUCCESS(rv, rv);
301 nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
303 if (!NS_ProcessNextEvent(thread, PR_FALSE)) {
304 PR_Sleep(PR_MillisecondsToInterval(100));
337 return NS_ERROR_NOT_IMPLEMENTED;
361 return NS_ERROR_NOT_IMPLEMENTED;
367 nsresult rv = NS_ERROR_UNEXPECTED;
373 NS_ENSURE_SUCCESS(rv, rv);
378 NS_WARNING(
"No code!");
392 NS_ENSURE_SUCCESS(rv, rv);
394 nsCOMPtr<nsIDOMDocument> document;
396 NS_ENSURE_SUCCESS(rv, rv);
397 NS_ENSURE_TRUE(document, NS_ERROR_FAILURE);
399 nsCOMPtr<nsIDOMNodeList> rootNodeList;
400 rv = document->GetElementsByTagName(NS_LITERAL_STRING(
"firmware"),
401 getter_AddRefs(rootNodeList));
402 NS_ENSURE_SUCCESS(rv, rv);
404 PRUint32 rootNodeListLength = 0;
405 rv = rootNodeList->GetLength(&rootNodeListLength);
406 NS_ENSURE_SUCCESS(rv, rv);
409 NS_ENSURE_TRUE(rootNodeListLength == 1, NS_ERROR_FAILURE);
411 nsCOMPtr<nsIDOMNode> rootNode;
412 rv = rootNodeList->Item(0, getter_AddRefs(rootNode));
413 NS_ENSURE_SUCCESS(rv, rv);
415 nsCOMPtr<nsIDOMNodeList> childNodes;
416 rv = rootNode->GetChildNodes(getter_AddRefs(childNodes));
417 NS_ENSURE_SUCCESS(rv, rv);
419 PRUint32 childNodeListLength = 0;
420 rv = childNodes->GetLength(&childNodeListLength);
421 NS_ENSURE_SUCCESS(rv, rv);
423 for(PRUint32
i = 0;
i < childNodeListLength; ++
i) {
424 nsCOMPtr<nsIDOMNode> domNode;
425 rv = childNodes->Item(
i, getter_AddRefs(domNode));
426 NS_ENSURE_SUCCESS(rv, rv);
428 nsCOMPtr<nsIDOMElement>
element = do_QueryInterface(domNode, &rv);
434 rv = element->GetTagName(tagName);
435 NS_ENSURE_SUCCESS(rv, rv);
438 rv = element->GetAttribute(NS_LITERAL_STRING(
"value"), value);
445 if(tagName.EqualsLiteral(
"version")) {
450 else if(tagName.EqualsLiteral(
"location")) {
451 nsCOMPtr<nsIURI>
uri;
453 getter_AddRefs(uri));
454 NS_ENSURE_SUCCESS(rv, rv);
462 nsCString supportURL;
464 NS_ENSURE_SUCCESS(rv, rv);
466 nsCOMPtr<nsIURI>
uri;
467 nsresult rv = NS_NewURI(getter_AddRefs(uri), supportURL);
468 NS_ENSURE_SUCCESS(rv, rv);
474 nsCString registerURL;
476 NS_ENSURE_SUCCESS(rv, rv);
478 rv = NS_NewURI(getter_AddRefs(uri), registerURL);
479 NS_ENSURE_SUCCESS(rv, rv);
487 NS_ENSURE_SUCCESS(rv, rv);
489 rv = NS_NewURI(getter_AddRefs(uri), resetURL);
490 NS_ENSURE_SUCCESS(rv, rv);
497 nsCOMPtr<nsIVariant> needsRecoveryModeVariant;
498 rv =
mDevice->GetPreference(NS_LITERAL_STRING(
"testing.firmware.needRecoveryMode"),
499 getter_AddRefs(needsRecoveryModeVariant));
500 NS_ENSURE_SUCCESS(rv, rv);
502 PRUint16 dataType = 0;
503 rv = needsRecoveryModeVariant->GetDataType(&dataType);
504 NS_ENSURE_SUCCESS(rv, rv);
507 if(dataType == nsIDataType::VTYPE_BOOL) {
508 rv = needsRecoveryModeVariant->GetAsBool(&needsRecoveryMode);
509 NS_ENSURE_SUCCESS(rv, rv);
519 nsCOMPtr<nsIVariant> shouldFailVariant;
520 rv =
mDevice->GetPreference(NS_LITERAL_STRING(
"testing.firmware.cfu.fail"),
521 getter_AddRefs(shouldFailVariant));
522 NS_ENSURE_SUCCESS(rv, rv);
524 PRUint16 dataType = 0;
525 rv = shouldFailVariant->GetDataType(&dataType);
526 NS_ENSURE_SUCCESS(rv, rv);
528 PRBool shouldFail = PR_FALSE;
529 if(dataType == nsIDataType::VTYPE_BOOL) {
530 rv = shouldFailVariant->GetAsBool(&shouldFail);
531 NS_ENSURE_SUCCESS(rv, rv);
537 NS_ENSURE_SUCCESS(rv, rv);
539 return NS_ERROR_FAILURE;
549 nsCOMPtr<nsIVariant>
data =
554 NS_ENSURE_SUCCESS(rv, rv);
563 sbMockDeviceFirmwareHandler::OnStartRequest(nsIRequest *aRequest,
568 NS_ENSURE_SUCCESS(rv, rv);
574 sbMockDeviceFirmwareHandler::OnDataAvailable(nsIRequest *aRequest,
576 nsIInputStream *aStream,
580 nsresult rv = NS_ERROR_UNEXPECTED;
581 nsCOMPtr<nsIFile> firmwareFile = do_QueryInterface(aContext, &rv);
582 NS_ENSURE_SUCCESS(rv, rv);
584 PRUint32 availableBytes = 0;
585 rv = aStream->Available(&availableBytes);
586 NS_ENSURE_SUCCESS(rv, rv);
589 reinterpret_cast<char *
>(NS_Alloc(availableBytes *
sizeof(
char)));
590 NS_ENSURE_TRUE(buffer, NS_ERROR_OUT_OF_MEMORY);
593 rv = aStream->Read(buffer, availableBytes, &readBytes);
596 NS_ENSURE_SUCCESS(rv, rv);
598 PRInt64 fileSize = 0;
599 rv = firmwareFile->GetFileSize(&fileSize);
600 NS_ENSURE_SUCCESS(rv, rv);
602 PRUint32 progress =
static_cast<PRUint32
>(aOffset * 100 / fileSize);
606 NS_ENSURE_SUCCESS(rv, rv);
608 PR_Sleep(PR_MillisecondsToInterval(50));
611 nsCOMPtr<nsIVariant> shouldFailVariant;
613 mDevice->GetPreference(NS_LITERAL_STRING(
"testing.firmware.write.fail"),
614 getter_AddRefs(shouldFailVariant));
615 NS_ENSURE_SUCCESS(rv, rv);
617 PRUint16 dataType = 0;
618 rv = shouldFailVariant->GetDataType(&dataType);
619 NS_ENSURE_SUCCESS(rv, rv);
621 PRBool shouldFail = PR_FALSE;
622 if(dataType == nsIDataType::VTYPE_BOOL) {
623 rv = shouldFailVariant->GetAsBool(&shouldFail);
624 NS_ENSURE_SUCCESS(rv, rv);
630 NS_ENSURE_SUCCESS(rv, rv);
632 return NS_ERROR_FAILURE;
640 sbMockDeviceFirmwareHandler::OnStopRequest(nsIRequest *aRequest,
642 nsresult aResultCode)
646 NS_ENSURE_SUCCESS(rv, rv);
649 NS_ENSURE_SUCCESS(rv, rv);
652 NS_ENSURE_SUCCESS(rv, rv);
nsresult HandleRefreshInfoRequest()
function readBytes(inputStream, count)
virtual nsresult OnGetCurrentFirmwareVersion(PRUint32 *aCurrentFirmwareVersion)
readonly attribute boolean needsRecoveryMode
Whether the device needs to be in recovery mode to have its firmware updated.
virtual nsresult OnGetDeviceModelNumber(nsAString &aModelNumber)
nsCOMPtr< nsIURI > mSupportLocation
nsCOMPtr< sbIDevice > mDevice
const unsigned long EVENT_FIRMWARE_WRITE_END
Firmware write to device start.
const unsigned long EVENT_FIRMWARE_CFU_END
Check for update end.
nsCOMPtr< nsIURI > mFirmwareLocation
Songbird Variant Utility Definitions.
nsresult SetState(handlerstate_t aState)
Get internal state.
nsresult CheckForError(const nsresult &aResult, PRUint32 aEventType, nsIVariant *aData=nsnull)
Check nsresult value, send error event if nsresult value is an error.
PRPackedBool mNeedsRecoveryMode
const unsigned long EVENT_FIRMWARE_UPDATE_END
Firmware update end.
nsCOMPtr< nsIURI > mResetInstructionsLocation
const unsigned long EVENT_FIRMWARE_WRITE_START
Firmware write to device start.
PRUint32 mFirmwareVersion
virtual nsresult OnRecover(sbIDeviceFirmwareUpdate *aFirmwareUpdate)
virtual nsresult OnRebind(sbIDevice *aDevice, sbIDeviceEventListener *aListener, PRBool *_retval)
handlerstate_t GetState()
Set internal state.
const unsigned long EVENT_FIRMWARE_CFU_START
Check for update start.
nsresult SendDeviceEvent(sbIDeviceEvent *aEvent, PRBool aAsync=PR_TRUE)
Send a device event.
virtual nsresult OnCancel()
const unsigned long EVENT_FIRMWARE_CFU_ERROR
Check for update error.
virtual nsresult OnCanUpdate(sbIDevice *aDevice, PRUint32 aDeviceVendorID, PRUint32 aDeviceProductID, PRBool *_retval)
const unsigned long EVENT_FIRMWARE_WRITE_PROGRESS
Firmware write to device start.
nsCOMPtr< sbPIMockFirmwareHandlerURLService > mHandlerURLService
const unsigned long EVENT_FIRMWARE_UPDATE_ERROR
Firmware update error.
virtual nsresult OnInit()
nsCOMPtr< nsIXMLHttpRequest > mXMLHttpRequest
virtual nsresult OnVerifyUpdate(sbIDeviceFirmwareUpdate *aFirmwareUpdate)
#define SB_DEVICE_FIRMWARE_HANLDER_REGISTERSELF_IMPL(_name, _desc)
NS_IMPL_ISUPPORTS_INHERITED1(sbMockDeviceFirmwareHandler, sbBaseDeviceFirmwareHandler, nsIStreamListener) SB_DEVICE_FIRMWARE_HANLDER_REGISTERSELF_IMPL(sbMockDeviceFirmwareHandler
PRPackedBool mRecoveryMode
nsCOMPtr< nsIURI > mRegisterLocation
virtual nsresult OnGetRecoveryMode(PRBool *aRecoveryMode)
nsresult CreateProxiedURI(const nsACString &aURISpec, nsIURI **aURI)
Create an nsIURI from a spec string (e.g. http://some.url.com/path) in a thread-safe manner...
virtual nsresult OnGetDeviceModelVersion(nsAString &aModelVersion)
restoreHistoryPrecursor aCount
nsCOMPtr< nsIURI > mReleaseNotesLocation
_getSelectedPageStyle s i
virtual nsresult OnGetDeviceVendor(nsAString &aDeviceVendor)
virtual nsresult OnGetCurrentFirmwareReadableVersion(nsAString &aCurrentFirmwareReadableVersion)
nsString mReadableFirmwareVersion
nsresult SendHttpRequest(const nsACString &aMethod, const nsACString &aUrl, const nsAString &aUsername=EmptyString(), const nsAString &aPassword=EmptyString(), const nsACString &aContentType=EmptyCString(), nsIVariant *aRequestBody=nsnull)
Send an HTTP request.
const unsigned long EVENT_FIRMWARE_UPDATE_START
Firmware update start.
const unsigned long EVENT_FIRMWARE_WRITE_ERROR
Firmware write error.
virtual nsresult OnVerifyDevice()
virtual nsresult OnHttpRequestCompleted()
virtual nsresult OnUpdate(sbIDeviceFirmwareUpdate *aFirmwareUpdate)
virtual nsresult OnRefreshInfo()