Actor::handle_message should return an enum instad of a boolean #7110 r=jdm

This commit is contained in:
Fabrice Desré 2015-08-13 18:14:34 -07:00
parent f5e97ef1b5
commit c7b48240b0
12 changed files with 83 additions and 76 deletions

View file

@ -15,6 +15,12 @@ use std::net::TcpStream;
use std::raw::TraitObject;
use std::sync::{Arc, Mutex};
#[derive(PartialEq)]
pub enum ActorMessageStatus {
Processed,
Ignored,
}
/// A common trait for all devtools actors that encompasses an immutable name
/// and the ability to process messages that are directed to particular actors.
/// TODO: ensure the name is immutable
@ -23,7 +29,7 @@ pub trait Actor: Any {
registry: &ActorRegistry,
msg_type: &str,
msg: &json::Object,
stream: &mut TcpStream) -> Result<bool, ()>;
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()>;
fn name(&self) -> String;
}
@ -194,7 +200,8 @@ impl ActorRegistry {
None => println!("message received for unknown actor \"{}\"", to),
Some(actor) => {
let msg_type = msg.get("type").unwrap().as_string().unwrap();
if !try!(actor.handle_message(self, &msg_type.to_string(), msg, stream)) {
if try!(actor.handle_message(self, &msg_type.to_string(), msg, stream))
!= ActorMessageStatus::Processed {
println!("unexpected message type \"{}\" found for actor \"{}\"",
msg_type, to);
}

View file

@ -7,7 +7,7 @@
//! Mediates interaction between the remote web console and equivalent functionality (object
//! inspection, JS evaluation, autocompletion) in Servo.
use actor::{Actor, ActorRegistry};
use actor::{Actor, ActorRegistry, ActorMessageStatus};
use actors::object::ObjectActor;
use protocol::JsonPacketStream;
@ -96,7 +96,7 @@ impl Actor for ConsoleActor {
registry: &ActorRegistry,
msg_type: &str,
msg: &json::Object,
stream: &mut TcpStream) -> Result<bool, ()> {
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| {
@ -124,7 +124,7 @@ impl Actor for ConsoleActor {
messages: messages,
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
"startListeners" => {
@ -139,7 +139,7 @@ impl Actor for ConsoleActor {
}
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
"stopListeners" => {
@ -155,7 +155,7 @@ impl Actor for ConsoleActor {
.collect(),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
//TODO: implement autocompletion like onAutocomplete in
@ -167,7 +167,7 @@ impl Actor for ConsoleActor {
matchProp: "".to_string(),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
"evaluateJS" => {
@ -237,10 +237,10 @@ impl Actor for ConsoleActor {
helperResult: Json::Object(BTreeMap::new()),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
_ => false
_ => ActorMessageStatus::Ignored
})
}
}

View file

@ -10,7 +10,7 @@ use std::sync::{Arc, Mutex};
use time::precise_time_ns;
use msg::constellation_msg::PipelineId;
use actor::{Actor, ActorRegistry};
use actor::{Actor, ActorRegistry, ActorMessageStatus};
use actors::timeline::HighResolutionStamp;
use devtools_traits::DevtoolScriptControlMsg;
@ -33,8 +33,8 @@ impl Actor for FramerateActor {
_registry: &ActorRegistry,
_msg_type: &str,
_msg: &json::Object,
_stream: &mut TcpStream) -> Result<bool, ()> {
Ok(false)
_stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(ActorMessageStatus::Ignored)
}
}

View file

@ -9,7 +9,7 @@ use devtools_traits::{DevtoolScriptControlMsg, NodeInfo, ComputedNodeLayout};
use devtools_traits::DevtoolScriptControlMsg::{GetRootNode, GetDocumentElement, GetChildren};
use devtools_traits::DevtoolScriptControlMsg::{GetLayout, ModifyAttribute};
use actor::{Actor, ActorRegistry};
use actor::{Actor, ActorRegistry, ActorMessageStatus};
use protocol::JsonPacketStream;
use ipc_channel::ipc::{self, IpcSender};
@ -69,14 +69,14 @@ impl Actor for HighlighterActor {
_registry: &ActorRegistry,
msg_type: &str,
_msg: &json::Object,
stream: &mut TcpStream) -> Result<bool, ()> {
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"showBoxModel" => {
let msg = ShowBoxModelReply {
from: self.name(),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
"hideBoxModel" => {
@ -84,10 +84,10 @@ impl Actor for HighlighterActor {
from: self.name(),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
_ => false,
_ => ActorMessageStatus::Ignored,
})
}
}
@ -106,7 +106,7 @@ impl Actor for NodeActor {
registry: &ActorRegistry,
msg_type: &str,
msg: &json::Object,
stream: &mut TcpStream) -> Result<bool, ()> {
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"modifyAttributes" => {
let target = msg.get(&"to".to_string()).unwrap().as_string().unwrap();
@ -123,10 +123,10 @@ impl Actor for NodeActor {
from: self.name(),
};
stream.write_json_packet(&reply);
true
ActorMessageStatus::Processed
}
_ => false,
_ => ActorMessageStatus::Ignored,
})
}
}
@ -280,14 +280,14 @@ impl Actor for WalkerActor {
registry: &ActorRegistry,
msg_type: &str,
msg: &json::Object,
stream: &mut TcpStream) -> Result<bool, ()> {
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"querySelector" => {
let msg = QuerySelectorReply {
from: self.name(),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
"documentElement" => {
@ -301,7 +301,7 @@ impl Actor for WalkerActor {
node: node,
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
"clearPseudoClassLocks" => {
@ -309,7 +309,7 @@ impl Actor for WalkerActor {
from: self.name(),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
"children" => {
@ -330,10 +330,10 @@ impl Actor for WalkerActor {
from: self.name(),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
_ => false,
_ => ActorMessageStatus::Ignored,
})
}
}
@ -426,7 +426,7 @@ impl Actor for PageStyleActor {
registry: &ActorRegistry,
msg_type: &str,
msg: &json::Object,
stream: &mut TcpStream) -> Result<bool, ()> {
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"getApplied" => {
//TODO: query script for relevant applied styles to node (msg.node)
@ -437,7 +437,7 @@ impl Actor for PageStyleActor {
from: self.name(),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
"getComputed" => {
@ -447,7 +447,7 @@ impl Actor for PageStyleActor {
from: self.name(),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
//TODO: query script for box layout properties of node (msg.node)
@ -484,10 +484,10 @@ impl Actor for PageStyleActor {
from: self.name(),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
_ => false,
_ => ActorMessageStatus::Ignored,
})
}
}
@ -501,7 +501,7 @@ impl Actor for InspectorActor {
registry: &ActorRegistry,
msg_type: &str,
_msg: &json::Object,
stream: &mut TcpStream) -> Result<bool, ()> {
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"getWalker" => {
if self.walker.borrow().is_none() {
@ -529,7 +529,7 @@ impl Actor for InspectorActor {
}
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
"getPageStyle" => {
@ -551,7 +551,7 @@ impl Actor for InspectorActor {
},
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
//TODO: this is an old message; try adding highlightable to the root traits instead
@ -574,10 +574,10 @@ impl Actor for InspectorActor {
},
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
_ => false,
_ => ActorMessageStatus::Ignored,
})
}
}

View file

@ -5,7 +5,7 @@
use rustc_serialize::json;
use std::net::TcpStream;
use actor::{Actor, ActorRegistry};
use actor::{Actor, ActorRegistry, ActorMessageStatus};
#[derive(RustcEncodable)]
pub struct TimelineMemoryReply {
@ -33,8 +33,8 @@ impl Actor for MemoryActor {
_registry: &ActorRegistry,
_msg_type: &str,
_msg: &json::Object,
_stream: &mut TcpStream) -> Result<bool, ()> {
Ok(false)
_stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(ActorMessageStatus::Ignored)
}
}

View file

@ -8,7 +8,7 @@
extern crate hyper;
use actor::{Actor, ActorRegistry};
use actor::{Actor, ActorRegistry, ActorMessageStatus};
use protocol::JsonPacketStream;
use rustc_serialize::json;
use std::net::TcpStream;
@ -74,7 +74,7 @@ impl Actor for NetworkEventActor {
_registry: &ActorRegistry,
msg_type: &str,
_msg: &json::Object,
stream: &mut TcpStream) -> Result<bool, ()> {
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"getRequestHeaders" => {
// TODO: Pass the correct values for headers, headerSize, rawHeaders
@ -85,24 +85,24 @@ impl Actor for NetworkEventActor {
rawHeaders: "Raw headers".to_string(),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
"getRequestCookies" => {
false
ActorMessageStatus::Ignored
}
"getRequestPostData" => {
false
ActorMessageStatus::Ignored
}
"getResponseHeaders" => {
false
ActorMessageStatus::Ignored
}
"getResponseCookies" => {
false
ActorMessageStatus::Ignored
}
"getResponseContent" => {
false
ActorMessageStatus::Ignored
}
_ => false
_ => ActorMessageStatus::Ignored
})
}
}

View file

@ -2,7 +2,7 @@
* 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 actor::{Actor, ActorRegistry};
use actor::{Actor, ActorRegistry, ActorMessageStatus};
use rustc_serialize::json;
use std::net::TcpStream;
@ -19,8 +19,8 @@ impl Actor for ObjectActor {
_: &ActorRegistry,
_: &str,
_: &json::Object,
_: &mut TcpStream) -> Result<bool, ()> {
Ok(false)
_: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(ActorMessageStatus::Ignored)
}
}

View file

@ -2,7 +2,7 @@
* 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 actor::{Actor, ActorRegistry};
use actor::{Actor, ActorRegistry, ActorMessageStatus};
use rustc_serialize::json;
use std::net::TcpStream;
@ -20,8 +20,8 @@ impl Actor for ProfilerActor {
_registry: &ActorRegistry,
_msg_type: &str,
_msg: &json::Object,
_stream: &mut TcpStream) -> Result<bool, ()> {
Ok(false)
_stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(ActorMessageStatus::Ignored)
}
}

View file

@ -7,7 +7,7 @@
/// Connection point for all new remote devtools interactions, providing lists of know actors
/// that perform more specific actions (tabs, addons, browser chrome, etc.)
use actor::{Actor, ActorRegistry};
use actor::{Actor, ActorRegistry, ActorMessageStatus};
use actors::tab::{TabActor, TabActorMsg};
use protocol::JsonPacketStream;
@ -55,7 +55,7 @@ impl Actor for RootActor {
registry: &ActorRegistry,
msg_type: &str,
_msg: &json::Object,
stream: &mut TcpStream) -> Result<bool, ()> {
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"listAddons" => {
let actor = ErrorReply {
@ -64,7 +64,7 @@ impl Actor for RootActor {
message: "This root actor has no browser addons.".to_string(),
};
stream.write_json_packet(&actor);
true
ActorMessageStatus::Processed
}
//https://wiki.mozilla.org/Remote_Debugging_Protocol#Listing_Browser_Tabs
@ -77,10 +77,10 @@ impl Actor for RootActor {
}).collect()
};
stream.write_json_packet(&actor);
true
ActorMessageStatus::Processed
}
_ => false
_ => ActorMessageStatus::Ignored
})
}
}

View file

@ -7,7 +7,7 @@
//! Connection point for remote devtools that wish to investigate a particular tab's contents.
//! Supports dynamic attaching and detaching which control notifications of navigation, etc.
use actor::{Actor, ActorRegistry};
use actor::{Actor, ActorRegistry, ActorMessageStatus};
use actors::console::ConsoleActor;
use devtools_traits::DevtoolScriptControlMsg::WantsLiveNotifications;
use protocol::JsonPacketStream;
@ -84,11 +84,11 @@ impl Actor for TabActor {
registry: &ActorRegistry,
msg_type: &str,
_msg: &json::Object,
stream: &mut TcpStream) -> Result<bool, ()> {
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"reconfigure" => {
stream.write_json_packet(&ReconfigureReply { from: self.name() });
true
ActorMessageStatus::Processed
}
// https://wiki.mozilla.org/Remote_Debugging_Protocol#Listing_Browser_Tabs
@ -107,7 +107,7 @@ impl Actor for TabActor {
stream.write_json_packet(&msg);
console_actor.script_chan.send(
WantsLiveNotifications(console_actor.pipeline, true)).unwrap();
true
ActorMessageStatus::Processed
}
//FIXME: The current implementation won't work for multiple connections. Need to ensure 105
@ -122,7 +122,7 @@ impl Actor for TabActor {
stream.write_json_packet(&msg);
console_actor.script_chan.send(
WantsLiveNotifications(console_actor.pipeline, false)).unwrap();
true
ActorMessageStatus::Processed
}
"listFrames" => {
@ -131,10 +131,10 @@ impl Actor for TabActor {
frames: vec!(),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
_ => false
_ => ActorMessageStatus::Ignored
})
}
}

View file

@ -13,7 +13,7 @@ use std::thread::sleep_ms;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::channel;
use actor::{Actor, ActorRegistry};
use actor::{Actor, ActorRegistry, ActorMessageStatus};
use actors::memory::{MemoryActor, TimelineMemoryReply};
use actors::framerate::FramerateActor;
use devtools_traits::DevtoolScriptControlMsg;
@ -232,7 +232,7 @@ impl Actor for TimelineActor {
registry: &ActorRegistry,
msg_type: &str,
msg: &json::Object,
stream: &mut TcpStream) -> Result<bool, ()> {
stream: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(match msg_type {
"start" => {
**self.is_recording.lock().as_mut().unwrap() = true;
@ -275,7 +275,7 @@ impl Actor for TimelineActor {
value: HighResolutionStamp::new(registry.start_stamp(), PreciseTime::now()),
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
"stop" => {
@ -297,7 +297,7 @@ impl Actor for TimelineActor {
**self.is_recording.lock().as_mut().unwrap() = false;
self.stream.borrow_mut().take();
true
ActorMessageStatus::Processed
}
"isRecording" => {
@ -307,11 +307,11 @@ impl Actor for TimelineActor {
};
stream.write_json_packet(&msg);
true
ActorMessageStatus::Processed
}
_ => {
false
ActorMessageStatus::Ignored
}
})
}

View file

@ -2,7 +2,7 @@
* 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 actor::{Actor, ActorRegistry};
use actor::{Actor, ActorRegistry, ActorMessageStatus};
use msg::constellation_msg::WorkerId;
use rustc_serialize::json;
use std::net::TcpStream;
@ -21,7 +21,7 @@ impl Actor for WorkerActor {
_: &ActorRegistry,
_: &str,
_: &json::Object,
_: &mut TcpStream) -> Result<bool, ()> {
Ok(true)
_: &mut TcpStream) -> Result<ActorMessageStatus, ()> {
Ok(ActorMessageStatus::Processed)
}
}