From c6e0232afb87c5ff0f29a7a49c834c08946b9f74 Mon Sep 17 00:00:00 2001 From: Anshul Malik Date: Tue, 11 Sep 2018 22:49:35 +0530 Subject: [PATCH] send pageErrors to devtools console --- components/devtools/actors/console.rs | 2 +- components/devtools/lib.rs | 51 ++++++++++++++++++++++++++- components/devtools_traits/lib.rs | 7 ++-- components/script/devtools.rs | 7 ++-- components/script/dom/globalscope.rs | 27 ++++++++++++-- 5 files changed, 82 insertions(+), 12 deletions(-) diff --git a/components/devtools/actors/console.rs b/components/devtools/actors/console.rs index e19965701643..89224d0213d7 100644 --- a/components/devtools/actors/console.rs +++ b/components/devtools/actors/console.rs @@ -27,7 +27,7 @@ trait EncodableConsoleMessage { impl EncodableConsoleMessage for CachedConsoleMessage { fn encode(&self) -> serde_json::Result { match *self { - CachedConsoleMessage::PageError(ref a) => serde_json::to_string(a), + CachedConsoleMessage::PageErrorAPI(ref a) => serde_json::to_string(a), CachedConsoleMessage::ConsoleAPI(ref a) => serde_json::to_string(a), } } diff --git a/components/devtools/lib.rs b/components/devtools/lib.rs index 2e11fa0eda24..61b02adeb61b 100644 --- a/components/devtools/lib.rs +++ b/components/devtools/lib.rs @@ -37,7 +37,7 @@ use actors::timeline::TimelineActor; use actors::worker::WorkerActor; use devtools_traits::{ChromeToDevtoolsControlMsg, ConsoleMessage, DevtoolsControlMsg}; use devtools_traits::{DevtoolScriptControlMsg, DevtoolsPageInfo, LogLevel, NetworkEvent}; -use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId}; +use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId, PageErrorAPI}; use ipc_channel::ipc::IpcSender; use msg::constellation_msg::PipelineId; use protocol::JsonPacketStream; @@ -70,6 +70,15 @@ mod actors { } mod protocol; +#[derive(Serialize)] +struct ConsolePageErrorCall { + from: String, + #[serde(rename = "type")] + type_: String, + #[serde(rename = "pageError")] + page_error: PageErrorAPI, +} + #[derive(Serialize)] struct ConsoleAPICall { from: String, @@ -285,6 +294,37 @@ fn run_server( actors.register(Box::new(thread)); } + fn handle_console_page_error_message( + actors: Arc>, + id: PipelineId, + worker_id: Option, + page_error: PageErrorAPI, + actor_pipelines: &HashMap, + actor_workers: &HashMap<(PipelineId, WorkerId), String>, + ) { + let console_actor_name = match find_console_actor( + actors.clone(), + id, + worker_id, + actor_workers, + actor_pipelines, + ) { + Some(name) => name, + None => return, + }; + let actors = actors.lock().unwrap(); + let console_actor = actors.find::(&console_actor_name); + + let msg = ConsolePageErrorCall { + from: console_actor.name.clone(), + type_: "pageError".to_owned(), + page_error: page_error + }; + for stream in &mut *console_actor.streams.borrow_mut() { + stream.write_json_packet(&msg); + } + } + fn handle_console_message( actors: Arc>, id: PipelineId, @@ -535,6 +575,15 @@ fn run_server( &mut actor_workers, pageinfo, ), + DevtoolsControlMsg::FromScript(ScriptToDevtoolsControlMsg::PageErrorAPI(id, page_error, worker_id)) => { + handle_console_page_error_message( + actors.clone(), + id, + worker_id, + page_error, + &actor_pipelines, + &actor_workers); + }, DevtoolsControlMsg::FromScript(ScriptToDevtoolsControlMsg::ConsoleAPI( id, console_message, diff --git a/components/devtools_traits/lib.rs b/components/devtools_traits/lib.rs index 764af5da0a27..b4ef4195d29d 100644 --- a/components/devtools_traits/lib.rs +++ b/components/devtools_traits/lib.rs @@ -83,6 +83,7 @@ pub enum ScriptToDevtoolsControlMsg { ), /// A particular page has invoked the console API. ConsoleAPI(PipelineId, ConsoleMessage, Option), + PageErrorAPI(PipelineId, PageErrorAPI, Option), /// An animation frame with the given timestamp was processed in a script thread. /// The actor with the provided name should be notified. FramerateTick(String, f64), @@ -261,9 +262,7 @@ bitflags! { } #[derive(Debug, Deserialize, Serialize)] -pub struct PageError { - #[serde(rename = "_type")] - pub type_: String, +pub struct PageErrorAPI { pub errorMessage: String, pub sourceName: String, pub lineText: String, @@ -293,7 +292,7 @@ pub struct ConsoleAPI { #[derive(Debug, Deserialize, Serialize)] pub enum CachedConsoleMessage { - PageError(PageError), + PageErrorAPI(PageErrorAPI), ConsoleAPI(ConsoleAPI), } diff --git a/components/script/devtools.rs b/components/script/devtools.rs index 2ce08179df4d..1f3d97550d92 100644 --- a/components/script/devtools.rs +++ b/components/script/devtools.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use devtools_traits::{AutoMargins, CachedConsoleMessage, CachedConsoleMessageTypes}; -use devtools_traits::{ComputedNodeLayout, ConsoleAPI, PageError}; +use devtools_traits::{ComputedNodeLayout, ConsoleAPI, PageErrorAPI}; use devtools_traits::{EvaluateJSReply, Modification, NodeInfo, TimelineMarker}; use devtools_traits::TimelineMarkerType; use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyleDeclarationMethods; @@ -173,8 +173,7 @@ pub fn handle_get_cached_messages(_pipeline_id: PipelineId, if message_types.contains(CachedConsoleMessageTypes::PAGE_ERROR) { // TODO: make script error reporter pass all reported errors // to devtools and cache them for returning here. - let msg = PageError { - type_: "PageError".to_owned(), + let msg = PageErrorAPI { errorMessage: "page error test".to_owned(), sourceName: String::new(), lineText: String::new(), @@ -188,7 +187,7 @@ pub fn handle_get_cached_messages(_pipeline_id: PipelineId, strict: false, private: false, }; - messages.push(CachedConsoleMessage::PageError(msg)); + messages.push(CachedConsoleMessage::PageErrorAPI(msg)); } if message_types.contains(CachedConsoleMessageTypes::CONSOLE_API) { // TODO: do for real diff --git a/components/script/dom/globalscope.rs b/components/script/dom/globalscope.rs index 596530058ec7..fadc7d16b1e0 100644 --- a/components/script/dom/globalscope.rs +++ b/components/script/dom/globalscope.rs @@ -1,8 +1,7 @@ /* 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 http://mozilla.org/MPL/2.0/. */ - -use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId}; +use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId, PageErrorAPI}; use dom::bindings::cell::DomRefCell; use dom::bindings::codegen::Bindings::EventSourceBinding::EventSourceBinding::EventSourceMethods; use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods; @@ -377,10 +376,34 @@ impl GlobalScope { // Step 9. if event_status == EventStatus::NotCanceled { + if let Some(chan) = self.devtools_chan() { + let worker_id = self.downcast::().map(|worker| { + worker.get_worker_id() + }); + + let msg = ScriptToDevtoolsControlMsg::PageErrorAPI(self.pipeline_id(), PageErrorAPI { + lineText: String::new(), + errorMessage: error_info.message.as_str().into(), + sourceName: error_info.filename.as_str().into(), + lineNumber: error_info.lineno, + columnNumber: error_info.column, + category: String::new(), + timeStamp: 0, + error: false, + warning: false, + exception: false, + strict: false, + private: false, + }, worker_id); + + chan.send(msg).unwrap(); + } + // https://html.spec.whatwg.org/multipage/#runtime-script-errors-2 if let Some(dedicated) = self.downcast::() { dedicated.forward_error_to_worker_object(error_info); } + } }