зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1299500 - Get rid of DeviceStorage API - part 7 - Directory::Remove, r=ehsan
This commit is contained in:
Родитель
6c1325bca7
Коммит
d4bba716a6
|
@ -10,7 +10,6 @@
|
|||
#include "GetDirectoryListingTask.h"
|
||||
#include "GetFileOrDirectoryTask.h"
|
||||
#include "GetFilesTask.h"
|
||||
#include "RemoveTask.h"
|
||||
#include "WorkerPrivate.h"
|
||||
|
||||
#include "nsCharSeparatedTokenizer.h"
|
||||
|
@ -205,76 +204,6 @@ Directory::Get(const nsAString& aPath, ErrorResult& aRv)
|
|||
return task->GetPromise();
|
||||
}
|
||||
|
||||
already_AddRefed<Promise>
|
||||
Directory::Remove(const StringOrFileOrDirectory& aPath, ErrorResult& aRv)
|
||||
{
|
||||
// Only exposed for DeviceStorage.
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
return RemoveInternal(aPath, false, aRv);
|
||||
}
|
||||
|
||||
already_AddRefed<Promise>
|
||||
Directory::RemoveDeep(const StringOrFileOrDirectory& aPath, ErrorResult& aRv)
|
||||
{
|
||||
// Only exposed for DeviceStorage.
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
return RemoveInternal(aPath, true, aRv);
|
||||
}
|
||||
|
||||
already_AddRefed<Promise>
|
||||
Directory::RemoveInternal(const StringOrFileOrDirectory& aPath, bool aRecursive,
|
||||
ErrorResult& aRv)
|
||||
{
|
||||
// Only exposed for DeviceStorage.
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
|
||||
nsresult error = NS_OK;
|
||||
nsCOMPtr<nsIFile> realPath;
|
||||
|
||||
// Check and get the target path.
|
||||
|
||||
RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// If this is a File
|
||||
if (aPath.IsFile()) {
|
||||
if (!fs->GetRealPath(aPath.GetAsFile().Impl(),
|
||||
getter_AddRefs(realPath))) {
|
||||
error = NS_ERROR_DOM_SECURITY_ERR;
|
||||
}
|
||||
|
||||
// If this is a string
|
||||
} else if (aPath.IsString()) {
|
||||
error = DOMPathToRealPath(aPath.GetAsString(), getter_AddRefs(realPath));
|
||||
|
||||
// Directory
|
||||
} else {
|
||||
MOZ_ASSERT(aPath.IsDirectory());
|
||||
if (!fs->IsSafeDirectory(&aPath.GetAsDirectory())) {
|
||||
error = NS_ERROR_DOM_SECURITY_ERR;
|
||||
} else {
|
||||
realPath = aPath.GetAsDirectory().mFile;
|
||||
}
|
||||
}
|
||||
|
||||
// The target must be a descendant of this directory.
|
||||
if (!FileSystemUtils::IsDescendantPath(mFile, realPath)) {
|
||||
error = NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR;
|
||||
}
|
||||
|
||||
RefPtr<RemoveTaskChild> task =
|
||||
RemoveTaskChild::Create(fs, mFile, realPath, aRecursive, aRv);
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
task->SetError(error);
|
||||
FileSystemPermissionRequest::RequestForTask(task);
|
||||
return task->GetPromise();
|
||||
}
|
||||
|
||||
void
|
||||
Directory::GetPath(nsAString& aRetval, ErrorResult& aRv)
|
||||
{
|
||||
|
|
|
@ -61,12 +61,6 @@ public:
|
|||
already_AddRefed<Promise>
|
||||
Get(const nsAString& aPath, ErrorResult& aRv);
|
||||
|
||||
already_AddRefed<Promise>
|
||||
Remove(const StringOrFileOrDirectory& aPath, ErrorResult& aRv);
|
||||
|
||||
already_AddRefed<Promise>
|
||||
RemoveDeep(const StringOrFileOrDirectory& aPath, ErrorResult& aRv);
|
||||
|
||||
// From https://microsoftedge.github.io/directory-upload/proposal.html#directory-interface :
|
||||
|
||||
void
|
||||
|
@ -132,10 +126,6 @@ private:
|
|||
nsresult
|
||||
DOMPathToRealPath(const nsAString& aPath, nsIFile** aFile) const;
|
||||
|
||||
already_AddRefed<Promise>
|
||||
RemoveInternal(const StringOrFileOrDirectory& aPath, bool aRecursive,
|
||||
ErrorResult& aRv);
|
||||
|
||||
nsCOMPtr<nsISupports> mParent;
|
||||
RefPtr<FileSystemBase> mFileSystem;
|
||||
nsCOMPtr<nsIFile> mFile;
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
#include "GetDirectoryListingTask.h"
|
||||
#include "GetFileOrDirectoryTask.h"
|
||||
#include "RemoveTask.h"
|
||||
|
||||
#include "mozilla/dom/FileSystemBase.h"
|
||||
|
||||
|
@ -50,7 +49,6 @@ FileSystemRequestParent::Initialize(const FileSystemParams& aParams)
|
|||
FILESYSTEM_REQUEST_PARENT_DISPATCH_ENTRY(GetDirectoryListing)
|
||||
FILESYSTEM_REQUEST_PARENT_DISPATCH_ENTRY(GetFileOrDirectory)
|
||||
FILESYSTEM_REQUEST_PARENT_DISPATCH_ENTRY(GetFiles)
|
||||
FILESYSTEM_REQUEST_PARENT_DISPATCH_ENTRY(Remove)
|
||||
|
||||
default: {
|
||||
MOZ_CRASH("not reached");
|
||||
|
|
|
@ -45,20 +45,11 @@ struct FileSystemGetFileOrDirectoryParams
|
|||
nsString realPath;
|
||||
};
|
||||
|
||||
struct FileSystemRemoveParams
|
||||
{
|
||||
nsString filesystem;
|
||||
nsString directory;
|
||||
nsString targetDirectory;
|
||||
bool recursive;
|
||||
};
|
||||
|
||||
union FileSystemParams
|
||||
{
|
||||
FileSystemGetDirectoryListingParams;
|
||||
FileSystemGetFilesParams;
|
||||
FileSystemGetFileOrDirectoryParams;
|
||||
FileSystemRemoveParams;
|
||||
};
|
||||
|
||||
} // dom namespace
|
||||
|
|
|
@ -1,258 +0,0 @@
|
|||
/* -*- 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 "RemoveTask.h"
|
||||
|
||||
#include "mozilla/dom/File.h"
|
||||
#include "mozilla/dom/FileSystemBase.h"
|
||||
#include "mozilla/dom/FileSystemUtils.h"
|
||||
#include "mozilla/dom/PFileSystemParams.h"
|
||||
#include "mozilla/dom/Promise.h"
|
||||
#include "mozilla/dom/ipc/BlobChild.h"
|
||||
#include "mozilla/dom/ipc/BlobParent.h"
|
||||
#include "nsIFile.h"
|
||||
#include "nsStringGlue.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace dom {
|
||||
|
||||
/**
|
||||
* RemoveTaskChild
|
||||
*/
|
||||
|
||||
/* static */ already_AddRefed<RemoveTaskChild>
|
||||
RemoveTaskChild::Create(FileSystemBase* aFileSystem,
|
||||
nsIFile* aDirPath,
|
||||
nsIFile* aTargetPath,
|
||||
bool aRecursive,
|
||||
ErrorResult& aRv)
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
|
||||
MOZ_ASSERT(aFileSystem);
|
||||
MOZ_ASSERT(aDirPath);
|
||||
MOZ_ASSERT(aTargetPath);
|
||||
|
||||
RefPtr<RemoveTaskChild> task =
|
||||
new RemoveTaskChild(aFileSystem, aDirPath, aTargetPath, aRecursive);
|
||||
|
||||
// aTargetPath can be null. In this case SetError will be called.
|
||||
|
||||
nsCOMPtr<nsIGlobalObject> globalObject =
|
||||
do_QueryInterface(aFileSystem->GetParentObject());
|
||||
if (NS_WARN_IF(!globalObject)) {
|
||||
aRv.Throw(NS_ERROR_FAILURE);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
task->mPromise = Promise::Create(globalObject, aRv);
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return task.forget();
|
||||
}
|
||||
|
||||
RemoveTaskChild::RemoveTaskChild(FileSystemBase* aFileSystem,
|
||||
nsIFile* aDirPath,
|
||||
nsIFile* aTargetPath,
|
||||
bool aRecursive)
|
||||
: FileSystemTaskChildBase(aFileSystem)
|
||||
, mDirPath(aDirPath)
|
||||
, mTargetPath(aTargetPath)
|
||||
, mRecursive(aRecursive)
|
||||
, mReturnValue(false)
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
|
||||
MOZ_ASSERT(aFileSystem);
|
||||
MOZ_ASSERT(aDirPath);
|
||||
MOZ_ASSERT(aTargetPath);
|
||||
}
|
||||
|
||||
RemoveTaskChild::~RemoveTaskChild()
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
}
|
||||
|
||||
already_AddRefed<Promise>
|
||||
RemoveTaskChild::GetPromise()
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
|
||||
return RefPtr<Promise>(mPromise).forget();
|
||||
}
|
||||
|
||||
FileSystemParams
|
||||
RemoveTaskChild::GetRequestParams(const nsString& aSerializedDOMPath,
|
||||
ErrorResult& aRv) const
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
|
||||
FileSystemRemoveParams param;
|
||||
param.filesystem() = aSerializedDOMPath;
|
||||
|
||||
aRv = mDirPath->GetPath(param.directory());
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return param;
|
||||
}
|
||||
|
||||
param.recursive() = mRecursive;
|
||||
|
||||
nsAutoString path;
|
||||
aRv = mTargetPath->GetPath(path);
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return param;
|
||||
}
|
||||
|
||||
param.targetDirectory() = path;
|
||||
|
||||
return param;
|
||||
}
|
||||
|
||||
void
|
||||
RemoveTaskChild::SetSuccessRequestResult(const FileSystemResponseValue& aValue,
|
||||
ErrorResult& aRv)
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
|
||||
|
||||
FileSystemBooleanResponse r = aValue;
|
||||
mReturnValue = r.success();
|
||||
}
|
||||
|
||||
void
|
||||
RemoveTaskChild::HandlerCallback()
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
|
||||
|
||||
if (mFileSystem->IsShutdown()) {
|
||||
mPromise = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
if (HasError()) {
|
||||
mPromise->MaybeReject(mErrorValue);
|
||||
mPromise = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
mPromise->MaybeResolve(mReturnValue);
|
||||
mPromise = nullptr;
|
||||
}
|
||||
|
||||
void
|
||||
RemoveTaskChild::GetPermissionAccessType(nsCString& aAccess) const
|
||||
{
|
||||
aAccess.AssignLiteral(DIRECTORY_WRITE_PERMISSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* RemoveTaskParent
|
||||
*/
|
||||
|
||||
/* static */ already_AddRefed<RemoveTaskParent>
|
||||
RemoveTaskParent::Create(FileSystemBase* aFileSystem,
|
||||
const FileSystemRemoveParams& aParam,
|
||||
FileSystemRequestParent* aParent,
|
||||
ErrorResult& aRv)
|
||||
{
|
||||
MOZ_ASSERT(XRE_IsParentProcess(), "Only call from parent process!");
|
||||
AssertIsOnBackgroundThread();
|
||||
MOZ_ASSERT(aFileSystem);
|
||||
|
||||
RefPtr<RemoveTaskParent> task =
|
||||
new RemoveTaskParent(aFileSystem, aParam, aParent);
|
||||
|
||||
aRv = NS_NewLocalFile(aParam.directory(), true,
|
||||
getter_AddRefs(task->mDirPath));
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
task->mRecursive = aParam.recursive();
|
||||
|
||||
aRv = NS_NewLocalFile(aParam.targetDirectory(), true,
|
||||
getter_AddRefs(task->mTargetPath));
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!FileSystemUtils::IsDescendantPath(task->mDirPath, task->mTargetPath)) {
|
||||
aRv.Throw(NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return task.forget();
|
||||
}
|
||||
|
||||
RemoveTaskParent::RemoveTaskParent(FileSystemBase* aFileSystem,
|
||||
const FileSystemRemoveParams& aParam,
|
||||
FileSystemRequestParent* aParent)
|
||||
: FileSystemTaskParentBase(aFileSystem, aParam, aParent)
|
||||
, mRecursive(false)
|
||||
, mReturnValue(false)
|
||||
{
|
||||
MOZ_ASSERT(XRE_IsParentProcess(), "Only call from parent process!");
|
||||
AssertIsOnBackgroundThread();
|
||||
MOZ_ASSERT(aFileSystem);
|
||||
}
|
||||
|
||||
FileSystemResponseValue
|
||||
RemoveTaskParent::GetSuccessRequestResult(ErrorResult& aRv) const
|
||||
{
|
||||
AssertIsOnBackgroundThread();
|
||||
|
||||
return FileSystemBooleanResponse(mReturnValue);
|
||||
}
|
||||
|
||||
nsresult
|
||||
RemoveTaskParent::IOWork()
|
||||
{
|
||||
MOZ_ASSERT(XRE_IsParentProcess(),
|
||||
"Only call from parent process!");
|
||||
MOZ_ASSERT(!NS_IsMainThread(), "Only call on worker thread!");
|
||||
|
||||
if (mFileSystem->IsShutdown()) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
MOZ_ASSERT(FileSystemUtils::IsDescendantPath(mDirPath, mTargetPath));
|
||||
|
||||
bool exists = false;
|
||||
nsresult rv = mTargetPath->Exists(&exists);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
if (!exists) {
|
||||
mReturnValue = false;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
bool isFile = false;
|
||||
rv = mTargetPath->IsFile(&isFile);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
if (isFile && !mFileSystem->IsSafeFile(mTargetPath)) {
|
||||
return NS_ERROR_DOM_SECURITY_ERR;
|
||||
}
|
||||
|
||||
rv = mTargetPath->Remove(mRecursive);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
mReturnValue = true;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
RemoveTaskParent::GetPermissionAccessType(nsCString& aAccess) const
|
||||
{
|
||||
aAccess.AssignLiteral(DIRECTORY_WRITE_PERMISSION);
|
||||
}
|
||||
|
||||
} // namespace dom
|
||||
} // namespace mozilla
|
|
@ -1,106 +0,0 @@
|
|||
/* -*- 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/. */
|
||||
|
||||
#ifndef mozilla_dom_RemoveTask_h
|
||||
#define mozilla_dom_RemoveTask_h
|
||||
|
||||
#include "mozilla/dom/FileSystemTaskBase.h"
|
||||
#include "mozilla/ErrorResult.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace dom {
|
||||
|
||||
class BlobImpl;
|
||||
class FileSystemRemoveParams;
|
||||
class Promise;
|
||||
|
||||
class RemoveTaskChild final : public FileSystemTaskChildBase
|
||||
{
|
||||
public:
|
||||
static already_AddRefed<RemoveTaskChild>
|
||||
Create(FileSystemBase* aFileSystem,
|
||||
nsIFile* aDirPath,
|
||||
nsIFile* aTargetPath,
|
||||
bool aRecursive,
|
||||
ErrorResult& aRv);
|
||||
|
||||
virtual
|
||||
~RemoveTaskChild();
|
||||
|
||||
already_AddRefed<Promise>
|
||||
GetPromise();
|
||||
|
||||
virtual void
|
||||
GetPermissionAccessType(nsCString& aAccess) const override;
|
||||
|
||||
protected:
|
||||
virtual FileSystemParams
|
||||
GetRequestParams(const nsString& aSerializedDOMPath,
|
||||
ErrorResult& aRv) const override;
|
||||
|
||||
virtual void
|
||||
SetSuccessRequestResult(const FileSystemResponseValue& aValue,
|
||||
ErrorResult& aRv) override;
|
||||
|
||||
virtual void
|
||||
HandlerCallback() override;
|
||||
|
||||
private:
|
||||
RemoveTaskChild(FileSystemBase* aFileSystem,
|
||||
nsIFile* aDirPath,
|
||||
nsIFile* aTargetPath,
|
||||
bool aRecursive);
|
||||
|
||||
RefPtr<Promise> mPromise;
|
||||
|
||||
// This path is the Directory::mFile.
|
||||
nsCOMPtr<nsIFile> mDirPath;
|
||||
|
||||
// This is what we want to remove. mTargetPath is discendant path of mDirPath.
|
||||
nsCOMPtr<nsIFile> mTargetPath;
|
||||
|
||||
bool mRecursive;
|
||||
bool mReturnValue;
|
||||
};
|
||||
|
||||
class RemoveTaskParent final : public FileSystemTaskParentBase
|
||||
{
|
||||
public:
|
||||
static already_AddRefed<RemoveTaskParent>
|
||||
Create(FileSystemBase* aFileSystem,
|
||||
const FileSystemRemoveParams& aParam,
|
||||
FileSystemRequestParent* aParent,
|
||||
ErrorResult& aRv);
|
||||
|
||||
virtual void
|
||||
GetPermissionAccessType(nsCString& aAccess) const override;
|
||||
|
||||
protected:
|
||||
virtual FileSystemResponseValue
|
||||
GetSuccessRequestResult(ErrorResult& aRv) const override;
|
||||
|
||||
virtual nsresult
|
||||
IOWork() override;
|
||||
|
||||
private:
|
||||
RemoveTaskParent(FileSystemBase* aFileSystem,
|
||||
const FileSystemRemoveParams& aParam,
|
||||
FileSystemRequestParent* aParent);
|
||||
|
||||
// This path is the Directory::mFile.
|
||||
nsCOMPtr<nsIFile> mDirPath;
|
||||
|
||||
// This is what we want to remove. mTargetPath is discendant path of mDirPath.
|
||||
nsCOMPtr<nsIFile> mTargetPath;
|
||||
|
||||
bool mRecursive;
|
||||
bool mReturnValue;
|
||||
};
|
||||
|
||||
} // namespace dom
|
||||
} // namespace mozilla
|
||||
|
||||
#endif // mozilla_dom_RemoveTask_h
|
|
@ -35,7 +35,6 @@ UNIFIED_SOURCES += [
|
|||
'GetFilesHelper.cpp',
|
||||
'GetFilesTask.cpp',
|
||||
'OSFileSystem.cpp',
|
||||
'RemoveTask.cpp',
|
||||
]
|
||||
|
||||
FINAL_LIBRARY = 'xul'
|
||||
|
|
|
@ -36,34 +36,6 @@ interface Directory {
|
|||
*/
|
||||
[Func="mozilla::dom::Directory::DeviceStorageEnabled", NewObject]
|
||||
Promise<(File or Directory)> get(DOMString path);
|
||||
|
||||
/*
|
||||
* Deletes a file or an empty directory. The target must be a descendent of
|
||||
* current directory.
|
||||
* @param path If a DOM string is passed, it is the relative path of the
|
||||
* target. Otherwise, the File or Directory object of the target should be
|
||||
* passed.
|
||||
* @return If the target is a non-empty directory, or if deleting the target
|
||||
* fails, the promise is rejected with a DOM error. If the target did not
|
||||
* exist, the promise is resolved with boolean false. If the target did exist
|
||||
* and was successfully deleted, the promise is resolved with boolean true.
|
||||
*/
|
||||
[Func="mozilla::dom::Directory::DeviceStorageEnabled", NewObject]
|
||||
Promise<boolean> remove((DOMString or File or Directory) path);
|
||||
|
||||
/*
|
||||
* Deletes a file or a directory recursively. The target should be a
|
||||
* descendent of current directory.
|
||||
* @param path If a DOM string is passed, it is the relative path of the
|
||||
* target. Otherwise, the File or Directory object of the target should be
|
||||
* passed.
|
||||
* @return If the target exists, but deleting the target fails, the promise is
|
||||
* rejected with a DOM error. If the target did not exist, the promise is
|
||||
* resolved with boolean false. If the target did exist and was successfully
|
||||
* deleted, the promise is resolved with boolean true.
|
||||
*/
|
||||
[Func="mozilla::dom::Directory::DeviceStorageEnabled", NewObject]
|
||||
Promise<boolean> removeDeep((DOMString or File or Directory) path);
|
||||
};
|
||||
|
||||
[Exposed=(Window,Worker)]
|
||||
|
|
Загрузка…
Ссылка в новой задаче