servo/components/script/clipboard_provider.rs
Martin Robinson 19e41ab9f9
libservo: Add a ClipboardDelegate and a default implementation (#35297)
Add a `ClipboardDelegate` to the `WebView` API and a default
implementation in libservo for this delegate that works on Mac, Windows,
and Linux. Support for Android will be added in the future. This means
that embedders do not need to do anything special to get clipboard
support, but can choose to override it or implement it for other
platforms.

In addition, this adds support for handling fetches of clipboard contents
and renames things to reflect that eventually other types of clipboard
content will be supported. Part of this is removing the string
argument from the `ClipboardEventType::Paste` enum because script will
need to get other types of content from the clipboard than just a
string. It now talks to the embedder to get this information directly.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-02-07 10:43:46 +00:00

43 lines
1.5 KiB
Rust

/* 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 embedder_traits::EmbedderMsg;
use ipc_channel::ipc::channel;
use script_traits::{ScriptMsg, ScriptToConstellationChan};
/// 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(ScriptMsg::ForwardToEmbedder(EmbedderMsg::GetClipboardText(
self.webview_id,
tx,
)))
.unwrap();
rx.recv().unwrap()
}
fn set_text(&mut self, s: String) {
self.constellation_sender
.send(ScriptMsg::ForwardToEmbedder(EmbedderMsg::SetClipboardText(
self.webview_id,
s,
)))
.unwrap();
}
}