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