michael@0: /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "RemoveTask.h" michael@0: michael@0: #include "DOMError.h" michael@0: #include "mozilla/dom/FileSystemBase.h" michael@0: #include "mozilla/dom/FileSystemUtils.h" michael@0: #include "mozilla/dom/Promise.h" michael@0: #include "nsIDOMFile.h" michael@0: #include "nsIFile.h" michael@0: #include "nsStringGlue.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: RemoveTask::RemoveTask(FileSystemBase* aFileSystem, michael@0: const nsAString& aDirPath, michael@0: nsIDOMFile* aTargetFile, michael@0: const nsAString& aTargetPath, michael@0: bool aRecursive) michael@0: : FileSystemTaskBase(aFileSystem) michael@0: , mDirRealPath(aDirPath) michael@0: , mTargetFile(aTargetFile) michael@0: , mTargetRealPath(aTargetPath) michael@0: , mRecursive(aRecursive) michael@0: , mReturnValue(false) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: MOZ_ASSERT(aFileSystem); michael@0: nsCOMPtr globalObject = michael@0: do_QueryInterface(aFileSystem->GetWindow()); michael@0: if (!globalObject) { michael@0: return; michael@0: } michael@0: mPromise = new Promise(globalObject); michael@0: } michael@0: michael@0: RemoveTask::RemoveTask(FileSystemBase* aFileSystem, michael@0: const FileSystemRemoveParams& aParam, michael@0: FileSystemRequestParent* aParent) michael@0: : FileSystemTaskBase(aFileSystem, aParam, aParent) michael@0: , mRecursive(false) michael@0: , mReturnValue(false) michael@0: { michael@0: MOZ_ASSERT(FileSystemUtils::IsParentProcess(), michael@0: "Only call from parent process!"); michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: MOZ_ASSERT(aFileSystem); michael@0: michael@0: mDirRealPath = aParam.directory(); michael@0: michael@0: mRecursive = aParam.recursive(); michael@0: michael@0: const FileSystemPathOrFileValue& target = aParam.target(); michael@0: michael@0: if (target.type() == FileSystemPathOrFileValue::TnsString) { michael@0: mTargetRealPath = target; michael@0: return; michael@0: } michael@0: michael@0: BlobParent* bp = static_cast(static_cast(target)); michael@0: nsCOMPtr blob = bp->GetBlob(); michael@0: mTargetFile = do_QueryInterface(blob); michael@0: MOZ_ASSERT(mTargetFile, "mTargetFile should not be null."); michael@0: } michael@0: michael@0: RemoveTask::~RemoveTask() michael@0: { michael@0: MOZ_ASSERT(!mPromise || NS_IsMainThread(), michael@0: "mPromise should be released on main thread!"); michael@0: } michael@0: michael@0: already_AddRefed michael@0: RemoveTask::GetPromise() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: return nsRefPtr(mPromise).forget(); michael@0: } michael@0: michael@0: FileSystemParams michael@0: RemoveTask::GetRequestParams(const nsString& aFileSystem) const michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: FileSystemRemoveParams param; michael@0: param.filesystem() = aFileSystem; michael@0: param.directory() = mDirRealPath; michael@0: param.recursive() = mRecursive; michael@0: if (mTargetFile) { michael@0: BlobChild* actor michael@0: = ContentChild::GetSingleton()->GetOrCreateActorForBlob(mTargetFile); michael@0: if (actor) { michael@0: param.target() = actor; michael@0: } michael@0: } else { michael@0: param.target() = mTargetRealPath; michael@0: } michael@0: return param; michael@0: } michael@0: michael@0: FileSystemResponseValue michael@0: RemoveTask::GetSuccessRequestResult() const michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: return FileSystemBooleanResponse(mReturnValue); michael@0: } michael@0: michael@0: void michael@0: RemoveTask::SetSuccessRequestResult(const FileSystemResponseValue& aValue) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: FileSystemBooleanResponse r = aValue; michael@0: mReturnValue = r.success(); michael@0: } michael@0: michael@0: nsresult michael@0: RemoveTask::Work() michael@0: { michael@0: MOZ_ASSERT(FileSystemUtils::IsParentProcess(), michael@0: "Only call from parent process!"); michael@0: MOZ_ASSERT(!NS_IsMainThread(), "Only call on worker thread!"); michael@0: michael@0: if (mFileSystem->IsShutdown()) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // Get the DOM path if a DOMFile is passed as the target. michael@0: if (mTargetFile) { michael@0: if (!mFileSystem->GetRealPath(mTargetFile, mTargetRealPath)) { michael@0: return NS_ERROR_DOM_SECURITY_ERR; michael@0: } michael@0: if (!FileSystemUtils::IsDescendantPath(mDirRealPath, mTargetRealPath)) { michael@0: return NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR; michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr file = mFileSystem->GetLocalFile(mTargetRealPath); michael@0: if (!file) { michael@0: return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR; michael@0: } michael@0: michael@0: bool exists = false; michael@0: nsresult rv = file->Exists(&exists); michael@0: if (NS_WARN_IF(NS_FAILED(rv))) { michael@0: return rv; michael@0: } michael@0: michael@0: if (!exists) { michael@0: mReturnValue = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool isFile = false; michael@0: rv = file->IsFile(&isFile); michael@0: if (NS_WARN_IF(NS_FAILED(rv))) { michael@0: return rv; michael@0: } michael@0: michael@0: if (isFile && !mFileSystem->IsSafeFile(file)) { michael@0: return NS_ERROR_DOM_SECURITY_ERR; michael@0: } michael@0: michael@0: rv = file->Remove(mRecursive); michael@0: if (NS_WARN_IF(NS_FAILED(rv))) { michael@0: return rv; michael@0: } michael@0: michael@0: mReturnValue = true; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: RemoveTask::HandlerCallback() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: if (mFileSystem->IsShutdown()) { michael@0: mPromise = nullptr; michael@0: return; michael@0: } michael@0: michael@0: if (HasError()) { michael@0: nsRefPtr domError = new DOMError(mFileSystem->GetWindow(), michael@0: mErrorValue); michael@0: mPromise->MaybeReject(domError); michael@0: mPromise = nullptr; michael@0: return; michael@0: } michael@0: michael@0: mPromise->MaybeResolve(mReturnValue); michael@0: mPromise = nullptr; michael@0: } michael@0: michael@0: void michael@0: RemoveTask::GetPermissionAccessType(nsCString& aAccess) const michael@0: { michael@0: aAccess.AssignLiteral("write"); michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla