Bumping manifests a=b2g-bump
[gecko.git] / dom / filesystem / Directory.cpp
blob799a18cb188b9fbc641dfd0d9c1ae9b6db465dd7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/Directory.h"
9 #include "CreateDirectoryTask.h"
10 #include "CreateFileTask.h"
11 #include "FileSystemPermissionRequest.h"
12 #include "GetFileOrDirectoryTask.h"
13 #include "RemoveTask.h"
15 #include "nsCharSeparatedTokenizer.h"
16 #include "nsString.h"
17 #include "mozilla/dom/DirectoryBinding.h"
18 #include "mozilla/dom/FileSystemBase.h"
19 #include "mozilla/dom/FileSystemUtils.h"
21 // Resolve the name collision of Microsoft's API name with macros defined in
22 // Windows header files. Undefine the macro of CreateDirectory to avoid
23 // Directory#CreateDirectory being replaced by Directory#CreateDirectoryW.
24 #ifdef CreateDirectory
25 #undef CreateDirectory
26 #endif
27 // Undefine the macro of CreateFile to avoid Directory#CreateFile being replaced
28 // by Directory#CreateFileW.
29 #ifdef CreateFile
30 #undef CreateFile
31 #endif
33 namespace mozilla {
34 namespace dom {
36 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(Directory)
37 NS_IMPL_CYCLE_COLLECTING_ADDREF(Directory)
38 NS_IMPL_CYCLE_COLLECTING_RELEASE(Directory)
39 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Directory)
40 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
41 NS_INTERFACE_MAP_ENTRY(nsISupports)
42 NS_INTERFACE_MAP_END
44 // static
45 already_AddRefed<Promise>
46 Directory::GetRoot(FileSystemBase* aFileSystem, ErrorResult& aRv)
48 nsRefPtr<GetFileOrDirectoryTask> task = new GetFileOrDirectoryTask(
49 aFileSystem, EmptyString(), true, aRv);
50 if (aRv.Failed()) {
51 return nullptr;
53 FileSystemPermissionRequest::RequestForTask(task);
54 return task->GetPromise();
57 Directory::Directory(FileSystemBase* aFileSystem,
58 const nsAString& aPath)
59 : mFileSystem(aFileSystem)
60 , mPath(aPath)
62 MOZ_ASSERT(aFileSystem, "aFileSystem should not be null.");
63 // Remove the trailing "/".
64 mPath.Trim(FILESYSTEM_DOM_PATH_SEPARATOR, false, true);
67 Directory::~Directory()
71 nsPIDOMWindow*
72 Directory::GetParentObject() const
74 return mFileSystem->GetWindow();
77 JSObject*
78 Directory::WrapObject(JSContext* aCx)
80 return DirectoryBinding::Wrap(aCx, this);
83 void
84 Directory::GetName(nsString& aRetval) const
86 aRetval.Truncate();
88 if (mPath.IsEmpty()) {
89 aRetval = mFileSystem->GetRootName();
90 return;
93 aRetval = Substring(mPath,
94 mPath.RFindChar(FileSystemUtils::kSeparatorChar) + 1);
97 already_AddRefed<Promise>
98 Directory::CreateFile(const nsAString& aPath, const CreateFileOptions& aOptions,
99 ErrorResult& aRv)
101 nsresult error = NS_OK;
102 nsString realPath;
103 nsRefPtr<File> blobData;
104 InfallibleTArray<uint8_t> arrayData;
105 bool replace = (aOptions.mIfExists == CreateIfExistsMode::Replace);
107 // Get the file content.
108 if (aOptions.mData.WasPassed()) {
109 auto& data = aOptions.mData.Value();
110 if (data.IsString()) {
111 NS_ConvertUTF16toUTF8 str(data.GetAsString());
112 arrayData.AppendElements(reinterpret_cast<const uint8_t *>(str.get()),
113 str.Length());
114 } else if (data.IsArrayBuffer()) {
115 const ArrayBuffer& buffer = data.GetAsArrayBuffer();
116 buffer.ComputeLengthAndData();
117 arrayData.AppendElements(buffer.Data(), buffer.Length());
118 } else if (data.IsArrayBufferView()){
119 const ArrayBufferView& view = data.GetAsArrayBufferView();
120 view.ComputeLengthAndData();
121 arrayData.AppendElements(view.Data(), view.Length());
122 } else {
123 blobData = data.GetAsBlob();
127 if (!DOMPathToRealPath(aPath, realPath)) {
128 error = NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
131 nsRefPtr<CreateFileTask> task =
132 new CreateFileTask(mFileSystem, realPath, blobData, arrayData, replace, aRv);
133 if (aRv.Failed()) {
134 return nullptr;
136 task->SetError(error);
137 FileSystemPermissionRequest::RequestForTask(task);
138 return task->GetPromise();
141 already_AddRefed<Promise>
142 Directory::CreateDirectory(const nsAString& aPath, ErrorResult& aRv)
144 nsresult error = NS_OK;
145 nsString realPath;
146 if (!DOMPathToRealPath(aPath, realPath)) {
147 error = NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
149 nsRefPtr<CreateDirectoryTask> task = new CreateDirectoryTask(
150 mFileSystem, realPath, aRv);
151 if (aRv.Failed()) {
152 return nullptr;
154 task->SetError(error);
155 FileSystemPermissionRequest::RequestForTask(task);
156 return task->GetPromise();
159 already_AddRefed<Promise>
160 Directory::Get(const nsAString& aPath, ErrorResult& aRv)
162 nsresult error = NS_OK;
163 nsString realPath;
164 if (!DOMPathToRealPath(aPath, realPath)) {
165 error = NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
167 nsRefPtr<GetFileOrDirectoryTask> task = new GetFileOrDirectoryTask(
168 mFileSystem, realPath, false, aRv);
169 if (aRv.Failed()) {
170 return nullptr;
172 task->SetError(error);
173 FileSystemPermissionRequest::RequestForTask(task);
174 return task->GetPromise();
177 already_AddRefed<Promise>
178 Directory::Remove(const StringOrFileOrDirectory& aPath, ErrorResult& aRv)
180 return RemoveInternal(aPath, false, aRv);
183 already_AddRefed<Promise>
184 Directory::RemoveDeep(const StringOrFileOrDirectory& aPath, ErrorResult& aRv)
186 return RemoveInternal(aPath, true, aRv);
189 already_AddRefed<Promise>
190 Directory::RemoveInternal(const StringOrFileOrDirectory& aPath, bool aRecursive,
191 ErrorResult& aRv)
193 nsresult error = NS_OK;
194 nsString realPath;
195 nsRefPtr<FileImpl> file;
197 // Check and get the target path.
199 if (aPath.IsFile()) {
200 file = aPath.GetAsFile().Impl();
201 goto parameters_check_done;
204 if (aPath.IsString()) {
205 if (!DOMPathToRealPath(aPath.GetAsString(), realPath)) {
206 error = NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
208 goto parameters_check_done;
211 if (!mFileSystem->IsSafeDirectory(&aPath.GetAsDirectory())) {
212 error = NS_ERROR_DOM_SECURITY_ERR;
213 goto parameters_check_done;
216 realPath = aPath.GetAsDirectory().mPath;
217 // The target must be a descendant of this directory.
218 if (!FileSystemUtils::IsDescendantPath(mPath, realPath)) {
219 error = NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR;
222 parameters_check_done:
224 nsRefPtr<RemoveTask> task = new RemoveTask(mFileSystem, mPath, file, realPath,
225 aRecursive, aRv);
226 if (aRv.Failed()) {
227 return nullptr;
229 task->SetError(error);
230 FileSystemPermissionRequest::RequestForTask(task);
231 return task->GetPromise();
234 FileSystemBase*
235 Directory::GetFileSystem() const
237 return mFileSystem.get();
240 bool
241 Directory::DOMPathToRealPath(const nsAString& aPath, nsAString& aRealPath) const
243 aRealPath.Truncate();
245 nsString relativePath;
246 relativePath = aPath;
248 // Trim white spaces.
249 static const char kWhitespace[] = "\b\t\r\n ";
250 relativePath.Trim(kWhitespace);
252 if (!IsValidRelativePath(relativePath)) {
253 return false;
256 aRealPath = mPath + NS_LITERAL_STRING(FILESYSTEM_DOM_PATH_SEPARATOR) +
257 relativePath;
259 return true;
262 // static
263 bool
264 Directory::IsValidRelativePath(const nsString& aPath)
266 // We don't allow empty relative path to access the root.
267 if (aPath.IsEmpty()) {
268 return false;
271 // Leading and trailing "/" are not allowed.
272 if (aPath.First() == FileSystemUtils::kSeparatorChar ||
273 aPath.Last() == FileSystemUtils::kSeparatorChar) {
274 return false;
277 NS_NAMED_LITERAL_STRING(kCurrentDir, ".");
278 NS_NAMED_LITERAL_STRING(kParentDir, "..");
280 // Split path and check each path component.
281 nsCharSeparatedTokenizer tokenizer(aPath, FileSystemUtils::kSeparatorChar);
282 while (tokenizer.hasMoreTokens()) {
283 nsDependentSubstring pathComponent = tokenizer.nextToken();
284 // The path containing empty components, such as "foo//bar", is invalid.
285 // We don't allow paths, such as "../foo", "foo/./bar" and "foo/../bar",
286 // to walk up the directory.
287 if (pathComponent.IsEmpty() ||
288 pathComponent.Equals(kCurrentDir) ||
289 pathComponent.Equals(kParentDir)) {
290 return false;
294 return true;
297 } // namespace dom
298 } // namespace mozilla