mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
247 lines
8.8 KiB
Rust
247 lines
8.8 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::LocationBinding::LocationMethods;
|
|
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
|
|
use dom::bindings::error::{Error, ErrorResult, Fallible};
|
|
use dom::bindings::inheritance::Castable;
|
|
use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
|
|
use dom::bindings::root::{Dom, DomRoot};
|
|
use dom::bindings::str::{DOMString, USVString};
|
|
use dom::bindings::structuredclone::StructuredCloneData;
|
|
use dom::eventtarget::EventTarget;
|
|
use dom::globalscope::GlobalScope;
|
|
use dom::popstateevent::PopStateEvent;
|
|
use dom::window::Window;
|
|
use dom_struct::dom_struct;
|
|
use js::jsapi::{Heap, JSContext};
|
|
use js::jsval::{JSVal, NullValue, UndefinedValue};
|
|
use js::rust::HandleValue;
|
|
use msg::constellation_msg::{HistoryStateId, TraversalDirection};
|
|
use net_traits::{CoreResourceMsg, IpcSend};
|
|
use profile_traits::ipc;
|
|
use profile_traits::ipc::channel;
|
|
use script_traits::ScriptMsg;
|
|
use std::cell::Cell;
|
|
|
|
enum PushOrReplace {
|
|
Push,
|
|
Replace,
|
|
}
|
|
|
|
// https://html.spec.whatwg.org/multipage/#the-history-interface
|
|
#[dom_struct]
|
|
pub struct History {
|
|
reflector_: Reflector,
|
|
window: Dom<Window>,
|
|
state: Heap<JSVal>,
|
|
state_id: Cell<Option<HistoryStateId>>,
|
|
}
|
|
|
|
impl History {
|
|
pub fn new_inherited(window: &Window) -> History {
|
|
let state = Heap::default();
|
|
state.set(NullValue());
|
|
History {
|
|
reflector_: Reflector::new(),
|
|
window: Dom::from_ref(&window),
|
|
state: state,
|
|
state_id: Cell::new(None),
|
|
}
|
|
}
|
|
|
|
pub fn new(window: &Window) -> DomRoot<History> {
|
|
reflect_dom_object(Box::new(History::new_inherited(window)),
|
|
window,
|
|
HistoryBinding::Wrap)
|
|
}
|
|
}
|
|
|
|
impl History {
|
|
fn traverse_history(&self, direction: TraversalDirection) -> ErrorResult {
|
|
if !self.window.Document().is_fully_active() {
|
|
return Err(Error::Security);
|
|
}
|
|
let msg = ScriptMsg::TraverseHistory(direction);
|
|
let _ = self.window.upcast::<GlobalScope>().script_to_constellation_chan().send(msg);
|
|
Ok(())
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
pub fn activate_state(&self, state_id: Option<HistoryStateId>) {
|
|
self.state_id.set(state_id);
|
|
let serialized_data = match state_id {
|
|
Some(state_id) => {
|
|
let (tx, rx) = ipc::channel(self.global().time_profiler_chan().clone()).unwrap();
|
|
let _ = self.window
|
|
.upcast::<GlobalScope>()
|
|
.resource_threads()
|
|
.send(CoreResourceMsg::GetHistoryState(state_id, tx));
|
|
rx.recv().unwrap()
|
|
},
|
|
None => None,
|
|
};
|
|
|
|
match serialized_data {
|
|
Some(serialized_data) => {
|
|
let global_scope = self.window.upcast::<GlobalScope>();
|
|
rooted!(in(global_scope.get_cx()) let mut state = UndefinedValue());
|
|
StructuredCloneData::Vector(serialized_data).read(&global_scope, state.handle_mut());
|
|
self.state.set(state.get());
|
|
},
|
|
None => {
|
|
self.state.set(NullValue());
|
|
}
|
|
}
|
|
|
|
unsafe {
|
|
PopStateEvent::dispatch_jsval(self.window.upcast::<EventTarget>(), &*self.window, self.state.handle());
|
|
}
|
|
}
|
|
|
|
// https://html.spec.whatwg.org/multipage/#dom-history-pushstate
|
|
// https://html.spec.whatwg.org/multipage/#dom-history-replacestate
|
|
fn push_or_replace_state(&self,
|
|
cx: *mut JSContext,
|
|
data: HandleValue,
|
|
_title: DOMString,
|
|
_url: Option<USVString>,
|
|
push_or_replace: PushOrReplace) -> ErrorResult {
|
|
// Step 1
|
|
let document = self.window.Document();
|
|
|
|
// Step 2
|
|
if !document.is_fully_active() {
|
|
return Err(Error::Security);
|
|
}
|
|
|
|
// TODO: Step 3 Optionally abort these steps
|
|
// https://github.com/servo/servo/issues/19159
|
|
|
|
// TODO: Step 4
|
|
|
|
// Step 5
|
|
let serialized_data = StructuredCloneData::write(cx, data)?.move_to_arraybuffer();
|
|
|
|
// TODO: Steps 6-7 Url Handling
|
|
// https://github.com/servo/servo/issues/19157
|
|
|
|
// Step 8
|
|
let state_id = match push_or_replace {
|
|
PushOrReplace::Push => {
|
|
let state_id = HistoryStateId::new();
|
|
self.state_id.set(Some(state_id));
|
|
let msg = ScriptMsg::PushHistoryState(state_id);
|
|
let _ = self.window.upcast::<GlobalScope>().script_to_constellation_chan().send(msg);
|
|
state_id
|
|
},
|
|
PushOrReplace::Replace => {
|
|
let state_id = match self.state_id.get() {
|
|
Some(state_id) => state_id,
|
|
None => {
|
|
let state_id = HistoryStateId::new();
|
|
self.state_id.set(Some(state_id));
|
|
state_id
|
|
},
|
|
};
|
|
let msg = ScriptMsg::ReplaceHistoryState(state_id);
|
|
let _ = self.window.upcast::<GlobalScope>().script_to_constellation_chan().send(msg);
|
|
state_id
|
|
},
|
|
};
|
|
|
|
let _ = self.window
|
|
.upcast::<GlobalScope>()
|
|
.resource_threads()
|
|
.send(CoreResourceMsg::SetHistoryState(state_id, serialized_data.clone()));
|
|
|
|
|
|
// TODO: Step 9 Update current entry to represent a GET request
|
|
// https://github.com/servo/servo/issues/19156
|
|
|
|
// TODO: Step 10 Set document's URL to new URL
|
|
// https://github.com/servo/servo/issues/19157
|
|
|
|
// Step 11
|
|
let global_scope = self.window.upcast::<GlobalScope>();
|
|
rooted!(in(cx) let mut state = UndefinedValue());
|
|
StructuredCloneData::Vector(serialized_data).read(&global_scope, state.handle_mut());
|
|
|
|
// Step 12
|
|
self.state.set(state.get());
|
|
|
|
// TODO: Step 13 Update Document's latest entry to current entry
|
|
// https://github.com/servo/servo/issues/19158
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl HistoryMethods for History {
|
|
// https://html.spec.whatwg.org/multipage/#dom-history-state
|
|
#[allow(unsafe_code)]
|
|
unsafe fn GetState(&self, _cx: *mut JSContext) -> Fallible<JSVal> {
|
|
if !self.window.Document().is_fully_active() {
|
|
return Err(Error::Security);
|
|
}
|
|
Ok(self.state.get())
|
|
}
|
|
|
|
// https://html.spec.whatwg.org/multipage/#dom-history-length
|
|
fn GetLength(&self) -> Fallible<u32> {
|
|
if !self.window.Document().is_fully_active() {
|
|
return Err(Error::Security);
|
|
}
|
|
let (sender, recv) =
|
|
channel(self.global().time_profiler_chan().clone()).expect("Failed to create channel to send jsh length.");
|
|
let msg = ScriptMsg::JointSessionHistoryLength(sender);
|
|
let _ = self.window.upcast::<GlobalScope>().script_to_constellation_chan().send(msg);
|
|
Ok(recv.recv().unwrap())
|
|
}
|
|
|
|
// https://html.spec.whatwg.org/multipage/#dom-history-go
|
|
fn Go(&self, delta: i32) -> ErrorResult {
|
|
let direction = if delta > 0 {
|
|
TraversalDirection::Forward(delta as usize)
|
|
} else if delta < 0 {
|
|
TraversalDirection::Back(-delta as usize)
|
|
} else {
|
|
return self.window.Location().Reload();
|
|
};
|
|
|
|
self.traverse_history(direction)
|
|
}
|
|
|
|
// https://html.spec.whatwg.org/multipage/#dom-history-back
|
|
fn Back(&self) -> ErrorResult {
|
|
self.traverse_history(TraversalDirection::Back(1))
|
|
}
|
|
|
|
// https://html.spec.whatwg.org/multipage/#dom-history-forward
|
|
fn Forward(&self) -> ErrorResult {
|
|
self.traverse_history(TraversalDirection::Forward(1))
|
|
}
|
|
|
|
// https://html.spec.whatwg.org/multipage/#dom-history-pushstate
|
|
#[allow(unsafe_code)]
|
|
unsafe fn PushState(&self,
|
|
cx: *mut JSContext,
|
|
data: HandleValue,
|
|
title: DOMString,
|
|
url: Option<USVString>) -> ErrorResult {
|
|
self.push_or_replace_state(cx, data, title, url, PushOrReplace::Push)
|
|
}
|
|
|
|
// https://html.spec.whatwg.org/multipage/#dom-history-replacestate
|
|
#[allow(unsafe_code)]
|
|
unsafe fn ReplaceState(&self,
|
|
cx: *mut JSContext,
|
|
data: HandleValue,
|
|
title: DOMString,
|
|
url: Option<USVString>) -> ErrorResult {
|
|
self.push_or_replace_state(cx, data, title, url, PushOrReplace::Replace)
|
|
}
|
|
}
|