Fix remaining bad uses of logDiagnosticMessageWithValue()
[WebKit-https.git] / Source / WebKit2 / UIProcess / WebBackForwardList.cpp
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 "WebBackForwardList.h"
28
29 #include "APIArray.h"
30 #include "SessionState.h"
31 #include "WebPageProxy.h"
32 #include <WebCore/DiagnosticLoggingKeys.h>
33
34 namespace WebKit {
35
36 // FIXME: Make this static once WebBackForwardListCF.cpp is no longer using it.
37 uint64_t generateWebBackForwardItemID();
38
39 uint64_t generateWebBackForwardItemID()
40 {
41     // These IDs exist in the UIProcess for items created by the UIProcess.
42     // The IDs generated here need to never collide with the IDs created in WebBackForwardListProxy in the WebProcess.
43     // We accomplish this by starting from 2, and only ever using even ids.
44     static uint64_t uniqueHistoryItemID = 0;
45     uniqueHistoryItemID += 2;
46     return uniqueHistoryItemID;
47 }
48
49 static const unsigned DefaultCapacity = 100;
50
51 WebBackForwardList::WebBackForwardList(WebPageProxy& page)
52     : m_page(&page)
53     , m_hasCurrentIndex(false)
54     , m_currentIndex(0)
55     , m_capacity(DefaultCapacity)
56 {
57 }
58
59 WebBackForwardList::~WebBackForwardList()
60 {
61     // A WebBackForwardList should never be destroyed unless it's associated page has been closed or is invalid.
62     ASSERT((!m_page && !m_hasCurrentIndex) || !m_page->isValid());
63 }
64
65 void WebBackForwardList::pageClosed()
66 {
67     // We should have always started out with an m_page and we should never close the page twice.
68     ASSERT(m_page);
69
70     if (m_page) {
71         size_t size = m_entries.size();
72         for (size_t i = 0; i < size; ++i) {
73             ASSERT(m_entries[i]);
74             if (!m_entries[i])
75                 continue;
76
77             didRemoveItem(*m_entries[i]);
78         }
79     }
80
81     m_page = 0;
82     m_entries.clear();
83     m_hasCurrentIndex = false;
84 }
85
86 void WebBackForwardList::addItem(WebBackForwardListItem* newItem)
87 {
88     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
89
90     if (!m_capacity || !newItem || !m_page)
91         return;
92
93     Vector<RefPtr<WebBackForwardListItem>> removedItems;
94     
95     if (m_hasCurrentIndex) {
96         m_page->recordAutomaticNavigationSnapshot();
97
98         // Toss everything in the forward list.
99         unsigned targetSize = m_currentIndex + 1;
100         removedItems.reserveCapacity(m_entries.size() - targetSize);
101         while (m_entries.size() > targetSize) {
102             didRemoveItem(*m_entries.last());
103             removedItems.append(WTFMove(m_entries.last()));
104             m_entries.removeLast();
105         }
106
107         // Toss the first item if the list is getting too big, as long as we're not using it
108         // (or even if we are, if we only want 1 entry).
109         if (m_entries.size() == m_capacity && (m_currentIndex || m_capacity == 1)) {
110             didRemoveItem(*m_entries[0]);
111             removedItems.append(WTFMove(m_entries[0]));
112             m_entries.remove(0);
113
114             if (m_entries.isEmpty())
115                 m_hasCurrentIndex = false;
116             else
117                 m_currentIndex--;
118         }
119     } else {
120         // If we have no current item index we should also not have any entries.
121         ASSERT(m_entries.isEmpty());
122
123         // But just in case it does happen in practice we'll get back in to a consistent state now before adding the new item.
124         size_t size = m_entries.size();
125         for (size_t i = 0; i < size; ++i) {
126             ASSERT(m_entries[i]);
127             if (!m_entries[i])
128                 continue;
129             didRemoveItem(*m_entries[i]);
130             removedItems.append(WTFMove(m_entries[i]));
131         }
132         m_entries.clear();
133     }
134
135     bool shouldKeepCurrentItem = true;
136
137     if (!m_hasCurrentIndex) {
138         ASSERT(m_entries.isEmpty());
139         m_currentIndex = 0;
140         m_hasCurrentIndex = true;
141     } else {
142         shouldKeepCurrentItem = m_page->shouldKeepCurrentBackForwardListItemInList(m_entries[m_currentIndex].get());
143         if (shouldKeepCurrentItem)
144             m_currentIndex++;
145     }
146
147     if (!shouldKeepCurrentItem) {
148         // m_current should never be pointing past the end of the entries Vector.
149         // If it is, something has gone wrong and we should not try to swap in the new item.
150         ASSERT(m_currentIndex < m_entries.size());
151
152         removedItems.append(m_entries[m_currentIndex]);
153         m_entries[m_currentIndex] = newItem;
154     } else {
155         // m_current should never be pointing more than 1 past the end of the entries Vector.
156         // If it is, something has gone wrong and we should not try to insert the new item.
157         ASSERT(m_currentIndex <= m_entries.size());
158
159         if (m_currentIndex <= m_entries.size())
160             m_entries.insert(m_currentIndex, newItem);
161     }
162
163     m_page->didChangeBackForwardList(newItem, WTFMove(removedItems));
164 }
165
166 void WebBackForwardList::goToItem(WebBackForwardListItem* item)
167 {
168     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
169
170     if (!m_entries.size() || !item || !m_page || !m_hasCurrentIndex)
171         return;
172
173     size_t targetIndex = m_entries.find(item);
174
175     // If the target item wasn't even in the list, there's nothing else to do.
176     if (targetIndex == notFound)
177         return;
178
179     if (targetIndex < m_currentIndex) {
180         unsigned delta = m_entries.size() - targetIndex - 1;
181         String deltaValue = delta > 10 ? ASCIILiteral("over10") : String::number(delta);
182         m_page->logDiagnosticMessage(WebCore::DiagnosticLoggingKeys::backNavigationDeltaKey(), deltaValue, false /* shouldSample */);
183     }
184
185     // If we're going to an item different from the current item, ask the client if the current
186     // item should remain in the list.
187     WebBackForwardListItem* currentItem = m_entries[m_currentIndex].get();
188     bool shouldKeepCurrentItem = true;
189     if (currentItem != item) {
190         m_page->recordAutomaticNavigationSnapshot();
191         shouldKeepCurrentItem = m_page->shouldKeepCurrentBackForwardListItemInList(m_entries[m_currentIndex].get());
192     }
193
194     // If the client said to remove the current item, remove it and then update the target index.
195     Vector<RefPtr<WebBackForwardListItem>> removedItems;
196     if (!shouldKeepCurrentItem) {
197         removedItems.append(currentItem);
198         m_entries.remove(m_currentIndex);
199         targetIndex = m_entries.find(item);
200
201         ASSERT(targetIndex != notFound);
202     }
203
204     m_currentIndex = targetIndex;
205     m_page->didChangeBackForwardList(nullptr, removedItems);
206 }
207
208 WebBackForwardListItem* WebBackForwardList::currentItem() const
209 {
210     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
211
212     return m_page && m_hasCurrentIndex ? m_entries[m_currentIndex].get() : nullptr;
213 }
214
215 WebBackForwardListItem* WebBackForwardList::backItem() const
216 {
217     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
218
219     return m_page && m_hasCurrentIndex && m_currentIndex ? m_entries[m_currentIndex - 1].get() : nullptr;
220 }
221
222 WebBackForwardListItem* WebBackForwardList::forwardItem() const
223 {
224     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
225
226     return m_page && m_hasCurrentIndex && m_entries.size() && m_currentIndex < m_entries.size() - 1 ? m_entries[m_currentIndex + 1].get() : nullptr;
227 }
228
229 WebBackForwardListItem* WebBackForwardList::itemAtIndex(int index) const
230 {
231     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
232
233     if (!m_hasCurrentIndex || !m_page)
234         return nullptr;
235     
236     // Do range checks without doing math on index to avoid overflow.
237     if (index < -backListCount())
238         return nullptr;
239     
240     if (index > forwardListCount())
241         return nullptr;
242         
243     return m_entries[index + m_currentIndex].get();
244 }
245
246 int WebBackForwardList::backListCount() const
247 {
248     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
249
250     return m_page && m_hasCurrentIndex ? m_currentIndex : 0;
251 }
252
253 int WebBackForwardList::forwardListCount() const
254 {
255     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
256
257     return m_page && m_hasCurrentIndex ? m_entries.size() - (m_currentIndex + 1) : 0;
258 }
259
260 Ref<API::Array> WebBackForwardList::backList() const
261 {
262     return backListAsAPIArrayWithLimit(backListCount());
263 }
264
265 Ref<API::Array> WebBackForwardList::forwardList() const
266 {
267     return forwardListAsAPIArrayWithLimit(forwardListCount());
268 }
269
270 Ref<API::Array> WebBackForwardList::backListAsAPIArrayWithLimit(unsigned limit) const
271 {
272     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
273
274     if (!m_page || !m_hasCurrentIndex)
275         return API::Array::create();
276
277     unsigned backListSize = static_cast<unsigned>(backListCount());
278     unsigned size = std::min(backListSize, limit);
279     if (!size)
280         return API::Array::create();
281
282     Vector<RefPtr<API::Object>> vector;
283     vector.reserveInitialCapacity(size);
284
285     ASSERT(backListSize >= size);
286     for (unsigned i = backListSize - size; i < backListSize; ++i) {
287         ASSERT(m_entries[i]);
288         vector.uncheckedAppend(m_entries[i].get());
289     }
290
291     return API::Array::create(WTFMove(vector));
292 }
293
294 Ref<API::Array> WebBackForwardList::forwardListAsAPIArrayWithLimit(unsigned limit) const
295 {
296     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
297
298     if (!m_page || !m_hasCurrentIndex)
299         return API::Array::create();
300
301     unsigned size = std::min(static_cast<unsigned>(forwardListCount()), limit);
302     if (!size)
303         return API::Array::create();
304
305     Vector<RefPtr<API::Object>> vector;
306     vector.reserveInitialCapacity(size);
307
308     unsigned last = m_currentIndex + size;
309     ASSERT(last < m_entries.size());
310     for (unsigned i = m_currentIndex + 1; i <= last; ++i) {
311         ASSERT(m_entries[i]);
312         vector.uncheckedAppend(m_entries[i].get());
313     }
314
315     return API::Array::create(WTFMove(vector));
316 }
317
318 void WebBackForwardList::removeAllItems()
319 {
320     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
321
322     Vector<RefPtr<WebBackForwardListItem>> removedItems;
323
324     for (auto& entry : m_entries) {
325         ASSERT(entry);
326         if (!entry)
327             continue;
328
329         didRemoveItem(*entry);
330         removedItems.append(WTFMove(entry));
331     }
332
333     m_entries.clear();
334     m_hasCurrentIndex = false;
335     m_page->didChangeBackForwardList(nullptr, WTFMove(removedItems));
336 }
337
338 void WebBackForwardList::clear()
339 {
340     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
341
342     size_t size = m_entries.size();
343     if (!m_page || size <= 1)
344         return;
345
346     RefPtr<WebBackForwardListItem> currentItem = this->currentItem();
347     Vector<RefPtr<WebBackForwardListItem>> removedItems;
348
349     if (!currentItem) {
350         // We should only ever have no current item if we also have no current item index.
351         ASSERT(!m_hasCurrentIndex);
352
353         // But just in case it does happen in practice we should get back into a consistent state now.
354         for (size_t i = 0; i < size; ++i) {
355             ASSERT(m_entries[i]);
356             if (!m_entries[i])
357                 continue;
358
359             didRemoveItem(*m_entries[i]);
360             removedItems.append(WTFMove(m_entries[i]));
361         }
362
363         m_entries.clear();
364         m_hasCurrentIndex = false;
365         m_page->didChangeBackForwardList(nullptr, WTFMove(removedItems));
366
367         return;
368     }
369
370     for (size_t i = 0; i < size; ++i) {
371         ASSERT(m_entries[i]);
372         if (m_entries[i] && m_entries[i] != currentItem)
373             didRemoveItem(*m_entries[i]);
374     }
375
376     removedItems.reserveCapacity(size - 1);
377     for (size_t i = 0; i < size; ++i) {
378         if (i != m_currentIndex && m_hasCurrentIndex && m_entries[i])
379             removedItems.append(WTFMove(m_entries[i]));
380     }
381
382     m_currentIndex = 0;
383
384     if (currentItem) {
385         m_entries.shrink(1);
386         m_entries[0] = WTFMove(currentItem);
387     } else {
388         m_entries.clear();
389         m_hasCurrentIndex = false;
390     }
391
392     m_page->didChangeBackForwardList(nullptr, WTFMove(removedItems));
393 }
394
395 BackForwardListState WebBackForwardList::backForwardListState(const std::function<bool (WebBackForwardListItem&)>& filter) const
396 {
397     ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
398
399     BackForwardListState backForwardListState;
400     if (m_hasCurrentIndex)
401         backForwardListState.currentIndex = m_currentIndex;
402
403     for (size_t i = 0; i < m_entries.size(); ++i) {
404         auto& entry = *m_entries[i];
405
406         if (filter && !filter(entry)) {
407             auto& currentIndex = backForwardListState.currentIndex;
408             if (currentIndex && i <= currentIndex.value() && currentIndex.value())
409                 --currentIndex.value();
410
411             continue;
412         }
413
414         backForwardListState.items.append(entry.itemState());
415     }
416
417     if (backForwardListState.items.isEmpty())
418         backForwardListState.currentIndex = std::nullopt;
419     else if (backForwardListState.items.size() <= backForwardListState.currentIndex.value())
420         backForwardListState.currentIndex = backForwardListState.items.size() - 1;
421
422     return backForwardListState;
423 }
424
425 void WebBackForwardList::restoreFromState(BackForwardListState backForwardListState)
426 {
427     Vector<RefPtr<WebBackForwardListItem>> items;
428     items.reserveInitialCapacity(backForwardListState.items.size());
429
430     for (auto& backForwardListItemState : backForwardListState.items) {
431         backForwardListItemState.identifier = generateWebBackForwardItemID();
432         items.uncheckedAppend(WebBackForwardListItem::create(WTFMove(backForwardListItemState), m_page->pageID()));
433     }
434     m_hasCurrentIndex = !!backForwardListState.currentIndex;
435     m_currentIndex = backForwardListState.currentIndex.value_or(0);
436     m_entries = WTFMove(items);
437 }
438
439 Vector<BackForwardListItemState> WebBackForwardList::itemStates() const
440 {
441     Vector<BackForwardListItemState> itemStates;
442     itemStates.reserveInitialCapacity(m_entries.size());
443
444     for (const auto& entry : m_entries)
445         itemStates.uncheckedAppend(entry->itemState());
446
447     return itemStates;
448 }
449
450 void WebBackForwardList::didRemoveItem(WebBackForwardListItem& backForwardListItem)
451 {
452     m_page->backForwardRemovedItem(backForwardListItem.itemID());
453
454 #if PLATFORM(COCOA)
455     backForwardListItem.setSnapshot(nullptr);
456 #endif
457 }
458
459 } // namespace WebKit