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 "GetFileOrDirectoryTask.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "js/Value.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 "nsDOMFile.h" |
michael@0 | 15 | #include "nsIFile.h" |
michael@0 | 16 | #include "nsStringGlue.h" |
michael@0 | 17 | |
michael@0 | 18 | namespace mozilla { |
michael@0 | 19 | namespace dom { |
michael@0 | 20 | |
michael@0 | 21 | GetFileOrDirectoryTask::GetFileOrDirectoryTask( |
michael@0 | 22 | FileSystemBase* aFileSystem, |
michael@0 | 23 | const nsAString& aTargetPath, |
michael@0 | 24 | bool aDirectoryOnly) |
michael@0 | 25 | : FileSystemTaskBase(aFileSystem) |
michael@0 | 26 | , mTargetRealPath(aTargetPath) |
michael@0 | 27 | , mIsDirectory(aDirectoryOnly) |
michael@0 | 28 | { |
michael@0 | 29 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 30 | MOZ_ASSERT(aFileSystem); |
michael@0 | 31 | nsCOMPtr<nsIGlobalObject> globalObject = |
michael@0 | 32 | do_QueryInterface(aFileSystem->GetWindow()); |
michael@0 | 33 | if (!globalObject) { |
michael@0 | 34 | return; |
michael@0 | 35 | } |
michael@0 | 36 | mPromise = new Promise(globalObject); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | GetFileOrDirectoryTask::GetFileOrDirectoryTask( |
michael@0 | 40 | FileSystemBase* aFileSystem, |
michael@0 | 41 | const FileSystemGetFileOrDirectoryParams& aParam, |
michael@0 | 42 | FileSystemRequestParent* aParent) |
michael@0 | 43 | : FileSystemTaskBase(aFileSystem, aParam, aParent) |
michael@0 | 44 | , mIsDirectory(false) |
michael@0 | 45 | { |
michael@0 | 46 | MOZ_ASSERT(FileSystemUtils::IsParentProcess(), |
michael@0 | 47 | "Only call from parent process!"); |
michael@0 | 48 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 49 | MOZ_ASSERT(aFileSystem); |
michael@0 | 50 | mTargetRealPath = aParam.realPath(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | GetFileOrDirectoryTask::~GetFileOrDirectoryTask() |
michael@0 | 54 | { |
michael@0 | 55 | MOZ_ASSERT(!mPromise || NS_IsMainThread(), |
michael@0 | 56 | "mPromise should be released on main thread!"); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | already_AddRefed<Promise> |
michael@0 | 60 | GetFileOrDirectoryTask::GetPromise() |
michael@0 | 61 | { |
michael@0 | 62 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 63 | return nsRefPtr<Promise>(mPromise).forget(); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | FileSystemParams |
michael@0 | 67 | GetFileOrDirectoryTask::GetRequestParams(const nsString& aFileSystem) const |
michael@0 | 68 | { |
michael@0 | 69 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 70 | return FileSystemGetFileOrDirectoryParams(aFileSystem, mTargetRealPath); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | FileSystemResponseValue |
michael@0 | 74 | GetFileOrDirectoryTask::GetSuccessRequestResult() const |
michael@0 | 75 | { |
michael@0 | 76 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 77 | if (mIsDirectory) { |
michael@0 | 78 | return FileSystemDirectoryResponse(mTargetRealPath); |
michael@0 | 79 | } |
michael@0 | 80 | BlobParent* actor = GetBlobParent(mTargetFile); |
michael@0 | 81 | if (!actor) { |
michael@0 | 82 | return FileSystemErrorResponse(NS_ERROR_DOM_FILESYSTEM_UNKNOWN_ERR); |
michael@0 | 83 | } |
michael@0 | 84 | FileSystemFileResponse response; |
michael@0 | 85 | response.blobParent() = actor; |
michael@0 | 86 | return response; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | void |
michael@0 | 90 | GetFileOrDirectoryTask::SetSuccessRequestResult(const FileSystemResponseValue& aValue) |
michael@0 | 91 | { |
michael@0 | 92 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 93 | switch (aValue.type()) { |
michael@0 | 94 | case FileSystemResponseValue::TFileSystemFileResponse: { |
michael@0 | 95 | FileSystemFileResponse r = aValue; |
michael@0 | 96 | BlobChild* actor = static_cast<BlobChild*>(r.blobChild()); |
michael@0 | 97 | nsCOMPtr<nsIDOMBlob> blob = actor->GetBlob(); |
michael@0 | 98 | mTargetFile = do_QueryInterface(blob); |
michael@0 | 99 | mIsDirectory = false; |
michael@0 | 100 | break; |
michael@0 | 101 | } |
michael@0 | 102 | case FileSystemResponseValue::TFileSystemDirectoryResponse: { |
michael@0 | 103 | FileSystemDirectoryResponse r = aValue; |
michael@0 | 104 | mTargetRealPath = r.realPath(); |
michael@0 | 105 | mIsDirectory = true; |
michael@0 | 106 | break; |
michael@0 | 107 | } |
michael@0 | 108 | default: { |
michael@0 | 109 | NS_RUNTIMEABORT("not reached"); |
michael@0 | 110 | break; |
michael@0 | 111 | } |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | nsresult |
michael@0 | 116 | GetFileOrDirectoryTask::Work() |
michael@0 | 117 | { |
michael@0 | 118 | MOZ_ASSERT(FileSystemUtils::IsParentProcess(), |
michael@0 | 119 | "Only call from parent process!"); |
michael@0 | 120 | MOZ_ASSERT(!NS_IsMainThread(), "Only call on worker thread!"); |
michael@0 | 121 | |
michael@0 | 122 | if (mFileSystem->IsShutdown()) { |
michael@0 | 123 | return NS_ERROR_FAILURE; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | // Whether we want to get the root directory. |
michael@0 | 127 | bool getRoot = mTargetRealPath.IsEmpty(); |
michael@0 | 128 | |
michael@0 | 129 | nsCOMPtr<nsIFile> file = mFileSystem->GetLocalFile(mTargetRealPath); |
michael@0 | 130 | if (!file) { |
michael@0 | 131 | return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | bool exists; |
michael@0 | 135 | nsresult rv = file->Exists(&exists); |
michael@0 | 136 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 137 | return rv; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | if (!exists) { |
michael@0 | 141 | if (!getRoot) { |
michael@0 | 142 | return NS_ERROR_DOM_FILE_NOT_FOUND_ERR; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | // If the root directory doesn't exit, create it. |
michael@0 | 146 | rv = file->Create(nsIFile::DIRECTORY_TYPE, 0777); |
michael@0 | 147 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 148 | return rv; |
michael@0 | 149 | } |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | // Get isDirectory. |
michael@0 | 153 | rv = file->IsDirectory(&mIsDirectory); |
michael@0 | 154 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 155 | return rv; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | if (mIsDirectory) { |
michael@0 | 159 | return NS_OK; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | // Check if the root is a directory. |
michael@0 | 163 | if (getRoot) { |
michael@0 | 164 | return NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | bool isFile; |
michael@0 | 168 | // Get isFile |
michael@0 | 169 | rv = file->IsFile(&isFile); |
michael@0 | 170 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 171 | return rv; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | if (!isFile) { |
michael@0 | 175 | // Neither directory or file. |
michael@0 | 176 | return NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | if (!mFileSystem->IsSafeFile(file)) { |
michael@0 | 180 | return NS_ERROR_DOM_SECURITY_ERR; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | mTargetFile = new nsDOMFileFile(file); |
michael@0 | 184 | |
michael@0 | 185 | return NS_OK; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | void |
michael@0 | 189 | GetFileOrDirectoryTask::HandlerCallback() |
michael@0 | 190 | { |
michael@0 | 191 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 192 | if (mFileSystem->IsShutdown()) { |
michael@0 | 193 | mPromise = nullptr; |
michael@0 | 194 | return; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | if (HasError()) { |
michael@0 | 198 | nsRefPtr<DOMError> domError = new DOMError(mFileSystem->GetWindow(), |
michael@0 | 199 | mErrorValue); |
michael@0 | 200 | mPromise->MaybeReject(domError); |
michael@0 | 201 | mPromise = nullptr; |
michael@0 | 202 | return; |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | if (mIsDirectory) { |
michael@0 | 206 | nsRefPtr<Directory> dir = new Directory(mFileSystem, mTargetRealPath); |
michael@0 | 207 | mPromise->MaybeResolve(dir); |
michael@0 | 208 | mPromise = nullptr; |
michael@0 | 209 | return; |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | mPromise->MaybeResolve(mTargetFile); |
michael@0 | 213 | mPromise = nullptr; |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | void |
michael@0 | 217 | GetFileOrDirectoryTask::GetPermissionAccessType(nsCString& aAccess) const |
michael@0 | 218 | { |
michael@0 | 219 | aAccess.AssignLiteral("read"); |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | } // namespace dom |
michael@0 | 223 | } // namespace mozilla |