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 "CreateDirectoryTask.h" michael@0: michael@0: #include "DOMError.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 "nsIFile.h" michael@0: #include "nsStringGlue.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: CreateDirectoryTask::CreateDirectoryTask(FileSystemBase* aFileSystem, michael@0: const nsAString& aPath) michael@0: : FileSystemTaskBase(aFileSystem) michael@0: , mTargetRealPath(aPath) 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: CreateDirectoryTask::CreateDirectoryTask( michael@0: FileSystemBase* aFileSystem, michael@0: const FileSystemCreateDirectoryParams& aParam, michael@0: FileSystemRequestParent* aParent) michael@0: : FileSystemTaskBase(aFileSystem, aParam, aParent) 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: CreateDirectoryTask::~CreateDirectoryTask() 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: CreateDirectoryTask::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: CreateDirectoryTask::GetRequestParams(const nsString& aFileSystem) const michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: return FileSystemCreateDirectoryParams(aFileSystem, mTargetRealPath); michael@0: } michael@0: michael@0: FileSystemResponseValue michael@0: CreateDirectoryTask::GetSuccessRequestResult() const michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: return FileSystemDirectoryResponse(mTargetRealPath); michael@0: } michael@0: michael@0: void michael@0: CreateDirectoryTask::SetSuccessRequestResult(const FileSystemResponseValue& aValue) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); michael@0: FileSystemDirectoryResponse r = aValue; michael@0: mTargetRealPath = r.realPath(); michael@0: } michael@0: michael@0: nsresult michael@0: CreateDirectoryTask::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: 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 fileExists; michael@0: nsresult rv = file->Exists(&fileExists); michael@0: if (NS_WARN_IF(NS_FAILED(rv))) { michael@0: return rv; michael@0: } michael@0: michael@0: if (fileExists) { michael@0: return NS_ERROR_DOM_FILESYSTEM_PATH_EXISTS_ERR; michael@0: } michael@0: michael@0: rv = file->Create(nsIFile::DIRECTORY_TYPE, 0770); michael@0: return rv; michael@0: } michael@0: michael@0: void michael@0: CreateDirectoryTask::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: nsRefPtr dir = new Directory(mFileSystem, mTargetRealPath); michael@0: mPromise->MaybeResolve(dir); michael@0: mPromise = nullptr; michael@0: } michael@0: michael@0: void michael@0: CreateDirectoryTask::GetPermissionAccessType(nsCString& aAccess) const michael@0: { michael@0: aAccess.AssignLiteral("create"); michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla