1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::rc::Rc;
use constellation_traits::BlobImpl;
use dom_struct::dom_struct;
use embedder_traits::EmbedderMsg;
use crate::dom::bindings::codegen::Bindings::ClipboardBinding::{
ClipboardMethods, PresentationStyle,
};
use crate::dom::bindings::refcounted::TrustedPromise;
use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::blob::Blob;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::promise::Promise;
use crate::dom::window::Window;
use crate::script_runtime::CanGc;
#[dom_struct]
pub(crate) struct Clipboard {
event_target: EventTarget,
}
impl Clipboard {
fn new_inherited() -> Clipboard {
Clipboard {
event_target: EventTarget::new_inherited(),
}
}
pub(crate) fn new(global: &GlobalScope, can_gc: CanGc) -> DomRoot<Clipboard> {
reflect_dom_object(Box::new(Clipboard::new_inherited()), global, can_gc)
}
}
impl ClipboardMethods<crate::DomTypeHolder> for Clipboard {
/// <https://w3c.github.io/clipboard-apis/#dom-clipboard-writetext>
fn WriteText(&self, data: DOMString, can_gc: CanGc) -> Rc<Promise> {
// Step 1 Let realm be this's relevant realm.
// Step 2 Let p be a new promise in realm.
let p = Promise::new(&self.global(), can_gc);
// Step 3 Run the following steps in parallel:
// TODO write permission could be removed from spec
// Step 3.1 Let r be the result of running check clipboard write permission.
// Step 3.2 If r is false, then:
// Step 3.2.1 Queue a global task on the permission task source, given realm’s global object,
// to reject p with "NotAllowedError" DOMException in realm.
// Step 3.2.2 Abort these steps.
let trusted_promise = TrustedPromise::new(p.clone());
let bytes = Vec::from(data);
// Step 3.3 Queue a global task on the clipboard task source,
// given realm’s global object, to perform the below steps:
self.global().task_manager().clipboard_task_source().queue(
task!(write_to_system_clipboard: move || {
let promise = trusted_promise.root();
let global = promise.global();
// Step 3.3.1 Let itemList be an empty sequence<Blob>.
let mut item_list = Vec::new();
// Step 3.3.2 Let textBlob be a new Blob created with: type attribute set to "text/plain;charset=utf-8",
// and its underlying byte sequence set to the UTF-8 encoding of data.
let text_blob = Blob::new(
&global,
BlobImpl::new_from_bytes(bytes, "text/plain;charset=utf-8".into()),
CanGc::note(),
);
// Step 3.3.3 Add textBlob to itemList.
item_list.push(text_blob);
// Step 3.3.4 Let option be set to "unspecified".
let option = PresentationStyle::Unspecified;
// Step 3.3.5 Write blobs and option to the clipboard with itemList and option.
write_blobs_and_option_to_the_clipboard(global.as_window(), item_list, option);
// Step 3.3.6 Resolve p.
promise.resolve_native(&(), CanGc::note());
}),
);
// Step 3.4 Return p.
p
}
}
/// <https://w3c.github.io/clipboard-apis/#write-blobs-and-option-to-the-clipboard>
fn write_blobs_and_option_to_the_clipboard(
window: &Window,
items: Vec<DomRoot<Blob>>,
_presentation_style: PresentationStyle,
) {
// TODO Step 1 Let webCustomFormats be a sequence<Blob>.
// Step 2 For each item in items:
for item in items {
// TODO support more formats than just text/plain
// Step 2.1 Let formatString be the result of running os specific well-known format given item’s type.
// Step 2.2 If formatString is empty then follow the below steps:
// Step 2.2.1 Let webCustomFormatString be the item’s type.
// Step 2.2.2 Let webCustomFormat be an empty type.
// Step 2.2.3 If webCustomFormatString starts with "web " prefix,
// then remove the "web " prefix and store the remaining string in webMimeTypeString.
// Step 2.2.4 Let webMimeType be the result of parsing a MIME type given webMimeTypeString.
// Step 2.2.5 If webMimeType is failure, then abort all steps.
// Step 2.2.6 Let webCustomFormat’s type's essence equal to webMimeType.
// Step 2.2.7 Set item’s type to webCustomFormat.
// Step 2.2.8 Append webCustomFormat to webCustomFormats.
// Step 2.3 Let payload be the result of UTF-8 decoding item’s underlying byte sequence.
// Step 2.4 Insert payload and presentationStyle into the system clipboard
// using formatString as the native clipboard format.
window.send_to_embedder(EmbedderMsg::SetClipboardText(
window.webview_id(),
String::from_utf8(
item.get_bytes()
.expect("No bytes found for Blob created by caller"),
)
.expect("DOMString contained invalid bytes"),
));
}
// TODO Step 3 Write web custom formats given webCustomFormats.
// Needs support to arbitrary formats inside arboard
}
|