2012-06-19 23:14:39 +00: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 "DeviceStorageRequestParent.h"
|
|
|
|
#include "nsDOMFile.h"
|
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
#include "mozilla/unused.h"
|
2012-08-09 22:41:18 +00:00
|
|
|
#include "mozilla/dom/ipc/Blob.h"
|
|
|
|
#include "ContentParent.h"
|
|
|
|
#include "nsProxyRelease.h"
|
2012-12-22 11:53:38 +00:00
|
|
|
#include "AppProcessChecker.h"
|
2012-11-30 05:41:40 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-06-19 23:14:39 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace devicestorage {
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::DeviceStorageRequestParent(const DeviceStorageParams& aParams)
|
2012-11-30 05:41:40 +00:00
|
|
|
: mParams(aParams)
|
|
|
|
, mMutex("DeviceStorageRequestParent::mMutex")
|
2012-08-28 15:39:22 +00:00
|
|
|
, mActorDestoryed(false)
|
2012-06-19 23:14:39 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(DeviceStorageRequestParent);
|
2012-11-30 05:41:40 +00:00
|
|
|
}
|
2012-06-19 23:14:39 +00:00
|
|
|
|
2012-11-30 05:41:40 +00:00
|
|
|
void
|
|
|
|
DeviceStorageRequestParent::Dispatch()
|
|
|
|
{
|
|
|
|
switch (mParams.type()) {
|
2012-06-19 23:14:39 +00:00
|
|
|
case DeviceStorageParams::TDeviceStorageAddParams:
|
|
|
|
{
|
2012-11-30 05:41:40 +00:00
|
|
|
DeviceStorageAddParams p = mParams;
|
2012-06-19 23:14:39 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> f;
|
|
|
|
NS_NewLocalFile(p.fullpath(), false, getter_AddRefs(f));
|
|
|
|
|
2012-08-30 22:17:37 +00:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(p.type(), f);
|
2012-08-09 22:41:18 +00:00
|
|
|
|
|
|
|
BlobParent* bp = static_cast<BlobParent*>(p.blobParent());
|
|
|
|
nsCOMPtr<nsIDOMBlob> blob = bp->GetBlob();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
blob->GetInternalStream(getter_AddRefs(stream));
|
|
|
|
|
|
|
|
nsRefPtr<CancelableRunnable> r = new WriteFileEvent(this, dsf, stream);
|
2012-06-19 23:14:39 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
NS_ASSERTION(target, "Must have stream transport service");
|
|
|
|
target->Dispatch(r, NS_DISPATCH_NORMAL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageParams::TDeviceStorageGetParams:
|
|
|
|
{
|
2012-11-30 05:41:40 +00:00
|
|
|
DeviceStorageGetParams p = mParams;
|
2012-06-19 23:14:39 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> f;
|
|
|
|
NS_NewLocalFile(p.fullpath(), false, getter_AddRefs(f));
|
|
|
|
|
2012-08-30 22:17:37 +00:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(p.type(), f);
|
2012-08-09 22:41:18 +00:00
|
|
|
dsf->SetPath(p.name());
|
|
|
|
nsRefPtr<CancelableRunnable> r = new ReadFileEvent(this, dsf);
|
2012-06-19 23:14:39 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
NS_ASSERTION(target, "Must have stream transport service");
|
|
|
|
target->Dispatch(r, NS_DISPATCH_NORMAL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageParams::TDeviceStorageDeleteParams:
|
|
|
|
{
|
2012-11-30 05:41:40 +00:00
|
|
|
DeviceStorageDeleteParams p = mParams;
|
2012-06-19 23:14:39 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> f;
|
|
|
|
NS_NewLocalFile(p.fullpath(), false, getter_AddRefs(f));
|
|
|
|
|
2012-08-30 22:17:37 +00:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(p.type(), f);
|
2012-08-09 22:41:18 +00:00
|
|
|
nsRefPtr<CancelableRunnable> r = new DeleteFileEvent(this, dsf);
|
2012-06-19 23:14:39 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
NS_ASSERTION(target, "Must have stream transport service");
|
|
|
|
target->Dispatch(r, NS_DISPATCH_NORMAL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-07-31 19:28:23 +00:00
|
|
|
case DeviceStorageParams::TDeviceStorageStatParams:
|
|
|
|
{
|
2012-11-30 05:41:40 +00:00
|
|
|
DeviceStorageStatParams p = mParams;
|
2012-07-31 19:28:23 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> f;
|
|
|
|
NS_NewLocalFile(p.fullpath(), false, getter_AddRefs(f));
|
|
|
|
|
2012-08-30 22:17:37 +00:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(p.type(), f);
|
2012-07-31 19:28:23 +00:00
|
|
|
nsRefPtr<StatFileEvent> r = new StatFileEvent(this, dsf);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
NS_ASSERTION(target, "Must have stream transport service");
|
|
|
|
target->Dispatch(r, NS_DISPATCH_NORMAL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-06-19 23:14:39 +00:00
|
|
|
case DeviceStorageParams::TDeviceStorageEnumerationParams:
|
|
|
|
{
|
2012-11-30 05:41:40 +00:00
|
|
|
DeviceStorageEnumerationParams p = mParams;
|
2012-06-19 23:14:39 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> f;
|
|
|
|
NS_NewLocalFile(p.fullpath(), false, getter_AddRefs(f));
|
|
|
|
|
2012-08-30 22:17:37 +00:00
|
|
|
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(p.type(), f);
|
2012-08-09 22:41:18 +00:00
|
|
|
nsRefPtr<CancelableRunnable> r = new EnumerateFileEvent(this, dsf, p.since());
|
2012-06-19 23:14:39 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
NS_ASSERTION(target, "Must have stream transport service");
|
|
|
|
target->Dispatch(r, NS_DISPATCH_NORMAL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
NS_RUNTIMEABORT("not reached");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 05:41:40 +00:00
|
|
|
bool
|
|
|
|
DeviceStorageRequestParent::EnsureRequiredPermissions(mozilla::dom::ContentParent* aParent)
|
|
|
|
{
|
|
|
|
if (mozilla::Preferences::GetBool("device.storage.testing", false)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString type;
|
|
|
|
DeviceStorageRequestType requestType;
|
|
|
|
|
|
|
|
switch (mParams.type())
|
|
|
|
{
|
|
|
|
case DeviceStorageParams::TDeviceStorageAddParams:
|
|
|
|
{
|
|
|
|
DeviceStorageAddParams p = mParams;
|
|
|
|
type = p.type();
|
|
|
|
requestType = DEVICE_STORAGE_REQUEST_CREATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageParams::TDeviceStorageGetParams:
|
|
|
|
{
|
|
|
|
DeviceStorageGetParams p = mParams;
|
|
|
|
type = p.type();
|
|
|
|
requestType = DEVICE_STORAGE_REQUEST_READ;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageParams::TDeviceStorageDeleteParams:
|
|
|
|
{
|
|
|
|
DeviceStorageDeleteParams p = mParams;
|
|
|
|
type = p.type();
|
|
|
|
requestType = DEVICE_STORAGE_REQUEST_DELETE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageParams::TDeviceStorageStatParams:
|
|
|
|
{
|
|
|
|
DeviceStorageStatParams p = mParams;
|
|
|
|
type = p.type();
|
|
|
|
requestType = DEVICE_STORAGE_REQUEST_STAT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DeviceStorageParams::TDeviceStorageEnumerationParams:
|
|
|
|
{
|
|
|
|
DeviceStorageEnumerationParams p = mParams;
|
|
|
|
type = p.type();
|
|
|
|
requestType = DEVICE_STORAGE_REQUEST_READ;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The 'apps' type is special. We only want this exposed
|
|
|
|
// if the caller has the "webapps-manage" permission.
|
|
|
|
if (type.EqualsLiteral("apps")) {
|
|
|
|
if (!AssertAppProcessPermission(aParent, "webapps-manage")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString permissionName;
|
|
|
|
nsresult rv = DeviceStorageTypeChecker::GetPermissionForType(type, permissionName);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString access;
|
|
|
|
rv = DeviceStorageTypeChecker::GetAccessForRequest(requestType, access);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
permissionName.AppendLiteral("-");
|
|
|
|
permissionName.Append(access);
|
|
|
|
|
|
|
|
if (!AssertAppProcessPermission(aParent, permissionName.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-19 23:14:39 +00:00
|
|
|
DeviceStorageRequestParent::~DeviceStorageRequestParent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(DeviceStorageRequestParent);
|
|
|
|
}
|
|
|
|
|
2013-01-11 10:14:04 +00:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(DeviceStorageRequestParent)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(DeviceStorageRequestParent)
|
2012-08-09 22:41:18 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
DeviceStorageRequestParent::ActorDestroy(ActorDestroyReason)
|
|
|
|
{
|
2012-08-28 15:39:22 +00:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mActorDestoryed = true;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t count = mRunnables.Length();
|
|
|
|
for (int32_t index = 0; index < count; index++) {
|
2012-08-09 22:41:18 +00:00
|
|
|
mRunnables[index]->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-19 23:14:39 +00:00
|
|
|
DeviceStorageRequestParent::PostErrorEvent::PostErrorEvent(DeviceStorageRequestParent* aParent,
|
|
|
|
const char* aError)
|
2012-08-09 22:41:18 +00:00
|
|
|
: CancelableRunnable(aParent)
|
2012-06-19 23:14:39 +00:00
|
|
|
{
|
2012-08-09 22:41:18 +00:00
|
|
|
CopyASCIItoUTF16(aError, mError);
|
2012-06-19 23:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::PostErrorEvent::~PostErrorEvent() {}
|
|
|
|
|
2012-08-09 22:41:18 +00:00
|
|
|
nsresult
|
|
|
|
DeviceStorageRequestParent::PostErrorEvent::CancelableRun() {
|
2012-06-19 23:14:39 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-08-09 22:41:18 +00:00
|
|
|
|
2012-06-19 23:14:39 +00:00
|
|
|
ErrorResponse response(mError);
|
|
|
|
unused << mParent->Send__delete__(mParent, response);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::PostSuccessEvent::PostSuccessEvent(DeviceStorageRequestParent* aParent)
|
2012-08-09 22:41:18 +00:00
|
|
|
: CancelableRunnable(aParent)
|
2012-06-19 23:14:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::PostSuccessEvent::~PostSuccessEvent() {}
|
|
|
|
|
2012-08-09 22:41:18 +00:00
|
|
|
nsresult
|
|
|
|
DeviceStorageRequestParent::PostSuccessEvent::CancelableRun() {
|
2012-06-19 23:14:39 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
SuccessResponse response;
|
|
|
|
unused << mParent->Send__delete__(mParent, response);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::PostBlobSuccessEvent::PostBlobSuccessEvent(DeviceStorageRequestParent* aParent,
|
2012-08-09 22:41:18 +00:00
|
|
|
DeviceStorageFile* aFile,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aLength,
|
2012-10-18 18:29:32 +00:00
|
|
|
nsACString& aMimeType,
|
|
|
|
uint64_t aLastModifiedDate)
|
2012-08-09 22:41:18 +00:00
|
|
|
: CancelableRunnable(aParent)
|
|
|
|
, mLength(aLength)
|
2012-10-18 18:29:32 +00:00
|
|
|
, mLastModificationDate(aLastModifiedDate)
|
2012-08-09 22:41:18 +00:00
|
|
|
, mFile(aFile)
|
2012-06-19 23:14:39 +00:00
|
|
|
, mMimeType(aMimeType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::PostBlobSuccessEvent::~PostBlobSuccessEvent() {}
|
|
|
|
|
2012-08-09 22:41:18 +00:00
|
|
|
nsresult
|
|
|
|
DeviceStorageRequestParent::PostBlobSuccessEvent::CancelableRun() {
|
2012-06-19 23:14:39 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-08-09 22:41:18 +00:00
|
|
|
nsString mime;
|
|
|
|
CopyASCIItoUTF16(mMimeType, mime);
|
|
|
|
|
2012-10-18 18:29:32 +00:00
|
|
|
nsCOMPtr<nsIDOMBlob> blob = new nsDOMFileFile(mFile->mPath, mime, mLength, mFile->mFile, mLastModificationDate);
|
2012-08-09 22:41:18 +00:00
|
|
|
|
|
|
|
ContentParent* cp = static_cast<ContentParent*>(mParent->Manager());
|
|
|
|
BlobParent* actor = cp->GetOrCreateActorForBlob(blob);
|
|
|
|
|
|
|
|
BlobResponse response;
|
|
|
|
response.blobParent() = actor;
|
|
|
|
|
2012-06-19 23:14:39 +00:00
|
|
|
unused << mParent->Send__delete__(mParent, response);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::PostEnumerationSuccessEvent::PostEnumerationSuccessEvent(DeviceStorageRequestParent* aParent,
|
|
|
|
InfallibleTArray<DeviceStorageFileValue>& aPaths)
|
2012-08-09 22:41:18 +00:00
|
|
|
: CancelableRunnable(aParent)
|
2012-06-19 23:14:39 +00:00
|
|
|
, mPaths(aPaths)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::PostEnumerationSuccessEvent::~PostEnumerationSuccessEvent() {}
|
|
|
|
|
2012-08-09 22:41:18 +00:00
|
|
|
nsresult
|
|
|
|
DeviceStorageRequestParent::PostEnumerationSuccessEvent::CancelableRun() {
|
2012-06-19 23:14:39 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
EnumerationResponse response(mPaths);
|
|
|
|
unused << mParent->Send__delete__(mParent, response);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::WriteFileEvent::WriteFileEvent(DeviceStorageRequestParent* aParent,
|
|
|
|
DeviceStorageFile* aFile,
|
2012-08-09 22:41:18 +00:00
|
|
|
nsIInputStream* aInputStream)
|
|
|
|
: CancelableRunnable(aParent)
|
2012-06-19 23:14:39 +00:00
|
|
|
, mFile(aFile)
|
2012-08-09 22:41:18 +00:00
|
|
|
, mInputStream(aInputStream)
|
2012-06-19 23:14:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::WriteFileEvent::~WriteFileEvent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-09 22:41:18 +00:00
|
|
|
nsresult
|
|
|
|
DeviceStorageRequestParent::WriteFileEvent::CancelableRun()
|
2012-06-19 23:14:39 +00:00
|
|
|
{
|
2012-08-09 22:41:18 +00:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-06-19 23:14:39 +00:00
|
|
|
nsRefPtr<nsRunnable> r;
|
2012-08-09 22:41:18 +00:00
|
|
|
|
|
|
|
if (!mInputStream) {
|
|
|
|
r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-13 15:20:14 +00:00
|
|
|
bool check = false;
|
|
|
|
mFile->mFile->Exists(&check);
|
|
|
|
if (check) {
|
|
|
|
nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mParent, POST_ERROR_EVENT_FILE_EXISTS);
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-09 22:41:18 +00:00
|
|
|
nsresult rv = mFile->Write(mInputStream);
|
2012-06-19 23:14:39 +00:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
r = new PostPathResultEvent(mParent, mFile->mPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::DeleteFileEvent::DeleteFileEvent(DeviceStorageRequestParent* aParent,
|
|
|
|
DeviceStorageFile* aFile)
|
2012-08-09 22:41:18 +00:00
|
|
|
: CancelableRunnable(aParent)
|
2012-06-19 23:14:39 +00:00
|
|
|
, mFile(aFile)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::DeleteFileEvent::~DeleteFileEvent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-09 22:41:18 +00:00
|
|
|
nsresult
|
|
|
|
DeviceStorageRequestParent::DeleteFileEvent::CancelableRun()
|
2012-06-19 23:14:39 +00:00
|
|
|
{
|
2012-07-31 19:28:23 +00:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
2012-08-09 22:41:18 +00:00
|
|
|
|
2012-08-18 02:43:00 +00:00
|
|
|
mFile->Remove();
|
2012-06-19 23:14:39 +00:00
|
|
|
|
|
|
|
nsRefPtr<nsRunnable> r;
|
|
|
|
|
|
|
|
bool check = false;
|
|
|
|
mFile->mFile->Exists(&check);
|
|
|
|
if (check) {
|
|
|
|
r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
r = new PostPathResultEvent(mParent, mFile->mPath);
|
|
|
|
}
|
2012-08-09 22:41:18 +00:00
|
|
|
|
2012-06-19 23:14:39 +00:00
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-31 19:28:23 +00:00
|
|
|
DeviceStorageRequestParent::StatFileEvent::StatFileEvent(DeviceStorageRequestParent* aParent,
|
|
|
|
DeviceStorageFile* aFile)
|
|
|
|
: CancelableRunnable(aParent)
|
|
|
|
, mFile(aFile)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::StatFileEvent::~StatFileEvent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DeviceStorageRequestParent::StatFileEvent::CancelableRun()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r;
|
2012-08-31 03:15:32 +00:00
|
|
|
uint64_t diskUsage = 0;
|
2012-09-05 21:30:36 +00:00
|
|
|
DeviceStorageFile::DirectoryDiskUsage(mFile->mFile, &diskUsage, mFile->mStorageType);
|
2012-08-31 03:15:32 +00:00
|
|
|
int64_t freeSpace = 0;
|
2012-07-31 19:28:23 +00:00
|
|
|
nsresult rv = mFile->mFile->GetDiskSpaceAvailable(&freeSpace);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-08-27 04:34:04 +00:00
|
|
|
freeSpace = 0;
|
2012-07-31 19:28:23 +00:00
|
|
|
}
|
2012-09-14 05:37:00 +00:00
|
|
|
|
2012-08-31 03:15:32 +00:00
|
|
|
r = new PostStatResultEvent(mParent, freeSpace, diskUsage);
|
2012-07-31 19:28:23 +00:00
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-19 23:14:39 +00:00
|
|
|
DeviceStorageRequestParent::ReadFileEvent::ReadFileEvent(DeviceStorageRequestParent* aParent,
|
|
|
|
DeviceStorageFile* aFile)
|
2012-08-09 22:41:18 +00:00
|
|
|
: CancelableRunnable(aParent)
|
2012-06-19 23:14:39 +00:00
|
|
|
, mFile(aFile)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeService = do_GetService(NS_MIMESERVICE_CONTRACTID);
|
|
|
|
if (mimeService) {
|
|
|
|
nsresult rv = mimeService->GetTypeFromFile(mFile->mFile, mMimeType);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mMimeType.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::ReadFileEvent::~ReadFileEvent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-09 22:41:18 +00:00
|
|
|
nsresult
|
|
|
|
DeviceStorageRequestParent::ReadFileEvent::CancelableRun()
|
2012-06-19 23:14:39 +00:00
|
|
|
{
|
2012-07-31 19:28:23 +00:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
2012-08-09 22:41:18 +00:00
|
|
|
|
2012-06-19 23:14:39 +00:00
|
|
|
nsCOMPtr<nsIRunnable> r;
|
|
|
|
bool check = false;
|
|
|
|
mFile->mFile->Exists(&check);
|
2012-08-09 22:41:18 +00:00
|
|
|
|
2012-06-19 23:14:39 +00:00
|
|
|
if (!check) {
|
|
|
|
r = new PostErrorEvent(mParent, POST_ERROR_EVENT_FILE_DOES_NOT_EXIST);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t fileSize;
|
2012-06-19 23:14:39 +00:00
|
|
|
nsresult rv = mFile->mFile->GetFileSize(&fileSize);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-18 18:29:32 +00:00
|
|
|
PRTime modDate;
|
|
|
|
rv = mFile->mFile->GetLastModifiedTime(&modDate);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = new PostBlobSuccessEvent(mParent, mFile, fileSize, mMimeType, modDate);
|
2012-06-19 23:14:39 +00:00
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::EnumerateFileEvent::EnumerateFileEvent(DeviceStorageRequestParent* aParent,
|
|
|
|
DeviceStorageFile* aFile,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t aSince)
|
2012-08-09 22:41:18 +00:00
|
|
|
: CancelableRunnable(aParent)
|
2012-06-19 23:14:39 +00:00
|
|
|
, mFile(aFile)
|
|
|
|
, mSince(aSince)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::EnumerateFileEvent::~EnumerateFileEvent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-09 22:41:18 +00:00
|
|
|
nsresult
|
|
|
|
DeviceStorageRequestParent::EnumerateFileEvent::CancelableRun()
|
2012-06-19 23:14:39 +00:00
|
|
|
{
|
2012-07-31 19:28:23 +00:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
2012-08-09 22:41:18 +00:00
|
|
|
|
2012-06-19 23:14:39 +00:00
|
|
|
nsCOMPtr<nsIRunnable> r;
|
|
|
|
bool check = false;
|
|
|
|
mFile->mFile->Exists(&check);
|
|
|
|
if (!check) {
|
|
|
|
r = new PostErrorEvent(mParent, POST_ERROR_EVENT_FILE_DOES_NOT_EXIST);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<DeviceStorageFile> > files;
|
|
|
|
mFile->CollectFiles(files, mSince);
|
|
|
|
|
|
|
|
InfallibleTArray<DeviceStorageFileValue> values;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count = files.Length();
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2012-06-19 23:14:39 +00:00
|
|
|
nsString fullpath;
|
|
|
|
files[i]->mFile->GetPath(fullpath);
|
2012-08-30 22:17:37 +00:00
|
|
|
DeviceStorageFileValue dsvf(mFile->mStorageType, files[i]->mPath, fullpath);
|
2012-06-19 23:14:39 +00:00
|
|
|
values.AppendElement(dsvf);
|
|
|
|
}
|
|
|
|
|
|
|
|
r = new PostEnumerationSuccessEvent(mParent, values);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::PostPathResultEvent::PostPathResultEvent(DeviceStorageRequestParent* aParent,
|
2012-08-09 22:41:18 +00:00
|
|
|
const nsAString& aPath)
|
|
|
|
: CancelableRunnable(aParent)
|
2012-06-19 23:14:39 +00:00
|
|
|
, mPath(aPath)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::PostPathResultEvent::~PostPathResultEvent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-09 22:41:18 +00:00
|
|
|
nsresult
|
|
|
|
DeviceStorageRequestParent::PostPathResultEvent::CancelableRun()
|
2012-06-19 23:14:39 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
SuccessResponse response;
|
|
|
|
unused << mParent->Send__delete__(mParent, response);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-31 19:28:23 +00:00
|
|
|
DeviceStorageRequestParent::PostStatResultEvent::PostStatResultEvent(DeviceStorageRequestParent* aParent,
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t aFreeBytes,
|
|
|
|
int64_t aTotalBytes)
|
2012-07-31 19:28:23 +00:00
|
|
|
: CancelableRunnable(aParent)
|
|
|
|
, mFreeBytes(aFreeBytes)
|
|
|
|
, mTotalBytes(aTotalBytes)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageRequestParent::PostStatResultEvent::~PostStatResultEvent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DeviceStorageRequestParent::PostStatResultEvent::CancelableRun()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-08-16 23:03:06 +00:00
|
|
|
nsString state;
|
|
|
|
state.Assign(NS_LITERAL_STRING("available"));
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
nsresult rv = GetSDCardStatus(state);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
state.Assign(NS_LITERAL_STRING("unavailable"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
StatStorageResponse response(mFreeBytes, mTotalBytes, state);
|
2012-07-31 19:28:23 +00:00
|
|
|
unused << mParent->Send__delete__(mParent, response);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-19 23:14:39 +00:00
|
|
|
|
|
|
|
} // namespace devicestorage
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|