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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/* -*- 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 "FileSystemDirectoryReader.h"
#include "CallbackRunnables.h"
#include "FileSystemFileEntry.h"
#include "js/Array.h" // JS::NewArrayObject
#include "js/PropertyAndElement.h" // JS_GetElement
#include "mozilla/dom/FileBinding.h"
#include "mozilla/dom/FileSystem.h"
#include "mozilla/dom/FileSystemDirectoryReaderBinding.h"
#include "mozilla/dom/FileSystemUtils.h"
#include "mozilla/dom/Directory.h"
#include "mozilla/dom/DirectoryBinding.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PromiseNativeHandler.h"
namespace mozilla::dom {
namespace {
class PromiseHandler final : public PromiseNativeHandler {
public:
NS_DECL_ISUPPORTS
PromiseHandler(FileSystemDirectoryEntry* aParentEntry,
FileSystem* aFileSystem,
FileSystemEntriesCallback* aSuccessCallback,
ErrorCallback* aErrorCallback)
: mParentEntry(aParentEntry),
mFileSystem(aFileSystem),
mSuccessCallback(aSuccessCallback),
mErrorCallback(aErrorCallback) {
MOZ_ASSERT(aParentEntry);
MOZ_ASSERT(aFileSystem);
MOZ_ASSERT(aSuccessCallback);
}
MOZ_CAN_RUN_SCRIPT
virtual void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
ErrorResult& aRv) override {
if (NS_WARN_IF(!aValue.isObject())) {
return;
}
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
uint32_t length;
if (NS_WARN_IF(!JS::GetArrayLength(aCx, obj, &length))) {
return;
}
Sequence<OwningNonNull<FileSystemEntry>> sequence;
if (NS_WARN_IF(!sequence.SetLength(length, fallible))) {
return;
}
for (uint32_t i = 0; i < length; ++i) {
JS::Rooted<JS::Value> value(aCx);
if (NS_WARN_IF(!JS_GetElement(aCx, obj, i, &value))) {
return;
}
if (NS_WARN_IF(!value.isObject())) {
return;
}
JS::Rooted<JSObject*> valueObj(aCx, &value.toObject());
RefPtr<File> file;
if (NS_SUCCEEDED(UNWRAP_OBJECT(File, valueObj, file))) {
RefPtr<FileSystemFileEntry> entry = new FileSystemFileEntry(
mParentEntry->GetParentObject(), file, mParentEntry, mFileSystem);
sequence[i] = entry;
continue;
}
RefPtr<Directory> directory;
if (NS_WARN_IF(
NS_FAILED(UNWRAP_OBJECT(Directory, valueObj, directory)))) {
return;
}
RefPtr<FileSystemDirectoryEntry> entry =
new FileSystemDirectoryEntry(mParentEntry->GetParentObject(),
directory, mParentEntry, mFileSystem);
sequence[i] = entry;
}
mSuccessCallback->Call(sequence);
}
virtual void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
ErrorResult& aRv) override {
if (mErrorCallback) {
RefPtr<ErrorCallbackRunnable> runnable = new ErrorCallbackRunnable(
mParentEntry->GetParentObject(), mErrorCallback,
NS_ERROR_DOM_INVALID_STATE_ERR);
FileSystemUtils::DispatchRunnable(mParentEntry->GetParentObject(),
runnable.forget());
}
}
private:
~PromiseHandler() = default;
RefPtr<FileSystemDirectoryEntry> mParentEntry;
RefPtr<FileSystem> mFileSystem;
const RefPtr<FileSystemEntriesCallback> mSuccessCallback;
RefPtr<ErrorCallback> mErrorCallback;
};
NS_IMPL_ISUPPORTS0(PromiseHandler);
} // anonymous namespace
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FileSystemDirectoryReader, mParentEntry,
mDirectory, mFileSystem)
NS_IMPL_CYCLE_COLLECTING_ADDREF(FileSystemDirectoryReader)
NS_IMPL_CYCLE_COLLECTING_RELEASE(FileSystemDirectoryReader)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileSystemDirectoryReader)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
FileSystemDirectoryReader::FileSystemDirectoryReader(
FileSystemDirectoryEntry* aParentEntry, FileSystem* aFileSystem,
Directory* aDirectory)
: mParentEntry(aParentEntry),
mFileSystem(aFileSystem),
mDirectory(aDirectory),
mAlreadyRead(false) {
MOZ_ASSERT(aParentEntry);
MOZ_ASSERT(aFileSystem);
}
FileSystemDirectoryReader::~FileSystemDirectoryReader() = default;
JSObject* FileSystemDirectoryReader::WrapObject(
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
return FileSystemDirectoryReader_Binding::Wrap(aCx, this, aGivenProto);
}
void FileSystemDirectoryReader::ReadEntries(
FileSystemEntriesCallback& aSuccessCallback,
const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback,
ErrorResult& aRv) {
MOZ_ASSERT(mDirectory);
if (mAlreadyRead) {
RefPtr<EmptyEntriesCallbackRunnable> runnable =
new EmptyEntriesCallbackRunnable(&aSuccessCallback);
FileSystemUtils::DispatchRunnable(GetParentObject(), runnable.forget());
return;
}
// This object can be used only once.
mAlreadyRead = true;
ErrorResult rv;
RefPtr<Promise> promise = mDirectory->GetFilesAndDirectories(rv);
if (NS_WARN_IF(rv.Failed())) {
ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
rv.StealNSResult());
return;
}
RefPtr<PromiseHandler> handler = new PromiseHandler(
mParentEntry, mFileSystem, &aSuccessCallback,
aErrorCallback.WasPassed() ? &aErrorCallback.Value() : nullptr);
promise->AppendNativeHandler(handler);
}
} // namespace mozilla::dom
|