script/dom/clipboard/clipboard.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::rc::Rc;
6use std::str::FromStr;
7
8use data_url::mime::Mime;
9use dom_struct::dom_struct;
10use embedder_traits::EmbedderMsg;
11use js::context::JSContext;
12use js::realm::CurrentRealm;
13use js::rust::HandleValue as SafeHandleValue;
14use script_bindings::reflector::reflect_dom_object_with_cx;
15use servo_constellation_traits::BlobImpl;
16
17use super::clipboarditem::Representation;
18use crate::dom::bindings::codegen::Bindings::ClipboardBinding::{
19 ClipboardMethods, PresentationStyle,
20};
21use crate::dom::bindings::error::Error;
22use crate::dom::bindings::refcounted::TrustedPromise;
23use crate::dom::bindings::reflector::DomGlobal;
24use crate::dom::bindings::root::DomRoot;
25use crate::dom::bindings::str::DOMString;
26use crate::dom::blob::Blob;
27use crate::dom::eventtarget::EventTarget;
28use crate::dom::globalscope::GlobalScope;
29use crate::dom::promise::Promise;
30use crate::dom::promisenativehandler::{Callback, PromiseNativeHandler};
31use crate::dom::window::Window;
32use crate::realms::enter_auto_realm;
33use crate::routed_promise::{RoutedPromiseListener, callback_promise};
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-clipboard-readtext>.
38#[derive(Clone, JSTraceable, MallocSizeOf)]
39struct RepresentationDataPromiseFulfillmentHandler {
40 #[conditional_malloc_size_of]
41 promise: Rc<Promise>,
42}
43
44impl Callback for RepresentationDataPromiseFulfillmentHandler {
45 /// The fulfillment case of Step 3.4.1.1.4.3 of
46 /// <https://w3c.github.io/clipboard-apis/#dom-clipboard-readtext>.
47 fn callback(&self, cx: &mut CurrentRealm, v: SafeHandleValue) {
48 // If v is a DOMString, then follow the below steps:
49 // Resolve p with v.
50 // Return p.
51 self.promise.resolve(cx.into(), v, CanGc::from_cx(cx));
52
53 // NOTE: Since we ask text from arboard, v can't be a Blob
54 // If v is a Blob, then follow the below steps:
55 // Let string be the result of UTF-8 decoding v’s underlying byte sequence.
56 // Resolve p with string.
57 // Return p.
58 }
59}
60
61/// The rejection handler for the reacting to representationDataPromise part of
62/// <https://w3c.github.io/clipboard-apis/#dom-clipboard-readtext>.
63#[derive(Clone, JSTraceable, MallocSizeOf)]
64struct RepresentationDataPromiseRejectionHandler {
65 #[conditional_malloc_size_of]
66 promise: Rc<Promise>,
67}
68
69impl Callback for RepresentationDataPromiseRejectionHandler {
70 /// The rejection case of Step 3.4.1.1.4.3 of
71 /// <https://w3c.github.io/clipboard-apis/#dom-clipboard-readtext>.
72 fn callback(&self, cx: &mut CurrentRealm, _v: SafeHandleValue) {
73 // Reject p with "NotFoundError" DOMException in realm.
74 // Return p.
75 self.promise
76 .reject_error(Error::NotFound(None), CanGc::from_cx(cx));
77 }
78}
79
80#[dom_struct]
81pub(crate) struct Clipboard {
82 event_target: EventTarget,
83}
84
85impl Clipboard {
86 fn new_inherited() -> Clipboard {
87 Clipboard {
88 event_target: EventTarget::new_inherited(),
89 }
90 }
91
92 pub(crate) fn new(cx: &mut JSContext, global: &GlobalScope) -> DomRoot<Clipboard> {
93 reflect_dom_object_with_cx(Box::new(Clipboard::new_inherited()), global, cx)
94 }
95}
96
97impl ClipboardMethods<crate::DomTypeHolder> for Clipboard {
98 /// <https://w3c.github.io/clipboard-apis/#dom-clipboard-readtext>
99 fn ReadText(&self, realm: &mut CurrentRealm) -> Rc<Promise> {
100 // Step 1 Let realm be this's relevant realm.
101 let global = self.global();
102
103 // Step 2 Let p be a new promise in realm.
104 let p = Promise::new_in_realm(realm);
105
106 // Step 3 Run the following steps in parallel:
107
108 // TODO Step 3.1 Let r be the result of running check clipboard read permission.
109 // Step 3.2 If r is false, then:
110 // Step 3.2.1 Queue a global task on the permission task source, given realm’s global object,
111 // to reject p with "NotAllowedError" DOMException in realm.
112 // Step 3.2.2 Abort these steps.
113
114 // Step 3.3 Let data be a copy of the system clipboard data.
115 let window = global.as_window();
116 let callback = callback_promise(&p, self, global.task_manager().clipboard_task_source());
117 window.send_to_embedder(EmbedderMsg::GetClipboardText(window.webview_id(), callback));
118
119 // Step 3.4 Queue a global task on the clipboard task source,
120 // given realm’s global object, to perform the below steps:
121 // NOTE: We queue the task inside route_promise and perform the steps inside handle_response
122
123 p
124 }
125
126 /// <https://w3c.github.io/clipboard-apis/#dom-clipboard-writetext>
127 fn WriteText(&self, realm: &mut CurrentRealm, data: DOMString) -> Rc<Promise> {
128 // Step 1 Let realm be this's relevant realm.
129 let global = self.global();
130 // Step 2 Let p be a new promise in realm.
131 let p = Promise::new_in_realm(realm);
132
133 // Step 3 Run the following steps in parallel:
134
135 // TODO write permission could be removed from spec
136 // Step 3.1 Let r be the result of running check clipboard write permission.
137 // Step 3.2 If r is false, then:
138 // Step 3.2.1 Queue a global task on the permission task source, given realm’s global object,
139 // to reject p with "NotAllowedError" DOMException in realm.
140 // Step 3.2.2 Abort these steps.
141
142 let trusted_promise = TrustedPromise::new(p.clone());
143 let bytes = Vec::from(data);
144
145 // Step 3.3 Queue a global task on the clipboard task source,
146 // given realm’s global object, to perform the below steps:
147 global.task_manager().clipboard_task_source().queue(
148 task!(write_to_system_clipboard: move |cx| {
149 let promise = trusted_promise.root();
150 let global = promise.global();
151
152 // Step 3.3.1 Let itemList be an empty sequence<Blob>.
153 let mut item_list = Vec::new();
154
155 // Step 3.3.2 Let textBlob be a new Blob created with: type attribute set to "text/plain;charset=utf-8",
156 // and its underlying byte sequence set to the UTF-8 encoding of data.
157 let text_blob = Blob::new(
158 &global,
159 BlobImpl::new_from_bytes(bytes, "text/plain;charset=utf-8".into()),
160 CanGc::from_cx(cx),
161 );
162
163 // Step 3.3.3 Add textBlob to itemList.
164 item_list.push(text_blob);
165
166 // Step 3.3.4 Let option be set to "unspecified".
167 let option = PresentationStyle::Unspecified;
168
169 // Step 3.3.5 Write blobs and option to the clipboard with itemList and option.
170 write_blobs_and_option_to_the_clipboard(global.as_window(), item_list, option);
171
172 // Step 3.3.6 Resolve p.
173 promise.resolve_native(&(), CanGc::from_cx(cx));
174 }),
175 );
176
177 // Step 3.4 Return p.
178 p
179 }
180}
181
182impl RoutedPromiseListener<Result<String, String>> for Clipboard {
183 fn handle_response(
184 &self,
185 cx: &mut js::context::JSContext,
186 response: Result<String, String>,
187 promise: &Rc<Promise>,
188 ) {
189 let global = self.global();
190 let text = response.unwrap_or_default();
191
192 // Step 3.4.1 For each systemClipboardItem in data:
193 // Step 3.4.1.1 For each systemClipboardRepresentation in systemClipboardItem:
194 // TODO: Arboard provide the first item that has a String representation
195
196 // Step 3.4.1.1.1 Let mimeType be the result of running the
197 // well-known mime type from os specific format algorithm given systemClipboardRepresentation’s name.
198 // Note: This is done by arboard, so we just convert the format to a MIME
199 let mime_type = Mime::from_str("text/plain").unwrap();
200
201 // Step 3.4.1.1.2 If mimeType is null, continue this loop.
202 // Note: Since the previous step is infallible, we don't need to handle this case
203
204 // Step 3.4.1.1.3 Let representation be a new representation.
205 let representation = Representation {
206 mime_type,
207 is_custom: false,
208 data: Promise::new_resolved(
209 &global,
210 GlobalScope::get_cx(),
211 DOMString::from(text),
212 CanGc::from_cx(cx),
213 ),
214 };
215
216 // Step 3.4.1.1.4 If representation’s MIME type essence is "text/plain", then:
217
218 // Step 3.4.1.1.4.1 Set representation’s MIME type to mimeType.
219 // Note: Done when creating a new representation
220
221 // Step 3.4.1.1.4.2 Let representationDataPromise be the representation’s data.
222 // Step 3.4.1.1.4.3 React to representationDataPromise:
223 let fulfillment_handler = Box::new(RepresentationDataPromiseFulfillmentHandler {
224 promise: promise.clone(),
225 });
226 let rejection_handler = Box::new(RepresentationDataPromiseRejectionHandler {
227 promise: promise.clone(),
228 });
229 let handler = PromiseNativeHandler::new(
230 &global,
231 Some(fulfillment_handler),
232 Some(rejection_handler),
233 CanGc::from_cx(cx),
234 );
235 let mut realm = enter_auto_realm(cx, &*global);
236 let cx = &mut realm.current_realm();
237 representation.data.append_native_handler(cx, &handler);
238
239 // Step 3.4.2 Reject p with "NotFoundError" DOMException in realm.
240 // Step 3.4.3 Return p.
241 // NOTE: We follow the same behaviour of Gecko by doing nothing if no text is available instead of rejecting p
242 }
243}
244
245/// <https://w3c.github.io/clipboard-apis/#write-blobs-and-option-to-the-clipboard>
246fn write_blobs_and_option_to_the_clipboard(
247 window: &Window,
248 items: Vec<DomRoot<Blob>>,
249 _presentation_style: PresentationStyle,
250) {
251 // TODO Step 1 Let webCustomFormats be a sequence<Blob>.
252
253 // Step 2 For each item in items:
254 for item in items {
255 // TODO support more formats than just text/plain
256 // Step 2.1 Let formatString be the result of running os specific well-known format given item’s type.
257
258 // Step 2.2 If formatString is empty then follow the below steps:
259
260 // Step 2.2.1 Let webCustomFormatString be the item’s type.
261 // Step 2.2.2 Let webCustomFormat be an empty type.
262 // Step 2.2.3 If webCustomFormatString starts with "web " prefix,
263 // then remove the "web " prefix and store the remaining string in webMimeTypeString.
264 // Step 2.2.4 Let webMimeType be the result of parsing a MIME type given webMimeTypeString.
265 // Step 2.2.5 If webMimeType is failure, then abort all steps.
266 // Step 2.2.6 Let webCustomFormat’s type's essence equal to webMimeType.
267 // Step 2.2.7 Set item’s type to webCustomFormat.
268 // Step 2.2.8 Append webCustomFormat to webCustomFormats.
269
270 // Step 2.3 Let payload be the result of UTF-8 decoding item’s underlying byte sequence.
271 // Step 2.4 Insert payload and presentationStyle into the system clipboard
272 // using formatString as the native clipboard format.
273 window.send_to_embedder(EmbedderMsg::SetClipboardText(
274 window.webview_id(),
275 String::from_utf8(
276 item.get_bytes()
277 .expect("No bytes found for Blob created by caller"),
278 )
279 .expect("DOMString contained invalid bytes"),
280 ));
281 }
282
283 // TODO Step 3 Write web custom formats given webCustomFormats.
284 // Needs support to arbitrary formats inside arboard
285}