[Extra zoom mode] Add a mechanism to override default viewport behaviors in extra...
[WebKit-https.git] / Source / WebKit / WebProcess / WebPage / ios / WebPageIOS.mm
1 /*
2  * Copyright (C) 2012-2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "WebPage.h"
28
29 #if PLATFORM(IOS)
30
31 #import "AccessibilityIOS.h"
32 #import "AssistedNodeInformation.h"
33 #import "DataReference.h"
34 #import "DrawingArea.h"
35 #import "EditingRange.h"
36 #import "EditorState.h"
37 #import "GestureTypes.h"
38 #import "InteractionInformationAtPosition.h"
39 #import "Logging.h"
40 #import "PluginView.h"
41 #import "PrintInfo.h"
42 #import "RemoteLayerTreeDrawingArea.h"
43 #import "SandboxUtilities.h"
44 #import "UserData.h"
45 #import "VisibleContentRectUpdateInfo.h"
46 #import "WKAccessibilityWebPageObjectIOS.h"
47 #import "WebChromeClient.h"
48 #import "WebCoreArgumentCoders.h"
49 #import "WebFrame.h"
50 #import "WebImage.h"
51 #import "WebPageProxyMessages.h"
52 #import "WebPreviewLoaderClient.h"
53 #import "WebProcess.h"
54 #import <CoreText/CTFont.h>
55 #import <WebCore/Autofill.h>
56 #import <WebCore/AutofillElements.h>
57 #import <WebCore/Chrome.h>
58 #import <WebCore/DataDetection.h>
59 #import <WebCore/DiagnosticLoggingClient.h>
60 #import <WebCore/DiagnosticLoggingKeys.h>
61 #import <WebCore/DragController.h>
62 #import <WebCore/Editing.h>
63 #import <WebCore/Editor.h>
64 #import <WebCore/Element.h>
65 #import <WebCore/ElementAncestorIterator.h>
66 #import <WebCore/EventHandler.h>
67 #import <WebCore/File.h>
68 #import <WebCore/FloatQuad.h>
69 #import <WebCore/FocusController.h>
70 #import <WebCore/Frame.h>
71 #import <WebCore/FrameLoaderClient.h>
72 #import <WebCore/FrameView.h>
73 #import <WebCore/GeometryUtilities.h>
74 #import <WebCore/HTMLAreaElement.h>
75 #import <WebCore/HTMLAttachmentElement.h>
76 #import <WebCore/HTMLBodyElement.h>
77 #import <WebCore/HTMLElement.h>
78 #import <WebCore/HTMLElementTypeHelpers.h>
79 #import <WebCore/HTMLFormElement.h>
80 #import <WebCore/HTMLImageElement.h>
81 #import <WebCore/HTMLInputElement.h>
82 #import <WebCore/HTMLOptGroupElement.h>
83 #import <WebCore/HTMLOptionElement.h>
84 #import <WebCore/HTMLParserIdioms.h>
85 #import <WebCore/HTMLSelectElement.h>
86 #import <WebCore/HTMLTextAreaElement.h>
87 #import <WebCore/HistoryItem.h>
88 #import <WebCore/HitTestResult.h>
89 #import <WebCore/KeyboardEvent.h>
90 #import <WebCore/LibWebRTCProvider.h>
91 #import <WebCore/MediaSessionManagerIOS.h>
92 #import <WebCore/Node.h>
93 #import <WebCore/NodeList.h>
94 #import <WebCore/NotImplemented.h>
95 #import <WebCore/Page.h>
96 #import <WebCore/Pasteboard.h>
97 #import <WebCore/PlatformKeyboardEvent.h>
98 #import <WebCore/PlatformMouseEvent.h>
99 #import <WebCore/RenderBlock.h>
100 #import <WebCore/RenderImage.h>
101 #import <WebCore/RenderThemeIOS.h>
102 #import <WebCore/RenderView.h>
103 #import <WebCore/RuntimeApplicationChecks.h>
104 #import <WebCore/Settings.h>
105 #import <WebCore/SharedBuffer.h>
106 #import <WebCore/StyleProperties.h>
107 #import <WebCore/TextIndicator.h>
108 #import <WebCore/TextIterator.h>
109 #import <WebCore/VisibleUnits.h>
110 #import <WebCore/WKContentObservation.h>
111 #import <WebCore/WebEvent.h>
112 #import <wtf/MathExtras.h>
113 #import <wtf/MemoryPressureHandler.h>
114 #import <wtf/SetForScope.h>
115 #import <wtf/SoftLinking.h>
116 #import <wtf/text/TextStream.h>
117
118 #if ENABLE(MEDIA_STREAM)
119 #import "CelestialSPI.h"
120 SOFT_LINK_PRIVATE_FRAMEWORK_OPTIONAL(Celestial)
121 SOFT_LINK_CLASS_OPTIONAL(Celestial, AVSystemController)
122 SOFT_LINK_CONSTANT_MAY_FAIL(Celestial, AVSystemController_PIDToInheritApplicationStateFrom, NSString *)
123 #endif
124
125 using namespace WebCore;
126
127 namespace WebKit {
128
129 const int blockSelectionStartWidth = 100;
130 const int blockSelectionStartHeight = 100;
131
132 void WebPage::platformInitialize()
133 {
134     platformInitializeAccessibility();
135 }
136
137 void WebPage::platformDetach()
138 {
139     [m_mockAccessibilityElement setWebPage:nullptr];
140 }
141     
142 void WebPage::platformInitializeAccessibility()
143 {
144     m_mockAccessibilityElement = adoptNS([[WKAccessibilityWebPageObject alloc] init]);
145     [m_mockAccessibilityElement setWebPage:this];
146     
147     NSData *remoteToken = newAccessibilityRemoteToken([NSUUID UUID]);
148     IPC::DataReference dataToken = IPC::DataReference(reinterpret_cast<const uint8_t*>([remoteToken bytes]), [remoteToken length]);
149     send(Messages::WebPageProxy::RegisterWebProcessAccessibilityToken(dataToken));
150 }
151
152 static void computeEditableRootHasContentAndPlainText(const VisibleSelection& selection, EditorState::PostLayoutData& data)
153 {
154     data.hasContent = false;
155     data.hasPlainText = false;
156     if (!selection.isContentEditable())
157         return;
158
159     if (data.selectedTextLength || data.characterAfterSelection || data.characterBeforeSelection || data.twoCharacterBeforeSelection) {
160         // If any of these variables have been previously set, the editable root must have plain text content, so we can bail from the remainder of the check.
161         data.hasContent = true;
162         data.hasPlainText = true;
163         return;
164     }
165
166     auto* root = selection.rootEditableElement();
167     if (!root)
168         return;
169
170     auto startInEditableRoot = firstPositionInNode(root);
171     data.hasContent = root->hasChildNodes() && !isEndOfEditableOrNonEditableContent(startInEditableRoot);
172     data.hasPlainText = data.hasContent && hasAnyPlainText(Range::create(root->document(), VisiblePosition { startInEditableRoot }, VisiblePosition { lastPositionInNode(root) }));
173 }
174
175 void WebPage::platformEditorState(Frame& frame, EditorState& result, IncludePostLayoutDataHint shouldIncludePostLayoutData) const
176 {
177     if (frame.editor().hasComposition()) {
178         RefPtr<Range> compositionRange = frame.editor().compositionRange();
179         Vector<WebCore::SelectionRect> compositionRects;
180         if (compositionRange) {
181             compositionRange->collectSelectionRects(compositionRects);
182             if (compositionRects.size())
183                 result.firstMarkedRect = compositionRects[0].rect();
184             if (compositionRects.size() > 1)
185                 result.lastMarkedRect = compositionRects.last().rect();
186             else
187                 result.lastMarkedRect = result.firstMarkedRect;
188             result.markedText = plainTextReplacingNoBreakSpace(compositionRange.get());
189         }
190     }
191
192     // We only set the remaining EditorState entries if the layout is done. To compute these
193     // entries, we need the layout to be done and we don't want to trigger a synchronous
194     // layout as this would be bad for performance. If we have a composition, we send everything
195     // right away as the UIProcess needs the caretRects ASAP for marked text.
196     bool frameViewHasFinishedLayout = frame.view() && !frame.view()->needsLayout();
197     if (shouldIncludePostLayoutData == IncludePostLayoutDataHint::No && !frameViewHasFinishedLayout && !frame.editor().hasComposition()) {
198         result.isMissingPostLayoutData = true;
199         return;
200     }
201
202     auto& postLayoutData = result.postLayoutData();
203     FrameView* view = frame.view();
204     const VisibleSelection& selection = frame.selection().selection();
205     postLayoutData.isStableStateUpdate = m_isInStableState;
206     bool startNodeIsInsideFixedPosition = false;
207     bool endNodeIsInsideFixedPosition = false;
208     if (selection.isCaret()) {
209         postLayoutData.caretRectAtStart = view->contentsToRootView(frame.selection().absoluteCaretBounds(&startNodeIsInsideFixedPosition));
210         endNodeIsInsideFixedPosition = startNodeIsInsideFixedPosition;
211         postLayoutData.caretRectAtEnd = postLayoutData.caretRectAtStart;
212         // FIXME: The following check should take into account writing direction.
213         postLayoutData.isReplaceAllowed = result.isContentEditable && atBoundaryOfGranularity(selection.start(), WordGranularity, DirectionForward);
214         postLayoutData.wordAtSelection = plainTextReplacingNoBreakSpace(wordRangeFromPosition(selection.start()).get());
215         if (selection.isContentEditable())
216             charactersAroundPosition(selection.start(), postLayoutData.characterAfterSelection, postLayoutData.characterBeforeSelection, postLayoutData.twoCharacterBeforeSelection);
217     } else if (selection.isRange()) {
218         postLayoutData.caretRectAtStart = view->contentsToRootView(VisiblePosition(selection.start()).absoluteCaretBounds(&startNodeIsInsideFixedPosition));
219         postLayoutData.caretRectAtEnd = view->contentsToRootView(VisiblePosition(selection.end()).absoluteCaretBounds(&endNodeIsInsideFixedPosition));
220         RefPtr<Range> selectedRange = selection.toNormalizedRange();
221         String selectedText;
222         if (selectedRange) {
223             selectedRange->collectSelectionRects(postLayoutData.selectionRects);
224             convertSelectionRectsToRootView(view, postLayoutData.selectionRects);
225             selectedText = plainTextReplacingNoBreakSpace(selectedRange.get(), TextIteratorDefaultBehavior, true);
226             postLayoutData.selectedTextLength = selectedText.length();
227             const int maxSelectedTextLength = 200;
228             if (selectedText.length() <= maxSelectedTextLength)
229                 postLayoutData.wordAtSelection = selectedText;
230         }
231         // FIXME: We should disallow replace when the string contains only CJ characters.
232         postLayoutData.isReplaceAllowed = result.isContentEditable && !result.isInPasswordField && !selectedText.isAllSpecialCharacters<isHTMLSpace>();
233     }
234     postLayoutData.insideFixedPosition = startNodeIsInsideFixedPosition || endNodeIsInsideFixedPosition;
235     if (!selection.isNone()) {
236         if (m_assistedNode && m_assistedNode->renderer()) {
237             postLayoutData.selectionClipRect = view->contentsToRootView(m_assistedNode->renderer()->absoluteBoundingBoxRect());
238             postLayoutData.caretColor = m_assistedNode->renderer()->style().caretColor();
239         }
240         computeEditableRootHasContentAndPlainText(selection, postLayoutData);
241     }
242 }
243
244 FloatSize WebPage::screenSize() const
245 {
246     return m_screenSize;
247 }
248
249 FloatSize WebPage::availableScreenSize() const
250 {
251     return m_availableScreenSize;
252 }
253
254 FloatSize WebPage::overrideScreenSize() const
255 {
256     return m_overrideScreenSize;
257 }
258
259 void WebPage::didReceiveMobileDocType(bool isMobileDoctype)
260 {
261     resetViewportDefaultConfiguration(m_mainFrame.get(), isMobileDoctype);
262 }
263
264 void WebPage::savePageState(HistoryItem& historyItem)
265 {
266     historyItem.setScaleIsInitial(!m_userHasChangedPageScaleFactor);
267     historyItem.setMinimumLayoutSizeInScrollViewCoordinates(m_viewportConfiguration.minimumLayoutSize());
268     historyItem.setContentSize(m_viewportConfiguration.contentsSize());
269 }
270
271 static double scaleAfterViewportWidthChange(double currentScale, bool userHasChangedPageScaleFactor, const ViewportConfiguration& viewportConfiguration, float unobscuredWidthInScrollViewCoordinates, const IntSize& newContentSize, const IntSize& oldContentSize, float visibleHorizontalFraction)
272 {
273     double scale;
274     if (!userHasChangedPageScaleFactor)
275         scale = viewportConfiguration.initialScale();
276     else
277         scale = std::max(std::min(currentScale, viewportConfiguration.maximumScale()), viewportConfiguration.minimumScale());
278
279     LOG(VisibleRects, "scaleAfterViewportWidthChange getting scale %.2f", scale);
280
281     if (userHasChangedPageScaleFactor) {
282         // When the content size changes, we keep the same relative horizontal content width in view, otherwise we would
283         // end up zoomed too far in landscape->portrait, and too close in portrait->landscape.
284         double widthToKeepInView = visibleHorizontalFraction * newContentSize.width();
285         double newScale = unobscuredWidthInScrollViewCoordinates / widthToKeepInView;
286         scale = std::max(std::min(newScale, viewportConfiguration.maximumScale()), viewportConfiguration.minimumScale());
287     }
288     return scale;
289 }
290
291 static FloatPoint relativeCenterAfterContentSizeChange(const FloatRect& originalContentRect, IntSize oldContentSize, IntSize newContentSize)
292 {
293     // If the content size has changed, keep the same relative position.
294     FloatPoint oldContentCenter = originalContentRect.center();
295     float relativeHorizontalPosition = oldContentCenter.x() / oldContentSize.width();
296     float relativeVerticalPosition =  oldContentCenter.y() / oldContentSize.height();
297     return FloatPoint(relativeHorizontalPosition * newContentSize.width(), relativeVerticalPosition * newContentSize.height());
298 }
299
300 static inline FloatRect adjustExposedRectForNewScale(const FloatRect& exposedRect, double exposedRectScale, double newScale)
301 {
302     double overscaledWidth = exposedRect.width();
303     double missingHorizonalMargin = exposedRect.width() * exposedRectScale / newScale - overscaledWidth;
304
305     double overscaledHeight = exposedRect.height();
306     double missingVerticalMargin = exposedRect.height() * exposedRectScale / newScale - overscaledHeight;
307
308     return FloatRect(exposedRect.x() - missingHorizonalMargin / 2, exposedRect.y() - missingVerticalMargin / 2, exposedRect.width() + missingHorizonalMargin, exposedRect.height() + missingVerticalMargin);
309 }
310
311 void WebPage::restorePageState(const HistoryItem& historyItem)
312 {
313     // When a HistoryItem is cleared, its scale factor and scroll point are set to zero. We should not try to restore the other
314     // parameters in those conditions.
315     if (!historyItem.pageScaleFactor()) {
316         send(Messages::WebPageProxy::CouldNotRestorePageState());
317         return;
318     }
319
320     // We can restore the exposed rect and scale, but we cannot touch the scroll position since the obscured insets
321     // may be changing in the UIProcess. The UIProcess can update the position from the information we send and will then
322     // scroll to the correct position through a regular VisibleContentRectUpdate.
323
324     m_userHasChangedPageScaleFactor = !historyItem.scaleIsInitial();
325
326     FrameView& frameView = *m_page->mainFrame().view();
327
328     FloatSize currentMinimumLayoutSizeInScrollViewCoordinates = m_viewportConfiguration.minimumLayoutSize();
329     if (historyItem.minimumLayoutSizeInScrollViewCoordinates() == currentMinimumLayoutSizeInScrollViewCoordinates) {
330         float boundedScale = std::min<float>(m_viewportConfiguration.maximumScale(), std::max<float>(m_viewportConfiguration.minimumScale(), historyItem.pageScaleFactor()));
331         scalePage(boundedScale, IntPoint());
332
333         std::optional<FloatPoint> scrollPosition;
334         if (historyItem.shouldRestoreScrollPosition()) {
335             m_drawingArea->setExposedContentRect(historyItem.exposedContentRect());
336             scrollPosition = FloatPoint(historyItem.scrollPosition());
337         }
338         send(Messages::WebPageProxy::RestorePageState(scrollPosition, frameView.scrollOrigin(), historyItem.obscuredInsets(), boundedScale));
339     } else {
340         IntSize oldContentSize = historyItem.contentSize();
341         IntSize newContentSize = frameView.contentsSize();
342         double visibleHorizontalFraction = static_cast<float>(historyItem.unobscuredContentRect().width()) / oldContentSize.width();
343
344         double newScale = scaleAfterViewportWidthChange(historyItem.pageScaleFactor(), !historyItem.scaleIsInitial(), m_viewportConfiguration, currentMinimumLayoutSizeInScrollViewCoordinates.width(), newContentSize, oldContentSize, visibleHorizontalFraction);
345
346         std::optional<FloatPoint> newCenter;
347         if (historyItem.shouldRestoreScrollPosition()) {
348             if (!oldContentSize.isEmpty() && !newContentSize.isEmpty() && newContentSize != oldContentSize)
349                 newCenter = relativeCenterAfterContentSizeChange(historyItem.unobscuredContentRect(), oldContentSize, newContentSize);
350             else
351                 newCenter = FloatRect(historyItem.unobscuredContentRect()).center();
352         }
353
354         scalePage(newScale, IntPoint());
355         send(Messages::WebPageProxy::RestorePageCenterAndScale(newCenter, newScale));
356     }
357 }
358
359 double WebPage::minimumPageScaleFactor() const
360 {
361     if (!m_viewportConfiguration.allowsUserScaling())
362         return m_page->pageScaleFactor();
363     return m_viewportConfiguration.minimumScale();
364 }
365
366 double WebPage::maximumPageScaleFactor() const
367 {
368     if (!m_viewportConfiguration.allowsUserScaling())
369         return m_page->pageScaleFactor();
370     return m_viewportConfiguration.maximumScale();
371 }
372
373 double WebPage::maximumPageScaleFactorIgnoringAlwaysScalable() const
374 {
375     if (!m_viewportConfiguration.allowsUserScalingIgnoringAlwaysScalable())
376         return m_page->pageScaleFactor();
377     return m_viewportConfiguration.maximumScaleIgnoringAlwaysScalable();
378 }
379
380 bool WebPage::allowsUserScaling() const
381 {
382     return m_viewportConfiguration.allowsUserScaling();
383 }
384
385 bool WebPage::handleEditingKeyboardEvent(KeyboardEvent* event)
386 {
387     auto* platformEvent = event->underlyingPlatformEvent();
388     if (!platformEvent)
389         return false;
390
391     // FIXME: Interpret the event immediately upon receiving it in UI process, without sending to WebProcess first.
392     bool eventWasHandled = false;
393     bool sendResult = WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPageProxy::InterpretKeyEvent(editorState(), platformEvent->type() == PlatformKeyboardEvent::Char),
394         Messages::WebPageProxy::InterpretKeyEvent::Reply(eventWasHandled), m_pageID);
395     return sendResult && eventWasHandled;
396 }
397
398 bool WebPage::parentProcessHasServiceWorkerEntitlement() const
399 {
400     static bool hasEntitlement = connectedProcessHasEntitlement(WebProcess::singleton().parentProcessConnection()->xpcConnection(), "com.apple.developer.WebKit.ServiceWorkers");
401     return hasEntitlement;
402 }
403
404 void WebPage::sendComplexTextInputToPlugin(uint64_t, const String&)
405 {
406     notImplemented();
407 }
408
409 void WebPage::performDictionaryLookupAtLocation(const FloatPoint&)
410 {
411     notImplemented();
412 }
413
414 void WebPage::performDictionaryLookupForSelection(Frame&, const VisibleSelection&, TextIndicatorPresentationTransition)
415 {
416     notImplemented();
417 }
418
419 void WebPage::performDictionaryLookupForRange(Frame&, Range&, NSDictionary *, TextIndicatorPresentationTransition)
420 {
421     notImplemented();
422 }
423
424 bool WebPage::performNonEditingBehaviorForSelector(const String&, WebCore::KeyboardEvent*)
425 {
426     notImplemented();
427     return false;
428 }
429
430 bool WebPage::performDefaultBehaviorForKeyEvent(const WebKeyboardEvent&)
431 {
432     notImplemented();
433     return false;
434 }
435
436 void WebPage::getSelectionContext(CallbackID callbackID)
437 {
438     Frame& frame = m_page->focusController().focusedOrMainFrame();
439     if (!frame.selection().isRange()) {
440         send(Messages::WebPageProxy::SelectionContextCallback(String(), String(), String(), callbackID));
441         return;
442     }
443     const int selectionExtendedContextLength = 350;
444     
445     String selectedText = plainTextReplacingNoBreakSpace(frame.selection().selection().toNormalizedRange().get());
446     String textBefore = plainTextReplacingNoBreakSpace(rangeExpandedByCharactersInDirectionAtWordBoundary(frame.selection().selection().start(), selectionExtendedContextLength, DirectionBackward).get(), TextIteratorDefaultBehavior, true);
447     String textAfter = plainTextReplacingNoBreakSpace(rangeExpandedByCharactersInDirectionAtWordBoundary(frame.selection().selection().end(), selectionExtendedContextLength, DirectionForward).get(), TextIteratorDefaultBehavior, true);
448
449     send(Messages::WebPageProxy::SelectionContextCallback(selectedText, textBefore, textAfter, callbackID));
450 }
451
452 NSObject *WebPage::accessibilityObjectForMainFramePlugin()
453 {
454     if (!m_page)
455         return nil;
456     
457     if (auto* pluginView = pluginViewForFrame(&m_page->mainFrame()))
458         return pluginView->accessibilityObject();
459     
460     return nil;
461 }
462     
463 void WebPage::registerUIProcessAccessibilityTokens(const IPC::DataReference& elementToken, const IPC::DataReference&)
464 {
465     NSData *elementTokenData = [NSData dataWithBytes:elementToken.data() length:elementToken.size()];
466     [m_mockAccessibilityElement setRemoteTokenData:elementTokenData];
467 }
468
469 void WebPage::readSelectionFromPasteboard(const String&, bool&)
470 {
471     notImplemented();
472 }
473
474 void WebPage::getStringSelectionForPasteboard(String&)
475 {
476     notImplemented();
477 }
478
479 void WebPage::getDataSelectionForPasteboard(const String, SharedMemory::Handle&, uint64_t&)
480 {
481     notImplemented();
482 }
483
484 WKAccessibilityWebPageObject* WebPage::accessibilityRemoteObject()
485 {
486     notImplemented();
487     return 0;
488 }
489
490 bool WebPage::platformHasLocalDataForURL(const WebCore::URL&)
491 {
492     notImplemented();
493     return false;
494 }
495
496 String WebPage::cachedSuggestedFilenameForURL(const URL&)
497 {
498     notImplemented();
499     return String();
500 }
501
502 String WebPage::cachedResponseMIMETypeForURL(const URL&)
503 {
504     notImplemented();
505     return String();
506 }
507
508 RefPtr<SharedBuffer> WebPage::cachedResponseDataForURL(const URL&)
509 {
510     notImplemented();
511     return nullptr;
512 }
513
514 bool WebPage::platformCanHandleRequest(const WebCore::ResourceRequest&)
515 {
516     notImplemented();
517     return false;
518 }
519
520 void WebPage::shouldDelayWindowOrderingEvent(const WebKit::WebMouseEvent&, bool&)
521 {
522     notImplemented();
523 }
524
525 void WebPage::acceptsFirstMouse(int, const WebKit::WebMouseEvent&, bool&)
526 {
527     notImplemented();
528 }
529
530 void WebPage::computePagesForPrintingPDFDocument(uint64_t, const PrintInfo&, Vector<IntRect>&)
531 {
532     notImplemented();
533 }
534
535 void WebPage::drawPagesToPDFFromPDFDocument(CGContextRef, PDFDocument *, const PrintInfo&, uint32_t, uint32_t)
536 {
537     notImplemented();
538 }
539
540 void WebPage::advanceToNextMisspelling(bool)
541 {
542     notImplemented();
543 }
544
545 IntRect WebPage::rectForElementAtInteractionLocation()
546 {
547     HitTestResult result = m_page->mainFrame().eventHandler().hitTestResultAtPoint(m_lastInteractionLocation, HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::AllowChildFrameContent);
548     Node* hitNode = result.innerNode();
549     if (!hitNode || !hitNode->renderer())
550         return IntRect();
551     return result.innerNodeFrame()->view()->contentsToRootView(hitNode->renderer()->absoluteBoundingBoxRect(true));
552 }
553
554 void WebPage::updateSelectionAppearance()
555 {
556     Frame& frame = m_page->focusController().focusedOrMainFrame();
557     if (!frame.editor().ignoreSelectionChanges() && (frame.editor().hasComposition() || !frame.selection().selection().isNone()))
558         didChangeSelection();
559 }
560
561 void WebPage::handleSyntheticClick(Node* nodeRespondingToClick, const WebCore::FloatPoint& location)
562 {
563     IntPoint roundedAdjustedPoint = roundedIntPoint(location);
564     Frame& mainframe = m_page->mainFrame();
565
566     WKBeginObservingContentChanges(true);
567
568     mainframe.eventHandler().mouseMoved(PlatformMouseEvent(roundedAdjustedPoint, roundedAdjustedPoint, NoButton, PlatformEvent::MouseMoved, 0, false, false, false, false, WallTime::now(), WebCore::ForceAtClick, WebCore::NoTap));
569     mainframe.document()->updateStyleIfNeeded();
570
571     WKStopObservingContentChanges();
572
573     m_pendingSyntheticClickNode = nullptr;
574     m_pendingSyntheticClickLocation = FloatPoint();
575
576     if (m_isClosed)
577         return;
578
579     switch (WKObservedContentChange()) {
580     case WKContentVisibilityChange:
581         // The move event caused new contents to appear. Don't send the click event.
582         return;
583     case WKContentIndeterminateChange:
584         // Wait for callback to completePendingSyntheticClickForContentChangeObserver() to decide whether to send the click event.
585         m_pendingSyntheticClickNode = nodeRespondingToClick;
586         m_pendingSyntheticClickLocation = location;
587         return;
588     case WKContentNoChange:
589         completeSyntheticClick(nodeRespondingToClick, location, WebCore::OneFingerTap);
590         return;
591     }
592     ASSERT_NOT_REACHED();
593 }
594
595 void WebPage::completePendingSyntheticClickForContentChangeObserver()
596 {
597     if (!m_pendingSyntheticClickNode)
598         return;
599     // Only dispatch the click if the document didn't get changed by any timers started by the move event.
600     if (WKObservedContentChange() == WKContentNoChange)
601         completeSyntheticClick(m_pendingSyntheticClickNode.get(), m_pendingSyntheticClickLocation, WebCore::OneFingerTap);
602
603     m_pendingSyntheticClickNode = nullptr;
604     m_pendingSyntheticClickLocation = FloatPoint();
605 }
606
607 void WebPage::completeSyntheticClick(Node* nodeRespondingToClick, const WebCore::FloatPoint& location, SyntheticClickType syntheticClickType)
608 {
609     IntPoint roundedAdjustedPoint = roundedIntPoint(location);
610     Frame& mainframe = m_page->mainFrame();
611
612     RefPtr<Frame> oldFocusedFrame = m_page->focusController().focusedFrame();
613     RefPtr<Element> oldFocusedElement = oldFocusedFrame ? oldFocusedFrame->document()->focusedElement() : nullptr;
614
615     SetForScope<bool> userIsInteractingChange { m_userIsInteracting, true };
616
617     bool tapWasHandled = false;
618     m_lastInteractionLocation = roundedAdjustedPoint;
619
620     tapWasHandled |= mainframe.eventHandler().handleMousePressEvent(PlatformMouseEvent(roundedAdjustedPoint, roundedAdjustedPoint, LeftButton, PlatformEvent::MousePressed, 1, false, false, false, false, WallTime::now(), WebCore::ForceAtClick, syntheticClickType));
621     if (m_isClosed)
622         return;
623
624     tapWasHandled |= mainframe.eventHandler().handleMouseReleaseEvent(PlatformMouseEvent(roundedAdjustedPoint, roundedAdjustedPoint, LeftButton, PlatformEvent::MouseReleased, 1, false, false, false, false, WallTime::now(), WebCore::ForceAtClick, syntheticClickType));
625     if (m_isClosed)
626         return;
627
628     RefPtr<Frame> newFocusedFrame = m_page->focusController().focusedFrame();
629     RefPtr<Element> newFocusedElement = newFocusedFrame ? newFocusedFrame->document()->focusedElement() : nullptr;
630
631     // If the focus has not changed, we need to notify the client anyway, since it might be
632     // necessary to start assisting the node.
633     // If the node has been focused by JavaScript without user interaction, the
634     // keyboard is not on screen.
635     if (newFocusedElement && newFocusedElement == oldFocusedElement)
636         elementDidFocus(newFocusedElement.get());
637
638     if (!tapWasHandled || !nodeRespondingToClick || !nodeRespondingToClick->isElementNode())
639         send(Messages::WebPageProxy::DidNotHandleTapAsClick(roundedIntPoint(location)));
640     
641     send(Messages::WebPageProxy::DidCompleteSyntheticClick());
642 }
643
644 void WebPage::handleTap(const IntPoint& point, uint64_t lastLayerTreeTransactionId)
645 {
646     FloatPoint adjustedPoint;
647     Node* nodeRespondingToClick = m_page->mainFrame().nodeRespondingToClickEvents(point, adjustedPoint);
648     Frame* frameRespondingToClick = nodeRespondingToClick ? nodeRespondingToClick->document().frame() : nullptr;
649     IntPoint adjustedIntPoint = roundedIntPoint(adjustedPoint);
650
651     if (!frameRespondingToClick || lastLayerTreeTransactionId < WebFrame::fromCoreFrame(*frameRespondingToClick)->firstLayerTreeTransactionIDAfterDidCommitLoad())
652         send(Messages::WebPageProxy::DidNotHandleTapAsClick(adjustedIntPoint));
653 #if ENABLE(DATA_DETECTION)
654     else if (is<Element>(*nodeRespondingToClick) && DataDetection::shouldCancelDefaultAction(downcast<Element>(*nodeRespondingToClick))) {
655         InteractionInformationRequest request(adjustedIntPoint);
656         requestPositionInformation(request);
657         send(Messages::WebPageProxy::DidNotHandleTapAsClick(adjustedIntPoint));
658     }
659 #endif
660     else
661         handleSyntheticClick(nodeRespondingToClick, adjustedPoint);
662 }
663
664 void WebPage::requestAssistedNodeInformation(WebKit::CallbackID callbackID)
665 {
666     AssistedNodeInformation info;
667     if (m_assistedNode)
668         getAssistedNodeInformation(info);
669
670     send(Messages::WebPageProxy::AssistedNodeInformationCallback(info, callbackID));
671 }
672
673 #if ENABLE(DATA_INTERACTION)
674 void WebPage::requestStartDataInteraction(const IntPoint& clientPosition, const IntPoint& globalPosition)
675 {
676     bool didStart = m_page->mainFrame().eventHandler().tryToBeginDataInteractionAtPoint(clientPosition, globalPosition);
677     send(Messages::WebPageProxy::DidHandleStartDataInteractionRequest(didStart));
678 }
679
680 void WebPage::requestAdditionalItemsForDragSession(const IntPoint& clientPosition, const IntPoint& globalPosition)
681 {
682     // To augment the platform drag session with additional items, end the current drag session and begin a new drag session with the new drag item.
683     // This process is opaque to the UI process, which still maintains the old drag item in its drag session. Similarly, this persistent drag session
684     // is opaque to the web process, which only sees that the current drag has ended, and that a new one is beginning.
685     PlatformMouseEvent event(clientPosition, globalPosition, LeftButton, PlatformEvent::MouseMoved, 0, false, false, false, false, WallTime::now(), 0, NoTap);
686     m_page->dragController().dragEnded();
687     m_page->mainFrame().eventHandler().dragSourceEndedAt(event, DragOperationNone, MayExtendDragSession::Yes);
688
689     bool didHandleDrag = m_page->mainFrame().eventHandler().tryToBeginDataInteractionAtPoint(clientPosition, globalPosition);
690     send(Messages::WebPageProxy::DidHandleAdditionalDragItemsRequest(didHandleDrag));
691 }
692
693 void WebPage::didConcludeEditDataInteraction()
694 {
695     std::optional<TextIndicatorData> textIndicatorData;
696
697     static auto defaultEditDataInteractionTextIndicatorOptions = TextIndicatorOptionIncludeSnapshotOfAllVisibleContentWithoutSelection | TextIndicatorOptionExpandClipBeyondVisibleRect | TextIndicatorOptionPaintAllContent | TextIndicatorOptionIncludeMarginIfRangeMatchesSelection | TextIndicatorOptionPaintBackgrounds | TextIndicatorOptionComputeEstimatedBackgroundColor| TextIndicatorOptionUseSelectionRectForSizing | TextIndicatorOptionIncludeSnapshotWithSelectionHighlight;
698     auto& frame = m_page->focusController().focusedOrMainFrame();
699     if (auto range = frame.selection().selection().toNormalizedRange()) {
700         if (auto textIndicator = TextIndicator::createWithRange(*range, defaultEditDataInteractionTextIndicatorOptions, TextIndicatorPresentationTransition::None, FloatSize()))
701             textIndicatorData = textIndicator->data();
702     }
703
704     send(Messages::WebPageProxy::DidConcludeEditDataInteraction(textIndicatorData));
705 }
706 #endif
707
708 void WebPage::sendTapHighlightForNodeIfNecessary(uint64_t requestID, Node* node)
709 {
710 #if ENABLE(TOUCH_EVENTS)
711     if (!node)
712         return;
713
714     if (m_page->isEditable() && node == m_page->mainFrame().document()->body())
715         return;
716
717     if (is<Element>(*node)) {
718         ASSERT(m_page);
719         m_page->mainFrame().loader().client().prefetchDNS(downcast<Element>(*node).absoluteLinkURL().host());
720     }
721
722     if (is<HTMLAreaElement>(node)) {
723         node = downcast<HTMLAreaElement>(node)->imageElement();
724         if (!node)
725             return;
726     }
727
728     Vector<FloatQuad> quads;
729     if (RenderObject *renderer = node->renderer()) {
730         renderer->absoluteQuads(quads);
731         Color highlightColor = renderer->style().tapHighlightColor();
732         if (!node->document().frame()->isMainFrame()) {
733             FrameView* view = node->document().frame()->view();
734             for (size_t i = 0; i < quads.size(); ++i) {
735                 FloatQuad& currentQuad = quads[i];
736                 currentQuad.setP1(view->contentsToRootView(roundedIntPoint(currentQuad.p1())));
737                 currentQuad.setP2(view->contentsToRootView(roundedIntPoint(currentQuad.p2())));
738                 currentQuad.setP3(view->contentsToRootView(roundedIntPoint(currentQuad.p3())));
739                 currentQuad.setP4(view->contentsToRootView(roundedIntPoint(currentQuad.p4())));
740             }
741         }
742
743         RoundedRect::Radii borderRadii;
744         if (is<RenderBox>(*renderer))
745             borderRadii = downcast<RenderBox>(*renderer).borderRadii();
746
747         send(Messages::WebPageProxy::DidGetTapHighlightGeometries(requestID, highlightColor, quads, roundedIntSize(borderRadii.topLeft()), roundedIntSize(borderRadii.topRight()), roundedIntSize(borderRadii.bottomLeft()), roundedIntSize(borderRadii.bottomRight())));
748     }
749 #else
750     UNUSED_PARAM(requestID);
751     UNUSED_PARAM(node);
752 #endif
753 }
754
755 void WebPage::handleTwoFingerTapAtPoint(const WebCore::IntPoint& point, uint64_t requestID)
756 {
757     FloatPoint adjustedPoint;
758     Node* nodeRespondingToClick = m_page->mainFrame().nodeRespondingToClickEvents(point, adjustedPoint);
759     if (!nodeRespondingToClick || !nodeRespondingToClick->renderer()) {
760         send(Messages::WebPageProxy::DidNotHandleTapAsClick(roundedIntPoint(adjustedPoint)));
761         return;
762     }
763     sendTapHighlightForNodeIfNecessary(requestID, nodeRespondingToClick);
764 #if ENABLE(DATA_DETECTION)
765     if (is<Element>(*nodeRespondingToClick) && DataDetection::shouldCancelDefaultAction(downcast<Element>(*nodeRespondingToClick))) {
766         InteractionInformationRequest request(roundedIntPoint(adjustedPoint));
767         requestPositionInformation(request);
768         send(Messages::WebPageProxy::DidNotHandleTapAsClick(roundedIntPoint(adjustedPoint)));
769     } else
770 #endif
771         completeSyntheticClick(nodeRespondingToClick, adjustedPoint, WebCore::TwoFingerTap);
772 }
773
774 void WebPage::potentialTapAtPosition(uint64_t requestID, const WebCore::FloatPoint& position)
775 {
776     m_potentialTapNode = m_page->mainFrame().nodeRespondingToClickEvents(position, m_potentialTapLocation, m_potentialTapSecurityOrigin.get());
777     sendTapHighlightForNodeIfNecessary(requestID, m_potentialTapNode.get());
778 #if ENABLE(TOUCH_EVENTS)
779     if (m_potentialTapNode && !m_potentialTapNode->allowsDoubleTapGesture())
780         send(Messages::WebPageProxy::DisableDoubleTapGesturesDuringTapIfNecessary(requestID));
781 #endif
782 }
783
784 void WebPage::commitPotentialTap(uint64_t lastLayerTreeTransactionId)
785 {
786     if (!m_potentialTapNode || (!m_potentialTapNode->renderer() && !is<HTMLAreaElement>(m_potentialTapNode.get()))) {
787         commitPotentialTapFailed();
788         return;
789     }
790
791     FloatPoint adjustedPoint;
792     Node* nodeRespondingToClick = m_page->mainFrame().nodeRespondingToClickEvents(m_potentialTapLocation, adjustedPoint, m_potentialTapSecurityOrigin.get());
793     Frame* frameRespondingToClick = nodeRespondingToClick ? nodeRespondingToClick->document().frame() : nullptr;
794
795     if (!frameRespondingToClick || lastLayerTreeTransactionId < WebFrame::fromCoreFrame(*frameRespondingToClick)->firstLayerTreeTransactionIDAfterDidCommitLoad()) {
796         commitPotentialTapFailed();
797         return;
798     }
799
800     if (m_potentialTapNode == nodeRespondingToClick) {
801 #if ENABLE(DATA_DETECTION)
802         if (is<Element>(*nodeRespondingToClick) && DataDetection::shouldCancelDefaultAction(downcast<Element>(*nodeRespondingToClick))) {
803             InteractionInformationRequest request(roundedIntPoint(m_potentialTapLocation));
804             requestPositionInformation(request);
805             commitPotentialTapFailed();
806         } else
807 #endif
808             handleSyntheticClick(nodeRespondingToClick, adjustedPoint);
809     } else
810         commitPotentialTapFailed();
811
812     m_potentialTapNode = nullptr;
813     m_potentialTapLocation = FloatPoint();
814     m_potentialTapSecurityOrigin = nullptr;
815 }
816
817 void WebPage::commitPotentialTapFailed()
818 {
819     send(Messages::WebPageProxy::CommitPotentialTapFailed());
820     send(Messages::WebPageProxy::DidNotHandleTapAsClick(roundedIntPoint(m_potentialTapLocation)));
821 }
822
823 void WebPage::cancelPotentialTap()
824 {
825     cancelPotentialTapInFrame(*m_mainFrame);
826 }
827
828 void WebPage::cancelPotentialTapInFrame(WebFrame& frame)
829 {
830     if (m_potentialTapNode) {
831         Frame* potentialTapFrame = m_potentialTapNode->document().frame();
832         if (potentialTapFrame && !potentialTapFrame->tree().isDescendantOf(frame.coreFrame()))
833             return;
834     }
835
836     m_potentialTapNode = nullptr;
837     m_potentialTapLocation = FloatPoint();
838     m_potentialTapSecurityOrigin = nullptr;
839 }
840
841 void WebPage::tapHighlightAtPosition(uint64_t requestID, const FloatPoint& position)
842 {
843     Frame& mainframe = m_page->mainFrame();
844     FloatPoint adjustedPoint;
845     sendTapHighlightForNodeIfNecessary(requestID, mainframe.nodeRespondingToClickEvents(position, adjustedPoint));
846 }
847
848 void WebPage::inspectorNodeSearchMovedToPosition(const FloatPoint& position)
849 {
850     IntPoint adjustedPoint = roundedIntPoint(position);
851     Frame& mainframe = m_page->mainFrame();
852
853     mainframe.eventHandler().mouseMoved(PlatformMouseEvent(adjustedPoint, adjustedPoint, NoButton, PlatformEvent::MouseMoved, 0, false, false, false, false, { }, 0, WebCore::NoTap));
854     mainframe.document()->updateStyleIfNeeded();
855 }
856
857 void WebPage::inspectorNodeSearchEndedAtPosition(const FloatPoint& position)
858 {
859     if (Node* node = m_page->mainFrame().deepestNodeAtLocation(position))
860         node->inspect();
861 }
862
863 void WebPage::blurAssistedNode()
864 {
865     if (is<Element>(m_assistedNode.get()))
866         downcast<Element>(*m_assistedNode).blur();
867 }
868
869 void WebPage::setAssistedNodeValue(const String& value)
870 {
871     // FIXME: should also handle the case of HTMLSelectElement.
872     if (is<HTMLInputElement>(m_assistedNode.get()))
873         downcast<HTMLInputElement>(*m_assistedNode).setValue(value, DispatchInputAndChangeEvent);
874 }
875
876 void WebPage::setAssistedNodeValueAsNumber(double value)
877 {
878     if (is<HTMLInputElement>(m_assistedNode.get()))
879         downcast<HTMLInputElement>(*m_assistedNode).setValueAsNumber(value, DispatchInputAndChangeEvent);
880 }
881
882 void WebPage::setAssistedNodeSelectedIndex(uint32_t index, bool allowMultipleSelection)
883 {
884     if (is<HTMLSelectElement>(m_assistedNode.get()))
885         downcast<HTMLSelectElement>(*m_assistedNode).optionSelectedByUser(index, true, allowMultipleSelection);
886 }
887
888 void WebPage::showInspectorHighlight(const WebCore::Highlight& highlight)
889 {
890     send(Messages::WebPageProxy::ShowInspectorHighlight(highlight));
891 }
892
893 void WebPage::hideInspectorHighlight()
894 {
895     send(Messages::WebPageProxy::HideInspectorHighlight());
896 }
897
898 void WebPage::showInspectorIndication()
899 {
900     send(Messages::WebPageProxy::ShowInspectorIndication());
901 }
902
903 void WebPage::hideInspectorIndication()
904 {
905     send(Messages::WebPageProxy::HideInspectorIndication());
906 }
907
908 void WebPage::enableInspectorNodeSearch()
909 {
910     send(Messages::WebPageProxy::EnableInspectorNodeSearch());
911 }
912
913 void WebPage::disableInspectorNodeSearch()
914 {
915     send(Messages::WebPageProxy::DisableInspectorNodeSearch());
916 }
917
918 void WebPage::setForceAlwaysUserScalable(bool userScalable)
919 {
920     m_forceAlwaysUserScalable = userScalable;
921     m_viewportConfiguration.setForceAlwaysUserScalable(userScalable);
922 }
923
924 static FloatQuad innerFrameQuad(const Frame& frame, const Node& assistedNode)
925 {
926     frame.document()->updateLayoutIgnorePendingStylesheets();
927     RenderElement* renderer = nullptr;
928     if (assistedNode.hasTagName(HTMLNames::textareaTag) || assistedNode.hasTagName(HTMLNames::inputTag) || assistedNode.hasTagName(HTMLNames::selectTag))
929         renderer = downcast<RenderElement>(assistedNode.renderer());
930     else if (Element* rootEditableElement = assistedNode.rootEditableElement())
931         renderer = rootEditableElement->renderer();
932     
933     if (!renderer)
934         return FloatQuad();
935
936     auto& style = renderer->style();
937     IntRect boundingBox = renderer->absoluteBoundingBoxRect(true /* use transforms*/);
938
939     boundingBox.move(style.borderLeftWidth(), style.borderTopWidth());
940     boundingBox.setWidth(boundingBox.width() - style.borderLeftWidth() - style.borderRightWidth());
941     boundingBox.setHeight(boundingBox.height() - style.borderBottomWidth() - style.borderTopWidth());
942
943     return FloatQuad(boundingBox);
944 }
945
946 static IntPoint constrainPoint(const IntPoint& point, const Frame& frame, const Node& assistedNode)
947 {
948     ASSERT(&assistedNode.document() == frame.document());
949     const int DEFAULT_CONSTRAIN_INSET = 2;
950     IntRect innerFrame = innerFrameQuad(frame, assistedNode).enclosingBoundingBox();
951     IntPoint constrainedPoint = point;
952
953     int minX = innerFrame.x() + DEFAULT_CONSTRAIN_INSET;
954     int maxX = innerFrame.maxX() - DEFAULT_CONSTRAIN_INSET;
955     int minY = innerFrame.y() + DEFAULT_CONSTRAIN_INSET;
956     int maxY = innerFrame.maxY() - DEFAULT_CONSTRAIN_INSET;
957
958     if (point.x() < minX)
959         constrainedPoint.setX(minX);
960     else if (point.x() > maxX)
961         constrainedPoint.setX(maxX);
962
963     if (point.y() < minY)
964         constrainedPoint.setY(minY);
965     else if (point.y() >= maxY)
966         constrainedPoint.setY(maxY);
967                     
968     return constrainedPoint;
969 }
970
971 static IntRect selectionBoxForRange(WebCore::Range* range)
972 {
973     if (!range)
974         return IntRect();
975     
976     IntRect boundingRect;
977     Vector<SelectionRect> selectionRects;
978     range->collectSelectionRects(selectionRects);
979     unsigned size = selectionRects.size();
980     
981     for (unsigned i = 0; i < size; ++i) {
982         const IntRect &coreRect = selectionRects[i].rect();
983         if (!i)
984             boundingRect = coreRect;
985         else
986             boundingRect.unite(coreRect);
987     }
988     return boundingRect;
989 }
990
991 static bool canShrinkToTextSelection(Node* node)
992 {
993     if (node && !is<Element>(*node))
994         node = node->parentElement();
995     
996     auto* renderer = (node) ? node->renderer() : nullptr;
997     return renderer && renderer->childrenInline() && (is<RenderBlock>(*renderer) && !downcast<RenderBlock>(*renderer).inlineContinuation()) && !renderer->isTable();
998 }
999
1000 static bool hasCustomLineHeight(Node& node)
1001 {
1002     auto* renderer = node.renderer();
1003     return renderer && renderer->style().lineHeight().isSpecified();
1004 }
1005     
1006 RefPtr<Range> WebPage::rangeForWebSelectionAtPosition(const IntPoint& point, const VisiblePosition& position, SelectionFlags& flags)
1007 {
1008     HitTestResult result = m_page->mainFrame().eventHandler().hitTestResultAtPoint((point), HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::DisallowUserAgentShadowContent | HitTestRequest::AllowChildFrameContent);
1009
1010     Node* currentNode = result.innerNode();
1011     if (!currentNode)
1012         return nullptr;
1013     RefPtr<Range> range;
1014     FloatRect boundingRectInScrollViewCoordinates;
1015
1016     if (!currentNode->isTextNode() && !canShrinkToTextSelection(currentNode) && hasCustomLineHeight(*currentNode)) {
1017         auto* renderer = currentNode->renderer();
1018         if (is<RenderBlockFlow>(renderer)) {
1019             auto* renderText = downcast<RenderBlockFlow>(*renderer).findClosestTextAtAbsolutePoint(point);
1020             if (renderText && renderText->textNode())
1021                 currentNode = renderText->textNode();
1022         }
1023     }
1024
1025     if (currentNode->isTextNode()) {
1026         range = enclosingTextUnitOfGranularity(position, ParagraphGranularity, DirectionForward);
1027         if (!range || range->collapsed())
1028             range = Range::create(currentNode->document(), position, position);
1029         else {
1030             m_blockRectForTextSelection = selectionBoxForRange(range.get());
1031             range = wordRangeFromPosition(position);
1032         }
1033
1034         return range;
1035     }
1036
1037     if (!currentNode->isElementNode())
1038         currentNode = currentNode->parentElement();
1039
1040     Node* bestChoice = currentNode;
1041     while (currentNode) {
1042         if (currentNode->renderer()) {
1043             boundingRectInScrollViewCoordinates = currentNode->renderer()->absoluteBoundingBoxRect(true);
1044             boundingRectInScrollViewCoordinates.scale(m_page->pageScaleFactor());
1045             if (boundingRectInScrollViewCoordinates.width() > m_blockSelectionDesiredSize.width() && boundingRectInScrollViewCoordinates.height() > m_blockSelectionDesiredSize.height())
1046                 break;
1047             bestChoice = currentNode;
1048         }
1049         currentNode = currentNode->parentElement();
1050     }
1051
1052     if (!bestChoice)
1053         return nullptr;
1054
1055     RenderObject* renderer = bestChoice->renderer();
1056     if (!renderer || renderer->style().userSelect() == SELECT_NONE)
1057         return nullptr;
1058
1059     if (renderer->childrenInline() && (is<RenderBlock>(*renderer) && !downcast<RenderBlock>(*renderer).inlineContinuation()) && !renderer->isTable()) {
1060         range = enclosingTextUnitOfGranularity(position, WordGranularity, DirectionBackward);
1061         if (range && !range->collapsed())
1062             return range;
1063     }
1064
1065     // If all we could find is a block whose height is very close to the height
1066     // of the visible area, don't use it.
1067     const float adjustmentFactor = .97;
1068     boundingRectInScrollViewCoordinates = renderer->absoluteBoundingBoxRect(true);
1069
1070     if (boundingRectInScrollViewCoordinates.height() > m_page->mainFrame().view()->exposedContentRect().height() * adjustmentFactor)
1071         return nullptr;
1072
1073     range = Range::create(bestChoice->document());
1074     range->selectNodeContents(*bestChoice);
1075     return range->collapsed() ? nullptr : range;
1076 }
1077
1078 void WebPage::selectWithGesture(const IntPoint& point, uint32_t granularity, uint32_t gestureType, uint32_t gestureState, bool isInteractingWithAssistedNode, CallbackID callbackID)
1079 {
1080     auto& frame = m_page->focusController().focusedOrMainFrame();
1081     VisiblePosition position = visiblePositionInFocusedNodeForPoint(frame, point, isInteractingWithAssistedNode);
1082
1083     if (position.isNull()) {
1084         send(Messages::WebPageProxy::GestureCallback(point, gestureType, gestureState, 0, callbackID));
1085         return;
1086     }
1087     RefPtr<Range> range;
1088     SelectionFlags flags = None;
1089     GestureRecognizerState wkGestureState = static_cast<GestureRecognizerState>(gestureState);
1090     switch (static_cast<GestureType>(gestureType)) {
1091     case GestureType::PhraseBoundary:
1092     {
1093         if (!frame.editor().hasComposition())
1094             break;
1095         RefPtr<Range> markedRange = frame.editor().compositionRange();
1096         if (position < markedRange->startPosition())
1097             position = markedRange->startPosition();
1098         if (position > markedRange->endPosition())
1099             position = markedRange->endPosition();
1100         if (wkGestureState != GestureRecognizerState::Began)
1101             flags = distanceBetweenPositions(markedRange->startPosition(), frame.selection().selection().start()) != distanceBetweenPositions(markedRange->startPosition(), position) ? PhraseBoundaryChanged : None;
1102         else
1103             flags = PhraseBoundaryChanged;
1104         range = Range::create(*frame.document(), position, position);
1105     }
1106         break;
1107
1108     case GestureType::OneFingerTap:
1109     {
1110         VisiblePosition result;
1111         // move the position at the end of the word
1112         if (atBoundaryOfGranularity(position, LineGranularity, DirectionForward)) {
1113             // Don't cross line boundaries.
1114             result = position;
1115         } else if (withinTextUnitOfGranularity(position, WordGranularity, DirectionForward)) {
1116             // The position lies within a word.
1117             RefPtr<Range> wordRange = enclosingTextUnitOfGranularity(position, WordGranularity, DirectionForward);
1118             if (wordRange) {
1119                 result = wordRange->startPosition();
1120                 if (distanceBetweenPositions(position, result) > 1)
1121                     result = wordRange->endPosition();
1122             }
1123         } else if (atBoundaryOfGranularity(position, WordGranularity, DirectionBackward)) {
1124             // The position is at the end of a word.
1125             result = position;
1126         } else {
1127             // The position is not within a word.
1128             // Go to the next boundary.
1129             result = positionOfNextBoundaryOfGranularity(position, WordGranularity, DirectionForward);
1130
1131             // If there is no such boundary we go to the end of the element.
1132             if (result.isNull())
1133                 result = endOfEditableContent(position);
1134         }
1135         if (result.isNotNull())
1136             range = Range::create(*frame.document(), result, result);
1137     }
1138         break;
1139
1140     case GestureType::Loupe:
1141         if (position.rootEditableElement())
1142             range = Range::create(*frame.document(), position, position);
1143         else
1144             range = wordRangeFromPosition(position);
1145         break;
1146
1147     case GestureType::TapAndAHalf:
1148         switch (wkGestureState) {
1149         case GestureRecognizerState::Began:
1150             range = wordRangeFromPosition(position);
1151             m_currentWordRange = range ? RefPtr<Range>(Range::create(*frame.document(), range->startPosition(), range->endPosition())) : nullptr;
1152             break;
1153         case GestureRecognizerState::Changed:
1154             if (!m_currentWordRange)
1155                 break;
1156             range = Range::create(*frame.document(), m_currentWordRange->startPosition(), m_currentWordRange->endPosition());
1157             if (position < range->startPosition())
1158                 range->setStart(position.deepEquivalent());
1159             if (position > range->endPosition())
1160                 range->setEnd(position.deepEquivalent());
1161             break;
1162         case GestureRecognizerState::Ended:
1163         case GestureRecognizerState::Cancelled:
1164             m_currentWordRange = nullptr;
1165             break;
1166         case GestureRecognizerState::Failed:
1167         case GestureRecognizerState::Possible:
1168             ASSERT_NOT_REACHED();
1169         }
1170         break;
1171
1172     case GestureType::OneFingerDoubleTap:
1173         if (atBoundaryOfGranularity(position, LineGranularity, DirectionForward)) {
1174             // Double-tap at end of line only places insertion point there.
1175             // This helps to get the callout for pasting at ends of lines,
1176             // paragraphs, and documents.
1177             range = Range::create(*frame.document(), position, position);
1178          } else
1179             range = wordRangeFromPosition(position);
1180         break;
1181
1182     case GestureType::TwoFingerSingleTap:
1183         // Single tap with two fingers selects the entire paragraph.
1184         range = enclosingTextUnitOfGranularity(position, ParagraphGranularity, DirectionForward);
1185         break;
1186
1187     case GestureType::OneFingerTripleTap:
1188         if (atBoundaryOfGranularity(position, LineGranularity, DirectionForward)) {
1189             // Triple-tap at end of line only places insertion point there.
1190             // This helps to get the callout for pasting at ends of lines,
1191             // paragraphs, and documents.
1192             range = Range::create(*frame.document(), position, position);
1193         } else
1194             range = enclosingTextUnitOfGranularity(position, ParagraphGranularity, DirectionForward);
1195         break;
1196
1197     case GestureType::MakeWebSelection:
1198         if (wkGestureState == GestureRecognizerState::Began) {
1199             m_blockSelectionDesiredSize.setWidth(blockSelectionStartWidth);
1200             m_blockSelectionDesiredSize.setHeight(blockSelectionStartHeight);
1201             m_currentBlockSelection = nullptr;
1202         }
1203         range = rangeForWebSelectionAtPosition(point, position, flags);
1204         break;
1205
1206     default:
1207         break;
1208     }
1209     if (range)
1210         frame.selection().setSelectedRange(range.get(), position.affinity(), true, UserTriggered);
1211
1212     send(Messages::WebPageProxy::GestureCallback(point, gestureType, gestureState, static_cast<uint32_t>(flags), callbackID));
1213 }
1214
1215 static RefPtr<Range> rangeForPosition(Frame* frame, const VisiblePosition& position, bool baseIsStart)
1216 {
1217     RefPtr<Range> range;
1218     VisiblePosition result = position;
1219
1220     if (baseIsStart) {
1221         VisiblePosition selectionStart = frame->selection().selection().visibleStart();
1222         bool wouldFlip = position <= selectionStart;
1223
1224         if (wouldFlip)
1225             result = selectionStart.next();
1226
1227         if (result.isNotNull())
1228             range = Range::create(*frame->document(), selectionStart, result);
1229     } else {
1230         VisiblePosition selectionEnd = frame->selection().selection().visibleEnd();
1231         bool wouldFlip = position >= selectionEnd;
1232
1233         if (wouldFlip)
1234             result = selectionEnd.previous();
1235
1236         if (result.isNotNull())
1237             range = Range::create(*frame->document(), result, selectionEnd);
1238     }
1239
1240     return range;
1241 }
1242
1243 static RefPtr<Range> rangeAtWordBoundaryForPosition(Frame* frame, const VisiblePosition& position, bool baseIsStart, SelectionDirection direction)
1244 {
1245     SelectionDirection sameDirection = baseIsStart ? DirectionForward : DirectionBackward;
1246     SelectionDirection oppositeDirection = baseIsStart ? DirectionBackward : DirectionForward;
1247     VisiblePosition base = baseIsStart ? frame->selection().selection().visibleStart() : frame->selection().selection().visibleEnd();
1248     VisiblePosition extent = baseIsStart ? frame->selection().selection().visibleEnd() : frame->selection().selection().visibleStart();
1249     VisiblePosition initialExtent = position;
1250
1251     if (atBoundaryOfGranularity(extent, WordGranularity, sameDirection)) {
1252         // This is a word boundary. Leave selection where it is.
1253         return nullptr;
1254     }
1255
1256     if (atBoundaryOfGranularity(extent, WordGranularity, oppositeDirection)) {
1257         // This is a word boundary in the wrong direction. Nudge the selection to a character before proceeding.
1258         extent = baseIsStart ? extent.previous() : extent.next();
1259     }
1260
1261     // Extend to the boundary of the word.
1262
1263     VisiblePosition wordBoundary = positionOfNextBoundaryOfGranularity(extent, WordGranularity, sameDirection);
1264     if (wordBoundary.isNotNull()
1265         && atBoundaryOfGranularity(wordBoundary, WordGranularity, sameDirection)
1266         && initialExtent != wordBoundary) {
1267         extent = wordBoundary;
1268         return (base < extent) ? Range::create(*frame->document(), base, extent) : Range::create(*frame->document(), extent, base);
1269     }
1270     // Conversely, if the initial extent equals the current word boundary, then
1271     // run the rest of this function to see if the selection should extend
1272     // the other direction to the other word.
1273
1274     // If this is where the extent was initially, then iterate in the other direction in the document until we hit the next word.
1275     while (extent.isNotNull()
1276         && !atBoundaryOfGranularity(extent, WordGranularity, sameDirection)
1277         && extent != base
1278         && !atBoundaryOfGranularity(extent, LineGranularity, sameDirection)
1279         && !atBoundaryOfGranularity(extent, LineGranularity, oppositeDirection)) {
1280         extent = baseIsStart ? extent.next() : extent.previous();
1281     }
1282
1283     // Don't let the smart extension make the extent equal the base.
1284     // Expand out to word boundary.
1285     if (extent.isNull() || extent == base)
1286         extent = wordBoundary;
1287     if (extent.isNull())
1288         return nullptr;
1289
1290     return (base < extent) ? Range::create(*frame->document(), base, extent) : Range::create(*frame->document(), extent, base);
1291 }
1292
1293 void WebPage::clearSelection(){
1294     m_currentBlockSelection = nullptr;
1295     m_page->focusController().focusedOrMainFrame().selection().clear();
1296 }
1297
1298 void WebPage::updateSelectionWithTouches(const IntPoint& point, uint32_t touches, bool baseIsStart, CallbackID callbackID)
1299 {
1300     Frame& frame = m_page->focusController().focusedOrMainFrame();
1301     IntPoint pointInDocument = frame.view()->rootViewToContents(point);
1302     VisiblePosition position = frame.visiblePositionForPoint(pointInDocument);
1303     if (position.isNull()) {
1304         send(Messages::WebPageProxy::TouchesCallback(point, touches, 0, callbackID));
1305         return;
1306     }
1307
1308     RefPtr<Range> range;
1309     VisiblePosition result;
1310     SelectionFlags flags = None;
1311
1312     switch (static_cast<SelectionTouch>(touches)) {
1313     case SelectionTouch::Started:
1314     case SelectionTouch::EndedNotMoving:
1315         break;
1316     
1317     case SelectionTouch::Ended:
1318         if (frame.selection().selection().isContentEditable()) {
1319             result = closestWordBoundaryForPosition(position);
1320             if (result.isNotNull())
1321                 range = Range::create(*frame.document(), result, result);
1322         } else
1323             range = rangeForPosition(&frame, position, baseIsStart);
1324         break;
1325
1326     case SelectionTouch::EndedMovingForward:
1327         range = rangeAtWordBoundaryForPosition(&frame, position, baseIsStart, DirectionForward);
1328         break;
1329         
1330     case SelectionTouch::EndedMovingBackward:
1331         range = rangeAtWordBoundaryForPosition(&frame, position, baseIsStart, DirectionBackward);
1332         break;
1333
1334     case SelectionTouch::Moved:
1335         range = rangeForPosition(&frame, position, baseIsStart);
1336         break;
1337     }
1338     if (range)
1339         frame.selection().setSelectedRange(range.get(), position.affinity(), true, UserTriggered);
1340
1341     send(Messages::WebPageProxy::TouchesCallback(point, touches, flags, callbackID));
1342 }
1343
1344 void WebPage::selectWithTwoTouches(const WebCore::IntPoint& from, const WebCore::IntPoint& to, uint32_t gestureType, uint32_t gestureState, CallbackID callbackID)
1345 {
1346     Frame& frame = m_page->focusController().focusedOrMainFrame();
1347     VisiblePosition fromPosition = frame.visiblePositionForPoint(frame.view()->rootViewToContents(from));
1348     VisiblePosition toPosition = frame.visiblePositionForPoint(frame.view()->rootViewToContents(to));
1349     RefPtr<Range> range;
1350     if (fromPosition.isNotNull() && toPosition.isNotNull()) {
1351         if (fromPosition < toPosition)
1352             range = Range::create(*frame.document(), fromPosition, toPosition);
1353         else
1354             range = Range::create(*frame.document(), toPosition, fromPosition);
1355         frame.selection().setSelectedRange(range.get(), fromPosition.affinity(), true, UserTriggered);
1356     }
1357
1358     // We can use the same callback for the gestures with one point.
1359     send(Messages::WebPageProxy::GestureCallback(from, gestureType, gestureState, 0, callbackID));
1360 }
1361
1362 void WebPage::extendSelection(uint32_t granularity)
1363 {
1364     Frame& frame = m_page->focusController().focusedOrMainFrame();
1365     // For the moment we handle only WordGranularity.
1366     if (granularity != WordGranularity || !frame.selection().isCaret())
1367         return;
1368
1369     VisiblePosition position = frame.selection().selection().start();
1370     frame.selection().setSelectedRange(wordRangeFromPosition(position).get(), position.affinity(), true, UserTriggered);
1371 }
1372
1373 void WebPage::selectWordBackward()
1374 {
1375     Frame& frame = m_page->focusController().focusedOrMainFrame();
1376     if (!frame.selection().isCaret())
1377         return;
1378
1379     VisiblePosition position = frame.selection().selection().start();
1380     VisiblePosition startPosition = positionOfNextBoundaryOfGranularity(position, WordGranularity, DirectionBackward);
1381     if (startPosition.isNotNull() && startPosition != position)
1382         frame.selection().setSelectedRange(Range::create(*frame.document(), startPosition, position).ptr(), position.affinity(), true, UserTriggered);
1383 }
1384
1385 void WebPage::moveSelectionByOffset(int32_t offset, CallbackID callbackID)
1386 {
1387     Frame& frame = m_page->focusController().focusedOrMainFrame();
1388     
1389     VisiblePosition startPosition = frame.selection().selection().end();
1390     if (startPosition.isNull())
1391         return;
1392     SelectionDirection direction = offset < 0 ? DirectionBackward : DirectionForward;
1393     VisiblePosition position = startPosition;
1394     for (int i = 0; i < abs(offset); ++i) {
1395         position = positionOfNextBoundaryOfGranularity(position, CharacterGranularity, direction);
1396         if (position.isNull())
1397             break;
1398     }
1399     if (position.isNotNull() && startPosition != position)
1400         frame.selection().setSelectedRange(Range::create(*frame.document(), position, position).ptr(), position.affinity(), true, UserTriggered);
1401     send(Messages::WebPageProxy::VoidCallback(callbackID));
1402 }
1403     
1404 void WebPage::startAutoscrollAtPosition(const WebCore::FloatPoint& positionInWindow)
1405 {
1406     if (m_assistedNode && m_assistedNode->renderer())
1407         m_page->mainFrame().eventHandler().startSelectionAutoscroll(m_assistedNode->renderer(), positionInWindow);
1408     else {
1409         Frame& frame = m_page->focusController().focusedOrMainFrame();
1410         VisibleSelection selection = frame.selection().selection();
1411         if (selection.isRange()) {
1412             RefPtr<Range> range = frame.selection().toNormalizedRange();
1413             Node& node = range->startContainer();
1414             auto* renderer = node.renderer();
1415             if (renderer)
1416                 m_page->mainFrame().eventHandler().startSelectionAutoscroll(renderer, positionInWindow);
1417         }
1418     }
1419 }
1420     
1421 void WebPage::cancelAutoscroll()
1422 {
1423     m_page->mainFrame().eventHandler().cancelSelectionAutoscroll();
1424 }
1425
1426 void WebPage::getRectsForGranularityWithSelectionOffset(uint32_t granularity, int32_t offset, CallbackID callbackID)
1427 {
1428     Frame& frame = m_page->focusController().focusedOrMainFrame();
1429     VisibleSelection selection = m_storedSelectionForAccessibility.isNone() ? frame.selection().selection() : m_storedSelectionForAccessibility;
1430     VisiblePosition selectionStart = selection.visibleStart();
1431
1432     if (selectionStart.isNull()) {
1433         send(Messages::WebPageProxy::SelectionRectsCallback({ }, callbackID));
1434         return;
1435     }
1436
1437     auto position = visiblePositionForPositionWithOffset(selectionStart, offset);
1438     SelectionDirection direction = offset < 0 ? DirectionBackward : DirectionForward;
1439
1440     auto range = enclosingTextUnitOfGranularity(position, static_cast<WebCore::TextGranularity>(granularity), direction);
1441     if (!range || range->collapsed()) {
1442         send(Messages::WebPageProxy::SelectionRectsCallback({ }, callbackID));
1443         return;
1444     }
1445
1446     Vector<WebCore::SelectionRect> selectionRects;
1447     range->collectSelectionRectsWithoutUnionInteriorLines(selectionRects);
1448     convertSelectionRectsToRootView(frame.view(), selectionRects);
1449     send(Messages::WebPageProxy::SelectionRectsCallback(selectionRects, callbackID));
1450 }
1451
1452 void WebPage::storeSelectionForAccessibility(bool shouldStore)
1453 {
1454     if (!shouldStore)
1455         m_storedSelectionForAccessibility = VisibleSelection();
1456     else {
1457         Frame& frame = m_page->focusController().focusedOrMainFrame();
1458         m_storedSelectionForAccessibility = frame.selection().selection();
1459     }
1460 }
1461
1462 static RefPtr<Range> rangeNearPositionMatchesText(const VisiblePosition& position, RefPtr<Range> originalRange, const String& matchText, RefPtr<Range> selectionRange)
1463 {
1464     auto range = Range::create(selectionRange->ownerDocument(), selectionRange->startPosition(), position.deepEquivalent().parentAnchoredEquivalent());
1465     unsigned targetOffset = TextIterator::rangeLength(range.ptr(), true);
1466     return findClosestPlainText(*selectionRange.get(), matchText, { }, targetOffset);
1467 }
1468
1469 void WebPage::getRectsAtSelectionOffsetWithText(int32_t offset, const String& text, CallbackID callbackID)
1470 {
1471     Frame& frame = m_page->focusController().focusedOrMainFrame();
1472     uint32_t length = text.length();
1473     VisibleSelection selection = m_storedSelectionForAccessibility.isNone() ? frame.selection().selection() : m_storedSelectionForAccessibility;
1474     VisiblePosition selectionStart = selection.visibleStart();
1475     VisiblePosition selectionEnd = selection.visibleEnd();
1476
1477     if (selectionStart.isNull() || selectionEnd.isNull()) {
1478         send(Messages::WebPageProxy::SelectionRectsCallback({ }, callbackID));
1479         return;
1480     }
1481
1482     auto startPosition = visiblePositionForPositionWithOffset(selectionStart, offset);
1483     auto endPosition = visiblePositionForPositionWithOffset(startPosition, length);
1484     auto range = Range::create(*frame.document(), startPosition, endPosition);
1485
1486     if (range->collapsed()) {
1487         send(Messages::WebPageProxy::SelectionRectsCallback({ }, callbackID));
1488         return;
1489     }
1490
1491     String rangeText = plainTextReplacingNoBreakSpace(range.ptr(), TextIteratorDefaultBehavior, true);
1492     if (rangeText != text) {
1493         auto selectionRange = selection.toNormalizedRange();
1494         // Try to search for a range which is the closest to the position within the selection range that matches the passed in text.
1495         if (auto wordRange = rangeNearPositionMatchesText(startPosition, range.ptr(), text, selectionRange)) {
1496             if (!wordRange->collapsed())
1497                 range = *wordRange;
1498         }
1499     }
1500
1501     Vector<WebCore::SelectionRect> selectionRects;
1502     range->collectSelectionRectsWithoutUnionInteriorLines(selectionRects);
1503     convertSelectionRectsToRootView(frame.view(), selectionRects);
1504     send(Messages::WebPageProxy::SelectionRectsCallback(selectionRects, callbackID));
1505 }
1506
1507 VisiblePosition WebPage::visiblePositionInFocusedNodeForPoint(const Frame& frame, const IntPoint& point, bool isInteractingWithAssistedNode)
1508 {
1509     IntPoint adjustedPoint(frame.view()->rootViewToContents(point));
1510     IntPoint constrainedPoint = m_assistedNode && isInteractingWithAssistedNode ? constrainPoint(adjustedPoint, frame, *m_assistedNode) : adjustedPoint;
1511     return frame.visiblePositionForPoint(constrainedPoint);
1512 }
1513
1514 void WebPage::selectPositionAtPoint(const WebCore::IntPoint& point, bool isInteractingWithAssistedNode, CallbackID callbackID)
1515 {
1516     auto& frame = m_page->focusController().focusedOrMainFrame();
1517     VisiblePosition position = visiblePositionInFocusedNodeForPoint(frame, point, isInteractingWithAssistedNode);
1518     
1519     if (position.isNotNull())
1520         frame.selection().setSelectedRange(Range::create(*frame.document(), position, position).ptr(), position.affinity(), true, UserTriggered);
1521     send(Messages::WebPageProxy::VoidCallback(callbackID));
1522 }
1523
1524 void WebPage::selectPositionAtBoundaryWithDirection(const WebCore::IntPoint& point, uint32_t granularity, uint32_t direction, bool isInteractingWithAssistedNode, CallbackID callbackID)
1525 {
1526     auto& frame = m_page->focusController().focusedOrMainFrame();
1527     VisiblePosition position = visiblePositionInFocusedNodeForPoint(frame, point, isInteractingWithAssistedNode);
1528
1529     if (position.isNotNull()) {
1530         position = positionOfNextBoundaryOfGranularity(position, static_cast<WebCore::TextGranularity>(granularity), static_cast<SelectionDirection>(direction));
1531         if (position.isNotNull())
1532             frame.selection().setSelectedRange(Range::create(*frame.document(), position, position).ptr(), UPSTREAM, true, UserTriggered);
1533     }
1534     send(Messages::WebPageProxy::VoidCallback(callbackID));
1535 }
1536
1537 void WebPage::moveSelectionAtBoundaryWithDirection(uint32_t granularity, uint32_t direction, CallbackID callbackID)
1538 {
1539     Frame& frame = m_page->focusController().focusedOrMainFrame();
1540     
1541     if (!frame.selection().selection().isNone()) {
1542         bool isForward = (direction == DirectionForward || direction == DirectionRight);
1543         VisiblePosition position = (isForward) ? frame.selection().selection().visibleEnd() : frame.selection().selection().visibleStart();
1544         position = positionOfNextBoundaryOfGranularity(position, static_cast<WebCore::TextGranularity>(granularity), static_cast<SelectionDirection>(direction));
1545         if (position.isNotNull())
1546             frame.selection().setSelectedRange(Range::create(*frame.document(), position, position).ptr(), isForward? UPSTREAM : DOWNSTREAM, true, UserTriggered);
1547     }
1548     send(Messages::WebPageProxy::VoidCallback(callbackID));
1549 }
1550
1551 RefPtr<Range> WebPage::rangeForGranularityAtPoint(Frame& frame, const WebCore::IntPoint& point, uint32_t granularity, bool isInteractingWithAssistedNode)
1552 {
1553     VisiblePosition position = visiblePositionInFocusedNodeForPoint(frame, point, isInteractingWithAssistedNode);
1554
1555     RefPtr<Range> range;
1556     switch (static_cast<WebCore::TextGranularity>(granularity)) {
1557     case WordGranularity:
1558         range = wordRangeFromPosition(position);
1559         break;
1560     case SentenceGranularity:
1561         range = enclosingTextUnitOfGranularity(position, SentenceGranularity, DirectionForward);
1562         break;
1563     case ParagraphGranularity:
1564         range = enclosingTextUnitOfGranularity(position, ParagraphGranularity, DirectionForward);
1565         break;
1566     case DocumentGranularity:
1567         frame.selection().selectAll();
1568         break;
1569     default:
1570         break;
1571     }
1572     return range;
1573 }
1574
1575 static inline bool rectIsTooBigForSelection(const IntRect& blockRect, const Frame& frame)
1576 {
1577     const float factor = 0.97;
1578     return blockRect.height() > frame.view()->unobscuredContentRect().height() * factor;
1579 }
1580
1581 void WebPage::selectTextWithGranularityAtPoint(const WebCore::IntPoint& point, uint32_t granularity, bool isInteractingWithAssistedNode, CallbackID callbackID)
1582 {
1583     auto& frame = m_page->focusController().focusedOrMainFrame();
1584     RefPtr<Range> range = rangeForGranularityAtPoint(frame, point, granularity, isInteractingWithAssistedNode);
1585     if (!isInteractingWithAssistedNode) {
1586         m_blockSelectionDesiredSize.setWidth(blockSelectionStartWidth);
1587         m_blockSelectionDesiredSize.setHeight(blockSelectionStartHeight);
1588         m_currentBlockSelection = nullptr;
1589         auto* renderer = range ? range->startContainer().renderer() : nullptr;
1590         if (renderer && renderer->style().preserveNewline())
1591             m_blockRectForTextSelection = renderer->absoluteBoundingBoxRect(true);
1592         else {
1593             auto paragraphRange = enclosingTextUnitOfGranularity(visiblePositionInFocusedNodeForPoint(frame, point, isInteractingWithAssistedNode), ParagraphGranularity, DirectionForward);
1594             if (paragraphRange && !paragraphRange->collapsed())
1595                 m_blockRectForTextSelection = selectionBoxForRange(paragraphRange.get());
1596         }
1597         
1598         if (rectIsTooBigForSelection(m_blockRectForTextSelection, frame))
1599             m_blockRectForTextSelection.setHeight(0);
1600     }
1601
1602     if (range)
1603         frame.selection().setSelectedRange(range.get(), UPSTREAM, true, UserTriggered);
1604     m_initialSelection = range;
1605     send(Messages::WebPageProxy::VoidCallback(callbackID));
1606 }
1607
1608 void WebPage::beginSelectionInDirection(uint32_t direction, CallbackID callbackID)
1609 {
1610     m_selectionAnchor = (static_cast<SelectionDirection>(direction) == DirectionLeft) ? Start : End;
1611     send(Messages::WebPageProxy::UnsignedCallback(m_selectionAnchor == Start, callbackID));
1612 }
1613
1614 void WebPage::updateSelectionWithExtentPointAndBoundary(const WebCore::IntPoint& point, uint32_t granularity, bool isInteractingWithAssistedNode, CallbackID callbackID)
1615 {
1616     auto& frame = m_page->focusController().focusedOrMainFrame();
1617     VisiblePosition position = visiblePositionInFocusedNodeForPoint(frame, point, isInteractingWithAssistedNode);
1618     RefPtr<Range> newRange = rangeForGranularityAtPoint(frame, point, granularity, isInteractingWithAssistedNode);
1619     
1620     if (position.isNull() || !m_initialSelection || !newRange) {
1621         send(Messages::WebPageProxy::UnsignedCallback(false, callbackID));
1622         return;
1623     }
1624     
1625     RefPtr<Range> range;
1626     VisiblePosition selectionStart = m_initialSelection->startPosition();
1627     VisiblePosition selectionEnd = m_initialSelection->endPosition();
1628
1629     if (position > m_initialSelection->endPosition())
1630         selectionEnd = newRange->endPosition();
1631     else if (position < m_initialSelection->startPosition())
1632         selectionStart = newRange->startPosition();
1633     
1634     if (selectionStart.isNotNull() && selectionEnd.isNotNull())
1635         range = Range::create(*frame.document(), selectionStart, selectionEnd);
1636     
1637     if (range)
1638         frame.selection().setSelectedRange(range.get(), UPSTREAM, true, UserTriggered);
1639     
1640     send(Messages::WebPageProxy::UnsignedCallback(selectionStart == m_initialSelection->startPosition(), callbackID));
1641 }
1642
1643 void WebPage::updateSelectionWithExtentPoint(const WebCore::IntPoint& point, bool isInteractingWithAssistedNode, CallbackID callbackID)
1644 {
1645     auto& frame = m_page->focusController().focusedOrMainFrame();
1646     VisiblePosition position = visiblePositionInFocusedNodeForPoint(frame, point, isInteractingWithAssistedNode);
1647
1648     if (position.isNull()) {
1649         send(Messages::WebPageProxy::UnsignedCallback(false, callbackID));
1650         return;
1651     }
1652
1653     RefPtr<Range> range;
1654     VisiblePosition selectionStart;
1655     VisiblePosition selectionEnd;
1656     
1657     if (m_selectionAnchor == Start) {
1658         selectionStart = frame.selection().selection().visibleStart();
1659         selectionEnd = position;
1660
1661         if (position <= selectionStart) {
1662             selectionStart = selectionStart.previous();
1663             selectionEnd = frame.selection().selection().visibleEnd();
1664             m_selectionAnchor = End;
1665         }
1666     } else {
1667         selectionStart = position;
1668         selectionEnd = frame.selection().selection().visibleEnd();
1669         
1670         if (position >= selectionEnd) {
1671             selectionStart = frame.selection().selection().visibleStart();
1672             selectionEnd = selectionEnd.next();
1673             m_selectionAnchor = Start;
1674         }
1675     }
1676     
1677     if (selectionStart.isNotNull() && selectionEnd.isNotNull())
1678         range = Range::create(*frame.document(), selectionStart, selectionEnd);
1679
1680     if (range)
1681         frame.selection().setSelectedRange(range.get(), UPSTREAM, true, UserTriggered);
1682
1683     send(Messages::WebPageProxy::UnsignedCallback(m_selectionAnchor == Start, callbackID));
1684 }
1685
1686 void WebPage::convertSelectionRectsToRootView(FrameView* view, Vector<SelectionRect>& selectionRects)
1687 {
1688     for (size_t i = 0; i < selectionRects.size(); ++i) {
1689         SelectionRect& currentRect = selectionRects[i];
1690         currentRect.setRect(view->contentsToRootView(currentRect.rect()));
1691     }
1692 }
1693
1694 void WebPage::requestDictationContext(CallbackID callbackID)
1695 {
1696     Frame& frame = m_page->focusController().focusedOrMainFrame();
1697     VisiblePosition startPosition = frame.selection().selection().start();
1698     VisiblePosition endPosition = frame.selection().selection().end();
1699     const unsigned dictationContextWordCount = 5;
1700
1701     String selectedText;
1702     if (frame.selection().isRange())
1703         selectedText = plainTextReplacingNoBreakSpace(frame.selection().selection().toNormalizedRange().get());
1704
1705     String contextBefore;
1706     if (startPosition != startOfEditableContent(startPosition)) {
1707         VisiblePosition currentPosition = startPosition;
1708         VisiblePosition lastPosition = startPosition;
1709         for (unsigned i = 0; i < dictationContextWordCount; ++i) {
1710             currentPosition = startOfWord(positionOfNextBoundaryOfGranularity(lastPosition, WordGranularity, DirectionBackward));
1711             if (currentPosition.isNull())
1712                 break;
1713             lastPosition = currentPosition;
1714         }
1715         if (lastPosition.isNotNull() && lastPosition != startPosition)
1716             contextBefore = plainTextReplacingNoBreakSpace(Range::create(*frame.document(), lastPosition, startPosition).ptr());
1717     }
1718
1719     String contextAfter;
1720     if (endPosition != endOfEditableContent(endPosition)) {
1721         VisiblePosition currentPosition = endPosition;
1722         VisiblePosition lastPosition = endPosition;
1723         for (unsigned i = 0; i < dictationContextWordCount; ++i) {
1724             currentPosition = endOfWord(positionOfNextBoundaryOfGranularity(lastPosition, WordGranularity, DirectionForward));
1725             if (currentPosition.isNull())
1726                 break;
1727             lastPosition = currentPosition;
1728         }
1729         if (lastPosition.isNotNull() && lastPosition != endPosition)
1730             contextAfter = plainTextReplacingNoBreakSpace(Range::create(*frame.document(), endPosition, lastPosition).ptr());
1731     }
1732
1733     send(Messages::WebPageProxy::SelectionContextCallback(selectedText, contextBefore, contextAfter, callbackID));
1734 }
1735
1736 void WebPage::replaceSelectedText(const String& oldText, const String& newText)
1737 {
1738     Frame& frame = m_page->focusController().focusedOrMainFrame();
1739     RefPtr<Range> wordRange = frame.selection().isCaret() ? wordRangeFromPosition(frame.selection().selection().start()) : frame.selection().toNormalizedRange();
1740     if (plainTextReplacingNoBreakSpace(wordRange.get()) != oldText)
1741         return;
1742     
1743     frame.editor().setIgnoreSelectionChanges(true);
1744     frame.selection().setSelectedRange(wordRange.get(), UPSTREAM, true);
1745     frame.editor().insertText(newText, 0);
1746     frame.editor().setIgnoreSelectionChanges(false);
1747 }
1748
1749 void WebPage::replaceDictatedText(const String& oldText, const String& newText)
1750 {
1751     Frame& frame = m_page->focusController().focusedOrMainFrame();
1752     if (frame.selection().isNone())
1753         return;
1754     
1755     if (frame.selection().isRange()) {
1756         frame.editor().deleteSelectionWithSmartDelete(false);
1757         return;
1758     }
1759     VisiblePosition position = frame.selection().selection().start();
1760     for (size_t i = 0; i < oldText.length(); ++i)
1761         position = position.previous();
1762     if (position.isNull())
1763         position = startOfDocument(static_cast<Node*>(frame.document()->documentElement()));
1764     RefPtr<Range> range = Range::create(*frame.document(), position, frame.selection().selection().start());
1765
1766     if (plainTextReplacingNoBreakSpace(range.get()) != oldText)
1767         return;
1768
1769     // We don't want to notify the client that the selection has changed until we are done inserting the new text.
1770     frame.editor().setIgnoreSelectionChanges(true);
1771     frame.selection().setSelectedRange(range.get(), UPSTREAM, true);
1772     frame.editor().insertText(newText, 0);
1773     frame.editor().setIgnoreSelectionChanges(false);
1774 }
1775
1776 void WebPage::requestAutocorrectionData(const String& textForAutocorrection, CallbackID callbackID)
1777 {
1778     Frame& frame = m_page->focusController().focusedOrMainFrame();
1779     if (!frame.selection().isCaret()) {
1780         send(Messages::WebPageProxy::AutocorrectionDataCallback(Vector<FloatRect>(), String(), 0, 0, callbackID));
1781         return;
1782     }
1783
1784     VisiblePosition position = frame.selection().selection().start();
1785     RefPtr<Range> range = wordRangeFromPosition(position);
1786     if (!range) {
1787         send(Messages::WebPageProxy::AutocorrectionDataCallback(Vector<FloatRect>(), String(), 0, 0, callbackID));
1788         return;
1789     }
1790
1791     String textForRange = plainTextReplacingNoBreakSpace(range.get());
1792     const unsigned maxSearchAttempts = 5;
1793     for (size_t i = 0;  i < maxSearchAttempts && textForRange != textForAutocorrection; ++i)
1794     {
1795         position = range->startPosition().previous();
1796         if (position.isNull() || position == range->startPosition())
1797             break;
1798         range = Range::create(*frame.document(), wordRangeFromPosition(position)->startPosition(), range->endPosition());
1799         textForRange = plainTextReplacingNoBreakSpace(range.get());
1800     }
1801
1802     Vector<SelectionRect> selectionRects;
1803     if (textForRange == textForAutocorrection)
1804         range->collectSelectionRects(selectionRects);
1805
1806     Vector<FloatRect> rectsForText;
1807     rectsForText.grow(selectionRects.size());
1808
1809     convertSelectionRectsToRootView(frame.view(), selectionRects);
1810     for (size_t i = 0; i < selectionRects.size(); i++)
1811         rectsForText[i] = selectionRects[i].rect();
1812
1813     bool multipleFonts = false;
1814     CTFontRef font = nil;
1815     if (auto* coreFont = frame.editor().fontForSelection(multipleFonts))
1816         font = coreFont->getCTFont();
1817
1818     CGFloat fontSize = CTFontGetSize(font);
1819     uint64_t fontTraits = CTFontGetSymbolicTraits(font);
1820     RetainPtr<NSString> fontName = adoptNS((NSString *)CTFontCopyFamilyName(font));
1821     send(Messages::WebPageProxy::AutocorrectionDataCallback(rectsForText, fontName.get(), fontSize, fontTraits, callbackID));
1822 }
1823
1824 void WebPage::applyAutocorrection(const String& correction, const String& originalText, CallbackID callbackID)
1825 {
1826     bool correctionApplied;
1827     syncApplyAutocorrection(correction, originalText, correctionApplied);
1828     send(Messages::WebPageProxy::StringCallback(correctionApplied ? correction : String(), callbackID));
1829 }
1830
1831 Seconds WebPage::eventThrottlingDelay() const
1832 {
1833     auto behaviorOverride = m_page->eventThrottlingBehaviorOverride();
1834     if (behaviorOverride) {
1835         switch (behaviorOverride.value()) {
1836         case EventThrottlingBehavior::Responsive:
1837             return 0_s;
1838         case EventThrottlingBehavior::Unresponsive:
1839             return 1_s;
1840         }
1841     }
1842
1843     if (m_isInStableState || m_estimatedLatency <= Seconds(1.0 / 60))
1844         return 0_s;
1845
1846     return std::min(m_estimatedLatency * 2, 1_s);
1847 }
1848
1849 void WebPage::syncApplyAutocorrection(const String& correction, const String& originalText, bool& correctionApplied)
1850 {
1851     correctionApplied = false;
1852
1853     Frame& frame = m_page->focusController().focusedOrMainFrame();
1854     if (!frame.selection().isCaretOrRange())
1855         return;
1856
1857     RefPtr<Range> range;
1858     String textForRange;
1859
1860     if (frame.selection().isCaret()) {
1861         VisiblePosition position = frame.selection().selection().start();
1862         range = wordRangeFromPosition(position);
1863         textForRange = plainTextReplacingNoBreakSpace(range.get());
1864         if (textForRange != originalText) {
1865             // Search for the original text before the selection caret.
1866             for (size_t i = 0; i < originalText.length(); ++i)
1867                 position = position.previous();
1868             if (position.isNull())
1869                 position = startOfDocument(static_cast<Node*>(frame.document()->documentElement()));
1870             range = Range::create(*frame.document(), position, frame.selection().selection().start());
1871             if (range)
1872                 textForRange = (range) ? plainTextReplacingNoBreakSpace(range.get()) : emptyString();
1873             unsigned loopCount = 0;
1874             const unsigned maxPositionsAttempts = 10;
1875             while (textForRange.length() && textForRange.length() > originalText.length() && loopCount < maxPositionsAttempts) {
1876                 position = position.next();
1877                 if (position.isNotNull() && position >= frame.selection().selection().start())
1878                     range = nullptr;
1879                 else
1880                     range = Range::create(*frame.document(), position, frame.selection().selection().start());
1881                 textForRange = (range) ? plainTextReplacingNoBreakSpace(range.get()) : emptyString();
1882                 loopCount++;
1883             }
1884         }
1885     } else {
1886         // Range selection.
1887         range = frame.selection().toNormalizedRange();
1888         if (!range) {
1889             correctionApplied = false;
1890             return;
1891         }
1892         textForRange = plainTextReplacingNoBreakSpace(range.get());
1893     }
1894
1895     if (textForRange != originalText) {
1896         correctionApplied = false;
1897         return;
1898     }
1899     
1900     // Correctly determine affinity, using logic currently only present in VisiblePosition
1901     EAffinity affinity = DOWNSTREAM;
1902     if (range && range->collapsed())
1903         affinity = VisiblePosition(range->startPosition(), UPSTREAM).affinity();
1904     
1905     frame.selection().setSelectedRange(range.get(), affinity, true);
1906     if (correction.length())
1907         frame.editor().insertText(correction, 0, originalText.isEmpty() ? TextEventInputKeyboard : TextEventInputAutocompletion);
1908     else
1909         frame.editor().deleteWithDirection(DirectionBackward, CharacterGranularity, false, true);
1910     correctionApplied = true;
1911 }
1912
1913 static void computeAutocorrectionContext(Frame& frame, String& contextBefore, String& markedText, String& selectedText, String& contextAfter, uint64_t& location, uint64_t& length)
1914 {
1915     RefPtr<Range> range;
1916     VisiblePosition startPosition = frame.selection().selection().start();
1917     VisiblePosition endPosition = frame.selection().selection().end();
1918     location = NSNotFound;
1919     length = 0;
1920     const unsigned minContextWordCount = 3;
1921     const unsigned minContextLenght = 12;
1922     const unsigned maxContextLength = 30;
1923
1924     if (frame.selection().isRange())
1925         selectedText = plainTextReplacingNoBreakSpace(frame.selection().selection().toNormalizedRange().get());
1926
1927     if (auto compositionRange = frame.editor().compositionRange()) {
1928         range = Range::create(*frame.document(), compositionRange->startPosition(), startPosition);
1929         String markedTextBefore;
1930         if (range)
1931             markedTextBefore = plainTextReplacingNoBreakSpace(range.get());
1932         range = Range::create(*frame.document(), endPosition, compositionRange->endPosition());
1933         String markedTextAfter;
1934         if (range)
1935             markedTextAfter = plainTextReplacingNoBreakSpace(range.get());
1936         markedText = markedTextBefore + selectedText + markedTextAfter;
1937         if (!markedText.isEmpty()) {
1938             location = markedTextBefore.length();
1939             length = selectedText.length();
1940         }
1941     } else {
1942         if (startPosition != startOfEditableContent(startPosition)) {
1943             VisiblePosition currentPosition = startPosition;
1944             VisiblePosition previousPosition;
1945             unsigned totalContextLength = 0;
1946             for (unsigned i = 0; i < minContextWordCount; ++i) {
1947                 if (contextBefore.length() >= minContextLenght)
1948                     break;
1949                 previousPosition = startOfWord(positionOfNextBoundaryOfGranularity(currentPosition, WordGranularity, DirectionBackward));
1950                 if (previousPosition.isNull())
1951                     break;
1952                 String currentWord = plainTextReplacingNoBreakSpace(Range::create(*frame.document(), previousPosition, currentPosition).ptr());
1953                 totalContextLength += currentWord.length();
1954                 if (totalContextLength >= maxContextLength)
1955                     break;
1956                 currentPosition = previousPosition;
1957             }
1958             if (currentPosition.isNotNull() && currentPosition != startPosition) {
1959                 contextBefore = plainTextReplacingNoBreakSpace(Range::create(*frame.document(), currentPosition, startPosition).ptr());
1960                 if (atBoundaryOfGranularity(currentPosition, ParagraphGranularity, DirectionBackward))
1961                     contextBefore = ASCIILiteral("\n ") + contextBefore;
1962             }
1963         }
1964
1965         if (endPosition != endOfEditableContent(endPosition)) {
1966             VisiblePosition nextPosition;
1967             if (!atBoundaryOfGranularity(endPosition, WordGranularity, DirectionForward) && withinTextUnitOfGranularity(endPosition, WordGranularity, DirectionForward))
1968                 nextPosition = positionOfNextBoundaryOfGranularity(endPosition, WordGranularity, DirectionForward);
1969             if (nextPosition.isNotNull())
1970                 contextAfter = plainTextReplacingNoBreakSpace(Range::create(*frame.document(), endPosition, nextPosition).ptr());
1971         }
1972     }
1973 }
1974
1975 void WebPage::requestAutocorrectionContext(CallbackID callbackID)
1976 {
1977     String contextBefore;
1978     String contextAfter;
1979     String selectedText;
1980     String markedText;
1981     uint64_t location;
1982     uint64_t length;
1983
1984     computeAutocorrectionContext(m_page->focusController().focusedOrMainFrame(), contextBefore, markedText, selectedText, contextAfter, location, length);
1985
1986     send(Messages::WebPageProxy::AutocorrectionContextCallback(contextBefore, markedText, selectedText, contextAfter, location, length, callbackID));
1987 }
1988
1989 void WebPage::getAutocorrectionContext(String& contextBefore, String& markedText, String& selectedText, String& contextAfter, uint64_t& location, uint64_t& length)
1990 {
1991     computeAutocorrectionContext(m_page->focusController().focusedOrMainFrame(), contextBefore, markedText, selectedText, contextAfter, location, length);
1992 }
1993
1994 static HTMLAnchorElement* containingLinkElement(Element* element)
1995 {
1996     for (auto& currentElement : elementLineage(element)) {
1997         if (currentElement.isLink() && is<HTMLAnchorElement>(currentElement))
1998             return downcast<HTMLAnchorElement>(&currentElement);
1999     }
2000     return nullptr;
2001 }
2002
2003 static inline bool isAssistableElement(Element& node)
2004 {
2005     if (is<HTMLSelectElement>(node))
2006         return true;
2007     if (is<HTMLTextAreaElement>(node))
2008         return true;
2009     if (is<HTMLInputElement>(node)) {
2010         HTMLInputElement& inputElement = downcast<HTMLInputElement>(node);
2011         // FIXME: This laundry list of types is not a good way to factor this. Need a suitable function on HTMLInputElement itself.
2012         return inputElement.isTextField() || inputElement.isDateField() || inputElement.isDateTimeLocalField() || inputElement.isMonthField() || inputElement.isTimeField();
2013     }
2014     return node.isContentEditable();
2015 }
2016
2017 void WebPage::getPositionInformation(const InteractionInformationRequest& request, InteractionInformationAtPosition& info)
2018 {
2019     info.request = request;
2020
2021     FloatPoint adjustedPoint;
2022     Node* hitNode = m_page->mainFrame().nodeRespondingToClickEvents(request.point, adjustedPoint);
2023
2024     info.nodeAtPositionIsAssistedNode = (hitNode == m_assistedNode);
2025     if (m_assistedNode) {
2026         const Frame& frame = m_page->focusController().focusedOrMainFrame();
2027         if (frame.editor().hasComposition()) {
2028             const uint32_t kHitAreaWidth = 66;
2029             const uint32_t kHitAreaHeight = 66;
2030             FrameView& view = *frame.view();
2031             IntPoint adjustedPoint(view.rootViewToContents(request.point));
2032             IntPoint constrainedPoint = m_assistedNode ? constrainPoint(adjustedPoint, frame, *m_assistedNode) : adjustedPoint;
2033             VisiblePosition position = frame.visiblePositionForPoint(constrainedPoint);
2034
2035             RefPtr<Range> compositionRange = frame.editor().compositionRange();
2036             if (position < compositionRange->startPosition())
2037                 position = compositionRange->startPosition();
2038             else if (position > compositionRange->endPosition())
2039                 position = compositionRange->endPosition();
2040             IntRect caretRect = view.contentsToRootView(position.absoluteCaretBounds());
2041             float deltaX = abs(caretRect.x() + (caretRect.width() / 2) - request.point.x());
2042             float deltaYFromTheTop = abs(caretRect.y() - request.point.y());
2043             float deltaYFromTheBottom = abs(caretRect.y() + caretRect.height() - request.point.y());
2044
2045             info.isNearMarkedText = !(deltaX > kHitAreaWidth || deltaYFromTheTop > kHitAreaHeight || deltaYFromTheBottom > kHitAreaHeight);
2046         }
2047     }
2048     bool elementIsLinkOrImage = false;
2049     if (hitNode) {
2050         Element* element = is<Element>(*hitNode) ? downcast<Element>(hitNode) : nullptr;
2051         if (element) {
2052             info.isElement = true;
2053             info.idAttribute = element->getIdAttribute();
2054             Element* linkElement = nullptr;
2055             if (element->renderer() && element->renderer()->isRenderImage()) {
2056                 elementIsLinkOrImage = true;
2057                 linkElement = containingLinkElement(element);
2058             } else if (element->isLink()) {
2059                 linkElement = element;
2060                 elementIsLinkOrImage = true;
2061             }
2062
2063             if (elementIsLinkOrImage) {
2064                 if (linkElement) {
2065                     info.isLink = true;
2066
2067                     if (request.includeSnapshot) {
2068                         // Ensure that the image contains at most 600K pixels, so that it is not too big.
2069                         if (RefPtr<WebImage> snapshot = snapshotNode(*element, SnapshotOptionsShareable, 600 * 1024))
2070                             info.image = &snapshot->bitmap();
2071                     }
2072
2073                     if (request.includeLinkIndicator) {
2074                         RefPtr<Range> linkRange = rangeOfContents(*linkElement);
2075                         if (linkRange) {
2076                             float deviceScaleFactor = corePage()->deviceScaleFactor();
2077                             const float marginInPoints = 4;
2078
2079                             RefPtr<TextIndicator> textIndicator = TextIndicator::createWithRange(*linkRange, TextIndicatorOptionTightlyFitContent | TextIndicatorOptionRespectTextColor | TextIndicatorOptionPaintBackgrounds | TextIndicatorOptionUseBoundingRectAndPaintAllContentForComplexRanges |
2080                                 TextIndicatorOptionIncludeMarginIfRangeMatchesSelection, TextIndicatorPresentationTransition::None, FloatSize(marginInPoints * deviceScaleFactor, marginInPoints * deviceScaleFactor));
2081
2082                             if (textIndicator)
2083                                 info.linkIndicator = textIndicator->data();
2084                         }
2085                     }
2086
2087 #if ENABLE(DATA_DETECTION)
2088                     info.isDataDetectorLink = DataDetection::isDataDetectorLink(*element);
2089                     if (info.isDataDetectorLink) {
2090                         const int dataDetectionExtendedContextLength = 350;
2091                         info.dataDetectorIdentifier = DataDetection::dataDetectorIdentifier(*element);
2092                         info.dataDetectorResults = element->document().frame()->dataDetectionResults();
2093                         if (DataDetection::requiresExtendedContext(*element)) {
2094                             RefPtr<Range> linkRange = Range::create(element->document());
2095                             linkRange->selectNodeContents(*element);
2096                             info.textBefore = plainTextReplacingNoBreakSpace(rangeExpandedByCharactersInDirectionAtWordBoundary(linkRange->startPosition(), dataDetectionExtendedContextLength, DirectionBackward).get(), TextIteratorDefaultBehavior, true);
2097                             info.textAfter = plainTextReplacingNoBreakSpace(rangeExpandedByCharactersInDirectionAtWordBoundary(linkRange->endPosition(), dataDetectionExtendedContextLength, DirectionForward).get(), TextIteratorDefaultBehavior, true);
2098                         }
2099                     }
2100 #endif
2101                 }
2102                 if (element->renderer() && element->renderer()->isRenderImage()) {
2103                     info.isImage = true;
2104                     auto& renderImage = downcast<RenderImage>(*(element->renderer()));
2105                     if (renderImage.cachedImage() && !renderImage.cachedImage()->errorOccurred()) {
2106                         if (Image* image = renderImage.cachedImage()->imageForRenderer(&renderImage)) {
2107                             if (image->width() > 1 && image->height() > 1) {
2108                                 info.imageURL = element->document().completeURL(renderImage.cachedImage()->url());
2109                                 info.isAnimatedImage = image->isAnimated();
2110
2111                                 if (request.includeSnapshot) {
2112                                     FloatSize screenSizeInPixels = screenSize();
2113                                     screenSizeInPixels.scale(corePage()->deviceScaleFactor());
2114                                     FloatSize scaledSize = largestRectWithAspectRatioInsideRect(image->size().width() / image->size().height(), FloatRect(0, 0, screenSizeInPixels.width(), screenSizeInPixels.height())).size();
2115                                     FloatSize bitmapSize = scaledSize.width() < image->size().width() ? scaledSize : image->size();
2116                                     // FIXME: Only select ExtendedColor on images known to need wide gamut
2117                                     ShareableBitmap::Configuration bitmapConfiguration;
2118                                     bitmapConfiguration.colorSpace.cgColorSpace = screenColorSpace(m_page->mainFrame().view());
2119                                     if (RefPtr<ShareableBitmap> sharedBitmap = ShareableBitmap::createShareable(IntSize(bitmapSize), bitmapConfiguration)) {
2120                                         auto graphicsContext = sharedBitmap->createGraphicsContext();
2121                                         graphicsContext->drawImage(*image, FloatRect(0, 0, bitmapSize.width(), bitmapSize.height()));
2122                                         info.image = sharedBitmap;
2123                                     }
2124                                 }
2125                             }
2126                         }
2127                     }
2128                 }
2129             }
2130             if (linkElement)
2131                 info.url = linkElement->document().completeURL(stripLeadingAndTrailingHTMLSpaces(linkElement->getAttribute(HTMLNames::hrefAttr)));
2132             info.title = element->attributeWithoutSynchronization(HTMLNames::titleAttr).string();
2133             if (linkElement && info.title.isEmpty())
2134                 info.title = element->innerText();
2135             if (element->renderer())
2136                 info.touchCalloutEnabled = element->renderer()->style().touchCalloutEnabled();
2137
2138             if (RenderElement* renderer = element->renderer()) {
2139                 if (renderer->isRenderImage())
2140                     info.bounds = downcast<RenderImage>(*renderer).absoluteContentQuad().enclosingBoundingBox();
2141                 else
2142                     info.bounds = renderer->absoluteBoundingBoxRect();
2143
2144                 if (!renderer->document().frame()->isMainFrame()) {
2145                     FrameView *view = renderer->document().frame()->view();
2146                     info.bounds = view->contentsToRootView(info.bounds);
2147                 }
2148             }
2149         }
2150     }
2151
2152     if (!elementIsLinkOrImage) {
2153         HitTestResult result = m_page->mainFrame().eventHandler().hitTestResultAtPoint(request.point, HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::DisallowUserAgentShadowContent | HitTestRequest::AllowChildFrameContent);
2154         hitNode = result.innerNode();
2155         // Hit test could return HTMLHtmlElement that has no renderer, if the body is smaller than the document.
2156         if (hitNode && hitNode->renderer()) {
2157             RenderObject* renderer = hitNode->renderer();
2158             m_page->focusController().setFocusedFrame(result.innerNodeFrame());
2159             info.bounds = renderer->absoluteBoundingBoxRect(true);
2160             // We don't want to select blocks that are larger than 97% of the visible area of the document.
2161             if (is<HTMLAttachmentElement>(*hitNode)) {
2162                 info.isAttachment = true;
2163                 const HTMLAttachmentElement& attachment = downcast<HTMLAttachmentElement>(*hitNode);
2164                 info.title = attachment.attachmentTitle();
2165                 if (attachment.file())
2166                     info.url = URL::fileURLWithFileSystemPath(downcast<HTMLAttachmentElement>(*hitNode).file()->path());
2167             } else {
2168                 info.isSelectable = renderer->style().userSelect() != SELECT_NONE;
2169                 if (info.isSelectable && !hitNode->isTextNode())
2170                     info.isSelectable = !isAssistableElement(*downcast<Element>(hitNode)) && !rectIsTooBigForSelection(info.bounds, *result.innerNodeFrame());
2171             }
2172         }
2173     }
2174
2175 #if ENABLE(DATA_INTERACTION)
2176     info.hasSelectionAtPosition = m_page->hasSelectionAtPosition(adjustedPoint);
2177 #endif
2178     info.adjustedPointForNodeRespondingToClickEvents = adjustedPoint;
2179 }
2180
2181 void WebPage::requestPositionInformation(const InteractionInformationRequest& request)
2182 {
2183     InteractionInformationAtPosition info;
2184
2185     getPositionInformation(request, info);
2186     send(Messages::WebPageProxy::DidReceivePositionInformation(info));
2187 }
2188
2189 void WebPage::startInteractionWithElementAtPosition(const WebCore::IntPoint& point)
2190 {
2191     FloatPoint adjustedPoint;
2192     m_interactionNode = m_page->mainFrame().nodeRespondingToClickEvents(point, adjustedPoint);
2193 }
2194
2195 void WebPage::stopInteraction()
2196 {
2197     m_interactionNode = nullptr;
2198 }
2199
2200 void WebPage::performActionOnElement(uint32_t action)
2201 {
2202     if (!is<HTMLElement>(m_interactionNode.get()))
2203         return;
2204
2205     HTMLElement& element = downcast<HTMLElement>(*m_interactionNode);
2206     if (!element.renderer())
2207         return;
2208
2209     if (static_cast<SheetAction>(action) == SheetAction::Copy) {
2210         if (is<RenderImage>(*element.renderer())) {
2211             URL url;
2212             String title;
2213             if (auto* linkElement = containingLinkElement(&element)) {
2214                 url = linkElement->href();
2215                 title = linkElement->attributeWithoutSynchronization(HTMLNames::titleAttr);
2216                 if (!title.length())
2217                     title = linkElement->textContent();
2218                 title = stripLeadingAndTrailingHTMLSpaces(title);
2219             }
2220             m_interactionNode->document().frame()->editor().writeImageToPasteboard(*Pasteboard::createForCopyAndPaste(), element, url, title);
2221         } else if (element.isLink()) {
2222             m_interactionNode->document().frame()->editor().copyURL(element.document().completeURL(stripLeadingAndTrailingHTMLSpaces(element.attributeWithoutSynchronization(HTMLNames::hrefAttr))), element.textContent());
2223         }
2224     } else if (static_cast<SheetAction>(action) == SheetAction::SaveImage) {
2225         if (!is<RenderImage>(*element.renderer()))
2226             return;
2227         CachedImage* cachedImage = downcast<RenderImage>(*element.renderer()).cachedImage();
2228         if (!cachedImage)
2229             return;
2230         RefPtr<SharedBuffer> buffer = cachedImage->resourceBuffer();
2231         if (!buffer)
2232             return;
2233         uint64_t bufferSize = buffer->size();
2234         RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::allocate(bufferSize);
2235         memcpy(sharedMemoryBuffer->data(), buffer->data(), bufferSize);
2236         SharedMemory::Handle handle;
2237         sharedMemoryBuffer->createHandle(handle, SharedMemory::Protection::ReadOnly);
2238         send(Messages::WebPageProxy::SaveImageToLibrary(handle, bufferSize));
2239     }
2240 }
2241
2242 static inline Element* nextAssistableElement(Node* startNode, Page& page, bool isForward)
2243 {
2244     if (!is<Element>(startNode))
2245         return nullptr;
2246
2247     Element* nextElement = downcast<Element>(startNode);
2248     do {
2249         nextElement = isForward
2250             ? page.focusController().nextFocusableElement(*nextElement)
2251             : page.focusController().previousFocusableElement(*nextElement);
2252     } while (nextElement && !isAssistableElement(*nextElement));
2253
2254     return nextElement;
2255 }
2256
2257 void WebPage::focusNextAssistedNode(bool isForward, CallbackID callbackID)
2258 {
2259     Element* nextElement = nextAssistableElement(m_assistedNode.get(), *m_page, isForward);
2260     m_userIsInteracting = true;
2261     if (nextElement)
2262         nextElement->focus();
2263     m_userIsInteracting = false;
2264     send(Messages::WebPageProxy::VoidCallback(callbackID));
2265 }
2266
2267 static IntRect elementRectInRootViewCoordinates(const Node& node, const Frame& frame)
2268 {
2269     auto* view = frame.view();
2270     if (!view)
2271         return { };
2272
2273     auto* renderer = node.renderer();
2274     if (!renderer)
2275         return { };
2276
2277     return view->contentsToRootView(renderer->absoluteBoundingBoxRect());
2278 }
2279
2280 void WebPage::getAssistedNodeInformation(AssistedNodeInformation& information)
2281 {
2282     layoutIfNeeded();
2283
2284     // FIXME: information.selectionRect should be set to the actual selection rect, but when this is called at focus time
2285     // we don't have a selection yet. Using the last interaction location is a reasonable approximation for now.
2286     information.selectionRect = IntRect(m_lastInteractionLocation, IntSize(1, 1));
2287
2288     if (RenderObject* renderer = m_assistedNode->renderer()) {
2289         Frame& elementFrame = m_page->focusController().focusedOrMainFrame();
2290         information.elementRect = elementRectInRootViewCoordinates(*m_assistedNode, elementFrame);
2291         information.nodeFontSize = renderer->style().fontDescription().computedSize();
2292
2293         bool inFixed = false;
2294         renderer->localToContainerPoint(FloatPoint(), nullptr, UseTransforms, &inFixed);
2295         information.insideFixedPosition = inFixed;
2296         information.isRTL = renderer->style().direction() == RTL;
2297
2298         FrameView* frameView = elementFrame.view();
2299         if (inFixed && elementFrame.isMainFrame() && !frameView->frame().settings().visualViewportEnabled()) {
2300             IntRect currentFixedPositionRect = frameView->customFixedPositionLayoutRect();
2301             frameView->setCustomFixedPositionLayoutRect(frameView->renderView()->documentRect());
2302             information.elementRect = frameView->contentsToRootView(renderer->absoluteBoundingBoxRect());
2303             frameView->setCustomFixedPositionLayoutRect(currentFixedPositionRect);
2304             
2305             if (!information.elementRect.contains(m_lastInteractionLocation))
2306                 information.selectionRect.setLocation(information.elementRect.location());
2307         } else {
2308             // Don't use the selection rect if interaction was outside the element rect.
2309             if (!information.elementRect.contains(m_lastInteractionLocation))
2310                 information.selectionRect = IntRect();
2311         }
2312     } else
2313         information.elementRect = IntRect();
2314
2315     information.minimumScaleFactor = minimumPageScaleFactor();
2316     information.maximumScaleFactor = maximumPageScaleFactor();
2317     information.maximumScaleFactorIgnoringAlwaysScalable = maximumPageScaleFactorIgnoringAlwaysScalable();
2318     information.allowsUserScaling = m_viewportConfiguration.allowsUserScaling();
2319     information.allowsUserScalingIgnoringAlwaysScalable = m_viewportConfiguration.allowsUserScalingIgnoringAlwaysScalable();
2320     if (auto* nextElement = nextAssistableElement(m_assistedNode.get(), *m_page, true)) {
2321         if (auto* frame = nextElement->document().frame())
2322             information.nextNodeRect = elementRectInRootViewCoordinates(*nextElement, *frame);
2323         information.hasNextNode = true;
2324     }
2325     if (auto* previousElement = nextAssistableElement(m_assistedNode.get(), *m_page, false)) {
2326         if (auto* frame = previousElement->document().frame())
2327             information.previousNodeRect = elementRectInRootViewCoordinates(*previousElement, *frame);
2328         information.hasPreviousNode = true;
2329     }
2330     information.assistedNodeIdentifier = m_currentAssistedNodeIdentifier;
2331
2332     if (is<LabelableElement>(*m_assistedNode)) {
2333         auto labels = downcast<LabelableElement>(*m_assistedNode).labels();
2334         Vector<Ref<Element>> associatedLabels;
2335         for (unsigned index = 0; index < labels->length(); ++index) {
2336             if (is<Element>(labels->item(index)) && labels->item(index)->renderer())
2337                 associatedLabels.append(downcast<Element>(*labels->item(index)));
2338         }
2339         for (auto& labelElement : associatedLabels) {
2340             auto text = labelElement->innerText();
2341             if (!text.isEmpty()) {
2342                 information.label = WTFMove(text);
2343                 break;
2344             }
2345         }
2346     }
2347
2348     if (is<Element>(m_assistedNode.get())) {
2349         auto& element = downcast<Element>(*m_assistedNode);
2350         information.title = element.title();
2351         information.ariaLabel = element.attributeWithoutSynchronization(HTMLNames::aria_labelAttr);
2352     }
2353
2354     if (is<HTMLSelectElement>(*m_assistedNode)) {
2355         HTMLSelectElement& element = downcast<HTMLSelectElement>(*m_assistedNode);
2356         information.elementType = InputType::Select;
2357         const Vector<HTMLElement*>& items = element.listItems();
2358         size_t count = items.size();
2359         int parentGroupID = 0;
2360         // The parent group ID indicates the group the option belongs to and is 0 for group elements.
2361         // If there are option elements in between groups, they are given it's own group identifier.
2362         // If a select does not have groups, all the option elements have group ID 0.
2363         for (size_t i = 0; i < count; ++i) {
2364             HTMLElement* item = items[i];
2365             if (is<HTMLOptionElement>(*item)) {
2366                 HTMLOptionElement& option = downcast<HTMLOptionElement>(*item);
2367                 information.selectOptions.append(OptionItem(option.text(), false, parentGroupID, option.selected(), option.hasAttributeWithoutSynchronization(WebCore::HTMLNames::disabledAttr)));
2368             } else if (is<HTMLOptGroupElement>(*item)) {
2369                 HTMLOptGroupElement& group = downcast<HTMLOptGroupElement>(*item);
2370                 parentGroupID++;
2371                 information.selectOptions.append(OptionItem(group.groupLabelText(), true, 0, false, group.hasAttributeWithoutSynchronization(WebCore::HTMLNames::disabledAttr)));
2372             }
2373         }
2374         information.selectedIndex = element.selectedIndex();
2375         information.isMultiSelect = element.multiple();
2376     } else if (is<HTMLTextAreaElement>(*m_assistedNode)) {
2377         HTMLTextAreaElement& element = downcast<HTMLTextAreaElement>(*m_assistedNode);
2378         information.autocapitalizeType = element.autocapitalizeType();
2379         information.isAutocorrect = element.shouldAutocorrect();
2380         information.elementType = InputType::TextArea;
2381         information.isReadOnly = element.isReadOnly();
2382         information.value = element.value();
2383         information.autofillFieldName = WebCore::toAutofillFieldName(element.autofillData().fieldName);
2384         information.placeholder = element.attributeWithoutSynchronization(HTMLNames::placeholderAttr);
2385     } else if (is<HTMLInputElement>(*m_assistedNode)) {
2386         HTMLInputElement& element = downcast<HTMLInputElement>(*m_assistedNode);
2387         HTMLFormElement* form = element.form();
2388         if (form)
2389             information.formAction = form->getURLAttribute(WebCore::HTMLNames::actionAttr);
2390         information.acceptsAutofilledLoginCredentials = !!WebCore::AutofillElements::computeAutofillElements(element);
2391         information.representingPageURL = element.document().urlForBindings();
2392         information.autocapitalizeType = element.autocapitalizeType();
2393         information.isAutocorrect = element.shouldAutocorrect();
2394         information.placeholder = element.attributeWithoutSynchronization(HTMLNames::placeholderAttr);
2395         if (element.isPasswordField())
2396             information.elementType = InputType::Password;
2397         else if (element.isSearchField())
2398             information.elementType = InputType::Search;
2399         else if (element.isEmailField())
2400             information.elementType = InputType::Email;
2401         else if (element.isTelephoneField())
2402             information.elementType = InputType::Phone;
2403         else if (element.isNumberField())
2404             information.elementType = element.getAttribute("pattern") == "\\d*" || element.getAttribute("pattern") == "[0-9]*" ? InputType::NumberPad : InputType::Number;
2405         else if (element.isDateTimeLocalField())
2406             information.elementType = InputType::DateTimeLocal;
2407         else if (element.isDateField())
2408             information.elementType = InputType::Date;
2409         else if (element.isDateTimeField())
2410             information.elementType = InputType::DateTime;
2411         else if (element.isTimeField())
2412             information.elementType = InputType::Time;
2413         else if (element.isWeekField())
2414             information.elementType = InputType::Week;
2415         else if (element.isMonthField())
2416             information.elementType = InputType::Month;
2417         else if (element.isURLField())
2418             information.elementType = InputType::URL;
2419         else if (element.isText()) {
2420             const AtomicString& pattern = element.attributeWithoutSynchronization(HTMLNames::patternAttr);
2421             if (pattern == "\\d*" || pattern == "[0-9]*")
2422                 information.elementType = InputType::NumberPad;
2423             else {
2424                 information.elementType = InputType::Text;
2425                 if (!information.formAction.isEmpty()
2426                     && (element.getNameAttribute().contains("search") || element.getIdAttribute().contains("search") || element.attributeWithoutSynchronization(HTMLNames::titleAttr).contains("search")))
2427                     information.elementType = InputType::Search;
2428             }
2429         }
2430
2431         information.isReadOnly = element.isReadOnly();
2432         information.value = element.value();
2433         information.valueAsNumber = element.valueAsNumber();
2434         information.autofillFieldName = WebCore::toAutofillFieldName(element.autofillData().fieldName);
2435     } else if (m_assistedNode->hasEditableStyle()) {
2436         information.elementType = InputType::ContentEditable;
2437         if (is<HTMLElement>(*m_assistedNode)) {
2438             auto& assistedElement = downcast<HTMLElement>(*m_assistedNode);
2439             information.isAutocorrect = assistedElement.shouldAutocorrect();
2440             information.autocapitalizeType = assistedElement.autocapitalizeType();
2441         } else {
2442             information.isAutocorrect = true;
2443             information.autocapitalizeType = AutocapitalizeTypeDefault;
2444         }
2445         information.isReadOnly = false;
2446     }
2447 }
2448
2449 void WebPage::autofillLoginCredentials(const String& username, const String& password)
2450 {
2451     if (is<HTMLInputElement>(*m_assistedNode)) {
2452         if (auto autofillElements = AutofillElements::computeAutofillElements(downcast<HTMLInputElement>(*m_assistedNode)))
2453             autofillElements->autofill(username, password);
2454     }
2455 }
2456
2457 void WebPage::setViewportConfigurationMinimumLayoutSize(const FloatSize& size)
2458 {
2459     LOG_WITH_STREAM(VisibleRects, stream << "WebPage " << m_pageID << " setViewportConfigurationMinimumLayoutSize " << size);
2460     if (m_viewportConfiguration.setMinimumLayoutSize(size))
2461         viewportConfigurationChanged();
2462 }
2463
2464 void WebPage::setMaximumUnobscuredSize(const FloatSize& maximumUnobscuredSize)
2465 {
2466     m_maximumUnobscuredSize = maximumUnobscuredSize;
2467     updateViewportSizeForCSSViewportUnits();
2468 }
2469
2470 void WebPage::setDeviceOrientation(int32_t deviceOrientation)
2471 {
2472     if (deviceOrientation == m_deviceOrientation)
2473         return;
2474     m_deviceOrientation = deviceOrientation;
2475     m_page->mainFrame().orientationChanged();
2476 }
2477
2478 // WebCore stores the page scale factor as float instead of double. When we get a scale from WebCore,
2479 // we need to ignore differences that are within a small rounding error on floats.
2480 static inline bool areEssentiallyEqualAsFloat(float a, float b)
2481 {
2482     return WTF::areEssentiallyEqual(a, b);
2483 }
2484
2485 void WebPage::resetTextAutosizing()
2486 {
2487     for (Frame* frame = &m_page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
2488         Document* document = frame->document();
2489         if (!document || !document->renderView())
2490             continue;
2491         document->renderView()->resetTextAutosizing();
2492     }
2493 }
2494
2495 void WebPage::dynamicViewportSizeUpdate(const FloatSize& minimumLayoutSize, const WebCore::FloatSize& maximumUnobscuredSize, const FloatRect& targetExposedContentRect, const FloatRect& targetUnobscuredRect, const WebCore::FloatRect& targetUnobscuredRectInScrollViewCoordinates, const WebCore::FloatBoxExtent& targetUnobscuredSafeAreaInsets, double targetScale, int32_t deviceOrientation, uint64_t dynamicViewportSizeUpdateID)
2496 {
2497     SetForScope<bool> dynamicSizeUpdateGuard(m_inDynamicSizeUpdate, true);
2498     // FIXME: this does not handle the cases where the content would change the content size or scroll position from JavaScript.
2499     // To handle those cases, we would need to redo this computation on every change until the next visible content rect update.
2500     LOG_WITH_STREAM(VisibleRects, stream << "\nWebPage::dynamicViewportSizeUpdate - minimumLayoutSize " << minimumLayoutSize << " targetUnobscuredRect " << targetUnobscuredRect << " targetExposedContentRect " << targetExposedContentRect << " targetScale " << targetScale);
2501
2502     FrameView& frameView = *m_page->mainFrame().view();
2503     IntSize oldContentSize = frameView.contentsSize();
2504     float oldPageScaleFactor = m_page->pageScaleFactor();
2505
2506     m_dynamicSizeUpdateHistory.add(std::make_pair(oldContentSize, oldPageScaleFactor), frameView.scrollPosition());
2507
2508     RefPtr<Node> oldNodeAtCenter;
2509     double visibleHorizontalFraction = 1;
2510     float relativeHorizontalPositionInNodeAtCenter = 0;
2511     float relativeVerticalPositionInNodeAtCenter = 0;
2512     {
2513         visibleHorizontalFraction = frameView.unobscuredContentSize().width() / oldContentSize.width();
2514         IntPoint unobscuredContentRectCenter = frameView.unobscuredContentRect().center();
2515
2516         HitTestResult hitTestResult = HitTestResult(unobscuredContentRectCenter);
2517
2518         if (RenderView* mainFrameRenderView = frameView.renderView())
2519             mainFrameRenderView->hitTest(HitTestRequest(), hitTestResult);
2520
2521         if (Node* node = hitTestResult.innerNode()) {
2522             if (RenderObject* renderer = node->renderer()) {
2523                 FrameView& containingView = *node->document().frame()->view();
2524                 FloatRect boundingBox = containingView.contentsToRootView(renderer->absoluteBoundingBoxRect(true));
2525                 relativeHorizontalPositionInNodeAtCenter = (unobscuredContentRectCenter.x() - boundingBox.x()) / boundingBox.width();
2526                 relativeVerticalPositionInNodeAtCenter = (unobscuredContentRectCenter.y() - boundingBox.y()) / boundingBox.height();
2527                 oldNodeAtCenter = node;
2528             }
2529         }
2530     }
2531
2532     LOG_WITH_STREAM(VisibleRects, stream << "WebPage::dynamicViewportSizeUpdate setting minimum layout size to " << minimumLayoutSize);
2533     m_viewportConfiguration.setMinimumLayoutSize(minimumLayoutSize);
2534     IntSize newLayoutSize = m_viewportConfiguration.layoutSize();
2535
2536     if (setFixedLayoutSize(newLayoutSize))
2537         resetTextAutosizing();
2538
2539     setMaximumUnobscuredSize(maximumUnobscuredSize);
2540     m_page->setUnobscuredSafeAreaInsets(targetUnobscuredSafeAreaInsets);
2541
2542     frameView.updateLayoutAndStyleIfNeededRecursive();
2543
2544     IntSize newContentSize = frameView.contentsSize();
2545
2546     double scale = scaleAfterViewportWidthChange(targetScale, m_userHasChangedPageScaleFactor, m_viewportConfiguration, targetUnobscuredRectInScrollViewCoordinates.width(), newContentSize, oldContentSize, visibleHorizontalFraction);
2547     FloatRect newUnobscuredContentRect = targetUnobscuredRect;
2548     FloatRect newExposedContentRect = targetExposedContentRect;
2549
2550     bool scaleChanged = !areEssentiallyEqualAsFloat(scale, targetScale);
2551     if (scaleChanged) {
2552         // The target scale the UI is using cannot be reached by the content. We need to compute new targets based
2553         // on the viewport constraint and report everything back to the UIProcess.
2554
2555         // 1) Compute a new unobscured rect centered around the original one.
2556         double scaleDifference = targetScale / scale;
2557         double newUnobscuredRectWidth = targetUnobscuredRect.width() * scaleDifference;
2558         double newUnobscuredRectHeight = targetUnobscuredRect.height() * scaleDifference;
2559         double newUnobscuredRectX = targetUnobscuredRect.x() - (newUnobscuredRectWidth - targetUnobscuredRect.width()) / 2;
2560         double newUnobscuredRectY = targetUnobscuredRect.y() - (newUnobscuredRectHeight - targetUnobscuredRect.height()) / 2;
2561         newUnobscuredContentRect = FloatRect(newUnobscuredRectX, newUnobscuredRectY, newUnobscuredRectWidth, newUnobscuredRectHeight);
2562
2563         // 2) Extend our new unobscuredRect by the obscured margins to get a new exposed rect.
2564         double obscuredTopMargin = (targetUnobscuredRect.y() - targetExposedContentRect.y()) * scaleDifference;
2565         double obscuredLeftMargin = (targetUnobscuredRect.x() - targetExposedContentRect.x()) * scaleDifference;
2566         double obscuredBottomMargin = (targetExposedContentRect.maxY() - targetUnobscuredRect.maxY()) * scaleDifference;
2567         double obscuredRightMargin = (targetExposedContentRect.maxX() - targetUnobscuredRect.maxX()) * scaleDifference;
2568         newExposedContentRect = FloatRect(newUnobscuredRectX - obscuredLeftMargin,
2569                                           newUnobscuredRectY - obscuredTopMargin,
2570                                           newUnobscuredRectWidth + obscuredLeftMargin + obscuredRightMargin,
2571                                           newUnobscuredRectHeight + obscuredTopMargin + obscuredBottomMargin);
2572     }
2573
2574     if (oldContentSize != newContentSize || scaleChanged) {
2575         // Snap the new unobscured rect back into the content rect.
2576         newUnobscuredContentRect.setWidth(std::min(static_cast<float>(newContentSize.width()), newUnobscuredContentRect.width()));
2577         newUnobscuredContentRect.setHeight(std::min(static_cast<float>(newContentSize.height()), newUnobscuredContentRect.height()));
2578
2579         bool positionWasRestoredFromSizeUpdateHistory = false;
2580         const auto& previousPosition = m_dynamicSizeUpdateHistory.find(std::pair<IntSize, float>(newContentSize, scale));
2581         if (previousPosition != m_dynamicSizeUpdateHistory.end()) {
2582             IntPoint restoredPosition = previousPosition->value;
2583             FloatPoint deltaPosition(restoredPosition.x() - newUnobscuredContentRect.x(), restoredPosition.y() - newUnobscuredContentRect.y());
2584             newUnobscuredContentRect.moveBy(deltaPosition);
2585             newExposedContentRect.moveBy(deltaPosition);
2586             positionWasRestoredFromSizeUpdateHistory = true;
2587         } else if (oldContentSize != newContentSize) {
2588             FloatPoint newRelativeContentCenter;
2589
2590             if (RenderObject* renderer = oldNodeAtCenter ? oldNodeAtCenter->renderer() : nullptr) {
2591                 FrameView& containingView = *oldNodeAtCenter->document().frame()->view();
2592                 FloatRect newBoundingBox = containingView.contentsToRootView(renderer->absoluteBoundingBoxRect(true));
2593                 newRelativeContentCenter = FloatPoint(newBoundingBox.x() + relativeHorizontalPositionInNodeAtCenter * newBoundingBox.width(), newBoundingBox.y() + relativeVerticalPositionInNodeAtCenter * newBoundingBox.height());
2594             } else
2595                 newRelativeContentCenter = relativeCenterAfterContentSizeChange(targetUnobscuredRect, oldContentSize, newContentSize);
2596
2597             FloatPoint newUnobscuredContentRectCenter = newUnobscuredContentRect.center();
2598             FloatPoint positionDelta(newRelativeContentCenter.x() - newUnobscuredContentRectCenter.x(), newRelativeContentCenter.y() - newUnobscuredContentRectCenter.y());
2599             newUnobscuredContentRect.moveBy(positionDelta);
2600             newExposedContentRect.moveBy(positionDelta);
2601         }
2602
2603         // Make the top/bottom edges "sticky" within 1 pixel.
2604         if (!positionWasRestoredFromSizeUpdateHistory) {
2605             if (targetUnobscuredRect.maxY() > oldContentSize.height() - 1) {
2606                 float bottomVerticalPosition = newContentSize.height() - newUnobscuredContentRect.height();
2607                 newUnobscuredContentRect.setY(bottomVerticalPosition);
2608                 newExposedContentRect.setY(bottomVerticalPosition);
2609             }
2610             if (targetUnobscuredRect.y() < 1) {
2611                 newUnobscuredContentRect.setY(0);
2612                 newExposedContentRect.setY(0);
2613             }
2614
2615             bool likelyResponsiveDesignViewport = newLayoutSize.width() == minimumLayoutSize.width() && areEssentiallyEqualAsFloat(scale, 1);
2616             bool contentBleedsOutsideLayoutWidth = newContentSize.width() > newLayoutSize.width();
2617             bool originalScrollPositionWasOnTheLeftEdge = targetUnobscuredRect.x() <= 0;
2618             if (likelyResponsiveDesignViewport && contentBleedsOutsideLayoutWidth && originalScrollPositionWasOnTheLeftEdge) {
2619                 // This is a special heuristics for "responsive" design with odd layout. It is quite common for responsive design
2620                 // to have content "bleeding" outside of the minimal layout width, usually from an image or table larger than expected.
2621                 // In those cases, the design usually does not adapt to the new width and remain at the newLayoutSize except for the
2622                 // large boxes.
2623                 // It is worth revisiting this special case as web developers get better with responsive design.
2624                 newExposedContentRect.setX(0);
2625                 newUnobscuredContentRect.setX(0);
2626             }
2627         }
2628
2629         float horizontalAdjustment = 0;
2630         if (newUnobscuredContentRect.maxX() > newContentSize.width())
2631             horizontalAdjustment -= newUnobscuredContentRect.maxX() - newContentSize.width();
2632         float verticalAdjustment = 0;
2633         if (newUnobscuredContentRect.maxY() > newContentSize.height())
2634             verticalAdjustment -= newUnobscuredContentRect.maxY() - newContentSize.height();
2635         if (newUnobscuredContentRect.x() < 0)
2636             horizontalAdjustment += - newUnobscuredContentRect.x();
2637         if (newUnobscuredContentRect.y() < 0)
2638             verticalAdjustment += - newUnobscuredContentRect.y();
2639
2640         FloatPoint adjustmentDelta(horizontalAdjustment, verticalAdjustment);
2641         newUnobscuredContentRect.moveBy(adjustmentDelta);
2642         newExposedContentRect.moveBy(adjustmentDelta);
2643     }
2644
2645     frameView.setScrollVelocity(0, 0, 0, MonotonicTime::now());
2646
2647     IntPoint roundedUnobscuredContentRectPosition = roundedIntPoint(newUnobscuredContentRect.location());
2648     frameView.setUnobscuredContentSize(newUnobscuredContentRect.size());
2649     m_drawingArea->setExposedContentRect(newExposedContentRect);
2650
2651     scalePage(scale, roundedUnobscuredContentRectPosition);
2652
2653     frameView.updateLayoutAndStyleIfNeededRecursive();
2654
2655     auto& settings = frameView.frame().settings();
2656     if (settings.visualViewportEnabled()) {
2657         LayoutRect documentRect = IntRect(frameView.scrollOrigin(), frameView.contentsSize());
2658         auto layoutViewportSize = FrameView::expandedLayoutViewportSize(frameView.baseLayoutViewportSize(), LayoutSize(documentRect.size()), settings.layoutViewportHeightExpansionFactor());
2659         LayoutRect layoutViewportRect = FrameView::computeUpdatedLayoutViewportRect(frameView.layoutViewportRect(), documentRect, LayoutSize(newUnobscuredContentRect.size()), LayoutRect(newUnobscuredContentRect), layoutViewportSize, frameView.minStableLayoutViewportOrigin(), frameView.maxStableLayoutViewportOrigin(), FrameView::LayoutViewportConstraint::ConstrainedToDocumentRect);
2660         frameView.setLayoutViewportOverrideRect(layoutViewportRect);
2661     } else {
2662         IntRect fixedPositionLayoutRect = enclosingIntRect(frameView.viewportConstrainedObjectsRect());
2663         frameView.setCustomFixedPositionLayoutRect(fixedPositionLayoutRect);
2664     }
2665
2666     frameView.setCustomSizeForResizeEvent(expandedIntSize(targetUnobscuredRectInScrollViewCoordinates.size()));
2667     setDeviceOrientation(deviceOrientation);
2668     frameView.setScrollOffset(roundedUnobscuredContentRectPosition);
2669
2670     m_drawingArea->scheduleCompositingLayerFlush();
2671
2672     send(Messages::WebPageProxy::DynamicViewportUpdateChangedTarget(pageScaleFactor(), frameView.scrollPosition(), dynamicViewportSizeUpdateID));
2673 }
2674
2675 void WebPage::synchronizeDynamicViewportUpdate(double& newTargetScale, FloatPoint& newScrollPosition, uint64_t& nextValidLayerTreeTransactionID)
2676 {
2677     newTargetScale = pageScaleFactor();
2678     newScrollPosition = m_page->mainFrame().view()->scrollPosition();
2679     nextValidLayerTreeTransactionID = downcast<RemoteLayerTreeDrawingArea>(*m_drawingArea).nextTransactionID();
2680 }
2681
2682 void WebPage::resetViewportDefaultConfiguration(WebFrame* frame, bool hasMobileDocType)
2683 {
2684     LOG_WITH_STREAM(VisibleRects, stream << "WebPage " << m_pageID << " resetViewportDefaultConfiguration");
2685     if (m_useTestingViewportConfiguration) {
2686         m_viewportConfiguration.setDefaultConfiguration(ViewportConfiguration::testingParameters());
2687         return;
2688     }
2689
2690     if (!frame) {
2691         m_viewportConfiguration.setDefaultConfiguration(ViewportConfiguration::webpageParameters());
2692         return;
2693     }
2694
2695     if (hasMobileDocType) {
2696         m_viewportConfiguration.setDefaultConfiguration(ViewportConfiguration::xhtmlMobileParameters());
2697         return;
2698     }
2699
2700     Document* document = frame->coreFrame()->document();
2701     if (document->isImageDocument())
2702         m_viewportConfiguration.setDefaultConfiguration(ViewportConfiguration::imageDocumentParameters());
2703     else if (document->isTextDocument())
2704         m_viewportConfiguration.setDefaultConfiguration(ViewportConfiguration::textDocumentParameters());
2705     else
2706         m_viewportConfiguration.setDefaultConfiguration(ViewportConfiguration::webpageParameters());
2707 }
2708
2709 void WebPage::viewportConfigurationChanged()
2710 {
2711     if (setFixedLayoutSize(m_viewportConfiguration.layoutSize()))
2712         resetTextAutosizing();
2713
2714     double initialScale = m_viewportConfiguration.initialScale();
2715     double scale;
2716     if (m_userHasChangedPageScaleFactor)
2717         scale = std::max(std::min(pageScaleFactor(), m_viewportConfiguration.maximumScale()), m_viewportConfiguration.minimumScale());
2718     else
2719         scale = initialScale;
2720
2721     LOG_WITH_STREAM(VisibleRects, stream << "WebPage " << m_pageID << " viewportConfigurationChanged - setting zoomedOutPageScaleFactor to " << m_viewportConfiguration.minimumScale() << " and scale to " << scale);
2722
2723     m_page->setZoomedOutPageScaleFactor(m_viewportConfiguration.minimumScale());
2724
2725     updateViewportSizeForCSSViewportUnits();
2726
2727     FrameView& frameView = *mainFrameView();
2728     IntPoint scrollPosition = frameView.scrollPosition();
2729     if (!m_hasReceivedVisibleContentRectsAfterDidCommitLoad) {
2730         FloatSize minimumLayoutSizeInScrollViewCoordinates = m_viewportConfiguration.minimumLayoutSize();
2731         minimumLayoutSizeInScrollViewCoordinates.scale(1 / scale);
2732         IntSize minimumLayoutSizeInDocumentCoordinates = roundedIntSize(minimumLayoutSizeInScrollViewCoordinates);
2733         frameView.setUnobscuredContentSize(minimumLayoutSizeInDocumentCoordinates);
2734         frameView.setScrollVelocity(0, 0, 0, MonotonicTime::now());
2735
2736         // FIXME: We could send down the obscured margins to find a better exposed rect and unobscured rect.
2737         // It is not a big deal at the moment because the tile coverage will always extend past the obscured bottom inset.
2738         m_drawingArea->setExposedContentRect(FloatRect(scrollPosition, minimumLayoutSizeInDocumentCoordinates));
2739     }
2740     scalePage(scale, scrollPosition);
2741     
2742     if (!m_hasReceivedVisibleContentRectsAfterDidCommitLoad) {
2743         // This takes scale into account, so do after the scale change.
2744         frameView.setCustomFixedPositionLayoutRect(enclosingIntRect(frameView.viewportConstrainedObjectsRect()));
2745
2746         frameView.setCustomSizeForResizeEvent(expandedIntSize(m_viewportConfiguration.minimumLayoutSize()));
2747     }
2748 }
2749
2750 void WebPage::updateViewportSizeForCSSViewportUnits()
2751 {
2752     FloatSize largestUnobscuredRect = m_maximumUnobscuredSize;
2753     if (largestUnobscuredRect.isEmpty())
2754         largestUnobscuredRect = m_viewportConfiguration.minimumLayoutSize();
2755
2756     FrameView& frameView = *mainFrameView();
2757     largestUnobscuredRect.scale(1 / m_viewportConfiguration.initialScaleIgnoringContentSize());
2758     frameView.setViewportSizeForCSSViewportUnits(roundedIntSize(largestUnobscuredRect));
2759 }
2760
2761 void WebPage::applicationWillResignActive()
2762 {
2763     [[NSNotificationCenter defaultCenter] postNotificationName:WebUIApplicationWillResignActiveNotification object:nil];
2764     m_page->applicationWillResignActive();
2765 }
2766
2767 void WebPage::applicationDidEnterBackground(bool isSuspendedUnderLock)
2768 {
2769     [[NSNotificationCenter defaultCenter] postNotificationName:WebUIApplicationDidEnterBackgroundNotification object:nil userInfo:@{@"isSuspendedUnderLock": [NSNumber numberWithBool:isSuspendedUnderLock]}];
2770
2771     m_isSuspendedUnderLock = isSuspendedUnderLock;
2772     setLayerTreeStateIsFrozen(true);
2773
2774     m_page->applicationDidEnterBackground();
2775 }
2776
2777 void WebPage::applicationDidFinishSnapshottingAfterEnteringBackground()
2778 {
2779     markLayersVolatile();
2780 }
2781
2782 void WebPage::applicationWillEnterForeground(bool isSuspendedUnderLock)
2783 {
2784     m_isSuspendedUnderLock = false;
2785     cancelMarkLayersVolatile();
2786     setLayerTreeStateIsFrozen(false);
2787
2788     [[NSNotificationCenter defaultCenter] postNotificationName:WebUIApplicationWillEnterForegroundNotification object:nil userInfo:@{@"isSuspendedUnderLock": @(isSuspendedUnderLock)}];
2789
2790     m_page->applicationWillEnterForeground();
2791 }
2792
2793 void WebPage::applicationDidBecomeActive()
2794 {
2795     [[NSNotificationCenter defaultCenter] postNotificationName:WebUIApplicationDidBecomeActiveNotification object:nil];
2796     m_page->applicationDidBecomeActive();
2797 }
2798
2799 static inline void adjustVelocityDataForBoundedScale(double& horizontalVelocity, double& verticalVelocity, double& scaleChangeRate, double exposedRectScale, double minimumScale, double maximumScale)
2800 {
2801     if (scaleChangeRate) {
2802         horizontalVelocity = 0;
2803         verticalVelocity = 0;
2804     }
2805
2806     if (exposedRectScale >= maximumScale || exposedRectScale <= minimumScale)
2807         scaleChangeRate = 0;
2808 }
2809
2810 static inline FloatRect adjustExposedRectForBoundedScale(const FloatRect& exposedRect, double exposedRectScale, double newScale)
2811 {
2812     if (exposedRectScale < newScale)
2813         return exposedRect;
2814
2815     return adjustExposedRectForNewScale(exposedRect, exposedRectScale, newScale);
2816 }
2817
2818 std::optional<float> WebPage::scaleFromUIProcess(const VisibleContentRectUpdateInfo& visibleContentRectUpdateInfo) const
2819 {
2820     auto transactionIDForLastScaleSentToUIProcess = downcast<RemoteLayerTreeDrawingArea>(*m_drawingArea).lastCommittedTransactionID();
2821     auto transactionIDForLastScaleFromUIProcess = visibleContentRectUpdateInfo.lastLayerTreeTransactionID();
2822     if (transactionIDForLastScaleSentToUIProcess != transactionIDForLastScaleFromUIProcess)
2823         return std::nullopt;
2824
2825     float scaleFromUIProcess = visibleContentRectUpdateInfo.scale();
2826     float currentScale = m_page->pageScaleFactor();
2827
2828     double scaleNoiseThreshold = 0.005;
2829     if (!m_isInStableState && fabs(scaleFromUIProcess - currentScale) < scaleNoiseThreshold) {
2830         // Tiny changes of scale during interactive zoom cause content to jump by one pixel, creating
2831         // visual noise. We filter those useless updates.
2832         scaleFromUIProcess = currentScale;
2833     }
2834     
2835     scaleFromUIProcess = std::min<float>(m_viewportConfiguration.maximumScale(), std::max<float>(m_viewportConfiguration.minimumScale(), scaleFromUIProcess));
2836     if (areEssentiallyEqualAsFloat(currentScale, scaleFromUIProcess))
2837         return std::nullopt;
2838
2839     return scaleFromUIProcess;
2840 }
2841
2842 static bool selectionIsInsideFixedPositionContainer(Frame& frame)
2843 {
2844     auto& selection = frame.selection().selection();
2845     if (selection.isNone())
2846         return false;
2847
2848     bool isInsideFixedPosition = false;
2849     if (selection.isCaret()) {
2850         frame.selection().absoluteCaretBounds(&isInsideFixedPosition);
2851         return isInsideFixedPosition;
2852     }
2853
2854     selection.visibleStart().absoluteCaretBounds(&isInsideFixedPosition);
2855     if (isInsideFixedPosition)
2856         return true;
2857
2858     selection.visibleEnd().absoluteCaretBounds(&isInsideFixedPosition);
2859     return isInsideFixedPosition;
2860 }
2861
2862 void WebPage::updateVisibleContentRects(const VisibleContentRectUpdateInfo& visibleContentRectUpdateInfo, MonotonicTime oldestTimestamp)
2863 {
2864     LOG_WITH_STREAM(VisibleRects, stream << "\nWebPage " << m_pageID << " updateVisibleContentRects " << visibleContentRectUpdateInfo);
2865
2866     // Skip any VisibleContentRectUpdate that have been queued before DidCommitLoad suppresses the updates in the UIProcess.
2867     if (visibleContentRectUpdateInfo.lastLayerTreeTransactionID() < m_mainFrame->firstLayerTreeTransactionIDAfterDidCommitLoad() && !visibleContentRectUpdateInfo.isFirstUpdateForNewViewSize())
2868         return;
2869
2870     m_hasReceivedVisibleContentRectsAfterDidCommitLoad = true;
2871     m_isInStableState = visibleContentRectUpdateInfo.inStableState();
2872
2873     auto scaleFromUIProcess = this->scaleFromUIProcess(visibleContentRectUpdateInfo);
2874
2875     // Skip progressively redrawing tiles if pinch-zooming while the system is under memory pressure.
2876     if (scaleFromUIProcess && !m_isInStableState && MemoryPressureHandler::singleton().isUnderMemoryPressure())
2877         return;
2878
2879     if (m_isInStableState)
2880         m_hasStablePageScaleFactor = true;
2881     else {
2882         if (!m_oldestNonStableUpdateVisibleContentRectsTimestamp)
2883             m_oldestNonStableUpdateVisibleContentRectsTimestamp = oldestTimestamp;
2884     }
2885
2886     float scaleToUse = scaleFromUIProcess.value_or(m_page->pageScaleFactor());
2887     FloatRect exposedContentRect = visibleContentRectUpdateInfo.exposedContentRect();
2888     FloatRect adjustedExposedContentRect = adjustExposedRectForBoundedScale(exposedContentRect, visibleContentRectUpdateInfo.scale(), scaleToUse);
2889     m_drawingArea->setExposedContentRect(adjustedExposedContentRect);
2890
2891     IntPoint scrollPosition = roundedIntPoint(visibleContentRectUpdateInfo.unobscuredContentRect().location());
2892
2893     bool hasSetPageScale = false;
2894     if (scaleFromUIProcess) {
2895         m_scaleWasSetByUIProcess = true;
2896         m_hasStablePageScaleFactor = m_isInStableState;
2897
2898         m_dynamicSizeUpdateHistory.clear();
2899
2900         m_page->setPageScaleFactor(scaleFromUIProcess.value(), scrollPosition, m_isInStableState);
2901         hasSetPageScale = true;
2902         send(Messages::WebPageProxy::PageScaleFactorDidChange(scaleFromUIProcess.value()));
2903     }
2904     
2905     if (!hasSetPageScale && m_isInStableState) {
2906         m_page->setPageScaleFactor(scaleToUse, scrollPosition, true);
2907         hasSetPageScale = true;
2908     }
2909
2910     auto& frame = m_page->mainFrame();
2911     FrameView& frameView = *frame.view();
2912     if (scrollPosition != frameView.scrollPosition())
2913         m_dynamicSizeUpdateHistory.clear();
2914
2915     if (m_viewportConfiguration.setCanIgnoreScalingConstraints(m_ignoreViewportScalingConstraints && visibleContentRectUpdateInfo.allowShrinkToFit()))
2916         viewportConfigurationChanged();
2917
2918     frameView.setUnobscuredContentSize(visibleContentRectUpdateInfo.unobscuredContentRect().size());
2919     m_page->setObscuredInsets(visibleContentRectUpdateInfo.obscuredInsets());
2920     m_page->setUnobscuredSafeAreaInsets(visibleContentRectUpdateInfo.unobscuredSafeAreaInsets());
2921     m_page->setEnclosedInScrollableAncestorView(visibleContentRectUpdateInfo.enclosedInScrollableAncestorView());
2922
2923     double horizontalVelocity = visibleContentRectUpdateInfo.horizontalVelocity();
2924     double verticalVelocity = visibleContentRectUpdateInfo.verticalVelocity();
2925     double scaleChangeRate = visibleContentRectUpdateInfo.scaleChangeRate();
2926     adjustVelocityDataForBoundedScale(horizontalVelocity, verticalVelocity, scaleChangeRate, visibleContentRectUpdateInfo.scale(), m_viewportConfiguration.minimumScale(), m_viewportConfiguration.maximumScale());
2927
2928     frameView.setScrollVelocity(horizontalVelocity, verticalVelocity, scaleChangeRate, visibleContentRectUpdateInfo.timestamp());
2929
2930     if (m_isInStableState) {
2931         if (frameView.frame().settings().visualViewportEnabled()) {
2932             if (visibleContentRectUpdateInfo.unobscuredContentRect() != visibleContentRectUpdateInfo.unobscuredContentRectRespectingInputViewBounds())
2933                 frameView.setVisualViewportOverrideRect(LayoutRect(visibleContentRectUpdateInfo.unobscuredContentRectRespectingInputViewBounds()));
2934             else
2935                 frameView.setVisualViewportOverrideRect(std::nullopt);
2936
2937             LOG_WITH_STREAM(VisibleRects, stream << "WebPage::updateVisibleContentRects - setLayoutViewportOverrideRect " << visibleContentRectUpdateInfo.customFixedPositionRect());
2938             frameView.setLayoutViewportOverrideRect(LayoutRect(visibleContentRectUpdateInfo.customFixedPositionRect()));
2939             if (selectionIsInsideFixedPositionContainer(frame)) {
2940                 // Ensure that the next layer tree commit contains up-to-date caret/selection rects.
2941                 frameView.frame().selection().setCaretRectNeedsUpdate();
2942                 sendPartialEditorStateAndSchedulePostLayoutUpdate();
2943             }
2944         } else
2945             frameView.setCustomFixedPositionLayoutRect(enclosingIntRect(visibleContentRectUpdateInfo.customFixedPositionRect()));
2946     }
2947
2948     if (!visibleContentRectUpdateInfo.isChangingObscuredInsetsInteractively())
2949         frameView.setCustomSizeForResizeEvent(expandedIntSize(visibleContentRectUpdateInfo.unobscuredRectInScrollViewCoordinates().size()));
2950
2951     if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator()) {
2952         ViewportRectStability viewportStability = ViewportRectStability::Stable;
2953         ScrollingLayerPositionAction layerAction = ScrollingLayerPositionAction::Sync;
2954         
2955         if (visibleContentRectUpdateInfo.isChangingObscuredInsetsInteractively()) {
2956             viewportStability = ViewportRectStability::ChangingObscuredInsetsInteractively;
2957             layerAction = ScrollingLayerPositionAction::SetApproximate;
2958         } else if (!m_isInStableState) {
2959             viewportStability = ViewportRectStability::Unstable;
2960             layerAction = ScrollingLayerPositionAction::SetApproximate;
2961         }
2962         scrollingCoordinator->reconcileScrollingState(frameView, scrollPosition, visibleContentRectUpdateInfo.customFixedPositionRect(), false, viewportStability, layerAction);
2963     }
2964 }
2965
2966 void WebPage::willStartUserTriggeredZooming()
2967 {
2968     m_page->diagnosticLoggingClient().logDiagnosticMessage(DiagnosticLoggingKeys::webViewKey(), DiagnosticLoggingKeys::userZoomActionKey(), ShouldSample::No);
2969     m_userHasChangedPageScaleFactor = true;
2970 }
2971
2972 #if ENABLE(WEBGL)
2973 WebCore::WebGLLoadPolicy WebPage::webGLPolicyForURL(WebFrame*, const URL&)
2974 {
2975     return WebGLAllowCreation;
2976 }
2977
2978 WebCore::WebGLLoadPolicy WebPage::resolveWebGLPolicyForURL(WebFrame*, const URL&)
2979 {
2980     return WebGLAllowCreation;
2981 }
2982 #endif
2983
2984 #if ENABLE(IOS_TOUCH_EVENTS)
2985 void WebPage::dispatchAsynchronousTouchEvents(const Vector<WebTouchEvent, 1>& queue)
2986 {
2987     bool ignored;
2988     for (const WebTouchEvent& event : queue)
2989         dispatchTouchEvent(event, ignored);
2990 }
2991 #endif
2992
2993 void WebPage::computePagesForPrintingAndDrawToPDF(uint64_t frameID, const PrintInfo& printInfo, CallbackID callbackID, Ref<Messages::WebPage::ComputePagesForPrintingAndDrawToPDF::DelayedReply>&& reply)
2994 {
2995     if (printInfo.snapshotFirstPage) {
2996         reply->send(1);
2997         IntSize snapshotSize { FloatSize { printInfo.availablePaperWidth, printInfo.availablePaperHeight } };
2998         IntRect snapshotRect { {0, 0}, snapshotSize };
2999         auto pdfData = pdfSnapshotAtSize(snapshotRect, snapshotSize, 0);
3000         send(Messages::WebPageProxy::DrawToPDFCallback(IPC::DataReference(CFDataGetBytePtr(pdfData.get()), CFDataGetLength(pdfData.get())), callbackID));
3001         return;
3002     }
3003
3004     Vector<WebCore::IntRect> pageRects;
3005     double totalScaleFactor;
3006     computePagesForPrintingImpl(frameID, printInfo, pageRects, totalScaleFactor);
3007
3008     ASSERT(pageRects.size() >= 1);
3009     std::size_t pageCount = pageRects.size();
3010     ASSERT(pageCount <= std::numeric_limits<uint32_t>::max());
3011     reply->send(pageCount);
3012
3013     RetainPtr<CFMutableDataRef> pdfPageData;
3014     drawPagesToPDFImpl(frameID, printInfo, 0, pageCount, pdfPageData);
3015     send(Messages::WebPageProxy::DrawToPDFCallback(IPC::DataReference(CFDataGetBytePtr(pdfPageData.get()), CFDataGetLength(pdfPageData.get())), callbackID));
3016
3017     endPrinting();
3018 }
3019
3020 void WebPage::contentSizeCategoryDidChange(const String& contentSizeCategory)
3021 {
3022     RenderThemeIOS::setContentSizeCategory(contentSizeCategory);
3023     Page::updateStyleForAllPagesAfterGlobalChangeInEnvironment();
3024 }
3025
3026 String WebPage::platformUserAgent(const URL&) const
3027 {
3028     return String();
3029 }
3030
3031 #if USE(QUICK_LOOK)
3032 void WebPage::didReceivePasswordForQuickLookDocument(const String& password)
3033 {
3034     WebPreviewLoaderClient::didReceivePassword(password, m_pageID);
3035 }
3036 #endif
3037
3038 bool WebPage::platformPrefersTextLegibilityBasedZoomScaling() const
3039 {
3040 #if ENABLE(EXTRA_ZOOM_MODE)
3041     return true;
3042 #else
3043     return false;
3044 #endif
3045 }
3046
3047 #if ENABLE(MEDIA_STREAM)
3048 void WebPage::prepareToSendUserMediaPermissionRequest()
3049 {
3050     static std::once_flag once;
3051     std::call_once(once, [] {
3052         if (!canLoadAVSystemController_PIDToInheritApplicationStateFrom())
3053             return;
3054
3055         NSError *error = nil;
3056         [[getAVSystemControllerClass() sharedAVSystemController] setAttribute:@(WebCore::presentingApplicationPID()) forKey:getAVSystemController_PIDToInheritApplicationStateFrom() error:&error];
3057         if (error)
3058             WTFLogAlways("Failed to set up PID proxying: %s", error.localizedDescription.UTF8String);
3059     });
3060 }
3061 #endif
3062
3063 } // namespace WebKit
3064
3065 #endif // PLATFORM(IOS)