aboutsummaryrefslogtreecommitdiffstats
path: root/components/embedder_traits/lib.rs
diff options
context:
space:
mode:
authorGregory Terzian <gterzian@users.noreply.github.com>2018-04-28 22:48:14 +0800
committerGregory Terzian <gterzian@users.noreply.github.com>2018-05-23 21:45:57 +0800
commitd4382407722108a52bf23b1f3a3114984f13fb90 (patch)
tree55f5dad9ccad14e6911cb5434f2d362ba79f166e /components/embedder_traits/lib.rs
parenta297e8f2881d0d1927160d2ebc0e4b26d71f2534 (diff)
downloadservo-d4382407722108a52bf23b1f3a3114984f13fb90.tar.gz
servo-d4382407722108a52bf23b1f3a3114984f13fb90.zip
move msg to embedder_traits, use in script, handle send error in embedder
Diffstat (limited to 'components/embedder_traits/lib.rs')
-rw-r--r--components/embedder_traits/lib.rs142
1 files changed, 141 insertions, 1 deletions
diff --git a/components/embedder_traits/lib.rs b/components/embedder_traits/lib.rs
index aa8a3c0e111..6650c553946 100644
--- a/components/embedder_traits/lib.rs
+++ b/components/embedder_traits/lib.rs
@@ -2,6 +2,146 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-#[macro_use] extern crate lazy_static;
+extern crate ipc_channel;
+#[macro_use]
+extern crate lazy_static;
+#[macro_use]
+extern crate log;
+extern crate msg;
+#[macro_use]
+extern crate serde;
+extern crate servo_url;
+extern crate style_traits;
+extern crate webrender_api;
pub mod resources;
+
+use ipc_channel::ipc::IpcSender;
+use msg::constellation_msg::{InputMethodType, Key, KeyModifiers, KeyState, TopLevelBrowsingContextId};
+use servo_url::ServoUrl;
+use std::fmt::{Debug, Error, Formatter};
+use std::sync::mpsc::{Receiver, Sender};
+use style_traits::cursor::CursorKind;
+use webrender_api::{DeviceIntPoint, DeviceUintSize};
+
+
+/// Used to wake up the event loop, provided by the servo port/embedder.
+pub trait EventLoopWaker : 'static + Send {
+ fn clone(&self) -> Box<EventLoopWaker + Send>;
+ fn wake(&self);
+}
+
+/// Sends messages to the embedder.
+pub struct EmbedderProxy {
+ pub sender: Sender<EmbedderMsg>,
+ pub event_loop_waker: Box<EventLoopWaker>,
+}
+
+impl EmbedderProxy {
+ pub fn send(&self, msg: EmbedderMsg) {
+ // Send a message and kick the OS event loop awake.
+ if let Err(err) = self.sender.send(msg) {
+ warn!("Failed to send response ({}).", err);
+ }
+ self.event_loop_waker.wake();
+ }
+}
+
+impl Clone for EmbedderProxy {
+ fn clone(&self) -> EmbedderProxy {
+ EmbedderProxy {
+ sender: self.sender.clone(),
+ event_loop_waker: self.event_loop_waker.clone(),
+ }
+ }
+}
+
+/// The port that the embedder receives messages on.
+pub struct EmbedderReceiver {
+ pub receiver: Receiver<EmbedderMsg>
+}
+
+impl EmbedderReceiver {
+ pub fn try_recv_embedder_msg(&mut self) -> Option<EmbedderMsg> {
+ self.receiver.try_recv().ok()
+ }
+ pub fn recv_embedder_msg(&mut self) -> EmbedderMsg {
+ self.receiver.recv().unwrap()
+ }
+}
+
+#[derive(Deserialize, Serialize)]
+pub enum EmbedderMsg {
+ /// A status message to be displayed by the browser chrome.
+ Status(TopLevelBrowsingContextId, Option<String>),
+ /// Alerts the embedder that the current page has changed its title.
+ ChangePageTitle(TopLevelBrowsingContextId, Option<String>),
+ /// Move the window to a point
+ MoveTo(TopLevelBrowsingContextId, DeviceIntPoint),
+ /// Resize the window to size
+ ResizeTo(TopLevelBrowsingContextId, DeviceUintSize),
+ // Show an alert message.
+ Alert(TopLevelBrowsingContextId, String, IpcSender<()>),
+ /// Wether or not to follow a link
+ AllowNavigation(TopLevelBrowsingContextId, ServoUrl, IpcSender<bool>),
+ /// Sends an unconsumed key event back to the embedder.
+ KeyEvent(Option<TopLevelBrowsingContextId>, Option<char>, Key, KeyState, KeyModifiers),
+ /// Changes the cursor.
+ SetCursor(CursorKind),
+ /// A favicon was detected
+ NewFavicon(TopLevelBrowsingContextId, ServoUrl),
+ /// <head> tag finished parsing
+ HeadParsed(TopLevelBrowsingContextId),
+ /// The history state has changed.
+ HistoryChanged(TopLevelBrowsingContextId, Vec<ServoUrl>, usize),
+ /// Enter or exit fullscreen
+ SetFullscreenState(TopLevelBrowsingContextId, bool),
+ /// The load of a page has begun
+ LoadStart(TopLevelBrowsingContextId),
+ /// The load of a page has completed
+ LoadComplete(TopLevelBrowsingContextId),
+ /// A pipeline panicked. First string is the reason, second one is the backtrace.
+ Panic(TopLevelBrowsingContextId, String, Option<String>),
+ /// Open dialog to select bluetooth device.
+ GetSelectedBluetoothDevice(Vec<String>, IpcSender<Option<String>>),
+ /// Open file dialog to select files. Set boolean flag to true allows to select multiple files.
+ SelectFiles(Vec<FilterPattern>, bool, IpcSender<Option<Vec<String>>>),
+ /// Request to present an IME to the user when an editable element is focused.
+ ShowIME(TopLevelBrowsingContextId, InputMethodType),
+ /// Request to hide the IME when the editable element is blurred.
+ HideIME(TopLevelBrowsingContextId),
+ /// Servo has shut down
+ Shutdown,
+}
+
+impl Debug for EmbedderMsg {
+ fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
+ match *self {
+ EmbedderMsg::Status(..) => write!(f, "Status"),
+ EmbedderMsg::ChangePageTitle(..) => write!(f, "ChangePageTitle"),
+ EmbedderMsg::MoveTo(..) => write!(f, "MoveTo"),
+ EmbedderMsg::ResizeTo(..) => write!(f, "ResizeTo"),
+ EmbedderMsg::Alert(..) => write!(f, "Alert"),
+ EmbedderMsg::AllowNavigation(..) => write!(f, "AllowNavigation"),
+ EmbedderMsg::KeyEvent(..) => write!(f, "KeyEvent"),
+ EmbedderMsg::SetCursor(..) => write!(f, "SetCursor"),
+ EmbedderMsg::NewFavicon(..) => write!(f, "NewFavicon"),
+ EmbedderMsg::HeadParsed(..) => write!(f, "HeadParsed"),
+ EmbedderMsg::HistoryChanged(..) => write!(f, "HistoryChanged"),
+ EmbedderMsg::SetFullscreenState(..) => write!(f, "SetFullscreenState"),
+ EmbedderMsg::LoadStart(..) => write!(f, "LoadStart"),
+ EmbedderMsg::LoadComplete(..) => write!(f, "LoadComplete"),
+ EmbedderMsg::Panic(..) => write!(f, "Panic"),
+ EmbedderMsg::GetSelectedBluetoothDevice(..) => write!(f, "GetSelectedBluetoothDevice"),
+ EmbedderMsg::SelectFiles(..) => write!(f, "SelectFiles"),
+ EmbedderMsg::ShowIME(..) => write!(f, "ShowIME"),
+ EmbedderMsg::HideIME(..) => write!(f, "HideIME"),
+ EmbedderMsg::Shutdown => write!(f, "Shutdown"),
+ }
+ }
+}
+
+/// Filter for file selection;
+/// the `String` content is expected to be extension (e.g, "doc", without the prefixing ".")
+#[derive(Clone, Debug, Deserialize, Serialize)]
+pub struct FilterPattern(pub String);