1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
/*
* Copyright (C) 2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WebBackForwardList.h"
#include "APIArray.h"
#include "SessionState.h"
#include "WebPageProxy.h"
namespace WebKit {
// FIXME: Make this static once WebBackForwardListCF.cpp is no longer using it.
uint64_t generateWebBackForwardItemID();
uint64_t generateWebBackForwardItemID()
{
// These IDs exist in the UIProcess for items created by the UIProcess.
// The IDs generated here need to never collide with the IDs created in WebBackForwardListProxy in the WebProcess.
// We accomplish this by starting from 2, and only ever using even ids.
static uint64_t uniqueHistoryItemID = 0;
uniqueHistoryItemID += 2;
return uniqueHistoryItemID;
}
static const unsigned DefaultCapacity = 100;
WebBackForwardList::WebBackForwardList(WebPageProxy& page)
: m_page(&page)
, m_hasCurrentIndex(false)
, m_currentIndex(0)
, m_capacity(DefaultCapacity)
{
}
WebBackForwardList::~WebBackForwardList()
{
// A WebBackForwardList should never be destroyed unless it's associated page has been closed or is invalid.
ASSERT((!m_page && !m_hasCurrentIndex) || !m_page->isValid());
}
void WebBackForwardList::pageClosed()
{
// We should have always started out with an m_page and we should never close the page twice.
ASSERT(m_page);
if (m_page) {
size_t size = m_entries.size();
for (size_t i = 0; i < size; ++i) {
ASSERT(m_entries[i]);
if (!m_entries[i])
continue;
m_page->backForwardRemovedItem(m_entries[i]->itemID());
}
}
m_page = 0;
m_entries.clear();
m_hasCurrentIndex = false;
}
void WebBackForwardList::addItem(WebBackForwardListItem* newItem)
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
if (!m_capacity || !newItem || !m_page)
return;
Vector<RefPtr<WebBackForwardListItem>> removedItems;
if (m_hasCurrentIndex) {
// Toss everything in the forward list.
unsigned targetSize = m_currentIndex + 1;
removedItems.reserveCapacity(m_entries.size() - targetSize);
while (m_entries.size() > targetSize) {
m_page->backForwardRemovedItem(m_entries.last()->itemID());
removedItems.append(m_entries.last().release());
m_entries.removeLast();
}
// Toss the first item if the list is getting too big, as long as we're not using it
// (or even if we are, if we only want 1 entry).
if (m_entries.size() == m_capacity && (m_currentIndex || m_capacity == 1)) {
m_page->backForwardRemovedItem(m_entries[0]->itemID());
removedItems.append(m_entries[0].release());
m_entries.remove(0);
if (m_entries.isEmpty())
m_hasCurrentIndex = false;
else
m_currentIndex--;
}
} else {
// If we have no current item index we should also not have any entries.
ASSERT(m_entries.isEmpty());
// But just in case it does happen in practice we'll get back in to a consistent state now before adding the new item.
size_t size = m_entries.size();
for (size_t i = 0; i < size; ++i) {
ASSERT(m_entries[i]);
if (!m_entries[i])
continue;
m_page->backForwardRemovedItem(m_entries[i]->itemID());
removedItems.append(m_entries[i].release());
}
m_entries.clear();
}
bool shouldKeepCurrentItem = true;
if (!m_hasCurrentIndex) {
ASSERT(m_entries.isEmpty());
m_currentIndex = 0;
m_hasCurrentIndex = true;
} else {
shouldKeepCurrentItem = m_page->shouldKeepCurrentBackForwardListItemInList(m_entries[m_currentIndex].get());
if (shouldKeepCurrentItem)
m_currentIndex++;
}
if (!shouldKeepCurrentItem) {
// m_current should never be pointing past the end of the entries Vector.
// If it is, something has gone wrong and we should not try to swap in the new item.
ASSERT(m_currentIndex < m_entries.size());
removedItems.append(m_entries[m_currentIndex]);
m_entries[m_currentIndex] = newItem;
} else {
// m_current should never be pointing more than 1 past the end of the entries Vector.
// If it is, something has gone wrong and we should not try to insert the new item.
ASSERT(m_currentIndex <= m_entries.size());
if (m_currentIndex <= m_entries.size())
m_entries.insert(m_currentIndex, newItem);
}
m_page->didChangeBackForwardList(newItem, WTF::move(removedItems));
}
void WebBackForwardList::goToItem(WebBackForwardListItem* item)
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
if (!m_entries.size() || !item || !m_page || !m_hasCurrentIndex)
return;
size_t targetIndex = m_entries.find(item);
// If the target item wasn't even in the list, there's nothing else to do.
if (targetIndex == notFound)
return;
// If we're going to an item different from the current item, ask the client if the current
// item should remain in the list.
WebBackForwardListItem* currentItem = m_entries[m_currentIndex].get();
bool shouldKeepCurrentItem = true;
if (currentItem != item)
shouldKeepCurrentItem = m_page->shouldKeepCurrentBackForwardListItemInList(m_entries[m_currentIndex].get());
// If the client said to remove the current item, remove it and then update the target index.
Vector<RefPtr<WebBackForwardListItem>> removedItems;
if (!shouldKeepCurrentItem) {
removedItems.append(currentItem);
m_entries.remove(m_currentIndex);
targetIndex = m_entries.find(item);
ASSERT(targetIndex != notFound);
}
m_currentIndex = targetIndex;
m_page->didChangeBackForwardList(nullptr, removedItems);
}
WebBackForwardListItem* WebBackForwardList::currentItem() const
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
return m_page && m_hasCurrentIndex ? m_entries[m_currentIndex].get() : nullptr;
}
WebBackForwardListItem* WebBackForwardList::backItem() const
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
return m_page && m_hasCurrentIndex && m_currentIndex ? m_entries[m_currentIndex - 1].get() : nullptr;
}
WebBackForwardListItem* WebBackForwardList::forwardItem() const
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
return m_page && m_hasCurrentIndex && m_entries.size() && m_currentIndex < m_entries.size() - 1 ? m_entries[m_currentIndex + 1].get() : nullptr;
}
WebBackForwardListItem* WebBackForwardList::itemAtIndex(int index) const
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
if (!m_hasCurrentIndex || !m_page)
return nullptr;
// Do range checks without doing math on index to avoid overflow.
if (index < -backListCount())
return nullptr;
if (index > forwardListCount())
return nullptr;
return m_entries[index + m_currentIndex].get();
}
int WebBackForwardList::backListCount() const
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
return m_page && m_hasCurrentIndex ? m_currentIndex : 0;
}
int WebBackForwardList::forwardListCount() const
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
return m_page && m_hasCurrentIndex ? m_entries.size() - (m_currentIndex + 1) : 0;
}
PassRefPtr<API::Array> WebBackForwardList::backList() const
{
return backListAsAPIArrayWithLimit(backListCount());
}
PassRefPtr<API::Array> WebBackForwardList::forwardList() const
{
return forwardListAsAPIArrayWithLimit(forwardListCount());
}
PassRefPtr<API::Array> WebBackForwardList::backListAsAPIArrayWithLimit(unsigned limit) const
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
if (!m_page || !m_hasCurrentIndex)
return API::Array::create();
unsigned backListSize = static_cast<unsigned>(backListCount());
unsigned size = std::min(backListSize, limit);
if (!size)
return API::Array::create();
Vector<RefPtr<API::Object>> vector;
vector.reserveInitialCapacity(size);
ASSERT(backListSize >= size);
for (unsigned i = backListSize - size; i < backListSize; ++i) {
ASSERT(m_entries[i]);
vector.uncheckedAppend(m_entries[i].get());
}
return API::Array::create(WTF::move(vector));
}
PassRefPtr<API::Array> WebBackForwardList::forwardListAsAPIArrayWithLimit(unsigned limit) const
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
if (!m_page || !m_hasCurrentIndex)
return API::Array::create();
unsigned size = std::min(static_cast<unsigned>(forwardListCount()), limit);
if (!size)
return API::Array::create();
Vector<RefPtr<API::Object>> vector;
vector.reserveInitialCapacity(size);
unsigned last = m_currentIndex + size;
ASSERT(last < m_entries.size());
for (unsigned i = m_currentIndex + 1; i <= last; ++i) {
ASSERT(m_entries[i]);
vector.uncheckedAppend(m_entries[i].get());
}
return API::Array::create(WTF::move(vector));
}
void WebBackForwardList::removeAllItems()
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
Vector<RefPtr<WebBackForwardListItem>> removedItems;
for (auto& entry : m_entries) {
ASSERT(entry);
if (!entry)
continue;
m_page->backForwardRemovedItem(entry->itemID());
removedItems.append(WTF::move(entry));
}
m_entries.clear();
m_hasCurrentIndex = false;
m_page->didChangeBackForwardList(nullptr, WTF::move(removedItems));
}
void WebBackForwardList::clear()
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
size_t size = m_entries.size();
if (!m_page || size <= 1)
return;
RefPtr<WebBackForwardListItem> currentItem = this->currentItem();
Vector<RefPtr<WebBackForwardListItem>> removedItems;
if (!currentItem) {
// We should only ever have no current item if we also have no current item index.
ASSERT(!m_hasCurrentIndex);
// But just in case it does happen in practice we should get back into a consistent state now.
for (size_t i = 0; i < size; ++i) {
ASSERT(m_entries[i]);
if (!m_entries[i])
continue;
m_page->backForwardRemovedItem(m_entries[i]->itemID());
removedItems.append(m_entries[i].release());
}
m_entries.clear();
m_hasCurrentIndex = false;
m_page->didChangeBackForwardList(nullptr, WTF::move(removedItems));
return;
}
for (size_t i = 0; i < size; ++i) {
ASSERT(m_entries[i]);
if (m_entries[i] && m_entries[i] != currentItem)
m_page->backForwardRemovedItem(m_entries[i]->itemID());
}
removedItems.reserveCapacity(size - 1);
for (size_t i = 0; i < size; ++i) {
if (i != m_currentIndex && m_hasCurrentIndex && m_entries[i])
removedItems.append(m_entries[i].release());
}
m_currentIndex = 0;
if (currentItem) {
m_entries.shrink(1);
m_entries[0] = currentItem.release();
} else {
m_entries.clear();
m_hasCurrentIndex = false;
}
m_page->didChangeBackForwardList(nullptr, WTF::move(removedItems));
}
BackForwardListState WebBackForwardList::backForwardListState(const std::function<bool (WebBackForwardListItem&)>& filter) const
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
BackForwardListState backForwardListState;
if (m_hasCurrentIndex)
backForwardListState.currentIndex = m_currentIndex;
for (size_t i = 0; i < m_entries.size(); ++i) {
auto& entry = *m_entries[i];
if (filter && !filter(entry)) {
if (backForwardListState.currentIndex && i <= backForwardListState.currentIndex.value())
--backForwardListState.currentIndex.value();
continue;
}
backForwardListState.items.append(entry.itemState());
}
return backForwardListState;
}
void WebBackForwardList::restoreFromState(BackForwardListState backForwardListState)
{
Vector<RefPtr<WebBackForwardListItem>> items;
items.reserveInitialCapacity(backForwardListState.items.size());
for (auto& backForwardListItemState : backForwardListState.items) {
backForwardListItemState.identifier = generateWebBackForwardItemID();
items.uncheckedAppend(WebBackForwardListItem::create(WTF::move(backForwardListItemState), m_page->pageID()));
}
m_hasCurrentIndex = !!backForwardListState.currentIndex;
m_currentIndex = backForwardListState.currentIndex.valueOr(0);
m_entries = WTF::move(items);
}
Vector<BackForwardListItemState> WebBackForwardList::itemStates() const
{
Vector<BackForwardListItemState> itemStates;
itemStates.reserveInitialCapacity(m_entries.size());
for (const auto& entry : m_entries)
itemStates.uncheckedAppend(entry->itemState());
return itemStates;
}
} // namespace WebKit
|