Home | History | Annotate | Download | only in web
      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 "BackForwardClientImpl.h"
     33 
     34 #include "WebViewClient.h"
     35 #include "WebViewImpl.h"
     36 #include "core/history/HistoryItem.h"
     37 #include "wtf/text/StringConcatenate.h"
     38 
     39 using namespace WebCore;
     40 
     41 namespace WebKit {
     42 
     43 const char backForwardNavigationScheme[] = "chrome-back-forward";
     44 
     45 BackForwardClientImpl::BackForwardClientImpl(WebViewImpl* webView)
     46     : m_webView(webView)
     47 {
     48 }
     49 
     50 BackForwardClientImpl::~BackForwardClientImpl()
     51 {
     52 }
     53 
     54 void BackForwardClientImpl::addItem(PassRefPtr<HistoryItem> item)
     55 {
     56     m_currentItem = item;
     57 
     58     // If WebCore adds a new HistoryItem, it means this is a new navigation (ie,
     59     // not a reload or back/forward).
     60     m_webView->observeNewNavigation();
     61 
     62     if (m_webView->client())
     63         m_webView->client()->didAddHistoryItem();
     64 }
     65 
     66 void BackForwardClientImpl::goToItem(HistoryItem* item)
     67 {
     68     m_currentItem = item;
     69 
     70     if (m_pendingHistoryItem == item)
     71         m_pendingHistoryItem = 0;
     72 }
     73 
     74 HistoryItem* BackForwardClientImpl::itemAtIndex(int index)
     75 {
     76     if (!m_webView->client())
     77         return 0;
     78 
     79     if (!index)
     80         return m_currentItem.get();
     81 
     82     if (index > forwardListCount() || -index > backListCount())
     83         return 0;
     84 
     85     // Since we don't keep the entire back/forward list, we have no way to
     86     // properly implement this method.  We return a dummy entry instead that we
     87     // intercept in our FrameLoaderClient implementation in case WebCore asks
     88     // to navigate to this HistoryItem.
     89 
     90     // FIXME: We should change WebCore to handle history.{back,forward,go}
     91     // differently.  It should perhaps just ask the FrameLoaderClient to
     92     // perform those navigations.
     93 
     94     String urlString = String(backForwardNavigationScheme) + "://go/" + String::number(index);
     95     m_pendingHistoryItem = HistoryItem::create(urlString);
     96     return m_pendingHistoryItem.get();
     97 }
     98 
     99 int BackForwardClientImpl::backListCount()
    100 {
    101     if (!m_webView->client())
    102         return 0;
    103 
    104     return m_webView->client()->historyBackListCount();
    105 }
    106 
    107 int BackForwardClientImpl::forwardListCount()
    108 {
    109     if (!m_webView->client())
    110         return 0;
    111 
    112     return m_webView->client()->historyForwardListCount();
    113 }
    114 
    115 bool BackForwardClientImpl::isActive()
    116 {
    117     return m_webView->client();
    118 }
    119 
    120 void BackForwardClientImpl::close()
    121 {
    122     m_currentItem = 0;
    123     m_pendingHistoryItem = 0;
    124 }
    125 
    126 } // namespace WebKit
    127