Skip to content
Permalink
Browse files

devtools: save and send cached messages

  • Loading branch information
paulrouget committed Dec 6, 2019
1 parent 6cd42bd commit b3b886e837792bc130d3514bef4d8619aed4c41d
@@ -10,15 +10,19 @@
use crate::actor::{Actor, ActorMessageStatus, ActorRegistry};
use crate::actors::object::ObjectActor;
use crate::protocol::JsonPacketStream;
use crate::{ConsoleAPICall, ConsoleMessage, ConsoleMsg, PageErrorMsg};
use devtools_traits::CachedConsoleMessage;
use devtools_traits::EvaluateJSReply::{ActorValue, BooleanValue, StringValue};
use devtools_traits::EvaluateJSReply::{NullValue, NumberValue, VoidValue};
use devtools_traits::{CachedConsoleMessageTypes, DevtoolScriptControlMsg};
use devtools_traits::{
CachedConsoleMessageTypes, ConsoleAPI, DevtoolScriptControlMsg, LogLevel, PageError,
};
use ipc_channel::ipc::{self, IpcSender};
use msg::constellation_msg::PipelineId;
use serde_json::{self, Map, Number, Value};
use std::cell::RefCell;
use std::net::TcpStream;
use time::precise_time_ns;
use uuid::Uuid;

trait EncodableConsoleMessage {
@@ -107,6 +111,7 @@ pub struct ConsoleActor {
pub pipeline: PipelineId,
pub script_chan: IpcSender<DevtoolScriptControlMsg>,
pub streams: RefCell<Vec<TcpStream>>,
pub cached_events: RefCell<Vec<CachedConsoleMessage>>,
}

impl ConsoleActor {
@@ -187,6 +192,58 @@ impl ConsoleActor {
};
std::result::Result::Ok(reply)
}

pub(crate) fn handle_page_error(&self, page_error: PageError) {
self.cached_events
.borrow_mut()
.push(CachedConsoleMessage::PageError(page_error.clone()));
let msg = PageErrorMsg {
from: self.name(),
type_: "pageError".to_owned(),
pageError: page_error,
};
for stream in &mut *self.streams.borrow_mut() {
stream.write_json_packet(&msg);
}
}

pub(crate) fn handle_console_api(&self, console_message: ConsoleMessage) {
let level = match console_message.logLevel {
LogLevel::Debug => "debug",
LogLevel::Info => "info",
LogLevel::Warn => "warn",
LogLevel::Error => "error",
_ => "log",
}
.to_owned();
self.cached_events
.borrow_mut()
.push(CachedConsoleMessage::ConsoleAPI(ConsoleAPI {
type_: "ConsoleAPI".to_owned(),
level: level.clone(),
filename: console_message.filename.clone(),
lineNumber: console_message.lineNumber as u32,
functionName: "".to_string(), //TODO
timeStamp: precise_time_ns(),
private: false,
arguments: vec![console_message.message.clone()],
}));
let msg = ConsoleAPICall {
from: self.name(),
type_: "consoleAPICall".to_owned(),
message: ConsoleMsg {
level: level,
timeStamp: precise_time_ns(),
arguments: vec![console_message.message],
filename: console_message.filename,
lineNumber: console_message.lineNumber,
columnNumber: console_message.columnNumber,
},
};
for stream in &mut *self.streams.borrow_mut() {
stream.write_json_packet(&msg);
}
}
}

impl Actor for ConsoleActor {
@@ -220,24 +277,27 @@ impl Actor for ConsoleActor {
s => debug!("unrecognized message type requested: \"{}\"", s),
};
}
let (chan, port) = ipc::channel().unwrap();
self.script_chan
.send(DevtoolScriptControlMsg::GetCachedMessages(
self.pipeline,
message_types,
chan,
))
.unwrap();
let messages = port
.recv()
.map_err(|_| ())?
.into_iter()
.map(|message| {
let json_string = message.encode().unwrap();
let mut messages = vec![];
for event in self.cached_events.borrow().iter() {
let include = match event {
CachedConsoleMessage::PageError(_)
if message_types.contains(CachedConsoleMessageTypes::PAGE_ERROR) =>
{
true
},
CachedConsoleMessage::ConsoleAPI(_)
if message_types.contains(CachedConsoleMessageTypes::CONSOLE_API) =>
{
true
},
_ => false,
};
if include {
let json_string = event.encode().unwrap();
let json = serde_json::from_str::<Value>(&json_string).unwrap();
json.as_object().unwrap().to_owned()
})
.collect();
messages.push(json.as_object().unwrap().to_owned())
}
}

let msg = GetCachedMessagesReply {
from: self.name(),
@@ -36,7 +36,7 @@ use crate::protocol::JsonPacketStream;
use crossbeam_channel::{unbounded, Receiver, Sender};
use devtools_traits::{ChromeToDevtoolsControlMsg, ConsoleMessage, DevtoolsControlMsg};
use devtools_traits::{DevtoolScriptControlMsg, DevtoolsPageInfo, LogLevel, NetworkEvent};
use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId};
use devtools_traits::{PageError, ScriptToDevtoolsControlMsg, WorkerId};
use ipc_channel::ipc::IpcSender;
use msg::constellation_msg::PipelineId;
use std::borrow::ToOwned;
@@ -46,7 +46,6 @@ use std::collections::HashMap;
use std::net::{Shutdown, TcpListener, TcpStream};
use std::sync::{Arc, Mutex};
use std::thread;
use time::precise_time_ns;

mod actor;
/// Corresponds to http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/server/actors/
@@ -88,6 +87,14 @@ struct ConsoleMsg {
columnNumber: usize,
}

#[derive(Serialize)]
struct PageErrorMsg {
from: String,
#[serde(rename = "type")]
type_: String,
pageError: PageError,
}

#[derive(Serialize)]
struct NetworkEventMsg {
from: String,
@@ -141,7 +148,7 @@ fn run_server(
receiver: Receiver<DevtoolsControlMsg>,
port: u16,
) {
let listener = TcpListener::bind(&("127.0.0.1", port)).unwrap();
let listener = TcpListener::bind(&("0.0.0.0", port)).unwrap();

let mut registry = ActorRegistry::new();

@@ -241,6 +248,7 @@ fn run_server(
script_chan: script_sender.clone(),
pipeline: pipeline,
streams: RefCell::new(Vec::new()),
cached_events: RefCell::new(Vec::new()),
};

let emulation = EmulationActor::new(actors.new_name("emulation"));
@@ -317,6 +325,22 @@ fn run_server(
actors.register(Box::new(thread));
}

fn handle_page_error(
actors: Arc<Mutex<ActorRegistry>>,
id: PipelineId,
page_error: PageError,
actor_pipelines: &HashMap<PipelineId, String>,
) {
let console_actor_name =
match find_console_actor(actors.clone(), id, None, &HashMap::new(), actor_pipelines) {
Some(name) => name,
None => return,
};
let actors = actors.lock().unwrap();
let console_actor = actors.find::<ConsoleActor>(&console_actor_name);
console_actor.handle_page_error(page_error);
}

fn handle_console_message(
actors: Arc<Mutex<ActorRegistry>>,
id: PipelineId,
@@ -337,28 +361,7 @@ fn run_server(
};
let actors = actors.lock().unwrap();
let console_actor = actors.find::<ConsoleActor>(&console_actor_name);
let msg = ConsoleAPICall {
from: console_actor.name.clone(),
type_: "consoleAPICall".to_owned(),
message: ConsoleMsg {
level: match console_message.logLevel {
LogLevel::Debug => "debug",
LogLevel::Info => "info",
LogLevel::Warn => "warn",
LogLevel::Error => "error",
_ => "log",
}
.to_owned(),
timeStamp: precise_time_ns(),
arguments: vec![console_message.message],
filename: console_message.filename,
lineNumber: console_message.lineNumber,
columnNumber: console_message.columnNumber,
},
};
for stream in &mut *console_actor.streams.borrow_mut() {
stream.write_json_packet(&msg);
}
console_actor.handle_console_api(console_message);
}

fn find_console_actor(
@@ -586,6 +589,10 @@ fn run_server(
&actor_pipelines,
&actor_workers,
),
DevtoolsControlMsg::FromScript(ScriptToDevtoolsControlMsg::ReportPageError(
id,
page_error,
)) => handle_page_error(actors.clone(), id, page_error, &actor_pipelines),
DevtoolsControlMsg::FromScript(ScriptToDevtoolsControlMsg::ReportCSSError(
id,
css_error,
@@ -82,6 +82,9 @@ pub enum ScriptToDevtoolsControlMsg {

/// Report a CSS parse error for the given pipeline
ReportCSSError(PipelineId, CSSError),

/// Report a page error for the given pipeline
ReportPageError(PipelineId, PageError),
}

/// Serialized JS return values
@@ -196,12 +199,6 @@ pub enum DevtoolScriptControlMsg {
GetChildren(PipelineId, String, IpcSender<Option<Vec<NodeInfo>>>),
/// Retrieve the computed layout properties of the given node in the given pipeline.
GetLayout(PipelineId, String, IpcSender<Option<ComputedNodeLayout>>),
/// Retrieve all stored console messages for the given pipeline.
GetCachedMessages(
PipelineId,
CachedConsoleMessageTypes,
IpcSender<Vec<CachedConsoleMessage>>,
),
/// Update a given node's attributes with a list of modifications.
ModifyAttribute(PipelineId, String, Vec<Modification>),
/// Request live console messages for a given pipeline (true if desired, false otherwise).
@@ -253,7 +250,7 @@ bitflags! {
}
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PageError {
#[serde(rename = "_type")]
pub type_: String,
@@ -18,9 +18,7 @@ use crate::dom::globalscope::GlobalScope;
use crate::dom::node::{window_from_node, Node, ShadowIncluding};
use crate::dom::window::Window;
use crate::script_thread::Documents;
use devtools_traits::TimelineMarkerType;
use devtools_traits::{AutoMargins, CachedConsoleMessage, CachedConsoleMessageTypes};
use devtools_traits::{ComputedNodeLayout, ConsoleAPI, PageError};
use devtools_traits::{AutoMargins, ComputedNodeLayout, TimelineMarkerType};
use devtools_traits::{EvaluateJSReply, Modification, NodeInfo, TimelineMarker};
use ipc_channel::ipc::IpcSender;
use js::jsval::UndefinedValue;
@@ -182,50 +180,6 @@ fn determine_auto_margins(window: &Window, node: &Node) -> AutoMargins {
}
}

pub fn handle_get_cached_messages(
_pipeline_id: PipelineId,
message_types: CachedConsoleMessageTypes,
reply: IpcSender<Vec<CachedConsoleMessage>>,
) {
// TODO: check the messageTypes against a global Cache for console messages and page exceptions
let mut messages = Vec::new();
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(),
errorMessage: "page error test".to_owned(),
sourceName: String::new(),
lineText: String::new(),
lineNumber: 0,
columnNumber: 0,
category: String::new(),
timeStamp: 0,
error: false,
warning: false,
exception: false,
strict: false,
private: false,
};
messages.push(CachedConsoleMessage::PageError(msg));
}
if message_types.contains(CachedConsoleMessageTypes::CONSOLE_API) {
// TODO: do for real
let msg = ConsoleAPI {
type_: "ConsoleAPI".to_owned(),
level: "error".to_owned(),
filename: "http://localhost/~mihai/mozilla/test.html".to_owned(),
lineNumber: 0,
functionName: String::new(),
timeStamp: 0,
private: false,
arguments: vec!["console error test".to_owned()],
};
messages.push(CachedConsoleMessage::ConsoleAPI(msg));
}
reply.send(messages).unwrap();
}

pub fn handle_modify_attribute(
documents: &Documents,
pipeline: PipelineId,
@@ -476,9 +476,6 @@ impl DedicatedWorkerGlobalScope {
DevtoolScriptControlMsg::EvaluateJS(_pipe_id, string, sender) => {
devtools::handle_evaluate_js(self.upcast(), string, sender)
},
DevtoolScriptControlMsg::GetCachedMessages(pipe_id, message_types, sender) => {
devtools::handle_get_cached_messages(pipe_id, message_types, sender)
},
DevtoolScriptControlMsg::WantsLiveNotifications(_pipe_id, bool_val) => {
devtools::handle_wants_live_notifications(self.upcast(), bool_val)
},
@@ -46,7 +46,7 @@ use crate::task_source::TaskSourceName;
use crate::timers::{IsInterval, OneshotTimerCallback, OneshotTimerHandle};
use crate::timers::{OneshotTimers, TimerCallback};
use content_security_policy::CspList;
use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId};
use devtools_traits::{PageError, ScriptToDevtoolsControlMsg, WorkerId};
use dom_struct::dom_struct;
use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER;
@@ -1085,6 +1085,27 @@ impl GlobalScope {
// https://html.spec.whatwg.org/multipage/#runtime-script-errors-2
if let Some(dedicated) = self.downcast::<DedicatedWorkerGlobalScope>() {
dedicated.forward_error_to_worker_object(error_info);
} else if self.is::<Window>() {
if let Some(ref chan) = self.devtools_chan {
let _ = chan.send(ScriptToDevtoolsControlMsg::ReportPageError(
self.pipeline_id.clone(),
PageError {
type_: "PageError".to_string(),
errorMessage: error_info.message.clone(),
sourceName: error_info.filename.clone(),
lineText: "".to_string(), //TODO
lineNumber: error_info.lineno,
columnNumber: error_info.column,
category: "script".to_string(),
timeStamp: 0, //TODO
error: true,
warning: false,
exception: true,
strict: false,
private: false,
},
));
}
}
}
}
@@ -371,9 +371,6 @@ impl ServiceWorkerGlobalScope {
DevtoolScriptControlMsg::EvaluateJS(_pipe_id, string, sender) => {
devtools::handle_evaluate_js(self.upcast(), string, sender)
},
DevtoolScriptControlMsg::GetCachedMessages(pipe_id, message_types, sender) => {
devtools::handle_get_cached_messages(pipe_id, message_types, sender)
},
DevtoolScriptControlMsg::WantsLiveNotifications(_pipe_id, bool_val) => {
devtools::handle_wants_live_notifications(self.upcast(), bool_val)
},

0 comments on commit b3b886e

Please sign in to comment.
You can’t perform that action at this time.