script/
clipboard_provider.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 base::id::WebViewId;
6use embedder_traits::{EmbedderMsg, ScriptToEmbedderChan};
7use ipc_channel::ipc::channel;
8use malloc_size_of_derive::MallocSizeOf;
9
10/// A trait which abstracts access to the embedder's clipboard in order to allow unit
11/// testing clipboard-dependent parts of `script`.
12pub trait ClipboardProvider {
13    /// Get the text content of the clipboard.
14    fn get_text(&mut self) -> Result<String, String>;
15    /// Set the text content of the clipboard.
16    fn set_text(&mut self, _: String);
17}
18
19#[derive(MallocSizeOf)]
20pub(crate) struct EmbedderClipboardProvider {
21    pub embedder_sender: ScriptToEmbedderChan,
22    pub webview_id: WebViewId,
23}
24
25impl ClipboardProvider for EmbedderClipboardProvider {
26    fn get_text(&mut self) -> Result<String, String> {
27        let (tx, rx) = channel().unwrap();
28        self.embedder_sender
29            .send(EmbedderMsg::GetClipboardText(self.webview_id, tx))
30            .unwrap();
31        rx.recv().unwrap()
32    }
33    fn set_text(&mut self, s: String) {
34        self.embedder_sender
35            .send(EmbedderMsg::SetClipboardText(self.webview_id, s))
36            .unwrap();
37    }
38}