diff options
author | Alan Jeffrey <ajeffrey@mozilla.com> | 2016-10-07 15:52:49 -0500 |
---|---|---|
committer | Alan Jeffrey <ajeffrey@mozilla.com> | 2016-11-08 10:48:58 -0600 |
commit | 90e9c910f4338adb01c322034a2cd3230e7e9b8c (patch) | |
tree | 72f56740559e86e4d900329680d1cd221a58b5b0 /components/script/webdriver_handlers.rs | |
parent | dae007fd1634bcf1545e67abaa7746fa95f10e94 (diff) | |
download | servo-90e9c910f4338adb01c322034a2cd3230e7e9b8c.tar.gz servo-90e9c910f4338adb01c322034a2cd3230e7e9b8c.zip |
Replace script thread root browsing context by a collection of documents.
Diffstat (limited to 'components/script/webdriver_handlers.rs')
-rw-r--r-- | components/script/webdriver_handlers.rs | 201 |
1 files changed, 102 insertions, 99 deletions
diff --git a/components/script/webdriver_handlers.rs b/components/script/webdriver_handlers.rs index bcb0186d8e6..331f0fbbf8c 100644 --- a/components/script/webdriver_handlers.rs +++ b/components/script/webdriver_handlers.rs @@ -10,20 +10,18 @@ use dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods; use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods; use dom::bindings::codegen::Bindings::HTMLOptionElementBinding::HTMLOptionElementMethods; use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; -use dom::bindings::codegen::Bindings::NodeListBinding::NodeListMethods; use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods; use dom::bindings::conversions::{ConversionResult, FromJSValConvertible, StringificationBehavior}; use dom::bindings::inheritance::Castable; use dom::bindings::js::Root; use dom::bindings::str::DOMString; -use dom::browsingcontext::BrowsingContext; use dom::element::Element; use dom::globalscope::GlobalScope; use dom::htmlelement::HTMLElement; use dom::htmliframeelement::HTMLIFrameElement; use dom::htmlinputelement::HTMLInputElement; use dom::htmloptionelement::HTMLOptionElement; -use dom::node::Node; +use dom::node::{Node, window_from_node}; use euclid::point::Point2D; use euclid::rect::Rect; use euclid::size::Size2D; @@ -35,21 +33,18 @@ use msg::constellation_msg::PipelineId; use net_traits::CookieSource::{HTTP, NonHTTP}; use net_traits::CoreResourceMsg::{GetCookiesDataForUrl, SetCookiesForUrlWithData}; use net_traits::IpcSend; +use script_thread::Documents; use script_traits::webdriver_msg::{WebDriverFrameId, WebDriverJSError, WebDriverJSResult, WebDriverJSValue}; use script_traits::webdriver_msg::WebDriverCookieError; use url::Url; -fn find_node_by_unique_id(context: &BrowsingContext, +fn find_node_by_unique_id(documents: &Documents, pipeline: PipelineId, node_id: String) -> Option<Root<Node>> { - let context = match context.find(pipeline) { - Some(context) => context, - None => return None - }; - - let document = context.active_document(); - document.upcast::<Node>().traverse_preorder().find(|candidate| candidate.unique_id() == node_id) + documents.find_document(pipeline).and_then(|document| + document.upcast::<Node>().traverse_preorder().find(|candidate| candidate.unique_id() == node_id) + ) } #[allow(unsafe_code)] @@ -79,16 +74,15 @@ pub unsafe fn jsval_to_webdriver(cx: *mut JSContext, val: HandleValue) -> WebDri } #[allow(unsafe_code)] -pub fn handle_execute_script(context: &BrowsingContext, +pub fn handle_execute_script(documents: &Documents, pipeline: PipelineId, eval: String, reply: IpcSender<WebDriverJSResult>) { - let context = match context.find(pipeline) { - Some(context) => context, + let window = match documents.find_window(pipeline) { + Some(window) => window, None => return reply.send(Err(WebDriverJSError::BrowsingContextNotFound)).unwrap() }; - let window = context.active_window(); let result = unsafe { let cx = window.get_cx(); rooted!(in(cx) let mut rval = UndefinedValue()); @@ -96,27 +90,26 @@ pub fn handle_execute_script(context: &BrowsingContext, &eval, rval.handle_mut()); jsval_to_webdriver(cx, rval.handle()) }; + reply.send(result).unwrap(); } -pub fn handle_execute_async_script(context: &BrowsingContext, +pub fn handle_execute_async_script(documents: &Documents, pipeline: PipelineId, eval: String, reply: IpcSender<WebDriverJSResult>) { - let context = match context.find(pipeline) { - Some(context) => context, + let window = match documents.find_window(pipeline) { + Some(window) => window, None => return reply.send(Err(WebDriverJSError::BrowsingContextNotFound)).unwrap() - }; + }; - let window = context.active_window(); let cx = window.get_cx(); window.set_webdriver_script_chan(Some(reply)); rooted!(in(cx) let mut rval = UndefinedValue()); - window.upcast::<GlobalScope>().evaluate_js_on_global_with_result( - &eval, rval.handle_mut()); + window.upcast::<GlobalScope>().evaluate_js_on_global_with_result(&eval, rval.handle_mut()); } -pub fn handle_get_frame_id(context: &BrowsingContext, +pub fn handle_get_frame_id(documents: &Documents, pipeline: PipelineId, webdriver_frame_id: WebDriverFrameId, reply: IpcSender<Result<Option<PipelineId>, ()>>) { @@ -126,7 +119,7 @@ pub fn handle_get_frame_id(context: &BrowsingContext, Ok(None) }, WebDriverFrameId::Element(x) => { - match find_node_by_unique_id(context, pipeline, x) { + match find_node_by_unique_id(documents, pipeline, x) { Some(ref node) => { match node.downcast::<HTMLIFrameElement>() { Some(ref elem) => Ok(elem.get_content_window()), @@ -137,8 +130,7 @@ pub fn handle_get_frame_id(context: &BrowsingContext, } }, WebDriverFrameId::Parent => { - let window = context.active_window(); - Ok(window.parent()) + documents.find_window(pipeline).map(|window| window.parent()).ok_or(()) } }; @@ -146,41 +138,31 @@ pub fn handle_get_frame_id(context: &BrowsingContext, reply.send(frame_id).unwrap() } -pub fn handle_find_element_css(context: &BrowsingContext, _pipeline: PipelineId, selector: String, +pub fn handle_find_element_css(documents: &Documents, pipeline: PipelineId, selector: String, reply: IpcSender<Result<Option<String>, ()>>) { - reply.send(match context.active_document().QuerySelector(DOMString::from(selector)) { - Ok(node) => { - Ok(node.map(|x| x.upcast::<Node>().unique_id())) - } - Err(_) => Err(()) - }).unwrap(); + let node_id = documents.find_document(pipeline) + .ok_or(()) + .and_then(|doc| doc.QuerySelector(DOMString::from(selector)).map_err(|_| ())) + .map(|node| node.map(|x| x.upcast::<Node>().unique_id())); + reply.send(node_id).unwrap(); } -pub fn handle_find_elements_css(context: &BrowsingContext, - _pipeline: PipelineId, +pub fn handle_find_elements_css(documents: &Documents, + pipeline: PipelineId, selector: String, reply: IpcSender<Result<Vec<String>, ()>>) { - reply.send(match context.active_document().QuerySelectorAll(DOMString::from(selector)) { - Ok(ref nodes) => { - let mut result = Vec::with_capacity(nodes.Length() as usize); - for i in 0..nodes.Length() { - if let Some(ref node) = nodes.Item(i) { - result.push(node.unique_id()); - } - } - Ok(result) - }, - Err(_) => { - Err(()) - } - }).unwrap(); + let node_ids = documents.find_document(pipeline) + .ok_or(()) + .and_then(|doc| doc.QuerySelectorAll(DOMString::from(selector)).map_err(|_| ())) + .map(|nodes| nodes.iter().map(|x| x.upcast::<Node>().unique_id()).collect()); + reply.send(node_ids).unwrap(); } -pub fn handle_focus_element(context: &BrowsingContext, +pub fn handle_focus_element(documents: &Documents, pipeline: PipelineId, element_id: String, reply: IpcSender<Result<(), ()>>) { - reply.send(match find_node_by_unique_id(context, pipeline, element_id) { + reply.send(match find_node_by_unique_id(documents, pipeline, element_id) { Some(ref node) => { match node.downcast::<HTMLElement>() { Some(ref elem) => { @@ -195,47 +177,62 @@ pub fn handle_focus_element(context: &BrowsingContext, }).unwrap(); } -pub fn handle_get_active_element(context: &BrowsingContext, - _pipeline: PipelineId, +pub fn handle_get_active_element(documents: &Documents, + pipeline: PipelineId, reply: IpcSender<Option<String>>) { - reply.send(context.active_document().GetActiveElement().map( - |elem| elem.upcast::<Node>().unique_id())).unwrap(); + reply.send(documents.find_document(pipeline) + .and_then(|doc| doc.GetActiveElement()) + .map(|elem| elem.upcast::<Node>().unique_id())).unwrap(); } -pub fn handle_get_cookies(context: &BrowsingContext, - _pipeline: PipelineId, - reply: IpcSender<Vec<Serde<Cookie>>>) { - let document = context.active_document(); - let url = document.url(); - let (sender, receiver) = ipc::channel().unwrap(); - let _ = document.window().upcast::<GlobalScope>().resource_threads().send( - GetCookiesDataForUrl(url.clone(), sender, NonHTTP) - ); - let cookies = receiver.recv().unwrap(); +pub fn handle_get_cookies(documents: &Documents, + pipeline: PipelineId, + reply: IpcSender<Vec<Serde<Cookie>>>) { + // TODO: Return an error if the pipeline doesn't exist? + let cookies: Vec<Serde<Cookie>> = match documents.find_document(pipeline) { + None => Vec::new(), + Some(document) => { + let url = document.url(); + let (sender, receiver) = ipc::channel().unwrap(); + let _ = document.window().upcast::<GlobalScope>().resource_threads().send( + GetCookiesDataForUrl(url.clone(), sender, NonHTTP) + ); + receiver.recv().unwrap() + }, + }; reply.send(cookies).unwrap(); } // https://w3c.github.io/webdriver/webdriver-spec.html#get-cookie -pub fn handle_get_cookie(context: &BrowsingContext, - _pipeline: PipelineId, +pub fn handle_get_cookie(documents: &Documents, + pipeline: PipelineId, name: String, reply: IpcSender<Vec<Serde<Cookie>>>) { - let document = context.active_document(); - let url = document.url(); - let (sender, receiver) = ipc::channel().unwrap(); - let _ = document.window().upcast::<GlobalScope>().resource_threads().send( - GetCookiesDataForUrl(url.clone(), sender, NonHTTP) - ); - let cookies = receiver.recv().unwrap(); + // TODO: Return an error if the pipeline doesn't exist? + let cookies: Vec<Serde<Cookie>> = match documents.find_document(pipeline) { + None => Vec::new(), + Some(document) => { + let url = document.url(); + let (sender, receiver) = ipc::channel().unwrap(); + let _ = document.window().upcast::<GlobalScope>().resource_threads().send( + GetCookiesDataForUrl(url.clone(), sender, NonHTTP) + ); + receiver.recv().unwrap() + }, + }; reply.send(cookies.into_iter().filter(|c| c.name == &*name).collect()).unwrap(); } // https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie -pub fn handle_add_cookie(context: &BrowsingContext, - _pipeline: PipelineId, +pub fn handle_add_cookie(documents: &Documents, + pipeline: PipelineId, cookie: Cookie, reply: IpcSender<Result<(), WebDriverCookieError>>) { - let document = context.active_document(); + // TODO: Return a different error if the pipeline doesn't exist? + let document = match documents.find_document(pipeline) { + Some(document) => document, + None => return reply.send(Err(WebDriverCookieError::UnableToSetCookie)).unwrap(), + }; let url = document.url(); let method = if cookie.httponly { HTTP @@ -262,15 +259,19 @@ pub fn handle_add_cookie(context: &BrowsingContext, }).unwrap(); } -pub fn handle_get_title(context: &BrowsingContext, _pipeline: PipelineId, reply: IpcSender<String>) { - reply.send(String::from(context.active_document().Title())).unwrap(); +pub fn handle_get_title(documents: &Documents, pipeline: PipelineId, reply: IpcSender<String>) { + // TODO: Return an error if the pipeline doesn't exist. + let title = documents.find_document(pipeline) + .map(|doc| String::from(doc.Title())) + .unwrap_or_default(); + reply.send(title).unwrap(); } -pub fn handle_get_rect(context: &BrowsingContext, +pub fn handle_get_rect(documents: &Documents, pipeline: PipelineId, element_id: String, reply: IpcSender<Result<Rect<f64>, ()>>) { - reply.send(match find_node_by_unique_id(context, pipeline, element_id) { + reply.send(match find_node_by_unique_id(documents, pipeline, element_id) { Some(elem) => { // https://w3c.github.io/webdriver/webdriver-spec.html#dfn-calculate-the-absolute-position match elem.downcast::<HTMLElement>() { @@ -304,11 +305,11 @@ pub fn handle_get_rect(context: &BrowsingContext, }).unwrap(); } -pub fn handle_get_text(context: &BrowsingContext, +pub fn handle_get_text(documents: &Documents, pipeline: PipelineId, node_id: String, reply: IpcSender<Result<String, ()>>) { - reply.send(match find_node_by_unique_id(context, pipeline, node_id) { + reply.send(match find_node_by_unique_id(documents, pipeline, node_id) { Some(ref node) => { Ok(node.GetTextContent().map_or("".to_owned(), String::from)) }, @@ -316,11 +317,11 @@ pub fn handle_get_text(context: &BrowsingContext, }).unwrap(); } -pub fn handle_get_name(context: &BrowsingContext, +pub fn handle_get_name(documents: &Documents, pipeline: PipelineId, node_id: String, reply: IpcSender<Result<String, ()>>) { - reply.send(match find_node_by_unique_id(context, pipeline, node_id) { + reply.send(match find_node_by_unique_id(documents, pipeline, node_id) { Some(node) => { Ok(String::from(node.downcast::<Element>().unwrap().TagName())) }, @@ -328,12 +329,12 @@ pub fn handle_get_name(context: &BrowsingContext, }).unwrap(); } -pub fn handle_get_attribute(context: &BrowsingContext, +pub fn handle_get_attribute(documents: &Documents, pipeline: PipelineId, node_id: String, name: String, reply: IpcSender<Result<Option<String>, ()>>) { - reply.send(match find_node_by_unique_id(context, pipeline, node_id) { + reply.send(match find_node_by_unique_id(documents, pipeline, node_id) { Some(node) => { Ok(node.downcast::<Element>().unwrap().GetAttribute(DOMString::from(name)) .map(String::from)) @@ -342,14 +343,14 @@ pub fn handle_get_attribute(context: &BrowsingContext, }).unwrap(); } -pub fn handle_get_css(context: &BrowsingContext, +pub fn handle_get_css(documents: &Documents, pipeline: PipelineId, node_id: String, name: String, reply: IpcSender<Result<String, ()>>) { - reply.send(match find_node_by_unique_id(context, pipeline, node_id) { + reply.send(match find_node_by_unique_id(documents, pipeline, node_id) { Some(node) => { - let window = context.active_window(); + let window = window_from_node(&*node); let elem = node.downcast::<Element>().unwrap(); Ok(String::from( window.GetComputedStyle(&elem, None).GetPropertyValue(DOMString::from(name)))) @@ -358,19 +359,21 @@ pub fn handle_get_css(context: &BrowsingContext, }).unwrap(); } -pub fn handle_get_url(context: &BrowsingContext, - _pipeline: PipelineId, +pub fn handle_get_url(documents: &Documents, + pipeline: PipelineId, reply: IpcSender<Url>) { - let document = context.active_document(); - let url = document.url(); - reply.send((*url).clone()).unwrap(); + // TODO: Return an error if the pipeline doesn't exist. + let url = documents.find_document(pipeline) + .map(|document| document.url().clone()) + .unwrap_or_else(|| Url::parse("about:blank").expect("infallible")); + reply.send(url).unwrap(); } -pub fn handle_is_enabled(context: &BrowsingContext, +pub fn handle_is_enabled(documents: &Documents, pipeline: PipelineId, element_id: String, reply: IpcSender<Result<bool, ()>>) { - reply.send(match find_node_by_unique_id(&context, pipeline, element_id) { + reply.send(match find_node_by_unique_id(&documents, pipeline, element_id) { Some(ref node) => { match node.downcast::<Element>() { Some(elem) => Ok(elem.enabled_state()), @@ -381,11 +384,11 @@ pub fn handle_is_enabled(context: &BrowsingContext, }).unwrap(); } -pub fn handle_is_selected(context: &BrowsingContext, +pub fn handle_is_selected(documents: &Documents, pipeline: PipelineId, element_id: String, reply: IpcSender<Result<bool, ()>>) { - reply.send(match find_node_by_unique_id(context, pipeline, element_id) { + reply.send(match find_node_by_unique_id(documents, pipeline, element_id) { Some(ref node) => { if let Some(input_element) = node.downcast::<HTMLInputElement>() { Ok(input_element.Checked()) |