script/dom/filesystem/
filesystemfileentry.rs1use std::cell::Cell;
6
7use dom_struct::dom_struct;
8use js::context::JSContext;
9use script_bindings::cell::DomRefCell;
10use script_bindings::reflector::reflect_dom_object;
11
12use crate::dom::bindings::callback::{ExceptionHandling, RootedCallback, TracedCallback};
13use crate::dom::bindings::codegen::Bindings::FileSystemEntryBinding::ErrorCallback;
14use crate::dom::bindings::codegen::Bindings::FileSystemFileEntryBinding::{
15 FileCallback, FileSystemFileEntryMethods,
16};
17use crate::dom::bindings::refcounted::Trusted;
18use crate::dom::bindings::reflector::DomGlobal;
19use crate::dom::bindings::root::{Dom, DomRoot};
20use crate::dom::bindings::str::USVString;
21use crate::dom::file::File;
22use crate::dom::filesystem::FileSystem;
23use crate::dom::filesystementry::FileSystemEntry;
24use crate::dom::globalscope::GlobalScope;
25
26#[dom_struct]
27pub(crate) struct FileSystemFileEntry {
28 filesystementry: FileSystemEntry,
29 file: Dom<File>,
30 pending_callbacks: DomRefCell<Vec<PendingFileCallback>>,
31 next_callback: Cell<usize>,
32}
33
34#[derive(JSTraceable, MallocSizeOf)]
35#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
36struct PendingFileCallback {
37 id: usize,
38 callback: TracedCallback<FileCallback>,
39}
40
41impl FileSystemFileEntry {
42 fn new_inherited(name: USVString, full_path: USVString, file: &File) -> FileSystemFileEntry {
43 FileSystemFileEntry {
44 filesystementry: FileSystemEntry::new_inherited(name, full_path, true),
45 file: Dom::from_ref(file),
46 pending_callbacks: Default::default(),
47 next_callback: Cell::new(0),
48 }
49 }
50
51 pub(crate) fn new(
52 cx: &mut JSContext,
53 global: &GlobalScope,
54 name: USVString,
55 full_path: USVString,
56 file: &File,
57 ) -> DomRoot<FileSystemFileEntry> {
58 reflect_dom_object(
59 cx,
60 Box::new(FileSystemFileEntry::new_inherited(name, full_path, file)),
61 global,
62 )
63 }
64
65 pub(crate) fn set_filesystem(&self, fs: &FileSystem) {
66 self.filesystementry.set_filesystem(fs);
67 }
68
69 pub(crate) fn file(&self) -> DomRoot<File> {
70 DomRoot::from_ref(&*self.file)
71 }
72}
73
74impl FileSystemFileEntryMethods<crate::DomTypeHolder> for FileSystemFileEntry {
75 fn File(
77 &self,
78 success_callback: RootedCallback<FileCallback>,
79 _error_callback: Option<RootedCallback<ErrorCallback>>,
80 ) {
81 let id = self.next_callback.get();
93 self.pending_callbacks
94 .borrow_mut()
95 .push(PendingFileCallback {
96 id,
97 callback: success_callback.to_traced(),
98 });
99 self.next_callback.set(id + 1);
100
101 let this = Trusted::new(self);
102 self.global()
103 .task_manager()
104 .dom_manipulation_task_source()
105 .queue(task!(invoke_file_callback: move |cx| {
106 let this = this.root();
107 let maybe_index = this
108 .pending_callbacks
109 .borrow()
110 .iter()
111 .position(|val| val.id == id);
112 if let Some(index) = maybe_index {
113 rooted!(&in(cx) let callback = this
114 .pending_callbacks
115 .safe_borrow_mut(cx.no_gc())
116 .swap_remove(index)
117 .callback);
118 let file = DomRoot::from_ref(&*this.file);
119 let _ = callback.Call__(cx, &file, ExceptionHandling::Report);
120 }
121 }));
122 }
123}