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

@ -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>, ()>>) {