dom/filesystem/RemoveTask.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 "RemoveTask.h"
michael@0 8
michael@0 9 #include "DOMError.h"
michael@0 10 #include "mozilla/dom/FileSystemBase.h"
michael@0 11 #include "mozilla/dom/FileSystemUtils.h"
michael@0 12 #include "mozilla/dom/Promise.h"
michael@0 13 #include "nsIDOMFile.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 RemoveTask::RemoveTask(FileSystemBase* aFileSystem,
michael@0 21 const nsAString& aDirPath,
michael@0 22 nsIDOMFile* aTargetFile,
michael@0 23 const nsAString& aTargetPath,
michael@0 24 bool aRecursive)
michael@0 25 : FileSystemTaskBase(aFileSystem)
michael@0 26 , mDirRealPath(aDirPath)
michael@0 27 , mTargetFile(aTargetFile)
michael@0 28 , mTargetRealPath(aTargetPath)
michael@0 29 , mRecursive(aRecursive)
michael@0 30 , mReturnValue(false)
michael@0 31 {
michael@0 32 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
michael@0 33 MOZ_ASSERT(aFileSystem);
michael@0 34 nsCOMPtr<nsIGlobalObject> globalObject =
michael@0 35 do_QueryInterface(aFileSystem->GetWindow());
michael@0 36 if (!globalObject) {
michael@0 37 return;
michael@0 38 }
michael@0 39 mPromise = new Promise(globalObject);
michael@0 40 }
michael@0 41
michael@0 42 RemoveTask::RemoveTask(FileSystemBase* aFileSystem,
michael@0 43 const FileSystemRemoveParams& aParam,
michael@0 44 FileSystemRequestParent* aParent)
michael@0 45 : FileSystemTaskBase(aFileSystem, aParam, aParent)
michael@0 46 , mRecursive(false)
michael@0 47 , mReturnValue(false)
michael@0 48 {
michael@0 49 MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
michael@0 50 "Only call from parent process!");
michael@0 51 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
michael@0 52 MOZ_ASSERT(aFileSystem);
michael@0 53
michael@0 54 mDirRealPath = aParam.directory();
michael@0 55
michael@0 56 mRecursive = aParam.recursive();
michael@0 57
michael@0 58 const FileSystemPathOrFileValue& target = aParam.target();
michael@0 59
michael@0 60 if (target.type() == FileSystemPathOrFileValue::TnsString) {
michael@0 61 mTargetRealPath = target;
michael@0 62 return;
michael@0 63 }
michael@0 64
michael@0 65 BlobParent* bp = static_cast<BlobParent*>(static_cast<PBlobParent*>(target));
michael@0 66 nsCOMPtr<nsIDOMBlob> blob = bp->GetBlob();
michael@0 67 mTargetFile = do_QueryInterface(blob);
michael@0 68 MOZ_ASSERT(mTargetFile, "mTargetFile should not be null.");
michael@0 69 }
michael@0 70
michael@0 71 RemoveTask::~RemoveTask()
michael@0 72 {
michael@0 73 MOZ_ASSERT(!mPromise || NS_IsMainThread(),
michael@0 74 "mPromise should be released on main thread!");
michael@0 75 }
michael@0 76
michael@0 77 already_AddRefed<Promise>
michael@0 78 RemoveTask::GetPromise()
michael@0 79 {
michael@0 80 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
michael@0 81 return nsRefPtr<Promise>(mPromise).forget();
michael@0 82 }
michael@0 83
michael@0 84 FileSystemParams
michael@0 85 RemoveTask::GetRequestParams(const nsString& aFileSystem) const
michael@0 86 {
michael@0 87 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
michael@0 88 FileSystemRemoveParams param;
michael@0 89 param.filesystem() = aFileSystem;
michael@0 90 param.directory() = mDirRealPath;
michael@0 91 param.recursive() = mRecursive;
michael@0 92 if (mTargetFile) {
michael@0 93 BlobChild* actor
michael@0 94 = ContentChild::GetSingleton()->GetOrCreateActorForBlob(mTargetFile);
michael@0 95 if (actor) {
michael@0 96 param.target() = actor;
michael@0 97 }
michael@0 98 } else {
michael@0 99 param.target() = mTargetRealPath;
michael@0 100 }
michael@0 101 return param;
michael@0 102 }
michael@0 103
michael@0 104 FileSystemResponseValue
michael@0 105 RemoveTask::GetSuccessRequestResult() const
michael@0 106 {
michael@0 107 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
michael@0 108 return FileSystemBooleanResponse(mReturnValue);
michael@0 109 }
michael@0 110
michael@0 111 void
michael@0 112 RemoveTask::SetSuccessRequestResult(const FileSystemResponseValue& aValue)
michael@0 113 {
michael@0 114 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
michael@0 115 FileSystemBooleanResponse r = aValue;
michael@0 116 mReturnValue = r.success();
michael@0 117 }
michael@0 118
michael@0 119 nsresult
michael@0 120 RemoveTask::Work()
michael@0 121 {
michael@0 122 MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
michael@0 123 "Only call from parent process!");
michael@0 124 MOZ_ASSERT(!NS_IsMainThread(), "Only call on worker thread!");
michael@0 125
michael@0 126 if (mFileSystem->IsShutdown()) {
michael@0 127 return NS_ERROR_FAILURE;
michael@0 128 }
michael@0 129
michael@0 130 // Get the DOM path if a DOMFile is passed as the target.
michael@0 131 if (mTargetFile) {
michael@0 132 if (!mFileSystem->GetRealPath(mTargetFile, mTargetRealPath)) {
michael@0 133 return NS_ERROR_DOM_SECURITY_ERR;
michael@0 134 }
michael@0 135 if (!FileSystemUtils::IsDescendantPath(mDirRealPath, mTargetRealPath)) {
michael@0 136 return NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR;
michael@0 137 }
michael@0 138 }
michael@0 139
michael@0 140 nsCOMPtr<nsIFile> file = mFileSystem->GetLocalFile(mTargetRealPath);
michael@0 141 if (!file) {
michael@0 142 return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
michael@0 143 }
michael@0 144
michael@0 145 bool exists = false;
michael@0 146 nsresult rv = file->Exists(&exists);
michael@0 147 if (NS_WARN_IF(NS_FAILED(rv))) {
michael@0 148 return rv;
michael@0 149 }
michael@0 150
michael@0 151 if (!exists) {
michael@0 152 mReturnValue = false;
michael@0 153 return NS_OK;
michael@0 154 }
michael@0 155
michael@0 156 bool isFile = false;
michael@0 157 rv = file->IsFile(&isFile);
michael@0 158 if (NS_WARN_IF(NS_FAILED(rv))) {
michael@0 159 return rv;
michael@0 160 }
michael@0 161
michael@0 162 if (isFile && !mFileSystem->IsSafeFile(file)) {
michael@0 163 return NS_ERROR_DOM_SECURITY_ERR;
michael@0 164 }
michael@0 165
michael@0 166 rv = file->Remove(mRecursive);
michael@0 167 if (NS_WARN_IF(NS_FAILED(rv))) {
michael@0 168 return rv;
michael@0 169 }
michael@0 170
michael@0 171 mReturnValue = true;
michael@0 172
michael@0 173 return NS_OK;
michael@0 174 }
michael@0 175
michael@0 176 void
michael@0 177 RemoveTask::HandlerCallback()
michael@0 178 {
michael@0 179 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
michael@0 180 if (mFileSystem->IsShutdown()) {
michael@0 181 mPromise = nullptr;
michael@0 182 return;
michael@0 183 }
michael@0 184
michael@0 185 if (HasError()) {
michael@0 186 nsRefPtr<DOMError> domError = new DOMError(mFileSystem->GetWindow(),
michael@0 187 mErrorValue);
michael@0 188 mPromise->MaybeReject(domError);
michael@0 189 mPromise = nullptr;
michael@0 190 return;
michael@0 191 }
michael@0 192
michael@0 193 mPromise->MaybeResolve(mReturnValue);
michael@0 194 mPromise = nullptr;
michael@0 195 }
michael@0 196
michael@0 197 void
michael@0 198 RemoveTask::GetPermissionAccessType(nsCString& aAccess) const
michael@0 199 {
michael@0 200 aAccess.AssignLiteral("write");
michael@0 201 }
michael@0 202
michael@0 203 } // namespace dom
michael@0 204 } // namespace mozilla

mercurial