mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Auto merge of #10826 - danlrobertson:cookies, r=asajeffrey
Add webdriver commands for (Get|Add)Cookie Add the webdriver commands for GetCookie and AddCookie. In addition the basic messages for sending cookie data back and forth from the resource thread needed to be created and the handlers for those messages were created as well. Do we plan to have some sort of test suite for the webdriver at some point? At this point I primarily test my PRs with basic shell scripts with a lot of `curl` and `jq`. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10826) <!-- Reviewable:end -->
This commit is contained in:
commit
054bb381e9
19 changed files with 214 additions and 23 deletions
|
@ -22,6 +22,7 @@ app_units = {version = "0.2.3", features = ["plugins"]}
|
|||
bitflags = "0.7"
|
||||
canvas_traits = {path = "../canvas_traits"}
|
||||
caseless = "0.1.0"
|
||||
cookie = { version = "0.2.5", features = ["serialize-serde", "serialize-rustc" ] }
|
||||
cssparser = {version = "0.5.4", features = ["heap_size", "serde-serialization"]}
|
||||
devtools_traits = {path = "../devtools_traits"}
|
||||
encoding = "0.2"
|
||||
|
|
|
@ -1562,7 +1562,7 @@ impl Document {
|
|||
}
|
||||
|
||||
/// https://html.spec.whatwg.org/multipage/#cookie-averse-document-object
|
||||
fn is_cookie_averse(&self) -> bool {
|
||||
pub fn is_cookie_averse(&self) -> bool {
|
||||
self.browsing_context.is_none() || !url_has_network_scheme(&self.url)
|
||||
}
|
||||
|
||||
|
|
|
@ -36,6 +36,7 @@ extern crate app_units;
|
|||
extern crate bitflags;
|
||||
extern crate canvas_traits;
|
||||
extern crate caseless;
|
||||
extern crate cookie as cookie_rs;
|
||||
extern crate core;
|
||||
#[macro_use]
|
||||
extern crate cssparser;
|
||||
|
|
|
@ -1059,6 +1059,8 @@ impl ScriptThread {
|
|||
fn handle_webdriver_msg(&self, pipeline_id: PipelineId, msg: WebDriverScriptCommand) {
|
||||
let context = self.root_browsing_context();
|
||||
match msg {
|
||||
WebDriverScriptCommand::AddCookie(params, reply) =>
|
||||
webdriver_handlers::handle_add_cookie(&context, pipeline_id, params, reply),
|
||||
WebDriverScriptCommand::ExecuteScript(script, reply) =>
|
||||
webdriver_handlers::handle_execute_script(&context, pipeline_id, script, reply),
|
||||
WebDriverScriptCommand::FindElementCSS(selector, reply) =>
|
||||
|
@ -1069,6 +1071,10 @@ impl ScriptThread {
|
|||
webdriver_handlers::handle_focus_element(&context, pipeline_id, element_id, reply),
|
||||
WebDriverScriptCommand::GetActiveElement(reply) =>
|
||||
webdriver_handlers::handle_get_active_element(&context, pipeline_id, reply),
|
||||
WebDriverScriptCommand::GetCookies(reply) =>
|
||||
webdriver_handlers::handle_get_cookies(&context, pipeline_id, reply),
|
||||
WebDriverScriptCommand::GetCookie(name, reply) =>
|
||||
webdriver_handlers::handle_get_cookie(&context, pipeline_id, name, reply),
|
||||
WebDriverScriptCommand::GetElementTagName(node_id, reply) =>
|
||||
webdriver_handlers::handle_get_name(&context, pipeline_id, node_id, reply),
|
||||
WebDriverScriptCommand::GetElementAttribute(node_id, name, reply) =>
|
||||
|
|
|
@ -2,6 +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 cookie_rs::Cookie;
|
||||
use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyleDeclarationMethods;
|
||||
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
|
||||
use dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
|
||||
|
@ -26,12 +27,16 @@ use dom::window::ScriptHelpers;
|
|||
use euclid::point::Point2D;
|
||||
use euclid::rect::Rect;
|
||||
use euclid::size::Size2D;
|
||||
use ipc_channel::ipc::IpcSender;
|
||||
use ipc_channel::ipc::{self, IpcSender};
|
||||
use js::jsapi::JSContext;
|
||||
use js::jsapi::{HandleValue, RootedValue};
|
||||
use js::jsval::UndefinedValue;
|
||||
use msg::constellation_msg::PipelineId;
|
||||
use msg::webdriver_msg::WebDriverCookieError;
|
||||
use msg::webdriver_msg::{WebDriverFrameId, WebDriverJSError, WebDriverJSResult, WebDriverJSValue};
|
||||
use net_traits::CookieSource::{HTTP, NonHTTP};
|
||||
use net_traits::CoreResourceMsg::{GetCookiesDataForUrl, SetCookiesForUrlWithData};
|
||||
use net_traits::IpcSend;
|
||||
use script_thread::get_browsing_context;
|
||||
use url::Url;
|
||||
|
||||
|
@ -177,6 +182,66 @@ pub fn handle_get_active_element(context: &BrowsingContext,
|
|||
|elem| elem.upcast::<Node>().unique_id())).unwrap();
|
||||
}
|
||||
|
||||
pub fn handle_get_cookies(context: &BrowsingContext,
|
||||
_pipeline: PipelineId,
|
||||
reply: IpcSender<Vec<Cookie>>) {
|
||||
let document = context.active_document();
|
||||
let url = document.url();
|
||||
let (sender, receiver) = ipc::channel().unwrap();
|
||||
let _ = document.window().resource_threads().send(
|
||||
GetCookiesDataForUrl(url.clone(), sender, NonHTTP)
|
||||
);
|
||||
let cookies = 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,
|
||||
name: String,
|
||||
reply: IpcSender<Vec<Cookie>>) {
|
||||
let document = context.active_document();
|
||||
let url = document.url();
|
||||
let (sender, receiver) = ipc::channel().unwrap();
|
||||
let _ = document.window().resource_threads().send(
|
||||
GetCookiesDataForUrl(url.clone(), sender, NonHTTP)
|
||||
);
|
||||
let cookies = 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,
|
||||
cookie: Cookie,
|
||||
reply: IpcSender<Result<(), WebDriverCookieError>>) {
|
||||
let document = context.active_document();
|
||||
let url = document.url();
|
||||
let method = if cookie.httponly {
|
||||
HTTP
|
||||
} else {
|
||||
NonHTTP
|
||||
};
|
||||
reply.send(match (document.is_cookie_averse(), cookie.domain.clone()) {
|
||||
(true, _) => Err(WebDriverCookieError::InvalidDomain),
|
||||
(false, Some(ref domain)) if url.host_str().map(|x| { x == &**domain }).unwrap_or(false) => {
|
||||
let _ = document.window().resource_threads().send(
|
||||
SetCookiesForUrlWithData(url.clone(), cookie, method)
|
||||
);
|
||||
Ok(())
|
||||
},
|
||||
(false, None) => {
|
||||
let _ = document.window().resource_threads().send(
|
||||
SetCookiesForUrlWithData(url.clone(), cookie, method)
|
||||
);
|
||||
Ok(())
|
||||
},
|
||||
(_, _) => {
|
||||
Err(WebDriverCookieError::UnableToSetCookie)
|
||||
},
|
||||
}).unwrap();
|
||||
}
|
||||
|
||||
pub fn handle_get_title(context: &BrowsingContext, _pipeline: PipelineId, reply: IpcSender<String>) {
|
||||
reply.send(String::from(context.active_document().Title())).unwrap();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue