2012-06-20 03:14:39 +04: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-17 05:01:27 +04:00
|
|
|
#include "DeviceStorageFileDescriptor.h"
|
2012-06-20 03:14:39 +04:00
|
|
|
#include "nsDeviceStorage.h"
|
2014-10-07 21:16:11 +04:00
|
|
|
#include "nsDOMFile.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "mozilla/dom/ipc/BlobChild.h"
|
2012-06-20 03:14:39 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace devicestorage {
|
|
|
|
|
|
|
|
DeviceStorageRequestChild::DeviceStorageRequestChild()
|
2012-10-05 00:40:45 +04:00
|
|
|
: mCallback(nullptr)
|
2012-06-20 03:14:39 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(DeviceStorageRequestChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestChild::DeviceStorageRequestChild(DOMRequest* aRequest,
|
2014-01-17 05:01:27 +04:00
|
|
|
DeviceStorageFile* aDSFile)
|
2012-06-20 03:14:39 +04:00
|
|
|
: mRequest(aRequest)
|
2014-01-17 05:01:27 +04:00
|
|
|
, mDSFile(aDSFile)
|
2012-10-05 00:40:45 +04:00
|
|
|
, mCallback(nullptr)
|
2012-06-20 03:14:39 +04:00
|
|
|
{
|
2014-01-17 05:01:27 +04: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-20 03:14:39 +04:00
|
|
|
MOZ_COUNT_CTOR(DeviceStorageRequestChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestChild::~DeviceStorageRequestChild() {
|
|
|
|
MOZ_COUNT_DTOR(DeviceStorageRequestChild);
|
|
|
|
}
|
2012-08-10 02:41:18 +04:00
|
|
|
|
2012-06-20 03:14:39 +04:00
|
|
|
bool
|
2013-08-03 03:35:16 +04:00
|
|
|
DeviceStorageRequestChild::
|
|
|
|
Recv__delete__(const DeviceStorageResponseValue& aValue)
|
2012-06-20 03:14:39 +04:00
|
|
|
{
|
2012-10-05 00:40:45 +04:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback->RequestComplete();
|
|
|
|
mCallback = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-08 09:44:22 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = mRequest->GetOwner();
|
|
|
|
if (!window) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-20 03:14:39 +04:00
|
|
|
switch (aValue.type()) {
|
|
|
|
|
|
|
|
case DeviceStorageResponseValue::TErrorResponse:
|
|
|
|
{
|
|
|
|
ErrorResponse r = aValue;
|
2012-08-09 01:07:39 +04:00
|
|
|
mRequest->FireError(r.error());
|
2012-06-20 03:14:39 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageResponseValue::TSuccessResponse:
|
|
|
|
{
|
2013-08-03 03:34:59 +04:00
|
|
|
nsString fullPath;
|
2014-01-17 05:01:27 +04:00
|
|
|
mDSFile->GetFullPath(fullPath);
|
|
|
|
AutoJSContext cx;
|
2014-06-26 02:35:36 +04:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-08 09:44:22 +04:00
|
|
|
StringToJsval(window, fullPath, &result);
|
2014-01-17 05:01:27 +04:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageResponseValue::TFileDescriptorResponse:
|
|
|
|
{
|
|
|
|
FileDescriptorResponse r = aValue;
|
|
|
|
|
|
|
|
nsString fullPath;
|
|
|
|
mDSFile->GetFullPath(fullPath);
|
2013-05-18 05:48:25 +04:00
|
|
|
AutoJSContext cx;
|
2014-06-26 02:35:36 +04:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-08 09:44:22 +04:00
|
|
|
StringToJsval(window, fullPath, &result);
|
2014-01-17 05:01:27 +04:00
|
|
|
|
|
|
|
mDSFileDescriptor->mDSFile = mDSFile;
|
|
|
|
mDSFileDescriptor->mFileDescriptor = r.fileDescriptor();
|
2012-08-09 01:07:39 +04:00
|
|
|
mRequest->FireSuccess(result);
|
2012-06-20 03:14:39 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageResponseValue::TBlobResponse:
|
|
|
|
{
|
|
|
|
BlobResponse r = aValue;
|
2012-08-10 02:41:18 +04:00
|
|
|
BlobChild* actor = static_cast<BlobChild*>(r.blobChild());
|
2014-10-08 20:15:22 +04:00
|
|
|
nsRefPtr<DOMFileImpl> bloblImpl = actor->GetBlobImpl();
|
|
|
|
nsRefPtr<DOMFile> blob = new DOMFile(mRequest->GetParentObject(), bloblImpl);
|
2012-06-20 03:14:39 +04:00
|
|
|
|
2013-05-18 05:48:25 +04:00
|
|
|
AutoJSContext cx;
|
2014-10-08 20:15:22 +04:00
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(cx, blob->WrapObject(cx));
|
|
|
|
MOZ_ASSERT(obj);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> result(cx, JS::ObjectValue(*obj));
|
2012-07-31 23:28:23 +04:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-01-25 23:05:24 +04:00
|
|
|
case DeviceStorageResponseValue::TFreeSpaceStorageResponse:
|
2012-07-31 23:28:23 +04:00
|
|
|
{
|
2013-01-25 23:05:24 +04:00
|
|
|
FreeSpaceStorageResponse r = aValue;
|
2013-05-18 05:48:25 +04:00
|
|
|
AutoJSContext cx;
|
|
|
|
JS::Rooted<JS::Value> result(cx, JS_NumberValue(double(r.freeBytes())));
|
2013-01-25 23:05:24 +04:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
2012-07-31 23:28:23 +04:00
|
|
|
|
2013-01-25 23:05:24 +04:00
|
|
|
case DeviceStorageResponseValue::TUsedSpaceStorageResponse:
|
|
|
|
{
|
|
|
|
UsedSpaceStorageResponse r = aValue;
|
2013-05-18 05:48:25 +04:00
|
|
|
AutoJSContext cx;
|
|
|
|
JS::Rooted<JS::Value> result(cx, JS_NumberValue(double(r.usedBytes())));
|
2013-01-25 23:05:24 +04:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageResponseValue::TAvailableStorageResponse:
|
|
|
|
{
|
|
|
|
AvailableStorageResponse r = aValue;
|
2013-05-18 05:48:25 +04:00
|
|
|
AutoJSContext cx;
|
2014-06-26 02:35:36 +04:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-08 09:44:22 +04:00
|
|
|
StringToJsval(window, r.mountState(), &result);
|
2013-09-06 10:11:58 +04:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-19 06:48:05 +04:00
|
|
|
case DeviceStorageResponseValue::TStorageStatusResponse:
|
|
|
|
{
|
|
|
|
StorageStatusResponse r = aValue;
|
|
|
|
AutoJSContext cx;
|
2014-06-26 02:35:36 +04:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-08 09:44:22 +04:00
|
|
|
StringToJsval(window, r.storageStatus(), &result);
|
2014-02-19 06:48:05 +04:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-06 10:11:58 +04:00
|
|
|
case DeviceStorageResponseValue::TFormatStorageResponse:
|
|
|
|
{
|
|
|
|
FormatStorageResponse r = aValue;
|
|
|
|
AutoJSContext cx;
|
2014-06-26 02:35:36 +04:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-08 09:44:22 +04:00
|
|
|
StringToJsval(window, r.mountState(), &result);
|
2012-08-09 01:07:39 +04:00
|
|
|
mRequest->FireSuccess(result);
|
2012-06-20 03:14:39 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-03-04 15:24:19 +04:00
|
|
|
case DeviceStorageResponseValue::TMountStorageResponse:
|
|
|
|
{
|
|
|
|
MountStorageResponse r = aValue;
|
|
|
|
AutoJSContext cx;
|
2014-06-26 02:35:36 +04:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-08 09:44:22 +04:00
|
|
|
StringToJsval(window, r.storageStatus(), &result);
|
2014-03-04 15:24:19 +04:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageResponseValue::TUnmountStorageResponse:
|
|
|
|
{
|
|
|
|
UnmountStorageResponse r = aValue;
|
|
|
|
AutoJSContext cx;
|
2014-06-26 02:35:36 +04:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2014-07-08 09:44:22 +04:00
|
|
|
StringToJsval(window, r.storageStatus(), &result);
|
2014-03-04 15:24:19 +04:00
|
|
|
mRequest->FireSuccess(result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-06-20 03:14:39 +04:00
|
|
|
case DeviceStorageResponseValue::TEnumerationResponse:
|
|
|
|
{
|
|
|
|
EnumerationResponse r = aValue;
|
2013-08-03 03:35:16 +04:00
|
|
|
nsDOMDeviceStorageCursor* cursor
|
|
|
|
= static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
|
2012-06-20 03:14:39 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count = r.paths().Length();
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2013-08-03 03:35:16 +04:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf
|
|
|
|
= new DeviceStorageFile(r.type(), r.paths()[i].storageName(),
|
|
|
|
r.rootdir(), r.paths()[i].name());
|
2012-06-20 03:14:39 +04:00
|
|
|
cursor->mFiles.AppendElement(dsf);
|
|
|
|
}
|
|
|
|
|
2014-03-18 04:23:03 +04:00
|
|
|
nsRefPtr<ContinueCursorEvent> event = new ContinueCursorEvent(cursor);
|
2012-10-05 00:40:45 +04:00
|
|
|
event->Continue();
|
2012-06-20 03:14:39 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
NS_RUNTIMEABORT("not reached");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-05 00:40:45 +04:00
|
|
|
void
|
2013-08-03 03:35:16 +04:00
|
|
|
DeviceStorageRequestChild::
|
|
|
|
SetCallback(DeviceStorageRequestChildCallback *aCallback)
|
2012-10-05 00:40:45 +04:00
|
|
|
{
|
|
|
|
mCallback = aCallback;
|
|
|
|
}
|
2012-06-20 03:14:39 +04:00
|
|
|
|
|
|
|
} // namespace devicestorage
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|