diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2018-01-30 14:14:04 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-30 14:14:04 -0600 |
commit | 469dc84b6ea20b6d378771b008101b49f34e1ad6 (patch) | |
tree | d64eaa5cbd3f9349518616a8c4b1c38d812a08dc /components/script/dom/history.rs | |
parent | 86b7af5db55ac340517318c0305dacf00208dd96 (diff) | |
parent | 198ea8f7676d60fdb80b34304da700d51e1e04f8 (diff) | |
download | servo-469dc84b6ea20b6d378771b008101b49f34e1ad6.tar.gz servo-469dc84b6ea20b6d378771b008101b49f34e1ad6.zip |
Auto merge of #19031 - cbrewster:push_replace_state_early, r=asajeffrey
Implement initial part of history.state
<!-- Please describe your changes on the following line: -->
Implements maintaining the current history state. Tracking history state in the session history will be done as a followup.
---
<!-- 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
- [ ] These changes fix #__ (github issue number if applicable).
<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____
<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19031)
<!-- Reviewable:end -->
Diffstat (limited to 'components/script/dom/history.rs')
-rw-r--r-- | components/script/dom/history.rs | 94 |
1 files changed, 93 insertions, 1 deletions
diff --git a/components/script/dom/history.rs b/components/script/dom/history.rs index 71e8c0c7e4e..17ee0d29cb6 100644 --- a/components/script/dom/history.rs +++ b/components/script/dom/history.rs @@ -10,25 +10,38 @@ use dom::bindings::error::{Error, ErrorResult, Fallible}; use dom::bindings::inheritance::Castable; use dom::bindings::reflector::{Reflector, reflect_dom_object}; use dom::bindings::root::{Dom, DomRoot}; +use dom::bindings::str::{DOMString, USVString}; +use dom::bindings::structuredclone::StructuredCloneData; use dom::globalscope::GlobalScope; use dom::window::Window; use dom_struct::dom_struct; use ipc_channel::ipc; +use js::jsapi::{HandleValue, Heap, JSContext}; +use js::jsval::{JSVal, NullValue, UndefinedValue}; use msg::constellation_msg::TraversalDirection; use script_traits::ScriptMsg; +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>, } 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, } } @@ -48,9 +61,68 @@ impl History { let _ = self.window.upcast::<GlobalScope>().script_to_constellation_chan().send(msg); Ok(()) } + + // 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)?; + + // TODO: Steps 6-7 Url Handling + // https://github.com/servo/servo/issues/19157 + + // TODO: Step 8 Push/Replace session history entry + // https://github.com/servo/servo/issues/19156 + + // 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()); + 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() { @@ -60,7 +132,7 @@ impl HistoryMethods for History { 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 { @@ -84,4 +156,24 @@ impl HistoryMethods for History { 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) + } } |