aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/origin.rs
diff options
context:
space:
mode:
authorJosh Matthews <josh@joshmatthews.net>2015-09-11 12:24:25 -0400
committerMs2ger <Ms2ger@gmail.com>2016-04-13 10:37:55 +0200
commita8233a135ec37bca77c238f78eea0c454af4c175 (patch)
treeb005a24eb2576cfe8a868dc3ed88f1eea06d9914 /components/script/origin.rs
parente8e354d5d3e6757c16912e7f5e6234ec5eece493 (diff)
downloadservo-a8233a135ec37bca77c238f78eea0c454af4c175.tar.gz
servo-a8233a135ec37bca77c238f78eea0c454af4c175.zip
Implement origin concept.
Diffstat (limited to 'components/script/origin.rs')
-rw-r--r--components/script/origin.rs73
1 files changed, 73 insertions, 0 deletions
diff --git a/components/script/origin.rs b/components/script/origin.rs
new file mode 100644
index 00000000000..096ffbbd6fb
--- /dev/null
+++ b/components/script/origin.rs
@@ -0,0 +1,73 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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 std::cell::RefCell;
+use std::rc::Rc;
+use url::{OpaqueOrigin, Origin as UrlOrigin};
+use url::{Url, Host};
+
+/// A representation of an [origin](https://html.spec.whatwg.org/multipage/#origin-2).
+#[derive(HeapSizeOf)]
+pub struct Origin {
+ #[ignore_heap_size_of = "Rc<T> has unclear ownership semantics"]
+ inner: Rc<RefCell<UrlOrigin>>,
+}
+
+// We can't use RefCell inside JSTraceable, but Origin doesn't contain JS values and
+// DOMRefCell makes it much harder to write unit tests (due to setting up required TLS).
+no_jsmanaged_fields!(Origin);
+
+impl Origin {
+ /// Create a new origin comprising a unique, opaque identifier.
+ pub fn opaque_identifier() -> Origin {
+ let opaque = UrlOrigin::UID(OpaqueOrigin::new());
+ Origin {
+ inner: Rc::new(RefCell::new(opaque)),
+ }
+ }
+
+ /// Create a new origin for the given URL.
+ pub fn new(url: &Url) -> Origin {
+ Origin {
+ inner: Rc::new(RefCell::new(url.origin())),
+ }
+ }
+
+ pub fn set(&self, origin: UrlOrigin) {
+ *self.inner.borrow_mut() = origin;
+ }
+
+ /// Does this origin represent a host/scheme/port tuple?
+ pub fn is_scheme_host_port_tuple(&self) -> bool {
+ match *self.inner.borrow() {
+ UrlOrigin::Tuple(..) => true,
+ UrlOrigin::UID(..) => false,
+ }
+ }
+
+ /// Return the host associated with this origin.
+ pub fn host(&self) -> Option<Host> {
+ match *self.inner.borrow() {
+ UrlOrigin::Tuple(_, ref host, _) => Some(host.clone()),
+ UrlOrigin::UID(..) => None,
+ }
+ }
+
+ /// https://html.spec.whatwg.org/multipage/#same-origin
+ pub fn same_origin(&self, other: &Origin) -> bool {
+ *self.inner.borrow() == *other.inner.borrow()
+ }
+
+ pub fn copy(&self) -> Origin {
+ Origin {
+ inner: Rc::new(RefCell::new(self.inner.borrow().clone())),
+ }
+ }
+
+ pub fn alias(&self) -> Origin {
+ Origin {
+ inner: self.inner.clone(),
+ }
+ }
+}