2015-05-03 22:32:37 +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: */
|
2014-03-05 07:25:40 +04:00
|
|
|
/* 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 "mozilla/dom/FileSystemTaskBase.h"
|
|
|
|
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsNetCID.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-03-05 07:25:40 +04:00
|
|
|
#include "mozilla/dom/FileSystemBase.h"
|
|
|
|
#include "mozilla/dom/FileSystemRequestParent.h"
|
|
|
|
#include "mozilla/dom/FileSystemUtils.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2017-03-08 22:15:45 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
2016-04-09 21:17:02 +03:00
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
2017-03-08 22:15:45 +03:00
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-12-30 11:53:20 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2014-03-05 07:25:40 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-12-30 11:53:20 +03:00
|
|
|
namespace {
|
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
nsresult
|
|
|
|
FileSystemErrorFromNsError(const nsresult& aErrorValue)
|
2015-12-30 11:53:20 +03:00
|
|
|
{
|
2016-04-09 21:17:02 +03:00
|
|
|
uint16_t module = NS_ERROR_GET_MODULE(aErrorValue);
|
|
|
|
if (module == NS_ERROR_MODULE_DOM_FILESYSTEM ||
|
|
|
|
module == NS_ERROR_MODULE_DOM_FILE ||
|
|
|
|
module == NS_ERROR_MODULE_DOM) {
|
|
|
|
return aErrorValue;
|
2015-12-30 11:53:20 +03:00
|
|
|
}
|
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
switch (aErrorValue) {
|
|
|
|
case NS_OK:
|
|
|
|
return NS_OK;
|
2015-12-30 11:53:20 +03:00
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
case NS_ERROR_FILE_INVALID_PATH:
|
|
|
|
case NS_ERROR_FILE_UNRECOGNIZED_PATH:
|
|
|
|
return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
|
2015-12-30 11:53:20 +03:00
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
case NS_ERROR_FILE_DESTINATION_NOT_DIR:
|
|
|
|
return NS_ERROR_DOM_FILESYSTEM_INVALID_MODIFICATION_ERR;
|
2015-12-30 11:53:20 +03:00
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
case NS_ERROR_FILE_ACCESS_DENIED:
|
|
|
|
case NS_ERROR_FILE_DIR_NOT_EMPTY:
|
|
|
|
return NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR;
|
|
|
|
|
|
|
|
case NS_ERROR_FILE_TARGET_DOES_NOT_EXIST:
|
|
|
|
case NS_ERROR_NOT_AVAILABLE:
|
|
|
|
return NS_ERROR_DOM_FILE_NOT_FOUND_ERR;
|
|
|
|
|
|
|
|
case NS_ERROR_FILE_ALREADY_EXISTS:
|
|
|
|
return NS_ERROR_DOM_FILESYSTEM_PATH_EXISTS_ERR;
|
|
|
|
|
|
|
|
case NS_ERROR_FILE_NOT_DIRECTORY:
|
|
|
|
return NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR;
|
|
|
|
|
|
|
|
case NS_ERROR_UNEXPECTED:
|
|
|
|
default:
|
|
|
|
return NS_ERROR_DOM_FILESYSTEM_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DispatchToIOThread(nsIRunnable* aRunnable)
|
2014-03-05 07:25:40 +04:00
|
|
|
{
|
2016-04-09 21:17:02 +03:00
|
|
|
MOZ_ASSERT(aRunnable);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> target
|
|
|
|
= do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
MOZ_ASSERT(target);
|
|
|
|
|
|
|
|
return target->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
|
|
|
|
2016-04-12 15:50:38 +03:00
|
|
|
// This runnable is used when an error value is set before doing any real
|
|
|
|
// operation on the I/O thread. In this case we skip all and we directly
|
|
|
|
// communicate the error.
|
2016-04-11 21:40:06 +03:00
|
|
|
class ErrorRunnable final : public CancelableRunnable
|
2016-04-12 15:50:38 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit ErrorRunnable(FileSystemTaskChildBase* aTask)
|
2017-04-05 10:47:09 +03:00
|
|
|
: CancelableRunnable("ErrorRunnable")
|
|
|
|
, mTask(aTask)
|
2016-04-12 15:50:38 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mTask->HasError());
|
|
|
|
|
|
|
|
mTask->HandlerCallback();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<FileSystemTaskChildBase> mTask;
|
|
|
|
};
|
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2017-03-08 22:15:45 +03:00
|
|
|
NS_IMPL_ISUPPORTS(FileSystemTaskChildBase, nsIIPCBackgroundChildCreateCallback)
|
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
/**
|
|
|
|
* FileSystemTaskBase class
|
|
|
|
*/
|
|
|
|
|
2017-04-05 10:47:56 +03:00
|
|
|
FileSystemTaskChildBase::FileSystemTaskChildBase(nsIGlobalObject* aGlobalObject,
|
|
|
|
FileSystemBase* aFileSystem)
|
2014-03-05 07:25:40 +04:00
|
|
|
: mErrorValue(NS_OK)
|
|
|
|
, mFileSystem(aFileSystem)
|
2017-04-05 10:47:56 +03:00
|
|
|
, mGlobalObject(aGlobalObject)
|
2014-03-05 07:25:40 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFileSystem, "aFileSystem should not be null.");
|
2016-04-12 15:50:38 +03:00
|
|
|
aFileSystem->AssertIsOnOwningThread();
|
2017-04-05 10:47:56 +03:00
|
|
|
MOZ_ASSERT(aGlobalObject);
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
|
|
|
|
2016-04-09 21:17:28 +03:00
|
|
|
FileSystemTaskChildBase::~FileSystemTaskChildBase()
|
2014-03-05 07:25:40 +04:00
|
|
|
{
|
2016-04-09 21:17:02 +03:00
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
|
|
|
|
2014-03-05 07:24:19 +04:00
|
|
|
FileSystemBase*
|
2016-04-09 21:17:28 +03:00
|
|
|
FileSystemTaskChildBase::GetFileSystem() const
|
2014-03-05 07:24:19 +04:00
|
|
|
{
|
2016-04-09 21:17:02 +03:00
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
2014-03-05 07:24:19 +04:00
|
|
|
return mFileSystem.get();
|
|
|
|
}
|
|
|
|
|
2014-03-05 07:25:40 +04:00
|
|
|
void
|
2016-04-09 21:17:28 +03:00
|
|
|
FileSystemTaskChildBase::Start()
|
2014-03-05 07:25:40 +04:00
|
|
|
{
|
2016-04-09 21:17:02 +03:00
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2017-03-08 22:15:45 +03:00
|
|
|
mozilla::ipc::PBackgroundChild* actor =
|
|
|
|
mozilla::ipc::BackgroundChild::GetForCurrentThread();
|
|
|
|
if (actor) {
|
|
|
|
ActorCreated(actor);
|
|
|
|
} else {
|
|
|
|
if (NS_WARN_IF(
|
|
|
|
!mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread(this))) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileSystemTaskChildBase::ActorFailed()
|
|
|
|
{
|
|
|
|
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileSystemTaskChildBase::ActorCreated(mozilla::ipc::PBackgroundChild* aActor)
|
|
|
|
{
|
2014-03-05 07:25:40 +04:00
|
|
|
if (HasError()) {
|
2016-04-12 15:50:38 +03:00
|
|
|
// In this case we don't want to use IPC at all.
|
|
|
|
RefPtr<ErrorRunnable> runnable = new ErrorRunnable(this);
|
2017-04-05 10:47:56 +03:00
|
|
|
FileSystemUtils::DispatchRunnable(mGlobalObject, runnable.forget());
|
2014-03-05 07:25:40 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFileSystem->IsShutdown()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
nsAutoString serialization;
|
|
|
|
mFileSystem->SerializeDOMPath(serialization);
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
ErrorResult rv;
|
2016-03-20 13:56:10 +03:00
|
|
|
FileSystemParams params = GetRequestParams(serialization, rv);
|
2016-03-20 13:56:10 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2016-07-22 17:50:10 +03:00
|
|
|
rv.SuppressException();
|
2016-03-20 13:56:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-05 07:25:40 +04:00
|
|
|
// Retain a reference so the task object isn't deleted without IPDL's
|
|
|
|
// knowledge. The reference will be released by
|
2016-04-09 21:17:02 +03:00
|
|
|
// mozilla::ipc::BackgroundChildImpl::DeallocPFileSystemRequestChild.
|
2014-03-05 07:25:40 +04:00
|
|
|
NS_ADDREF_THIS();
|
2016-03-20 13:56:10 +03:00
|
|
|
|
2017-04-05 10:47:56 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsIEventTarget* target = mGlobalObject->EventTargetFor(TaskCategory::Other);
|
|
|
|
MOZ_ASSERT(target);
|
|
|
|
|
|
|
|
aActor->SetEventTargetForActor(this, target);
|
|
|
|
}
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2017-04-05 10:47:56 +03:00
|
|
|
aActor->SendPFileSystemRequestConstructor(this, params);
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-04-09 21:17:28 +03:00
|
|
|
FileSystemTaskChildBase::SetRequestResult(const FileSystemResponseValue& aValue)
|
2014-03-05 07:25:40 +04:00
|
|
|
{
|
2016-04-09 21:17:02 +03:00
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
|
|
|
|
2014-03-05 07:25:40 +04:00
|
|
|
if (aValue.type() == FileSystemResponseValue::TFileSystemErrorResponse) {
|
|
|
|
FileSystemErrorResponse r = aValue;
|
|
|
|
mErrorValue = r.error();
|
|
|
|
} else {
|
2016-03-20 13:56:10 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
SetSuccessRequestResult(aValue, rv);
|
|
|
|
mErrorValue = rv.StealNSResult();
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2016-04-09 21:17:28 +03:00
|
|
|
FileSystemTaskChildBase::Recv__delete__(const FileSystemResponseValue& aValue)
|
2014-03-05 07:25:40 +04:00
|
|
|
{
|
2016-04-09 21:17:02 +03:00
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
|
|
|
|
2014-03-05 07:25:40 +04:00
|
|
|
SetRequestResult(aValue);
|
|
|
|
HandlerCallback();
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-04-09 21:17:28 +03:00
|
|
|
FileSystemTaskChildBase::SetError(const nsresult& aErrorValue)
|
2014-03-05 07:25:40 +04:00
|
|
|
{
|
2016-04-09 21:17:02 +03:00
|
|
|
mErrorValue = FileSystemErrorFromNsError(aErrorValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FileSystemTaskParentBase class
|
|
|
|
*/
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
FileSystemTaskParentBase::FileSystemTaskParentBase(
|
|
|
|
FileSystemBase* aFileSystem,
|
|
|
|
const FileSystemParams& aParam,
|
|
|
|
FileSystemRequestParent* aParent)
|
|
|
|
: Runnable("dom::FileSystemTaskParentBase")
|
|
|
|
, mErrorValue(NS_OK)
|
2016-04-09 21:17:02 +03:00
|
|
|
, mFileSystem(aFileSystem)
|
|
|
|
, mRequestParent(aParent)
|
2017-06-01 23:42:05 +03:00
|
|
|
, mBackgroundEventTarget(GetCurrentThreadEventTarget())
|
2016-04-09 21:17:02 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess(),
|
|
|
|
"Only call from parent process!");
|
|
|
|
MOZ_ASSERT(aFileSystem, "aFileSystem should not be null.");
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
MOZ_ASSERT(mBackgroundEventTarget);
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemTaskParentBase::~FileSystemTaskParentBase()
|
|
|
|
{
|
|
|
|
// This task can be released on different threads because we dispatch it (as
|
|
|
|
// runnable) to main-thread, I/O and then back to the PBackground thread.
|
2017-06-14 04:27:17 +03:00
|
|
|
NS_ProxyRelease(
|
|
|
|
"FileSystemTaskParentBase::mFileSystem",
|
|
|
|
mBackgroundEventTarget, mFileSystem.forget());
|
|
|
|
NS_ProxyRelease(
|
|
|
|
"FileSystemTaskParentBase::mRequestParent",
|
|
|
|
mBackgroundEventTarget, mRequestParent.forget());
|
2016-04-09 21:17:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileSystemTaskParentBase::Start()
|
|
|
|
{
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
|
|
|
|
2016-09-02 10:12:24 +03:00
|
|
|
DebugOnly<nsresult> rv = DispatchToIOThread(this);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "DispatchToIOThread failed");
|
2016-04-09 21:17:02 +03:00
|
|
|
}
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
void
|
|
|
|
FileSystemTaskParentBase::HandleResult()
|
|
|
|
{
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
if (mFileSystem->IsShutdown()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
MOZ_ASSERT(mRequestParent);
|
|
|
|
Unused << mRequestParent->Send__delete__(mRequestParent, GetRequestResult());
|
|
|
|
}
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
FileSystemResponseValue
|
|
|
|
FileSystemTaskParentBase::GetRequestResult() const
|
|
|
|
{
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
mFileSystem->AssertIsOnOwningThread();
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
if (HasError()) {
|
|
|
|
return FileSystemErrorResponse(mErrorValue);
|
|
|
|
}
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
FileSystemResponseValue value = GetSuccessRequestResult(rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return FileSystemErrorResponse(rv.StealNSResult());
|
|
|
|
}
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileSystemTaskParentBase::SetError(const nsresult& aErrorValue)
|
|
|
|
{
|
|
|
|
mErrorValue = FileSystemErrorFromNsError(aErrorValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileSystemTaskParentBase::Run()
|
|
|
|
{
|
2017-03-14 13:38:06 +03:00
|
|
|
// This method can run in 2 different threads. Here why:
|
|
|
|
// 1. We are are on the I/O thread and we call IOWork().
|
|
|
|
// 2. After step 1, it returns back to the PBackground thread.
|
2016-04-09 21:17:02 +03:00
|
|
|
|
|
|
|
// Run I/O thread tasks
|
|
|
|
if (!IsOnBackgroundThread()) {
|
|
|
|
nsresult rv = IOWork();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
SetError(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's go back to PBackground thread to finish the work.
|
|
|
|
rv = mBackgroundEventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
2016-04-09 21:17:02 +03:00
|
|
|
|
|
|
|
// If we are here, it's because the I/O work has been done and we have to
|
|
|
|
// handle the result back via IPC.
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
HandleResult();
|
|
|
|
return NS_OK;
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|