1 | /* |
2 | * Copyright (C) 2007, 2015 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. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "HistoryItem.h" |
29 | #include <wtf/Forward.h> |
30 | #include <wtf/ListHashSet.h> |
31 | #include <wtf/Noncopyable.h> |
32 | |
33 | namespace WebCore { |
34 | |
35 | class CachedPage; |
36 | class Frame; |
37 | class Page; |
38 | |
39 | enum class PruningReason { None, ProcessSuspended, MemoryPressure, ReachedMaxSize }; |
40 | |
41 | class PageCache { |
42 | WTF_MAKE_NONCOPYABLE(PageCache); WTF_MAKE_FAST_ALLOCATED; |
43 | public: |
44 | // Function to obtain the global page cache. |
45 | WEBCORE_EXPORT static PageCache& singleton(); |
46 | |
47 | bool canCache(Page&) const; |
48 | |
49 | // Used when memory is low to prune some cached pages. |
50 | WEBCORE_EXPORT void pruneToSizeNow(unsigned maxSize, PruningReason); |
51 | WEBCORE_EXPORT void setMaxSize(unsigned); // number of pages to cache. |
52 | unsigned maxSize() const { return m_maxSize; } |
53 | |
54 | WEBCORE_EXPORT bool addIfCacheable(HistoryItem&, Page*); // Prunes if maxSize() is exceeded. |
55 | WEBCORE_EXPORT void remove(HistoryItem&); |
56 | CachedPage* get(HistoryItem&, Page*); |
57 | std::unique_ptr<CachedPage> take(HistoryItem&, Page*); |
58 | |
59 | void removeAllItemsForPage(Page&); |
60 | |
61 | unsigned pageCount() const { return m_items.size(); } |
62 | WEBCORE_EXPORT unsigned frameCount() const; |
63 | |
64 | void markPagesForDeviceOrPageScaleChanged(Page&); |
65 | void markPagesForContentsSizeChanged(Page&); |
66 | #if ENABLE(VIDEO_TRACK) |
67 | void markPagesForCaptionPreferencesChanged(); |
68 | #endif |
69 | |
70 | private: |
71 | PageCache(); |
72 | ~PageCache() = delete; // Make sure nobody accidentally calls delete -- WebCore does not delete singletons. |
73 | |
74 | static bool canCachePageContainingThisFrame(Frame&); |
75 | |
76 | void prune(PruningReason); |
77 | void dump() const; |
78 | |
79 | ListHashSet<RefPtr<HistoryItem>> m_items; |
80 | unsigned m_maxSize {0}; |
81 | |
82 | #if !ASSERT_DISABLED |
83 | bool m_isInRemoveAllItemsForPage { false }; |
84 | #endif |
85 | |
86 | friend class WTF::NeverDestroyed<PageCache>; |
87 | }; |
88 | |
89 | } // namespace WebCore |
90 | |