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