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/page/BackForwardClient.h"
38 #include "core/page/Page.h"
39 #include "platform/weborigin/KURL.h"
40 #include "platform/weborigin/SecurityOrigin.h"
41 #include "wtf/MainThread.h"
45 History::History(LocalFrame* frame)
46 : DOMWindowProperty(frame)
47 , m_lastStateObjectRequested(nullptr)
49 ScriptWrappable::init(this);
52 unsigned History::length() const
58 return m_frame->page()->backForward().backForwardListCount();
61 SerializedScriptValue* History::state()
63 m_lastStateObjectRequested = stateInternal();
64 return m_lastStateObjectRequested.get();
67 SerializedScriptValue* History::stateInternal() const
72 if (HistoryItem* historyItem = m_frame->loader().currentItem())
73 return historyItem->stateObject();
78 bool History::stateChanged() const
80 return m_lastStateObjectRequested != stateInternal();
83 bool History::isSameAsCurrentState(SerializedScriptValue* state) const
85 return state == stateInternal();
88 void History::back(ExecutionContext* context)
93 void History::forward(ExecutionContext* context)
98 void History::go(ExecutionContext* context, int distance)
103 ASSERT(isMainThread());
104 Document* activeDocument = toDocument(context);
108 if (!activeDocument->canNavigate(*m_frame))
111 m_frame->navigationScheduler().scheduleHistoryNavigation(distance);
114 KURL History::urlForState(const String& urlString)
116 Document* document = m_frame->document();
118 if (urlString.isNull())
119 return document->url();
120 if (urlString.isEmpty())
121 return document->baseURL();
123 return KURL(document->baseURL(), urlString);
126 void History::stateObjectAdded(PassRefPtr<SerializedScriptValue> data, const String& /* title */, const String& urlString, FrameLoadType type, ExceptionState& exceptionState)
128 if (!m_frame || !m_frame->page() || !m_frame->loader().documentLoader())
131 KURL fullURL = urlForState(urlString);
132 if (!fullURL.isValid() || !m_frame->document()->securityOrigin()->canRequest(fullURL)) {
133 // 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.
134 exceptionState.throwSecurityError("A history state object with URL '" + fullURL.elidedString() + "' cannot be created in a document with origin '" + m_frame->document()->securityOrigin()->toString() + "'.");
137 m_frame->loader().updateForSameDocumentNavigation(fullURL, SameDocumentNavigationHistoryApi, data, type);