Add basic support for executeAsyncScript.

This relies on a global webdriverCallback function, which is visible to content.
Obviously that's not a long term solution for a number of reasons, but it allows
us to experiment for now
This commit is contained in:
James Graham 2015-04-29 19:49:38 +01:00
parent 98cb65ca0a
commit 8d10fa1f2d
6 changed files with 96 additions and 35 deletions

View file

@ -56,6 +56,9 @@
//void postMessage(any message, DOMString targetOrigin, optional sequence<Transferable> transfer);
// Shouldn't be public, but just to make things work for now
void webdriverCallback(optional any result);
// also has obsolete members
};
Window implements GlobalEventHandlers;

View file

@ -35,8 +35,10 @@ use script_task::{TimerSource, ScriptChan, ScriptPort, NonWorkerScriptChan};
use script_task::ScriptMsg;
use script_traits::ScriptControlChan;
use timers::{IsInterval, TimerId, TimerManager, TimerCallback};
use webdriver_handlers::jsval_to_webdriver;
use devtools_traits::{DevtoolsControlChan, TimelineMarker, TimelineMarkerType, TracingMetadata};
use webdriver_traits::EvaluateJSReply;
use msg::compositor_msg::ScriptListener;
use msg::constellation_msg::{LoadData, PipelineId, SubpageId, ConstellationChan, WindowSizeData, WorkerId};
use net_traits::ResourceTask;
@ -166,6 +168,9 @@ pub struct Window {
/// A counter of the number of pending reflows for this window.
pending_reflow_count: Cell<u32>,
/// A channel for communicating results of async scripts back to the webdriver server
webdriver_script_chan: RefCell<Option<Sender<Result<EvaluateJSReply, ()>>>>
}
impl Window {
@ -483,6 +488,17 @@ impl<'a> WindowMethods for JSRef<'a, Window> {
let doc = self.Document().root();
doc.r().cancel_animation_frame(ident);
}
fn WebdriverCallback(self, cx: *mut JSContext, val: JSVal) {
let rv = jsval_to_webdriver(cx, val);
{
let opt_chan = self.webdriver_script_chan.borrow();
if let Some(ref chan) = *opt_chan {
chan.send(rv).unwrap();
}
}
self.set_webdriver_script_chan(None);
}
}
pub trait WindowHelpers {
@ -523,6 +539,7 @@ pub trait WindowHelpers {
fn emit_timeline_marker(self, marker: TimelineMarker);
fn set_devtools_timeline_marker(self, marker: TimelineMarkerType, reply: Sender<TimelineMarker>);
fn drop_devtools_timeline_markers(self);
fn set_webdriver_script_chan(self, chan: Option<Sender<Result<EvaluateJSReply, ()>>>);
}
pub trait ScriptHelpers {
@ -880,6 +897,10 @@ impl<'a> WindowHelpers for JSRef<'a, Window> {
self.devtools_markers.borrow_mut().clear();
*self.devtools_marker_sender.borrow_mut() = None;
}
fn set_webdriver_script_chan(self, chan: Option<Sender<Result<EvaluateJSReply, ()>>>) {
*self.webdriver_script_chan.borrow_mut() = chan;
}
}
impl Window {
@ -947,6 +968,7 @@ impl Window {
devtools_marker_sender: RefCell::new(None),
devtools_markers: RefCell::new(HashSet::new()),
devtools_wants_updates: Cell::new(false),
webdriver_script_chan: RefCell::new(None),
};
WindowBinding::Wrap(runtime.cx(), win)

View file

@ -316,7 +316,7 @@ pub struct ScriptTask {
/// The JavaScript runtime.
js_runtime: Rc<Runtime>,
mouse_over_targets: DOMRefCell<Vec<JS<Node>>>
mouse_over_targets: DOMRefCell<Vec<JS<Node>>>,
}
/// In the event of task failure, all data on the stack runs its destructor. However, there
@ -814,7 +814,9 @@ impl ScriptTask {
WebDriverScriptCommand::GetElementText(node_id, reply) =>
webdriver_handlers::handle_get_text(&page, pipeline_id, node_id, reply),
WebDriverScriptCommand::GetTitle(reply) =>
webdriver_handlers::handle_get_title(&page, pipeline_id, reply)
webdriver_handlers::handle_get_title(&page, pipeline_id, reply),
WebDriverScriptCommand::ExecuteAsyncScript(script, reply) =>
webdriver_handlers::handle_execute_async_script(&page, pipeline_id, script, reply),
}
}

View file

@ -12,8 +12,10 @@ use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::codegen::Bindings::NodeListBinding::NodeListMethods;
use dom::bindings::js::{OptionalRootable, Rootable, Temporary};
use dom::node::{Node, NodeHelpers};
use dom::window::ScriptHelpers;
use dom::window::{ScriptHelpers, WindowHelpers};
use dom::document::DocumentHelpers;
use js::jsapi::JSContext;
use js::jsval::JSVal;
use page::Page;
use msg::constellation_msg::PipelineId;
use script_task::get_page;
@ -35,26 +37,38 @@ fn find_node_by_unique_id(page: &Rc<Page>, pipeline: PipelineId, node_id: String
None
}
pub fn jsval_to_webdriver(cx: *mut JSContext, val: JSVal) -> Result<EvaluateJSReply, ()> {
if val.is_undefined() {
Ok(EvaluateJSReply::VoidValue)
} else if val.is_boolean() {
Ok(EvaluateJSReply::BooleanValue(val.to_boolean()))
} else if val.is_double() {
Ok(EvaluateJSReply::NumberValue(FromJSValConvertible::from_jsval(cx, val, ()).unwrap()))
} else if val.is_string() {
//FIXME: use jsstring_to_str when jsval grows to_jsstring
Ok(EvaluateJSReply::StringValue(FromJSValConvertible::from_jsval(cx, val, StringificationBehavior::Default).unwrap()))
} else if val.is_null() {
Ok(EvaluateJSReply::NullValue)
} else {
Err(())
}
}
pub fn handle_execute_script(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<Result<EvaluateJSReply, ()>>) {
let page = get_page(&*page, pipeline);
let window = page.window().root();
let cx = window.r().get_cx();
let rval = window.r().evaluate_js_on_global_with_result(&eval);
reply.send(if rval.is_undefined() {
Ok(EvaluateJSReply::VoidValue)
} else if rval.is_boolean() {
Ok(EvaluateJSReply::BooleanValue(rval.to_boolean()))
} else if rval.is_double() {
Ok(EvaluateJSReply::NumberValue(FromJSValConvertible::from_jsval(cx, rval, ()).unwrap()))
} else if rval.is_string() {
//FIXME: use jsstring_to_str when jsval grows to_jsstring
Ok(EvaluateJSReply::StringValue(FromJSValConvertible::from_jsval(cx, rval, StringificationBehavior::Default).unwrap()))
} else if rval.is_null() {
Ok(EvaluateJSReply::NullValue)
} else {
Err(())
}).unwrap();
reply.send(jsval_to_webdriver(cx, rval)).unwrap();
}
pub fn handle_execute_async_script(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<Result<EvaluateJSReply, ()>>) {
let page = get_page(&*page, pipeline);
let window = page.window().root();
window.r().set_webdriver_script_chan(Some(reply));
window.r().evaluate_js_on_global_with_result(&eval);
}
pub fn handle_find_element_css(page: &Rc<Page>, _pipeline: PipelineId, selector: String, reply: Sender<Result<Option<String>, ()>>) {