Skip to main content

script/dom/filesystem/
filesystemfileentry.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use 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    /// <https://wicg.github.io/entries-api/#dom-filesystemfileentry-file>
76    fn File(
77        &self,
78        success_callback: RootedCallback<FileCallback>,
79        _error_callback: Option<RootedCallback<ErrorCallback>>,
80    ) {
81        // Per spec 7.4: in parallel,
82        // 1. (TODO) Evaluate path
83        // 2-3. (TODO) errorCallback
84
85        // Note: Step 1 - 3 is meant to re-check if file exists on OS filesystem.
86        // It is unreachable for now, as the file data is already
87        // stored in-memory on this entry as `File` (set by webkitGetAsEntry).
88
89        // 4. on success, queue a task to invoke successCallback
90        // with a new `File` object representing item and "report".
91
92        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}