1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "RemoveTask.h"
10 #include "mozilla/dom/FileSystemBase.h"
11 #include "mozilla/dom/FileSystemUtils.h"
12 #include "mozilla/dom/Promise.h"
13 #include "nsDOMFile.h"
15 #include "nsStringGlue.h"
20 RemoveTask::RemoveTask(FileSystemBase
* aFileSystem
,
21 const nsAString
& aDirPath
,
22 DOMFileImpl
* aTargetFile
,
23 const nsAString
& aTargetPath
,
26 : FileSystemTaskBase(aFileSystem
)
27 , mDirRealPath(aDirPath
)
28 , mTargetFileImpl(aTargetFile
)
29 , mTargetRealPath(aTargetPath
)
30 , mRecursive(aRecursive
)
33 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
34 MOZ_ASSERT(aFileSystem
);
35 nsCOMPtr
<nsIGlobalObject
> globalObject
=
36 do_QueryInterface(aFileSystem
->GetWindow());
40 mPromise
= Promise::Create(globalObject
, aRv
);
43 RemoveTask::RemoveTask(FileSystemBase
* aFileSystem
,
44 const FileSystemRemoveParams
& aParam
,
45 FileSystemRequestParent
* aParent
)
46 : FileSystemTaskBase(aFileSystem
, aParam
, aParent
)
50 MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
51 "Only call from parent process!");
52 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
53 MOZ_ASSERT(aFileSystem
);
55 mDirRealPath
= aParam
.directory();
57 mRecursive
= aParam
.recursive();
59 const FileSystemPathOrFileValue
& target
= aParam
.target();
61 if (target
.type() == FileSystemPathOrFileValue::TnsString
) {
62 mTargetRealPath
= target
;
66 BlobParent
* bp
= static_cast<BlobParent
*>(static_cast<PBlobParent
*>(target
));
67 nsCOMPtr
<nsIDOMBlob
> blob
= bp
->GetBlob();
69 mTargetFileImpl
= static_cast<DOMFile
*>(blob
.get())->Impl();
72 RemoveTask::~RemoveTask()
74 MOZ_ASSERT(!mPromise
|| NS_IsMainThread(),
75 "mPromise should be released on main thread!");
78 already_AddRefed
<Promise
>
79 RemoveTask::GetPromise()
81 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
82 return nsRefPtr
<Promise
>(mPromise
).forget();
86 RemoveTask::GetRequestParams(const nsString
& aFileSystem
) const
88 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
89 FileSystemRemoveParams param
;
90 param
.filesystem() = aFileSystem
;
91 param
.directory() = mDirRealPath
;
92 param
.recursive() = mRecursive
;
93 if (mTargetFileImpl
) {
94 nsRefPtr
<DOMFile
> file
= new DOMFile(mTargetFileImpl
);
96 = ContentChild::GetSingleton()->GetOrCreateActorForBlob(file
);
98 param
.target() = actor
;
101 param
.target() = mTargetRealPath
;
106 FileSystemResponseValue
107 RemoveTask::GetSuccessRequestResult() const
109 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
110 return FileSystemBooleanResponse(mReturnValue
);
114 RemoveTask::SetSuccessRequestResult(const FileSystemResponseValue
& aValue
)
116 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
117 FileSystemBooleanResponse r
= aValue
;
118 mReturnValue
= r
.success();
124 MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
125 "Only call from parent process!");
126 MOZ_ASSERT(!NS_IsMainThread(), "Only call on worker thread!");
128 if (mFileSystem
->IsShutdown()) {
129 return NS_ERROR_FAILURE
;
132 // Get the DOM path if a DOMFile is passed as the target.
133 if (mTargetFileImpl
) {
134 if (!mFileSystem
->GetRealPath(mTargetFileImpl
, mTargetRealPath
)) {
135 return NS_ERROR_DOM_SECURITY_ERR
;
137 if (!FileSystemUtils::IsDescendantPath(mDirRealPath
, mTargetRealPath
)) {
138 return NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR
;
142 nsCOMPtr
<nsIFile
> file
= mFileSystem
->GetLocalFile(mTargetRealPath
);
144 return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR
;
148 nsresult rv
= file
->Exists(&exists
);
149 if (NS_WARN_IF(NS_FAILED(rv
))) {
154 mReturnValue
= false;
159 rv
= file
->IsFile(&isFile
);
160 if (NS_WARN_IF(NS_FAILED(rv
))) {
164 if (isFile
&& !mFileSystem
->IsSafeFile(file
)) {
165 return NS_ERROR_DOM_SECURITY_ERR
;
168 rv
= file
->Remove(mRecursive
);
169 if (NS_WARN_IF(NS_FAILED(rv
))) {
179 RemoveTask::HandlerCallback()
181 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
182 if (mFileSystem
->IsShutdown()) {
188 nsRefPtr
<DOMError
> domError
= new DOMError(mFileSystem
->GetWindow(),
190 mPromise
->MaybeRejectBrokenly(domError
);
195 mPromise
->MaybeResolve(mReturnValue
);
200 RemoveTask::GetPermissionAccessType(nsCString
& aAccess
) const
202 aAccess
.AssignLiteral("write");
206 } // namespace mozilla