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.
This commit is contained in:
Dan Robertson 2016-06-25 20:06:17 +00:00
parent 7d978e7b3d
commit 246723114f
No known key found for this signature in database
GPG key ID: 1BE34CFA559DED58
19 changed files with 214 additions and 23 deletions

View file

@ -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();
}