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 "mozilla/dom/FileSystemTaskBase.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "nsNetUtil.h" // Stream transport service. |
michael@0 | 10 | #include "mozilla/dom/ContentChild.h" |
michael@0 | 11 | #include "mozilla/dom/FileSystemBase.h" |
michael@0 | 12 | #include "mozilla/dom/FileSystemRequestParent.h" |
michael@0 | 13 | #include "mozilla/dom/FileSystemUtils.h" |
michael@0 | 14 | #include "mozilla/dom/Promise.h" |
michael@0 | 15 | #include "mozilla/dom/PContent.h" |
michael@0 | 16 | #include "mozilla/unused.h" |
michael@0 | 17 | #include "nsDOMFile.h" |
michael@0 | 18 | |
michael@0 | 19 | namespace mozilla { |
michael@0 | 20 | namespace dom { |
michael@0 | 21 | |
michael@0 | 22 | FileSystemTaskBase::FileSystemTaskBase(FileSystemBase* aFileSystem) |
michael@0 | 23 | : mErrorValue(NS_OK) |
michael@0 | 24 | , mFileSystem(aFileSystem) |
michael@0 | 25 | { |
michael@0 | 26 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 27 | MOZ_ASSERT(aFileSystem, "aFileSystem should not be null."); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | FileSystemTaskBase::FileSystemTaskBase(FileSystemBase* aFileSystem, |
michael@0 | 31 | const FileSystemParams& aParam, |
michael@0 | 32 | FileSystemRequestParent* aParent) |
michael@0 | 33 | : mErrorValue(NS_OK) |
michael@0 | 34 | , mFileSystem(aFileSystem) |
michael@0 | 35 | , mRequestParent(aParent) |
michael@0 | 36 | { |
michael@0 | 37 | MOZ_ASSERT(FileSystemUtils::IsParentProcess(), |
michael@0 | 38 | "Only call from parent process!"); |
michael@0 | 39 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 40 | MOZ_ASSERT(aFileSystem, "aFileSystem should not be null."); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | FileSystemTaskBase::~FileSystemTaskBase() |
michael@0 | 44 | { |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | FileSystemBase* |
michael@0 | 48 | FileSystemTaskBase::GetFileSystem() const |
michael@0 | 49 | { |
michael@0 | 50 | return mFileSystem.get(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | void |
michael@0 | 54 | FileSystemTaskBase::Start() |
michael@0 | 55 | { |
michael@0 | 56 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 57 | |
michael@0 | 58 | if (HasError()) { |
michael@0 | 59 | NS_DispatchToMainThread(this); |
michael@0 | 60 | return; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | if (FileSystemUtils::IsParentProcess()) { |
michael@0 | 64 | // Run in parent process. |
michael@0 | 65 | // Start worker thread. |
michael@0 | 66 | nsCOMPtr<nsIEventTarget> target |
michael@0 | 67 | = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); |
michael@0 | 68 | NS_ASSERTION(target, "Must have stream transport service."); |
michael@0 | 69 | target->Dispatch(this, NS_DISPATCH_NORMAL); |
michael@0 | 70 | return; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | // Run in child process. |
michael@0 | 74 | if (mFileSystem->IsShutdown()) { |
michael@0 | 75 | return; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | // Retain a reference so the task object isn't deleted without IPDL's |
michael@0 | 79 | // knowledge. The reference will be released by |
michael@0 | 80 | // mozilla::dom::ContentChild::DeallocPFileSystemRequestChild. |
michael@0 | 81 | NS_ADDREF_THIS(); |
michael@0 | 82 | ContentChild::GetSingleton()->SendPFileSystemRequestConstructor(this, |
michael@0 | 83 | GetRequestParams(mFileSystem->ToString())); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | NS_IMETHODIMP |
michael@0 | 87 | FileSystemTaskBase::Run() |
michael@0 | 88 | { |
michael@0 | 89 | if (!NS_IsMainThread()) { |
michael@0 | 90 | // Run worker thread tasks |
michael@0 | 91 | nsresult rv = Work(); |
michael@0 | 92 | if (NS_FAILED(rv)) { |
michael@0 | 93 | SetError(rv); |
michael@0 | 94 | } |
michael@0 | 95 | // Dispatch itself to main thread |
michael@0 | 96 | NS_DispatchToMainThread(this); |
michael@0 | 97 | return NS_OK; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | // Run main thread tasks |
michael@0 | 101 | HandleResult(); |
michael@0 | 102 | return NS_OK; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void |
michael@0 | 106 | FileSystemTaskBase::HandleResult() |
michael@0 | 107 | { |
michael@0 | 108 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 109 | if (mFileSystem->IsShutdown()) { |
michael@0 | 110 | return; |
michael@0 | 111 | } |
michael@0 | 112 | if (mRequestParent && mRequestParent->IsRunning()) { |
michael@0 | 113 | unused << mRequestParent->Send__delete__(mRequestParent, |
michael@0 | 114 | GetRequestResult()); |
michael@0 | 115 | } else { |
michael@0 | 116 | HandlerCallback(); |
michael@0 | 117 | } |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | FileSystemResponseValue |
michael@0 | 121 | FileSystemTaskBase::GetRequestResult() const |
michael@0 | 122 | { |
michael@0 | 123 | MOZ_ASSERT(FileSystemUtils::IsParentProcess(), |
michael@0 | 124 | "Only call from parent process!"); |
michael@0 | 125 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 126 | if (HasError()) { |
michael@0 | 127 | return FileSystemErrorResponse(mErrorValue); |
michael@0 | 128 | } else { |
michael@0 | 129 | return GetSuccessRequestResult(); |
michael@0 | 130 | } |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | void |
michael@0 | 134 | FileSystemTaskBase::SetRequestResult(const FileSystemResponseValue& aValue) |
michael@0 | 135 | { |
michael@0 | 136 | MOZ_ASSERT(!FileSystemUtils::IsParentProcess(), |
michael@0 | 137 | "Only call from child process!"); |
michael@0 | 138 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 139 | if (aValue.type() == FileSystemResponseValue::TFileSystemErrorResponse) { |
michael@0 | 140 | FileSystemErrorResponse r = aValue; |
michael@0 | 141 | mErrorValue = r.error(); |
michael@0 | 142 | } else { |
michael@0 | 143 | SetSuccessRequestResult(aValue); |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | bool |
michael@0 | 148 | FileSystemTaskBase::Recv__delete__(const FileSystemResponseValue& aValue) |
michael@0 | 149 | { |
michael@0 | 150 | SetRequestResult(aValue); |
michael@0 | 151 | HandlerCallback(); |
michael@0 | 152 | return true; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | BlobParent* |
michael@0 | 156 | FileSystemTaskBase::GetBlobParent(nsIDOMFile* aFile) const |
michael@0 | 157 | { |
michael@0 | 158 | MOZ_ASSERT(FileSystemUtils::IsParentProcess(), |
michael@0 | 159 | "Only call from parent process!"); |
michael@0 | 160 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 161 | MOZ_ASSERT(aFile); |
michael@0 | 162 | |
michael@0 | 163 | // Load the lazy dom file data from the parent before sending to the child. |
michael@0 | 164 | nsString mimeType; |
michael@0 | 165 | aFile->GetType(mimeType); |
michael@0 | 166 | uint64_t fileSize; |
michael@0 | 167 | aFile->GetSize(&fileSize); |
michael@0 | 168 | uint64_t lastModifiedDate; |
michael@0 | 169 | aFile->GetMozLastModifiedDate(&lastModifiedDate); |
michael@0 | 170 | |
michael@0 | 171 | ContentParent* cp = static_cast<ContentParent*>(mRequestParent->Manager()); |
michael@0 | 172 | return cp->GetOrCreateActorForBlob(aFile); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | void |
michael@0 | 176 | FileSystemTaskBase::SetError(const nsresult& aErrorValue) |
michael@0 | 177 | { |
michael@0 | 178 | uint16_t module = NS_ERROR_GET_MODULE(aErrorValue); |
michael@0 | 179 | if (module == NS_ERROR_MODULE_DOM_FILESYSTEM || |
michael@0 | 180 | module == NS_ERROR_MODULE_DOM_FILE || |
michael@0 | 181 | module == NS_ERROR_MODULE_DOM) { |
michael@0 | 182 | mErrorValue = aErrorValue; |
michael@0 | 183 | return; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | switch (aErrorValue) { |
michael@0 | 187 | case NS_OK: |
michael@0 | 188 | mErrorValue = NS_OK; |
michael@0 | 189 | return; |
michael@0 | 190 | |
michael@0 | 191 | case NS_ERROR_FILE_INVALID_PATH: |
michael@0 | 192 | case NS_ERROR_FILE_UNRECOGNIZED_PATH: |
michael@0 | 193 | mErrorValue = NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR; |
michael@0 | 194 | return; |
michael@0 | 195 | |
michael@0 | 196 | case NS_ERROR_FILE_DESTINATION_NOT_DIR: |
michael@0 | 197 | mErrorValue = NS_ERROR_DOM_FILESYSTEM_INVALID_MODIFICATION_ERR; |
michael@0 | 198 | return; |
michael@0 | 199 | |
michael@0 | 200 | case NS_ERROR_FILE_ACCESS_DENIED: |
michael@0 | 201 | case NS_ERROR_FILE_DIR_NOT_EMPTY: |
michael@0 | 202 | mErrorValue = NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR; |
michael@0 | 203 | return; |
michael@0 | 204 | |
michael@0 | 205 | case NS_ERROR_FILE_TARGET_DOES_NOT_EXIST: |
michael@0 | 206 | case NS_ERROR_NOT_AVAILABLE: |
michael@0 | 207 | mErrorValue = NS_ERROR_DOM_FILE_NOT_FOUND_ERR; |
michael@0 | 208 | return; |
michael@0 | 209 | |
michael@0 | 210 | case NS_ERROR_FILE_ALREADY_EXISTS: |
michael@0 | 211 | mErrorValue = NS_ERROR_DOM_FILESYSTEM_PATH_EXISTS_ERR; |
michael@0 | 212 | return; |
michael@0 | 213 | |
michael@0 | 214 | case NS_ERROR_FILE_NOT_DIRECTORY: |
michael@0 | 215 | mErrorValue = NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR; |
michael@0 | 216 | return; |
michael@0 | 217 | |
michael@0 | 218 | case NS_ERROR_UNEXPECTED: |
michael@0 | 219 | default: |
michael@0 | 220 | mErrorValue = NS_ERROR_DOM_FILESYSTEM_UNKNOWN_ERR; |
michael@0 | 221 | return; |
michael@0 | 222 | } |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | } // namespace dom |
michael@0 | 226 | } // namespace mozilla |