move msg to embedder_traits, use in script, handle send error in embedder

This commit is contained in:
Gregory Terzian 2018-04-28 22:48:14 +08:00
parent a297e8f288
commit d438240772
31 changed files with 362 additions and 337 deletions

View file

@ -88,6 +88,7 @@ use dom::webglcontextevent::WebGLContextEvent;
use dom::window::{ReflowReason, Window};
use dom::windowproxy::WindowProxy;
use dom_struct::dom_struct;
use embedder_traits::EmbedderMsg;
use encoding_rs::{Encoding, UTF_8};
use euclid::Point2D;
use fetch::FetchCanceller;
@ -855,7 +856,8 @@ impl Document {
// Notify the embedder to hide the input method.
if elem.input_method_type().is_some() {
self.send_to_constellation(ScriptMsg::HideIME);
let top_level_browsing_context_id = self.window().top_level_browsing_context_id();
self.send_to_embedder(EmbedderMsg::HideIME(top_level_browsing_context_id));
}
}
@ -869,12 +871,13 @@ impl Document {
// Update the focus state for all elements in the focus chain.
// https://html.spec.whatwg.org/multipage/#focus-chain
if focus_type == FocusType::Element {
self.send_to_constellation(ScriptMsg::Focus);
self.window().send_to_constellation(ScriptMsg::Focus);
}
// Notify the embedder to display an input method.
if let Some(kind) = elem.input_method_type() {
self.send_to_constellation(ScriptMsg::ShowIME(kind));
let top_level_browsing_context_id = self.window().top_level_browsing_context_id();
self.send_to_embedder(EmbedderMsg::ShowIME(top_level_browsing_context_id, kind));
}
}
}
@ -882,14 +885,23 @@ impl Document {
/// Handles any updates when the document's title has changed.
pub fn title_changed(&self) {
if self.browsing_context().is_some() {
self.send_title_to_constellation();
self.send_title_to_embedder();
}
}
/// Sends this document's title to the constellation.
pub fn send_title_to_constellation(&self) {
let title = Some(String::from(self.Title()));
self.send_to_constellation(ScriptMsg::SetTitle(title));
pub fn send_title_to_embedder(&self) {
let window = self.window();
if window.is_top_level() {
let title = Some(String::from(self.Title()));
let top_level_browsing_context_id = window.top_level_browsing_context_id();
self.send_to_embedder(EmbedderMsg::ChangePageTitle(top_level_browsing_context_id, title));
}
}
fn send_to_embedder(&self, msg: EmbedderMsg) {
let window = self.window();
window.send_to_embedder(msg);
}
pub fn dirty_all_nodes(&self) {
@ -1352,8 +1364,9 @@ impl Document {
}
if cancel_state == EventDefault::Allowed {
let msg = ScriptMsg::SendKeyEvent(ch, key, state, modifiers);
self.send_to_constellation(msg);
let top_level_browsing_context_id = self.window().top_level_browsing_context_id();
let msg = EmbedderMsg::KeyEvent(Some(top_level_browsing_context_id), ch, key, state, modifiers);
self.send_to_embedder(msg);
// This behavior is unspecced
// We are supposed to dispatch synthetic click activation for Space and/or Return,
@ -1488,7 +1501,7 @@ impl Document {
// repeated rAF.
let event = ScriptMsg::ChangeRunningAnimationsState(AnimationState::AnimationCallbacksPresent);
self.send_to_constellation(event);
self.window().send_to_constellation(event);
}
ident
@ -1559,7 +1572,7 @@ impl Document {
);
}
let event = ScriptMsg::ChangeRunningAnimationsState(AnimationState::NoAnimationCallbacksPresent);
self.send_to_constellation(event);
self.window().send_to_constellation(event);
}
// Update the counter of spurious animation frames.
@ -2009,7 +2022,7 @@ impl Document {
}
pub fn notify_constellation_load(&self) {
self.send_to_constellation(ScriptMsg::LoadComplete);
self.window().send_to_constellation(ScriptMsg::LoadComplete);
}
pub fn set_current_parser(&self, script: Option<&ServoParser>) {
@ -2167,11 +2180,6 @@ impl Document {
registry.lookup_definition(local_name, is)
}
fn send_to_constellation(&self, msg: ScriptMsg) {
let global_scope = self.window.upcast::<GlobalScope>();
global_scope.script_to_constellation_chan().send(msg).unwrap();
}
pub fn increment_throw_on_dynamic_markup_insertion_counter(&self) {
let counter = self.throw_on_dynamic_markup_insertion_counter.get();
self.throw_on_dynamic_markup_insertion_counter.set(counter + 1);
@ -2787,8 +2795,9 @@ impl Document {
let window = self.window();
// Step 6
if !error {
let event = ScriptMsg::SetFullscreenState(true);
self.send_to_constellation(event);
let top_level_browsing_context_id = self.window().top_level_browsing_context_id();
let event = EmbedderMsg::SetFullscreenState(top_level_browsing_context_id, true);
self.send_to_embedder(event);
}
let pipeline_id = self.window().pipeline_id();
@ -2822,8 +2831,9 @@ impl Document {
let window = self.window();
// Step 8
let event = ScriptMsg::SetFullscreenState(false);
self.send_to_constellation(event);
let top_level_browsing_context_id = self.window().top_level_browsing_context_id();
let event = EmbedderMsg::SetFullscreenState(top_level_browsing_context_id, true);
self.send_to_embedder(event);
// Step 9
let trusted_element = Trusted::new(element.r());

View file

@ -12,13 +12,12 @@ use dom::bindings::str::DOMString;
use dom::document::Document;
use dom::element::{AttributeMutation, Element, RawLayoutElementHelpers};
use dom::eventtarget::EventTarget;
use dom::globalscope::GlobalScope;
use dom::htmlelement::HTMLElement;
use dom::node::{Node, document_from_node, window_from_node};
use dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct;
use embedder_traits::EmbedderMsg;
use html5ever::{LocalName, Prefix};
use script_traits::ScriptMsg;
use servo_url::ServoUrl;
use style::attr::AttrValue;
use time;
@ -151,8 +150,11 @@ impl VirtualMethods for HTMLBodyElement {
let window = window_from_node(self);
let document = window.Document();
document.set_reflow_timeout(time::precise_time_ns() + INITIAL_REFLOW_DELAY);
let event = ScriptMsg::HeadParsed;
window.upcast::<GlobalScope>().script_to_constellation_chan().send(event).unwrap();
if window.is_top_level() {
let top_level_browsing_context_id = window.top_level_browsing_context_id();
let msg = EmbedderMsg::HeadParsed(top_level_browsing_context_id);
window.send_to_embedder(msg);
}
}
fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {

View file

@ -38,12 +38,13 @@ use dom::validation::Validatable;
use dom::validitystate::ValidationFlags;
use dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct;
use embedder_traits::FilterPattern;
use html5ever::{LocalName, Prefix};
use mime_guess;
use msg::constellation_msg::InputMethodType;
use net_traits::{CoreResourceMsg, IpcSend};
use net_traits::blob_url_store::get_blob_origin;
use net_traits::filemanager_thread::{FileManagerThreadMsg, FilterPattern};
use net_traits::filemanager_thread::FileManagerThreadMsg;
use profile_traits::ipc;
use script_layout_interface::rpc::TextIndexResponse;
use script_traits::ScriptToConstellationChan;

View file

@ -16,15 +16,14 @@ use dom::document::Document;
use dom::domtokenlist::DOMTokenList;
use dom::element::{AttributeMutation, Element, ElementCreator};
use dom::element::{cors_setting_for_element, reflect_cross_origin_attribute, set_cross_origin_attribute};
use dom::globalscope::GlobalScope;
use dom::htmlelement::HTMLElement;
use dom::node::{Node, UnbindContext, document_from_node, window_from_node};
use dom::stylesheet::StyleSheet as DOMStyleSheet;
use dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct;
use embedder_traits::EmbedderMsg;
use html5ever::{LocalName, Prefix};
use net_traits::ReferrerPolicy;
use script_traits::ScriptMsg;
use servo_arc::Arc;
use std::borrow::ToOwned;
use std::cell::Cell;
@ -306,8 +305,13 @@ impl HTMLLinkElement {
let document = document_from_node(self);
match document.base_url().join(href) {
Ok(url) => {
let event = ScriptMsg::NewFavicon(url.clone());
document.window().upcast::<GlobalScope>().script_to_constellation_chan().send(event).unwrap();
let window = document.window();
if window.is_top_level() {
let top_level_browsing_context_id = window.top_level_browsing_context_id();
let msg = EmbedderMsg::NewFavicon(top_level_browsing_context_id, url.clone());
window.send_to_embedder(msg);
}
}
Err(e) => debug!("Parsing url {} failed: {}", href, e)
}

View file

@ -48,6 +48,7 @@ use dom::windowproxy::WindowProxy;
use dom::worklet::Worklet;
use dom::workletglobalscope::WorkletGlobalScopeType;
use dom_struct::dom_struct;
use embedder_traits::EmbedderMsg;
use euclid::{Point2D, Vector2D, Rect, Size2D, TypedPoint2D, TypedScale, TypedSize2D};
use fetch;
use ipc_channel::ipc::IpcSender;
@ -58,7 +59,7 @@ use js::jsval::UndefinedValue;
use js::rust::HandleValue;
use layout_image::fetch_image_for_layout;
use microtask::MicrotaskQueue;
use msg::constellation_msg::PipelineId;
use msg::constellation_msg::{PipelineId, TopLevelBrowsingContextId};
use net_traits::{ResourceThreads, ReferrerPolicy};
use net_traits::image_cache::{ImageCache, ImageResponder, ImageResponse};
use net_traits::image_cache::{PendingImageId, PendingImageResponse};
@ -114,8 +115,6 @@ use task_source::performance_timeline::PerformanceTimelineTaskSource;
use task_source::user_interaction::UserInteractionTaskSource;
use time;
use timers::{IsInterval, TimerCallback};
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
use tinyfiledialogs::{self, MessageBoxIcon};
use url::Position;
use webdriver_handlers::jsval_to_webdriver;
use webrender_api::{ExternalScrollId, DeviceIntPoint, DeviceUintSize, DocumentId};
@ -356,6 +355,11 @@ impl Window {
self.parent_info
}
pub fn top_level_browsing_context_id(&self) -> TopLevelBrowsingContextId {
let window_proxy = self.window_proxy.get().unwrap();
window_proxy.top_level_browsing_context_id()
}
pub fn new_script_pair(&self) -> (Box<ScriptChan + Send>, Box<ScriptPort + Send>) {
let (tx, rx) = channel();
(Box::new(SendableMainThreadScriptChan(tx)), Box::new(rx))
@ -444,18 +448,6 @@ impl Window {
}
}
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
fn display_alert_dialog(message: &str) {
if !opts::get().headless {
tinyfiledialogs::message_box_ok("Alert!", message, MessageBoxIcon::Warning);
}
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
fn display_alert_dialog(_message: &str) {
// tinyfiledialogs not supported on Android
}
// https://html.spec.whatwg.org/multipage/#atob
pub fn base64_btoa(input: DOMString) -> Fallible<DOMString> {
// "The btoa() method must throw an InvalidCharacterError exception if
@ -541,14 +533,12 @@ impl WindowMethods for Window {
stdout.flush().unwrap();
stderr.flush().unwrap();
}
let (sender, receiver) = ProfiledIpc::channel(self.global().time_profiler_chan().clone()).unwrap();
self.send_to_constellation(ScriptMsg::Alert(s.to_string(), sender));
let should_display_alert_dialog = receiver.recv().unwrap();
if should_display_alert_dialog {
display_alert_dialog(&s);
}
let window_proxy = self.window_proxy.get().unwrap();
let top_level_browsing_context_id = window_proxy.top_level_browsing_context_id();
let msg = EmbedderMsg::Alert(top_level_browsing_context_id, s.to_string(), sender);
self.send_to_embedder(msg);
receiver.recv().unwrap();
}
// https://html.spec.whatwg.org/multipage/#dom-window-closed
@ -937,7 +927,8 @@ impl WindowMethods for Window {
//TODO determine if this operation is allowed
let dpr = self.device_pixel_ratio();
let size = TypedSize2D::new(width, height).to_f32() * dpr;
self.send_to_constellation(ScriptMsg::ResizeTo(size.to_u32()));
let top_level_browsing_context_id = self.top_level_browsing_context_id();
self.send_to_embedder(EmbedderMsg::ResizeTo(top_level_browsing_context_id, size.to_u32()));
}
// https://drafts.csswg.org/cssom-view/#dom-window-resizeby
@ -953,7 +944,10 @@ impl WindowMethods for Window {
//TODO determine if this operation is allowed
let dpr = self.device_pixel_ratio();
let point = TypedPoint2D::new(x, y).to_f32() * dpr;
self.send_to_constellation(ScriptMsg::MoveTo(point.to_i32()));
let window_proxy = self.window_proxy.get().unwrap();
let top_level_browsing_context_id = window_proxy.top_level_browsing_context_id();
let msg = EmbedderMsg::MoveTo(top_level_browsing_context_id, point.to_i32());
self.send_to_embedder(msg);
}
// https://drafts.csswg.org/cssom-view/#dom-window-moveby
@ -1742,7 +1736,11 @@ impl Window {
self.navigation_start_precise.set(time::precise_time_ns());
}
fn send_to_constellation(&self, msg: ScriptMsg) {
pub fn send_to_embedder(&self, msg: EmbedderMsg) {
self.send_to_constellation(ScriptMsg::ForwardToEmbedder(msg));
}
pub fn send_to_constellation(&self, msg: ScriptMsg) {
self.upcast::<GlobalScope>()
.script_to_constellation_chan()
.send(msg)