Skip to content

Commit

Permalink
Format components devtools and devtools_traits #21373
Browse files Browse the repository at this point in the history
  • Loading branch information
kingdido999 committed Sep 2, 2018
1 parent b211e45 commit ad822b7
Show file tree
Hide file tree
Showing 17 changed files with 670 additions and 462 deletions.
51 changes: 30 additions & 21 deletions components/devtools/actor.rs
Expand Up @@ -3,7 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/// General actor system infrastructure.

use devtools_traits::PreciseTime;
use serde_json::{Map, Value};
use std::any::Any;
Expand All @@ -23,11 +22,13 @@ pub enum ActorMessageStatus {
/// and the ability to process messages that are directed to particular actors.
/// TODO: ensure the name is immutable
pub trait Actor: Any + ActorAsAny {
fn handle_message(&self,
registry: &ActorRegistry,
msg_type: &str,
msg: &Map<String, Value>,
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()>;
fn handle_message(
&self,
registry: &ActorRegistry,
msg_type: &str,
msg: &Map<String, Value>,
stream: &mut TcpStream,
) -> Result<ActorMessageStatus, ()>;
fn name(&self) -> String;
}

Expand All @@ -37,8 +38,12 @@ pub trait ActorAsAny {
}

impl<T: Actor> ActorAsAny for T {
fn actor_as_any(&self) -> &Any { self }
fn actor_as_any_mut(&mut self) -> &mut Any { self }
fn actor_as_any(&self) -> &Any {
self
}
fn actor_as_any_mut(&mut self) -> &mut Any {
self
}
}

/// A list of known, owned actors.
Expand All @@ -57,8 +62,8 @@ impl ActorRegistry {
pub fn new() -> ActorRegistry {
ActorRegistry {
actors: HashMap::new(),
new_actors: RefCell::new(vec!()),
old_actors: RefCell::new(vec!()),
new_actors: RefCell::new(vec![]),
old_actors: RefCell::new(vec![]),
script_actors: RefCell::new(HashMap::new()),
shareable: None,
next: Cell::new(0),
Expand Down Expand Up @@ -149,29 +154,33 @@ impl ActorRegistry {

/// Attempt to process a message as directed by its `to` property. If the actor is not
/// found or does not indicate that it knew how to process the message, ignore the failure.
pub fn handle_message(&mut self,
msg: &Map<String, Value>,
stream: &mut TcpStream)
-> Result<(), ()> {
pub fn handle_message(
&mut self,
msg: &Map<String, Value>,
stream: &mut TcpStream,
) -> Result<(), ()> {
let to = msg.get("to").unwrap().as_str().unwrap();

match self.actors.get(to) {
None => debug!("message received for unknown actor \"{}\"", to),
Some(actor) => {
let msg_type = msg.get("type").unwrap().as_str().unwrap();
if actor.handle_message(self, msg_type, msg, stream)?
!= ActorMessageStatus::Processed {
debug!("unexpected message type \"{}\" found for actor \"{}\"",
msg_type, to);
if actor.handle_message(self, msg_type, msg, stream)? !=
ActorMessageStatus::Processed
{
debug!(
"unexpected message type \"{}\" found for actor \"{}\"",
msg_type, to
);
}
}
},
}
let new_actors = replace(&mut *self.new_actors.borrow_mut(), vec!());
let new_actors = replace(&mut *self.new_actors.borrow_mut(), vec![]);
for actor in new_actors.into_iter() {
self.actors.insert(actor.name().to_owned(), actor);
}

let old_actors = replace(&mut *self.old_actors.borrow_mut(), vec!());
let old_actors = replace(&mut *self.old_actors.borrow_mut(), vec![]);
for name in old_actors {
self.drop_actor(name);
}
Expand Down
102 changes: 61 additions & 41 deletions components/devtools/actors/console.rs
Expand Up @@ -94,102 +94,122 @@ impl Actor for ConsoleActor {
self.name.clone()
}

fn handle_message(&self,
registry: &ActorRegistry,
msg_type: &str,
msg: &Map<String, Value>,
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
fn handle_message(
&self,
registry: &ActorRegistry,
msg_type: &str,
msg: &Map<String, Value>,
stream: &mut TcpStream,
) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"getCachedMessages" => {
let str_types = msg.get("messageTypes").unwrap().as_array().unwrap().into_iter().map(|json_type| {
json_type.as_str().unwrap()
});
let str_types = msg
.get("messageTypes")
.unwrap()
.as_array()
.unwrap()
.into_iter()
.map(|json_type| json_type.as_str().unwrap());
let mut message_types = CachedConsoleMessageTypes::empty();
for str_type in str_types {
match str_type {
"PageError" => message_types.insert(CachedConsoleMessageTypes::PAGE_ERROR),
"ConsoleAPI" => message_types.insert(CachedConsoleMessageTypes::CONSOLE_API),
"ConsoleAPI" => {
message_types.insert(CachedConsoleMessageTypes::CONSOLE_API)
},
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 json = serde_json::from_str::<Value>(&json_string).unwrap();
json.as_object().unwrap().to_owned()
}).collect();
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 json = serde_json::from_str::<Value>(&json_string).unwrap();
json.as_object().unwrap().to_owned()
}).collect();

let msg = GetCachedMessagesReply {
from: self.name(),
messages: messages,
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},

"startListeners" => {
//TODO: actually implement listener filters that support starting/stopping
let msg = StartedListenersReply {
from: self.name(),
nativeConsoleAPI: true,
startedListeners:
vec!("PageError".to_owned(), "ConsoleAPI".to_owned()),
startedListeners: vec!["PageError".to_owned(), "ConsoleAPI".to_owned()],
traits: StartedListenersTraits {
customNetworkRequest: true,
}
},
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},

"stopListeners" => {
//TODO: actually implement listener filters that support starting/stopping
let msg = StopListenersReply {
from: self.name(),
stoppedListeners: msg.get("listeners")
.unwrap()
.as_array()
.unwrap_or(&vec!())
.iter()
.map(|listener| listener.as_str().unwrap().to_owned())
.collect(),
stoppedListeners: msg
.get("listeners")
.unwrap()
.as_array()
.unwrap_or(&vec![])
.iter()
.map(|listener| listener.as_str().unwrap().to_owned())
.collect(),
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},

//TODO: implement autocompletion like onAutocomplete in
// http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/server/actors/webconsole.js
"autocomplete" => {
let msg = AutocompleteReply {
from: self.name(),
matches: vec!(),
matches: vec![],
matchProp: "".to_owned(),
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},

"evaluateJS" => {
let input = msg.get("text").unwrap().as_str().unwrap().to_owned();
let (chan, port) = ipc::channel().unwrap();
self.script_chan.send(DevtoolScriptControlMsg::EvaluateJS(
self.pipeline, input.clone(), chan)).unwrap();
self.script_chan
.send(DevtoolScriptControlMsg::EvaluateJS(
self.pipeline,
input.clone(),
chan,
)).unwrap();

//TODO: extract conversion into protocol module or some other useful place
let result = match port.recv().map_err(|_| ())? {
VoidValue => {
let mut m = Map::new();
m.insert("type".to_owned(), Value::String("undefined".to_owned()));
Value::Object(m)
}
},
NullValue => {
let mut m = Map::new();
m.insert("type".to_owned(), Value::String("null".to_owned()));
Value::Object(m)
}
},
BooleanValue(val) => Value::Bool(val),
NumberValue(val) => {
if val.is_nan() {
Expand All @@ -211,7 +231,7 @@ impl Actor for ConsoleActor {
} else {
Value::Number(Number::from_f64(val).unwrap())
}
}
},
StringValue(s) => Value::String(s),
ActorValue { class, uuid } => {
//TODO: make initial ActorValue message include these properties?
Expand All @@ -225,7 +245,7 @@ impl Actor for ConsoleActor {
m.insert("frozen".to_owned(), Value::Bool(false));
m.insert("sealed".to_owned(), Value::Bool(false));
Value::Object(m)
}
},
};

//TODO: catch and return exception values from JS evaluation
Expand All @@ -240,7 +260,7 @@ impl Actor for ConsoleActor {
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},

"setPreferences" => {
let msg = SetPreferencesReply {
Expand All @@ -249,9 +269,9 @@ impl Actor for ConsoleActor {
};
stream.write_json_packet(&msg);
ActorMessageStatus::Processed
}
},

_ => ActorMessageStatus::Ignored
_ => ActorMessageStatus::Ignored,
})
}
}
28 changes: 14 additions & 14 deletions components/devtools/actors/framerate.rs
Expand Up @@ -26,21 +26,24 @@ impl Actor for FramerateActor {
self.name.clone()
}


fn handle_message(&self,
_registry: &ActorRegistry,
_msg_type: &str,
_msg: &Map<String, Value>,
_stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
fn handle_message(
&self,
_registry: &ActorRegistry,
_msg_type: &str,
_msg: &Map<String, Value>,
_stream: &mut TcpStream,
) -> Result<ActorMessageStatus, ()> {
Ok(ActorMessageStatus::Ignored)
}
}

impl FramerateActor {
/// return name of actor
pub fn create(registry: &ActorRegistry,
pipeline_id: PipelineId,
script_sender: IpcSender<DevtoolScriptControlMsg>) -> String {
pub fn create(
registry: &ActorRegistry,
pipeline_id: PipelineId,
script_sender: IpcSender<DevtoolScriptControlMsg>,
) -> String {
let actor_name = registry.new_name("framerate");
let mut actor = FramerateActor {
name: actor_name.clone(),
Expand All @@ -60,8 +63,7 @@ impl FramerateActor {
self.ticks.push(HighResolutionStamp::wrap(tick));

if self.is_recording {
let msg = DevtoolScriptControlMsg::RequestAnimationFrame(self.pipeline,
self.name());
let msg = DevtoolScriptControlMsg::RequestAnimationFrame(self.pipeline, self.name());
self.script_sender.send(msg).unwrap();
}
}
Expand All @@ -78,8 +80,7 @@ impl FramerateActor {
self.start_time = Some(precise_time_ns());
self.is_recording = true;

let msg = DevtoolScriptControlMsg::RequestAnimationFrame(self.pipeline,
self.name());
let msg = DevtoolScriptControlMsg::RequestAnimationFrame(self.pipeline, self.name());
self.script_sender.send(msg).unwrap();
}

Expand All @@ -90,7 +91,6 @@ impl FramerateActor {
self.is_recording = false;
self.start_time = None;
}

}

impl Drop for FramerateActor {
Expand Down

0 comments on commit ad822b7

Please sign in to comment.