diff options
author | Dan Robertson <drobertson@tripwire.com> | 2016-06-25 20:06:17 +0000 |
---|---|---|
committer | Dan Robertson <drobertson@tripwire.com> | 2016-06-25 22:24:35 +0000 |
commit | 246723114fa54f82643bad827393cf58f1fdeea9 (patch) | |
tree | 31630771bcea6e8d67725beabd8b34a22572aed4 /components/script/webdriver_handlers.rs | |
parent | 7d978e7b3d6b41b234f7c9b4051b746a1fbeddee (diff) | |
download | servo-246723114fa54f82643bad827393cf58f1fdeea9.tar.gz servo-246723114fa54f82643bad827393cf58f1fdeea9.zip |
Use common cookie struct add cookie webdriver cmds
One cookie struct to rule them all. One struct to represent them.
One cookie struct to bind them all, and through the IPC carry them.
Diffstat (limited to 'components/script/webdriver_handlers.rs')
-rw-r--r-- | components/script/webdriver_handlers.rs | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/components/script/webdriver_handlers.rs b/components/script/webdriver_handlers.rs index 3c9d1ffcfb2..1c320b5be06 100644 --- a/components/script/webdriver_handlers.rs +++ b/components/script/webdriver_handlers.rs @@ -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(); } |