1 /* 2 * Copyright (C) 2009 Google 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "BackForwardListClientImpl.h" 33 34 #include "HistoryItem.h" 35 #include "WebViewClient.h" 36 #include "WebViewImpl.h" 37 38 using namespace WebCore; 39 40 namespace WebKit { 41 42 const char backForwardNavigationScheme[] = "chrome-back-forward"; 43 44 BackForwardListClientImpl::BackForwardListClientImpl(WebViewImpl* webView) 45 : m_webView(webView) 46 { 47 } 48 49 BackForwardListClientImpl::~BackForwardListClientImpl() 50 { 51 } 52 53 void BackForwardListClientImpl::setCurrentHistoryItem(HistoryItem* item) 54 { 55 m_previousItem = m_currentItem; 56 m_currentItem = item; 57 } 58 59 HistoryItem* BackForwardListClientImpl::previousHistoryItem() const 60 { 61 return m_previousItem.get(); 62 } 63 64 void BackForwardListClientImpl::addItem(PassRefPtr<HistoryItem> item) 65 { 66 m_previousItem = m_currentItem; 67 m_currentItem = item; 68 69 // If WebCore adds a new HistoryItem, it means this is a new navigation (ie, 70 // not a reload or back/forward). 71 m_webView->observeNewNavigation(); 72 73 if (m_webView->client()) 74 m_webView->client()->didAddHistoryItem(); 75 } 76 77 void BackForwardListClientImpl::goToItem(HistoryItem* item) 78 { 79 m_previousItem = m_currentItem; 80 m_currentItem = item; 81 82 if (m_pendingHistoryItem == item) 83 m_pendingHistoryItem = 0; 84 } 85 86 HistoryItem* BackForwardListClientImpl::currentItem() 87 { 88 return m_currentItem.get(); 89 } 90 91 HistoryItem* BackForwardListClientImpl::itemAtIndex(int index) 92 { 93 if (!m_webView->client() || index > forwardListCount() || -index > backListCount()) 94 return 0; 95 96 // Since we don't keep the entire back/forward list, we have no way to 97 // properly implement this method. We return a dummy entry instead that we 98 // intercept in our FrameLoaderClient implementation in case WebCore asks 99 // to navigate to this HistoryItem. 100 101 // FIXME: We should change WebCore to handle history.{back,forward,go} 102 // differently. It should perhaps just ask the FrameLoaderClient to 103 // perform those navigations. 104 105 String url_string = String::format( 106 "%s://go/%d", backForwardNavigationScheme, index); 107 108 m_pendingHistoryItem = 109 HistoryItem::create(url_string, String(), 0.0); 110 return m_pendingHistoryItem.get(); 111 } 112 113 int BackForwardListClientImpl::backListCount() 114 { 115 if (!m_webView->client()) 116 return 0; 117 118 return m_webView->client()->historyBackListCount(); 119 } 120 121 int BackForwardListClientImpl::forwardListCount() 122 { 123 if (!m_webView->client()) 124 return 0; 125 126 return m_webView->client()->historyForwardListCount(); 127 } 128 129 void BackForwardListClientImpl::close() 130 { 131 m_currentItem = 0; 132 m_previousItem = 0; 133 m_pendingHistoryItem = 0; 134 } 135 136 } // namespace WebKit 137