pub mod resources;
use std::fmt::{Debug, Error, Formatter};
use base::id::{PipelineId, TopLevelBrowsingContextId, WebViewId};
use crossbeam_channel::{Receiver, Sender};
use ipc_channel::ipc::IpcSender;
use keyboard_types::KeyboardEvent;
use log::warn;
use num_derive::FromPrimitive;
use serde::{Deserialize, Serialize};
use servo_url::ServoUrl;
use webrender_api::units::{DeviceIntPoint, DeviceIntRect, DeviceIntSize};
#[repr(u8)]
#[derive(Clone, Copy, Debug, Deserialize, Eq, FromPrimitive, PartialEq, Serialize)]
pub enum Cursor {
None,
Default,
Pointer,
ContextMenu,
Help,
Progress,
Wait,
Cell,
Crosshair,
Text,
VerticalText,
Alias,
Copy,
Move,
NoDrop,
NotAllowed,
Grab,
Grabbing,
EResize,
NResize,
NeResize,
NwResize,
SResize,
SeResize,
SwResize,
WResize,
EwResize,
NsResize,
NeswResize,
NwseResize,
ColResize,
RowResize,
AllScroll,
ZoomIn,
ZoomOut,
}
#[cfg(feature = "webxr")]
pub use webxr_api::MainThreadWaker as EventLoopWaker;
#[cfg(not(feature = "webxr"))]
pub trait EventLoopWaker: 'static + Send {
fn clone_box(&self) -> Box<dyn EventLoopWaker>;
fn wake(&self);
}
#[cfg(not(feature = "webxr"))]
impl Clone for Box<dyn EventLoopWaker> {
fn clone(&self) -> Self {
EventLoopWaker::clone_box(self.as_ref())
}
}
pub struct EmbedderProxy {
pub sender: Sender<(Option<TopLevelBrowsingContextId>, EmbedderMsg)>,
pub event_loop_waker: Box<dyn EventLoopWaker>,
}
impl EmbedderProxy {
pub fn send(&self, msg: (Option<TopLevelBrowsingContextId>, EmbedderMsg)) {
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(),
}
}
}
pub struct EmbedderReceiver {
pub receiver: Receiver<(Option<TopLevelBrowsingContextId>, EmbedderMsg)>,
}
impl EmbedderReceiver {
pub fn try_recv_embedder_msg(
&mut self,
) -> Option<(Option<TopLevelBrowsingContextId>, EmbedderMsg)> {
self.receiver.try_recv().ok()
}
pub fn recv_embedder_msg(&mut self) -> (Option<TopLevelBrowsingContextId>, EmbedderMsg) {
self.receiver.recv().unwrap()
}
}
#[derive(Deserialize, Serialize)]
pub enum ContextMenuResult {
Dismissed,
Ignored,
Selected(usize),
}
#[derive(Deserialize, Serialize)]
pub enum PromptDefinition {
Alert(String, IpcSender<()>),
OkCancel(String, IpcSender<PromptResult>),
YesNo(String, IpcSender<PromptResult>),
Input(String, String, IpcSender<Option<String>>),
}
#[derive(Deserialize, PartialEq, Serialize)]
pub enum PromptOrigin {
Untrusted,
Trusted,
}
#[derive(Deserialize, PartialEq, Serialize)]
pub enum PromptResult {
Primary,
Secondary,
Dismissed,
}
#[derive(Deserialize, Serialize)]
pub enum EmbedderMsg {
Status(Option<String>),
ChangePageTitle(Option<String>),
MoveTo(DeviceIntPoint),
ResizeTo(DeviceIntSize),
Prompt(PromptDefinition, PromptOrigin),
ShowContextMenu(IpcSender<ContextMenuResult>, Option<String>, Vec<String>),
AllowNavigationRequest(PipelineId, ServoUrl),
AllowOpeningWebView(IpcSender<bool>),
WebViewOpened(TopLevelBrowsingContextId),
WebViewClosed(TopLevelBrowsingContextId),
WebViewFocused(TopLevelBrowsingContextId),
WebViewBlurred,
AllowUnload(IpcSender<bool>),
Keyboard(KeyboardEvent),
GetClipboardContents(IpcSender<String>),
SetClipboardContents(String),
SetCursor(Cursor),
NewFavicon(ServoUrl),
HeadParsed,
HistoryChanged(Vec<ServoUrl>, usize),
SetFullscreenState(bool),
LoadStart,
LoadComplete,
Panic(String, Option<String>),
GetSelectedBluetoothDevice(Vec<String>, IpcSender<Option<String>>),
SelectFiles(Vec<FilterPattern>, bool, IpcSender<Option<Vec<String>>>),
PromptPermission(PermissionPrompt, IpcSender<PermissionRequest>),
ShowIME(InputMethodType, Option<(String, i32)>, bool, DeviceIntRect),
HideIME,
Shutdown,
ReportProfile(Vec<u8>),
MediaSessionEvent(MediaSessionEvent),
OnDevtoolsStarted(Result<u16, ()>, String),
ReadyToPresent(Vec<WebViewId>),
EventDelivered(CompositorEventVariant),
PlayGamepadHapticEffect(usize, GamepadHapticEffectType, IpcSender<bool>),
StopGamepadHapticEffect(usize, IpcSender<bool>),
}
#[derive(Debug, Deserialize, Serialize)]
pub enum CompositorEventVariant {
ResizeEvent,
MouseButtonEvent,
MouseMoveEvent,
TouchEvent,
WheelEvent,
KeyboardEvent,
CompositionEvent,
IMEDismissedEvent,
GamepadEvent,
}
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::Prompt(..) => write!(f, "Prompt"),
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"),
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::PromptPermission(..) => write!(f, "PromptPermission"),
EmbedderMsg::ShowIME(..) => write!(f, "ShowIME"),
EmbedderMsg::HideIME => write!(f, "HideIME"),
EmbedderMsg::Shutdown => write!(f, "Shutdown"),
EmbedderMsg::AllowOpeningWebView(..) => write!(f, "AllowOpeningWebView"),
EmbedderMsg::WebViewOpened(..) => write!(f, "WebViewOpened"),
EmbedderMsg::WebViewClosed(..) => write!(f, "WebViewClosed"),
EmbedderMsg::WebViewFocused(..) => write!(f, "WebViewFocused"),
EmbedderMsg::WebViewBlurred => write!(f, "WebViewBlurred"),
EmbedderMsg::ReportProfile(..) => write!(f, "ReportProfile"),
EmbedderMsg::MediaSessionEvent(..) => write!(f, "MediaSessionEvent"),
EmbedderMsg::OnDevtoolsStarted(..) => write!(f, "OnDevtoolsStarted"),
EmbedderMsg::ShowContextMenu(..) => write!(f, "ShowContextMenu"),
EmbedderMsg::ReadyToPresent(..) => write!(f, "ReadyToPresent"),
EmbedderMsg::EventDelivered(..) => write!(f, "HitTestedEvent"),
EmbedderMsg::PlayGamepadHapticEffect(..) => write!(f, "PlayGamepadHapticEffect"),
EmbedderMsg::StopGamepadHapticEffect(..) => write!(f, "StopGamepadHapticEffect"),
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct FilterPattern(pub String);
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MediaMetadata {
pub title: String,
pub artist: String,
pub album: String,
}
impl MediaMetadata {
pub fn new(title: String) -> Self {
Self {
title,
artist: "".to_owned(),
album: "".to_owned(),
}
}
}
#[repr(i32)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum MediaSessionPlaybackState {
None_ = 1,
Playing,
Paused,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MediaPositionState {
pub duration: f64,
pub playback_rate: f64,
pub position: f64,
}
impl MediaPositionState {
pub fn new(duration: f64, playback_rate: f64, position: f64) -> Self {
Self {
duration,
playback_rate,
position,
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum MediaSessionEvent {
SetMetadata(MediaMetadata),
PlaybackStateChange(MediaSessionPlaybackState),
SetPositionState(MediaPositionState),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum PermissionName {
Geolocation,
Notifications,
Push,
Midi,
Camera,
Microphone,
Speaker,
DeviceInfo,
BackgroundSync,
Bluetooth,
PersistentStorage,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum PermissionPrompt {
Insecure(PermissionName),
Request(PermissionName),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum PermissionRequest {
Granted,
Denied,
}
#[derive(Debug, Deserialize, Serialize)]
pub enum InputMethodType {
Color,
Date,
DatetimeLocal,
Email,
Month,
Number,
Password,
Search,
Tel,
Text,
Time,
Url,
Week,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DualRumbleEffectParams {
pub duration: f64,
pub start_delay: f64,
pub strong_magnitude: f64,
pub weak_magnitude: f64,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum GamepadHapticEffectType {
DualRumble(DualRumbleEffectParams),
}