servo: Merge #12552 - History interface Go, Back, and Forward (from cbrewster:history_interface); r=asajeffrey
authorConnor Brewster <connor.brewster@eagles.oc.edu>
Fri, 22 Jul 2016 15:34:14 -0500
changeset 339350 dcfc8c3cbd73cec1b952c90f296a729483e1b942
parent 339349 a2215c75c4c5c781961ea3be033afb7f70bb5ca2
child 339351 c5b2df42cc2597371a2a113f0e9023511c542500
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersasajeffrey
servo: Merge #12552 - History interface Go, Back, and Forward (from cbrewster:history_interface); r=asajeffrey <!-- Please describe your changes on the following line: --> r? @asajeffrey --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #5670 (github issue number if applicable). <!-- Either: --> - [X] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> implement go, forward, back Source-Repo: https://github.com/servo/servo Source-Revision: 0e887ca8d3327b63db43cc329dd83bf4c02e2a1c
servo/components/script/dom/history.rs
servo/components/script/dom/mod.rs
servo/components/script/dom/webidls/History.webidl
servo/components/script/dom/webidls/Window.webidl
servo/components/script/dom/window.rs
new file mode 100644
--- /dev/null
+++ b/servo/components/script/dom/history.rs
@@ -0,0 +1,70 @@
+/* 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 dom::bindings::codegen::Bindings::HistoryBinding;
+use dom::bindings::codegen::Bindings::HistoryBinding::HistoryMethods;
+use dom::bindings::codegen::Bindings::LocationBinding::LocationMethods;
+use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
+use dom::bindings::global::GlobalRef;
+use dom::bindings::js::{JS, Root};
+use dom::bindings::reflector::{Reflector, reflect_dom_object};
+use dom::window::Window;
+use msg::constellation_msg::TraversalDirection;
+use script_traits::ScriptMsg as ConstellationMsg;
+
+// https://html.spec.whatwg.org/multipage/#the-history-interface
+#[dom_struct]
+pub struct History {
+    reflector_: Reflector,
+    window: JS<Window>,
+}
+
+impl History {
+    pub fn new_inherited(window: &Window) -> History {
+        History {
+            reflector_: Reflector::new(),
+            window: JS::from_ref(&window),
+        }
+    }
+
+    pub fn new(window: &Window) -> Root<History> {
+        reflect_dom_object(box History::new_inherited(window),
+                           GlobalRef::Window(window),
+                           HistoryBinding::Wrap)
+    }
+}
+
+impl History {
+    fn traverse_history(&self, direction: TraversalDirection) {
+        let pipeline = self.window.pipeline();
+        let msg = ConstellationMsg::TraverseHistory(Some(pipeline), direction);
+        let _ = self.window.constellation_chan().send(msg);
+    }
+}
+
+impl HistoryMethods for History {
+    // https://html.spec.whatwg.org/multipage/#dom-history-go
+    fn Go(&self, delta: i32) {
+        let direction = if delta > 0 {
+            TraversalDirection::Forward(delta as usize)
+        } else if delta < 0 {
+            TraversalDirection::Back(-delta as usize)
+        } else {
+            self.window.Location().Reload();
+            return;
+        };
+
+        self.traverse_history(direction);
+    }
+
+    // https://html.spec.whatwg.org/multipage/#dom-history-back
+    fn Back(&self) {
+        self.traverse_history(TraversalDirection::Back(1));
+    }
+
+    // https://html.spec.whatwg.org/multipage/#dom-history-forward
+    fn Forward(&self) {
+        self.traverse_history(TraversalDirection::Forward(1));
+    }
+}
--- a/servo/components/script/dom/mod.rs
+++ b/servo/components/script/dom/mod.rs
@@ -265,16 +265,17 @@ pub mod eventtarget;
 pub mod file;
 pub mod filelist;
 pub mod filereader;
 pub mod focusevent;
 pub mod forcetouchevent;
 pub mod formdata;
 pub mod hashchangeevent;
 pub mod headers;
+pub mod history;
 pub mod htmlanchorelement;
 pub mod htmlappletelement;
 pub mod htmlareaelement;
 pub mod htmlaudioelement;
 pub mod htmlbaseelement;
 pub mod htmlbodyelement;
 pub mod htmlbrelement;
 pub mod htmlbuttonelement;
new file mode 100644
--- /dev/null
+++ b/servo/components/script/dom/webidls/History.webidl
@@ -0,0 +1,18 @@
+/* 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/. */
+
+// enum ScrollRestoration { "auto", "manual" };
+
+// https://html.spec.whatwg.org/multipage/#the-history-interface
+[Exposed=(Window,Worker)]
+interface History {
+  // readonly attribute unsigned long length;
+  // attribute ScrollRestoration scrollRestoration;
+  // readonly attribute any state;
+  void go(optional long delta = 0);
+  void back();
+  void forward();
+  // void pushState(any data, DOMString title, optional USVString? url = null);
+  // void replaceState(any data, DOMString title, optional USVString? url = null);
+};
--- a/servo/components/script/dom/webidls/Window.webidl
+++ b/servo/components/script/dom/webidls/Window.webidl
@@ -6,17 +6,17 @@
 [PrimaryGlobal, Exposed=(Window,Worker)]
 /*sealed*/ interface Window : EventTarget {
   // the current browsing context
   [Unforgeable] readonly attribute WindowProxy window;
   [BinaryName="Self_", Replaceable] readonly attribute WindowProxy self;
   [Unforgeable] readonly attribute Document document;
   //         attribute DOMString name;
   [/*PutForwards=href, */Unforgeable] readonly attribute Location location;
-  //readonly attribute History history;
+  readonly attribute History history;
   //[Replaceable] readonly attribute BarProp locationbar;
   //[Replaceable] readonly attribute BarProp menubar;
   //[Replaceable] readonly attribute BarProp personalbar;
   //[Replaceable] readonly attribute BarProp scrollbars;
   //[Replaceable] readonly attribute BarProp statusbar;
   //[Replaceable] readonly attribute BarProp toolbar;
   attribute DOMString status;
   void close();
--- a/servo/components/script/dom/window.rs
+++ b/servo/components/script/dom/window.rs
@@ -25,16 +25,17 @@ use dom::bindings::utils::{GlobalStaticD
 use dom::browsingcontext::BrowsingContext;
 use dom::console::Console;
 use dom::crypto::Crypto;
 use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration};
 use dom::document::Document;
 use dom::element::Element;
 use dom::event::Event;
 use dom::eventtarget::EventTarget;
+use dom::history::History;
 use dom::htmliframeelement::build_mozbrowser_custom_event;
 use dom::location::Location;
 use dom::navigator::Navigator;
 use dom::node::{Node, from_untrusted_node_address, window_from_node};
 use dom::performance::Performance;
 use dom::screen::Screen;
 use dom::storage::Storage;
 use euclid::{Point2D, Rect, Size2D};
@@ -153,16 +154,17 @@ pub struct Window {
     console: MutNullableHeap<JS<Console>>,
     crypto: MutNullableHeap<JS<Crypto>>,
     navigator: MutNullableHeap<JS<Navigator>>,
     #[ignore_heap_size_of = "channels are hard"]
     image_cache_thread: ImageCacheThread,
     #[ignore_heap_size_of = "channels are hard"]
     image_cache_chan: ImageCacheChan,
     browsing_context: MutNullableHeap<JS<BrowsingContext>>,
+    history: MutNullableHeap<JS<History>>,
     performance: MutNullableHeap<JS<Performance>>,
     navigation_start: u64,
     navigation_start_precise: f64,
     screen: MutNullableHeap<JS<Screen>>,
     session_storage: MutNullableHeap<JS<Storage>>,
     local_storage: MutNullableHeap<JS<Storage>>,
     status: DOMRefCell<DOMString>,
     #[ignore_heap_size_of = "channels are hard"]
@@ -470,16 +472,21 @@ impl WindowMethods for Window {
         self.main_thread_script_chan().send(MainThreadScriptMsg::ExitWindow(self.id.clone())).unwrap();
     }
 
     // https://html.spec.whatwg.org/multipage/#dom-document-2
     fn Document(&self) -> Root<Document> {
         self.browsing_context().active_document()
     }
 
+    // https://html.spec.whatwg.org/multipage/#dom-history
+    fn History(&self) -> Root<History> {
+        self.history.or_init(|| History::new(self))
+    }
+
     // https://html.spec.whatwg.org/multipage/#dom-location
     fn Location(&self) -> Root<Location> {
         self.Document().GetLocation().unwrap()
     }
 
     // https://html.spec.whatwg.org/multipage/#dom-sessionstorage
     fn SessionStorage(&self) -> Root<Storage> {
         self.session_storage.or_init(|| Storage::new(&GlobalRef::Window(self), StorageType::Session))
@@ -1643,16 +1650,17 @@ impl Window {
             image_cache_chan: image_cache_chan,
             console: Default::default(),
             crypto: Default::default(),
             navigator: Default::default(),
             image_cache_thread: image_cache_thread,
             mem_profiler_chan: mem_profiler_chan,
             time_profiler_chan: time_profiler_chan,
             devtools_chan: devtools_chan,
+            history: Default::default(),
             browsing_context: Default::default(),
             performance: Default::default(),
             navigation_start: (current_time.sec * 1000 + current_time.nsec as i64 / 1000000) as u64,
             navigation_start_precise: time::precise_time_ns() as f64,
             screen: Default::default(),
             session_storage: Default::default(),
             local_storage: Default::default(),
             status: DOMRefCell::new(DOMString::new()),