| 1 | /* |
| 2 | * Copyright (C) 2014 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 "ViewSnapshotStore.h" |
| 28 | |
| 29 | #include "WebBackForwardList.h" |
| 30 | #include "WebPageProxy.h" |
| 31 | #include <wtf/NeverDestroyed.h> |
| 32 | |
| 33 | #if PLATFORM(IOS_FAMILY) |
| 34 | static const size_t maximumSnapshotCacheSize = 50 * (1024 * 1024); |
| 35 | #else |
| 36 | static const size_t maximumSnapshotCacheSize = 400 * (1024 * 1024); |
| 37 | #endif |
| 38 | |
| 39 | namespace WebKit { |
| 40 | using namespace WebCore; |
| 41 | |
| 42 | ViewSnapshotStore::ViewSnapshotStore() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | ViewSnapshotStore::~ViewSnapshotStore() |
| 47 | { |
| 48 | discardSnapshotImages(); |
| 49 | } |
| 50 | |
| 51 | ViewSnapshotStore& ViewSnapshotStore::singleton() |
| 52 | { |
| 53 | static NeverDestroyed<ViewSnapshotStore> store; |
| 54 | return store; |
| 55 | } |
| 56 | |
| 57 | void ViewSnapshotStore::didAddImageToSnapshot(ViewSnapshot& snapshot) |
| 58 | { |
| 59 | bool isNewEntry = m_snapshotsWithImages.add(&snapshot).isNewEntry; |
| 60 | ASSERT_UNUSED(isNewEntry, isNewEntry); |
| 61 | m_snapshotCacheSize += snapshot.imageSizeInBytes(); |
| 62 | } |
| 63 | |
| 64 | void ViewSnapshotStore::willRemoveImageFromSnapshot(ViewSnapshot& snapshot) |
| 65 | { |
| 66 | bool removed = m_snapshotsWithImages.remove(&snapshot); |
| 67 | ASSERT_UNUSED(removed, removed); |
| 68 | m_snapshotCacheSize -= snapshot.imageSizeInBytes(); |
| 69 | } |
| 70 | |
| 71 | void ViewSnapshotStore::pruneSnapshots(WebPageProxy& webPageProxy) |
| 72 | { |
| 73 | if (m_snapshotCacheSize <= maximumSnapshotCacheSize) |
| 74 | return; |
| 75 | |
| 76 | ASSERT(!m_snapshotsWithImages.isEmpty()); |
| 77 | |
| 78 | // FIXME: We have enough information to do smarter-than-LRU eviction (making use of the back-forward lists, etc.) |
| 79 | |
| 80 | m_snapshotsWithImages.first()->clearImage(); |
| 81 | } |
| 82 | |
| 83 | void ViewSnapshotStore::recordSnapshot(WebPageProxy& webPageProxy, WebBackForwardListItem& item) |
| 84 | { |
| 85 | if (webPageProxy.isShowingNavigationGestureSnapshot()) |
| 86 | return; |
| 87 | |
| 88 | pruneSnapshots(webPageProxy); |
| 89 | |
| 90 | webPageProxy.willRecordNavigationSnapshot(item); |
| 91 | |
| 92 | auto snapshot = webPageProxy.takeViewSnapshot(); |
| 93 | if (!snapshot) |
| 94 | return; |
| 95 | |
| 96 | snapshot->setRenderTreeSize(webPageProxy.renderTreeSize()); |
| 97 | snapshot->setDeviceScaleFactor(webPageProxy.deviceScaleFactor()); |
| 98 | snapshot->setBackgroundColor(webPageProxy.pageExtendedBackgroundColor()); |
| 99 | snapshot->setViewScrollPosition(WebCore::roundedIntPoint(webPageProxy.viewScrollPosition())); |
| 100 | |
| 101 | item.setSnapshot(WTFMove(snapshot)); |
| 102 | } |
| 103 | |
| 104 | void ViewSnapshotStore::discardSnapshotImages() |
| 105 | { |
| 106 | while (!m_snapshotsWithImages.isEmpty()) |
| 107 | m_snapshotsWithImages.first()->clearImage(); |
| 108 | } |
| 109 | |
| 110 | ViewSnapshot::~ViewSnapshot() |
| 111 | { |
| 112 | clearImage(); |
| 113 | } |
| 114 | |
| 115 | } // namespace WebKit |
| 116 | |