fune/servo/components/script/dom/history.rs
Anthony Ramine be2b512750 servo: Merge #15715 - Make #[dom_struct] a proc_macro attribute (from nox:custom-derive); r=SimonSapin
The rustup is needed for https://github.com/rust-lang/rust/pull/40039.

Source-Repo: https://github.com/servo/servo
Source-Revision: a204c4176dcccdad8ec99d74055c66794c3f64ba

--HG--
rename : servo/components/domobject_derive/Cargo.toml => servo/components/dom_struct/Cargo.toml
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : 2cad140c3d6b99837f0bd15a6f3ccba0f353e049
2017-02-24 05:56:30 -08:00

84 lines
2.9 KiB
Rust

/* 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::inheritance::Castable;
use dom::bindings::js::{JS, Root};
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::globalscope::GlobalScope;
use dom::window::Window;
use dom_struct::dom_struct;
use ipc_channel::ipc;
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),
window,
HistoryBinding::Wrap)
}
}
impl History {
fn traverse_history(&self, direction: TraversalDirection) {
let global_scope = self.window.upcast::<GlobalScope>();
let pipeline = global_scope.pipeline_id();
let msg = ConstellationMsg::TraverseHistory(Some(pipeline), direction);
let _ = global_scope.constellation_chan().send(msg);
}
}
impl HistoryMethods for History {
// https://html.spec.whatwg.org/multipage/#dom-history-length
fn Length(&self) -> u32 {
let global_scope = self.window.upcast::<GlobalScope>();
let pipeline = global_scope.pipeline_id();
let (sender, recv) = ipc::channel().expect("Failed to create channel to send jsh length.");
let msg = ConstellationMsg::JointSessionHistoryLength(pipeline, sender);
let _ = global_scope.constellation_chan().send(msg);
recv.recv().unwrap()
}
// 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));
}
}