2016-04-13 14:15:56 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "GetFilesTask.h"
|
|
|
|
|
|
|
|
#include "HTMLSplitOnSpacesTokenizer.h"
|
|
|
|
#include "js/Value.h"
|
|
|
|
#include "mozilla/dom/File.h"
|
|
|
|
#include "mozilla/dom/FileSystemBase.h"
|
|
|
|
#include "mozilla/dom/FileSystemUtils.h"
|
2017-04-24 13:16:49 +03:00
|
|
|
#include "mozilla/dom/IPCBlobUtils.h"
|
2016-04-13 14:15:56 +03:00
|
|
|
#include "mozilla/dom/PFileSystemParams.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "nsIFile.h"
|
2017-12-07 03:52:51 +03:00
|
|
|
#include "nsString.h"
|
2016-04-13 14:15:56 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GetFilesTaskChild
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<GetFilesTaskChild>
|
|
|
|
GetFilesTaskChild::Create(FileSystemBase* aFileSystem,
|
2016-05-13 14:11:38 +03:00
|
|
|
Directory* aDirectory,
|
2016-04-13 14:15:56 +03:00
|
|
|
nsIFile* aTargetPath,
|
|
|
|
bool aRecursiveFlag,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFileSystem);
|
2016-05-13 14:11:38 +03:00
|
|
|
MOZ_ASSERT(aDirectory);
|
2016-04-13 14:15:56 +03:00
|
|
|
aFileSystem->AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject =
|
|
|
|
do_QueryInterface(aFileSystem->GetParentObject());
|
|
|
|
if (NS_WARN_IF(!globalObject)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<GetFilesTaskChild> task =
|
2017-04-05 10:47:56 +03:00
|
|
|
new GetFilesTaskChild(globalObject, aFileSystem, aDirectory, aTargetPath,
|
2016-05-13 14:11:38 +03:00
|
|
|
aRecursiveFlag);
|
2016-04-13 14:15:56 +03:00
|
|
|
|
|
|
|
// aTargetPath can be null. In this case SetError will be called.
|
|
|
|
|
|
|
|
task->mPromise = Promise::Create(globalObject, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return task.forget();
|
|
|
|
}
|
|
|
|
|
2017-04-05 10:47:56 +03:00
|
|
|
GetFilesTaskChild::GetFilesTaskChild(nsIGlobalObject *aGlobalObject,
|
|
|
|
FileSystemBase* aFileSystem,
|
2016-05-13 14:11:38 +03:00
|
|
|
Directory* aDirectory,
|
2016-04-13 14:15:56 +03:00
|
|
|
nsIFile* aTargetPath,
|
|
|
|
bool aRecursiveFlag)
|
2017-04-05 10:47:56 +03:00
|
|
|
: FileSystemTaskChildBase(aGlobalObject, aFileSystem)
|
2016-05-13 14:11:38 +03:00
|
|
|
, mDirectory(aDirectory)
|
2016-04-13 14:15:56 +03:00
|
|
|
, mTargetPath(aTargetPath)
|
|
|
|
, mRecursiveFlag(aRecursiveFlag)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFileSystem);
|
2016-05-13 14:11:38 +03:00
|
|
|
MOZ_ASSERT(aDirectory);
|
2016-04-13 14:15:56 +03:00
|
|
|
aFileSystem->AssertIsOnOwningThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
GetFilesTaskChild::~GetFilesTaskChild()
|
|
|
|
{
|
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
GetFilesTaskChild::GetPromise()
|
|
|
|
{
|
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
|
|
|
return RefPtr<Promise>(mPromise).forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemParams
|
|
|
|
GetFilesTaskChild::GetRequestParams(const nsString& aSerializedDOMPath,
|
|
|
|
ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
nsAutoString path;
|
|
|
|
aRv = mTargetPath->GetPath(path);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return FileSystemGetFilesParams();
|
|
|
|
}
|
|
|
|
|
2016-05-13 14:11:38 +03:00
|
|
|
nsAutoString domPath;
|
|
|
|
mDirectory->GetPath(domPath, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return FileSystemGetFilesParams();
|
|
|
|
}
|
|
|
|
|
|
|
|
return FileSystemGetFilesParams(aSerializedDOMPath, path, domPath,
|
|
|
|
mRecursiveFlag);
|
2016-04-13 14:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetFilesTaskChild::SetSuccessRequestResult(const FileSystemResponseValue& aValue,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aValue.type() ==
|
|
|
|
FileSystemResponseValue::TFileSystemFilesResponse);
|
|
|
|
|
|
|
|
FileSystemFilesResponse r = aValue;
|
|
|
|
|
|
|
|
if (!mTargetData.SetLength(r.data().Length(), mozilla::fallible_t())) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < r.data().Length(); ++i) {
|
|
|
|
const FileSystemFileResponse& data = r.data()[i];
|
2017-04-24 13:16:49 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = IPCBlobUtils::Deserialize(data.blob());
|
2017-02-01 09:33:18 +03:00
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
|
|
|
mTargetData[i] = File::Create(mFileSystem->GetParentObject(), blobImpl);
|
2016-04-13 14:15:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetFilesTaskChild::HandlerCallback()
|
|
|
|
{
|
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
|
|
|
if (mFileSystem->IsShutdown()) {
|
|
|
|
mPromise = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasError()) {
|
|
|
|
mPromise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
mPromise = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-01 09:33:18 +03:00
|
|
|
mPromise->MaybeResolve(mTargetData);
|
2016-04-13 14:15:56 +03:00
|
|
|
mPromise = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GetFilesTaskParent
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<GetFilesTaskParent>
|
|
|
|
GetFilesTaskParent::Create(FileSystemBase* aFileSystem,
|
|
|
|
const FileSystemGetFilesParams& aParam,
|
|
|
|
FileSystemRequestParent* aParent,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess(), "Only call from parent process!");
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_ASSERT(aFileSystem);
|
|
|
|
|
|
|
|
RefPtr<GetFilesTaskParent> task =
|
|
|
|
new GetFilesTaskParent(aFileSystem, aParam, aParent);
|
|
|
|
|
2017-01-18 06:48:29 +03:00
|
|
|
aRv = NS_NewLocalFile(aParam.realPath(), true,
|
|
|
|
getter_AddRefs(task->mTargetPath));
|
2016-04-13 14:15:56 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return task.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
GetFilesTaskParent::GetFilesTaskParent(FileSystemBase* aFileSystem,
|
|
|
|
const FileSystemGetFilesParams& aParam,
|
|
|
|
FileSystemRequestParent* aParent)
|
|
|
|
: FileSystemTaskParentBase(aFileSystem, aParam, aParent)
|
2016-07-23 11:35:06 +03:00
|
|
|
, GetFilesHelperBase(aParam.recursiveFlag())
|
2016-05-13 14:11:38 +03:00
|
|
|
, mDirectoryDOMPath(aParam.domPath())
|
2016-04-13 14:15:56 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess(), "Only call from parent process!");
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_ASSERT(aFileSystem);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemResponseValue
|
|
|
|
GetFilesTaskParent::GetSuccessRequestResult(ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
FallibleTArray<FileSystemFileResponse> inputs;
|
2017-02-01 09:33:18 +03:00
|
|
|
if (!inputs.SetLength(mTargetBlobImplArray.Length(), mozilla::fallible_t())) {
|
2016-04-13 14:15:56 +03:00
|
|
|
FileSystemFilesResponse response;
|
2017-02-28 22:50:35 +03:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2016-04-13 14:15:56 +03:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2017-02-01 09:33:18 +03:00
|
|
|
for (unsigned i = 0; i < mTargetBlobImplArray.Length(); i++) {
|
2017-04-24 13:16:49 +03:00
|
|
|
IPCBlob ipcBlob;
|
|
|
|
aRv = IPCBlobUtils::Serialize(mTargetBlobImplArray[i],
|
|
|
|
mRequestParent->Manager(), ipcBlob);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
FileSystemFilesResponse response;
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
inputs[i] = FileSystemFileResponse(ipcBlob);
|
2016-04-13 14:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemFilesResponse response;
|
|
|
|
response.data().SwapElements(inputs);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetFilesTaskParent::IOWork()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess(),
|
|
|
|
"Only call from parent process!");
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread(), "Only call on I/O thread!");
|
|
|
|
|
|
|
|
if (mFileSystem->IsShutdown()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool exists;
|
|
|
|
nsresult rv = mTargetPath->Exists(&exists);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDir;
|
2016-07-23 11:35:06 +03:00
|
|
|
rv = mTargetPath->IsDirectory(&isDir);
|
2016-04-13 14:15:56 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isDir) {
|
|
|
|
return NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR;
|
|
|
|
}
|
|
|
|
|
2016-07-23 11:35:06 +03:00
|
|
|
// Get isDirectory.
|
|
|
|
rv = ExploreDirectory(mDirectoryDOMPath, mTargetPath);
|
2016-04-13 14:15:56 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-16 10:53:49 +03:00
|
|
|
nsresult
|
|
|
|
GetFilesTaskParent::GetTargetPath(nsAString& aPath) const
|
|
|
|
{
|
|
|
|
return mTargetPath->GetPath(aPath);
|
|
|
|
}
|
|
|
|
|
2016-04-13 14:15:56 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|