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 "GetFileOrDirectoryTask.h" michael@0: michael@0: #include "js/Value.h" michael@0: #include "mozilla/dom/Directory.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 "nsDOMFile.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: GetFileOrDirectoryTask::GetFileOrDirectoryTask( michael@0: FileSystemBase* aFileSystem, michael@0: const nsAString& aTargetPath, michael@0: bool aDirectoryOnly) michael@0: : FileSystemTaskBase(aFileSystem) michael@0: , mTargetRealPath(aTargetPath) michael@0: , mIsDirectory(aDirectoryOnly) 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: GetFileOrDirectoryTask::GetFileOrDirectoryTask( michael@0: FileSystemBase* aFileSystem, michael@0: const FileSystemGetFileOrDirectoryParams& aParam, michael@0: FileSystemRequestParent* aParent) michael@0: : FileSystemTaskBase(aFileSystem, aParam, aParent) michael@0: , mIsDirectory(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: mTargetRealPath = aParam.realPath(); michael@0: } michael@0: michael@0: GetFileOrDirectoryTask::~GetFileOrDirectoryTask() 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: GetFileOrDirectoryTask::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: GetFileOrDirectoryTask::GetRequestParams(const nsString& aFileSystem) const michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: return FileSystemGetFileOrDirectoryParams(aFileSystem, mTargetRealPath); michael@0: } michael@0: michael@0: FileSystemResponseValue michael@0: GetFileOrDirectoryTask::GetSuccessRequestResult() const michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: if (mIsDirectory) { michael@0: return FileSystemDirectoryResponse(mTargetRealPath); michael@0: } michael@0: BlobParent* actor = GetBlobParent(mTargetFile); michael@0: if (!actor) { michael@0: return FileSystemErrorResponse(NS_ERROR_DOM_FILESYSTEM_UNKNOWN_ERR); michael@0: } michael@0: FileSystemFileResponse response; michael@0: response.blobParent() = actor; michael@0: return response; michael@0: } michael@0: michael@0: void michael@0: GetFileOrDirectoryTask::SetSuccessRequestResult(const FileSystemResponseValue& aValue) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: switch (aValue.type()) { michael@0: case FileSystemResponseValue::TFileSystemFileResponse: { michael@0: FileSystemFileResponse r = aValue; michael@0: BlobChild* actor = static_cast(r.blobChild()); michael@0: nsCOMPtr blob = actor->GetBlob(); michael@0: mTargetFile = do_QueryInterface(blob); michael@0: mIsDirectory = false; michael@0: break; michael@0: } michael@0: case FileSystemResponseValue::TFileSystemDirectoryResponse: { michael@0: FileSystemDirectoryResponse r = aValue; michael@0: mTargetRealPath = r.realPath(); michael@0: mIsDirectory = true; michael@0: break; michael@0: } michael@0: default: { michael@0: NS_RUNTIMEABORT("not reached"); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: GetFileOrDirectoryTask::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: // Whether we want to get the root directory. michael@0: bool getRoot = mTargetRealPath.IsEmpty(); 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; 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: if (!getRoot) { michael@0: return NS_ERROR_DOM_FILE_NOT_FOUND_ERR; michael@0: } michael@0: michael@0: // If the root directory doesn't exit, create it. michael@0: rv = file->Create(nsIFile::DIRECTORY_TYPE, 0777); michael@0: if (NS_WARN_IF(NS_FAILED(rv))) { michael@0: return rv; michael@0: } michael@0: } michael@0: michael@0: // Get isDirectory. michael@0: rv = file->IsDirectory(&mIsDirectory); michael@0: if (NS_WARN_IF(NS_FAILED(rv))) { michael@0: return rv; michael@0: } michael@0: michael@0: if (mIsDirectory) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Check if the root is a directory. michael@0: if (getRoot) { michael@0: return NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR; michael@0: } michael@0: michael@0: bool isFile; michael@0: // Get isFile 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) { michael@0: // Neither directory or file. michael@0: return NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR; michael@0: } michael@0: michael@0: if (!mFileSystem->IsSafeFile(file)) { michael@0: return NS_ERROR_DOM_SECURITY_ERR; michael@0: } michael@0: michael@0: mTargetFile = new nsDOMFileFile(file); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: GetFileOrDirectoryTask::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: if (mIsDirectory) { michael@0: nsRefPtr dir = new Directory(mFileSystem, mTargetRealPath); michael@0: mPromise->MaybeResolve(dir); michael@0: mPromise = nullptr; michael@0: return; michael@0: } michael@0: michael@0: mPromise->MaybeResolve(mTargetFile); michael@0: mPromise = nullptr; michael@0: } michael@0: michael@0: void michael@0: GetFileOrDirectoryTask::GetPermissionAccessType(nsCString& aAccess) const michael@0: { michael@0: aAccess.AssignLiteral("read"); michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla