Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M1453: Review for GetCachedMessages #4175

Closed
wants to merge 9 commits into from

Implemented modifyAttribute message handling

  • Loading branch information
sam0rai9 committed Nov 22, 2014
commit 1ee80eeee255403caf1e03c03e6eabb477a0a6e1
@@ -5,7 +5,8 @@
/// 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::{GetLayout, NodeInfo};
use devtools_traits::{GetLayout, NodeInfo, ModifyAttribute};
use devtools_traits::Modification;

use actor::{Actor, ActorRegistry};
use protocol::JsonPacketSender;
@@ -41,6 +42,12 @@ struct HighlighterActor {
name: String,
}

pub struct NodeActor {
pub name: String,
script_chan: Sender<DevtoolScriptControlMsg>,
pipeline: PipelineId,
}

#[deriving(Encodable)]
struct ShowBoxModelReply {
from: String,
@@ -83,6 +90,38 @@ impl Actor for HighlighterActor {
}
}

impl Actor for NodeActor {
fn name(&self) -> String {
self.name.clone()
}

fn handle_message(&self,
_registry: &ActorRegistry,
msg_type: &String,
_msg: &json::JsonObject,
stream: &mut TcpStream) -> bool {
match msg_type.as_slice() {
"modifyAttributes" => {
let target = _msg.find(&"to".to_string()).unwrap().as_string().unwrap();
let _mods = _msg.find(&"modifications".to_string()).unwrap().as_list().unwrap();

let mut modifications: Vec<Modification> = Vec::new();
for json_mod in _mods.iter() {
let modification: Modification = json::decode(json_mod.to_string().as_slice()).unwrap();
modifications.push(modification);
}

self.script_chan.send(ModifyAttribute(self.pipeline,
_registry.actor_to_script(target.to_string()),
modifications));
true
}

_ => false,
}
}
}

#[deriving(Encodable)]
struct GetWalkerReply {
from: String,
@@ -131,14 +170,22 @@ struct NodeActorMsg {
}

trait NodeInfoToProtocol {
fn encode(self, actors: &ActorRegistry, display: bool) -> NodeActorMsg;
fn encode(self, actors: &ActorRegistry, display: bool,
script_chan: Sender<DevtoolScriptControlMsg>, pipeline: PipelineId) -> NodeActorMsg;
}

impl NodeInfoToProtocol for NodeInfo {
fn encode(self, actors: &ActorRegistry, display: bool) -> NodeActorMsg {
fn encode(self, actors: &ActorRegistry, display: bool,
script_chan: Sender<DevtoolScriptControlMsg>, pipeline: PipelineId) -> NodeActorMsg {
let actor_name = if !actors.script_actor_registered(self.uniqueId.clone()) {
let name = actors.new_name("node");
let node_actor = NodeActor {
name: name.clone(),
script_chan: script_chan,
pipeline: pipeline.clone(),
};
actors.register_script_actor(self.uniqueId, name.clone());
actors.register_later(box node_actor);
name
} else {
actors.script_to_actor(self.uniqueId)
@@ -232,8 +279,7 @@ impl Actor for WalkerActor {
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 node = doc_elem_info.encode(registry, true, self.script_chan.clone(), self.pipeline);

let msg = DocumentElementReply {
from: self.name(),
@@ -263,7 +309,7 @@ impl Actor for WalkerActor {
hasFirst: true,
hasLast: true,
nodes: children.into_iter().map(|child| {
child.encode(registry, true)
child.encode(registry, true, self.script_chan.clone(), self.pipeline)
}).collect(),
from: self.name(),
};
@@ -452,7 +498,7 @@ impl Actor for InspectorActor {
self.script_chan.send(GetRootNode(self.pipeline, tx));
let root_info = rx.recv();

let node = root_info.encode(registry, false);
let node = root_info.encode(registry, false, self.script_chan.clone(), self.pipeline);

let msg = GetWalkerReply {
from: self.name(),
@@ -11,11 +11,12 @@
#![allow(non_snake_case)]

extern crate "msg" as servo_msg;

extern crate serialize;
/// This module contains shared types and messages for use by devtools/script.
/// The traits are here instead of in script so that the devtools crate can be
/// modified independently of the rest of Servo.

use serialize::{Decodable, Decoder};
use servo_msg::constellation_msg::PipelineId;

pub type DevtoolsControlChan = Sender<DevtoolsControlMsg>;
@@ -73,6 +74,7 @@ pub enum DevtoolScriptControlMsg {
GetDocumentElement(PipelineId, Sender<NodeInfo>),
GetChildren(PipelineId, String, Sender<Vec<NodeInfo>>),
GetLayout(PipelineId, String, Sender<(f32, f32)>),
ModifyAttribute(PipelineId, String, Vec<Modification>),
}

/// Messages to instruct devtools server to update its state relating to a particular
@@ -81,3 +83,23 @@ pub enum ScriptDevtoolControlMsg {
/// Report a new JS error message
ReportConsoleMsg(String),
}

#[deriving(Encodable)]
pub struct Modification{
pub attributeName: String,
pub newValue: Option<String>,
}

impl<D:Decoder<E>, E> Decodable<D, E> for Modification {
fn decode(d: &mut D) -> Result<Modification, E> {
d.read_struct("Modification", 2u, |d|
Ok(Modification {
attributeName: try!(d.read_struct_field("attributeName", 0u, |d| Decodable::decode(d))),
newValue: match d.read_struct_field("newValue", 1u, |d| Decodable::decode(d)) {
Ok(opt) => opt,
Err(_) => None
}
})
)
}
}
@@ -13,6 +13,7 @@ use dom::bindings::codegen::Bindings::EventTargetBinding::EventTargetMethods;
use dom::bindings::codegen::InheritTypes::{EventTargetCast, NodeCast, EventCast, ElementCast};
use dom::bindings::conversions;
use dom::bindings::conversions::{FromJSValConvertible, Empty};
use dom::bindings::error::Error;
use dom::bindings::global;
use dom::bindings::js::{JS, JSRef, RootCollection, Temporary, OptionalRootable};
use dom::bindings::trace::JSTraceable;
@@ -37,7 +38,8 @@ use timers::TimerId;
use devtools_traits;
use devtools_traits::{DevtoolsControlChan, DevtoolsControlPort, NewGlobal, NodeInfo, GetRootNode};
use devtools_traits::{DevtoolScriptControlMsg, EvaluateJS, EvaluateJSReply, GetDocumentElement};
use devtools_traits::{GetChildren, GetLayout};
use devtools_traits::{GetChildren, GetLayout, ModifyAttribute};
use devtools_traits::Modification;
use script_traits::{CompositorEvent, ResizeEvent, ReflowEvent, ClickEvent, MouseDownEvent};
use script_traits::{MouseMoveEvent, MouseUpEvent, ConstellationControlMsg, ScriptTaskFactory};
use script_traits::{ResizeMsg, AttachLayoutMsg, LoadMsg, SendEventMsg, ResizeInactiveMsg};
@@ -542,6 +544,7 @@ impl ScriptTask {
FromDevtools(GetDocumentElement(id, reply)) => self.handle_get_document_element(id, reply),
FromDevtools(GetChildren(id, node_id, reply)) => self.handle_get_children(id, node_id, reply),
FromDevtools(GetLayout(id, node_id, reply)) => self.handle_get_layout(id, node_id, reply),
FromDevtools(ModifyAttribute(id, node_id, modifications)) => self.handle_modify_attribute(id, node_id, modifications),
}
}

@@ -623,6 +626,26 @@ impl ScriptTask {
reply.send((rect.Width(), rect.Height()));
}

fn handle_modify_attribute(&self, pipeline: PipelineId, node_id: String, modifications: Vec<Modification>) {
let node = self.find_node_by_unique_id(pipeline, node_id).root();
let elem: JSRef<Element> = ElementCast::to_ref(*node).expect("should be getting layout of element");

for _modification in modifications.iter(){
match _modification.newValue {
Some(ref string) => {
let result: Result<(), Error> = elem.SetAttribute(_modification.attributeName.clone(), string.clone());
let result = match result {
Ok(result) => result,
Err(e) => {
//TBD: Error handling
},
};
},
None => elem.RemoveAttribute(_modification.attributeName.clone()),
}
}
}

fn handle_new_layout(&self, new_layout_info: NewLayoutInfo) {
debug!("Script: new layout: {:?}", new_layout_info);
let NewLayoutInfo {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.