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::addItem(WebBackForwardListItem* newItem)
50 if (m_capacity == 0 || !m_enabled)
53 // Toss anything in the forward list
54 if (m_current != NoCurrentItemIndex) {
55 unsigned targetSize = m_current + 1;
56 while (m_entries.size() > targetSize) {
57 RefPtr<WebBackForwardListItem> item = m_entries.last();
58 m_entries.removeLast();
62 // Toss the first item if the list is getting too big, as long as we're not using it
63 // (or even if we are, if we only want 1 entry).
64 if (m_entries.size() == m_capacity && (m_current != 0 || m_capacity == 1)) {
65 RefPtr<WebBackForwardListItem> item = m_entries[0];
70 m_page->didChangeBackForwardList();
73 m_entries.insert(m_current + 1, newItem);
77 m_page->didChangeBackForwardList();
80 void WebBackForwardList::goToItem(WebBackForwardListItem* item)
82 if (!m_entries.size() || !item)
86 for (; index < m_entries.size(); ++index) {
87 if (m_entries[index] == item)
90 if (index < m_entries.size()) {
93 m_page->didChangeBackForwardList();
97 WebBackForwardListItem* WebBackForwardList::currentItem()
99 if (m_current != NoCurrentItemIndex)
100 return m_entries[m_current].get();
104 WebBackForwardListItem* WebBackForwardList::backItem()
106 if (m_current && m_current != NoCurrentItemIndex)
107 return m_entries[m_current - 1].get();
111 WebBackForwardListItem* WebBackForwardList::forwardItem()
113 if (m_entries.size() && m_current < m_entries.size() - 1)
114 return m_entries[m_current + 1].get();
118 WebBackForwardListItem* WebBackForwardList::itemAtIndex(int index)
120 // Do range checks without doing math on index to avoid overflow.
121 if (index < -static_cast<int>(m_current))
124 if (index > forwardListCount())
127 return m_entries[index + m_current].get();
130 int WebBackForwardList::backListCount()
132 return m_current == NoCurrentItemIndex ? 0 : m_current;
135 int WebBackForwardList::forwardListCount()
137 return m_current == NoCurrentItemIndex ? 0 : static_cast<int>(m_entries.size()) - (m_current + 1);
140 PassRefPtr<ImmutableArray> WebBackForwardList::backListAsImmutableArrayWithLimit(unsigned limit)
142 unsigned backListSize = static_cast<unsigned>(backListCount());
143 unsigned size = std::min(backListSize, limit);
145 return ImmutableArray::create();
147 Vector<RefPtr<APIObject> > vector;
148 vector.reserveInitialCapacity(size);
150 ASSERT(backListSize >= size);
151 for (unsigned i = backListSize - size; i < backListSize; ++i)
152 vector.uncheckedAppend(m_entries[i].get());
154 return ImmutableArray::adopt(vector);
157 PassRefPtr<ImmutableArray> WebBackForwardList::forwardListAsImmutableArrayWithLimit(unsigned limit)
159 unsigned size = std::min(static_cast<unsigned>(forwardListCount()), limit);
161 return ImmutableArray::create();
163 Vector<RefPtr<APIObject> > vector;
164 vector.reserveInitialCapacity(size);
166 unsigned last = m_current + size;
167 ASSERT(last < m_entries.size());
168 for (unsigned i = m_current + 1; i <= last; ++i)
169 vector.uncheckedAppend(m_entries[i].get());
171 return ImmutableArray::adopt(vector);
174 void WebBackForwardList::clear()
176 if (m_entries.size() <= 1)
179 RefPtr<WebBackForwardListItem> item = currentItem();
181 m_entries[0] = item.release();
186 m_page->didChangeBackForwardList();
189 } // namespace WebKit