1use 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};
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;
34
35#[derive(Clone, JSTraceable, MallocSizeOf)]
38struct RepresentationDataPromiseFulfillmentHandler {
39 #[conditional_malloc_size_of]
40 promise: Rc<Promise>,
41 type_: String,
42}
43
44impl Callback for RepresentationDataPromiseFulfillmentHandler {
45 fn callback(&self, cx: &mut CurrentRealm, v: SafeHandleValue) {
47 if v.get().is_string() {
49 let data_as_bytes =
51 match DOMString::safe_from_jsval(cx, v, StringificationBehavior::Default) {
52 Ok(ConversionResult::Success(s)) => s.as_bytes().to_owned(),
53 _ => return,
54 };
55
56 let blob_data = Blob::new(
58 cx,
59 &self.promise.global(),
60 BlobImpl::new_from_bytes(data_as_bytes, self.type_.clone()),
61 );
62
63 self.promise.resolve_native(cx, &blob_data);
65 }
66 else if DomRoot::<Blob>::safe_from_jsval(cx, v, ())
68 .is_ok_and(|result| result.get_success_value().is_some())
69 {
70 self.promise.resolve(cx, v);
72 }
73 }
74}
75
76#[derive(Clone, JSTraceable, MallocSizeOf)]
79struct RepresentationDataPromiseRejectionHandler {
80 #[conditional_malloc_size_of]
81 promise: Rc<Promise>,
82}
83
84impl Callback for RepresentationDataPromiseRejectionHandler {
85 fn callback(&self, cx: &mut CurrentRealm, _v: SafeHandleValue) {
87 self.promise.reject_error(cx, Error::NotFound(None));
89 }
90}
91
92const CUSTOM_FORMAT_PREFIX: &str = "web ";
94
95#[derive(JSTraceable, MallocSizeOf)]
97pub(super) struct Representation {
98 #[no_trace]
99 #[ignore_malloc_size_of = "Extern type"]
100 pub mime_type: Mime,
101 pub is_custom: bool,
102 #[conditional_malloc_size_of]
103 pub data: Rc<Promise>,
104}
105
106#[dom_struct]
107pub(crate) struct ClipboardItem {
108 reflector_: Reflector,
109 representations: DomRefCell<Vec<Representation>>,
110 presentation_style: DomRefCell<PresentationStyle>,
111 #[ignore_malloc_size_of = "mozjs"]
112 frozen_types: CachedFrozenArray,
113}
114
115impl ClipboardItem {
116 fn new_inherited() -> ClipboardItem {
117 ClipboardItem {
118 reflector_: Reflector::new(),
119 representations: Default::default(),
120 presentation_style: Default::default(),
121 frozen_types: CachedFrozenArray::new(),
122 }
123 }
124
125 fn new(
126 cx: &mut JSContext,
127 window: &Window,
128 proto: Option<HandleObject>,
129 ) -> DomRoot<ClipboardItem> {
130 reflect_dom_object_with_proto(cx, Box::new(ClipboardItem::new_inherited()), window, proto)
131 }
132}
133
134impl ClipboardItemMethods<crate::DomTypeHolder> for ClipboardItem {
135 fn Constructor(
137 cx: &mut JSContext,
138 global: &Window,
139 proto: Option<HandleObject>,
140 items: Record<DOMString, Rc<Promise>>,
141 options: &ClipboardItemOptions,
142 ) -> Fallible<DomRoot<ClipboardItem>> {
143 if items.is_empty() {
145 return Err(Error::Type(c"No item provided".to_owned()));
146 }
147
148 let clipboard_item = ClipboardItem::new(cx, global, proto);
153
154 *clipboard_item
156 .presentation_style
157 .safe_borrow_mut(cx.no_gc()) = options.presentationStyle;
158
159 for (key, value) in items.deref() {
161 let key = key.str();
166 let (key, is_custom) = match key.strip_prefix(CUSTOM_FORMAT_PREFIX) {
167 None => (&*key, false),
168 Some(stripped) => (stripped, true),
170 };
171
172 let mime_type =
175 Mime::from_str(key).map_err(|_| Error::Type(c"Invalid mime type".to_owned()))?;
176
177 if clipboard_item
180 .representations
181 .borrow()
182 .iter()
183 .any(|representation| {
184 representation.mime_type == mime_type && representation.is_custom == is_custom
185 })
186 {
187 return Err(Error::Type(c"Tried to add a duplicate mime".to_owned()));
188 }
189
190 let representation = Representation {
195 mime_type,
196 is_custom,
197 data: value.clone(),
198 };
199
200 clipboard_item
202 .representations
203 .safe_borrow_mut(cx.no_gc())
204 .push(representation);
205 }
206
207 Ok(clipboard_item)
210 }
211
212 fn PresentationStyle(&self) -> PresentationStyle {
214 *self.presentation_style.borrow()
215 }
216
217 fn Types(&self, cx: &mut JSContext, retval: MutableHandleValue) {
219 self.frozen_types.get_or_init(
220 cx,
221 || {
222 let mut types = Vec::new();
224
225 self.representations
226 .borrow()
227 .iter()
228 .for_each(|representation| {
229 let mime_type_string = representation.mime_type.to_string();
231
232 let mime_type_string = if representation.is_custom {
234 format!("{}{}", CUSTOM_FORMAT_PREFIX, mime_type_string)
235 } else {
236 mime_type_string
237 };
238
239 types.push(DOMString::from(mime_type_string));
241 });
242 types
243 },
244 retval,
245 );
246 }
247
248 fn GetType(&self, realm: &mut CurrentRealm, type_: DOMString) -> Fallible<Rc<Promise>> {
250 let global = self.global();
252
253 let type_ = type_.str();
258 let (type_, is_custom) = match type_.strip_prefix(CUSTOM_FORMAT_PREFIX) {
259 None => (&*type_, false),
260 Some(stripped) => (stripped, true),
262 };
263
264 let mime_type =
267 Mime::from_str(type_).map_err(|_| Error::Type(c"Invalid mime type".to_owned()))?;
268
269 let item_type_list = self.representations.borrow();
271
272 let p = Promise::new_in_realm(realm);
274
275 for representation in item_type_list.iter() {
277 if representation.mime_type == mime_type && representation.is_custom == is_custom {
279 let representation_data_promise = &representation.data;
281
282 let fulfillment_handler = Box::new(RepresentationDataPromiseFulfillmentHandler {
284 promise: p.clone(),
285 type_: representation.mime_type.to_string(),
286 });
287 let rejection_handler =
288 Box::new(RepresentationDataPromiseRejectionHandler { promise: p.clone() });
289
290 let handler = PromiseNativeHandler::new(
291 realm,
292 &global,
293 Some(fulfillment_handler),
294 Some(rejection_handler),
295 );
296 representation_data_promise.append_native_handler(realm, &handler);
297
298 return Ok(p);
300 }
301 }
302
303 p.reject_error(realm, Error::NotFound(None));
305
306 Ok(p)
308 }
309
310 fn Supports(_: &Window, type_: DOMString) -> bool {
312 type_ == "text/plain"
316 }
317}