Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "CreateDirectoryTask.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "DOMError.h" |
michael@0 | 10 | #include "mozilla/dom/Directory.h" |
michael@0 | 11 | #include "mozilla/dom/FileSystemBase.h" |
michael@0 | 12 | #include "mozilla/dom/FileSystemUtils.h" |
michael@0 | 13 | #include "mozilla/dom/Promise.h" |
michael@0 | 14 | #include "nsIFile.h" |
michael@0 | 15 | #include "nsStringGlue.h" |
michael@0 | 16 | |
michael@0 | 17 | namespace mozilla { |
michael@0 | 18 | namespace dom { |
michael@0 | 19 | |
michael@0 | 20 | CreateDirectoryTask::CreateDirectoryTask(FileSystemBase* aFileSystem, |
michael@0 | 21 | const nsAString& aPath) |
michael@0 | 22 | : FileSystemTaskBase(aFileSystem) |
michael@0 | 23 | , mTargetRealPath(aPath) |
michael@0 | 24 | { |
michael@0 | 25 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 26 | MOZ_ASSERT(aFileSystem); |
michael@0 | 27 | nsCOMPtr<nsIGlobalObject> globalObject = |
michael@0 | 28 | do_QueryInterface(aFileSystem->GetWindow()); |
michael@0 | 29 | if (!globalObject) { |
michael@0 | 30 | return; |
michael@0 | 31 | } |
michael@0 | 32 | mPromise = new Promise(globalObject); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | CreateDirectoryTask::CreateDirectoryTask( |
michael@0 | 36 | FileSystemBase* aFileSystem, |
michael@0 | 37 | const FileSystemCreateDirectoryParams& aParam, |
michael@0 | 38 | FileSystemRequestParent* aParent) |
michael@0 | 39 | : FileSystemTaskBase(aFileSystem, aParam, aParent) |
michael@0 | 40 | { |
michael@0 | 41 | MOZ_ASSERT(FileSystemUtils::IsParentProcess(), |
michael@0 | 42 | "Only call from parent process!"); |
michael@0 | 43 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 44 | MOZ_ASSERT(aFileSystem); |
michael@0 | 45 | mTargetRealPath = aParam.realPath(); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | CreateDirectoryTask::~CreateDirectoryTask() |
michael@0 | 49 | { |
michael@0 | 50 | MOZ_ASSERT(!mPromise || NS_IsMainThread(), |
michael@0 | 51 | "mPromise should be released on main thread!"); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | already_AddRefed<Promise> |
michael@0 | 55 | CreateDirectoryTask::GetPromise() |
michael@0 | 56 | { |
michael@0 | 57 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 58 | return nsRefPtr<Promise>(mPromise).forget(); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | FileSystemParams |
michael@0 | 62 | CreateDirectoryTask::GetRequestParams(const nsString& aFileSystem) const |
michael@0 | 63 | { |
michael@0 | 64 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 65 | return FileSystemCreateDirectoryParams(aFileSystem, mTargetRealPath); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | FileSystemResponseValue |
michael@0 | 69 | CreateDirectoryTask::GetSuccessRequestResult() const |
michael@0 | 70 | { |
michael@0 | 71 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 72 | return FileSystemDirectoryResponse(mTargetRealPath); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | void |
michael@0 | 76 | CreateDirectoryTask::SetSuccessRequestResult(const FileSystemResponseValue& aValue) |
michael@0 | 77 | { |
michael@0 | 78 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 79 | FileSystemDirectoryResponse r = aValue; |
michael@0 | 80 | mTargetRealPath = r.realPath(); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | nsresult |
michael@0 | 84 | CreateDirectoryTask::Work() |
michael@0 | 85 | { |
michael@0 | 86 | MOZ_ASSERT(FileSystemUtils::IsParentProcess(), |
michael@0 | 87 | "Only call from parent process!"); |
michael@0 | 88 | MOZ_ASSERT(!NS_IsMainThread(), "Only call on worker thread!"); |
michael@0 | 89 | |
michael@0 | 90 | if (mFileSystem->IsShutdown()) { |
michael@0 | 91 | return NS_ERROR_FAILURE; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | nsCOMPtr<nsIFile> file = mFileSystem->GetLocalFile(mTargetRealPath); |
michael@0 | 95 | if (!file) { |
michael@0 | 96 | return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | bool fileExists; |
michael@0 | 100 | nsresult rv = file->Exists(&fileExists); |
michael@0 | 101 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 102 | return rv; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | if (fileExists) { |
michael@0 | 106 | return NS_ERROR_DOM_FILESYSTEM_PATH_EXISTS_ERR; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | rv = file->Create(nsIFile::DIRECTORY_TYPE, 0770); |
michael@0 | 110 | return rv; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | void |
michael@0 | 114 | CreateDirectoryTask::HandlerCallback() |
michael@0 | 115 | { |
michael@0 | 116 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 117 | if (mFileSystem->IsShutdown()) { |
michael@0 | 118 | mPromise = nullptr; |
michael@0 | 119 | return; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | if (HasError()) { |
michael@0 | 123 | nsRefPtr<DOMError> domError = new DOMError(mFileSystem->GetWindow(), |
michael@0 | 124 | mErrorValue); |
michael@0 | 125 | mPromise->MaybeReject(domError); |
michael@0 | 126 | mPromise = nullptr; |
michael@0 | 127 | return; |
michael@0 | 128 | } |
michael@0 | 129 | nsRefPtr<Directory> dir = new Directory(mFileSystem, mTargetRealPath); |
michael@0 | 130 | mPromise->MaybeResolve(dir); |
michael@0 | 131 | mPromise = nullptr; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | void |
michael@0 | 135 | CreateDirectoryTask::GetPermissionAccessType(nsCString& aAccess) const |
michael@0 | 136 | { |
michael@0 | 137 | aAccess.AssignLiteral("create"); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | } // namespace dom |
michael@0 | 141 | } // namespace mozilla |