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;
11use script_bindings::match_domstring_ascii;
12
13use crate::dom::bindings::cell::DomRefCell;
14use crate::dom::bindings::codegen::Bindings::DataTransferBinding::DataTransferMethods;
15use crate::dom::bindings::inheritance::Castable;
16use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object_with_proto};
17use crate::dom::bindings::root::{Dom, DomRoot};
18use crate::dom::bindings::str::DOMString;
19use crate::dom::datatransferitemlist::DataTransferItemList;
20use crate::dom::element::Element;
21use crate::dom::filelist::FileList;
22use crate::dom::html::htmlimageelement::HTMLImageElement;
23use crate::dom::window::Window;
24use crate::drag_data_store::{DragDataStore, Mode};
25use crate::script_runtime::{CanGc, JSContext};
26
27const VALID_DROP_EFFECTS: [&str; 4] = ["none", "copy", "link", "move"];
28const VALID_EFFECTS_ALLOWED: [&str; 9] = [
29    "none",
30    "copy",
31    "copyLink",
32    "copyMove",
33    "link",
34    "linkMove",
35    "move",
36    "all",
37    "uninitialized",
38];
39
40#[dom_struct]
41pub(crate) struct DataTransfer {
42    reflector_: Reflector,
43    drop_effect: DomRefCell<DOMString>,
44    effect_allowed: DomRefCell<DOMString>,
45    items: Dom<DataTransferItemList>,
46    #[conditional_malloc_size_of]
47    #[no_trace]
48    data_store: Rc<RefCell<Option<DragDataStore>>>,
49}
50
51impl DataTransfer {
52    fn new_inherited(
53        data_store: Rc<RefCell<Option<DragDataStore>>>,
54        item_list: &DataTransferItemList,
55    ) -> DataTransfer {
56        DataTransfer {
57            reflector_: Reflector::new(),
58            drop_effect: DomRefCell::new(DOMString::from("none")),
59            effect_allowed: DomRefCell::new(DOMString::from("none")),
60            items: Dom::from_ref(item_list),
61            data_store,
62        }
63    }
64
65    pub(crate) fn new_with_proto(
66        window: &Window,
67        proto: Option<HandleObject>,
68        can_gc: CanGc,
69        data_store: Rc<RefCell<Option<DragDataStore>>>,
70    ) -> DomRoot<DataTransfer> {
71        let item_list = DataTransferItemList::new(window, Rc::clone(&data_store), can_gc);
72
73        reflect_dom_object_with_proto(
74            Box::new(DataTransfer::new_inherited(data_store, &item_list)),
75            window,
76            proto,
77            can_gc,
78        )
79    }
80
81    pub(crate) fn new(
82        window: &Window,
83        data_store: Rc<RefCell<Option<DragDataStore>>>,
84        can_gc: CanGc,
85    ) -> DomRoot<DataTransfer> {
86        Self::new_with_proto(window, None, can_gc, data_store)
87    }
88
89    pub(crate) fn data_store(&self) -> Option<Ref<'_, DragDataStore>> {
90        Ref::filter_map(self.data_store.borrow(), |data_store| data_store.as_ref()).ok()
91    }
92}
93
94impl DataTransferMethods<crate::DomTypeHolder> for DataTransfer {
95    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer>
96    fn Constructor(
97        window: &Window,
98        proto: Option<HandleObject>,
99        can_gc: CanGc,
100    ) -> DomRoot<DataTransfer> {
101        let mut drag_data_store = DragDataStore::new();
102        drag_data_store.set_mode(Mode::ReadWrite);
103
104        let data_store = Rc::new(RefCell::new(Some(drag_data_store)));
105
106        DataTransfer::new_with_proto(window, proto, can_gc, data_store)
107    }
108
109    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-dropeffect>
110    fn DropEffect(&self) -> DOMString {
111        self.drop_effect.borrow().clone()
112    }
113
114    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-dropeffect>
115    fn SetDropEffect(&self, value: DOMString) {
116        if VALID_DROP_EFFECTS.contains(&&*value.str()) {
117            *self.drop_effect.borrow_mut() = value;
118        }
119    }
120
121    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-effectallowed>
122    fn EffectAllowed(&self) -> DOMString {
123        self.effect_allowed.borrow().clone()
124    }
125
126    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-effectallowed>
127    fn SetEffectAllowed(&self, value: DOMString) {
128        if self
129            .data_store
130            .borrow()
131            .as_ref()
132            .is_some_and(|data_store| data_store.mode() == Mode::ReadWrite) &&
133            VALID_EFFECTS_ALLOWED.contains(&&*value.str())
134        {
135            *self.drop_effect.borrow_mut() = value;
136        }
137    }
138
139    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-items>
140    fn Items(&self) -> DomRoot<DataTransferItemList> {
141        DomRoot::from_ref(&self.items)
142    }
143
144    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-setdragimage>
145    fn SetDragImage(&self, image: &Element, x: i32, y: i32) {
146        // Step 1 If the DataTransfer is no longer associated with a data store, return.
147        let mut option = self.data_store.borrow_mut();
148        let data_store = match option.as_mut() {
149            Some(value) => value,
150            None => return,
151        };
152
153        // Step 2 If the data store's mode is not the read/write mode, return.
154        if data_store.mode() != Mode::ReadWrite {
155            return;
156        }
157
158        // Step 3
159        if let Some(image) = image.downcast::<HTMLImageElement>() {
160            match image.image_data().as_ref().and_then(Image::as_raster_image) {
161                Some(image) => data_store.set_bitmap(Some(image), x, y),
162                None => warn!("Vector images are not yet supported in setDragImage"),
163            }
164        }
165    }
166
167    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-types>
168    fn Types(&self, cx: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
169        self.items.frozen_types(cx, retval, can_gc);
170    }
171
172    /// <https://html.spec.whatwg.org/multipage/#dom-datatransfer-getdata>
173    fn GetData(&self, mut format: DOMString) -> DOMString {
174        // Step 1 If the DataTransfer object is not associated with a drag data store, then return the empty string.
175        let option = self.data_store.borrow();
176        let data_store = match option.as_ref() {
177            Some(value) => value,
178            None => return DOMString::new(),
179        };
180
181        // Step 2 If the drag data store's mode is the protected mode, then return the empty string.
182        if data_store.mode() == Mode::Protected {
183            return DOMString::new();
184        }
185
186        // Step 3 Let format be the first argument, converted to ASCII lowercase.
187        format.make_ascii_lowercase();
188        // Step 4 Let convert-to-URL be false.
189        let mut convert_to_url = false;
190
191        let type_ = match_domstring_ascii!(format,
192            // Step 5 If format equals "text", change it to "text/plain".
193            "text" => DOMString::from("text/plain"),
194            // Step 6 If format equals "url", change it to "text/uri-list" and set convert-to-URL to true.
195            "url" => {
196                convert_to_url = true;
197                DOMString::from("text/uri-list")
198            },
199            _ => format.clone(),);
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}