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 "ExceptionCode.h" 30 #include "Frame.h" 31 #include "FrameLoader.h" 32 #include "FrameLoaderClient.h" 33 #include "HistoryItem.h" 34 #include "Page.h" 35 36 namespace WebCore { 37 38 History::History(Frame* frame) 39 : m_frame(frame) 40 { 41 } 42 43 Frame* History::frame() const 44 { 45 return m_frame; 46 } 47 48 void History::disconnectFrame() 49 { 50 m_frame = 0; 51 } 52 53 unsigned History::length() const 54 { 55 if (!m_frame) 56 return 0; 57 if (!m_frame->page()) 58 return 0; 59 return m_frame->page()->getHistoryLength(); 60 } 61 62 void History::back() 63 { 64 if (!m_frame) 65 return; 66 m_frame->redirectScheduler()->scheduleHistoryNavigation(-1); 67 } 68 69 void History::forward() 70 { 71 if (!m_frame) 72 return; 73 m_frame->redirectScheduler()->scheduleHistoryNavigation(1); 74 } 75 76 void History::go(int distance) 77 { 78 if (!m_frame) 79 return; 80 m_frame->redirectScheduler()->scheduleHistoryNavigation(distance); 81 } 82 83 KURL History::urlForState(const String& urlString) 84 { 85 KURL baseURL = m_frame->loader()->baseURL(); 86 if (urlString.isEmpty()) 87 return baseURL; 88 89 return KURL(baseURL, urlString); 90 } 91 92 void History::stateObjectAdded(PassRefPtr<SerializedScriptValue> data, const String& title, const String& urlString, StateObjectType stateObjectType, ExceptionCode& ec) 93 { 94 if (!m_frame || !m_frame->page()) 95 return; 96 97 KURL fullURL = urlForState(urlString); 98 RefPtr<SecurityOrigin> origin = SecurityOrigin::create(fullURL); 99 if (!fullURL.isValid() || !m_frame->document()->securityOrigin()->isSameSchemeHostPort(origin.get())) { 100 ec = SECURITY_ERR; 101 return; 102 } 103 104 if (stateObjectType == StateObjectPush) 105 m_frame->loader()->history()->pushState(data, title, fullURL.string()); 106 else if (stateObjectType == StateObjectReplace) 107 m_frame->loader()->history()->replaceState(data, title, fullURL.string()); 108 109 if (!urlString.isEmpty()) 110 m_frame->document()->updateURLForPushOrReplaceState(fullURL); 111 112 if (stateObjectType == StateObjectPush) 113 m_frame->loader()->client()->dispatchDidPushStateWithinPage(); 114 else if (stateObjectType == StateObjectReplace) 115 m_frame->loader()->client()->dispatchDidReplaceStateWithinPage(); 116 } 117 118 } // namespace WebCore 119