2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "core/frame/History.h"
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "core/dom/Document.h"
31 #include "core/dom/ExceptionCode.h"
32 #include "core/frame/LocalFrame.h"
33 #include "core/loader/DocumentLoader.h"
34 #include "core/loader/FrameLoader.h"
35 #include "core/loader/FrameLoaderClient.h"
36 #include "core/loader/HistoryItem.h"
37 #include "core/loader/NavigationScheduler.h"
38 #include "core/page/Page.h"
39 #include "platform/RuntimeEnabledFeatures.h"
40 #include "platform/weborigin/KURL.h"
41 #include "platform/weborigin/SecurityOrigin.h"
42 #include "wtf/MainThread.h"
46 History::History(LocalFrame
* frame
)
47 : DOMWindowProperty(frame
)
48 , m_lastStateObjectRequested(nullptr)
54 DOMWindowProperty::trace(visitor
);
57 unsigned History::length() const
59 if (!m_frame
|| !m_frame
->loader().client())
61 return m_frame
->loader().client()->backForwardLength();
64 SerializedScriptValue
* History::state()
66 m_lastStateObjectRequested
= stateInternal();
67 return m_lastStateObjectRequested
.get();
70 SerializedScriptValue
* History::stateInternal() const
75 if (HistoryItem
* historyItem
= m_frame
->loader().currentItem())
76 return historyItem
->stateObject();
81 void History::setScrollRestoration(const String
& value
)
83 ASSERT(value
== "manual" || value
== "auto");
84 if (!m_frame
|| !m_frame
->loader().client() || !RuntimeEnabledFeatures::scrollRestorationEnabled())
87 HistoryScrollRestorationType scrollRestoration
= value
== "manual" ? ScrollRestorationManual
: ScrollRestorationAuto
;
88 if (scrollRestoration
== scrollRestorationInternal())
91 if (HistoryItem
* historyItem
= m_frame
->loader().currentItem()) {
92 historyItem
->setScrollRestorationType(scrollRestoration
);
93 m_frame
->loader().client()->didUpdateCurrentHistoryItem();
97 String
History::scrollRestoration()
99 return scrollRestorationInternal() == ScrollRestorationManual
? "manual" : "auto";
102 HistoryScrollRestorationType
History::scrollRestorationInternal() const
104 if (m_frame
&& RuntimeEnabledFeatures::scrollRestorationEnabled()) {
105 if (HistoryItem
* historyItem
= m_frame
->loader().currentItem())
106 return historyItem
->scrollRestorationType();
109 return ScrollRestorationAuto
;
112 bool History::stateChanged() const
114 return m_lastStateObjectRequested
!= stateInternal();
117 bool History::isSameAsCurrentState(SerializedScriptValue
* state
) const
119 return state
== stateInternal();
122 void History::back(ExecutionContext
* context
)
127 void History::forward(ExecutionContext
* context
)
132 void History::go(ExecutionContext
* context
, int delta
)
134 if (!m_frame
|| !m_frame
->loader().client())
137 ASSERT(isMainThread());
138 Document
* activeDocument
= toDocument(context
);
142 if (!activeDocument
->frame() || !activeDocument
->frame()->canNavigate(*m_frame
))
144 if (!NavigationDisablerForBeforeUnload::isNavigationAllowed())
148 m_frame
->loader().client()->navigateBackForward(delta
);
150 m_frame
->reload(FrameLoadTypeReload
, ClientRedirect
);
153 KURL
History::urlForState(const String
& urlString
)
155 Document
* document
= m_frame
->document();
157 if (urlString
.isNull())
158 return document
->url();
159 if (urlString
.isEmpty())
160 return document
->baseURL();
162 return KURL(document
->baseURL(), urlString
);
165 void History::stateObjectAdded(PassRefPtr
<SerializedScriptValue
> data
, const String
& /* title */, const String
& urlString
, HistoryScrollRestorationType restorationType
, FrameLoadType type
, ExceptionState
& exceptionState
)
167 if (!m_frame
|| !m_frame
->page() || !m_frame
->loader().documentLoader())
170 KURL fullURL
= urlForState(urlString
);
171 if (!fullURL
.isValid() || !m_frame
->document()->securityOrigin()->canRequest(fullURL
)) {
172 // We can safely expose the URL to JavaScript, as a) no redirection takes place: JavaScript already had this URL, b) JavaScript can only access a same-origin History object.
173 exceptionState
.throwSecurityError("A history state object with URL '" + fullURL
.elidedString() + "' cannot be created in a document with origin '" + m_frame
->document()->securityOrigin()->toString() + "'.");
177 m_frame
->loader().updateForSameDocumentNavigation(fullURL
, SameDocumentNavigationHistoryApi
, data
, restorationType
, type
);