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"
|
|
|
|
#include "nsDeviceStorage.h"
|
|
|
|
#include "nsDOMFile.h"
|
2012-08-09 15:41:18 -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,
|
|
|
|
DeviceStorageFile* aFile)
|
|
|
|
: mRequest(aRequest)
|
|
|
|
, mFile(aFile)
|
2012-10-04 13:40:45 -07:00
|
|
|
, mCallback(nullptr)
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
mFile->GetFullPath(fullPath);
|
2013-05-17 18:48:25 -07:00
|
|
|
AutoJSContext cx;
|
|
|
|
JS::Rooted<JS::Value> result(cx,
|
2013-08-02 16:34:59 -07:00
|
|
|
StringToJsval(mRequest->GetOwner(), fullPath));
|
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,
|
|
|
|
InterfaceToJsval(mRequest->GetOwner(), 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;
|
|
|
|
JS::Rooted<JS::Value> result(
|
|
|
|
cx, StringToJsval(mRequest->GetOwner(), r.mountState()));
|
2012-08-08 14:07:39 -07:00
|
|
|
mRequest->FireSuccess(result);
|
2012-06-19 16:14:39 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<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
|