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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
/*
* Copyright (C) 2007, 2014, 2015 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. ``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
* 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 "PageCache.h"
#include "ApplicationCacheHost.h"
#include "BackForwardController.h"
#include "MemoryCache.h"
#include "CachedPage.h"
#include "DOMWindow.h"
#include "DeviceMotionController.h"
#include "DeviceOrientationController.h"
#include "DiagnosticLoggingClient.h"
#include "DiagnosticLoggingKeys.h"
#include "Document.h"
#include "DocumentLoader.h"
#include "FocusController.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "FrameView.h"
#include "HistoryController.h"
#include "IgnoreOpensDuringUnloadCountIncrementer.h"
#include "Logging.h"
#include "MainFrame.h"
#include "MemoryPressureHandler.h"
#include "Page.h"
#include "Settings.h"
#include "SubframeLoader.h"
#include <wtf/CurrentTime.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/TemporaryChange.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringConcatenate.h>
#if ENABLE(PROXIMITY_EVENTS)
#include "DeviceProximityController.h"
#endif
namespace WebCore {
#define PCLOG(...) LOG(PageCache, "%*s%s", indentLevel*4, "", makeString(__VA_ARGS__).utf8().data())
static inline void logPageCacheFailureDiagnosticMessage(DiagnosticLoggingClient& client, const String& reason)
{
client.logDiagnosticMessageWithValue(DiagnosticLoggingKeys::pageCacheKey(), DiagnosticLoggingKeys::failureKey(), reason, ShouldSample::Yes);
}
static inline void logPageCacheFailureDiagnosticMessage(Page* page, const String& reason)
{
if (!page)
return;
logPageCacheFailureDiagnosticMessage(page->mainFrame().diagnosticLoggingClient(), reason);
}
static bool canCacheFrame(Frame& frame, DiagnosticLoggingClient& diagnosticLoggingClient, unsigned indentLevel)
{
PCLOG("+---");
FrameLoader& frameLoader = frame.loader();
// Prevent page caching if a subframe is still in provisional load stage.
// We only do this check for subframes because the main frame is reused when navigating to a new page.
if (!frame.isMainFrame() && frameLoader.state() == FrameStateProvisional) {
PCLOG(" -Frame is in provisional load stage");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::provisionalLoadKey());
return false;
}
DocumentLoader* documentLoader = frameLoader.documentLoader();
if (!documentLoader) {
PCLOG(" -There is no DocumentLoader object");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::noDocumentLoaderKey());
return false;
}
URL currentURL = documentLoader->url();
URL newURL = frameLoader.provisionalDocumentLoader() ? frameLoader.provisionalDocumentLoader()->url() : URL();
if (!newURL.isEmpty())
PCLOG(" Determining if frame can be cached navigating from (", currentURL.string(), ") to (", newURL.string(), "):");
else
PCLOG(" Determining if subframe with URL (", currentURL.string(), ") can be cached:");
bool isCacheable = true;
if (!documentLoader->mainDocumentError().isNull()) {
PCLOG(" -Main document has an error");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::mainDocumentErrorKey());
if (documentLoader->mainDocumentError().isCancellation() && documentLoader->subresourceLoadersArePageCacheAcceptable())
PCLOG(" -But, it was a cancellation and all loaders during the cancelation were loading images or XHR.");
else
isCacheable = false;
}
// Do not cache error pages (these can be recognized as pages with substitute data or unreachable URLs).
if (documentLoader->substituteData().isValid() && !documentLoader->substituteData().failingURL().isEmpty()) {
PCLOG(" -Frame is an error page");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::isErrorPageKey());
isCacheable = false;
}
if (frameLoader.subframeLoader().containsPlugins() && !frame.page()->settings().pageCacheSupportsPlugins()) {
PCLOG(" -Frame contains plugins");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::hasPluginsKey());
isCacheable = false;
}
if (frame.isMainFrame() && frame.document() && frame.document()->url().protocolIs("https") && documentLoader->response().cacheControlContainsNoStore()) {
PCLOG(" -Frame is HTTPS, and cache control prohibits storing");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::httpsNoStoreKey());
isCacheable = false;
}
if (frame.isMainFrame() && !frameLoader.history().currentItem()) {
PCLOG(" -Main frame has no current history item");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::noCurrentHistoryItemKey());
isCacheable = false;
}
if (frameLoader.quickRedirectComing()) {
PCLOG(" -Quick redirect is coming");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::quirkRedirectComingKey());
isCacheable = false;
}
if (documentLoader->isLoading()) {
PCLOG(" -DocumentLoader is still loading");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::isLoadingKey());
isCacheable = false;
}
if (documentLoader->isStopping()) {
PCLOG(" -DocumentLoader is in the middle of stopping");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::documentLoaderStoppingKey());
isCacheable = false;
}
Vector<ActiveDOMObject*> unsuspendableObjects;
if (frame.document() && !frame.document()->canSuspendActiveDOMObjectsForDocumentSuspension(&unsuspendableObjects)) {
PCLOG(" -The document cannot suspend its active DOM Objects");
for (auto* activeDOMObject : unsuspendableObjects) {
PCLOG(" - Unsuspendable: ", activeDOMObject->activeDOMObjectName());
diagnosticLoggingClient.logDiagnosticMessageWithValue(DiagnosticLoggingKeys::pageCacheKey(), DiagnosticLoggingKeys::unsuspendableDOMObjectKey(), activeDOMObject->activeDOMObjectName(), ShouldSample::Yes);
UNUSED_PARAM(activeDOMObject);
}
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::cannotSuspendActiveDOMObjectsKey());
isCacheable = false;
}
// FIXME: We should investigating caching frames that have an associated
// application cache. <rdar://problem/5917899> tracks that work.
if (!documentLoader->applicationCacheHost()->canCacheInPageCache()) {
PCLOG(" -The DocumentLoader uses an application cache");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::applicationCacheKey());
isCacheable = false;
}
if (!frameLoader.client().canCachePage()) {
PCLOG(" -The client says this frame cannot be cached");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::deniedByClientKey());
isCacheable = false;
}
for (Frame* child = frame.tree().firstChild(); child; child = child->tree().nextSibling()) {
if (!canCacheFrame(*child, diagnosticLoggingClient, indentLevel + 1))
isCacheable = false;
}
PCLOG(isCacheable ? " Frame CAN be cached" : " Frame CANNOT be cached");
PCLOG("+---");
return isCacheable;
}
static bool canCachePage(Page& page)
{
unsigned indentLevel = 0;
PCLOG("--------\n Determining if page can be cached:");
MainFrame& mainFrame = page.mainFrame();
DiagnosticLoggingClient& diagnosticLoggingClient = mainFrame.diagnosticLoggingClient();
bool isCacheable = canCacheFrame(mainFrame, diagnosticLoggingClient, indentLevel + 1);
if (!page.settings().usesPageCache()) {
PCLOG(" -Page settings says b/f cache disabled");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::isDisabledKey());
isCacheable = false;
}
#if ENABLE(DEVICE_ORIENTATION) && !PLATFORM(IOS)
if (DeviceMotionController::isActiveAt(&page)) {
PCLOG(" -Page is using DeviceMotion");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::deviceMotionKey());
isCacheable = false;
}
if (DeviceOrientationController::isActiveAt(&page)) {
PCLOG(" -Page is using DeviceOrientation");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::deviceOrientationKey());
isCacheable = false;
}
#endif
#if ENABLE(PROXIMITY_EVENTS)
if (DeviceProximityController::isActiveAt(page)) {
PCLOG(" -Page is using DeviceProximity");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, deviceProximityKey);
isCacheable = false;
}
#endif
FrameLoadType loadType = page.mainFrame().loader().loadType();
switch (loadType) {
case FrameLoadType::Reload:
// No point writing to the cache on a reload, since we will just write over it again when we leave that page.
PCLOG(" -Load type is: Reload");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::reloadKey());
isCacheable = false;
break;
case FrameLoadType::Same: // user loads same URL again (but not reload button)
// No point writing to the cache on a same load, since we will just write over it again when we leave that page.
PCLOG(" -Load type is: Same");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::sameLoadKey());
isCacheable = false;
break;
case FrameLoadType::RedirectWithLockedBackForwardList:
// Don't write to the cache if in the middle of a redirect, since we will want to store the final page we end up on.
PCLOG(" -Load type is: RedirectWithLockedBackForwardList");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::redirectKey());
isCacheable = false;
break;
case FrameLoadType::Replace:
// No point writing to the cache on a replace, since we will just write over it again when we leave that page.
PCLOG(" -Load type is: Replace");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::replaceKey());
isCacheable = false;
break;
case FrameLoadType::ReloadFromOrigin: {
// No point writing to the cache on a reload, since we will just write over it again when we leave that page.
PCLOG(" -Load type is: ReloadFromOrigin");
logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::reloadFromOriginKey());
isCacheable = false;
break;
}
case FrameLoadType::Standard:
case FrameLoadType::Back:
case FrameLoadType::Forward:
case FrameLoadType::IndexedBackForward: // a multi-item hop in the backforward list
// Cacheable.
break;
}
if (isCacheable)
PCLOG(" Page CAN be cached\n--------");
else
PCLOG(" Page CANNOT be cached\n--------");
diagnosticLoggingClient.logDiagnosticMessageWithResult(DiagnosticLoggingKeys::pageCacheKey(), DiagnosticLoggingKeys::canCacheKey(), isCacheable ? DiagnosticLoggingResultPass : DiagnosticLoggingResultFail, ShouldSample::Yes);
return isCacheable;
}
PageCache& PageCache::singleton()
{
static NeverDestroyed<PageCache> globalPageCache;
return globalPageCache;
}
bool PageCache::canCache(Page& page) const
{
if (!m_maxSize) {
logPageCacheFailureDiagnosticMessage(&page, DiagnosticLoggingKeys::isDisabledKey());
return false;
}
if (MemoryPressureHandler::singleton().isUnderMemoryPressure()) {
logPageCacheFailureDiagnosticMessage(&page, DiagnosticLoggingKeys::underMemoryPressureKey());
return false;
}
return canCachePage(page);
}
void PageCache::pruneToSizeNow(unsigned size, PruningReason pruningReason)
{
TemporaryChange<unsigned> change(m_maxSize, size);
prune(pruningReason);
}
void PageCache::setMaxSize(unsigned maxSize)
{
m_maxSize = maxSize;
prune(PruningReason::None);
}
unsigned PageCache::frameCount() const
{
unsigned frameCount = m_items.size();
for (auto& item : m_items) {
ASSERT(item->m_cachedPage);
frameCount += item->m_cachedPage->cachedMainFrame()->descendantFrameCount();
}
return frameCount;
}
void PageCache::markPagesForDeviceOrPageScaleChanged(Page& page)
{
for (auto& item : m_items) {
CachedPage& cachedPage = *item->m_cachedPage;
if (&page.mainFrame() == &cachedPage.cachedMainFrame()->view()->frame())
cachedPage.markForDeviceOrPageScaleChanged();
}
}
void PageCache::markPagesForContentsSizeChanged(Page& page)
{
for (auto& item : m_items) {
CachedPage& cachedPage = *item->m_cachedPage;
if (&page.mainFrame() == &cachedPage.cachedMainFrame()->view()->frame())
cachedPage.markForContentsSizeChanged();
}
}
#if ENABLE(VIDEO_TRACK)
void PageCache::markPagesForCaptionPreferencesChanged()
{
for (auto& item : m_items) {
ASSERT(item->m_cachedPage);
item->m_cachedPage->markForCaptionPreferencesChanged();
}
}
#endif
static String pruningReasonToDiagnosticLoggingKey(PruningReason pruningReason)
{
switch (pruningReason) {
case PruningReason::MemoryPressure:
return DiagnosticLoggingKeys::prunedDueToMemoryPressureKey();
case PruningReason::ProcessSuspended:
return DiagnosticLoggingKeys::prunedDueToProcessSuspended();
case PruningReason::ReachedMaxSize:
return DiagnosticLoggingKeys::prunedDueToMaxSizeReached();
case PruningReason::None:
break;
}
ASSERT_NOT_REACHED();
return emptyString();
}
static void setPageCacheState(Page& page, Document::PageCacheState pageCacheState)
{
for (Frame* frame = &page.mainFrame(); frame; frame = frame->tree().traverseNext()) {
if (auto* document = frame->document())
document->setPageCacheState(pageCacheState);
}
}
static void firePageHideEventRecursively(Frame& frame)
{
auto* document = frame.document();
if (!document)
return;
// stopLoading() will fire the pagehide event in each subframe and the HTML specification states
// that the parent document's ignore-opens-during-unload counter should be incremented while the
// pagehide event is being fired in its subframes:
// https://html.spec.whatwg.org/multipage/browsers.html#unload-a-document
IgnoreOpensDuringUnloadCountIncrementer ignoreOpensDuringUnloadCountIncrementer(document);
frame.loader().stopLoading(UnloadEventPolicyUnloadAndPageHide);
for (RefPtr<Frame> child = frame.tree().firstChild(); child; child = child->tree().nextSibling())
firePageHideEventRecursively(*child);
}
void PageCache::addIfCacheable(HistoryItem& item, Page* page)
{
if (item.isInPageCache())
return;
if (!page || !canCache(*page))
return;
setPageCacheState(*page, Document::AboutToEnterPageCache);
// Focus the main frame, defocusing a focused subframe (if we have one). We do this here,
// before the page enters the page cache, while we still can dispatch DOM blur/focus events.
if (page->focusController().focusedFrame())
page->focusController().setFocusedFrame(&page->mainFrame());
// Fire the pagehide event in all frames.
firePageHideEventRecursively(page->mainFrame());
// Check that the page is still page-cacheable after firing the pagehide event. The JS event handlers
// could have altered the page in a way that could prevent caching.
if (!canCache(*page)) {
setPageCacheState(*page, Document::NotInPageCache);
return;
}
setPageCacheState(*page, Document::InPageCache);
// Make sure we no longer fire any JS events past this point.
NoEventDispatchAssertion assertNoEventDispatch;
item.m_cachedPage = std::make_unique<CachedPage>(*page);
item.m_pruningReason = PruningReason::None;
m_items.add(&item);
prune(PruningReason::ReachedMaxSize);
}
std::unique_ptr<CachedPage> PageCache::take(HistoryItem& item, Page* page)
{
if (!item.m_cachedPage) {
if (item.m_pruningReason != PruningReason::None)
logPageCacheFailureDiagnosticMessage(page, pruningReasonToDiagnosticLoggingKey(item.m_pruningReason));
return nullptr;
}
m_items.remove(&item);
std::unique_ptr<CachedPage> cachedPage = WTFMove(item.m_cachedPage);
if (cachedPage->hasExpired()) {
LOG(PageCache, "Not restoring page for %s from back/forward cache because cache entry has expired", item.url().string().ascii().data());
logPageCacheFailureDiagnosticMessage(page, DiagnosticLoggingKeys::expiredKey());
return nullptr;
}
return cachedPage;
}
CachedPage* PageCache::get(HistoryItem& item, Page* page)
{
CachedPage* cachedPage = item.m_cachedPage.get();
if (!cachedPage) {
if (item.m_pruningReason != PruningReason::None)
logPageCacheFailureDiagnosticMessage(page, pruningReasonToDiagnosticLoggingKey(item.m_pruningReason));
return nullptr;
}
if (cachedPage->hasExpired()) {
LOG(PageCache, "Not restoring page for %s from back/forward cache because cache entry has expired", item.url().string().ascii().data());
logPageCacheFailureDiagnosticMessage(page, DiagnosticLoggingKeys::expiredKey());
remove(item);
return nullptr;
}
return cachedPage;
}
void PageCache::remove(HistoryItem& item)
{
// Safely ignore attempts to remove items not in the cache.
if (!item.m_cachedPage)
return;
m_items.remove(&item);
item.m_cachedPage = nullptr;
}
void PageCache::prune(PruningReason pruningReason)
{
while (pageCount() > maxSize()) {
auto oldestItem = m_items.takeFirst();
oldestItem->m_cachedPage = nullptr;
oldestItem->m_pruningReason = pruningReason;
}
}
} // namespace WebCore
|