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 "mozilla/dom/FileSystemTaskBase.h"
9 #include "nsNetUtil.h" // Stream transport service.
10 #include "mozilla/dom/ContentChild.h"
11 #include "mozilla/dom/FileSystemBase.h"
12 #include "mozilla/dom/FileSystemRequestParent.h"
13 #include "mozilla/dom/FileSystemUtils.h"
14 #include "mozilla/dom/Promise.h"
15 #include "mozilla/dom/PContent.h"
16 #include "mozilla/unused.h"
17 #include "nsDOMFile.h"
22 FileSystemTaskBase::FileSystemTaskBase(FileSystemBase
* aFileSystem
)
24 , mFileSystem(aFileSystem
)
26 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
27 MOZ_ASSERT(aFileSystem
, "aFileSystem should not be null.");
30 FileSystemTaskBase::FileSystemTaskBase(FileSystemBase
* aFileSystem
,
31 const FileSystemParams
& aParam
,
32 FileSystemRequestParent
* aParent
)
34 , mFileSystem(aFileSystem
)
35 , mRequestParent(aParent
)
37 MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
38 "Only call from parent process!");
39 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
40 MOZ_ASSERT(aFileSystem
, "aFileSystem should not be null.");
43 FileSystemTaskBase::~FileSystemTaskBase()
48 FileSystemTaskBase::GetFileSystem() const
50 return mFileSystem
.get();
54 FileSystemTaskBase::Start()
56 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
59 NS_DispatchToMainThread(this);
63 if (FileSystemUtils::IsParentProcess()) {
64 // Run in parent process.
65 // Start worker thread.
66 nsCOMPtr
<nsIEventTarget
> target
67 = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID
);
68 NS_ASSERTION(target
, "Must have stream transport service.");
69 target
->Dispatch(this, NS_DISPATCH_NORMAL
);
73 // Run in child process.
74 if (mFileSystem
->IsShutdown()) {
78 // Retain a reference so the task object isn't deleted without IPDL's
79 // knowledge. The reference will be released by
80 // mozilla::dom::ContentChild::DeallocPFileSystemRequestChild.
82 ContentChild::GetSingleton()->SendPFileSystemRequestConstructor(this,
83 GetRequestParams(mFileSystem
->ToString()));
87 FileSystemTaskBase::Run()
89 if (!NS_IsMainThread()) {
90 // Run worker thread tasks
95 // Dispatch itself to main thread
96 NS_DispatchToMainThread(this);
100 // Run main thread tasks
106 FileSystemTaskBase::HandleResult()
108 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
109 if (mFileSystem
->IsShutdown()) {
112 if (mRequestParent
&& mRequestParent
->IsRunning()) {
113 unused
<< mRequestParent
->Send__delete__(mRequestParent
,
120 FileSystemResponseValue
121 FileSystemTaskBase::GetRequestResult() const
123 MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
124 "Only call from parent process!");
125 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
127 return FileSystemErrorResponse(mErrorValue
);
129 return GetSuccessRequestResult();
134 FileSystemTaskBase::SetRequestResult(const FileSystemResponseValue
& aValue
)
136 MOZ_ASSERT(!FileSystemUtils::IsParentProcess(),
137 "Only call from child process!");
138 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
139 if (aValue
.type() == FileSystemResponseValue::TFileSystemErrorResponse
) {
140 FileSystemErrorResponse r
= aValue
;
141 mErrorValue
= r
.error();
143 SetSuccessRequestResult(aValue
);
148 FileSystemTaskBase::Recv__delete__(const FileSystemResponseValue
& aValue
)
150 SetRequestResult(aValue
);
156 FileSystemTaskBase::GetBlobParent(nsIDOMFile
* aFile
) const
158 MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
159 "Only call from parent process!");
160 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
163 // Load the lazy dom file data from the parent before sending to the child.
165 aFile
->GetType(mimeType
);
167 aFile
->GetSize(&fileSize
);
168 uint64_t lastModifiedDate
;
169 aFile
->GetMozLastModifiedDate(&lastModifiedDate
);
171 ContentParent
* cp
= static_cast<ContentParent
*>(mRequestParent
->Manager());
172 return cp
->GetOrCreateActorForBlob(aFile
);
176 FileSystemTaskBase::SetError(const nsresult
& aErrorValue
)
178 uint16_t module
= NS_ERROR_GET_MODULE(aErrorValue
);
179 if (module
== NS_ERROR_MODULE_DOM_FILESYSTEM
||
180 module
== NS_ERROR_MODULE_DOM_FILE
||
181 module
== NS_ERROR_MODULE_DOM
) {
182 mErrorValue
= aErrorValue
;
186 switch (aErrorValue
) {
191 case NS_ERROR_FILE_INVALID_PATH
:
192 case NS_ERROR_FILE_UNRECOGNIZED_PATH
:
193 mErrorValue
= NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR
;
196 case NS_ERROR_FILE_DESTINATION_NOT_DIR
:
197 mErrorValue
= NS_ERROR_DOM_FILESYSTEM_INVALID_MODIFICATION_ERR
;
200 case NS_ERROR_FILE_ACCESS_DENIED
:
201 case NS_ERROR_FILE_DIR_NOT_EMPTY
:
202 mErrorValue
= NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR
;
205 case NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
:
206 case NS_ERROR_NOT_AVAILABLE
:
207 mErrorValue
= NS_ERROR_DOM_FILE_NOT_FOUND_ERR
;
210 case NS_ERROR_FILE_ALREADY_EXISTS
:
211 mErrorValue
= NS_ERROR_DOM_FILESYSTEM_PATH_EXISTS_ERR
;
214 case NS_ERROR_FILE_NOT_DIRECTORY
:
215 mErrorValue
= NS_ERROR_DOM_FILESYSTEM_TYPE_MISMATCH_ERR
;
218 case NS_ERROR_UNEXPECTED
:
220 mErrorValue
= NS_ERROR_DOM_FILESYSTEM_UNKNOWN_ERR
;
226 } // namespace mozilla