1/* 2 * Copyright (C) 2007 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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. 12 * 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. 24 */ 25 26#include "config.h" 27#include "History.h" 28 29#include "BackForwardController.h" 30#include "Document.h" 31#include "ExceptionCode.h" 32#include "Frame.h" 33#include "FrameLoader.h" 34#include "FrameLoaderClient.h" 35#include "HistoryController.h" 36#include "HistoryItem.h" 37#include "Page.h" 38#include "SecurityOrigin.h" 39#include "SerializedScriptValue.h" 40#include <wtf/MainThread.h> 41 42namespace WebCore { 43 44History::History(Frame* frame) 45 : DOMWindowProperty(frame) 46 , m_lastStateObjectRequested(0) 47{ 48} 49 50unsigned History::length() const 51{ 52 if (!m_frame) 53 return 0; 54 if (!m_frame->page()) 55 return 0; 56 return m_frame->page()->backForward()->count(); 57} 58 59PassRefPtr<SerializedScriptValue> History::state() 60{ 61 m_lastStateObjectRequested = stateInternal(); 62 return m_lastStateObjectRequested; 63} 64 65PassRefPtr<SerializedScriptValue> History::stateInternal() const 66{ 67 if (!m_frame) 68 return 0; 69 70 if (HistoryItem* historyItem = m_frame->loader()->history()->currentItem()) 71 return historyItem->stateObject(); 72 73 return 0; 74} 75 76bool History::stateChanged() const 77{ 78 return m_lastStateObjectRequested != stateInternal(); 79} 80 81bool History::isSameAsCurrentState(SerializedScriptValue* state) const 82{ 83 return state == stateInternal().get(); 84} 85 86void History::back() 87{ 88 go(-1); 89} 90 91void History::back(ScriptExecutionContext* context) 92{ 93 go(context, -1); 94} 95 96void History::forward() 97{ 98 go(1); 99} 100 101void History::forward(ScriptExecutionContext* context) 102{ 103 go(context, 1); 104} 105 106void History::go(int distance) 107{ 108 if (!m_frame) 109 return; 110 111 m_frame->navigationScheduler()->scheduleHistoryNavigation(distance); 112} 113 114void History::go(ScriptExecutionContext* context, int distance) 115{ 116 if (!m_frame) 117 return; 118 119 ASSERT(isMainThread()); 120 Document* activeDocument = toDocument(context); 121 if (!activeDocument) 122 return; 123 124 if (!activeDocument->canNavigate(m_frame)) 125 return; 126 127 m_frame->navigationScheduler()->scheduleHistoryNavigation(distance); 128} 129 130KURL History::urlForState(const String& urlString) 131{ 132 KURL baseURL = m_frame->document()->baseURL(); 133 if (urlString.isEmpty()) 134 return baseURL; 135 136 return KURL(baseURL, urlString); 137} 138 139void History::stateObjectAdded(PassRefPtr<SerializedScriptValue> data, const String& title, const String& urlString, StateObjectType stateObjectType, ExceptionCode& ec) 140{ 141 if (!m_frame || !m_frame->page()) 142 return; 143 144 KURL fullURL = urlForState(urlString); 145 if (!fullURL.isValid() || !m_frame->document()->securityOrigin()->canRequest(fullURL)) { 146 ec = SECURITY_ERR; 147 return; 148 } 149 150 if (stateObjectType == StateObjectPush) 151 m_frame->loader()->history()->pushState(data, title, fullURL.string()); 152 else if (stateObjectType == StateObjectReplace) 153 m_frame->loader()->history()->replaceState(data, title, fullURL.string()); 154 155 if (!urlString.isEmpty()) 156 m_frame->document()->updateURLForPushOrReplaceState(fullURL); 157 158 if (stateObjectType == StateObjectPush) 159 m_frame->loader()->client()->dispatchDidPushStateWithinPage(); 160 else if (stateObjectType == StateObjectReplace) 161 m_frame->loader()->client()->dispatchDidReplaceStateWithinPage(); 162} 163 164} // namespace WebCore 165