Add WebKit Legacy SPI to retrieve editable elements in rect
[WebKit-https.git] / Source / WebCore / page / Page.cpp
1 /*
2  * Copyright (C) 2006-2017 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "Page.h"
22
23 #include "ActivityStateChangeObserver.h"
24 #include "AlternativeTextClient.h"
25 #include "ApplicationCacheStorage.h"
26 #include "ApplicationStateChangeListener.h"
27 #include "AuthenticatorCoordinator.h"
28 #include "BackForwardCache.h"
29 #include "BackForwardClient.h"
30 #include "BackForwardController.h"
31 #include "CSSAnimationController.h"
32 #include "CacheStorageProvider.h"
33 #include "Chrome.h"
34 #include "ChromeClient.h"
35 #include "ConstantPropertyMap.h"
36 #include "ContextMenuClient.h"
37 #include "ContextMenuController.h"
38 #include "CookieJar.h"
39 #include "CustomHeaderFields.h"
40 #include "DOMRect.h"
41 #include "DOMRectList.h"
42 #include "DatabaseProvider.h"
43 #include "DiagnosticLoggingClient.h"
44 #include "DiagnosticLoggingKeys.h"
45 #include "DocumentLoader.h"
46 #include "DocumentMarkerController.h"
47 #include "DocumentTimeline.h"
48 #include "DragController.h"
49 #include "Editing.h"
50 #include "Editor.h"
51 #include "EditorClient.h"
52 #include "EmptyClients.h"
53 #include "Event.h"
54 #include "EventNames.h"
55 #include "ExtensionStyleSheets.h"
56 #include "FocusController.h"
57 #include "FrameLoader.h"
58 #include "FrameLoaderClient.h"
59 #include "FrameSelection.h"
60 #include "FrameTree.h"
61 #include "FrameView.h"
62 #include "FullscreenManager.h"
63 #include "HTMLElement.h"
64 #include "HTMLMediaElement.h"
65 #include "HTMLTextAreaElement.h"
66 #include "HTMLTextFormControlElement.h"
67 #include "HistoryController.h"
68 #include "HistoryItem.h"
69 #include "InspectorClient.h"
70 #include "InspectorController.h"
71 #include "InspectorInstrumentation.h"
72 #include "LegacySchemeRegistry.h"
73 #include "LibWebRTCProvider.h"
74 #include "LoaderStrategy.h"
75 #include "Logging.h"
76 #include "LowPowerModeNotifier.h"
77 #include "MediaCanStartListener.h"
78 #include "Navigator.h"
79 #include "PageConfiguration.h"
80 #include "PageConsoleClient.h"
81 #include "PageDebuggable.h"
82 #include "PageGroup.h"
83 #include "PageOverlayController.h"
84 #include "PaymentCoordinator.h"
85 #include "PerformanceLogging.h"
86 #include "PerformanceLoggingClient.h"
87 #include "PerformanceMonitor.h"
88 #include "PlatformMediaSessionManager.h"
89 #include "PlatformStrategies.h"
90 #include "PlugInClient.h"
91 #include "PluginData.h"
92 #include "PluginInfoProvider.h"
93 #include "PluginViewBase.h"
94 #include "PointerCaptureController.h"
95 #include "PointerLockController.h"
96 #include "ProgressTracker.h"
97 #include "RenderDescendantIterator.h"
98 #include "RenderLayerCompositor.h"
99 #include "RenderTheme.h"
100 #include "RenderView.h"
101 #include "RenderWidget.h"
102 #include "ResizeObserver.h"
103 #include "ResourceUsageOverlay.h"
104 #include "RuntimeEnabledFeatures.h"
105 #include "SVGDocumentExtensions.h"
106 #include "ScriptController.h"
107 #include "ScriptedAnimationController.h"
108 #include "ScrollLatchingState.h"
109 #include "ScrollingCoordinator.h"
110 #include "Settings.h"
111 #include "SharedBuffer.h"
112 #include "SocketProvider.h"
113 #include "StorageArea.h"
114 #include "StorageNamespace.h"
115 #include "StorageNamespaceProvider.h"
116 #include "StyleResolver.h"
117 #include "StyleScope.h"
118 #include "SubframeLoader.h"
119 #include "TextIterator.h"
120 #include "TextResourceDecoder.h"
121 #include "UserContentProvider.h"
122 #include "UserInputBridge.h"
123 #include "ValidationMessageClient.h"
124 #include "VisitedLinkState.h"
125 #include "VisitedLinkStore.h"
126 #include "VoidCallback.h"
127 #include "WheelEventDeltaFilter.h"
128 #include "Widget.h"
129 #include <wtf/Deque.h>
130 #include <wtf/FileSystem.h>
131 #include <wtf/RefCountedLeakCounter.h>
132 #include <wtf/StdLibExtras.h>
133 #include <wtf/SystemTracing.h>
134 #include <wtf/text/Base64.h>
135 #include <wtf/text/StringHash.h>
136
137 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
138 #include "HTMLVideoElement.h"
139 #include "MediaPlaybackTarget.h"
140 #endif
141
142 #if PLATFORM(MAC)
143 #include "ServicesOverlayController.h"
144 #endif
145
146 #if ENABLE(MEDIA_SESSION)
147 #include "MediaSessionManager.h"
148 #endif
149
150 #if ENABLE(INDEXED_DATABASE)
151 #include "IDBConnectionToServer.h"
152 #include "InProcessIDBServer.h"
153 #endif
154
155 #if ENABLE(DATA_INTERACTION)
156 #include "SelectionRect.h"
157 #endif
158
159 #if ENABLE(WEBGL)
160 #include "WebGLStateTracker.h"
161 #endif
162
163 namespace WebCore {
164
165 static HashSet<Page*>& allPages()
166 {
167     static NeverDestroyed<HashSet<Page*>> set;
168     return set;
169 }
170
171 static unsigned nonUtilityPageCount { 0 };
172
173 static inline bool isUtilityPageChromeClient(ChromeClient& chromeClient)
174 {
175     return chromeClient.isEmptyChromeClient() || chromeClient.isSVGImageChromeClient();
176 }
177
178 DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, pageCounter, ("Page"));
179
180 void Page::forEachPage(const WTF::Function<void(Page&)>& function)
181 {
182     for (auto* page : allPages())
183         function(*page);
184 }
185
186 void Page::updateValidationBubbleStateIfNeeded()
187 {
188     if (auto* client = validationMessageClient())
189         client->updateValidationBubbleStateIfNeeded();
190 }
191
192 static void networkStateChanged(bool isOnLine)
193 {
194     Vector<Ref<Frame>> frames;
195
196     // Get all the frames of all the pages in all the page groups
197     for (auto* page : allPages()) {
198         for (Frame* frame = &page->mainFrame(); frame; frame = frame->tree().traverseNext())
199             frames.append(*frame);
200         InspectorInstrumentation::networkStateChanged(*page);
201     }
202
203     auto& eventName = isOnLine ? eventNames().onlineEvent : eventNames().offlineEvent;
204     for (auto& frame : frames) {
205         if (!frame->document())
206             continue;
207         frame->document()->dispatchWindowEvent(Event::create(eventName, Event::CanBubble::No, Event::IsCancelable::No));
208     }
209 }
210
211 static constexpr OptionSet<ActivityState::Flag> pageInitialActivityState()
212 {
213     return { ActivityState::IsVisible, ActivityState::IsInWindow };
214 }
215
216 Page::Page(PageConfiguration&& pageConfiguration)
217     : m_chrome(makeUnique<Chrome>(*this, *pageConfiguration.chromeClient))
218     , m_dragCaretController(makeUnique<DragCaretController>())
219 #if ENABLE(DRAG_SUPPORT)
220     , m_dragController(makeUnique<DragController>(*this, *pageConfiguration.dragClient))
221 #endif
222     , m_focusController(makeUnique<FocusController>(*this, pageInitialActivityState()))
223 #if ENABLE(CONTEXT_MENUS)
224     , m_contextMenuController(makeUnique<ContextMenuController>(*this, *pageConfiguration.contextMenuClient))
225 #endif
226     , m_userInputBridge(makeUnique<UserInputBridge>(*this))
227     , m_inspectorController(makeUnique<InspectorController>(*this, pageConfiguration.inspectorClient))
228 #if ENABLE(POINTER_EVENTS)
229     , m_pointerCaptureController(makeUnique<PointerCaptureController>(*this))
230 #endif
231 #if ENABLE(POINTER_LOCK)
232     , m_pointerLockController(makeUnique<PointerLockController>(*this))
233 #endif
234     , m_settings(Settings::create(this))
235     , m_progress(makeUnique<ProgressTracker>(*pageConfiguration.progressTrackerClient))
236     , m_backForwardController(makeUnique<BackForwardController>(*this, WTFMove(pageConfiguration.backForwardClient)))
237     , m_mainFrame(Frame::create(this, nullptr, pageConfiguration.loaderClientForMainFrame))
238     , m_editorClient(WTFMove(pageConfiguration.editorClient))
239     , m_plugInClient(pageConfiguration.plugInClient)
240     , m_validationMessageClient(WTFMove(pageConfiguration.validationMessageClient))
241     , m_diagnosticLoggingClient(WTFMove(pageConfiguration.diagnosticLoggingClient))
242     , m_performanceLoggingClient(WTFMove(pageConfiguration.performanceLoggingClient))
243 #if ENABLE(WEBGL)
244     , m_webGLStateTracker(WTFMove(pageConfiguration.webGLStateTracker))
245 #endif
246 #if ENABLE(SPEECH_SYNTHESIS)
247     , m_speechSynthesisClient(WTFMove(pageConfiguration.speechSynthesisClient))
248 #endif
249     , m_libWebRTCProvider(WTFMove(pageConfiguration.libWebRTCProvider))
250     , m_verticalScrollElasticity(ScrollElasticityAllowed)
251     , m_horizontalScrollElasticity(ScrollElasticityAllowed)
252     , m_domTimerAlignmentInterval(DOMTimer::defaultAlignmentInterval())
253     , m_domTimerAlignmentIntervalIncreaseTimer(*this, &Page::domTimerAlignmentIntervalIncreaseTimerFired)
254     , m_activityState(pageInitialActivityState())
255     , m_alternativeTextClient(pageConfiguration.alternativeTextClient)
256     , m_consoleClient(makeUnique<PageConsoleClient>(*this))
257 #if ENABLE(REMOTE_INSPECTOR)
258     , m_inspectorDebuggable(makeUnique<PageDebuggable>(*this))
259 #endif
260     , m_socketProvider(WTFMove(pageConfiguration.socketProvider))
261     , m_cookieJar(WTFMove(pageConfiguration.cookieJar))
262     , m_applicationCacheStorage(*WTFMove(pageConfiguration.applicationCacheStorage))
263     , m_cacheStorageProvider(WTFMove(pageConfiguration.cacheStorageProvider))
264     , m_databaseProvider(*WTFMove(pageConfiguration.databaseProvider))
265     , m_pluginInfoProvider(*WTFMove(pageConfiguration.pluginInfoProvider))
266     , m_storageNamespaceProvider(*WTFMove(pageConfiguration.storageNamespaceProvider))
267     , m_userContentProvider(*WTFMove(pageConfiguration.userContentProvider))
268     , m_visitedLinkStore(*WTFMove(pageConfiguration.visitedLinkStore))
269     , m_sessionID(pageConfiguration.sessionID)
270 #if ENABLE(VIDEO)
271     , m_playbackControlsManagerUpdateTimer(*this, &Page::playbackControlsManagerUpdateTimerFired)
272 #endif
273     , m_isUtilityPage(isUtilityPageChromeClient(chrome().client()))
274     , m_performanceMonitor(isUtilityPage() ? nullptr : makeUnique<PerformanceMonitor>(*this))
275     , m_lowPowerModeNotifier(makeUnique<LowPowerModeNotifier>([this](bool isLowPowerModeEnabled) { handleLowModePowerChange(isLowPowerModeEnabled); }))
276     , m_performanceLogging(makeUnique<PerformanceLogging>(*this))
277 #if PLATFORM(MAC) && (ENABLE(SERVICE_CONTROLS) || ENABLE(TELEPHONE_NUMBER_DETECTION))
278     , m_servicesOverlayController(makeUnique<ServicesOverlayController>(*this))
279 #endif
280     , m_recentWheelEventDeltaFilter(WheelEventDeltaFilter::create())
281     , m_pageOverlayController(makeUnique<PageOverlayController>(*this))
282 #if ENABLE(APPLE_PAY)
283     , m_paymentCoordinator(makeUnique<PaymentCoordinator>(*pageConfiguration.paymentCoordinatorClient))
284 #endif
285 #if ENABLE(WEB_AUTHN)
286     , m_authenticatorCoordinator(makeUniqueRef<AuthenticatorCoordinator>(WTFMove(pageConfiguration.authenticatorCoordinatorClient)))
287 #endif
288 #if ENABLE(APPLICATION_MANIFEST)
289     , m_applicationManifest(pageConfiguration.applicationManifest)
290 #endif
291 {
292     updateTimerThrottlingState();
293
294     m_pluginInfoProvider->addPage(*this);
295     m_userContentProvider->addPage(*this);
296     m_visitedLinkStore->addPage(*this);
297
298     static bool addedListener;
299     if (!addedListener) {
300         platformStrategies()->loaderStrategy()->addOnlineStateChangeListener(&networkStateChanged);
301         addedListener = true;
302     }
303
304     ASSERT(!allPages().contains(this));
305     allPages().add(this);
306
307     if (!isUtilityPage()) {
308         ++nonUtilityPageCount;
309         MemoryPressureHandler::setPageCount(nonUtilityPageCount);
310     }
311
312 #ifndef NDEBUG
313     pageCounter.increment();
314 #endif
315
316 #if ENABLE(REMOTE_INSPECTOR)
317     if (m_inspectorController->inspectorClient() && m_inspectorController->inspectorClient()->allowRemoteInspectionToPageDirectly())
318         m_inspectorDebuggable->init();
319 #endif
320
321 #if PLATFORM(COCOA)
322     platformInitialize();
323 #endif
324
325 #if USE(LIBWEBRTC)
326     m_libWebRTCProvider->supportsVP8(RuntimeEnabledFeatures::sharedFeatures().webRTCVP8CodecEnabled());
327 #endif
328 }
329
330 Page::~Page()
331 {
332     ASSERT(!m_nestedRunLoopCount);
333     ASSERT(!m_unnestCallback);
334
335     m_validationMessageClient = nullptr;
336     m_diagnosticLoggingClient = nullptr;
337     m_performanceLoggingClient = nullptr;
338     m_mainFrame->setView(nullptr);
339     setGroupName(String());
340     allPages().remove(this);
341     if (!isUtilityPage()) {
342         --nonUtilityPageCount;
343         MemoryPressureHandler::setPageCount(nonUtilityPageCount);
344     }
345     
346     m_settings->pageDestroyed();
347
348     m_inspectorController->inspectedPageDestroyed();
349
350     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
351         frame->willDetachPage();
352         frame->detachFromPage();
353     }
354
355     if (m_plugInClient)
356         m_plugInClient->pageDestroyed();
357     if (m_alternativeTextClient)
358         m_alternativeTextClient->pageDestroyed();
359
360     if (m_scrollingCoordinator)
361         m_scrollingCoordinator->pageDestroyed();
362
363     backForward().close();
364     if (!isUtilityPage())
365         BackForwardCache::singleton().removeAllItemsForPage(*this);
366
367 #ifndef NDEBUG
368     pageCounter.decrement();
369 #endif
370
371     m_pluginInfoProvider->removePage(*this);
372     m_userContentProvider->removePage(*this);
373     m_visitedLinkStore->removePage(*this);
374 }
375
376 void Page::clearPreviousItemFromAllPages(HistoryItem* item)
377 {
378     for (auto* page : allPages()) {
379         auto& controller = page->mainFrame().loader().history();
380         if (item == controller.previousItem()) {
381             controller.clearPreviousItem();
382             return;
383         }
384     }
385 }
386
387 uint64_t Page::renderTreeSize() const
388 {
389     uint64_t total = 0;
390     for (const Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
391         if (!frame->document() || !frame->document()->renderView())
392             continue;
393         total += frame->document()->renderView()->rendererCount();
394     }
395     return total;
396 }
397
398 OptionSet<DisabledAdaptations> Page::disabledAdaptations() const
399 {
400     if (mainFrame().document())
401         return mainFrame().document()->disabledAdaptations();
402
403     return { };
404 }
405
406 ViewportArguments Page::viewportArguments() const
407 {
408     return mainFrame().document() ? mainFrame().document()->viewportArguments() : ViewportArguments();
409 }
410
411 void Page::setOverrideViewportArguments(const Optional<ViewportArguments>& viewportArguments)
412 {
413     if (viewportArguments == m_overrideViewportArguments)
414         return;
415
416     m_overrideViewportArguments = viewportArguments;
417     if (auto* document = mainFrame().document())
418         document->updateViewportArguments();
419 }
420
421 ScrollingCoordinator* Page::scrollingCoordinator()
422 {
423     if (!m_scrollingCoordinator && m_settings->scrollingCoordinatorEnabled()) {
424         m_scrollingCoordinator = chrome().client().createScrollingCoordinator(*this);
425         if (!m_scrollingCoordinator)
426             m_scrollingCoordinator = ScrollingCoordinator::create(this);
427     }
428
429     return m_scrollingCoordinator.get();
430 }
431
432 String Page::scrollingStateTreeAsText()
433 {
434     if (Document* document = m_mainFrame->document())
435         document->updateLayout();
436
437     if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
438         return scrollingCoordinator->scrollingStateTreeAsText();
439
440     return String();
441 }
442
443 String Page::synchronousScrollingReasonsAsText()
444 {
445     if (Document* document = m_mainFrame->document())
446         document->updateLayout();
447
448     if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
449         return scrollingCoordinator->synchronousScrollingReasonsAsText();
450
451     return String();
452 }
453
454 Ref<DOMRectList> Page::nonFastScrollableRects()
455 {
456     if (Document* document = m_mainFrame->document())
457         document->updateLayout();
458
459     Vector<IntRect> rects;
460     if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator()) {
461         const EventTrackingRegions& eventTrackingRegions = scrollingCoordinator->absoluteEventTrackingRegions();
462         for (const auto& synchronousEventRegion : eventTrackingRegions.eventSpecificSynchronousDispatchRegions)
463             rects.appendVector(synchronousEventRegion.value.rects());
464     }
465
466     Vector<FloatQuad> quads(rects.size());
467     for (size_t i = 0; i < rects.size(); ++i)
468         quads[i] = FloatRect(rects[i]);
469
470     return DOMRectList::create(quads);
471 }
472
473 Ref<DOMRectList> Page::touchEventRectsForEvent(const String& eventName)
474 {
475     if (Document* document = m_mainFrame->document()) {
476         document->updateLayout();
477 #if ENABLE(IOS_TOUCH_EVENTS)
478         document->updateTouchEventRegions();
479 #endif
480     }
481
482     Vector<IntRect> rects;
483     if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator()) {
484         const EventTrackingRegions& eventTrackingRegions = scrollingCoordinator->absoluteEventTrackingRegions();
485         const auto& region = eventTrackingRegions.eventSpecificSynchronousDispatchRegions.get(eventName);
486         rects.appendVector(region.rects());
487     }
488
489     Vector<FloatQuad> quads(rects.size());
490     for (size_t i = 0; i < rects.size(); ++i)
491         quads[i] = FloatRect(rects[i]);
492
493     return DOMRectList::create(quads);
494 }
495
496 Ref<DOMRectList> Page::passiveTouchEventListenerRects()
497 {
498     if (Document* document = m_mainFrame->document()) {
499         document->updateLayout();
500 #if ENABLE(IOS_TOUCH_EVENTS)
501         document->updateTouchEventRegions();
502 #endif  
503     }
504
505     Vector<IntRect> rects;
506     if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
507         rects.appendVector(scrollingCoordinator->absoluteEventTrackingRegions().asynchronousDispatchRegion.rects());
508
509     Vector<FloatQuad> quads(rects.size());
510     for (size_t i = 0; i < rects.size(); ++i)
511         quads[i] = FloatRect(rects[i]);
512
513     return DOMRectList::create(quads);
514 }
515
516 bool Page::openedByDOM() const
517 {
518     return m_openedByDOM;
519 }
520
521 void Page::setOpenedByDOM()
522 {
523     m_openedByDOM = true;
524 }
525
526 void Page::goToItem(HistoryItem& item, FrameLoadType type, ShouldTreatAsContinuingLoad shouldTreatAsContinuingLoad)
527 {
528     // stopAllLoaders may end up running onload handlers, which could cause further history traversals that may lead to the passed in HistoryItem
529     // being deref()-ed. Make sure we can still use it with HistoryController::goToItem later.
530     Ref<HistoryItem> protector(item);
531
532     auto& frameLoader = m_mainFrame->loader();
533     if (frameLoader.history().shouldStopLoadingForHistoryItem(item))
534         m_mainFrame->loader().stopAllLoadersAndCheckCompleteness();
535
536     m_mainFrame->loader().history().goToItem(item, type, shouldTreatAsContinuingLoad);
537 }
538
539 void Page::setGroupName(const String& name)
540 {
541     if (m_group && !m_group->name().isEmpty()) {
542         ASSERT(m_group != m_singlePageGroup.get());
543         ASSERT(!m_singlePageGroup);
544         m_group->removePage(*this);
545     }
546
547     if (name.isEmpty())
548         m_group = m_singlePageGroup.get();
549     else {
550         m_singlePageGroup = nullptr;
551         m_group = PageGroup::pageGroup(name);
552         m_group->addPage(*this);
553     }
554 }
555
556 const String& Page::groupName() const
557 {
558     return m_group ? m_group->name() : nullAtom().string();
559 }
560
561 void Page::initGroup()
562 {
563     ASSERT(!m_singlePageGroup);
564     ASSERT(!m_group);
565     m_singlePageGroup = makeUnique<PageGroup>(*this);
566     m_group = m_singlePageGroup.get();
567 }
568
569 void Page::updateStyleAfterChangeInEnvironment()
570 {
571     forEachDocument([](Document& document) {
572         if (StyleResolver* styleResolver = document.styleScope().resolverIfExists())
573             styleResolver->invalidateMatchedDeclarationsCache();
574         document.scheduleFullStyleRebuild();
575         document.styleScope().didChangeStyleSheetEnvironment();
576         document.scheduleTimedRenderingUpdate();
577     });
578 }
579
580 void Page::updateStyleForAllPagesAfterGlobalChangeInEnvironment()
581 {
582     for (auto* page : allPages())
583         page->updateStyleAfterChangeInEnvironment();
584 }
585
586 void Page::setNeedsRecalcStyleInAllFrames()
587 {
588     // FIXME: Figure out what this function is actually trying to add in different call sites.
589     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
590         if (Document* document = frame->document())
591             document->styleScope().didChangeStyleSheetEnvironment();
592     }
593 }
594
595 void Page::refreshPlugins(bool reload)
596 {
597     HashSet<PluginInfoProvider*> pluginInfoProviders;
598
599     for (auto* page : allPages())
600         pluginInfoProviders.add(&page->pluginInfoProvider());
601
602     for (auto& pluginInfoProvider : pluginInfoProviders)
603         pluginInfoProvider->refresh(reload);
604 }
605
606 PluginData& Page::pluginData()
607 {
608     if (!m_pluginData)
609         m_pluginData = PluginData::create(*this);
610     return *m_pluginData;
611 }
612
613 void Page::clearPluginData()
614 {
615     m_pluginData = nullptr;
616 }
617
618 bool Page::showAllPlugins() const
619 {
620     if (m_showAllPlugins)
621         return true;
622
623     if (Document* document = mainFrame().document())
624         return document->securityOrigin().isLocal();
625
626     return false;
627 }
628
629 inline Optional<std::pair<MediaCanStartListener&, Document&>>  Page::takeAnyMediaCanStartListener()
630 {
631     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
632         if (!frame->document())
633             continue;
634         if (MediaCanStartListener* listener = frame->document()->takeAnyMediaCanStartListener())
635             return { { *listener, *frame->document() } };
636     }
637     return WTF::nullopt;
638 }
639
640 void Page::setCanStartMedia(bool canStartMedia)
641 {
642     if (m_canStartMedia == canStartMedia)
643         return;
644
645     m_canStartMedia = canStartMedia;
646
647     while (m_canStartMedia) {
648         auto listener = takeAnyMediaCanStartListener();
649         if (!listener)
650             break;
651         listener->first.mediaCanStart(listener->second);
652     }
653 }
654
655 static Frame* incrementFrame(Frame* curr, bool forward, CanWrap canWrap, DidWrap* didWrap = nullptr)
656 {
657     return forward
658         ? curr->tree().traverseNext(canWrap, didWrap)
659         : curr->tree().traversePrevious(canWrap, didWrap);
660 }
661
662 bool Page::findString(const String& target, FindOptions options, DidWrap* didWrap)
663 {
664     if (target.isEmpty())
665         return false;
666
667     CanWrap canWrap = options.contains(WrapAround) ? CanWrap::Yes : CanWrap::No;
668     Frame* frame = &focusController().focusedOrMainFrame();
669     Frame* startFrame = frame;
670     do {
671         if (frame->editor().findString(target, (options - WrapAround) | StartInSelection)) {
672             if (frame != startFrame)
673                 startFrame->selection().clear();
674             focusController().setFocusedFrame(frame);
675             return true;
676         }
677         frame = incrementFrame(frame, !options.contains(Backwards), canWrap, didWrap);
678     } while (frame && frame != startFrame);
679
680     // Search contents of startFrame, on the other side of the selection that we did earlier.
681     // We cheat a bit and just research with wrap on
682     if (canWrap == CanWrap::Yes && !startFrame->selection().isNone()) {
683         if (didWrap)
684             *didWrap = DidWrap::Yes;
685         bool found = startFrame->editor().findString(target, options | WrapAround | StartInSelection);
686         focusController().setFocusedFrame(frame);
687         return found;
688     }
689
690     return false;
691 }
692
693 void Page::findStringMatchingRanges(const String& target, FindOptions options, int limit, Vector<RefPtr<Range>>& matchRanges, int& indexForSelection)
694 {
695     indexForSelection = 0;
696
697     Frame* frame = &mainFrame();
698     Frame* frameWithSelection = nullptr;
699     do {
700         frame->editor().countMatchesForText(target, 0, options, limit ? (limit - matchRanges.size()) : 0, true, &matchRanges);
701         if (frame->selection().isRange())
702             frameWithSelection = frame;
703         frame = incrementFrame(frame, true, CanWrap::No);
704     } while (frame);
705
706     if (matchRanges.isEmpty())
707         return;
708
709     if (frameWithSelection) {
710         indexForSelection = NoMatchAfterUserSelection;
711         RefPtr<Range> selectedRange = frameWithSelection->selection().selection().firstRange();
712         if (options.contains(Backwards)) {
713             for (size_t i = matchRanges.size(); i > 0; --i) {
714                 auto result = selectedRange->compareBoundaryPoints(Range::END_TO_START, *matchRanges[i - 1]);
715                 if (!result.hasException() && result.releaseReturnValue() > 0) {
716                     indexForSelection = i - 1;
717                     break;
718                 }
719             }
720         } else {
721             for (size_t i = 0, size = matchRanges.size(); i < size; ++i) {
722                 auto result = selectedRange->compareBoundaryPoints(Range::START_TO_END, *matchRanges[i]);
723                 if (!result.hasException() && result.releaseReturnValue() < 0) {
724                     indexForSelection = i;
725                     break;
726                 }
727             }
728         }
729     } else {
730         if (options.contains(Backwards))
731             indexForSelection = matchRanges.size() - 1;
732         else
733             indexForSelection = 0;
734     }
735 }
736
737 RefPtr<Range> Page::rangeOfString(const String& target, Range* referenceRange, FindOptions options)
738 {
739     if (target.isEmpty())
740         return nullptr;
741
742     if (referenceRange && referenceRange->ownerDocument().page() != this)
743         return nullptr;
744
745     CanWrap canWrap = options.contains(WrapAround) ? CanWrap::Yes : CanWrap::No;
746     Frame* frame = referenceRange ? referenceRange->ownerDocument().frame() : &mainFrame();
747     Frame* startFrame = frame;
748     do {
749         if (RefPtr<Range> resultRange = frame->editor().rangeOfString(target, frame == startFrame ? referenceRange : 0, options - WrapAround))
750             return resultRange;
751
752         frame = incrementFrame(frame, !options.contains(Backwards), canWrap);
753     } while (frame && frame != startFrame);
754
755     // Search contents of startFrame, on the other side of the reference range that we did earlier.
756     // We cheat a bit and just search again with wrap on.
757     if (canWrap == CanWrap::Yes && referenceRange) {
758         if (RefPtr<Range> resultRange = startFrame->editor().rangeOfString(target, referenceRange, options | WrapAround | StartInSelection))
759             return resultRange;
760     }
761
762     return nullptr;
763 }
764
765 unsigned Page::findMatchesForText(const String& target, FindOptions options, unsigned maxMatchCount, ShouldHighlightMatches shouldHighlightMatches, ShouldMarkMatches shouldMarkMatches)
766 {
767     if (target.isEmpty())
768         return 0;
769
770     unsigned matchCount = 0;
771
772     Frame* frame = &mainFrame();
773     do {
774         if (shouldMarkMatches == MarkMatches)
775             frame->editor().setMarkedTextMatchesAreHighlighted(shouldHighlightMatches == HighlightMatches);
776         matchCount += frame->editor().countMatchesForText(target, 0, options, maxMatchCount ? (maxMatchCount - matchCount) : 0, shouldMarkMatches == MarkMatches, 0);
777         frame = incrementFrame(frame, true, CanWrap::No);
778     } while (frame);
779
780     return matchCount;
781 }
782
783 unsigned Page::markAllMatchesForText(const String& target, FindOptions options, bool shouldHighlight, unsigned maxMatchCount)
784 {
785     return findMatchesForText(target, options, maxMatchCount, shouldHighlight ? HighlightMatches : DoNotHighlightMatches, MarkMatches);
786 }
787
788 unsigned Page::countFindMatches(const String& target, FindOptions options, unsigned maxMatchCount)
789 {
790     return findMatchesForText(target, options, maxMatchCount, DoNotHighlightMatches, DoNotMarkMatches);
791 }
792
793 struct FindReplacementRange {
794     RefPtr<ContainerNode> root;
795     size_t location { notFound };
796     size_t length { 0 };
797 };
798
799 static void replaceRanges(Page& page, const Vector<FindReplacementRange>& ranges, const String& replacementText)
800 {
801     HashMap<RefPtr<ContainerNode>, Vector<FindReplacementRange>> rangesByContainerNode;
802     for (auto& range : ranges) {
803         auto& rangeList = rangesByContainerNode.ensure(range.root, [] {
804             return Vector<FindReplacementRange> { };
805         }).iterator->value;
806
807         // Ensure that ranges are sorted by their end offsets, per editing container.
808         auto endOffsetForRange = range.location + range.length;
809         auto insertionIndex = rangeList.size();
810         for (auto iterator = rangeList.rbegin(); iterator != rangeList.rend(); ++iterator) {
811             auto endOffsetBeforeInsertionIndex = iterator->location + iterator->length;
812             if (endOffsetForRange >= endOffsetBeforeInsertionIndex)
813                 break;
814             insertionIndex--;
815         }
816         rangeList.insert(insertionIndex, range);
817     }
818
819     HashMap<RefPtr<Frame>, unsigned> frameToTraversalIndexMap;
820     unsigned currentFrameTraversalIndex = 0;
821     for (Frame* frame = &page.mainFrame(); frame; frame = frame->tree().traverseNext())
822         frameToTraversalIndexMap.set(frame, currentFrameTraversalIndex++);
823
824     // Likewise, iterate backwards (in document and frame order) through editing containers that contain text matches,
825     // so that we're consistent with our backwards iteration behavior per editing container when replacing text.
826     auto containerNodesInOrderOfReplacement = copyToVector(rangesByContainerNode.keys());
827     std::sort(containerNodesInOrderOfReplacement.begin(), containerNodesInOrderOfReplacement.end(), [frameToTraversalIndexMap] (auto& firstNode, auto& secondNode) {
828         if (firstNode == secondNode)
829             return false;
830
831         auto firstFrame = makeRefPtr(firstNode->document().frame());
832         if (!firstFrame)
833             return true;
834
835         auto secondFrame = makeRefPtr(secondNode->document().frame());
836         if (!secondFrame)
837             return false;
838
839         if (firstFrame == secondFrame) {
840             // comparePositions is used here instead of Node::compareDocumentPosition because some editing roots may exist inside shadow roots.
841             return comparePositions({ firstNode.get(), Position::PositionIsBeforeChildren }, { secondNode.get(), Position::PositionIsBeforeChildren }) > 0;
842         }
843         return frameToTraversalIndexMap.get(firstFrame) > frameToTraversalIndexMap.get(secondFrame);
844     });
845
846     for (auto& container : containerNodesInOrderOfReplacement) {
847         auto frame = makeRefPtr(container->document().frame());
848         if (!frame)
849             continue;
850
851         // Iterate backwards through ranges when replacing text, such that earlier text replacements don't clobber replacement ranges later on.
852         auto& ranges = rangesByContainerNode.find(container)->value;
853         for (auto iterator = ranges.rbegin(); iterator != ranges.rend(); ++iterator) {
854             auto range = TextIterator::rangeFromLocationAndLength(container.get(), iterator->location, iterator->length);
855             if (!range || range->collapsed())
856                 continue;
857
858             frame->selection().setSelectedRange(range.get(), DOWNSTREAM, FrameSelection::ShouldCloseTyping::Yes);
859             frame->editor().replaceSelectionWithText(replacementText, Editor::SelectReplacement::Yes, Editor::SmartReplace::No, EditAction::InsertReplacement);
860         }
861     }
862 }
863
864 uint32_t Page::replaceRangesWithText(const Vector<Ref<Range>>& rangesToReplace, const String& replacementText, bool selectionOnly)
865 {
866     // FIXME: In the future, we should respect the `selectionOnly` flag by checking whether each range being replaced is
867     // contained within its frame's selection.
868     UNUSED_PARAM(selectionOnly);
869
870     Vector<FindReplacementRange> replacementRanges;
871     replacementRanges.reserveInitialCapacity(rangesToReplace.size());
872
873     for (auto& range : rangesToReplace) {
874         auto highestRoot = makeRefPtr(highestEditableRoot(range->startPosition()));
875         if (!highestRoot || highestRoot != highestEditableRoot(range->endPosition()))
876             continue;
877
878         auto frame = makeRefPtr(highestRoot->document().frame());
879         if (!frame)
880             continue;
881
882         size_t replacementLocation = notFound;
883         size_t replacementLength = 0;
884         if (!TextIterator::getLocationAndLengthFromRange(highestRoot.get(), range.ptr(), replacementLocation, replacementLength))
885             continue;
886
887         if (replacementLocation == notFound || !replacementLength)
888             continue;
889
890         replacementRanges.append({ WTFMove(highestRoot), replacementLocation, replacementLength });
891     }
892
893     replaceRanges(*this, replacementRanges, replacementText);
894     return rangesToReplace.size();
895 }
896
897 uint32_t Page::replaceSelectionWithText(const String& replacementText)
898 {
899     auto frame = makeRef(focusController().focusedOrMainFrame());
900     auto selection = frame->selection().selection();
901     if (!selection.isContentEditable())
902         return 0;
903
904     auto editAction = selection.isRange() ? EditAction::InsertReplacement : EditAction::Insert;
905     frame->editor().replaceSelectionWithText(replacementText, Editor::SelectReplacement::Yes, Editor::SmartReplace::No, editAction);
906     return 1;
907 }
908
909 void Page::unmarkAllTextMatches()
910 {
911     Frame* frame = &mainFrame();
912     do {
913         frame->document()->markers().removeMarkers(DocumentMarker::TextMatch);
914         frame = incrementFrame(frame, true, CanWrap::No);
915     } while (frame);
916 }
917
918 static bool isEditableTextInputElement(const Element& element)
919 {
920     if (is<HTMLTextFormControlElement>(element)) {
921         if (!element.isTextField() && !is<HTMLTextAreaElement>(element))
922             return false;
923         return downcast<HTMLTextFormControlElement>(element).isInnerTextElementEditable();
924     }
925     return element.isRootEditableElement();
926 }
927
928 Vector<Ref<Element>> Page::editableElementsInRect(const FloatRect& searchRectInRootViewCoordinates) const
929 {
930     Vector<Ref<Element>> result;
931     for (auto* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
932         auto* document = frame->document();
933         if (!document)
934             continue;
935
936         Deque<Node*> nodesToSearch;
937         nodesToSearch.append(document);
938         while (!nodesToSearch.isEmpty()) {
939             auto* node = nodesToSearch.takeFirst();
940
941             // It is possible to have nested text input contexts (e.g. <input type='text'> inside contenteditable) but
942             // in this case we just take the outermost context and skip the rest.
943             if (!is<Element>(node) || !isEditableTextInputElement(downcast<Element>(*node))) {
944                 for (auto* child = node->firstChild(); child; child = child->nextSibling())
945                     nodesToSearch.append(child);
946                 continue;
947             }
948
949             auto& element = downcast<Element>(*node);
950             if (searchRectInRootViewCoordinates.intersects(element.clientRect()))
951                 result.append(element);
952         }
953     }
954     return result;
955 }
956
957 const VisibleSelection& Page::selection() const
958 {
959     return focusController().focusedOrMainFrame().selection().selection();
960 }
961
962 void Page::setDefersLoading(bool defers)
963 {
964     if (!m_settings->loadDeferringEnabled())
965         return;
966
967     if (m_settings->wantsBalancedSetDefersLoadingBehavior()) {
968         ASSERT(defers || m_defersLoadingCallCount);
969         if (defers && ++m_defersLoadingCallCount > 1)
970             return;
971         if (!defers && --m_defersLoadingCallCount)
972             return;
973     } else {
974         ASSERT(!m_defersLoadingCallCount);
975         if (defers == m_defersLoading)
976             return;
977     }
978
979     m_defersLoading = defers;
980     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext())
981         frame->loader().setDefersLoading(defers);
982 }
983
984 void Page::clearUndoRedoOperations()
985 {
986     m_editorClient->clearUndoRedoOperations();
987 }
988
989 bool Page::inLowQualityImageInterpolationMode() const
990 {
991     return m_inLowQualityInterpolationMode;
992 }
993
994 void Page::setInLowQualityImageInterpolationMode(bool mode)
995 {
996     m_inLowQualityInterpolationMode = mode;
997 }
998
999 DiagnosticLoggingClient& Page::diagnosticLoggingClient() const
1000 {
1001     if (!settings().diagnosticLoggingEnabled() || !m_diagnosticLoggingClient)
1002         return emptyDiagnosticLoggingClient();
1003     return *m_diagnosticLoggingClient;
1004 }
1005
1006 void Page::setMediaVolume(float volume)
1007 {
1008     if (volume < 0 || volume > 1)
1009         return;
1010
1011     if (m_mediaVolume == volume)
1012         return;
1013
1014     m_mediaVolume = volume;
1015     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1016         if (!frame->document())
1017             continue;
1018         frame->document()->mediaVolumeDidChange();
1019     }
1020 }
1021
1022 void Page::setZoomedOutPageScaleFactor(float scale)
1023 {
1024     if (m_zoomedOutPageScaleFactor == scale)
1025         return;
1026     m_zoomedOutPageScaleFactor = scale;
1027
1028     mainFrame().deviceOrPageScaleFactorChanged();
1029 }
1030
1031 void Page::setPageScaleFactor(float scale, const IntPoint& origin, bool inStableState)
1032 {
1033     LOG(Viewports, "Page::setPageScaleFactor %.2f - inStableState %d", scale, inStableState);
1034
1035     Document* document = mainFrame().document();
1036     FrameView* view = document->view();
1037
1038     if (scale == m_pageScaleFactor) {
1039         if (view && view->scrollPosition() != origin) {
1040             if (!m_settings->delegatesPageScaling())
1041                 document->updateLayoutIgnorePendingStylesheets();
1042
1043             if (!view->delegatesScrolling())
1044                 view->setScrollPosition(origin);
1045 #if USE(COORDINATED_GRAPHICS)
1046             else
1047                 view->requestScrollPositionUpdate(origin);
1048 #endif
1049         }
1050 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
1051         if (inStableState) {
1052             for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1053                 if (!frame->document())
1054                     continue;
1055                 frame->document()->pageScaleFactorChangedAndStable();
1056             }
1057         }
1058 #endif
1059         return;
1060     }
1061
1062     m_pageScaleFactor = scale;
1063
1064     if (!m_settings->delegatesPageScaling()) {
1065         view->setNeedsLayoutAfterViewConfigurationChange();
1066         view->setNeedsCompositingGeometryUpdate();
1067
1068         document->resolveStyle(Document::ResolveStyleType::Rebuild);
1069
1070         // Transform change on RenderView doesn't trigger repaint on non-composited contents.
1071         mainFrame().view()->invalidateRect(IntRect(LayoutRect::infiniteRect()));
1072     }
1073
1074     mainFrame().deviceOrPageScaleFactorChanged();
1075
1076     if (view && view->fixedElementsLayoutRelativeToFrame())
1077         view->setViewportConstrainedObjectsNeedLayout();
1078
1079     if (view && view->scrollPosition() != origin) {
1080         if (!m_settings->delegatesPageScaling() && document->renderView() && document->renderView()->needsLayout() && view->didFirstLayout())
1081             view->layoutContext().layout();
1082
1083         if (!view->delegatesScrolling())
1084             view->setScrollPosition(origin);
1085 #if USE(COORDINATED_GRAPHICS)
1086         else
1087             view->requestScrollPositionUpdate(origin);
1088 #endif
1089     }
1090
1091 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
1092     if (inStableState) {
1093         for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1094             if (!frame->document())
1095                 continue;
1096             frame->document()->pageScaleFactorChangedAndStable();
1097         }
1098     }
1099 #else
1100     UNUSED_PARAM(inStableState);
1101 #endif
1102 }
1103
1104 void Page::setViewScaleFactor(float scale)
1105 {
1106     if (m_viewScaleFactor == scale)
1107         return;
1108
1109     m_viewScaleFactor = scale;
1110     BackForwardCache::singleton().markPagesForDeviceOrPageScaleChanged(*this);
1111 }
1112
1113 void Page::setDeviceScaleFactor(float scaleFactor)
1114 {
1115     ASSERT(scaleFactor > 0);
1116     if (scaleFactor <= 0)
1117         return;
1118     
1119     if (m_deviceScaleFactor == scaleFactor)
1120         return;
1121
1122     m_deviceScaleFactor = scaleFactor;
1123     setNeedsRecalcStyleInAllFrames();
1124
1125     mainFrame().deviceOrPageScaleFactorChanged();
1126     BackForwardCache::singleton().markPagesForDeviceOrPageScaleChanged(*this);
1127
1128     pageOverlayController().didChangeDeviceScaleFactor();
1129 }
1130
1131 void Page::setInitialScale(float initialScale)
1132 {
1133     m_initialScale = initialScale;
1134 }
1135
1136 void Page::setUserInterfaceLayoutDirection(UserInterfaceLayoutDirection userInterfaceLayoutDirection)
1137 {
1138     if (m_userInterfaceLayoutDirection == userInterfaceLayoutDirection)
1139         return;
1140
1141     m_userInterfaceLayoutDirection = userInterfaceLayoutDirection;
1142 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
1143     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1144         if (!frame->document())
1145             continue;
1146         frame->document()->userInterfaceLayoutDirectionChanged();
1147     }
1148 #endif
1149 }
1150
1151 #if ENABLE(VIDEO)
1152 void Page::updateMediaElementRateChangeRestrictions()
1153 {
1154     for (auto* mediaElement : HTMLMediaElement::allMediaElements())
1155         mediaElement->updateRateChangeRestrictions();
1156 }
1157 #endif
1158
1159 void Page::didStartProvisionalLoad()
1160 {
1161     if (m_performanceMonitor)
1162         m_performanceMonitor->didStartProvisionalLoad();
1163 }
1164
1165 void Page::didFinishLoad()
1166 {
1167     resetRelevantPaintedObjectCounter();
1168
1169     if (m_performanceMonitor)
1170         m_performanceMonitor->didFinishLoad();
1171 }
1172
1173 bool Page::isOnlyNonUtilityPage() const
1174 {
1175     return !isUtilityPage() && nonUtilityPageCount == 1;
1176 }
1177
1178 bool Page::isLowPowerModeEnabled() const
1179 {
1180     if (m_lowPowerModeEnabledOverrideForTesting)
1181         return m_lowPowerModeEnabledOverrideForTesting.value();
1182
1183     return m_lowPowerModeNotifier->isLowPowerModeEnabled();
1184 }
1185
1186 void Page::setLowPowerModeEnabledOverrideForTesting(Optional<bool> isEnabled)
1187 {
1188     m_lowPowerModeEnabledOverrideForTesting = isEnabled;
1189     handleLowModePowerChange(m_lowPowerModeEnabledOverrideForTesting.valueOr(false));
1190 }
1191
1192 void Page::setTopContentInset(float contentInset)
1193 {
1194     if (m_topContentInset == contentInset)
1195         return;
1196     
1197     m_topContentInset = contentInset;
1198     
1199     if (FrameView* view = mainFrame().view())
1200         view->topContentInsetDidChange(m_topContentInset);
1201 }
1202
1203 void Page::setShouldSuppressScrollbarAnimations(bool suppressAnimations)
1204 {
1205     if (suppressAnimations == m_suppressScrollbarAnimations)
1206         return;
1207
1208     lockAllOverlayScrollbarsToHidden(suppressAnimations);
1209     m_suppressScrollbarAnimations = suppressAnimations;
1210 }
1211
1212 void Page::lockAllOverlayScrollbarsToHidden(bool lockOverlayScrollbars)
1213 {
1214     FrameView* view = mainFrame().view();
1215     if (!view)
1216         return;
1217
1218     view->lockOverlayScrollbarStateToHidden(lockOverlayScrollbars);
1219     
1220     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1221         FrameView* frameView = frame->view();
1222         if (!frameView)
1223             continue;
1224
1225         const HashSet<ScrollableArea*>* scrollableAreas = frameView->scrollableAreas();
1226         if (!scrollableAreas)
1227             continue;
1228
1229         for (auto& scrollableArea : *scrollableAreas)
1230             scrollableArea->lockOverlayScrollbarStateToHidden(lockOverlayScrollbars);
1231     }
1232 }
1233     
1234 void Page::setVerticalScrollElasticity(ScrollElasticity elasticity)
1235 {
1236     if (m_verticalScrollElasticity == elasticity)
1237         return;
1238     
1239     m_verticalScrollElasticity = elasticity;
1240     
1241     if (FrameView* view = mainFrame().view())
1242         view->setVerticalScrollElasticity(elasticity);
1243 }
1244     
1245 void Page::setHorizontalScrollElasticity(ScrollElasticity elasticity)
1246 {
1247     if (m_horizontalScrollElasticity == elasticity)
1248         return;
1249     
1250     m_horizontalScrollElasticity = elasticity;
1251     
1252     if (FrameView* view = mainFrame().view())
1253         view->setHorizontalScrollElasticity(elasticity);
1254 }
1255
1256 void Page::setPagination(const Pagination& pagination)
1257 {
1258     if (m_pagination == pagination)
1259         return;
1260
1261     m_pagination = pagination;
1262
1263     setNeedsRecalcStyleInAllFrames();
1264 }
1265
1266 void Page::setPaginationLineGridEnabled(bool enabled)
1267 {
1268     if (m_paginationLineGridEnabled == enabled)
1269         return;
1270     
1271     m_paginationLineGridEnabled = enabled;
1272     
1273     setNeedsRecalcStyleInAllFrames();
1274 }
1275
1276 unsigned Page::pageCount() const
1277 {
1278     if (m_pagination.mode == Pagination::Unpaginated)
1279         return 0;
1280
1281     if (Document* document = mainFrame().document())
1282         document->updateLayoutIgnorePendingStylesheets();
1283
1284     RenderView* contentRenderer = mainFrame().contentRenderer();
1285     return contentRenderer ? contentRenderer->pageCount() : 0;
1286 }
1287
1288 void Page::setIsInWindow(bool isInWindow)
1289 {
1290     setActivityState(isInWindow ? m_activityState | ActivityState::IsInWindow : m_activityState - ActivityState::IsInWindow);
1291 }
1292
1293 void Page::setIsInWindowInternal(bool isInWindow)
1294 {
1295     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1296         if (FrameView* frameView = frame->view())
1297             frameView->setIsInWindow(isInWindow);
1298     }
1299
1300     if (isInWindow)
1301         resumeAnimatingImages();
1302 }
1303
1304 void Page::addActivityStateChangeObserver(ActivityStateChangeObserver& observer)
1305 {
1306     m_activityStateChangeObservers.add(&observer);
1307 }
1308
1309 void Page::removeActivityStateChangeObserver(ActivityStateChangeObserver& observer)
1310 {
1311     m_activityStateChangeObservers.remove(&observer);
1312 }
1313
1314 void Page::layoutIfNeeded()
1315 {
1316     if (FrameView* view = m_mainFrame->view())
1317         view->updateLayoutAndStyleIfNeededRecursive();
1318 }
1319
1320 // https://html.spec.whatwg.org/multipage/webappapis.html#update-the-rendering
1321 void Page::updateRendering()
1322 {
1323     // This function is not reentrant, e.g. a rAF callback may force repaint.
1324     if (m_inUpdateRendering) {
1325         layoutIfNeeded();
1326         return;
1327     }
1328
1329     TraceScope traceScope(RenderingUpdateStart, RenderingUpdateEnd);
1330
1331     SetForScope<bool> change(m_inUpdateRendering, true);
1332
1333     layoutIfNeeded();
1334
1335     // Flush autofocus candidates
1336
1337     forEachDocument([&](Document& document) {
1338         document.runResizeSteps();
1339     });
1340
1341     forEachDocument([&](Document& document) {
1342         document.runScrollSteps();
1343     });
1344
1345     forEachDocument([&](Document& document) {
1346         document.evaluateMediaQueriesAndReportChanges();        
1347     });
1348
1349     Vector<Ref<Document>> documents = collectDocuments(); // The requestAnimationFrame callbacks may change the frame hierarchy of the page
1350     for (auto& document : documents) {
1351         DOMHighResTimeStamp timestamp = document->domWindow()->nowTimestamp();
1352         document->updateAnimationsAndSendEvents(timestamp);
1353         // FIXME: Run the fullscreen steps.
1354         document->serviceRequestAnimationFrameCallbacks(timestamp);
1355     }
1356
1357     layoutIfNeeded();
1358
1359 #if ENABLE(INTERSECTION_OBSERVER)
1360     for (auto& document : documents)
1361         document->updateIntersectionObservations();
1362 #endif
1363 #if ENABLE(RESIZE_OBSERVER)
1364     for (auto& document : documents)
1365         document->updateResizeObservations(*this);
1366 #endif
1367
1368     layoutIfNeeded();
1369 }
1370
1371 void Page::suspendScriptedAnimations()
1372 {
1373     m_scriptedAnimationsSuspended = true;
1374     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1375         if (frame->document())
1376             frame->document()->suspendScriptedAnimationControllerCallbacks();
1377     }
1378 }
1379
1380 void Page::resumeScriptedAnimations()
1381 {
1382     m_scriptedAnimationsSuspended = false;
1383     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1384         if (frame->document())
1385             frame->document()->resumeScriptedAnimationControllerCallbacks();
1386     }
1387 }
1388
1389 enum class ThrottlingReasonOperation { Add, Remove };
1390 static void updateScriptedAnimationsThrottlingReason(Page& page, ThrottlingReasonOperation operation, ScriptedAnimationController::ThrottlingReason reason)
1391 {
1392     for (Frame* frame = &page.mainFrame(); frame; frame = frame->tree().traverseNext()) {
1393         auto* document = frame->document();
1394         if (!document)
1395             continue;
1396         auto* scriptedAnimationController = document->scriptedAnimationController();
1397         if (!scriptedAnimationController)
1398             continue;
1399
1400         if (operation == ThrottlingReasonOperation::Add)
1401             scriptedAnimationController->addThrottlingReason(reason);
1402         else
1403             scriptedAnimationController->removeThrottlingReason(reason);
1404     }
1405 }
1406
1407 void Page::setIsVisuallyIdleInternal(bool isVisuallyIdle)
1408 {
1409     updateScriptedAnimationsThrottlingReason(*this, isVisuallyIdle ? ThrottlingReasonOperation::Add : ThrottlingReasonOperation::Remove, ScriptedAnimationController::ThrottlingReason::VisuallyIdle);
1410 }
1411
1412 void Page::handleLowModePowerChange(bool isLowPowerModeEnabled)
1413 {
1414     updateScriptedAnimationsThrottlingReason(*this, isLowPowerModeEnabled ? ThrottlingReasonOperation::Add : ThrottlingReasonOperation::Remove, ScriptedAnimationController::ThrottlingReason::LowPowerMode);
1415     if (RuntimeEnabledFeatures::sharedFeatures().webAnimationsCSSIntegrationEnabled()) {
1416         forEachDocument([&] (Document& document) {
1417             if (auto timeline = document.existingTimeline())
1418                 timeline->updateThrottlingState();
1419         });
1420     } else
1421         mainFrame().animation().updateThrottlingState();
1422     updateDOMTimerAlignmentInterval();
1423 }
1424
1425 void Page::userStyleSheetLocationChanged()
1426 {
1427     // FIXME: Eventually we will move to a model of just being handed the sheet
1428     // text instead of loading the URL ourselves.
1429     URL url = m_settings->userStyleSheetLocation();
1430     
1431     // Allow any local file URL scheme to be loaded.
1432     if (LegacySchemeRegistry::shouldTreatURLSchemeAsLocal(url.protocol().toStringWithoutCopying()))
1433         m_userStyleSheetPath = url.fileSystemPath();
1434     else
1435         m_userStyleSheetPath = String();
1436
1437     m_didLoadUserStyleSheet = false;
1438     m_userStyleSheet = String();
1439     m_userStyleSheetModificationTime = WTF::nullopt;
1440
1441     // Data URLs with base64-encoded UTF-8 style sheets are common. We can process them
1442     // synchronously and avoid using a loader. 
1443     if (url.protocolIsData() && url.string().startsWith("data:text/css;charset=utf-8;base64,")) {
1444         m_didLoadUserStyleSheet = true;
1445
1446         Vector<char> styleSheetAsUTF8;
1447         if (base64Decode(decodeURLEscapeSequences(url.string().substring(35)), styleSheetAsUTF8, Base64IgnoreSpacesAndNewLines))
1448             m_userStyleSheet = String::fromUTF8(styleSheetAsUTF8.data(), styleSheetAsUTF8.size());
1449     }
1450
1451     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1452         if (frame->document())
1453             frame->document()->extensionStyleSheets().updatePageUserSheet();
1454     }
1455 }
1456
1457 const String& Page::userStyleSheet() const
1458 {
1459     if (m_userStyleSheetPath.isEmpty())
1460         return m_userStyleSheet;
1461
1462     auto modificationTime = FileSystem::getFileModificationTime(m_userStyleSheetPath);
1463     if (!modificationTime) {
1464         // The stylesheet either doesn't exist, was just deleted, or is
1465         // otherwise unreadable. If we've read the stylesheet before, we should
1466         // throw away that data now as it no longer represents what's on disk.
1467         m_userStyleSheet = String();
1468         return m_userStyleSheet;
1469     }
1470
1471     // If the stylesheet hasn't changed since the last time we read it, we can
1472     // just return the old data.
1473     if (m_didLoadUserStyleSheet && (m_userStyleSheetModificationTime && modificationTime.value() <= m_userStyleSheetModificationTime.value()))
1474         return m_userStyleSheet;
1475
1476     m_didLoadUserStyleSheet = true;
1477     m_userStyleSheet = String();
1478     m_userStyleSheetModificationTime = modificationTime;
1479
1480     // FIXME: It would be better to load this asynchronously to avoid blocking
1481     // the process, but we will first need to create an asynchronous loading
1482     // mechanism that is not tied to a particular Frame. We will also have to
1483     // determine what our behavior should be before the stylesheet is loaded
1484     // and what should happen when it finishes loading, especially with respect
1485     // to when the load event fires, when Document::close is called, and when
1486     // layout/paint are allowed to happen.
1487     auto data = SharedBuffer::createWithContentsOfFile(m_userStyleSheetPath);
1488     if (!data)
1489         return m_userStyleSheet;
1490
1491     m_userStyleSheet = TextResourceDecoder::create("text/css")->decodeAndFlush(data->data(), data->size());
1492
1493     return m_userStyleSheet;
1494 }
1495
1496 void Page::userAgentChanged()
1497 {
1498     for (auto* frame = &m_mainFrame.get(); frame; frame = frame->tree().traverseNext()) {
1499         auto* window = frame->window();
1500         if (!window)
1501             continue;
1502         if (auto* navigator = window->optionalNavigator())
1503             navigator->userAgentChanged();
1504     }
1505 }
1506
1507 void Page::invalidateStylesForAllLinks()
1508 {
1509     for (Frame* frame = &m_mainFrame.get(); frame; frame = frame->tree().traverseNext()) {
1510         if (!frame->document())
1511             continue;
1512         frame->document()->visitedLinkState().invalidateStyleForAllLinks();
1513     }
1514 }
1515
1516 void Page::invalidateStylesForLink(SharedStringHash linkHash)
1517 {
1518     for (Frame* frame = &m_mainFrame.get(); frame; frame = frame->tree().traverseNext()) {
1519         if (!frame->document())
1520             continue;
1521         frame->document()->visitedLinkState().invalidateStyleForLink(linkHash);
1522     }
1523 }
1524
1525 void Page::invalidateInjectedStyleSheetCacheInAllFrames()
1526 {
1527     for (Frame* frame = &m_mainFrame.get(); frame; frame = frame->tree().traverseNext()) {
1528         Document* document = frame->document();
1529         if (!document)
1530             continue;
1531         document->extensionStyleSheets().invalidateInjectedStyleSheetCache();
1532     }
1533 }
1534
1535 void Page::setDebugger(JSC::Debugger* debugger)
1536 {
1537     if (m_debugger == debugger)
1538         return;
1539
1540     m_debugger = debugger;
1541
1542     for (Frame* frame = &m_mainFrame.get(); frame; frame = frame->tree().traverseNext())
1543         frame->windowProxy().attachDebugger(m_debugger);
1544 }
1545
1546 StorageNamespace* Page::sessionStorage(bool optionalCreate)
1547 {
1548     if (!m_sessionStorage && optionalCreate)
1549         m_sessionStorage = m_storageNamespaceProvider->createSessionStorageNamespace(*this, m_settings->sessionStorageQuota());
1550
1551     return m_sessionStorage.get();
1552 }
1553
1554 void Page::setSessionStorage(RefPtr<StorageNamespace>&& newStorage)
1555 {
1556     m_sessionStorage = WTFMove(newStorage);
1557 }
1558
1559 bool Page::hasCustomHTMLTokenizerTimeDelay() const
1560 {
1561     return m_settings->maxParseDuration() != -1;
1562 }
1563
1564 double Page::customHTMLTokenizerTimeDelay() const
1565 {
1566     ASSERT(m_settings->maxParseDuration() != -1);
1567     return m_settings->maxParseDuration();
1568 }
1569
1570 void Page::setMemoryCacheClientCallsEnabled(bool enabled)
1571 {
1572     if (m_areMemoryCacheClientCallsEnabled == enabled)
1573         return;
1574
1575     m_areMemoryCacheClientCallsEnabled = enabled;
1576     if (!enabled)
1577         return;
1578
1579     for (RefPtr<Frame> frame = &mainFrame(); frame; frame = frame->tree().traverseNext())
1580         frame->loader().tellClientAboutPastMemoryCacheLoads();
1581 }
1582
1583 void Page::hiddenPageDOMTimerThrottlingStateChanged()
1584 {
1585     // Disable & reengage to ensure state is updated.
1586     setTimerThrottlingState(TimerThrottlingState::Disabled);
1587     updateTimerThrottlingState();
1588 }
1589
1590 void Page::updateTimerThrottlingState()
1591 {
1592     // Timer throttling disabled if page is visually active, or disabled by setting.
1593     if (!m_settings->hiddenPageDOMTimerThrottlingEnabled() || !(m_activityState & ActivityState::IsVisuallyIdle)) {
1594         setTimerThrottlingState(TimerThrottlingState::Disabled);
1595         return;
1596     }
1597
1598     // If the page is visible (but idle), there is any activity (loading, media playing, etc), or per setting,
1599     // we allow timer throttling, but not increasing timer throttling.
1600     if (!m_settings->hiddenPageDOMTimerThrottlingAutoIncreases()
1601         || m_activityState.containsAny({ActivityState::IsVisible, ActivityState::IsAudible, ActivityState::IsLoading, ActivityState::IsCapturingMedia })) {
1602         setTimerThrottlingState(TimerThrottlingState::Enabled);
1603         return;
1604     }
1605
1606     // If we get here increasing timer throttling is enabled.
1607     setTimerThrottlingState(TimerThrottlingState::EnabledIncreasing);
1608 }
1609
1610 void Page::setTimerThrottlingState(TimerThrottlingState state)
1611 {
1612     if (state == m_timerThrottlingState)
1613         return;
1614
1615     m_timerThrottlingState = state;
1616     m_timerThrottlingStateLastChangedTime = MonotonicTime::now();
1617
1618     updateDOMTimerAlignmentInterval();
1619
1620     // When throttling is disabled, release all throttled timers.
1621     if (state == TimerThrottlingState::Disabled) {
1622         for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1623             if (auto* document = frame->document())
1624                 document->didChangeTimerAlignmentInterval();
1625         }
1626     }
1627 }
1628
1629 void Page::setDOMTimerAlignmentIntervalIncreaseLimit(Seconds limit)
1630 {
1631     m_domTimerAlignmentIntervalIncreaseLimit = limit;
1632
1633     // If (m_domTimerAlignmentIntervalIncreaseLimit < m_domTimerAlignmentInterval) then we need
1634     // to update m_domTimerAlignmentInterval, if greater then need to restart the increase timer.
1635     if (m_timerThrottlingState == TimerThrottlingState::EnabledIncreasing)
1636         updateDOMTimerAlignmentInterval();
1637 }
1638
1639 void Page::updateDOMTimerAlignmentInterval()
1640 {
1641     bool needsIncreaseTimer = false;
1642
1643     switch (m_timerThrottlingState) {
1644     case TimerThrottlingState::Disabled:
1645         m_domTimerAlignmentInterval = isLowPowerModeEnabled() ? DOMTimer::defaultAlignmentIntervalInLowPowerMode() : DOMTimer::defaultAlignmentInterval();
1646         break;
1647
1648     case TimerThrottlingState::Enabled:
1649         m_domTimerAlignmentInterval = DOMTimer::hiddenPageAlignmentInterval();
1650         break;
1651
1652     case TimerThrottlingState::EnabledIncreasing:
1653         // For pages in prerender state maximum throttling kicks in immediately.
1654         if (m_isPrerender)
1655             m_domTimerAlignmentInterval = m_domTimerAlignmentIntervalIncreaseLimit;
1656         else {
1657             ASSERT(!!m_timerThrottlingStateLastChangedTime);
1658             m_domTimerAlignmentInterval = MonotonicTime::now() - m_timerThrottlingStateLastChangedTime;
1659             // If we're below the limit, set the timer. If above, clamp to limit.
1660             if (m_domTimerAlignmentInterval < m_domTimerAlignmentIntervalIncreaseLimit)
1661                 needsIncreaseTimer = true;
1662             else
1663                 m_domTimerAlignmentInterval = m_domTimerAlignmentIntervalIncreaseLimit;
1664         }
1665         // Alignment interval should not be less than DOMTimer::hiddenPageAlignmentInterval().
1666         m_domTimerAlignmentInterval = std::max(m_domTimerAlignmentInterval, DOMTimer::hiddenPageAlignmentInterval());
1667     }
1668
1669     // If throttling is enabled, auto-increasing of throttling is enabled, and the auto-increase
1670     // limit has not yet been reached, and then arm the timer to consider an increase. Time to wait
1671     // between increases is equal to the current throttle time. Since alinment interval increases
1672     // exponentially, time between steps is exponential too.
1673     if (!needsIncreaseTimer)
1674         m_domTimerAlignmentIntervalIncreaseTimer.stop();
1675     else if (!m_domTimerAlignmentIntervalIncreaseTimer.isActive())
1676         m_domTimerAlignmentIntervalIncreaseTimer.startOneShot(m_domTimerAlignmentInterval);
1677 }
1678
1679 void Page::domTimerAlignmentIntervalIncreaseTimerFired()
1680 {
1681     ASSERT(m_settings->hiddenPageDOMTimerThrottlingAutoIncreases());
1682     ASSERT(m_timerThrottlingState == TimerThrottlingState::EnabledIncreasing);
1683     ASSERT(m_domTimerAlignmentInterval < m_domTimerAlignmentIntervalIncreaseLimit);
1684     
1685     // Alignment interval is increased to equal the time the page has been throttled, to a limit.
1686     updateDOMTimerAlignmentInterval();
1687 }
1688
1689 void Page::dnsPrefetchingStateChanged()
1690 {
1691     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1692         if (!frame->document())
1693             continue;
1694         frame->document()->initDNSPrefetch();
1695     }
1696 }
1697
1698 Vector<Ref<PluginViewBase>> Page::pluginViews()
1699 {
1700     Vector<Ref<PluginViewBase>> views;
1701     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1702         auto* view = frame->view();
1703         if (!view)
1704             break;
1705         for (auto& widget : view->children()) {
1706             if (is<PluginViewBase>(widget))
1707                 views.append(downcast<PluginViewBase>(widget.get()));
1708         }
1709     }
1710     return views;
1711 }
1712
1713 void Page::storageBlockingStateChanged()
1714 {
1715     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1716         if (!frame->document())
1717             continue;
1718         frame->document()->storageBlockingStateDidChange();
1719     }
1720
1721     // Collect the PluginViews in to a vector to ensure that action the plug-in takes
1722     // from below storageBlockingStateChanged does not affect their lifetime.
1723     for (auto& view : pluginViews())
1724         view->storageBlockingStateChanged();
1725 }
1726
1727 void Page::updateIsPlayingMedia(uint64_t sourceElementID)
1728 {
1729     MediaProducer::MediaStateFlags state = MediaProducer::IsNotPlaying;
1730     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1731         if (Document* document = frame->document())
1732             state |= document->mediaState();
1733     }
1734
1735     if (state == m_mediaState)
1736         return;
1737
1738     m_mediaState = state;
1739
1740     chrome().client().isPlayingMediaDidChange(state, sourceElementID);
1741 }
1742
1743 void Page::schedulePlaybackControlsManagerUpdate()
1744 {
1745 #if ENABLE(VIDEO)
1746     if (!m_playbackControlsManagerUpdateTimer.isActive())
1747         m_playbackControlsManagerUpdateTimer.startOneShot(0_s);
1748 #endif
1749 }
1750
1751 #if ENABLE(VIDEO)
1752 void Page::playbackControlsManagerUpdateTimerFired()
1753 {
1754     if (auto bestMediaElement = HTMLMediaElement::bestMediaElementForShowingPlaybackControlsManager(MediaElementSession::PlaybackControlsPurpose::ControlsManager))
1755         chrome().client().setUpPlaybackControlsManager(*bestMediaElement);
1756     else
1757         chrome().client().clearPlaybackControlsManager();
1758 }
1759 #endif
1760
1761 void Page::setMuted(MediaProducer::MutedStateFlags muted)
1762 {
1763     if (m_mutedState == muted)
1764         return;
1765
1766     m_mutedState = muted;
1767
1768     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1769         if (!frame->document())
1770             continue;
1771         frame->document()->pageMutedStateDidChange();
1772     }
1773 }
1774
1775 void Page::stopMediaCapture()
1776 {
1777 #if ENABLE(MEDIA_STREAM)
1778     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1779         if (!frame->document())
1780             continue;
1781
1782         frame->document()->stopMediaCapture();
1783     }
1784 #endif
1785 }
1786
1787 void Page::stopAllMediaPlayback()
1788 {
1789 #if ENABLE(VIDEO)
1790     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1791         if (auto* document = frame->document())
1792             document->stopAllMediaPlayback();
1793     }
1794 #endif
1795 }
1796
1797 void Page::suspendAllMediaPlayback()
1798 {
1799 #if ENABLE(VIDEO)
1800     ASSERT(!m_mediaPlaybackIsSuspended);
1801     if (m_mediaPlaybackIsSuspended)
1802         return;
1803
1804     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1805         if (auto* document = frame->document())
1806             document->suspendAllMediaPlayback();
1807     }
1808
1809     m_mediaPlaybackIsSuspended = true;
1810 #endif
1811 }
1812
1813 void Page::resumeAllMediaPlayback()
1814 {
1815 #if ENABLE(VIDEO)
1816     ASSERT(m_mediaPlaybackIsSuspended);
1817     if (!m_mediaPlaybackIsSuspended)
1818         return;
1819     m_mediaPlaybackIsSuspended = false;
1820
1821     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1822         if (auto* document = frame->document())
1823             document->resumeAllMediaPlayback();
1824     }
1825 #endif
1826 }
1827
1828 void Page::suspendAllMediaBuffering()
1829 {
1830 #if ENABLE(VIDEO)
1831     ASSERT(!m_mediaBufferingIsSuspended);
1832     if (m_mediaBufferingIsSuspended)
1833         return;
1834     m_mediaBufferingIsSuspended = true;
1835
1836     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1837         if (auto* document = frame->document())
1838             document->suspendAllMediaBuffering();
1839     }
1840 #endif
1841 }
1842
1843 void Page::resumeAllMediaBuffering()
1844 {
1845 #if ENABLE(VIDEO)
1846     if (!m_mediaBufferingIsSuspended)
1847         return;
1848     m_mediaBufferingIsSuspended = false;
1849
1850     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
1851         if (auto* document = frame->document())
1852             document->resumeAllMediaBuffering();
1853     }
1854 #endif
1855 }
1856
1857 #if ENABLE(MEDIA_SESSION)
1858 void Page::handleMediaEvent(MediaEventType eventType)
1859 {
1860     switch (eventType) {
1861     case MediaEventType::PlayPause:
1862         MediaSessionManager::singleton().togglePlayback();
1863         break;
1864     case MediaEventType::TrackNext:
1865         MediaSessionManager::singleton().skipToNextTrack();
1866         break;
1867     case MediaEventType::TrackPrevious:
1868         MediaSessionManager::singleton().skipToPreviousTrack();
1869         break;
1870     }
1871 }
1872
1873 void Page::setVolumeOfMediaElement(double volume, uint64_t elementID)
1874 {
1875     if (HTMLMediaElement* element = HTMLMediaElement::elementWithID(elementID))
1876         element->setVolume(volume, ASSERT_NO_EXCEPTION);
1877 }
1878 #endif
1879
1880 #if !ASSERT_DISABLED
1881 void Page::checkSubframeCountConsistency() const
1882 {
1883     ASSERT(m_subframeCount >= 0);
1884
1885     int subframeCount = 0;
1886     for (const Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext())
1887         ++subframeCount;
1888
1889     ASSERT(m_subframeCount + 1 == subframeCount);
1890 }
1891 #endif
1892
1893 void Page::resumeAnimatingImages()
1894 {
1895     // Drawing models which cache painted content while out-of-window (WebKit2's composited drawing areas, etc.)
1896     // require that we repaint animated images to kickstart the animation loop.
1897     if (FrameView* view = mainFrame().view())
1898         view->resumeVisibleImageAnimationsIncludingSubframes();
1899 }
1900
1901 void Page::setActivityState(OptionSet<ActivityState::Flag> activityState)
1902 {
1903     auto changed = m_activityState ^ activityState;
1904     if (!changed)
1905         return;
1906
1907     auto oldActivityState = m_activityState;
1908
1909     bool wasVisibleAndActive = isVisibleAndActive();
1910     m_activityState = activityState;
1911
1912     m_focusController->setActivityState(activityState);
1913
1914     if (changed & ActivityState::IsVisible)
1915         setIsVisibleInternal(activityState.contains(ActivityState::IsVisible));
1916     if (changed & ActivityState::IsInWindow)
1917         setIsInWindowInternal(activityState.contains(ActivityState::IsInWindow));
1918     if (changed & ActivityState::IsVisuallyIdle)
1919         setIsVisuallyIdleInternal(activityState.contains(ActivityState::IsVisuallyIdle));
1920     if (changed & ActivityState::WindowIsActive) {
1921         if (auto* view = m_mainFrame->view())
1922             view->updateTiledBackingAdaptiveSizing();
1923     }
1924
1925     if (changed.containsAny({ActivityState::IsVisible, ActivityState::IsVisuallyIdle, ActivityState::IsAudible, ActivityState::IsLoading, ActivityState::IsCapturingMedia }))
1926         updateTimerThrottlingState();
1927
1928     for (auto* observer : m_activityStateChangeObservers)
1929         observer->activityStateDidChange(oldActivityState, m_activityState);
1930
1931     if (wasVisibleAndActive != isVisibleAndActive())
1932         PlatformMediaSessionManager::updateNowPlayingInfoIfNecessary();
1933
1934     if (m_performanceMonitor)
1935         m_performanceMonitor->activityStateChanged(oldActivityState, activityState);
1936 }
1937
1938 bool Page::isVisibleAndActive() const
1939 {
1940     return m_activityState.contains(ActivityState::IsVisible) && m_activityState.contains(ActivityState::WindowIsActive);
1941 }
1942
1943 bool Page::isWindowActive() const
1944 {
1945     return m_activityState.contains(ActivityState::WindowIsActive);
1946 }
1947
1948 void Page::setIsVisible(bool isVisible)
1949 {
1950     auto state = m_activityState;
1951
1952     if (isVisible) {
1953         state.remove(ActivityState::IsVisuallyIdle);
1954         state.add({ ActivityState::IsVisible, ActivityState::IsVisibleOrOccluded });
1955     } else {
1956         state.add(ActivityState::IsVisuallyIdle);
1957         state.remove({ ActivityState::IsVisible, ActivityState::IsVisibleOrOccluded });
1958     }
1959     setActivityState(state);
1960 }
1961
1962 enum class SVGAnimationsState { Paused, Resumed };
1963 static inline void setSVGAnimationsState(Page& page, SVGAnimationsState state)
1964 {
1965     for (Frame* frame = &page.mainFrame(); frame; frame = frame->tree().traverseNext()) {
1966         auto* document = frame->document();
1967         if (!document)
1968             continue;
1969
1970         if (!document->svgExtensions())
1971             continue;
1972
1973         if (state == SVGAnimationsState::Paused)
1974             document->accessSVGExtensions().pauseAnimations();
1975         else
1976             document->accessSVGExtensions().unpauseAnimations();
1977     }
1978 }
1979
1980 void Page::setIsVisibleInternal(bool isVisible)
1981 {
1982     // FIXME: The visibility state should be stored on the top-level document.
1983     // https://bugs.webkit.org/show_bug.cgi?id=116769
1984
1985     if (isVisible) {
1986         m_isPrerender = false;
1987
1988         resumeScriptedAnimations();
1989 #if PLATFORM(IOS_FAMILY)
1990         resumeDeviceMotionAndOrientationUpdates();
1991 #endif
1992
1993         if (FrameView* view = mainFrame().view())
1994             view->show();
1995
1996         if (m_settings->hiddenPageCSSAnimationSuspensionEnabled()) {
1997             if (RuntimeEnabledFeatures::sharedFeatures().webAnimationsCSSIntegrationEnabled()) {
1998                 forEachDocument([&] (Document& document) {
1999                     if (auto* timeline = document.existingTimeline())
2000                         timeline->resumeAnimations();
2001                 });
2002             } else
2003                 mainFrame().animation().resumeAnimations();
2004         }
2005
2006         setSVGAnimationsState(*this, SVGAnimationsState::Resumed);
2007
2008         resumeAnimatingImages();
2009
2010         if (m_navigationToLogWhenVisible) {
2011             logNavigation(m_navigationToLogWhenVisible.value());
2012             m_navigationToLogWhenVisible = WTF::nullopt;
2013         }
2014     }
2015
2016     if (!isVisible) {
2017         if (m_settings->hiddenPageCSSAnimationSuspensionEnabled()) {
2018             if (RuntimeEnabledFeatures::sharedFeatures().webAnimationsCSSIntegrationEnabled()) {
2019                 forEachDocument([&] (Document& document) {
2020                     if (auto* timeline = document.existingTimeline())
2021                         timeline->suspendAnimations();
2022                 });
2023             } else
2024                 mainFrame().animation().suspendAnimations();
2025         }
2026
2027         setSVGAnimationsState(*this, SVGAnimationsState::Paused);
2028
2029 #if PLATFORM(IOS_FAMILY)
2030         suspendDeviceMotionAndOrientationUpdates();
2031 #endif
2032
2033         suspendScriptedAnimations();
2034
2035         if (FrameView* view = mainFrame().view())
2036             view->hide();
2037     }
2038
2039     Vector<Ref<Document>> documents;
2040     for (Frame* frame = &m_mainFrame.get(); frame; frame = frame->tree().traverseNext())
2041         documents.append(*frame->document());
2042
2043     for (auto& document : documents)
2044         document->visibilityStateChanged();
2045 }
2046
2047 void Page::setIsPrerender()
2048 {
2049     m_isPrerender = true;
2050     updateDOMTimerAlignmentInterval();
2051 }
2052
2053 VisibilityState Page::visibilityState() const
2054 {
2055     if (isVisible())
2056         return VisibilityState::Visible;
2057     if (m_isPrerender)
2058         return VisibilityState::Prerender;
2059     return VisibilityState::Hidden;
2060 }
2061
2062 void Page::setHeaderHeight(int headerHeight)
2063 {
2064     if (headerHeight == m_headerHeight)
2065         return;
2066
2067     m_headerHeight = headerHeight;
2068
2069     FrameView* frameView = mainFrame().view();
2070     if (!frameView)
2071         return;
2072
2073     RenderView* renderView = frameView->renderView();
2074     if (!renderView)
2075         return;
2076
2077     frameView->setNeedsLayoutAfterViewConfigurationChange();
2078     frameView->setNeedsCompositingGeometryUpdate();
2079 }
2080
2081 void Page::setFooterHeight(int footerHeight)
2082 {
2083     if (footerHeight == m_footerHeight)
2084         return;
2085
2086     m_footerHeight = footerHeight;
2087
2088     FrameView* frameView = mainFrame().view();
2089     if (!frameView)
2090         return;
2091
2092     RenderView* renderView = frameView->renderView();
2093     if (!renderView)
2094         return;
2095
2096     frameView->setNeedsLayoutAfterViewConfigurationChange();
2097     frameView->setNeedsCompositingGeometryUpdate();
2098 }
2099
2100 void Page::incrementNestedRunLoopCount()
2101 {
2102     m_nestedRunLoopCount++;
2103 }
2104
2105 void Page::decrementNestedRunLoopCount()
2106 {
2107     ASSERT(m_nestedRunLoopCount);
2108     if (m_nestedRunLoopCount <= 0)
2109         return;
2110
2111     m_nestedRunLoopCount--;
2112
2113     if (!m_nestedRunLoopCount && m_unnestCallback) {
2114         callOnMainThread([this] {
2115             if (insideNestedRunLoop())
2116                 return;
2117
2118             // This callback may destruct the Page.
2119             if (m_unnestCallback) {
2120                 auto callback = WTFMove(m_unnestCallback);
2121                 callback();
2122             }
2123         });
2124     }
2125 }
2126
2127 void Page::whenUnnested(WTF::Function<void()>&& callback)
2128 {
2129     ASSERT(!m_unnestCallback);
2130
2131     m_unnestCallback = WTFMove(callback);
2132 }
2133
2134 #if ENABLE(REMOTE_INSPECTOR)
2135 bool Page::remoteInspectionAllowed() const
2136 {
2137     return m_inspectorDebuggable->remoteDebuggingAllowed();
2138 }
2139
2140 void Page::setRemoteInspectionAllowed(bool allowed)
2141 {
2142     m_inspectorDebuggable->setRemoteDebuggingAllowed(allowed);
2143 }
2144
2145 String Page::remoteInspectionNameOverride() const
2146 {
2147     return m_inspectorDebuggable->nameOverride();
2148 }
2149
2150 void Page::setRemoteInspectionNameOverride(const String& name)
2151 {
2152     m_inspectorDebuggable->setNameOverride(name);
2153 }
2154
2155 void Page::remoteInspectorInformationDidChange() const
2156 {
2157     m_inspectorDebuggable->update();
2158 }
2159 #endif
2160
2161 void Page::addLayoutMilestones(OptionSet<LayoutMilestone> milestones)
2162 {
2163     // In the future, we may want a function that replaces m_layoutMilestones instead of just adding to it.
2164     m_requestedLayoutMilestones.add(milestones);
2165 }
2166
2167 void Page::removeLayoutMilestones(OptionSet<LayoutMilestone> milestones)
2168 {
2169     m_requestedLayoutMilestones.remove(milestones);
2170 }
2171
2172 Color Page::pageExtendedBackgroundColor() const
2173 {
2174     FrameView* frameView = mainFrame().view();
2175     if (!frameView)
2176         return Color();
2177
2178     RenderView* renderView = frameView->renderView();
2179     if (!renderView)
2180         return Color();
2181
2182     return renderView->compositor().rootExtendedBackgroundColor();
2183 }
2184
2185 // These are magical constants that might be tweaked over time.
2186 static const double gMinimumPaintedAreaRatio = 0.1;
2187 static const double gMaximumUnpaintedAreaRatio = 0.04;
2188
2189 bool Page::isCountingRelevantRepaintedObjects() const
2190 {
2191     return m_isCountingRelevantRepaintedObjects && m_requestedLayoutMilestones.contains(DidHitRelevantRepaintedObjectsAreaThreshold);
2192 }
2193
2194 void Page::startCountingRelevantRepaintedObjects()
2195 {
2196     // Reset everything in case we didn't hit the threshold last time.
2197     resetRelevantPaintedObjectCounter();
2198
2199     m_isCountingRelevantRepaintedObjects = true;
2200 }
2201
2202 void Page::resetRelevantPaintedObjectCounter()
2203 {
2204     m_isCountingRelevantRepaintedObjects = false;
2205     m_relevantUnpaintedRenderObjects.clear();
2206     m_topRelevantPaintedRegion = Region();
2207     m_bottomRelevantPaintedRegion = Region();
2208     m_relevantUnpaintedRegion = Region();
2209 }
2210
2211 static LayoutRect relevantViewRect(RenderView* view)
2212 {
2213     LayoutRect viewRect = view->viewRect();
2214
2215     float relevantViewRectWidth = 980;
2216 #if PLATFORM(WATCHOS)
2217     // FIXME(186051): Consider limiting the relevant rect width to the view width everywhere.
2218     relevantViewRectWidth = std::min<float>(viewRect.width().toFloat(), relevantViewRectWidth);
2219 #endif
2220
2221     // DidHitRelevantRepaintedObjectsAreaThreshold is a LayoutMilestone intended to indicate that
2222     // a certain relevant amount of content has been drawn to the screen. This is the rect that
2223     // has been determined to be relevant in the context of this goal. We may choose to tweak
2224     // the rect over time, much like we may choose to tweak gMinimumPaintedAreaRatio and
2225     // gMaximumUnpaintedAreaRatio. But this seems to work well right now.
2226     LayoutRect relevantViewRect { 0, 0, LayoutUnit(relevantViewRectWidth), 1300 };
2227     // If the viewRect is wider than the relevantViewRect, center the relevantViewRect.
2228     if (viewRect.width() > relevantViewRect.width())
2229         relevantViewRect.setX((viewRect.width() - relevantViewRect.width()) / 2);
2230
2231     return relevantViewRect;
2232 }
2233
2234 void Page::addRelevantRepaintedObject(RenderObject* object, const LayoutRect& objectPaintRect)
2235 {
2236     if (!isCountingRelevantRepaintedObjects())
2237         return;
2238
2239     // Objects inside sub-frames are not considered to be relevant.
2240     if (&object->frame() != &mainFrame())
2241         return;
2242
2243     LayoutRect relevantRect = relevantViewRect(&object->view());
2244
2245     // The objects are only relevant if they are being painted within the viewRect().
2246     if (!objectPaintRect.intersects(snappedIntRect(relevantRect)))
2247         return;
2248
2249     IntRect snappedPaintRect = snappedIntRect(objectPaintRect);
2250
2251     // If this object was previously counted as an unpainted object, remove it from that HashSet
2252     // and corresponding Region. FIXME: This doesn't do the right thing if the objects overlap.
2253     if (m_relevantUnpaintedRenderObjects.remove(object))
2254         m_relevantUnpaintedRegion.subtract(snappedPaintRect);
2255
2256     // Split the relevantRect into a top half and a bottom half. Making sure we have coverage in
2257     // both halves helps to prevent cases where we have a fully loaded menu bar or masthead with
2258     // no content beneath that.
2259     LayoutRect topRelevantRect = relevantRect;
2260     topRelevantRect.contract(LayoutSize(0_lu, relevantRect.height() / 2));
2261     LayoutRect bottomRelevantRect = topRelevantRect;
2262     bottomRelevantRect.setY(relevantRect.height() / 2);
2263
2264     // If the rect straddles both Regions, split it appropriately.
2265     if (topRelevantRect.intersects(snappedPaintRect) && bottomRelevantRect.intersects(snappedPaintRect)) {
2266         IntRect topIntersection = snappedPaintRect;
2267         topIntersection.intersect(snappedIntRect(topRelevantRect));
2268         m_topRelevantPaintedRegion.unite(topIntersection);
2269
2270         IntRect bottomIntersection = snappedPaintRect;
2271         bottomIntersection.intersect(snappedIntRect(bottomRelevantRect));
2272         m_bottomRelevantPaintedRegion.unite(bottomIntersection);
2273     } else if (topRelevantRect.intersects(snappedPaintRect))
2274         m_topRelevantPaintedRegion.unite(snappedPaintRect);
2275     else
2276         m_bottomRelevantPaintedRegion.unite(snappedPaintRect);
2277
2278     float topPaintedArea = m_topRelevantPaintedRegion.totalArea();
2279     float bottomPaintedArea = m_bottomRelevantPaintedRegion.totalArea();
2280     float viewArea = relevantRect.width() * relevantRect.height();
2281
2282     float ratioThatIsPaintedOnTop = topPaintedArea / viewArea;
2283     float ratioThatIsPaintedOnBottom = bottomPaintedArea / viewArea;
2284     float ratioOfViewThatIsUnpainted = m_relevantUnpaintedRegion.totalArea() / viewArea;
2285
2286     if (ratioThatIsPaintedOnTop > (gMinimumPaintedAreaRatio / 2) && ratioThatIsPaintedOnBottom > (gMinimumPaintedAreaRatio / 2)
2287         && ratioOfViewThatIsUnpainted < gMaximumUnpaintedAreaRatio) {
2288         m_isCountingRelevantRepaintedObjects = false;
2289         resetRelevantPaintedObjectCounter();
2290         if (Frame* frame = &mainFrame())
2291             frame->loader().didReachLayoutMilestone(DidHitRelevantRepaintedObjectsAreaThreshold);
2292     }
2293 }
2294
2295 void Page::addRelevantUnpaintedObject(RenderObject* object, const LayoutRect& objectPaintRect)
2296 {
2297     if (!isCountingRelevantRepaintedObjects())
2298         return;
2299
2300     // The objects are only relevant if they are being painted within the relevantViewRect().
2301     if (!objectPaintRect.intersects(snappedIntRect(relevantViewRect(&object->view()))))
2302         return;
2303
2304     m_relevantUnpaintedRenderObjects.add(object);
2305     m_relevantUnpaintedRegion.unite(snappedIntRect(objectPaintRect));
2306 }
2307
2308 void Page::suspendDeviceMotionAndOrientationUpdates()
2309 {
2310     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2311         if (Document* document = frame->document())
2312             document->suspendDeviceMotionAndOrientationUpdates();
2313     }
2314 }
2315
2316 void Page::resumeDeviceMotionAndOrientationUpdates()
2317 {
2318     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2319         if (Document* document = frame->document())
2320             document->resumeDeviceMotionAndOrientationUpdates();
2321     }
2322 }
2323
2324 void Page::suspendActiveDOMObjectsAndAnimations()
2325 {
2326     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext())
2327         frame->suspendActiveDOMObjectsAndAnimations();
2328 }
2329
2330 void Page::resumeActiveDOMObjectsAndAnimations()
2331 {
2332     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext())
2333         frame->resumeActiveDOMObjectsAndAnimations();
2334
2335     resumeAnimatingImages();
2336 }
2337
2338 bool Page::hasSeenAnyPlugin() const
2339 {
2340     return !m_seenPlugins.isEmpty();
2341 }
2342
2343 bool Page::hasSeenPlugin(const String& serviceType) const
2344 {
2345     return m_seenPlugins.contains(serviceType);
2346 }
2347
2348 void Page::sawPlugin(const String& serviceType)
2349 {
2350     m_seenPlugins.add(serviceType);
2351 }
2352
2353 void Page::resetSeenPlugins()
2354 {
2355     m_seenPlugins.clear();
2356 }
2357
2358 bool Page::hasSeenAnyMediaEngine() const
2359 {
2360     return !m_seenMediaEngines.isEmpty();
2361 }
2362
2363 bool Page::hasSeenMediaEngine(const String& engineDescription) const
2364 {
2365     return m_seenMediaEngines.contains(engineDescription);
2366 }
2367
2368 void Page::sawMediaEngine(const String& engineDescription)
2369 {
2370     m_seenMediaEngines.add(engineDescription);
2371 }
2372
2373 void Page::resetSeenMediaEngines()
2374 {
2375     m_seenMediaEngines.clear();
2376 }
2377
2378 void Page::hiddenPageCSSAnimationSuspensionStateChanged()
2379 {
2380     if (!isVisible()) {
2381         if (RuntimeEnabledFeatures::sharedFeatures().webAnimationsCSSIntegrationEnabled()) {
2382             forEachDocument([&] (Document& document) {
2383                 if (auto* timeline = document.existingTimeline()) {
2384                     if (m_settings->hiddenPageCSSAnimationSuspensionEnabled())
2385                         timeline->suspendAnimations();
2386                     else
2387                         timeline->resumeAnimations();
2388                 }
2389             });
2390         } else {
2391             if (m_settings->hiddenPageCSSAnimationSuspensionEnabled())
2392                 mainFrame().animation().suspendAnimations();
2393             else
2394                 mainFrame().animation().resumeAnimations();
2395         }
2396     }
2397 }
2398
2399 #if ENABLE(VIDEO_TRACK)
2400 void Page::captionPreferencesChanged()
2401 {
2402     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2403         if (!frame->document())
2404             continue;
2405         frame->document()->captionPreferencesChanged();
2406     }
2407 }
2408 #endif
2409
2410 void Page::forbidPrompts()
2411 {
2412     ++m_forbidPromptsDepth;
2413 }
2414
2415 void Page::allowPrompts()
2416 {
2417     ASSERT(m_forbidPromptsDepth);
2418     --m_forbidPromptsDepth;
2419 }
2420
2421 bool Page::arePromptsAllowed()
2422 {
2423     return !m_forbidPromptsDepth;
2424 }
2425
2426 void Page::logNavigation(const Navigation& navigation)
2427 {
2428     String navigationDescription;
2429     switch (navigation.type) {
2430     case FrameLoadType::Standard:
2431         navigationDescription = "standard"_s;
2432         break;
2433     case FrameLoadType::Back:
2434         navigationDescription = "back"_s;
2435         break;
2436     case FrameLoadType::Forward:
2437         navigationDescription = "forward"_s;
2438         break;
2439     case FrameLoadType::IndexedBackForward:
2440         navigationDescription = "indexedBackForward"_s;
2441         break;
2442     case FrameLoadType::Reload:
2443         navigationDescription = "reload"_s;
2444         break;
2445     case FrameLoadType::Same:
2446         navigationDescription = "same"_s;
2447         break;
2448     case FrameLoadType::ReloadFromOrigin:
2449         navigationDescription = "reloadFromOrigin"_s;
2450         break;
2451     case FrameLoadType::ReloadExpiredOnly:
2452         navigationDescription = "reloadRevalidatingExpired"_s;
2453         break;
2454     case FrameLoadType::Replace:
2455     case FrameLoadType::RedirectWithLockedBackForwardList:
2456         // Not logging those for now.
2457         return;
2458     }
2459     diagnosticLoggingClient().logDiagnosticMessage(DiagnosticLoggingKeys::navigationKey(), navigationDescription, ShouldSample::No);
2460
2461     if (!navigation.domain.isEmpty())
2462         diagnosticLoggingClient().logDiagnosticMessageWithEnhancedPrivacy(DiagnosticLoggingKeys::domainVisitedKey(), navigation.domain.string(), ShouldSample::Yes);
2463 }
2464
2465 void Page::mainFrameLoadStarted(const URL& destinationURL, FrameLoadType type)
2466 {
2467     Navigation navigation = { RegistrableDomain { destinationURL }, type };
2468
2469     // To avoid being too verbose, we only log navigations if the page is or becomes visible. This avoids logging non-user observable loads.
2470     if (!isVisible()) {
2471         m_navigationToLogWhenVisible = navigation;
2472         return;
2473     }
2474
2475     m_navigationToLogWhenVisible = WTF::nullopt;
2476     logNavigation(navigation);
2477 }
2478
2479 PluginInfoProvider& Page::pluginInfoProvider()
2480 {
2481     return m_pluginInfoProvider;
2482 }
2483
2484 UserContentProvider& Page::userContentProvider()
2485 {
2486     return m_userContentProvider;
2487 }
2488
2489 void Page::setUserContentProvider(Ref<UserContentProvider>&& userContentProvider)
2490 {
2491     m_userContentProvider->removePage(*this);
2492     m_userContentProvider = WTFMove(userContentProvider);
2493     m_userContentProvider->addPage(*this);
2494
2495     invalidateInjectedStyleSheetCacheInAllFrames();
2496 }
2497
2498 VisitedLinkStore& Page::visitedLinkStore()
2499 {
2500     return m_visitedLinkStore;
2501 }
2502
2503 void Page::setVisitedLinkStore(Ref<VisitedLinkStore>&& visitedLinkStore)
2504 {
2505     m_visitedLinkStore->removePage(*this);
2506     m_visitedLinkStore = WTFMove(visitedLinkStore);
2507     m_visitedLinkStore->addPage(*this);
2508
2509     invalidateStylesForAllLinks();
2510 }
2511
2512 PAL::SessionID Page::sessionID() const
2513 {
2514     return m_sessionID;
2515 }
2516
2517 // This is only called by WebKitLegacy.
2518 void Page::setSessionID(PAL::SessionID sessionID)
2519 {
2520     ASSERT(sessionID.isValid());
2521     ASSERT(m_sessionID == PAL::SessionID::legacyPrivateSessionID() || m_sessionID == PAL::SessionID::defaultSessionID());
2522     ASSERT(sessionID == PAL::SessionID::legacyPrivateSessionID() || sessionID == PAL::SessionID::defaultSessionID());
2523
2524 #if ENABLE(INDEXED_DATABASE)
2525     if (sessionID != m_sessionID)
2526         m_idbConnectionToServer = nullptr;
2527 #endif
2528
2529     if (sessionID != m_sessionID && m_sessionStorage)
2530         m_sessionStorage->setSessionIDForTesting(sessionID);
2531
2532     bool privateBrowsingStateChanged = (sessionID.isEphemeral() != m_sessionID.isEphemeral());
2533
2534     m_sessionID = sessionID;
2535
2536     if (!privateBrowsingStateChanged)
2537         return;
2538
2539     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2540         if (!frame->document())
2541             continue;
2542         frame->document()->privateBrowsingStateDidChange(m_sessionID);
2543     }
2544
2545     // Collect the PluginViews in to a vector to ensure that action the plug-in takes
2546     // from below privateBrowsingStateChanged does not affect their lifetime.
2547
2548     for (auto& view : pluginViews())
2549         view->privateBrowsingStateChanged(sessionID.isEphemeral());
2550 }
2551
2552 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
2553 void Page::addPlaybackTargetPickerClient(uint64_t contextId)
2554 {
2555     chrome().client().addPlaybackTargetPickerClient(contextId);
2556 }
2557
2558 void Page::removePlaybackTargetPickerClient(uint64_t contextId)
2559 {
2560     chrome().client().removePlaybackTargetPickerClient(contextId);
2561 }
2562
2563 void Page::showPlaybackTargetPicker(uint64_t contextId, const WebCore::IntPoint& location, bool isVideo, RouteSharingPolicy routeSharingPolicy, const String& routingContextUID)
2564 {
2565 #if PLATFORM(IOS_FAMILY)
2566     // FIXME: refactor iOS implementation.
2567     UNUSED_PARAM(contextId);
2568     UNUSED_PARAM(location);
2569     chrome().client().showPlaybackTargetPicker(isVideo, routeSharingPolicy, routingContextUID);
2570 #else
2571     UNUSED_PARAM(routeSharingPolicy);
2572     UNUSED_PARAM(routingContextUID);
2573     chrome().client().showPlaybackTargetPicker(contextId, location, isVideo);
2574 #endif
2575 }
2576
2577 void Page::playbackTargetPickerClientStateDidChange(uint64_t contextId, MediaProducer::MediaStateFlags state)
2578 {
2579     chrome().client().playbackTargetPickerClientStateDidChange(contextId, state);
2580 }
2581
2582 void Page::setMockMediaPlaybackTargetPickerEnabled(bool enabled)
2583 {
2584     chrome().client().setMockMediaPlaybackTargetPickerEnabled(enabled);
2585 }
2586
2587 void Page::setMockMediaPlaybackTargetPickerState(const String& name, MediaPlaybackTargetContext::State state)
2588 {
2589     chrome().client().setMockMediaPlaybackTargetPickerState(name, state);
2590 }
2591
2592 void Page::mockMediaPlaybackTargetPickerDismissPopup()
2593 {
2594     chrome().client().mockMediaPlaybackTargetPickerDismissPopup();
2595 }
2596
2597 void Page::setPlaybackTarget(uint64_t contextId, Ref<MediaPlaybackTarget>&& target)
2598 {
2599     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2600         if (!frame->document())
2601             continue;
2602         frame->document()->setPlaybackTarget(contextId, target.copyRef());
2603     }
2604 }
2605
2606 void Page::playbackTargetAvailabilityDidChange(uint64_t contextId, bool available)
2607 {
2608     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2609         if (!frame->document())
2610             continue;
2611         frame->document()->playbackTargetAvailabilityDidChange(contextId, available);
2612     }
2613 }
2614
2615 void Page::setShouldPlayToPlaybackTarget(uint64_t clientId, bool shouldPlay)
2616 {
2617     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2618         if (!frame->document())
2619             continue;
2620         frame->document()->setShouldPlayToPlaybackTarget(clientId, shouldPlay);
2621     }
2622 }
2623
2624 void Page::playbackTargetPickerWasDismissed(uint64_t clientId)
2625 {
2626     for (auto* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2627         if (!frame->document())
2628             continue;
2629         frame->document()->playbackTargetPickerWasDismissed(clientId);
2630     }
2631 }
2632 #endif
2633
2634 WheelEventTestMonitor& Page::ensureWheelEventTestMonitor()
2635 {
2636     if (!m_wheelEventTestMonitor) {
2637         m_wheelEventTestMonitor = adoptRef(new WheelEventTestMonitor());
2638         // We need to update the scrolling coordinator so that the mainframe scrolling node can expect wheel event test triggers.
2639         if (auto* frameView = mainFrame().view()) {
2640             if (m_scrollingCoordinator)
2641                 m_scrollingCoordinator->updateIsMonitoringWheelEventsForFrameView(*frameView);
2642         }
2643     }
2644
2645     return *m_wheelEventTestMonitor;
2646 }
2647
2648 #if ENABLE(VIDEO)
2649 void Page::setAllowsMediaDocumentInlinePlayback(bool flag)
2650 {
2651     if (m_allowsMediaDocumentInlinePlayback == flag)
2652         return;
2653     m_allowsMediaDocumentInlinePlayback = flag;
2654
2655     Vector<Ref<Document>> documents;
2656     for (Frame* frame = &m_mainFrame.get(); frame; frame = frame->tree().traverseNext())
2657         documents.append(*frame->document());
2658
2659     for (auto& document : documents)
2660         document->allowsMediaDocumentInlinePlaybackChanged();
2661 }
2662 #endif
2663
2664 #if ENABLE(INDEXED_DATABASE)
2665 IDBClient::IDBConnectionToServer& Page::idbConnection()
2666 {
2667     if (!m_idbConnectionToServer)
2668         m_idbConnectionToServer = &databaseProvider().idbConnectionToServerForSession(m_sessionID);
2669     
2670     return *m_idbConnectionToServer;
2671 }
2672
2673 IDBClient::IDBConnectionToServer* Page::optionalIDBConnection()
2674 {
2675     return m_idbConnectionToServer.get();
2676 }
2677
2678 void Page::clearIDBConnection()
2679 {
2680     m_idbConnectionToServer = nullptr;
2681 }
2682 #endif
2683
2684 #if ENABLE(RESOURCE_USAGE)
2685 void Page::setResourceUsageOverlayVisible(bool visible)
2686 {
2687     if (!visible) {
2688         m_resourceUsageOverlay = nullptr;
2689         return;
2690     }
2691
2692     if (!m_resourceUsageOverlay && m_settings->acceleratedCompositingEnabled())
2693         m_resourceUsageOverlay = makeUnique<ResourceUsageOverlay>(*this);
2694 }
2695 #endif
2696
2697 bool Page::isAlwaysOnLoggingAllowed() const
2698 {
2699     return m_sessionID.isAlwaysOnLoggingAllowed();
2700 }
2701
2702 String Page::captionUserPreferencesStyleSheet()
2703 {
2704     return m_captionUserPreferencesStyleSheet;
2705 }
2706
2707 void Page::setCaptionUserPreferencesStyleSheet(const String& styleSheet)
2708 {
2709     if (m_captionUserPreferencesStyleSheet == styleSheet)
2710         return;
2711
2712     m_captionUserPreferencesStyleSheet = styleSheet;
2713 }
2714
2715 void Page::accessibilitySettingsDidChange()
2716 {
2717     forEachDocument([](auto& document) {
2718         document.styleScope().evaluateMediaQueriesForAccessibilitySettingsChange();
2719         document.updateElementsAffectedByMediaQueries();
2720         document.scheduleTimedRenderingUpdate();
2721     });
2722 }
2723
2724 void Page::appearanceDidChange()
2725 {
2726     forEachDocument([](auto& document) {
2727         document.styleScope().didChangeStyleSheetEnvironment();
2728         document.styleScope().evaluateMediaQueriesForAppearanceChange();
2729         document.updateElementsAffectedByMediaQueries();
2730         document.scheduleTimedRenderingUpdate();
2731     });
2732 }
2733
2734 void Page::setUnobscuredSafeAreaInsets(const FloatBoxExtent& insets)
2735 {
2736     if (m_unobscuredSafeAreaInsets == insets)
2737         return;
2738
2739     m_unobscuredSafeAreaInsets = insets;
2740
2741     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2742         if (!frame->document())
2743             continue;
2744         frame->document()->constantProperties().didChangeSafeAreaInsets();
2745     }
2746 }
2747
2748 void Page::setUseSystemAppearance(bool value)
2749 {
2750     if (m_useSystemAppearance == value)
2751         return;
2752
2753     m_useSystemAppearance = value;
2754
2755     appearanceDidChange();
2756
2757     for (auto* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2758         auto* document = frame->document();
2759         if (!document)
2760             continue;
2761
2762         // System apperance change may affect stylesheet parsing. We need to reparse.
2763         document->extensionStyleSheets().clearPageUserSheet();
2764         document->extensionStyleSheets().invalidateInjectedStyleSheetCache();
2765     }
2766 }
2767
2768 void Page::effectiveAppearanceDidChange(bool useDarkAppearance, bool useElevatedUserInterfaceLevel)
2769 {
2770 #if HAVE(OS_DARK_MODE_SUPPORT)
2771     if (m_useDarkAppearance == useDarkAppearance && m_useElevatedUserInterfaceLevel == useElevatedUserInterfaceLevel)
2772         return;
2773
2774     m_useDarkAppearance = useDarkAppearance;
2775     m_useElevatedUserInterfaceLevel = useElevatedUserInterfaceLevel;
2776
2777     InspectorInstrumentation::defaultAppearanceDidChange(*this, useDarkAppearance);
2778
2779     appearanceDidChange();
2780 #else
2781     UNUSED_PARAM(useDarkAppearance);
2782
2783     if (m_useElevatedUserInterfaceLevel == useElevatedUserInterfaceLevel)
2784         return;
2785
2786     m_useElevatedUserInterfaceLevel = useElevatedUserInterfaceLevel;
2787
2788     appearanceDidChange();
2789 #endif
2790 }
2791
2792 bool Page::useDarkAppearance() const
2793 {
2794 #if HAVE(OS_DARK_MODE_SUPPORT)
2795     FrameView* view = mainFrame().view();
2796     if (!view || !equalLettersIgnoringASCIICase(view->mediaType(), "screen"))
2797         return false;
2798     if (m_useDarkAppearanceOverride)
2799         return m_useDarkAppearanceOverride.value();
2800     return m_useDarkAppearance;
2801 #else
2802     return false;
2803 #endif
2804 }
2805
2806 void Page::setUseDarkAppearanceOverride(Optional<bool> valueOverride)
2807 {
2808 #if HAVE(OS_DARK_MODE_SUPPORT)
2809     if (valueOverride == m_useDarkAppearanceOverride)
2810         return;
2811
2812     m_useDarkAppearanceOverride = valueOverride;
2813
2814     appearanceDidChange();
2815 #else
2816     UNUSED_PARAM(valueOverride);
2817 #endif
2818 }
2819
2820 void Page::setFullscreenInsets(const FloatBoxExtent& insets)
2821 {
2822     if (insets == m_fullscreenInsets)
2823         return;
2824     m_fullscreenInsets = insets;
2825
2826     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2827         if (!frame->document())
2828             continue;
2829         frame->document()->constantProperties().didChangeFullscreenInsets();
2830     }
2831 }
2832
2833 void Page::setFullscreenAutoHideDuration(Seconds duration)
2834 {
2835     if (duration == m_fullscreenAutoHideDuration)
2836         return;
2837     m_fullscreenAutoHideDuration = duration;
2838     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2839         if (!frame->document())
2840             continue;
2841         frame->document()->constantProperties().setFullscreenAutoHideDuration(duration);
2842     }
2843 }
2844
2845 void Page::setFullscreenControlsHidden(bool hidden)
2846 {
2847 #if ENABLE(FULLSCREEN_API)
2848     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2849         if (!frame->document())
2850             continue;
2851         frame->document()->fullscreenManager().setFullscreenControlsHidden(hidden);
2852     }
2853 #else
2854     UNUSED_PARAM(hidden);
2855 #endif
2856 }
2857
2858 #if ENABLE(DATA_INTERACTION)
2859
2860 bool Page::hasSelectionAtPosition(const FloatPoint& position) const
2861 {
2862     auto currentSelection = m_mainFrame->selection().selection();
2863     if (!currentSelection.isRange())
2864         return false;
2865
2866     if (auto selectedRange = currentSelection.toNormalizedRange()) {
2867         Vector<SelectionRect> selectionRects;
2868         selectedRange->collectSelectionRects(selectionRects);
2869         for (auto selectionRect : selectionRects) {
2870             if (FloatRect(selectionRect.rect()).contains(position))
2871                 return true;
2872         }
2873     }
2874
2875     return false;
2876 }
2877
2878 #endif
2879
2880 void Page::disableICECandidateFiltering()
2881 {
2882     m_shouldEnableICECandidateFilteringByDefault = false;
2883 #if ENABLE(WEB_RTC)
2884     m_rtcController.disableICECandidateFilteringForAllOrigins();
2885 #endif
2886 }
2887
2888 void Page::enableICECandidateFiltering()
2889 {
2890     m_shouldEnableICECandidateFilteringByDefault = true;
2891 #if ENABLE(WEB_RTC)
2892     m_rtcController.enableICECandidateFiltering();
2893 #endif
2894 }
2895
2896 void Page::didChangeMainDocument()
2897 {
2898 #if ENABLE(WEB_RTC)
2899     m_rtcController.reset(m_shouldEnableICECandidateFilteringByDefault);
2900 #endif
2901 #if ENABLE(POINTER_EVENTS)
2902     m_pointerCaptureController->reset();
2903 #endif
2904 }
2905
2906 RenderingUpdateScheduler& Page::renderingUpdateScheduler()
2907 {
2908     if (!m_renderingUpdateScheduler)
2909         m_renderingUpdateScheduler = RenderingUpdateScheduler::create(*this);
2910     return *m_renderingUpdateScheduler;
2911 }
2912
2913 void Page::forEachDocument(const Function<void(Document&)>& functor)
2914 {
2915     for (auto& document : collectDocuments())
2916         functor(document);
2917 }
2918
2919 Vector<Ref<Document>> Page::collectDocuments()
2920 {
2921     Vector<Ref<Document>> documents;
2922     for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
2923         auto* document = frame->document();
2924         if (!document)
2925             continue;
2926         documents.append(*document);
2927     }
2928     return documents;
2929 }
2930
2931 void Page::applicationWillResignActive()
2932 {
2933     forEachDocument([&] (Document& document) {
2934         document.forEachApplicationStateChangeListener([&] (ApplicationStateChangeListener& listener) {
2935             listener.applicationWillResignActive();
2936         });
2937     });
2938 }
2939
2940 void Page::applicationDidEnterBackground()
2941 {
2942     m_libWebRTCProvider->setActive(false);
2943 }
2944
2945 void Page::applicationWillEnterForeground()
2946 {
2947     m_libWebRTCProvider->setActive(true);
2948 }
2949
2950 void Page::applicationDidBecomeActive()
2951 {
2952     forEachDocument([&] (Document& document) {
2953         document.forEachApplicationStateChangeListener([&] (ApplicationStateChangeListener& listener) {
2954             listener.applicationDidBecomeActive();
2955         });
2956     });
2957 }
2958
2959 #if PLATFORM(MAC)
2960 ScrollLatchingState* Page::latchingState()
2961 {
2962     if (m_latchingState.isEmpty())
2963         return nullptr;
2964
2965     return &m_latchingState.last();
2966 }
2967
2968 void Page::pushNewLatchingState()
2969 {
2970     m_latchingState.append(ScrollLatchingState());
2971 }
2972
2973 void Page::resetLatchingState()
2974 {
2975     m_latchingState.clear();
2976 }
2977
2978 void Page::popLatchingState()
2979 {
2980     m_latchingState.removeLast();
2981 }
2982
2983 void Page::removeLatchingStateForTarget(Element& targetNode)
2984 {
2985     if (m_latchingState.isEmpty())
2986         return;
2987
2988     m_latchingState.removeAllMatching([&targetNode] (ScrollLatchingState& state) {
2989         auto* wheelElement = state.wheelEventElement();
2990         if (!wheelElement)
2991             return false;
2992
2993         return targetNode.isEqualNode(wheelElement);
2994     });
2995 }
2996 #endif // PLATFORM(MAC)
2997
2998 static void dispatchPrintEvent(Frame& mainFrame, const AtomString& eventType)
2999 {
3000     Vector<Ref<Frame>> frames;
3001     for (auto* frame = &mainFrame; frame; frame = frame->tree().traverseNext())
3002         frames.append(*frame);
3003
3004     for (auto& frame : frames) {
3005         if (auto* window = frame->window())
3006             window->dispatchEvent(Event::create(eventType, Event::CanBubble::No, Event::IsCancelable::No), window->document());
3007     }
3008 }
3009
3010 void Page::dispatchBeforePrintEvent()
3011 {
3012     dispatchPrintEvent(m_mainFrame, eventNames().beforeprintEvent);
3013 }
3014
3015 void Page::dispatchAfterPrintEvent()
3016 {
3017     dispatchPrintEvent(m_mainFrame, eventNames().afterprintEvent);
3018 }
3019
3020 #if ENABLE(APPLE_PAY)
3021 void Page::setPaymentCoordinator(std::unique_ptr<PaymentCoordinator>&& paymentCoordinator)
3022 {
3023     m_paymentCoordinator = WTFMove(paymentCoordinator);
3024 }
3025 #endif
3026
3027 void Page::configureLoggingChannel(const String& channelName, WTFLogChannelState state, WTFLogLevel level)
3028 {
3029 #if !RELEASE_LOG_DISABLED
3030     if (auto* channel = getLogChannel(channelName)) {
3031         channel->state = state;
3032         channel->level = level;
3033
3034 #if USE(LIBWEBRTC)
3035         if (channel == &LogWebRTC && m_mainFrame->document())
3036             libWebRTCProvider().setEnableLogging(!sessionID().isEphemeral());
3037 #endif
3038     }
3039
3040     chrome().client().configureLoggingChannel(channelName, state, level);
3041 #else
3042     UNUSED_PARAM(channelName);
3043     UNUSED_PARAM(state);
3044     UNUSED_PARAM(level);
3045 #endif
3046 }
3047
3048 void Page::didFinishLoadingImageForElement(HTMLImageElement& element)
3049 {
3050     auto protectedElement = makeRef(element);
3051     if (auto frame = makeRefPtr(element.document().frame()))
3052         frame->editor().revealSelectionIfNeededAfterLoadingImageForElement(element);
3053     chrome().client().didFinishLoadingImageForElement(element);
3054 }
3055
3056 #if ENABLE(TEXT_AUTOSIZING)
3057 void Page::recomputeTextAutoSizingInAllFrames()
3058 {
3059     ASSERT(settings().textAutosizingEnabled() && settings().textAutosizingUsesIdempotentMode());
3060     for (auto* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
3061         if (!frame->document())
3062             continue;
3063         auto& document = *frame->document();
3064         if (!document.renderView() || !document.styleScope().resolverIfExists())
3065             continue;
3066
3067         auto& styleResolver = document.styleScope().resolver();
3068         for (auto& renderer : descendantsOfType<RenderElement>(*document.renderView())) {
3069             if (auto* element = renderer.element()) {
3070                 auto needsLayout = styleResolver.adjustRenderStyleForTextAutosizing(renderer.mutableStyle(), *element);
3071                 if (needsLayout)
3072                     renderer.setNeedsLayout();
3073             }
3074         }
3075     }
3076 }
3077 #endif
3078
3079 } // namespace WebCore