From 18434c99275cfa9c42e03d646c9aaa8b820bac23 Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Thu, 13 Jun 2019 20:15:12 +0200 Subject: [PATCH 01/14] Remove clipboard handling from constellation, and remove ScriptMessage for getting/setting clipboard --- components/constellation/constellation.rs | 34 ----------------------- components/script_traits/script_msg.rs | 6 ---- 2 files changed, 40 deletions(-) diff --git a/components/constellation/constellation.rs b/components/constellation/constellation.rs index 11967e10038..cf7f69479fe 100644 --- a/components/constellation/constellation.rs +++ b/components/constellation/constellation.rs @@ -341,9 +341,6 @@ pub struct Constellation { /// The size of the top-level window. window_size: WindowSizeData, - /// Means of accessing the clipboard - clipboard_ctx: Option, - /// Bits of state used to interact with the webdriver implementation webdriver: WebDriverData, @@ -692,13 +689,6 @@ where ), }, phantom: PhantomData, - clipboard_ctx: match ClipboardContext::new() { - Ok(c) => Some(c), - Err(e) => { - warn!("Error creating clipboard context ({})", e); - None - }, - }, webdriver: WebDriverData::new(), scheduler_chan: TimerScheduler::start(), document_states: HashMap::new(), @@ -1343,30 +1333,6 @@ where FromScriptMsg::Focus => { self.handle_focus_msg(source_pipeline_id); }, - FromScriptMsg::GetClipboardContents(sender) => { - let contents = match self.clipboard_ctx { - Some(ref mut ctx) => { - match ctx.get_contents() { - Ok(c) => c, - Err(e) => { - warn!("Error getting clipboard contents ({}), defaulting to empty string", e); - "".to_owned() - }, - } - }, - None => "".to_owned(), - }; - if let Err(e) = sender.send(contents) { - warn!("Failed to send clipboard ({})", e); - } - }, - FromScriptMsg::SetClipboardContents(s) => { - if let Some(ref mut ctx) = self.clipboard_ctx { - if let Err(e) = ctx.set_contents(s) { - warn!("Error setting clipboard contents ({})", e); - } - } - }, FromScriptMsg::VisibilityChangeComplete(is_visible) => { self.handle_visibility_change_complete(source_pipeline_id, is_visible); }, diff --git a/components/script_traits/script_msg.rs b/components/script_traits/script_msg.rs index 48862e8378f..6f11e2356df 100644 --- a/components/script_traits/script_msg.rs +++ b/components/script_traits/script_msg.rs @@ -121,8 +121,6 @@ pub enum ScriptMsg { CreateCanvasPaintThread(Size2D, IpcSender<(IpcSender, CanvasId)>), /// Notifies the constellation that this frame has received focus. Focus, - /// Requests that the constellation retrieve the current contents of the clipboard - GetClipboardContents(IpcSender), /// Get the top-level browsing context info for a given browsing context. GetTopForBrowsingContext( BrowsingContextId, @@ -183,8 +181,6 @@ pub enum ScriptMsg { AuxiliaryBrowsingContextLoadInfo, IpcSender, ), - /// Requests that the constellation set the contents of the clipboard - SetClipboardContents(String), /// Mark a new document as active ActivateDocument, /// Set the document state for a pipeline (used by screenshot / reftests) @@ -224,7 +220,6 @@ impl fmt::Debug for ScriptMsg { ChangeRunningAnimationsState(..) => "ChangeRunningAnimationsState", CreateCanvasPaintThread(..) => "CreateCanvasPaintThread", Focus => "Focus", - GetClipboardContents(..) => "GetClipboardContents", GetBrowsingContextInfo(..) => "GetBrowsingContextInfo", GetTopForBrowsingContext(..) => "GetParentBrowsingContext", GetChildBrowsingContextId(..) => "GetChildBrowsingContextId", @@ -242,7 +237,6 @@ impl fmt::Debug for ScriptMsg { ScriptLoadedURLInIFrame(..) => "ScriptLoadedURLInIFrame", ScriptNewIFrame(..) => "ScriptNewIFrame", ScriptNewAuxiliary(..) => "ScriptNewAuxiliary", - SetClipboardContents(..) => "SetClipboardContents", ActivateDocument => "ActivateDocument", SetDocumentState(..) => "SetDocumentState", SetFinalUrl(..) => "SetFinalUrl", From 1e5103e675db0a7e7bb844473e24b0053e19dbef Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Thu, 13 Jun 2019 20:16:04 +0200 Subject: [PATCH 02/14] Add Embbedder messages for setting/getting clipboard contents --- components/embedder_traits/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/components/embedder_traits/lib.rs b/components/embedder_traits/lib.rs index 29431ebbb63..b0364ddd23e 100644 --- a/components/embedder_traits/lib.rs +++ b/components/embedder_traits/lib.rs @@ -132,6 +132,10 @@ pub enum EmbedderMsg { AllowUnload(IpcSender), /// Sends an unconsumed key event back to the embedder. Keyboard(KeyboardEvent), + /// Gets system clipboard contents + GetClipboardContents(IpcSender), + /// Sets system clipboard contents + SetClipboardContents(String), /// Changes the cursor. SetCursor(Cursor), /// A favicon was detected @@ -175,6 +179,8 @@ impl Debug for EmbedderMsg { EmbedderMsg::AllowUnload(..) => write!(f, "AllowUnload"), EmbedderMsg::AllowNavigationRequest(..) => write!(f, "AllowNavigationRequest"), EmbedderMsg::Keyboard(..) => write!(f, "Keyboard"), + EmbedderMsg::GetClipboardContents(..) => write!(f, "GetClipboardContents"), + EmbedderMsg::SetClipboardContents(..) => write!(f, "SetClipboardContents"), EmbedderMsg::SetCursor(..) => write!(f, "SetCursor"), EmbedderMsg::NewFavicon(..) => write!(f, "NewFavicon"), EmbedderMsg::HeadParsed => write!(f, "HeadParsed"), From f4d972adb2407275e30c97b175ab4885f18de546 Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Thu, 13 Jun 2019 20:17:49 +0200 Subject: [PATCH 03/14] Forward to embedder message for setting/getting clipboard contents from clipboard provider. Create clipboard context in browser.rs and handle new messages. --- Cargo.lock | 1 + components/script/clipboard_provider.rs | 11 ++++++-- ports/glutin/Cargo.toml | 1 + ports/glutin/browser.rs | 35 +++++++++++++++++++++++++ ports/glutin/main2.rs | 1 + 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 179e7109d5d..bd223b40a86 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3935,6 +3935,7 @@ dependencies = [ "backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)", + "clipboard 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.19.7 (registry+https://github.com/rust-lang/crates.io-index)", "gleam 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/components/script/clipboard_provider.rs b/components/script/clipboard_provider.rs index 72afd5727d2..3d67d69bcef 100644 --- a/components/script/clipboard_provider.rs +++ b/components/script/clipboard_provider.rs @@ -2,6 +2,7 @@ * 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 embedder_traits::EmbedderMsg; use ipc_channel::ipc::channel; use script_traits::{ScriptMsg, ScriptToConstellationChan}; use std::borrow::ToOwned; @@ -16,11 +17,17 @@ pub trait ClipboardProvider { impl ClipboardProvider for ScriptToConstellationChan { fn clipboard_contents(&mut self) -> String { let (tx, rx) = channel().unwrap(); - self.send(ScriptMsg::GetClipboardContents(tx)).unwrap(); + self.send(ScriptMsg::ForwardToEmbedder( + EmbedderMsg::GetClipboardContents(tx), + )) + .unwrap(); rx.recv().unwrap() } fn set_clipboard_contents(&mut self, s: String) { - self.send(ScriptMsg::SetClipboardContents(s)).unwrap(); + self.send(ScriptMsg::ForwardToEmbedder( + EmbedderMsg::SetClipboardContents(s), + )) + .unwrap(); } } diff --git a/ports/glutin/Cargo.toml b/ports/glutin/Cargo.toml index 41fbf47d734..d47b8bce8a9 100644 --- a/ports/glutin/Cargo.toml +++ b/ports/glutin/Cargo.toml @@ -54,6 +54,7 @@ libc = "0.2" log = "0.4" rust-webvr = { version = "0.11", features = ["glwindow"] } tinyfiledialogs = "3.0" +clipboard = "0.5" [target.'cfg(any(target_os = "linux", target_os = "windows"))'.dependencies] image = "0.21" diff --git a/ports/glutin/browser.rs b/ports/glutin/browser.rs index e2f7d397919..bde2b6b80f4 100644 --- a/ports/glutin/browser.rs +++ b/ports/glutin/browser.rs @@ -16,6 +16,7 @@ use servo::servo_config::opts; use servo::servo_config::pref; use servo::servo_url::ServoUrl; use servo::webrender_api::ScrollLocation; +use clipboard::{ClipboardContext, ClipboardProvider}; use std::env; use std::fs::File; use std::io::Write; @@ -43,6 +44,7 @@ pub struct Browser { loading_state: Option, window: Rc, event_queue: Vec, + clipboard_ctx: Option, shutdown_requested: bool, } @@ -66,6 +68,13 @@ where favicon: None, loading_state: None, window: window, + clipboard_ctx: match ClipboardContext::new() { + Ok(c) => Some(c), + Err(e) => { + warn!("Error creating clipboard context ({})", e); + None + }, + }, event_queue: Vec::new(), shutdown_requested: false, } @@ -344,6 +353,32 @@ where EmbedderMsg::Keyboard(key_event) => { self.handle_key_from_servo(browser_id, key_event); }, + EmbedderMsg::GetClipboardContents(sender) => { + println!("glutin/browser.rs GetClipboardContents {:?} ", browser_id); + let contents = match self.clipboard_ctx { + Some(ref mut ctx) => { + match ctx.get_contents() { + Ok(c) => c, + Err(e) => { + warn!("Error getting clipboard contents ({}), defaulting to empty string", e); + "".to_owned() + }, + } + }, + None => "".to_owned(), + }; + if let Err(e) = sender.send(contents) { + warn!("Failed to send clipboard ({})", e); + } + } + EmbedderMsg::SetClipboardContents(text) => { + println!("browser.rs SetClipBoardContents {} {:?}", text, browser_id); + if let Some( ref mut ctx ) = self.clipboard_ctx { + if let Err(e) = ctx.set_contents(text) { + warn!("Error setting clipboard contents ({})", e); + } + } + } EmbedderMsg::SetCursor(cursor) => { self.window.set_cursor(cursor); }, diff --git a/ports/glutin/main2.rs b/ports/glutin/main2.rs index 496c0515e19..0e208fabb3c 100644 --- a/ports/glutin/main2.rs +++ b/ports/glutin/main2.rs @@ -9,6 +9,7 @@ extern crate log; #[cfg(all(feature = "unstable", any(target_os = "macos", target_os = "linux")))] #[macro_use] extern crate sig; +extern crate clipboard; mod app; mod browser; From 05a6495d434768b614a267ab4982facb952d207f Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Mon, 27 May 2019 19:51:37 +0200 Subject: [PATCH 04/14] Remove not needed DummyClipboardContext --- components/script/clipboard_provider.rs | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/components/script/clipboard_provider.rs b/components/script/clipboard_provider.rs index 3d67d69bcef..666bf74c825 100644 --- a/components/script/clipboard_provider.rs +++ b/components/script/clipboard_provider.rs @@ -5,7 +5,6 @@ use embedder_traits::EmbedderMsg; use ipc_channel::ipc::channel; use script_traits::{ScriptMsg, ScriptToConstellationChan}; -use std::borrow::ToOwned; pub trait ClipboardProvider { // blocking method to get the clipboard contents @@ -30,24 +29,3 @@ impl ClipboardProvider for ScriptToConstellationChan { .unwrap(); } } - -pub struct DummyClipboardContext { - content: String, -} - -impl DummyClipboardContext { - pub fn new(s: &str) -> DummyClipboardContext { - DummyClipboardContext { - content: s.to_owned(), - } - } -} - -impl ClipboardProvider for DummyClipboardContext { - fn clipboard_contents(&mut self) -> String { - self.content.clone() - } - fn set_clipboard_contents(&mut self, s: String) { - self.content = s; - } -} From 9c74e4d9ce19e338cd4d9a6f38dc856dff836cde Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Thu, 13 Jun 2019 20:28:49 +0200 Subject: [PATCH 05/14] Code cleanup --- components/constellation/constellation.rs | 1 - ports/glutin/browser.rs | 4 +--- ports/glutin/main2.rs | 2 +- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/components/constellation/constellation.rs b/components/constellation/constellation.rs index cf7f69479fe..9d3b5cf094d 100644 --- a/components/constellation/constellation.rs +++ b/components/constellation/constellation.rs @@ -107,7 +107,6 @@ use canvas::canvas_paint_thread::CanvasPaintThread; use canvas::webgl_thread::WebGLThreads; use canvas_traits::canvas::CanvasId; use canvas_traits::canvas::CanvasMsg; -use clipboard::{ClipboardContext, ClipboardProvider}; use compositing::compositor_thread::CompositorProxy; use compositing::compositor_thread::Msg as ToCompositorMsg; use compositing::SendableFrameTree; diff --git a/ports/glutin/browser.rs b/ports/glutin/browser.rs index bde2b6b80f4..042f84072c7 100644 --- a/ports/glutin/browser.rs +++ b/ports/glutin/browser.rs @@ -354,7 +354,6 @@ where self.handle_key_from_servo(browser_id, key_event); }, EmbedderMsg::GetClipboardContents(sender) => { - println!("glutin/browser.rs GetClipboardContents {:?} ", browser_id); let contents = match self.clipboard_ctx { Some(ref mut ctx) => { match ctx.get_contents() { @@ -372,8 +371,7 @@ where } } EmbedderMsg::SetClipboardContents(text) => { - println!("browser.rs SetClipBoardContents {} {:?}", text, browser_id); - if let Some( ref mut ctx ) = self.clipboard_ctx { + if let Some(ref mut ctx) = self.clipboard_ctx { if let Err(e) = ctx.set_contents(text) { warn!("Error setting clipboard contents ({})", e); } diff --git a/ports/glutin/main2.rs b/ports/glutin/main2.rs index 0e208fabb3c..8f3a1b4fda0 100644 --- a/ports/glutin/main2.rs +++ b/ports/glutin/main2.rs @@ -2,6 +2,7 @@ * 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/. */ +extern crate clipboard; #[macro_use] extern crate lazy_static; #[macro_use] @@ -9,7 +10,6 @@ extern crate log; #[cfg(all(feature = "unstable", any(target_os = "macos", target_os = "linux")))] #[macro_use] extern crate sig; -extern crate clipboard; mod app; mod browser; From 884b54a626213f21085f8b913e052b49f8c5fd54 Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Sat, 15 Jun 2019 11:57:32 +0200 Subject: [PATCH 06/14] Remove unneeded clipboard import. --- ports/glutin/main2.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/ports/glutin/main2.rs b/ports/glutin/main2.rs index 8f3a1b4fda0..496c0515e19 100644 --- a/ports/glutin/main2.rs +++ b/ports/glutin/main2.rs @@ -2,7 +2,6 @@ * 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/. */ -extern crate clipboard; #[macro_use] extern crate lazy_static; #[macro_use] From 2726fc1dea50b4ef8b4f36e95a666fac7eab1ef9 Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Sat, 15 Jun 2019 12:05:35 +0200 Subject: [PATCH 07/14] Restore DummyClipboardContext for textinput.rs tests --- tests/unit/script/textinput.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/unit/script/textinput.rs b/tests/unit/script/textinput.rs index aa4bc51b206..001938134c8 100644 --- a/tests/unit/script/textinput.rs +++ b/tests/unit/script/textinput.rs @@ -8,13 +8,34 @@ // except according to those terms. use keyboard_types::{Key, Modifiers}; -use script::clipboard_provider::DummyClipboardContext; +use script::clipboard_provider::ClipboardProvider; use script::test::DOMString; use script::textinput::{ Direction, Lines, Selection, SelectionDirection, TextInput, TextPoint, UTF16CodeUnits, UTF8Bytes, }; +pub struct DummyClipboardContext { + content: String, +} + +impl DummyClipboardContext { + pub fn new(s: &str) -> DummyClipboardContext { + DummyClipboardContext { + content: s.to_owned(), + } + } +} + +impl ClipboardProvider for DummyClipboardContext { + fn clipboard_contents(&mut self) -> String { + self.content.clone() + } + fn set_clipboard_contents(&mut self, s: String) { + self.content = s; + } +} + fn text_input(lines: Lines, s: &str) -> TextInput { TextInput::new( lines, From 588cec0422d74e60dacb037b78d17b8e17bf4ec3 Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Sat, 15 Jun 2019 13:38:38 +0200 Subject: [PATCH 08/14] Clipboard handling in libsimpleservo --- Cargo.lock | 1 + ports/libsimpleservo/api/Cargo.toml | 1 + ports/libsimpleservo/api/src/lib.rs | 32 +++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index bd223b40a86..067de976919 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4311,6 +4311,7 @@ source = "git+https://github.com/pcwalton/signpost.git#7ed712507f343c38646b9d1fe name = "simpleservo" version = "0.0.1" dependencies = [ + "clipboard 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "core-foundation 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "gl_generator 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ports/libsimpleservo/api/Cargo.toml b/ports/libsimpleservo/api/Cargo.toml index 0556947d7da..32ae1accd7c 100644 --- a/ports/libsimpleservo/api/Cargo.toml +++ b/ports/libsimpleservo/api/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" publish = false [dependencies] +clipboard = "0.5" libservo = { path = "../../../components/servo" } log = "0.4" diff --git a/ports/libsimpleservo/api/src/lib.rs b/ports/libsimpleservo/api/src/lib.rs index baafef8aa63..dd94167586c 100644 --- a/ports/libsimpleservo/api/src/lib.rs +++ b/ports/libsimpleservo/api/src/lib.rs @@ -9,6 +9,7 @@ pub mod gl_glue; pub use servo::script_traits::MouseButton; +use clipboard::{ClipboardContext, ClipboardProvider}; use servo::compositing::windowing::{ AnimationState, EmbedderCoordinates, EmbedderMethods, MouseWindowEvent, WindowEvent, WindowMethods, @@ -25,6 +26,7 @@ use servo::servo_url::ServoUrl; use servo::webrender_api::{DevicePixel, FramebufferPixel, ScrollLocation}; use servo::webvr::{VRExternalShmemPtr, VRMainThreadHeartbeat, VRServiceManager}; use servo::{self, gl, BrowserId, Servo}; + use std::cell::RefCell; use std::mem; use std::os::raw::c_void; @@ -126,6 +128,7 @@ pub struct ServoGlue { browsers: Vec, events: Vec, current_url: Option, + clipboard_ctx: Option, } pub fn servo_version() -> String { @@ -192,6 +195,13 @@ pub fn init( browsers: vec![], events: vec![], current_url: Some(url.clone()), + clipboard_ctx: match ClipboardContext::new() { + Ok(c) => Some(c), + Err(e) => { + warn!("Error creating clipboard context ({})", e); + None + }, + }, }; let browser_id = BrowserId::new(); let _ = servo_glue.process_event(WindowEvent::NewBrowser(url, browser_id)); @@ -516,6 +526,28 @@ impl ServoGlue { } self.events.push(WindowEvent::SelectBrowser(new_browser_id)); }, + EmbedderMsg::GetClipboardContents(sender) => { + let contents = match self.clipboard_ctx { + Some(ref mut ctx) => match ctx.get_contents() { + Ok(c) => c, + Err(e) => { + warn!("Error getting clipboard contents ({}), defaulting to empty string", e); + "".to_owned() + }, + }, + None => "".to_owned(), + }; + if let Err(e) = sender.send(contents) { + warn!("Failed to send clipboard ({})", e); + } + }, + EmbedderMsg::SetClipboardContents(text) => { + if let Some(ref mut ctx) = self.clipboard_ctx { + if let Err(e) = ctx.set_contents(text) { + warn!("Error setting clipboard contents ({})", e); + } + } + }, EmbedderMsg::CloseBrowser => { // TODO: close the appropriate "tab". let _ = self.browsers.pop(); From caaa2a425c6502799d90f9e3479c36fffa13424f Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Mon, 24 Jun 2019 22:51:43 +0200 Subject: [PATCH 09/14] Add HostTrait methods get_clipboard_contents set_clipboard_contents, and call them instead of directly handlind clipboard --- Cargo.lock | 1 - ports/libsimpleservo/api/Cargo.toml | 1 - ports/libsimpleservo/api/src/lib.rs | 34 ++++++---------------------- ports/libsimpleservo/capi/src/lib.rs | 10 ++++++++ 4 files changed, 17 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 067de976919..bd223b40a86 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4311,7 +4311,6 @@ source = "git+https://github.com/pcwalton/signpost.git#7ed712507f343c38646b9d1fe name = "simpleservo" version = "0.0.1" dependencies = [ - "clipboard 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "core-foundation 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "gl_generator 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ports/libsimpleservo/api/Cargo.toml b/ports/libsimpleservo/api/Cargo.toml index 32ae1accd7c..0556947d7da 100644 --- a/ports/libsimpleservo/api/Cargo.toml +++ b/ports/libsimpleservo/api/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" publish = false [dependencies] -clipboard = "0.5" libservo = { path = "../../../components/servo" } log = "0.4" diff --git a/ports/libsimpleservo/api/src/lib.rs b/ports/libsimpleservo/api/src/lib.rs index dd94167586c..1e92baf9670 100644 --- a/ports/libsimpleservo/api/src/lib.rs +++ b/ports/libsimpleservo/api/src/lib.rs @@ -9,7 +9,6 @@ pub mod gl_glue; pub use servo::script_traits::MouseButton; -use clipboard::{ClipboardContext, ClipboardProvider}; use servo::compositing::windowing::{ AnimationState, EmbedderCoordinates, EmbedderMethods, MouseWindowEvent, WindowEvent, WindowMethods, @@ -112,6 +111,10 @@ pub trait HostTrait { fn on_shutdown_complete(&self); /// A text input is focused. fn on_ime_state_changed(&self, show: bool); + /// Gets sytem clipboard contents + fn get_clipboard_contents(&self) -> Option; + /// Sets system clipboard contents + fn set_clipboard_contents(&self, contents: String); } pub struct ServoGlue { @@ -128,7 +131,6 @@ pub struct ServoGlue { browsers: Vec, events: Vec, current_url: Option, - clipboard_ctx: Option, } pub fn servo_version() -> String { @@ -195,13 +197,6 @@ pub fn init( browsers: vec![], events: vec![], current_url: Some(url.clone()), - clipboard_ctx: match ClipboardContext::new() { - Ok(c) => Some(c), - Err(e) => { - warn!("Error creating clipboard context ({})", e); - None - }, - }, }; let browser_id = BrowserId::new(); let _ = servo_glue.process_event(WindowEvent::NewBrowser(url, browser_id)); @@ -527,26 +522,11 @@ impl ServoGlue { self.events.push(WindowEvent::SelectBrowser(new_browser_id)); }, EmbedderMsg::GetClipboardContents(sender) => { - let contents = match self.clipboard_ctx { - Some(ref mut ctx) => match ctx.get_contents() { - Ok(c) => c, - Err(e) => { - warn!("Error getting clipboard contents ({}), defaulting to empty string", e); - "".to_owned() - }, - }, - None => "".to_owned(), - }; - if let Err(e) = sender.send(contents) { - warn!("Failed to send clipboard ({})", e); - } + let contents = self.callbacks.host_callbacks.get_clipboard_contents(); + sender.send(contents.unwrap_or("".to_owned())); }, EmbedderMsg::SetClipboardContents(text) => { - if let Some(ref mut ctx) = self.clipboard_ctx { - if let Err(e) = ctx.set_contents(text) { - warn!("Error setting clipboard contents ({})", e); - } - } + self.callbacks.host_callbacks.set_clipboard_contents(text); }, EmbedderMsg::CloseBrowser => { // TODO: close the appropriate "tab". diff --git a/ports/libsimpleservo/capi/src/lib.rs b/ports/libsimpleservo/capi/src/lib.rs index 50af7a4d284..a8c64606287 100644 --- a/ports/libsimpleservo/capi/src/lib.rs +++ b/ports/libsimpleservo/capi/src/lib.rs @@ -43,6 +43,8 @@ pub struct CHostCallbacks { pub on_animating_changed: extern "C" fn(animating: bool), pub on_shutdown_complete: extern "C" fn(), pub on_ime_state_changed: extern "C" fn(show: bool), + pub get_clipboard_contents: extern "C" fn() -> *const c_char, + pub set_clipboard_contents: extern "C" fn(contents: *const c_char), } /// Servo options @@ -347,4 +349,12 @@ impl HostTrait for HostCallbacks { debug!("on_ime_state_changed"); (self.0.on_ime_state_changed)(show); } + + fn get_clipboard_contents(&self) -> Option { + unimplemented!() + } + + fn set_clipboard_contents(&self, contents: String) { + unimplemented!() + } } From 02d55885c840ab5b19d15b4830d5811437d1ad02 Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Mon, 1 Jul 2019 19:12:59 +0200 Subject: [PATCH 10/14] Handle get_clipboard_contents and set_clipboard_contents in libsimpleservo capi --- ports/libsimpleservo/capi/src/lib.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/ports/libsimpleservo/capi/src/lib.rs b/ports/libsimpleservo/capi/src/lib.rs index a8c64606287..a403edae3f5 100644 --- a/ports/libsimpleservo/capi/src/lib.rs +++ b/ports/libsimpleservo/capi/src/lib.rs @@ -351,10 +351,21 @@ impl HostTrait for HostCallbacks { } fn get_clipboard_contents(&self) -> Option { - unimplemented!() + debug!("get_clipboard_contents"); + let raw_contents = (self.0.get_clipboard_contents)(); + if raw_contents.is_null() { + return None; + } + let c_str = unsafe { CStr::from_ptr(raw_contents) }; + let contents_str = c_str.to_str().expect("Can't create str"); + Some(contents_str.to_owned()) } fn set_clipboard_contents(&self, contents: String) { - unimplemented!() + debug!("set_clipboard_contents"); + let contents = CString::new(contents).expect("Can't create string"); + let contents_ptr = contents.as_ptr(); + mem::forget(contents); + (self.0.set_clipboard_contents)(contents_ptr); } } From 8da643d998cc497338b6e6d8a5bc40a07881e691 Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Mon, 1 Jul 2019 19:16:21 +0200 Subject: [PATCH 11/14] Add stub for set_clipboard_contents and get_clipboard_contents in libsimpleservo jniapi and libmlservo --- ports/libmlservo/src/lib.rs | 6 ++++++ ports/libsimpleservo/jniapi/src/lib.rs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/ports/libmlservo/src/lib.rs b/ports/libmlservo/src/lib.rs index 271d1c748d3..349a7323615 100644 --- a/ports/libmlservo/src/lib.rs +++ b/ports/libmlservo/src/lib.rs @@ -354,6 +354,12 @@ impl HostTrait for HostCallbacks { fn on_ime_state_changed(&self, show: bool) { (self.keyboard.0)(self.app, show) } + + fn get_clipboard_contents(&self) -> Option { + unimplemented!() + } + + fn set_clipboard_contents(&self, contents: String) {} } pub struct ServoInstance { diff --git a/ports/libsimpleservo/jniapi/src/lib.rs b/ports/libsimpleservo/jniapi/src/lib.rs index d4a88b44421..fef7ee9f2b7 100644 --- a/ports/libsimpleservo/jniapi/src/lib.rs +++ b/ports/libsimpleservo/jniapi/src/lib.rs @@ -457,6 +457,12 @@ impl HostTrait for HostCallbacks { } fn on_ime_state_changed(&self, _show: bool) {} + + fn get_clipboard_contents(&self) -> Option { + unimplemented!() + } + + fn set_clipboard_contents(&self, contents: String) {} } fn initialize_android_glue(env: &JNIEnv, activity: JObject) { From 1bbe92ff760ff21cf1841a96cd687c69dd5d421f Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Tue, 2 Jul 2019 21:29:45 +0200 Subject: [PATCH 12/14] Return None instead of panicking --- ports/libmlservo/src/lib.rs | 2 +- ports/libsimpleservo/jniapi/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/libmlservo/src/lib.rs b/ports/libmlservo/src/lib.rs index 349a7323615..c8b0aa32e8e 100644 --- a/ports/libmlservo/src/lib.rs +++ b/ports/libmlservo/src/lib.rs @@ -356,7 +356,7 @@ impl HostTrait for HostCallbacks { } fn get_clipboard_contents(&self) -> Option { - unimplemented!() + None } fn set_clipboard_contents(&self, contents: String) {} diff --git a/ports/libsimpleservo/jniapi/src/lib.rs b/ports/libsimpleservo/jniapi/src/lib.rs index fef7ee9f2b7..0df31c2ce85 100644 --- a/ports/libsimpleservo/jniapi/src/lib.rs +++ b/ports/libsimpleservo/jniapi/src/lib.rs @@ -459,7 +459,7 @@ impl HostTrait for HostCallbacks { fn on_ime_state_changed(&self, _show: bool) {} fn get_clipboard_contents(&self) -> Option { - unimplemented!() + None } fn set_clipboard_contents(&self, contents: String) {} From 775ff3b6235922e1bec15f28aed77fc1c7e9abdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Mieczkowski?= Date: Wed, 3 Jul 2019 10:47:37 +0200 Subject: [PATCH 13/14] Fix warning about unused result --- ports/libsimpleservo/api/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/libsimpleservo/api/src/lib.rs b/ports/libsimpleservo/api/src/lib.rs index 1e92baf9670..9e9f3ffe376 100644 --- a/ports/libsimpleservo/api/src/lib.rs +++ b/ports/libsimpleservo/api/src/lib.rs @@ -523,7 +523,7 @@ impl ServoGlue { }, EmbedderMsg::GetClipboardContents(sender) => { let contents = self.callbacks.host_callbacks.get_clipboard_contents(); - sender.send(contents.unwrap_or("".to_owned())); + let _ = sender.send(contents.unwrap_or("".to_owned())); }, EmbedderMsg::SetClipboardContents(text) => { self.callbacks.host_callbacks.set_clipboard_contents(text); From 7fb2bbed002885fb38723f5a7c75524da7fbab0b Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Wed, 3 Jul 2019 20:31:20 +0200 Subject: [PATCH 14/14] Prefix unused arguments with underscore to prevent warnings --- ports/libmlservo/src/lib.rs | 2 +- ports/libsimpleservo/jniapi/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/libmlservo/src/lib.rs b/ports/libmlservo/src/lib.rs index c8b0aa32e8e..eab8dc82f23 100644 --- a/ports/libmlservo/src/lib.rs +++ b/ports/libmlservo/src/lib.rs @@ -359,7 +359,7 @@ impl HostTrait for HostCallbacks { None } - fn set_clipboard_contents(&self, contents: String) {} + fn set_clipboard_contents(&self, _contents: String) {} } pub struct ServoInstance { diff --git a/ports/libsimpleservo/jniapi/src/lib.rs b/ports/libsimpleservo/jniapi/src/lib.rs index 0df31c2ce85..6949457d764 100644 --- a/ports/libsimpleservo/jniapi/src/lib.rs +++ b/ports/libsimpleservo/jniapi/src/lib.rs @@ -462,7 +462,7 @@ impl HostTrait for HostCallbacks { None } - fn set_clipboard_contents(&self, contents: String) {} + fn set_clipboard_contents(&self, _contents: String) {} } fn initialize_android_glue(env: &JNIEnv, activity: JObject) {