Skip to main content

script/dom/clipboard/
clipboarditem.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::ops::Deref;
6use std::rc::Rc;
7use std::str::FromStr;
8
9use data_url::mime::Mime;
10use dom_struct::dom_struct;
11use js::context::JSContext;
12use js::realm::CurrentRealm;
13use js::rust::{HandleObject, HandleValue as SafeHandleValue, MutableHandleValue};
14use script_bindings::cell::DomRefCell;
15use script_bindings::record::Record;
16use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto_and_cx};
17use servo_constellation_traits::BlobImpl;
18
19use crate::dom::bindings::codegen::Bindings::ClipboardBinding::{
20    ClipboardItemMethods, ClipboardItemOptions, PresentationStyle,
21};
22use crate::dom::bindings::conversions::{
23    ConversionResult, FromJSValConvertible, StringificationBehavior,
24};
25use crate::dom::bindings::error::{Error, Fallible};
26use crate::dom::bindings::frozenarray::CachedFrozenArray;
27use crate::dom::bindings::reflector::DomGlobal;
28use crate::dom::bindings::root::DomRoot;
29use crate::dom::bindings::str::DOMString;
30use crate::dom::blob::Blob;
31use crate::dom::promise::Promise;
32use crate::dom::promisenativehandler::{Callback, PromiseNativeHandler};
33use crate::dom::window::Window;
34use crate::script_runtime::CanGc;
35
36/// The fulfillment handler for the reacting to representationDataPromise part of
37/// <https://w3c.github.io/clipboard-apis/#dom-clipboarditem-gettype>.
38#[derive(Clone, JSTraceable, MallocSizeOf)]
39struct RepresentationDataPromiseFulfillmentHandler {
40    #[conditional_malloc_size_of]
41    promise: Rc<Promise>,
42    type_: String,
43}
44
45impl Callback for RepresentationDataPromiseFulfillmentHandler {
46    /// Substeps of 8.1.2.1 If representationDataPromise was fulfilled with value v, then:
47    fn callback(&self, cx: &mut CurrentRealm, v: SafeHandleValue) {
48        // 1. If v is a DOMString, then follow the below steps:
49        if v.get().is_string() {
50            // 1.1 Let dataAsBytes be the result of UTF-8 encoding v.
51            let data_as_bytes =
52                match DOMString::safe_from_jsval(cx, v, StringificationBehavior::Default) {
53                    Ok(ConversionResult::Success(s)) => s.as_bytes().to_owned(),
54                    _ => return,
55                };
56
57            // 1.2 Let blobData be a Blob created using dataAsBytes with its type set to mimeType, serialized.
58            let blob_data = Blob::new(
59                cx,
60                &self.promise.global(),
61                BlobImpl::new_from_bytes(data_as_bytes, self.type_.clone()),
62            );
63
64            // 1.3 Resolve p with blobData.
65            self.promise.resolve_native_with_cx(cx, &blob_data);
66        }
67        // 2. If v is a Blob, then follow the below steps:
68        else if DomRoot::<Blob>::safe_from_jsval(cx, v, ())
69            .is_ok_and(|result| result.get_success_value().is_some())
70        {
71            // 2.1 Resolve p with v.
72            self.promise.resolve_with_cx(cx, v);
73        }
74    }
75}
76
77/// The rejection handler for the reacting to representationDataPromise part of
78/// <https://w3c.github.io/clipboard-apis/#dom-clipboarditem-gettype>.
79#[derive(Clone, JSTraceable, MallocSizeOf)]
80struct RepresentationDataPromiseRejectionHandler {
81    #[conditional_malloc_size_of]
82    promise: Rc<Promise>,
83}
84
85impl Callback for RepresentationDataPromiseRejectionHandler {
86    /// Substeps of 8.1.2.2 If representationDataPromise was rejected, then:
87    fn callback(&self, cx: &mut CurrentRealm, _v: SafeHandleValue) {
88        // 1. Reject p with "NotFoundError" DOMException in realm.
89        self.promise.reject_error_with_cx(cx, Error::NotFound(None));
90    }
91}
92
93/// <https://w3c.github.io/clipboard-apis/#web-custom-format>
94const CUSTOM_FORMAT_PREFIX: &str = "web ";
95
96/// <https://w3c.github.io/clipboard-apis/#representation>
97#[derive(JSTraceable, MallocSizeOf)]
98pub(super) struct Representation {
99    #[no_trace]
100    #[ignore_malloc_size_of = "Extern type"]
101    pub mime_type: Mime,
102    pub is_custom: bool,
103    #[conditional_malloc_size_of]
104    pub data: Rc<Promise>,
105}
106
107#[dom_struct]
108pub(crate) struct ClipboardItem {
109    reflector_: Reflector,
110    representations: DomRefCell<Vec<Representation>>,
111    presentation_style: DomRefCell<PresentationStyle>,
112    #[ignore_malloc_size_of = "mozjs"]
113    frozen_types: CachedFrozenArray,
114}
115
116impl ClipboardItem {
117    fn new_inherited() -> ClipboardItem {
118        ClipboardItem {
119            reflector_: Reflector::new(),
120            representations: Default::default(),
121            presentation_style: Default::default(),
122            frozen_types: CachedFrozenArray::new(),
123        }
124    }
125
126    fn new(
127        cx: &mut JSContext,
128        window: &Window,
129        proto: Option<HandleObject>,
130    ) -> DomRoot<ClipboardItem> {
131        reflect_dom_object_with_proto_and_cx(
132            Box::new(ClipboardItem::new_inherited()),
133            window,
134            proto,
135            cx,
136        )
137    }
138}
139
140impl ClipboardItemMethods<crate::DomTypeHolder> for ClipboardItem {
141    /// <https://w3c.github.io/clipboard-apis/#dom-clipboarditem-clipboarditem>
142    fn Constructor(
143        cx: &mut JSContext,
144        global: &Window,
145        proto: Option<HandleObject>,
146        items: Record<DOMString, Rc<Promise>>,
147        options: &ClipboardItemOptions,
148    ) -> Fallible<DomRoot<ClipboardItem>> {
149        // Step 1 If items is empty, then throw a TypeError.
150        if items.is_empty() {
151            return Err(Error::Type(c"No item provided".to_owned()));
152        }
153
154        // Step 2 If options is empty, then set options["presentationStyle"] = "unspecified".
155        // NOTE: This is done inside bindings
156
157        // Step 3 Set this's clipboard item to a new clipboard item.
158        let clipboard_item = ClipboardItem::new(cx, global, proto);
159
160        // Step 4 Set this's clipboard item's presentation style to options["presentationStyle"].
161        *clipboard_item.presentation_style.borrow_mut() = options.presentationStyle;
162
163        // Step 6 For each (key, value) in items:
164        for (key, value) in items.deref() {
165            // Step 6.2 Let isCustom be false.
166
167            // Step 6.3 If key starts with `"web "` prefix, then
168            // Step 6.3.1 Remove `"web "` prefix and assign the remaining string to key.
169            let key = key.str();
170            let (key, is_custom) = match key.strip_prefix(CUSTOM_FORMAT_PREFIX) {
171                None => (&*key, false),
172                // Step 6.3.2 Set isCustom true
173                Some(stripped) => (stripped, true),
174            };
175
176            // Step 6.5 Let mimeType be the result of parsing a MIME type given key.
177            // Step 6.6 If mimeType is failure, then throw a TypeError.
178            let mime_type =
179                Mime::from_str(key).map_err(|_| Error::Type(c"Invalid mime type".to_owned()))?;
180
181            // Step 6.7 If this's clipboard item's list of representations contains a representation
182            // whose MIME type is mimeType and whose [representation/isCustom] is isCustom, then throw a TypeError.
183            if clipboard_item
184                .representations
185                .borrow()
186                .iter()
187                .any(|representation| {
188                    representation.mime_type == mime_type && representation.is_custom == is_custom
189                })
190            {
191                return Err(Error::Type(c"Tried to add a duplicate mime".to_owned()));
192            }
193
194            // Step 6.1 Let representation be a new representation.
195            // Step 6.4 Set representation’s isCustom flag to isCustom.
196            // Step 6.8 Set representation’s MIME type to mimeType.
197            // Step 6.9 Set representation’s data to value.
198            let representation = Representation {
199                mime_type,
200                is_custom,
201                data: value.clone(),
202            };
203
204            // Step 6.10 Append representation to this's clipboard item's list of representations.
205            clipboard_item
206                .representations
207                .borrow_mut()
208                .push(representation);
209        }
210
211        // NOTE: The steps for creating a frozen array from the list of mimeType are done in the Types() method
212
213        Ok(clipboard_item)
214    }
215
216    /// <https://w3c.github.io/clipboard-apis/#dom-clipboarditem-presentationstyle>
217    fn PresentationStyle(&self) -> PresentationStyle {
218        *self.presentation_style.borrow()
219    }
220
221    /// <https://w3c.github.io/clipboard-apis/#dom-clipboarditem-types>
222    fn Types(&self, cx: &mut JSContext, retval: MutableHandleValue) {
223        self.frozen_types.get_or_init(
224            || {
225                // Step 5 Let types be a list of DOMString.
226                let mut types = Vec::new();
227
228                self.representations
229                    .borrow()
230                    .iter()
231                    .for_each(|representation| {
232                        // Step 6.11 Let mimeTypeString be the result of serializing a MIME type with mimeType.
233                        let mime_type_string = representation.mime_type.to_string();
234
235                        // Step 6.12 If isCustom is true, prefix mimeTypeString with `"web "`.
236                        let mime_type_string = if representation.is_custom {
237                            format!("{}{}", CUSTOM_FORMAT_PREFIX, mime_type_string)
238                        } else {
239                            mime_type_string
240                        };
241
242                        // Step 6.13 Add mimeTypeString to types.
243                        types.push(DOMString::from(mime_type_string));
244                    });
245                types
246            },
247            cx.into(),
248            retval,
249            CanGc::from_cx(cx),
250        );
251    }
252
253    /// <https://w3c.github.io/clipboard-apis/#dom-clipboarditem-gettype>
254    fn GetType(&self, realm: &mut CurrentRealm, type_: DOMString) -> Fallible<Rc<Promise>> {
255        // Step 1 Let realm be this’s relevant realm.
256        let global = self.global();
257
258        // Step 2 Let isCustom be false.
259
260        // Step 3 If type starts with `"web "` prefix, then:
261        // Step 3.1 Remove `"web "` prefix and assign the remaining string to type.
262        let type_ = type_.str();
263        let (type_, is_custom) = match type_.strip_prefix(CUSTOM_FORMAT_PREFIX) {
264            None => (&*type_, false),
265            // Step 3.2 Set isCustom to true.
266            Some(stripped) => (stripped, true),
267        };
268
269        // Step 4 Let mimeType be the result of parsing a MIME type given type.
270        // Step 5 If mimeType is failure, then throw a TypeError.
271        let mime_type =
272            Mime::from_str(type_).map_err(|_| Error::Type(c"Invalid mime type".to_owned()))?;
273
274        // Step 6 Let itemTypeList be this’s clipboard item’s list of representations.
275        let item_type_list = self.representations.borrow();
276
277        // Step 7 Let p be a new promise in realm.
278        let p = Promise::new_in_realm(realm);
279
280        // Step 8 For each representation in itemTypeList
281        for representation in item_type_list.iter() {
282            // Step 8.1 If representation’s MIME type is mimeType and representation’s isCustom is isCustom, then:
283            if representation.mime_type == mime_type && representation.is_custom == is_custom {
284                // Step 8.1.1 Let representationDataPromise be the representation’s data.
285                let representation_data_promise = &representation.data;
286
287                // Step 8.1.2 React to representationDataPromise:
288                let fulfillment_handler = Box::new(RepresentationDataPromiseFulfillmentHandler {
289                    promise: p.clone(),
290                    type_: representation.mime_type.to_string(),
291                });
292                let rejection_handler =
293                    Box::new(RepresentationDataPromiseRejectionHandler { promise: p.clone() });
294
295                let handler = PromiseNativeHandler::new(
296                    realm,
297                    &global,
298                    Some(fulfillment_handler),
299                    Some(rejection_handler),
300                );
301                representation_data_promise.append_native_handler(realm, &handler);
302
303                // Step 8.1.3 Return p.
304                return Ok(p);
305            }
306        }
307
308        // Step 9 Reject p with "NotFoundError" DOMException in realm.
309        p.reject_error_with_cx(realm, Error::NotFound(None));
310
311        // Step 10 Return p.
312        Ok(p)
313    }
314
315    /// <https://w3c.github.io/clipboard-apis/#dom-clipboarditem-supports>
316    fn Supports(_: &Window, type_: DOMString) -> bool {
317        // TODO Step 1 If type is in mandatory data types or optional data types, then return true.
318        // Step 2 If not, then return false.
319        // NOTE: We only supports text/plain
320        type_ == "text/plain"
321    }
322}