aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom
diff options
context:
space:
mode:
authorJosh Matthews <josh@joshmatthews.net>2015-02-01 19:39:34 +0100
committerJosh Matthews <josh@joshmatthews.net>2015-02-04 13:35:05 +0000
commitd2444dd370ec68c9012b4e026946d298e77ce900 (patch)
tree888de6a87df82b433e0c477abaae2c750a61c590 /components/script/dom
parent824709f178d2853b64f90f2c6e05a8c555fdcf17 (diff)
downloadservo-d2444dd370ec68c9012b4e026946d298e77ce900.tar.gz
servo-d2444dd370ec68c9012b4e026946d298e77ce900.zip
Implement document.cookies.
Diffstat (limited to 'components/script/dom')
-rw-r--r--components/script/dom/document.rs36
-rw-r--r--components/script/dom/webidls/Document.webidl2
2 files changed, 37 insertions, 1 deletions
diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs
index a8161c62640..0b136459cd8 100644
--- a/components/script/dom/document.rs
+++ b/components/script/dom/document.rs
@@ -19,7 +19,7 @@ use dom::bindings::codegen::InheritTypes::{HTMLAreaElementDerived, HTMLEmbedElem
use dom::bindings::codegen::InheritTypes::{HTMLFormElementDerived, HTMLImageElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLScriptElementDerived};
use dom::bindings::error::{ErrorResult, Fallible};
-use dom::bindings::error::Error::{NotSupported, InvalidCharacter};
+use dom::bindings::error::Error::{NotSupported, InvalidCharacter, Security};
use dom::bindings::error::Error::{HierarchyRequest, NamespaceError};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{MutNullableJS, JS, JSRef, LayoutJS, Temporary, TemporaryPushable};
@@ -54,6 +54,8 @@ use dom::range::Range;
use dom::treewalker::TreeWalker;
use dom::uievent::UIEvent;
use dom::window::{Window, WindowHelpers};
+use net::resource_task::ControlMsg::{SetCookiesForUrl, GetCookiesForUrl};
+use net::cookie_storage::CookieSource::NonHTTP;
use util::namespace;
use util::str::{DOMString, split_html_space_chars};
@@ -68,6 +70,7 @@ use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::ascii::AsciiExt;
use std::cell::{Cell, Ref};
use std::default::Default;
+use std::sync::mpsc::channel;
use time;
#[derive(PartialEq)]
@@ -1004,7 +1007,38 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
Temporary::new(self.window)
}
+ // https://html.spec.whatwg.org/multipage/dom.html#dom-document-cookie
+ fn GetCookie(self) -> Fallible<DOMString> {
+ //TODO: return empty string for cookie-averse Document
+ let url = self.url();
+ if !is_scheme_host_port_tuple(&url) {
+ return Err(Security);
+ }
+ let window = self.window.root();
+ let page = window.page();
+ let (tx, rx) = channel();
+ let _ = page.resource_task.send(GetCookiesForUrl(url, tx, NonHTTP));
+ let cookies = rx.recv().unwrap();
+ Ok(cookies.unwrap_or("".to_owned()))
+ }
+
+ // https://html.spec.whatwg.org/multipage/dom.html#dom-document-cookie
+ fn SetCookie(self, cookie: DOMString) -> ErrorResult {
+ //TODO: ignore for cookie-averse Document
+ let url = self.url();
+ if !is_scheme_host_port_tuple(&url) {
+ return Err(Security);
+ }
+ let window = self.window.root();
+ let page = window.page();
+ let _ = page.resource_task.send(SetCookiesForUrl(url, cookie, NonHTTP));
+ Ok(())
+ }
+
global_event_handlers!();
event_handler!(readystatechange, GetOnreadystatechange, SetOnreadystatechange);
}
+fn is_scheme_host_port_tuple(url: &Url) -> bool {
+ url.host().is_some() && url.port_or_default().is_some()
+}
diff --git a/components/script/dom/webidls/Document.webidl b/components/script/dom/webidls/Document.webidl
index 92b7f35c34b..26d2fa03163 100644
--- a/components/script/dom/webidls/Document.webidl
+++ b/components/script/dom/webidls/Document.webidl
@@ -65,6 +65,8 @@ partial interface Document {
readonly attribute DocumentReadyState readyState;
readonly attribute DOMString lastModified;
readonly attribute Location location;
+ [Throws]
+ attribute DOMString cookie;
// DOM tree accessors
[SetterThrows]