script/dom/
datatransfer.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::{Ref, RefCell};
6use std::rc::Rc;
7
8use dom_struct::dom_struct;
9use js::rust::{HandleObject, MutableHandleValue};
10use net_traits::image_cache::Image;
11
12use crate::dom::bindings::cell::DomRefCell;
13use crate::dom::bindings::codegen::Bindings::DataTransferBinding::DataTransferMethods;
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object_with_proto};
16use crate::dom::bindings::root::{Dom, DomRoot};
17use crate::dom::bindings::str::DOMString;
18use crate::dom::datatransferitemlist::DataTransferItemList;
19use crate::dom::element::Element;
20use crate::dom::filelist::FileList;
21use crate::dom::html::htmlimageelement::HTMLImageElement;
22use crate::dom::window::Window;
23use crate::drag_data_store::{DragDataStore, Mode};
24use crate::script_runtime::{CanGc, JSContext};
25
26const VALID_DROP_EFFECTS: [&str; 4] = ["none", "copy", "link", "move"];
27const VALID_EFFECTS_ALLOWED: [&str; 9] = [
28    "none",
29    "copy",
30    "copyLink",
31    "copyMove",
32    "link",
33    "linkMove",
34    "move",
35    "all",
36    "uninitialized",
37];
38
39#[dom_struct]
40pub(crate) struct DataTransfer {
41    reflector_: Reflector,
42    drop_effect: DomRefCell<DOMString>,
43    effect_allowed: DomRefCell<DOMString>,
44    items: Dom<DataTransferItemList>,
45    #[ignore_malloc_size_of = "Rc"]
46    #[no_trace]
47    data_store: Rc<RefCell<Option<DragDataStore>>>,
48}
49
50impl DataTransfer {
51    fn new_inherited(
52        data_store: Rc<RefCell<Option<DragDataStore>>>,
53        item_list: &DataTransferItemList,
54    ) -> DataTransfer {
55        DataTransfer {
56            reflector_: Reflector::new(),
57            drop_effect: DomRefCell::new(DOMString::from("none")),
58            effect_allowed: DomRefCell::new(DOMString::from("none")),
59            items: Dom::from_ref(item_list),
60            data_store,
61        }
62    }
63
64    pub(crate) fn new_with_proto(
65        window: &Window,
66        proto: Option<HandleObject>,
67        can_gc: CanGc,
68        data_store: Rc<RefCell<Option<DragDataStore>>>,
69    ) -> DomRoot<DataTransfer> {
70        let item_list = DataTransferItemList::new(window, Rc::clone(&data_store), can_gc);
71
72        reflect_dom_object_with_proto(
73            Box::new(DataTransfer::new_inherited(data_store, &item_list)),
74            window,
75            proto,
76            can_gc,
77        )
78    }
79
80    pub(crate) fn new(
81        window: &Window,
82        data_store: Rc<RefCell<Option<DragDataStore>>>,
83        can_gc: CanGc,
84    ) -> DomRoot<DataTransfer> {
85        Self::new_with_proto(window, None, can_gc, data_store)
86    }
87
88    pub(crate) fn data_store(&self) -> Option<Ref<'_, DragDataStore>> {
89        Ref::filter_map(self.data_store.borrow(), |data_store| data_store.as_ref()).ok()
90    }
91}
92
93impl DataTransferMethods<crate::DomTypeHolder> for DataTransfer {
94    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer>
95    fn Constructor(
96        window: &Window,
97        proto: Option<HandleObject>,
98        can_gc: CanGc,
99    ) -> DomRoot<DataTransfer> {
100        let mut drag_data_store = DragDataStore::new();
101        drag_data_store.set_mode(Mode::ReadWrite);
102
103        let data_store = Rc::new(RefCell::new(Some(drag_data_store)));
104
105        DataTransfer::new_with_proto(window, proto, can_gc, data_store)
106    }
107
108    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-dropeffect>
109    fn DropEffect(&self) -> DOMString {
110        self.drop_effect.borrow().clone()
111    }
112
113    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-dropeffect>
114    fn SetDropEffect(&self, value: DOMString) {
115        if VALID_DROP_EFFECTS.contains(&value.as_ref()) {
116            *self.drop_effect.borrow_mut() = value;
117        }
118    }
119
120    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-effectallowed>
121    fn EffectAllowed(&self) -> DOMString {
122        self.effect_allowed.borrow().clone()
123    }
124
125    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-effectallowed>
126    fn SetEffectAllowed(&self, value: DOMString) {
127        if self
128            .data_store
129            .borrow()
130            .as_ref()
131            .is_some_and(|data_store| data_store.mode() == Mode::ReadWrite) &&
132            VALID_EFFECTS_ALLOWED.contains(&value.as_ref())
133        {
134            *self.drop_effect.borrow_mut() = value;
135        }
136    }
137
138    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-items>
139    fn Items(&self) -> DomRoot<DataTransferItemList> {
140        DomRoot::from_ref(&self.items)
141    }
142
143    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-setdragimage>
144    fn SetDragImage(&self, image: &Element, x: i32, y: i32) {
145        // Step 1 If the DataTransfer is no longer associated with a data store, return.
146        let mut option = self.data_store.borrow_mut();
147        let data_store = match option.as_mut() {
148            Some(value) => value,
149            None => return,
150        };
151
152        // Step 2 If the data store's mode is not the read/write mode, return.
153        if data_store.mode() != Mode::ReadWrite {
154            return;
155        }
156
157        // Step 3
158        if let Some(image) = image.downcast::<HTMLImageElement>() {
159            match image.image_data().as_ref().and_then(Image::as_raster_image) {
160                Some(image) => data_store.set_bitmap(Some(image), x, y),
161                None => warn!("Vector images are not yet supported in setDragImage"),
162            }
163        }
164    }
165
166    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-types>
167    fn Types(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
168        self.items.frozen_types(cx, retval, can_gc);
169    }
170
171    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-getdata>
172    fn GetData(&self, mut format: DOMString) -> DOMString {
173        // Step 1 If the DataTransfer object is not associated with a drag data store, then return the empty string.
174        let option = self.data_store.borrow();
175        let data_store = match option.as_ref() {
176            Some(value) => value,
177            None => return DOMString::new(),
178        };
179
180        // Step 2 If the drag data store's mode is the protected mode, then return the empty string.
181        if data_store.mode() == Mode::Protected {
182            return DOMString::new();
183        }
184
185        // Step 3 Let format be the first argument, converted to ASCII lowercase.
186        format.make_ascii_lowercase();
187        // Step 4 Let convert-to-URL be false.
188        let mut convert_to_url = false;
189
190        let type_ = match format.as_ref() {
191            // Step 5 If format equals "text", change it to "text/plain".
192            "text" => DOMString::from("text/plain"),
193            // Step 6 If format equals "url", change it to "text/uri-list" and set convert-to-URL to true.
194            "url" => {
195                convert_to_url = true;
196                DOMString::from("text/uri-list")
197            },
198            _ => format,
199        };
200
201        let data = data_store.find_matching_text(&type_);
202
203        // Step 8
204        if let Some(result) = data {
205            // Step 9 If convert-to-URL is true, then parse result as appropriate for text/uri-list data,
206            // and then set result to the first URL from the list, if any, or the empty string otherwise.
207            if convert_to_url {
208                // TODO parse uri-list as [RFC2483]
209            }
210
211            // Step 10 Return result.
212            result
213        } else {
214            // Step 7 If there is no item in the drag data store item list
215            // whose kind is text and whose type string is equal to format, return the empty string.
216            DOMString::new()
217        }
218    }
219
220    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-setdata>
221    fn SetData(&self, format: DOMString, data: DOMString) {
222        // Step 1 If the DataTransfer object is no longer associated with a drag data store, return. Nothing happens.
223        let mut option = self.data_store.borrow_mut();
224        let data_store = match option.as_mut() {
225            Some(value) => value,
226            None => return,
227        };
228
229        // Step 2 If the drag data store's mode is not the read/write mode, return. Nothing happens.
230        if data_store.mode() != Mode::ReadWrite {
231            return;
232        }
233
234        data_store.set_data(format, data);
235        self.items.invalidate_frozen_types();
236    }
237
238    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-cleardata>
239    fn ClearData(&self, format: Option<DOMString>) {
240        // Step 1 If the DataTransfer is not associated with a data store, return.
241        let mut option = self.data_store.borrow_mut();
242        let data_store = match option.as_mut() {
243            Some(value) => value,
244            None => return,
245        };
246
247        // Step 2 If the data store is not in the read/write mode, return.
248        if data_store.mode() != Mode::ReadWrite {
249            return;
250        }
251
252        if data_store.clear_data(format) {
253            self.items.invalidate_frozen_types();
254        }
255    }
256
257    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-files>
258    fn Files(&self, can_gc: CanGc) -> DomRoot<FileList> {
259        // Step 1 Start with an empty list.
260        let mut files = Vec::new();
261
262        // Step 2 If the DataTransfer is not associated with a data store return the empty list.
263        if let Some(data_store) = self.data_store.borrow().as_ref() {
264            data_store.files(&self.global(), can_gc, &mut files);
265        }
266
267        // Step 5
268        FileList::new(self.global().as_window(), files, can_gc)
269    }
270}