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)
51 void History::trace(Visitor* visitor)
53 DOMWindowProperty::trace(visitor);
56 unsigned History::length() const
62 return m_frame->page()->backForward().backForwardListCount();
65 SerializedScriptValue* History::state()
67 m_lastStateObjectRequested = stateInternal();
68 return m_lastStateObjectRequested.get();
71 SerializedScriptValue* History::stateInternal() const
76 if (HistoryItem* historyItem = m_frame->loader().currentItem())
77 return historyItem->stateObject();
82 bool History::stateChanged() const
84 return m_lastStateObjectRequested != stateInternal();
87 bool History::isSameAsCurrentState(SerializedScriptValue* state) const
89 return state == stateInternal();
92 void History::back(ExecutionContext* context)
97 void History::forward(ExecutionContext* context)
102 void History::go(ExecutionContext* context, int distance)
107 ASSERT(isMainThread());
108 Document* activeDocument = toDocument(context);
112 if (!activeDocument->canNavigate(*m_frame))
115 m_frame->navigationScheduler().scheduleHistoryNavigation(distance);
118 KURL History::urlForState(const String& urlString)
120 Document* document = m_frame->document();
122 if (urlString.isNull())
123 return document->url();
124 if (urlString.isEmpty())
125 return document->baseURL();
127 return KURL(document->baseURL(), urlString);
130 void History::stateObjectAdded(PassRefPtr<SerializedScriptValue> data, const String& /* title */, const String& urlString, FrameLoadType type, ExceptionState& exceptionState)
132 if (!m_frame || !m_frame->page() || !m_frame->loader().documentLoader())
135 KURL fullURL = urlForState(urlString);
136 if (!fullURL.isValid() || !m_frame->document()->securityOrigin()->canRequest(fullURL)) {
137 // 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.
138 exceptionState.throwSecurityError("A history state object with URL '" + fullURL.elidedString() + "' cannot be created in a document with origin '" + m_frame->document()->securityOrigin()->toString() + "'.");
141 m_frame->loader().updateForSameDocumentNavigation(fullURL, SameDocumentNavigationHistoryApi, data, type);