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/. */
|
2017-03-08 22:15:45 +03:00
|
|
|
|
2014-03-05 07:25:40 +04:00
|
|
|
#include "mozilla/dom/FileSystemRequestParent.h"
|
2017-03-08 22:15:45 +03:00
|
|
|
#include "mozilla/dom/PFileSystemParams.h"
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2015-06-23 02:31:32 +03:00
|
|
|
#include "GetDirectoryListingTask.h"
|
2014-03-05 07:25:40 +04:00
|
|
|
#include "GetFileOrDirectoryTask.h"
|
2020-10-27 13:01:12 +03:00
|
|
|
#include "GetFilesTask.h"
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2017-03-16 10:53:49 +03:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2014-03-05 07:25:40 +04:00
|
|
|
#include "mozilla/dom/FileSystemBase.h"
|
2017-03-16 10:53:49 +03:00
|
|
|
#include "mozilla/dom/FileSystemSecurity.h"
|
2017-03-08 22:15:45 +03:00
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
2020-10-27 13:01:12 +03:00
|
|
|
#include "mozilla/dom/OSFileSystem.h"
|
2017-03-16 10:53:49 +03:00
|
|
|
#include "mozilla/Unused.h"
|
|
|
|
#include "nsProxyRelease.h"
|
2017-03-08 22:15:45 +03:00
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2014-03-05 07:25:40 +04:00
|
|
|
|
|
|
|
FileSystemRequestParent::FileSystemRequestParent() : mDestroyed(false) {
|
2016-04-09 21:17:02 +03:00
|
|
|
AssertIsOnBackgroundThread();
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemRequestParent::~FileSystemRequestParent() {
|
2016-04-09 21:17:02 +03:00
|
|
|
AssertIsOnBackgroundThread();
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define FILESYSTEM_REQUEST_PARENT_DISPATCH_ENTRY(name) \
|
|
|
|
case FileSystemParams::TFileSystem##name##Params: { \
|
|
|
|
const FileSystem##name##Params& p = aParams; \
|
2017-03-08 22:15:45 +03:00
|
|
|
mFileSystem = new OSFileSystemParent(p.filesystem()); \
|
2016-04-09 21:17:02 +03:00
|
|
|
MOZ_ASSERT(mFileSystem); \
|
|
|
|
mTask = name##TaskParent::Create(mFileSystem, p, this, 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 false; \
|
|
|
|
} \
|
2014-03-05 07:25:40 +04:00
|
|
|
break; \
|
|
|
|
}
|
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
bool FileSystemRequestParent::Initialize(const FileSystemParams& aParams) {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
|
2014-03-05 07:25:40 +04:00
|
|
|
switch (aParams.type()) {
|
2015-06-23 02:31:32 +03:00
|
|
|
FILESYSTEM_REQUEST_PARENT_DISPATCH_ENTRY(GetDirectoryListing)
|
2014-03-05 07:25:40 +04:00
|
|
|
FILESYSTEM_REQUEST_PARENT_DISPATCH_ENTRY(GetFileOrDirectory)
|
2016-04-13 14:15:56 +03:00
|
|
|
FILESYSTEM_REQUEST_PARENT_DISPATCH_ENTRY(GetFiles)
|
2014-03-05 07:25:40 +04:00
|
|
|
|
|
|
|
default: {
|
2016-12-03 00:46:53 +03:00
|
|
|
MOZ_CRASH("not reached");
|
2014-03-05 07:25:40 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
if (NS_WARN_IF(!mTask || !mFileSystem)) {
|
2014-03-05 07:25:40 +04:00
|
|
|
// Should never reach here.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-16 10:53:49 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CheckPermissionRunnable final : public Runnable {
|
|
|
|
public:
|
|
|
|
CheckPermissionRunnable(already_AddRefed<ContentParent> aParent,
|
|
|
|
FileSystemRequestParent* aActor,
|
|
|
|
FileSystemTaskParentBase* aTask,
|
|
|
|
const nsAString& aPath)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::CheckPermissionRunnable"),
|
|
|
|
mContentParent(aParent),
|
2017-03-16 10:53:49 +03:00
|
|
|
mActor(aActor),
|
|
|
|
mTask(aTask),
|
|
|
|
mPath(aPath),
|
2020-06-23 08:05:36 +03:00
|
|
|
mBackgroundEventTarget(GetCurrentEventTarget()) {
|
2017-03-16 10:53:49 +03:00
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
MOZ_ASSERT(mContentParent);
|
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
MOZ_ASSERT(mTask);
|
|
|
|
MOZ_ASSERT(mBackgroundEventTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
auto raii = mozilla::MakeScopeExit([&] { mContentParent = nullptr; });
|
|
|
|
|
|
|
|
if (!mozilla::Preferences::GetBool("dom.filesystem.pathcheck.disabled",
|
|
|
|
false)) {
|
|
|
|
RefPtr<FileSystemSecurity> fss = FileSystemSecurity::Get();
|
|
|
|
if (NS_WARN_IF(!fss || !fss->ContentProcessHasAccessTo(
|
|
|
|
mContentParent->ChildID(), mPath))) {
|
|
|
|
mContentParent->KillHard("This path is not allowed.");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mBackgroundEventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
// It can happen that this actor has been destroyed in the meantime we were
|
|
|
|
// on the main-thread.
|
|
|
|
if (!mActor->Destroyed()) {
|
|
|
|
mTask->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~CheckPermissionRunnable() {
|
2017-06-14 04:27:17 +03:00
|
|
|
NS_ProxyRelease("CheckPermissionRunnable::mActor", mBackgroundEventTarget,
|
|
|
|
mActor.forget());
|
2017-03-16 10:53:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ContentParent> mContentParent;
|
|
|
|
RefPtr<FileSystemRequestParent> mActor;
|
|
|
|
RefPtr<FileSystemTaskParentBase> mTask;
|
|
|
|
const nsString mPath;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> mBackgroundEventTarget;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
void FileSystemRequestParent::Start() {
|
2017-03-16 10:53:49 +03:00
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
2016-04-09 21:17:02 +03:00
|
|
|
MOZ_ASSERT(!mDestroyed);
|
|
|
|
MOZ_ASSERT(mFileSystem);
|
|
|
|
MOZ_ASSERT(mTask);
|
|
|
|
|
2017-03-16 10:53:49 +03:00
|
|
|
nsAutoString path;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(mTask->GetTargetPath(path)))) {
|
|
|
|
Unused << Send__delete__(
|
|
|
|
this, FileSystemErrorResponse(NS_ERROR_DOM_SECURITY_ERR));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ContentParent> parent = BackgroundParent::GetContentParent(Manager());
|
|
|
|
|
|
|
|
// If the ContentParent is null we are dealing with a same-process actor.
|
|
|
|
if (!parent) {
|
|
|
|
mTask->Start();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Runnable> runnable =
|
|
|
|
new CheckPermissionRunnable(parent.forget(), this, mTask, path);
|
|
|
|
NS_DispatchToMainThread(runnable);
|
2016-04-09 21:17:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileSystemRequestParent::ActorDestroy(ActorDestroyReason aWhy) {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_ASSERT(!mDestroyed);
|
|
|
|
|
2014-03-05 07:25:40 +04:00
|
|
|
if (!mFileSystem) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-09 21:17:02 +03:00
|
|
|
|
2014-03-05 07:25:40 +04:00
|
|
|
mFileSystem->Shutdown();
|
|
|
|
mFileSystem = nullptr;
|
2016-04-09 21:17:02 +03:00
|
|
|
mTask = nullptr;
|
|
|
|
mDestroyed = true;
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|