aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/clipboard_provider.rs
blob: 4c0f5467be6342a427149aa5f01bce9ed4341407 (plain) (blame)
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
/* 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 base::id::WebViewId;
use constellation_traits::{ScriptToConstellationChan, ScriptToConstellationMessage};
use embedder_traits::EmbedderMsg;
use ipc_channel::ipc::channel;

/// A trait which abstracts access to the embedder's clipboard in order to allow unit
/// testing clipboard-dependent parts of `script`.
pub trait ClipboardProvider {
    /// Get the text content of the clipboard.
    fn get_text(&mut self) -> Result<String, String>;
    /// Set the text content of the clipboard.
    fn set_text(&mut self, _: String);
}

pub(crate) struct EmbedderClipboardProvider {
    pub constellation_sender: ScriptToConstellationChan,
    pub webview_id: WebViewId,
}

impl ClipboardProvider for EmbedderClipboardProvider {
    fn get_text(&mut self) -> Result<String, String> {
        let (tx, rx) = channel().unwrap();
        self.constellation_sender
            .send(ScriptToConstellationMessage::ForwardToEmbedder(
                EmbedderMsg::GetClipboardText(self.webview_id, tx),
            ))
            .unwrap();
        rx.recv().unwrap()
    }
    fn set_text(&mut self, s: String) {
        self.constellation_sender
            .send(ScriptToConstellationMessage::ForwardToEmbedder(
                EmbedderMsg::SetClipboardText(self.webview_id, s),
            ))
            .unwrap();
    }
}