Skip to main content

script/dom/filesystem/
filesystemdirectoryentry.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 dom_struct::dom_struct;
6use js::context::JSContext;
7use script_bindings::cell::DomRefCell;
8use script_bindings::reflector::reflect_dom_object;
9
10use crate::dom::bindings::callback::RootedCallback;
11use crate::dom::bindings::codegen::Bindings::FileSystemDirectoryEntryBinding::{
12    FileSystemDirectoryEntryMethods, FileSystemFlags,
13};
14use crate::dom::bindings::codegen::Bindings::FileSystemEntryBinding::{
15    ErrorCallback, FileSystemEntryCallback,
16};
17use crate::dom::bindings::reflector::DomGlobal;
18use crate::dom::bindings::root::{Dom, DomRoot};
19use crate::dom::bindings::str::USVString;
20use crate::dom::filesystem::FileSystem;
21use crate::dom::filesystemdirectoryreader::FileSystemDirectoryReader;
22use crate::dom::filesystementry::FileSystemEntry;
23use crate::dom::globalscope::GlobalScope;
24
25#[dom_struct]
26pub(crate) struct FileSystemDirectoryEntry {
27    filesystementry: FileSystemEntry,
28    children: DomRefCell<Vec<Dom<FileSystemEntry>>>,
29}
30
31impl FileSystemDirectoryEntry {
32    fn new_inherited(name: USVString, full_path: USVString) -> FileSystemDirectoryEntry {
33        FileSystemDirectoryEntry {
34            filesystementry: FileSystemEntry::new_inherited(name, full_path, false),
35            children: DomRefCell::new(Vec::new()),
36        }
37    }
38
39    pub(crate) fn new(
40        cx: &mut JSContext,
41        global: &GlobalScope,
42        name: USVString,
43        full_path: USVString,
44    ) -> DomRoot<FileSystemDirectoryEntry> {
45        reflect_dom_object(
46            cx,
47            Box::new(FileSystemDirectoryEntry::new_inherited(name, full_path)),
48            global,
49        )
50    }
51
52    pub(crate) fn set_filesystem(&self, fs: &FileSystem) {
53        self.filesystementry.set_filesystem(fs);
54    }
55
56    pub(crate) fn push_child(&self, child: &FileSystemEntry) {
57        self.children.borrow_mut().push(Dom::from_ref(child));
58    }
59
60    pub(crate) fn children(&self) -> Vec<DomRoot<FileSystemEntry>> {
61        self.children
62            .borrow()
63            .iter()
64            .map(|child| DomRoot::from_ref(&**child))
65            .collect()
66    }
67}
68
69impl FileSystemDirectoryEntryMethods<crate::DomTypeHolder> for FileSystemDirectoryEntry {
70    /// <https://wicg.github.io/entries-api/#dom-filesystemdirectoryentry-createreader>
71    fn CreateReader(&self, cx: &mut JSContext) -> DomRoot<FileSystemDirectoryReader> {
72        FileSystemDirectoryReader::new(cx, &self.global(), self)
73    }
74
75    /// <https://wicg.github.io/entries-api/#dom-filesystemdirectoryentry-getfile>
76    fn GetFile(
77        &self,
78        _path: Option<Option<USVString>>,
79        _options: &FileSystemFlags,
80        _success_callback: Option<RootedCallback<FileSystemEntryCallback>>,
81        _error_callback: Option<RootedCallback<ErrorCallback>>,
82    ) {
83        // TODO: Implement per spec 7.2. Need to hook embedder.
84    }
85
86    /// <https://wicg.github.io/entries-api/#dom-filesystemdirectoryentry-getdirectory>
87    fn GetDirectory(
88        &self,
89        _path: Option<Option<USVString>>,
90        _options: &FileSystemFlags,
91        _success_callback: Option<RootedCallback<FileSystemEntryCallback>>,
92        _error_callback: Option<RootedCallback<ErrorCallback>>,
93    ) {
94        // TODO: Implement per spec 7.2. Need to hook embedder.
95    }
96}