1/*
2 * Copyright (C) 2010 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "BackForwardController.h"
28
29#include "BackForwardClient.h"
30#include "HistoryItem.h"
31#include "Page.h"
32#include "ShouldTreatAsContinuingLoad.h"
33
34namespace WebCore {
35
36BackForwardController::BackForwardController(Page& page, Ref<BackForwardClient>&& client)
37 : m_page(page)
38 , m_client(WTFMove(client))
39{
40}
41
42BackForwardController::~BackForwardController() = default;
43
44RefPtr<HistoryItem> BackForwardController::backItem()
45{
46 return itemAtIndex(-1);
47}
48
49RefPtr<HistoryItem> BackForwardController::currentItem()
50{
51 return itemAtIndex(0);
52}
53
54RefPtr<HistoryItem> BackForwardController::forwardItem()
55{
56 return itemAtIndex(1);
57}
58
59bool BackForwardController::canGoBackOrForward(int distance) const
60{
61 if (!distance)
62 return true;
63 if (distance > 0 && static_cast<unsigned>(distance) <= forwardCount())
64 return true;
65 if (distance < 0 && static_cast<unsigned>(-distance) <= backCount())
66 return true;
67 return false;
68}
69
70void BackForwardController::goBackOrForward(int distance)
71{
72 if (!distance)
73 return;
74
75 auto historyItem = itemAtIndex(distance);
76 if (!historyItem) {
77 if (distance > 0) {
78 if (int forwardCount = this->forwardCount())
79 historyItem = itemAtIndex(forwardCount);
80 } else {
81 if (int backCount = this->backCount())
82 historyItem = itemAtIndex(-backCount);
83 }
84 }
85
86 if (!historyItem)
87 return;
88
89 m_page.goToItem(*historyItem, FrameLoadType::IndexedBackForward, ShouldTreatAsContinuingLoad::No);
90}
91
92bool BackForwardController::goBack()
93{
94 auto historyItem = backItem();
95 if (!historyItem)
96 return false;
97
98 m_page.goToItem(*historyItem, FrameLoadType::Back, ShouldTreatAsContinuingLoad::No);
99 return true;
100}
101
102bool BackForwardController::goForward()
103{
104 auto historyItem = forwardItem();
105 if (!historyItem)
106 return false;
107
108 m_page.goToItem(*historyItem, FrameLoadType::Forward, ShouldTreatAsContinuingLoad::No);
109 return true;
110}
111
112void BackForwardController::addItem(Ref<HistoryItem>&& item)
113{
114 m_client->addItem(WTFMove(item));
115}
116
117void BackForwardController::setCurrentItem(HistoryItem& item)
118{
119 m_client->goToItem(item);
120}
121
122unsigned BackForwardController::count() const
123{
124 return m_client->backListCount() + 1 + m_client->forwardListCount();
125}
126
127unsigned BackForwardController::backCount() const
128{
129 return m_client->backListCount();
130}
131
132unsigned BackForwardController::forwardCount() const
133{
134 return m_client->forwardListCount();
135}
136
137RefPtr<HistoryItem> BackForwardController::itemAtIndex(int i)
138{
139 return m_client->itemAtIndex(i);
140}
141
142void BackForwardController::close()
143{
144 m_client->close();
145}
146
147} // namespace WebCore
148