michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: #include "DeviceStorageRequestChild.h" michael@0: #include "DeviceStorageFileDescriptor.h" michael@0: #include "nsDeviceStorage.h" michael@0: #include "nsDOMFile.h" michael@0: #include "mozilla/dom/ipc/Blob.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: namespace devicestorage { michael@0: michael@0: DeviceStorageRequestChild::DeviceStorageRequestChild() michael@0: : mCallback(nullptr) michael@0: { michael@0: MOZ_COUNT_CTOR(DeviceStorageRequestChild); michael@0: } michael@0: michael@0: DeviceStorageRequestChild::DeviceStorageRequestChild(DOMRequest* aRequest, michael@0: DeviceStorageFile* aDSFile) michael@0: : mRequest(aRequest) michael@0: , mDSFile(aDSFile) michael@0: , mCallback(nullptr) michael@0: { michael@0: MOZ_ASSERT(aRequest); michael@0: MOZ_ASSERT(aDSFile); michael@0: MOZ_COUNT_CTOR(DeviceStorageRequestChild); michael@0: } michael@0: michael@0: DeviceStorageRequestChild::DeviceStorageRequestChild(DOMRequest* aRequest, michael@0: DeviceStorageFile* aDSFile, michael@0: DeviceStorageFileDescriptor* aDSFileDescriptor) michael@0: : mRequest(aRequest) michael@0: , mDSFile(aDSFile) michael@0: , mDSFileDescriptor(aDSFileDescriptor) michael@0: , mCallback(nullptr) michael@0: { michael@0: MOZ_ASSERT(aRequest); michael@0: MOZ_ASSERT(aDSFile); michael@0: MOZ_ASSERT(aDSFileDescriptor); michael@0: MOZ_COUNT_CTOR(DeviceStorageRequestChild); michael@0: } michael@0: michael@0: DeviceStorageRequestChild::~DeviceStorageRequestChild() { michael@0: MOZ_COUNT_DTOR(DeviceStorageRequestChild); michael@0: } michael@0: michael@0: bool michael@0: DeviceStorageRequestChild:: michael@0: Recv__delete__(const DeviceStorageResponseValue& aValue) michael@0: { michael@0: if (mCallback) { michael@0: mCallback->RequestComplete(); michael@0: mCallback = nullptr; michael@0: } michael@0: michael@0: nsCOMPtr window = mRequest->GetOwner(); michael@0: if (!window) { michael@0: return true; michael@0: } michael@0: michael@0: switch (aValue.type()) { michael@0: michael@0: case DeviceStorageResponseValue::TErrorResponse: michael@0: { michael@0: ErrorResponse r = aValue; michael@0: mRequest->FireError(r.error()); michael@0: break; michael@0: } michael@0: michael@0: case DeviceStorageResponseValue::TSuccessResponse: michael@0: { michael@0: nsString fullPath; michael@0: mDSFile->GetFullPath(fullPath); michael@0: AutoJSContext cx; michael@0: JS::Rooted result(cx, michael@0: StringToJsval(window, fullPath)); michael@0: mRequest->FireSuccess(result); michael@0: break; michael@0: } michael@0: michael@0: case DeviceStorageResponseValue::TFileDescriptorResponse: michael@0: { michael@0: FileDescriptorResponse r = aValue; michael@0: michael@0: nsString fullPath; michael@0: mDSFile->GetFullPath(fullPath); michael@0: AutoJSContext cx; michael@0: JS::Rooted result(cx, michael@0: StringToJsval(window, fullPath)); michael@0: michael@0: mDSFileDescriptor->mDSFile = mDSFile; michael@0: mDSFileDescriptor->mFileDescriptor = r.fileDescriptor(); michael@0: mRequest->FireSuccess(result); michael@0: break; michael@0: } michael@0: michael@0: case DeviceStorageResponseValue::TBlobResponse: michael@0: { michael@0: BlobResponse r = aValue; michael@0: BlobChild* actor = static_cast(r.blobChild()); michael@0: nsCOMPtr blob = actor->GetBlob(); michael@0: michael@0: nsCOMPtr file = do_QueryInterface(blob); michael@0: AutoJSContext cx; michael@0: JS::Rooted result(cx, michael@0: InterfaceToJsval(window, file, &NS_GET_IID(nsIDOMFile))); michael@0: mRequest->FireSuccess(result); michael@0: break; michael@0: } michael@0: michael@0: case DeviceStorageResponseValue::TFreeSpaceStorageResponse: michael@0: { michael@0: FreeSpaceStorageResponse r = aValue; michael@0: AutoJSContext cx; michael@0: JS::Rooted result(cx, JS_NumberValue(double(r.freeBytes()))); michael@0: mRequest->FireSuccess(result); michael@0: break; michael@0: } michael@0: michael@0: case DeviceStorageResponseValue::TUsedSpaceStorageResponse: michael@0: { michael@0: UsedSpaceStorageResponse r = aValue; michael@0: AutoJSContext cx; michael@0: JS::Rooted result(cx, JS_NumberValue(double(r.usedBytes()))); michael@0: mRequest->FireSuccess(result); michael@0: break; michael@0: } michael@0: michael@0: case DeviceStorageResponseValue::TAvailableStorageResponse: michael@0: { michael@0: AvailableStorageResponse r = aValue; michael@0: AutoJSContext cx; michael@0: JS::Rooted result( michael@0: cx, StringToJsval(window, r.mountState())); michael@0: mRequest->FireSuccess(result); michael@0: break; michael@0: } michael@0: michael@0: case DeviceStorageResponseValue::TStorageStatusResponse: michael@0: { michael@0: StorageStatusResponse r = aValue; michael@0: AutoJSContext cx; michael@0: JS::Rooted result( michael@0: cx, StringToJsval(window, r.storageStatus())); michael@0: mRequest->FireSuccess(result); michael@0: break; michael@0: } michael@0: michael@0: case DeviceStorageResponseValue::TFormatStorageResponse: michael@0: { michael@0: FormatStorageResponse r = aValue; michael@0: AutoJSContext cx; michael@0: JS::Rooted result( michael@0: cx, StringToJsval(window, r.mountState())); michael@0: mRequest->FireSuccess(result); michael@0: break; michael@0: } michael@0: michael@0: case DeviceStorageResponseValue::TMountStorageResponse: michael@0: { michael@0: MountStorageResponse r = aValue; michael@0: AutoJSContext cx; michael@0: JS::Rooted result( michael@0: cx, StringToJsval(window, r.storageStatus())); michael@0: mRequest->FireSuccess(result); michael@0: break; michael@0: } michael@0: michael@0: case DeviceStorageResponseValue::TUnmountStorageResponse: michael@0: { michael@0: UnmountStorageResponse r = aValue; michael@0: AutoJSContext cx; michael@0: JS::Rooted result( michael@0: cx, StringToJsval(window, r.storageStatus())); michael@0: mRequest->FireSuccess(result); michael@0: break; michael@0: } michael@0: michael@0: case DeviceStorageResponseValue::TEnumerationResponse: michael@0: { michael@0: EnumerationResponse r = aValue; michael@0: nsDOMDeviceStorageCursor* cursor michael@0: = static_cast(mRequest.get()); michael@0: michael@0: uint32_t count = r.paths().Length(); michael@0: for (uint32_t i = 0; i < count; i++) { michael@0: nsRefPtr dsf michael@0: = new DeviceStorageFile(r.type(), r.paths()[i].storageName(), michael@0: r.rootdir(), r.paths()[i].name()); michael@0: cursor->mFiles.AppendElement(dsf); michael@0: } michael@0: michael@0: nsRefPtr event = new ContinueCursorEvent(cursor); michael@0: event->Continue(); michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: { michael@0: NS_RUNTIMEABORT("not reached"); michael@0: break; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: DeviceStorageRequestChild:: michael@0: SetCallback(DeviceStorageRequestChildCallback *aCallback) michael@0: { michael@0: mCallback = aCallback; michael@0: } michael@0: michael@0: } // namespace devicestorage michael@0: } // namespace dom michael@0: } // namespace mozilla