2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #include "WebBackForwardList.h"
28 #include "WebPageProxy.h"
32 static const unsigned DefaultCapacity = 100;
33 static const unsigned NoCurrentItemIndex = UINT_MAX;
35 WebBackForwardList::WebBackForwardList(WebPageProxy* page)
37 , m_current(NoCurrentItemIndex)
38 , m_capacity(DefaultCapacity)
44 WebBackForwardList::~WebBackForwardList()
48 void WebBackForwardList::pageClosed()
51 size_t size = m_entries.size();
52 for (size_t i = 0; i < size; ++i)
53 m_page->backForwardRemovedItem(m_entries[i]->itemID());
59 void WebBackForwardList::addItem(WebBackForwardListItem* newItem)
61 if (m_capacity == 0 || !m_enabled)
64 // Toss anything in the forward list
65 if (m_current != NoCurrentItemIndex) {
66 unsigned targetSize = m_current + 1;
67 while (m_entries.size() > targetSize) {
69 m_page->backForwardRemovedItem(m_entries.last()->itemID());
70 m_entries.removeLast();
74 // Toss the first item if the list is getting too big, as long as we're not using it
75 // (or even if we are, if we only want 1 entry).
76 if (m_entries.size() == m_capacity && (m_current != 0 || m_capacity == 1)) {
78 m_page->backForwardRemovedItem(m_entries[0]->itemID());
83 m_entries.insert(m_current + 1, newItem);
87 m_page->didChangeBackForwardList();
90 void WebBackForwardList::goToItem(WebBackForwardListItem* item)
92 if (!m_entries.size() || !item)
96 for (; index < m_entries.size(); ++index) {
97 if (m_entries[index] == item)
100 if (index < m_entries.size()) {
103 m_page->didChangeBackForwardList();
107 WebBackForwardListItem* WebBackForwardList::currentItem()
109 if (m_current != NoCurrentItemIndex)
110 return m_entries[m_current].get();
114 WebBackForwardListItem* WebBackForwardList::backItem()
116 if (m_current && m_current != NoCurrentItemIndex)
117 return m_entries[m_current - 1].get();
121 WebBackForwardListItem* WebBackForwardList::forwardItem()
123 if (m_entries.size() && m_current < m_entries.size() - 1)
124 return m_entries[m_current + 1].get();
128 WebBackForwardListItem* WebBackForwardList::itemAtIndex(int index)
130 // Do range checks without doing math on index to avoid overflow.
131 if (index < -static_cast<int>(m_current))
134 if (index > forwardListCount())
137 return m_entries[index + m_current].get();
140 int WebBackForwardList::backListCount()
142 return m_current == NoCurrentItemIndex ? 0 : m_current;
145 int WebBackForwardList::forwardListCount()
147 return m_current == NoCurrentItemIndex ? 0 : static_cast<int>(m_entries.size()) - (m_current + 1);
150 PassRefPtr<ImmutableArray> WebBackForwardList::backListAsImmutableArrayWithLimit(unsigned limit)
152 unsigned backListSize = static_cast<unsigned>(backListCount());
153 unsigned size = std::min(backListSize, limit);
155 return ImmutableArray::create();
157 Vector<RefPtr<APIObject> > vector;
158 vector.reserveInitialCapacity(size);
160 ASSERT(backListSize >= size);
161 for (unsigned i = backListSize - size; i < backListSize; ++i)
162 vector.uncheckedAppend(m_entries[i].get());
164 return ImmutableArray::adopt(vector);
167 PassRefPtr<ImmutableArray> WebBackForwardList::forwardListAsImmutableArrayWithLimit(unsigned limit)
169 unsigned size = std::min(static_cast<unsigned>(forwardListCount()), limit);
171 return ImmutableArray::create();
173 Vector<RefPtr<APIObject> > vector;
174 vector.reserveInitialCapacity(size);
176 unsigned last = m_current + size;
177 ASSERT(last < m_entries.size());
178 for (unsigned i = m_current + 1; i <= last; ++i)
179 vector.uncheckedAppend(m_entries[i].get());
181 return ImmutableArray::adopt(vector);
184 void WebBackForwardList::clear()
186 size_t size = m_entries.size();
190 RefPtr<WebBackForwardListItem> currentItem = this->currentItem();
193 for (size_t i = 0; i < size; ++i) {
194 if (m_entries[i] != currentItem)
195 m_page->backForwardRemovedItem(m_entries[i]->itemID());
200 m_entries[0] = currentItem.release();
205 m_page->didChangeBackForwardList();
208 } // namespace WebKit