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