Skip to main content

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