2012-06-19 16:14:39 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
|
|
#include "DeviceStorageRequestChild.h"
|
2014-01-16 17:01:27 -08:00
|
|
|
#include "DeviceStorageFileDescriptor.h"
|
2012-06-19 16:14:39 -07:00
|
|
|
#include "nsDeviceStorage.h"
|
|
|
|
#include "nsDOMFile.h"
|
2014-09-17 16:36:01 -07:00
|
|
|
#include "mozilla/dom/ipc/Blob.h"
|
2012-06-19 16:14:39 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace devicestorage {
|
|
|
|
|
|
|
|
DeviceStorageRequestChild::DeviceStorageRequestChild()
|
2012-10-04 13:40:45 -07:00
|
|
|
: mCallback(nullptr)
|
2012-06-19 16:14:39 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(DeviceStorageRequestChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestChild::DeviceStorageRequestChild(DOMRequest* aRequest,
|
2014-01-16 17:01:27 -08:00
|
|
|
DeviceStorageFile* aDSFile)
|
2012-06-19 16:14:39 -07:00
|
|
|
: mRequest(aRequest)
|
2014-01-16 17:01:27 -08:00
|
|
|
, mDSFile(aDSFile)
|
2012-10-04 13:40:45 -07:00
|
|
|
, mCallback(nullptr)
|
2012-06-19 16:14:39 -07:00
|
|
|
{
|
2014-01-16 17:01:27 -08:00
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
MOZ_ASSERT(aDSFile);
|
|
|
|
MOZ_COUNT_CTOR(DeviceStorageRequestChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestChild::DeviceStorageRequestChild(DOMRequest* aRequest,
|
|
|
|
DeviceStorageFile* aDSFile,
|
|
|
|
DeviceStorageFileDescriptor* aDSFileDescriptor)
|
|
|
|
: mRequest(aRequest)
|
|
|
|
, mDSFile(aDSFile)
|
|
|
|
, mDSFileDescriptor(aDSFileDescriptor)
|
|
|
|
, mCallback(nullptr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
MOZ_ASSERT(aDSFile);
|
|
|
|
MOZ_ASSERT(aDSFileDescriptor);
|
2012-06-19 16:14:39 -07:00
|
|
|
MOZ_COUNT_CTOR(DeviceStorageRequestChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestChild::~DeviceStorageRequestChild() {
|
|
|
|
MOZ_COUNT_DTOR(DeviceStorageRequestChild);
|
|
|
|
}
|
2012-08-09 15:41:18 -07:00
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
bool
|
2013-08-02 16:35:16 -07:00
|
|
|
DeviceStorageRequestChild::
|
|
|
|
Recv__delete__(const DeviceStorageResponseValue& aValue)
|
2012-06-19 16:14:39 -07:00
|
|
|
{
|
2012-10-04 13:40:45 -07:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback->RequestComplete();
|
|
|
|
mCallback = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-07 22:44:22 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = mRequest->GetOwner();
|
|
|
|
if (!window) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
switch (aValue.type()) {
|
|
|
|
|
|
|
|
case DeviceStorageResponseValue::TErrorResponse:
|
|
|
|
{
|
|
|
|
ErrorResponse r = aValue;
|
2012-08-08 14:07:39 -07:00
|
|
|
mRequest->FireError(r.error());
|
2012-06-19 16:14:39 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageResponseValue::TSuccessResponse:
|
|
|
|
{
|
2013-08-02 16:34:59 -07:00
|
|
|
nsString fullPath;
|
2014-01-16 17:01:27 -08:00
|
|
|
mDSFile->GetFullPath(fullPath);
|
|
|
|
AutoJSContext cx;
|
2014-06-25 15:35:36 -07:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-07 22:44:22 -07:00
|
|
|
StringToJsval(window, fullPath, &result);
|
2014-01-16 17:01:27 -08:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageResponseValue::TFileDescriptorResponse:
|
|
|
|
{
|
|
|
|
FileDescriptorResponse r = aValue;
|
|
|
|
|
|
|
|
nsString fullPath;
|
|
|
|
mDSFile->GetFullPath(fullPath);
|
2013-05-17 18:48:25 -07:00
|
|
|
AutoJSContext cx;
|
2014-06-25 15:35:36 -07:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-07 22:44:22 -07:00
|
|
|
StringToJsval(window, fullPath, &result);
|
2014-01-16 17:01:27 -08:00
|
|
|
|
|
|
|
mDSFileDescriptor->mDSFile = mDSFile;
|
|
|
|
mDSFileDescriptor->mFileDescriptor = r.fileDescriptor();
|
2012-08-08 14:07:39 -07:00
|
|
|
mRequest->FireSuccess(result);
|
2012-06-19 16:14:39 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageResponseValue::TBlobResponse:
|
|
|
|
{
|
|
|
|
BlobResponse r = aValue;
|
2012-08-09 15:41:18 -07:00
|
|
|
BlobChild* actor = static_cast<BlobChild*>(r.blobChild());
|
|
|
|
nsCOMPtr<nsIDOMBlob> blob = actor->GetBlob();
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-09-28 22:30:52 -07:00
|
|
|
nsCOMPtr<nsIDOMFile> file = do_QueryInterface(blob);
|
2013-05-17 18:48:25 -07:00
|
|
|
AutoJSContext cx;
|
|
|
|
JS::Rooted<JS::Value> result(cx,
|
2014-07-07 22:44:22 -07:00
|
|
|
InterfaceToJsval(window, file, &NS_GET_IID(nsIDOMFile)));
|
2012-07-31 12:28:23 -07:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-01-25 11:05:24 -08:00
|
|
|
case DeviceStorageResponseValue::TFreeSpaceStorageResponse:
|
2012-07-31 12:28:23 -07:00
|
|
|
{
|
2013-01-25 11:05:24 -08:00
|
|
|
FreeSpaceStorageResponse r = aValue;
|
2013-05-17 18:48:25 -07:00
|
|
|
AutoJSContext cx;
|
|
|
|
JS::Rooted<JS::Value> result(cx, JS_NumberValue(double(r.freeBytes())));
|
2013-01-25 11:05:24 -08:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
2012-07-31 12:28:23 -07:00
|
|
|
|
2013-01-25 11:05:24 -08:00
|
|
|
case DeviceStorageResponseValue::TUsedSpaceStorageResponse:
|
|
|
|
{
|
|
|
|
UsedSpaceStorageResponse r = aValue;
|
2013-05-17 18:48:25 -07:00
|
|
|
AutoJSContext cx;
|
|
|
|
JS::Rooted<JS::Value> result(cx, JS_NumberValue(double(r.usedBytes())));
|
2013-01-25 11:05:24 -08:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageResponseValue::TAvailableStorageResponse:
|
|
|
|
{
|
|
|
|
AvailableStorageResponse r = aValue;
|
2013-05-17 18:48:25 -07:00
|
|
|
AutoJSContext cx;
|
2014-06-25 15:35:36 -07:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-07 22:44:22 -07:00
|
|
|
StringToJsval(window, r.mountState(), &result);
|
2013-09-05 23:11:58 -07:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-18 18:48:05 -08:00
|
|
|
case DeviceStorageResponseValue::TStorageStatusResponse:
|
|
|
|
{
|
|
|
|
StorageStatusResponse r = aValue;
|
|
|
|
AutoJSContext cx;
|
2014-06-25 15:35:36 -07:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-07 22:44:22 -07:00
|
|
|
StringToJsval(window, r.storageStatus(), &result);
|
2014-02-18 18:48:05 -08:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-05 23:11:58 -07:00
|
|
|
case DeviceStorageResponseValue::TFormatStorageResponse:
|
|
|
|
{
|
|
|
|
FormatStorageResponse r = aValue;
|
|
|
|
AutoJSContext cx;
|
2014-06-25 15:35:36 -07:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-07 22:44:22 -07:00
|
|
|
StringToJsval(window, r.mountState(), &result);
|
2012-08-08 14:07:39 -07:00
|
|
|
mRequest->FireSuccess(result);
|
2012-06-19 16:14:39 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-03-04 03:24:19 -08:00
|
|
|
case DeviceStorageResponseValue::TMountStorageResponse:
|
|
|
|
{
|
|
|
|
MountStorageResponse r = aValue;
|
|
|
|
AutoJSContext cx;
|
2014-06-25 15:35:36 -07:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-07 22:44:22 -07:00
|
|
|
StringToJsval(window, r.storageStatus(), &result);
|
2014-03-04 03:24:19 -08:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageResponseValue::TUnmountStorageResponse:
|
|
|
|
{
|
|
|
|
UnmountStorageResponse r = aValue;
|
|
|
|
AutoJSContext cx;
|
2014-06-25 15:35:36 -07:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-07 22:44:22 -07:00
|
|
|
StringToJsval(window, r.storageStatus(), &result);
|
2014-03-04 03:24:19 -08:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:14:39 -07:00
|
|
|
case DeviceStorageResponseValue::TEnumerationResponse:
|
|
|
|
{
|
|
|
|
EnumerationResponse r = aValue;
|
2013-08-02 16:35:16 -07:00
|
|
|
nsDOMDeviceStorageCursor* cursor
|
|
|
|
= static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
|
2012-06-19 16:14:39 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = r.paths().Length();
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2013-08-02 16:35:16 -07:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf
|
|
|
|
= new DeviceStorageFile(r.type(), r.paths()[i].storageName(),
|
|
|
|
r.rootdir(), r.paths()[i].name());
|
2012-06-19 16:14:39 -07:00
|
|
|
cursor->mFiles.AppendElement(dsf);
|
|
|
|
}
|
|
|
|
|
2014-03-17 17:23:03 -07:00
|
|
|
nsRefPtr<ContinueCursorEvent> event = new ContinueCursorEvent(cursor);
|
2012-10-04 13:40:45 -07:00
|
|
|
event->Continue();
|
2012-06-19 16:14:39 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
NS_RUNTIMEABORT("not reached");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-04 13:40:45 -07:00
|
|
|
void
|
2013-08-02 16:35:16 -07:00
|
|
|
DeviceStorageRequestChild::
|
|
|
|
SetCallback(DeviceStorageRequestChildCallback *aCallback)
|
2012-10-04 13:40:45 -07:00
|
|
|
{
|
|
|
|
mCallback = aCallback;
|
|
|
|
}
|
2012-06-19 16:14:39 -07:00
|
|
|
|
|
|
|
} // namespace devicestorage
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|