mirror of
https://github.com/servo/servo.git
synced 2025-08-02 20:20:14 +01:00
Enable real DOM inspector support.
This commit is contained in:
parent
e9c4aa534d
commit
fa57fe890b
8 changed files with 265 additions and 94 deletions
|
@ -71,6 +71,7 @@ impl<'a> AnyRefExt<'a> for &'a Actor {
|
|||
pub struct ActorRegistry {
|
||||
actors: HashMap<String, Box<Actor+Send+Sized>>,
|
||||
new_actors: RefCell<Vec<Box<Actor+Send+Sized>>>,
|
||||
script_actors: RefCell<HashMap<String, String>>,
|
||||
next: Cell<u32>,
|
||||
}
|
||||
|
||||
|
@ -80,10 +81,38 @@ impl ActorRegistry {
|
|||
ActorRegistry {
|
||||
actors: HashMap::new(),
|
||||
new_actors: RefCell::new(vec!()),
|
||||
script_actors: RefCell::new(HashMap::new()),
|
||||
next: Cell::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register_script_actor(&self, script_id: String, actor: String) {
|
||||
println!("registering {:s} ({:s})", actor.as_slice(), script_id.as_slice());
|
||||
let mut script_actors = self.script_actors.borrow_mut();
|
||||
script_actors.insert(script_id, actor);
|
||||
}
|
||||
|
||||
pub fn script_to_actor(&self, script_id: String) -> String {
|
||||
if script_id.as_slice() == "" {
|
||||
return "".to_string();
|
||||
}
|
||||
self.script_actors.borrow().find(&script_id).unwrap().to_string()
|
||||
}
|
||||
|
||||
pub fn script_actor_registered(&self, script_id: String) -> bool {
|
||||
self.script_actors.borrow().contains_key(&script_id)
|
||||
}
|
||||
|
||||
pub fn actor_to_script(&self, actor: String) -> String {
|
||||
for (key, value) in self.script_actors.borrow().iter() {
|
||||
println!("checking {:s}", value.as_slice());
|
||||
if value.as_slice() == actor.as_slice() {
|
||||
return key.to_string();
|
||||
}
|
||||
}
|
||||
fail!("couldn't find actor named {:s}", actor)
|
||||
}
|
||||
|
||||
/// Create a unique name based on a monotonically increasing suffix
|
||||
pub fn new_name(&self, prefix: &str) -> String {
|
||||
let suffix = self.next.get();
|
||||
|
|
|
@ -4,9 +4,13 @@
|
|||
|
||||
/// Liberally derived from the [Firefox JS implementation](http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/server/actors/inspector.js).
|
||||
|
||||
use devtools_traits::{GetRootNode, GetDocumentElement, GetChildren, DevtoolScriptControlMsg};
|
||||
use devtools_traits::NodeInfo;
|
||||
|
||||
use actor::{Actor, ActorRegistry};
|
||||
use protocol::JsonPacketSender;
|
||||
|
||||
use servo_msg::constellation_msg::PipelineId;
|
||||
use serialize::json;
|
||||
use std::cell::RefCell;
|
||||
use std::io::TcpStream;
|
||||
|
@ -16,6 +20,8 @@ pub struct InspectorActor {
|
|||
pub walker: RefCell<Option<String>>,
|
||||
pub pageStyle: RefCell<Option<String>>,
|
||||
pub highlighter: RefCell<Option<String>>,
|
||||
pub script_chan: Sender<DevtoolScriptControlMsg>,
|
||||
pub pipeline: PipelineId,
|
||||
}
|
||||
|
||||
#[deriving(Encodable)]
|
||||
|
@ -122,8 +128,59 @@ struct NodeActorMsg {
|
|||
incompleteValue: bool,
|
||||
}
|
||||
|
||||
trait NodeInfoToProtocol {
|
||||
fn encode(self, actors: &ActorRegistry, display: bool) -> NodeActorMsg;
|
||||
}
|
||||
|
||||
impl NodeInfoToProtocol for NodeInfo {
|
||||
fn encode(self, actors: &ActorRegistry, display: bool) -> NodeActorMsg {
|
||||
let actor_name = if !actors.script_actor_registered(self.uniqueId.clone()) {
|
||||
let name = actors.new_name("node");
|
||||
actors.register_script_actor(self.uniqueId, name.clone());
|
||||
name
|
||||
} else {
|
||||
actors.script_to_actor(self.uniqueId)
|
||||
};
|
||||
|
||||
NodeActorMsg {
|
||||
actor: actor_name,
|
||||
baseURI: self.baseURI,
|
||||
parent: actors.script_to_actor(self.parent.clone()),
|
||||
nodeType: self.nodeType,
|
||||
namespaceURI: self.namespaceURI,
|
||||
nodeName: self.nodeName,
|
||||
numChildren: self.numChildren,
|
||||
|
||||
name: self.name,
|
||||
publicId: self.publicId,
|
||||
systemId: self.systemId,
|
||||
|
||||
attrs: self.attrs.move_iter().map(|attr| {
|
||||
AttrMsg {
|
||||
namespace: attr.namespace,
|
||||
name: attr.name,
|
||||
value: attr.value,
|
||||
}
|
||||
}).collect(),
|
||||
|
||||
pseudoClassLocks: vec!(), //TODO get this data from script
|
||||
|
||||
isDisplayed: display,
|
||||
|
||||
hasEventListeners: false, //TODO get this data from script
|
||||
|
||||
isDocumentElement: self.isDocumentElement,
|
||||
|
||||
shortValue: self.shortValue,
|
||||
incompleteValue: self.incompleteValue,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct WalkerActor {
|
||||
name: String,
|
||||
script_chan: Sender<DevtoolScriptControlMsg>,
|
||||
pipeline: PipelineId,
|
||||
}
|
||||
|
||||
#[deriving(Encodable)]
|
||||
|
@ -156,9 +213,9 @@ impl Actor for WalkerActor {
|
|||
}
|
||||
|
||||
fn handle_message(&self,
|
||||
_registry: &ActorRegistry,
|
||||
registry: &ActorRegistry,
|
||||
msg_type: &String,
|
||||
_msg: &json::Object,
|
||||
msg: &json::Object,
|
||||
stream: &mut TcpStream) -> bool {
|
||||
match msg_type.as_slice() {
|
||||
"querySelector" => {
|
||||
|
@ -170,38 +227,15 @@ impl Actor for WalkerActor {
|
|||
}
|
||||
|
||||
"documentElement" => {
|
||||
let (tx, rx) = channel();
|
||||
self.script_chan.send(GetDocumentElement(self.pipeline, tx));
|
||||
let doc_elem_info = rx.recv();
|
||||
|
||||
let node = doc_elem_info.encode(registry, true);
|
||||
|
||||
let msg = DocumentElementReply {
|
||||
from: self.name(),
|
||||
node: NodeActorMsg {
|
||||
actor: "node0".to_string(),
|
||||
baseURI: "".to_string(),
|
||||
parent: "".to_string(),
|
||||
nodeType: 1, //ELEMENT_NODE
|
||||
namespaceURI: "".to_string(),
|
||||
nodeName: "html".to_string(),
|
||||
numChildren: 0,
|
||||
|
||||
name: "".to_string(),
|
||||
publicId: "".to_string(),
|
||||
systemId: "".to_string(),
|
||||
|
||||
attrs: vec!(AttrMsg {
|
||||
namespace: "".to_string(),
|
||||
name: "manifest".to_string(),
|
||||
value: "foo.manifest".to_string(),
|
||||
}),
|
||||
|
||||
pseudoClassLocks: vec!(),
|
||||
|
||||
isDisplayed: true,
|
||||
|
||||
hasEventListeners: false,
|
||||
|
||||
isDocumentElement: true,
|
||||
|
||||
shortValue: "".to_string(),
|
||||
incompleteValue: false,
|
||||
}
|
||||
node: node,
|
||||
};
|
||||
stream.write_json_packet(&msg);
|
||||
true
|
||||
|
@ -216,10 +250,19 @@ impl Actor for WalkerActor {
|
|||
}
|
||||
|
||||
"children" => {
|
||||
let target = msg.find(&"node".to_string()).unwrap().as_string().unwrap();
|
||||
let (tx, rx) = channel();
|
||||
self.script_chan.send(GetChildren(self.pipeline,
|
||||
registry.actor_to_script(target.to_string()),
|
||||
tx));
|
||||
let children = rx.recv();
|
||||
|
||||
let msg = ChildrenReply {
|
||||
hasFirst: true,
|
||||
hasLast: true,
|
||||
nodes: vec!(),
|
||||
nodes: children.move_iter().map(|child| {
|
||||
child.encode(registry, true)
|
||||
}).collect(),
|
||||
from: self.name(),
|
||||
};
|
||||
stream.write_json_packet(&msg);
|
||||
|
@ -231,26 +274,6 @@ impl Actor for WalkerActor {
|
|||
}
|
||||
}
|
||||
|
||||
struct NodeActor {
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl Actor for NodeActor {
|
||||
fn name(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
|
||||
fn handle_message(&self,
|
||||
_registry: &ActorRegistry,
|
||||
msg_type: &String,
|
||||
_msg: &json::Object,
|
||||
_stream: &mut TcpStream) -> bool {
|
||||
match msg_type.as_slice() {
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Encodable)]
|
||||
struct GetPageStyleReply {
|
||||
from: String,
|
||||
|
@ -367,50 +390,19 @@ impl Actor for InspectorActor {
|
|||
if self.walker.borrow().is_none() {
|
||||
let walker = WalkerActor {
|
||||
name: registry.new_name("walker"),
|
||||
script_chan: self.script_chan.clone(),
|
||||
pipeline: self.pipeline,
|
||||
};
|
||||
let mut walker_name = self.walker.borrow_mut();
|
||||
*walker_name = Some(walker.name());
|
||||
registry.register_later(box walker);
|
||||
}
|
||||
|
||||
let node = NodeActor {
|
||||
name: registry.new_name("node"),
|
||||
};
|
||||
let node_actor_name = node.name();
|
||||
registry.register_later(box node);
|
||||
let (tx, rx) = channel();
|
||||
self.script_chan.send(GetRootNode(self.pipeline, tx));
|
||||
let root_info = rx.recv();
|
||||
|
||||
//TODO: query script for actual root node
|
||||
//TODO: extra node actor creation
|
||||
let node = NodeActorMsg {
|
||||
actor: node_actor_name,
|
||||
baseURI: "".to_string(),
|
||||
parent: "".to_string(),
|
||||
nodeType: 1, //ELEMENT_NODE
|
||||
namespaceURI: "".to_string(),
|
||||
nodeName: "html".to_string(),
|
||||
numChildren: 1,
|
||||
|
||||
name: "".to_string(),
|
||||
publicId: "".to_string(),
|
||||
systemId: "".to_string(),
|
||||
|
||||
attrs: vec!(AttrMsg {
|
||||
namespace: "".to_string(),
|
||||
name: "manifest".to_string(),
|
||||
value: "foo.manifest".to_string(),
|
||||
}),
|
||||
|
||||
pseudoClassLocks: vec!(),
|
||||
|
||||
isDisplayed: true,
|
||||
|
||||
hasEventListeners: false,
|
||||
|
||||
isDocumentElement: true,
|
||||
|
||||
shortValue: "".to_string(),
|
||||
incompleteValue: false,
|
||||
};
|
||||
let node = root_info.encode(registry, false);
|
||||
|
||||
let msg = GetWalkerReply {
|
||||
from: self.name(),
|
||||
|
|
|
@ -142,7 +142,7 @@ fn run_server(port: Receiver<DevtoolsControlMsg>) {
|
|||
let (tab, console, inspector) = {
|
||||
let console = ConsoleActor {
|
||||
name: actors.new_name("console"),
|
||||
script_chan: sender,
|
||||
script_chan: sender.clone(),
|
||||
pipeline: pipeline,
|
||||
};
|
||||
let inspector = InspectorActor {
|
||||
|
@ -150,6 +150,8 @@ fn run_server(port: Receiver<DevtoolsControlMsg>) {
|
|||
walker: RefCell::new(None),
|
||||
pageStyle: RefCell::new(None),
|
||||
highlighter: RefCell::new(None),
|
||||
script_chan: sender,
|
||||
pipeline: pipeline,
|
||||
};
|
||||
//TODO: send along the current page title and URL
|
||||
let tab = TabActor {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue