dom/filesystem/CreateDirectoryTask.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:4ad95ee2f13c
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/. */
6
7 #include "CreateDirectoryTask.h"
8
9 #include "DOMError.h"
10 #include "mozilla/dom/Directory.h"
11 #include "mozilla/dom/FileSystemBase.h"
12 #include "mozilla/dom/FileSystemUtils.h"
13 #include "mozilla/dom/Promise.h"
14 #include "nsIFile.h"
15 #include "nsStringGlue.h"
16
17 namespace mozilla {
18 namespace dom {
19
20 CreateDirectoryTask::CreateDirectoryTask(FileSystemBase* aFileSystem,
21 const nsAString& aPath)
22 : FileSystemTaskBase(aFileSystem)
23 , mTargetRealPath(aPath)
24 {
25 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
26 MOZ_ASSERT(aFileSystem);
27 nsCOMPtr<nsIGlobalObject> globalObject =
28 do_QueryInterface(aFileSystem->GetWindow());
29 if (!globalObject) {
30 return;
31 }
32 mPromise = new Promise(globalObject);
33 }
34
35 CreateDirectoryTask::CreateDirectoryTask(
36 FileSystemBase* aFileSystem,
37 const FileSystemCreateDirectoryParams& aParam,
38 FileSystemRequestParent* aParent)
39 : FileSystemTaskBase(aFileSystem, aParam, aParent)
40 {
41 MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
42 "Only call from parent process!");
43 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
44 MOZ_ASSERT(aFileSystem);
45 mTargetRealPath = aParam.realPath();
46 }
47
48 CreateDirectoryTask::~CreateDirectoryTask()
49 {
50 MOZ_ASSERT(!mPromise || NS_IsMainThread(),
51 "mPromise should be released on main thread!");
52 }
53
54 already_AddRefed<Promise>
55 CreateDirectoryTask::GetPromise()
56 {
57 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
58 return nsRefPtr<Promise>(mPromise).forget();
59 }
60
61 FileSystemParams
62 CreateDirectoryTask::GetRequestParams(const nsString& aFileSystem) const
63 {
64 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
65 return FileSystemCreateDirectoryParams(aFileSystem, mTargetRealPath);
66 }
67
68 FileSystemResponseValue
69 CreateDirectoryTask::GetSuccessRequestResult() const
70 {
71 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
72 return FileSystemDirectoryResponse(mTargetRealPath);
73 }
74
75 void
76 CreateDirectoryTask::SetSuccessRequestResult(const FileSystemResponseValue& aValue)
77 {
78 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
79 FileSystemDirectoryResponse r = aValue;
80 mTargetRealPath = r.realPath();
81 }
82
83 nsresult
84 CreateDirectoryTask::Work()
85 {
86 MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
87 "Only call from parent process!");
88 MOZ_ASSERT(!NS_IsMainThread(), "Only call on worker thread!");
89
90 if (mFileSystem->IsShutdown()) {
91 return NS_ERROR_FAILURE;
92 }
93
94 nsCOMPtr<nsIFile> file = mFileSystem->GetLocalFile(mTargetRealPath);
95 if (!file) {
96 return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
97 }
98
99 bool fileExists;
100 nsresult rv = file->Exists(&fileExists);
101 if (NS_WARN_IF(NS_FAILED(rv))) {
102 return rv;
103 }
104
105 if (fileExists) {
106 return NS_ERROR_DOM_FILESYSTEM_PATH_EXISTS_ERR;
107 }
108
109 rv = file->Create(nsIFile::DIRECTORY_TYPE, 0770);
110 return rv;
111 }
112
113 void
114 CreateDirectoryTask::HandlerCallback()
115 {
116 MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
117 if (mFileSystem->IsShutdown()) {
118 mPromise = nullptr;
119 return;
120 }
121
122 if (HasError()) {
123 nsRefPtr<DOMError> domError = new DOMError(mFileSystem->GetWindow(),
124 mErrorValue);
125 mPromise->MaybeReject(domError);
126 mPromise = nullptr;
127 return;
128 }
129 nsRefPtr<Directory> dir = new Directory(mFileSystem, mTargetRealPath);
130 mPromise->MaybeResolve(dir);
131 mPromise = nullptr;
132 }
133
134 void
135 CreateDirectoryTask::GetPermissionAccessType(nsCString& aAccess) const
136 {
137 aAccess.AssignLiteral("create");
138 }
139
140 } // namespace dom
141 } // namespace mozilla

mercurial