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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* 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 https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use dom_struct::dom_struct;
use js::context::JSContext;
use script_bindings::cell::DomRefCell;
use script_bindings::reflector::{Reflector, reflect_dom_object};
use crate::dom::bindings::callback::{ExceptionHandling, RootedCallback, TracedCallback};
use crate::dom::bindings::codegen::Bindings::FileSystemDirectoryReaderBinding::{
FileSystemDirectoryReaderMethods, FileSystemEntriesCallback,
};
use crate::dom::bindings::codegen::Bindings::FileSystemEntryBinding::ErrorCallback;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::DomGlobal;
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
use crate::dom::domexception::{DOMErrorName, DOMException};
use crate::dom::filesystemdirectoryentry::FileSystemDirectoryEntry;
use crate::dom::globalscope::GlobalScope;
/// Maximum number of entries returned by a single `readEntries()` call,
/// matching Chromium's limit.
const READ_ENTRIES_BATCH_SIZE: usize = 100;
#[dom_struct]
pub(crate) struct FileSystemDirectoryReader {
reflector_: Reflector,
dir: Dom<FileSystemDirectoryEntry>,
idx: Cell<usize>,
reading_flag: Cell<bool>,
done_flag: Cell<bool>,
reader_error: MutNullableDom<DOMException>,
pending_callbacks: DomRefCell<Vec<PendingEntriesCallback>>,
next_callback: Cell<usize>,
}
#[derive(JSTraceable, MallocSizeOf)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
struct PendingEntriesCallback {
id: usize,
callback: TracedCallback<FileSystemEntriesCallback>,
error: Option<TracedCallback<ErrorCallback>>,
}
/// The action for a `readEntries` call decided synchronously.
#[derive(Clone, Copy)]
enum ReadEntriesAction {
/// Another read already in progress.
InvalidState,
/// A previous read failed.
ReaderError,
/// Every entry already produced.
Done,
/// Start a new read.
Read,
}
impl FileSystemDirectoryReader {
fn new_inherited(dir: &FileSystemDirectoryEntry) -> FileSystemDirectoryReader {
FileSystemDirectoryReader {
reflector_: Reflector::new(),
dir: Dom::from_ref(dir),
idx: Cell::new(0),
reading_flag: Cell::new(false),
done_flag: Cell::new(false),
reader_error: MutNullableDom::new(None),
pending_callbacks: Default::default(),
next_callback: Cell::new(0),
}
}
pub(crate) fn new(
cx: &mut JSContext,
global: &GlobalScope,
dir: &FileSystemDirectoryEntry,
) -> DomRoot<FileSystemDirectoryReader> {
reflect_dom_object(
cx,
Box::new(FileSystemDirectoryReader::new_inherited(dir)),
global,
)
}
}
impl FileSystemDirectoryReaderMethods<crate::DomTypeHolder> for FileSystemDirectoryReader {
/// <https://wicg.github.io/entries-api/#dom-filesystemdirectoryreader-readentries>
fn ReadEntries(
&self,
success_callback: RootedCallback<FileSystemEntriesCallback>,
error_callback: Option<RootedCallback<ErrorCallback>>,
) {
// Steps 1-4. The checks and the flag update run synchronously,
// before the callback task is queued.
//
// 1. If this’s reading flag is true, queue a task to invoke
// errorCallback with « a newly created "InvalidStateError"
// DOMException » and "report", and abort these steps.
// 2. If this’s reader error is not null, queue a task to invoke
// errorCallback (if given) with « reader error » and "report", and
// abort these steps.
// 3. If this’s done flag is true, queue a task to invoke
// successCallback with an empty list and "report", and abort these
// steps.
// 4. Set this’s reading flag to true.
let action = if self.reading_flag.get() {
ReadEntriesAction::InvalidState
} else if self.reader_error.get().is_some() {
ReadEntriesAction::ReaderError
} else if self.done_flag.get() {
ReadEntriesAction::Done
} else {
self.reading_flag.set(true);
ReadEntriesAction::Read
};
let id = self.next_callback.get();
self.pending_callbacks
.borrow_mut()
.push(PendingEntriesCallback {
id,
callback: success_callback.to_traced(),
error: error_callback.map(|callback| callback.to_traced()),
});
self.next_callback.set(id + 1);
let this = Trusted::new(self);
self.global()
.task_manager()
.dom_manipulation_task_source()
.queue(task!(invoke_read_entries: move |cx| {
let this = this.root();
let maybe_index = this
.pending_callbacks
.borrow()
.iter()
.position(|val| val.id == id);
if let Some(index) = maybe_index {
let error_callback = this
.pending_callbacks
.borrow_mut()
.get(index)
.and_then(|pending| pending.error.as_ref())
.map(|callback| callback.root(cx));
rooted!(&in(cx) let callback = this
.pending_callbacks
.borrow_mut()
.swap_remove(index)
.callback);
match action {
ReadEntriesAction::InvalidState => {
// 1. Invoke errorCallback with « a newly created
// "InvalidStateError" DOMException » and "report".
let error = DOMException::new(
cx,
&this.global(),
DOMErrorName::InvalidStateError,
);
if let Some(error_callback) = error_callback {
let _ = error_callback.Call__(
cx,
&error,
ExceptionHandling::Report,
);
}
}
ReadEntriesAction::ReaderError => {
// 2. Invoke errorCallback (if given) with « reader error » and "report".
let error = this
.reader_error
.get()
.expect("reader error must be set");
if let Some(error_callback) = error_callback {
let _ = error_callback.Call__(
cx,
&error,
ExceptionHandling::Report,
);
}
}
ReadEntriesAction::Done => {
// 3. Invoke successCallback with an empty list.
let _ = callback.Call__(cx, Vec::new(), ExceptionHandling::Report);
}
ReadEntriesAction::Read => {
// 5. In parallel, run these steps:
//
// 5.1. If this’s directory is null, then..
// TODO: (Not reachable until we hook embedder)
//
// 5.2. Let entries be a non-zero number of
// entries from this’s directory that have
// not yet been produced by this
// FileSystemDirectoryReader, if any.
let start = this.idx.get();
let entries = this.dir.children_batch(start, READ_ENTRIES_BATCH_SIZE);
this.idx.set(start + entries.len());
//
// 5.3. If the previous step failed (for example,
// the directory was deleted or permission is denied), then:
// (Not reachable as we use in-memory entries currently)
//
// 1. Queue a task to run these steps:
// 1. Let error be a an appropriate DOMException.
// 2. Set this’s reader error to error.
// 3. Set this’s reading flag to false.
// 4. Invoke errorCallback (if given) with
// « error » and "report".
// 2. Abort these steps.
// TODO: Reading the in-memory children cannot
// currently fail; this is where
// `reader_error` would be set.
//
// 5.4. Queue a task to run these steps:
// 5.4.1. If entries is empty, then set this’s done flag to true.
if entries.is_empty() {
this.done_flag.set(true);
}
// 5.4.2. Set this’s reading flag to false.
this.reading_flag.set(false);
// 5.4.3. Invoke successCallback with « entries » and "report".
let _ = callback.Call__(cx, entries, ExceptionHandling::Report);
}
}
}
}));
}
}