1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "FileSystemRootDirectoryEntry.h"
#include "FileSystemRootDirectoryReader.h"
#include "mozilla/dom/FileSystemUtils.h"
#include "CallbackRunnables.h"
#include "nsReadableUtils.h"
namespace mozilla::dom {
NS_IMPL_CYCLE_COLLECTION_INHERITED(FileSystemRootDirectoryEntry,
FileSystemDirectoryEntry, mEntries)
NS_IMPL_ADDREF_INHERITED(FileSystemRootDirectoryEntry, FileSystemDirectoryEntry)
NS_IMPL_RELEASE_INHERITED(FileSystemRootDirectoryEntry,
FileSystemDirectoryEntry)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileSystemRootDirectoryEntry)
NS_INTERFACE_MAP_END_INHERITING(FileSystemDirectoryEntry)
FileSystemRootDirectoryEntry::FileSystemRootDirectoryEntry(
nsIGlobalObject* aGlobal, Sequence<RefPtr<FileSystemEntry>> aEntries,
FileSystem* aFileSystem)
: FileSystemDirectoryEntry(aGlobal, nullptr, nullptr, aFileSystem),
mEntries(std::move(aEntries)) {
MOZ_ASSERT(aGlobal);
}
FileSystemRootDirectoryEntry::~FileSystemRootDirectoryEntry() = default;
void FileSystemRootDirectoryEntry::GetName(nsAString& aName,
ErrorResult& aRv) const {
aName.Truncate();
}
void FileSystemRootDirectoryEntry::GetFullPath(nsAString& aPath,
ErrorResult& aRv) const {
aPath.AssignLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
}
already_AddRefed<FileSystemDirectoryReader>
FileSystemRootDirectoryEntry::CreateReader() {
RefPtr<FileSystemDirectoryReader> reader =
new FileSystemRootDirectoryReader(this, Filesystem(), mEntries);
return reader.forget();
}
void FileSystemRootDirectoryEntry::GetInternal(
const nsAString& aPath, const FileSystemFlags& aFlag,
const Optional<OwningNonNull<FileSystemEntryCallback>>& aSuccessCallback,
const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback,
GetInternalType aType) {
if (!aSuccessCallback.WasPassed() && !aErrorCallback.WasPassed()) {
return;
}
if (aFlag.mCreate) {
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
NS_ERROR_DOM_SECURITY_ERR);
return;
}
nsTArray<nsString> parts;
if (!FileSystemUtils::IsValidRelativeDOMPath(aPath, parts)) {
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
NS_ERROR_DOM_NOT_FOUND_ERR);
return;
}
MOZ_ASSERT(!parts.IsEmpty());
RefPtr<FileSystemEntry> entry;
for (uint32_t i = 0; i < mEntries.Length(); ++i) {
ErrorResult rv;
nsAutoString name;
mEntries[i]->GetName(name, rv);
if (NS_WARN_IF(rv.Failed())) {
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
rv.StealNSResult());
return;
}
if (name == parts[0]) {
entry = mEntries[i];
break;
}
}
// Not found.
if (!entry) {
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
NS_ERROR_DOM_NOT_FOUND_ERR);
return;
}
// No subdirectory in the path.
if (parts.Length() == 1) {
if ((entry->IsFile() && aType == eGetDirectory) ||
(entry->IsDirectory() && aType == eGetFile)) {
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
NS_ERROR_DOM_TYPE_MISMATCH_ERR);
return;
}
if (aSuccessCallback.WasPassed()) {
RefPtr<EntryCallbackRunnable> runnable =
new EntryCallbackRunnable(&aSuccessCallback.Value(), entry);
FileSystemUtils::DispatchRunnable(GetParentObject(), runnable.forget());
}
return;
}
// Subdirectories, but this is a file.
if (entry->IsFile()) {
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
NS_ERROR_DOM_NOT_FOUND_ERR);
return;
}
// Let's recreate a path without the first directory.
nsAutoString path;
StringJoinAppend(
path,
NS_LITERAL_STRING_FROM_CSTRING(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL),
Span{parts}.From(1));
auto* directoryEntry = static_cast<FileSystemDirectoryEntry*>(entry.get());
directoryEntry->GetInternal(path, aFlag, aSuccessCallback, aErrorCallback,
aType);
}
} // namespace mozilla::dom
|