Skip to content

Commit

Permalink
Auto merge of #6022 - jgraham:webdriver_execute_async_script, r=jdm
Browse files Browse the repository at this point in the history
<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/6022)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed May 14, 2015
2 parents 7e022b2 + 50f59c8 commit b05c3fc
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 92 deletions.
61 changes: 45 additions & 16 deletions components/compositing/constellation.rs
Expand Up @@ -29,6 +29,7 @@ use msg::constellation_msg::{IFrameSandboxState, MozBrowserEvent, NavigationDire
use msg::constellation_msg::{Key, KeyState, KeyModifiers, LoadData};
use msg::constellation_msg::{SubpageId, WindowSizeData};
use msg::constellation_msg::{self, ConstellationChan, Failure};
use msg::constellation_msg::{WebDriverCommandMsg};
use net_traits::{self, ResourceTask};
use net_traits::image_cache_task::ImageCacheTask;
use net_traits::storage_task::{StorageTask, StorageTaskMsg};
Expand All @@ -49,7 +50,7 @@ use util::geometry::PagePx;
use util::opts;
use util::task::spawn_named;
use clipboard::ClipboardContext;
use webdriver_traits::WebDriverScriptCommand;
use webdriver_traits;

/// Maintains the pipelines and navigation context and grants permission to composite.
///
Expand Down Expand Up @@ -122,6 +123,9 @@ pub struct Constellation<LTF, STF> {

/// Means of accessing the clipboard
clipboard_ctx: ClipboardContext,

/// Bits of state used to interact with the webdriver implementation
webdriver: WebDriverData
}

/// Stores the navigation context for a single frame in the frame tree.
Expand Down Expand Up @@ -185,6 +189,18 @@ pub struct SendableFrameTree {
pub children: Vec<SendableFrameTree>,
}

struct WebDriverData {
load_channel: Option<Sender<webdriver_traits::LoadComplete>>
}

impl WebDriverData {
pub fn new() -> WebDriverData {
WebDriverData {
load_channel: None
}
}
}

#[derive(Clone, Copy)]
enum ExitPipelineMode {
Normal,
Expand Down Expand Up @@ -233,6 +249,7 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
},
phantom: PhantomData,
clipboard_ctx: ClipboardContext::new().unwrap(),
webdriver: WebDriverData::new()
};
constellation.run();
});
Expand Down Expand Up @@ -376,7 +393,7 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
// script, and reflow messages have been sent.
ConstellationMsg::LoadComplete => {
debug!("constellation got load complete message");
self.compositor_proxy.send(CompositorMsg::LoadComplete);
self.handle_load_complete_msg()
}
// Handle a forward or back request
ConstellationMsg::Navigate(pipeline_info, direction) => {
Expand Down Expand Up @@ -426,14 +443,9 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
};
sender.send(result).unwrap();
}
ConstellationMsg::CompositePng(reply) => {
self.compositor_proxy.send(CompositorMsg::CreatePng(reply));
}
ConstellationMsg::WebDriverCommand(pipeline_id,
command) => {
ConstellationMsg::WebDriverCommand(command) => {
debug!("constellation got webdriver command message");
self.handle_webdriver_command_msg(pipeline_id,
command);
self.handle_webdriver_msg(command);
}
ConstellationMsg::ViewportConstrained(pipeline_id, constraints) => {
debug!("constellation got viewport-constrained event message");
Expand Down Expand Up @@ -649,6 +661,14 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
}
}

fn handle_load_complete_msg(&mut self) {
self.compositor_proxy.send(CompositorMsg::LoadComplete);
if let Some(ref reply_chan) = self.webdriver.load_channel {
reply_chan.send(webdriver_traits::LoadComplete).unwrap();
}
self.webdriver.load_channel = None;
}

fn handle_navigate_msg(&mut self,
pipeline_info: Option<(PipelineId, SubpageId)>,
direction: constellation_msg::NavigationDirection) {
Expand Down Expand Up @@ -814,15 +834,24 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
}
}

fn handle_webdriver_command_msg(&mut self,
pipeline_id: PipelineId,
msg: WebDriverScriptCommand) {
fn handle_webdriver_msg(&mut self, msg: WebDriverCommandMsg) {
// Find the script channel for the given parent pipeline,
// and pass the event to that script task.
let pipeline = self.pipeline(pipeline_id);
let control_msg = ConstellationControlMsg::WebDriverCommand(pipeline_id, msg);
let ScriptControlChan(ref script_channel) = pipeline.script_chan;
script_channel.send(control_msg).unwrap();
match msg {
WebDriverCommandMsg::LoadUrl(pipeline_id, load_data, reply) => {
self.handle_load_url_msg(pipeline_id, load_data);
self.webdriver.load_channel = Some(reply);
},
WebDriverCommandMsg::ScriptCommand(pipeline_id, cmd) => {
let pipeline = self.pipeline(pipeline_id);
let control_msg = ConstellationControlMsg::WebDriverScriptCommand(pipeline_id, cmd);
let ScriptControlChan(ref script_channel) = pipeline.script_chan;
script_channel.send(control_msg).unwrap();
},
WebDriverCommandMsg::TakeScreenshot(reply) => {
self.compositor_proxy.send(CompositorMsg::CreatePng(reply));
},
}
}

fn add_or_replace_pipeline_in_frame_tree(&mut self, frame_change: FrameChange) {
Expand Down
11 changes: 7 additions & 4 deletions components/msg/constellation_msg.rs
Expand Up @@ -18,7 +18,7 @@ use util::geometry::{PagePx, ViewportPx};
use std::collections::HashMap;
use std::sync::mpsc::{channel, Sender, Receiver};
use style::viewport::ViewportConstraints;
use webdriver_traits::WebDriverScriptCommand;
use webdriver_traits::{WebDriverScriptCommand, LoadComplete};
use url::Url;

#[derive(Clone)]
Expand Down Expand Up @@ -237,11 +237,9 @@ pub enum Msg {
/// Requests that the constellation retrieve the current contents of the clipboard
GetClipboardContents(Sender<String>),
/// Dispatch a webdriver command
WebDriverCommand(PipelineId, WebDriverScriptCommand),
WebDriverCommand(WebDriverCommandMsg),
/// Notifies the constellation that the viewport has been constrained in some manner
ViewportConstrained(PipelineId, ViewportConstraints),
/// Create a PNG of the window contents
CompositePng(Sender<Option<png::Image>>),
/// Query the constellation to see if the current compositor output is stable
IsReadyToSaveImage(HashMap<PipelineId, Epoch>),
/// Notification that this iframe should be removed.
Expand Down Expand Up @@ -319,6 +317,11 @@ impl MozBrowserEvent {
}
}

pub enum WebDriverCommandMsg {
LoadUrl(PipelineId, LoadData, Sender<LoadComplete>),
ScriptCommand(PipelineId, WebDriverScriptCommand),
TakeScreenshot(Sender<Option<png::Image>>)
}

/// Similar to net::resource_task::LoadData
/// can be passed to LoadUrl to load a page with GET/POST
Expand Down
7 changes: 7 additions & 0 deletions components/script/dom/webidls/Window.webidl
Expand Up @@ -128,6 +128,13 @@ partial interface Window {
};
Window implements OnErrorEventHandlerForWindow;

// WebDriver extensions
partial interface Window {
// Shouldn't be public, but just to make things work for now
void webdriverCallback(optional any result);
void webdriverTimeout();
};

// https://html.spec.whatwg.org/multipage/#dom-sessionstorage
[NoInterfaceObject]
interface WindowSessionStorage {
Expand Down
26 changes: 26 additions & 0 deletions components/script/dom/window.rs
Expand Up @@ -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::{WebDriverJSError, WebDriverJSResult};
use msg::compositor_msg::ScriptListener;
use msg::constellation_msg::{LoadData, PipelineId, SubpageId, ConstellationChan, WindowSizeData, WorkerId};
use net_traits::ResourceTask;
Expand Down Expand Up @@ -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<WebDriverJSResult>>>
}

impl Window {
Expand Down Expand Up @@ -483,6 +488,21 @@ 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_mut().take();
if let Some(chan) = opt_chan {
chan.send(rv).unwrap();
}
}

fn WebdriverTimeout(self) {
let opt_chan = self.webdriver_script_chan.borrow_mut().take();
if let Some(chan) = opt_chan {
chan.send(Err(WebDriverJSError::Timeout)).unwrap();
}
}
}

pub trait WindowHelpers {
Expand Down Expand Up @@ -523,6 +543,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<WebDriverJSResult>>);
}

pub trait ScriptHelpers {
Expand Down Expand Up @@ -880,6 +901,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<WebDriverJSResult>>) {
*self.webdriver_script_chan.borrow_mut() = chan;
}
}

impl Window {
Expand Down Expand Up @@ -947,6 +972,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)
Expand Down
12 changes: 7 additions & 5 deletions components/script/script_task.rs
Expand Up @@ -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
Expand Down Expand Up @@ -726,7 +726,7 @@ impl ScriptTask {
self.handle_update_subpage_id(containing_pipeline_id, old_subpage_id, new_subpage_id),
ConstellationControlMsg::FocusIFrame(containing_pipeline_id, subpage_id) =>
self.handle_focus_iframe_msg(containing_pipeline_id, subpage_id),
ConstellationControlMsg::WebDriverCommand(pipeline_id, msg) =>
ConstellationControlMsg::WebDriverScriptCommand(pipeline_id, msg) =>
self.handle_webdriver_msg(pipeline_id, msg),
ConstellationControlMsg::TickAllAnimations(pipeline_id) =>
self.handle_tick_all_animations(pipeline_id),
Expand Down Expand Up @@ -801,8 +801,8 @@ impl ScriptTask {
fn handle_webdriver_msg(&self, pipeline_id: PipelineId, msg: WebDriverScriptCommand) {
let page = self.root_page();
match msg {
WebDriverScriptCommand::EvaluateJS(script, reply) =>
webdriver_handlers::handle_evaluate_js(&page, pipeline_id, script, reply),
WebDriverScriptCommand::ExecuteScript(script, reply) =>
webdriver_handlers::handle_execute_script(&page, pipeline_id, script, reply),
WebDriverScriptCommand::FindElementCSS(selector, reply) =>
webdriver_handlers::handle_find_element_css(&page, pipeline_id, selector, reply),
WebDriverScriptCommand::FindElementsCSS(selector, reply) =>
Expand All @@ -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),
}
}

Expand Down
47 changes: 30 additions & 17 deletions components/script/webdriver_handlers.rs
Expand Up @@ -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 webdriver_traits::{EvaluateJSReply};
use webdriver_traits::{WebDriverJSValue, WebDriverJSError, WebDriverJSResult};
use dom::bindings::conversions::FromJSValConvertible;
use dom::bindings::conversions::StringificationBehavior;
use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast};
Expand All @@ -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;
Expand All @@ -35,26 +37,37 @@ fn find_node_by_unique_id(page: &Rc<Page>, pipeline: PipelineId, node_id: String
None
}

pub fn handle_evaluate_js(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<Result<EvaluateJSReply, ()>>) {
pub fn jsval_to_webdriver(cx: *mut JSContext, val: JSVal) -> WebDriverJSResult {
if val.is_undefined() {
Ok(WebDriverJSValue::Undefined)
} else if val.is_boolean() {
Ok(WebDriverJSValue::Boolean(val.to_boolean()))
} else if val.is_double() {
Ok(WebDriverJSValue::Number(FromJSValConvertible::from_jsval(cx, val, ()).unwrap()))
} else if val.is_string() {
//FIXME: use jsstring_to_str when jsval grows to_jsstring
Ok(WebDriverJSValue::String(FromJSValConvertible::from_jsval(cx, val, StringificationBehavior::Default).unwrap()))
} else if val.is_null() {
Ok(WebDriverJSValue::Null)
} else {
Err(WebDriverJSError::UnknownType)
}
}

pub fn handle_execute_script(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<WebDriverJSResult>) {
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<WebDriverJSResult>) {
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>, ()>>) {
Expand Down
2 changes: 1 addition & 1 deletion components/script_traits/lib.rs
Expand Up @@ -98,7 +98,7 @@ pub enum ConstellationControlMsg {
/// Set an iframe to be focused. Used when an element in an iframe gains focus.
FocusIFrame(PipelineId, SubpageId),
// Passes a webdriver command to the script task for execution
WebDriverCommand(PipelineId, WebDriverScriptCommand),
WebDriverScriptCommand(PipelineId, WebDriverScriptCommand),
/// Notifies script task that all animations are done
TickAllAnimations(PipelineId),
/// Notifies script that a stylesheet has finished loading.
Expand Down

0 comments on commit b05c3fc

Please sign in to comment.