Add the ability for tests to run script in the UI process in WebKitTestRunner
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / Cocoa / WKWebView.mm
1 /*
2  * Copyright (C) 2014, 2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. 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 "WKWebViewInternal.h"
28
29 #if WK_API_ENABLED
30
31 #import "APIFormClient.h"
32 #import "APIPageConfiguration.h"
33 #import "APISerializedScriptValue.h"
34 #import "CompletionHandlerCallChecker.h"
35 #import "DiagnosticLoggingClient.h"
36 #import "FindClient.h"
37 #import "LegacySessionStateCoding.h"
38 #import "NavigationState.h"
39 #import "RemoteLayerTreeScrollingPerformanceData.h"
40 #import "RemoteLayerTreeTransaction.h"
41 #import "RemoteObjectRegistry.h"
42 #import "RemoteObjectRegistryMessages.h"
43 #import "UIDelegate.h"
44 #import "ViewGestureController.h"
45 #import "ViewSnapshotStore.h"
46 #import "WKBackForwardListInternal.h"
47 #import "WKBackForwardListItemInternal.h"
48 #import "WKBrowsingContextHandleInternal.h"
49 #import "WKErrorInternal.h"
50 #import "WKHistoryDelegatePrivate.h"
51 #import "WKLayoutMode.h"
52 #import "WKNSData.h"
53 #import "WKNSURLExtras.h"
54 #import "WKNavigationDelegate.h"
55 #import "WKNavigationInternal.h"
56 #import "WKPreferencesInternal.h"
57 #import "WKProcessPoolInternal.h"
58 #import "WKSharedAPICast.h"
59 #import "WKUIDelegate.h"
60 #import "WKUIDelegatePrivate.h"
61 #import "WKUserContentControllerInternal.h"
62 #import "WKWebViewConfigurationInternal.h"
63 #import "WKWebViewContentProvider.h"
64 #import "WKWebsiteDataStoreInternal.h"
65 #import "WebBackForwardList.h"
66 #import "WebCertificateInfo.h"
67 #import "WebFormSubmissionListenerProxy.h"
68 #import "WebKitSystemInterface.h"
69 #import "WebPageGroup.h"
70 #import "WebPageProxy.h"
71 #import "WebPreferencesKeys.h"
72 #import "WebProcessPool.h"
73 #import "WebProcessProxy.h"
74 #import "_WKDiagnosticLoggingDelegate.h"
75 #import "_WKFindDelegate.h"
76 #import "_WKFormDelegate.h"
77 #import "_WKRemoteObjectRegistryInternal.h"
78 #import "_WKSessionStateInternal.h"
79 #import "_WKVisitedLinkStoreInternal.h"
80 #import <WebCore/IOSurface.h>
81 #import <wtf/HashMap.h>
82 #import <wtf/MathExtras.h>
83 #import <wtf/NeverDestroyed.h>
84 #import <wtf/Optional.h>
85 #import <wtf/RetainPtr.h>
86
87 #if PLATFORM(IOS)
88 #import "_WKFrameHandleInternal.h"
89 #import "_WKWebViewPrintFormatter.h"
90 #import "PrintInfo.h"
91 #import "ProcessThrottler.h"
92 #import "RemoteLayerTreeDrawingAreaProxy.h"
93 #import "RemoteScrollingCoordinatorProxy.h"
94 #import "UIKitSPI.h"
95 #import "WKContentViewInteraction.h"
96 #import "WKPDFView.h"
97 #import "WKScrollView.h"
98 #import "WKWebViewContentProviderRegistry.h"
99 #import "WebPageMessages.h"
100 #import "WebVideoFullscreenManagerProxy.h"
101 #import <UIKit/UIApplication.h>
102 #import <WebCore/CoreGraphicsSPI.h>
103 #import <WebCore/FrameLoaderTypes.h>
104 #import <WebCore/InspectorOverlay.h>
105 #import <WebCore/QuartzCoreSPI.h>
106
107 @interface UIScrollView (UIScrollViewInternal)
108 - (void)_adjustForAutomaticKeyboardInfo:(NSDictionary*)info animated:(BOOL)animated lastAdjustment:(CGFloat*)lastAdjustment;
109 - (BOOL)_isScrollingToTop;
110 - (BOOL)_isInterruptingDeceleration;
111 - (CGPoint)_animatedTargetOffset;
112 @end
113
114 @interface UIPeripheralHost(UIKitInternal)
115 - (CGFloat)getVerticalOverlapForView:(UIView *)view usingKeyboardInfo:(NSDictionary *)info;
116 @end
117
118 @interface UIView (UIViewInternal)
119 - (UIViewController *)_viewControllerForAncestor;
120 @end
121
122 @interface UIWindow (UIWindowInternal)
123 - (BOOL)_isHostedInAnotherProcess;
124 @end
125
126 @interface UIViewController (UIViewControllerInternal)
127 - (UIViewController *)_rootAncestorViewController;
128 - (UIViewController *)_viewControllerForSupportedInterfaceOrientations;
129 @end
130
131 enum class DynamicViewportUpdateMode {
132     NotResizing,
133     ResizingWithAnimation,
134     ResizingWithDocumentHidden,
135 };
136
137 #endif
138
139 #if PLATFORM(MAC)
140 #import "WKViewInternal.h"
141 #import <WebCore/ColorMac.h>
142 #endif
143
144 static HashMap<WebKit::WebPageProxy*, WKWebView *>& pageToViewMap()
145 {
146     static NeverDestroyed<HashMap<WebKit::WebPageProxy*, WKWebView *>> map;
147     return map;
148 }
149
150 WKWebView* fromWebPageProxy(WebKit::WebPageProxy& page)
151 {
152     return pageToViewMap().get(&page);
153 }
154
155 @implementation WKWebView {
156     std::unique_ptr<WebKit::NavigationState> _navigationState;
157     std::unique_ptr<WebKit::UIDelegate> _uiDelegate;
158
159     RetainPtr<_WKRemoteObjectRegistry> _remoteObjectRegistry;
160     _WKRenderingProgressEvents _observedRenderingProgressEvents;
161
162     WebKit::WeakObjCPtr<id <_WKFormDelegate>> _formDelegate;
163 #if PLATFORM(IOS)
164     RetainPtr<WKScrollView> _scrollView;
165     RetainPtr<WKContentView> _contentView;
166
167     BOOL _overridesMinimumLayoutSize;
168     CGSize _minimumLayoutSizeOverride;
169     BOOL _overridesMaximumUnobscuredSize;
170     CGSize _maximumUnobscuredSizeOverride;
171     CGRect _inputViewBounds;
172     CGFloat _viewportMetaTagWidth;
173     BOOL _allowsLinkPreview;
174
175     UIEdgeInsets _obscuredInsets;
176     BOOL _haveSetObscuredInsets;
177     BOOL _isChangingObscuredInsetsInteractively;
178
179     UIInterfaceOrientation _interfaceOrientationOverride;
180     BOOL _overridesInterfaceOrientation;
181
182     BOOL _hasCommittedLoadForMainFrame;
183     BOOL _needsResetViewStateAfterCommitLoadForMainFrame;
184     uint64_t _firstPaintAfterCommitLoadTransactionID;
185     DynamicViewportUpdateMode _dynamicViewportUpdateMode;
186     CATransform3D _resizeAnimationTransformAdjustments;
187     uint64_t _resizeAnimationTransformTransactionID;
188     RetainPtr<UIView> _resizeAnimationView;
189     CGFloat _lastAdjustmentForScroller;
190     Optional<CGRect> _frozenVisibleContentRect;
191     Optional<CGRect> _frozenUnobscuredContentRect;
192
193     BOOL _needsToRestoreExposedRect;
194     WebCore::FloatRect _exposedRectToRestore;
195     BOOL _needsToRestoreUnobscuredCenter;
196     WebCore::FloatPoint _unobscuredCenterToRestore;
197     uint64_t _firstTransactionIDAfterPageRestore;
198     double _scaleToRestore;
199
200     std::unique_ptr<WebKit::ViewGestureController> _gestureController;
201     BOOL _allowsBackForwardNavigationGestures;
202
203     RetainPtr<UIView <WKWebViewContentProvider>> _customContentView;
204     RetainPtr<UIView> _customContentFixedOverlayView;
205
206     WebCore::Color _scrollViewBackgroundColor;
207
208     BOOL _delayUpdateVisibleContentRects;
209     BOOL _hadDelayedUpdateVisibleContentRects;
210
211     BOOL _pageIsPrintingToPDF;
212     RetainPtr<CGPDFDocumentRef> _printedDocument;
213     Vector<std::function<void ()>> _snapshotsDeferredDuringResize;
214 #endif
215 #if PLATFORM(MAC)
216     RetainPtr<WKView> _wkView;
217 #endif
218 }
219
220 - (instancetype)initWithFrame:(CGRect)frame
221 {
222     return [self initWithFrame:frame configuration:adoptNS([[WKWebViewConfiguration alloc] init]).get()];
223 }
224
225 #if PLATFORM(IOS)
226 static int32_t deviceOrientationForUIInterfaceOrientation(UIInterfaceOrientation orientation)
227 {
228     switch (orientation) {
229     case UIInterfaceOrientationUnknown:
230     case UIInterfaceOrientationPortrait:
231         return 0;
232     case UIInterfaceOrientationPortraitUpsideDown:
233         return 180;
234     case UIInterfaceOrientationLandscapeLeft:
235         return -90;
236     case UIInterfaceOrientationLandscapeRight:
237         return 90;
238     }
239 }
240
241 static int32_t deviceOrientation()
242 {
243     return deviceOrientationForUIInterfaceOrientation([[UIApplication sharedApplication] statusBarOrientation]);
244 }
245
246 - (BOOL)_isShowingVideoPictureInPicture
247 {
248     if (!_page || !_page->videoFullscreenManager())
249         return false;
250     
251     return _page->videoFullscreenManager()->hasMode(WebCore::HTMLMediaElementEnums::VideoFullscreenModePictureInPicture);
252 }
253
254 - (BOOL)_mayAutomaticallyShowVideoPictureInPicture
255 {
256 #if (__IPHONE_OS_VERSION_MIN_REQUIRED <= 80200) || !HAVE(AVKIT)
257     return false;
258 #else
259     if (!_page || !_page->videoFullscreenManager())
260         return false;
261
262     return _page->videoFullscreenManager()->mayAutomaticallyShowVideoPictureInPicture();
263 #endif
264 }
265
266 static bool shouldAllowPictureInPictureMediaPlayback()
267 {
268 #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 90000
269     static bool shouldAllowPictureInPictureMediaPlayback = iosExecutableWasLinkedOnOrAfterVersion(wkIOSSystemVersion_9_0);
270     return shouldAllowPictureInPictureMediaPlayback;
271 #else
272     return false;
273 #endif
274 }
275
276 #endif
277
278 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
279 {
280     if (!(self = [super initWithFrame:frame]))
281         return nil;
282
283     if (!configuration)
284         [NSException raise:NSInvalidArgumentException format:@"Configuration cannot be nil"];
285
286     _configuration = adoptNS([configuration copy]);
287
288     if (WKWebView *relatedWebView = [_configuration _relatedWebView]) {
289         WKProcessPool *processPool = [_configuration processPool];
290         WKProcessPool *relatedWebViewProcessPool = [relatedWebView->_configuration processPool];
291         if (processPool && processPool != relatedWebViewProcessPool)
292             [NSException raise:NSInvalidArgumentException format:@"Related web view %@ has process pool %@ but configuration specifies a different process pool %@", relatedWebView, relatedWebViewProcessPool, configuration.processPool];
293
294         [_configuration setProcessPool:relatedWebViewProcessPool];
295     }
296
297     [_configuration _validate];
298
299     CGRect bounds = self.bounds;
300
301     WebKit::WebProcessPool& processPool = *[_configuration processPool]->_processPool;
302     
303     auto pageConfiguration = API::PageConfiguration::create();
304
305     pageConfiguration->setProcessPool(&processPool);
306     pageConfiguration->setPreferences([_configuration preferences]->_preferences.get());
307     if (WKWebView *relatedWebView = [_configuration _relatedWebView])
308         pageConfiguration->setRelatedPage(relatedWebView->_page.get());
309
310     pageConfiguration->setUserContentController([_configuration userContentController]->_userContentControllerProxy.get());
311     pageConfiguration->setVisitedLinkStore([_configuration _visitedLinkStore]->_visitedLinkStore.get());
312     pageConfiguration->setWebsiteDataStore([_configuration websiteDataStore]->_websiteDataStore.get());
313     pageConfiguration->setTreatsSHA1SignedCertificatesAsInsecure([_configuration _treatsSHA1SignedCertificatesAsInsecure]);
314
315     RefPtr<WebKit::WebPageGroup> pageGroup;
316     NSString *groupIdentifier = configuration._groupIdentifier;
317     if (groupIdentifier.length) {
318         pageGroup = WebKit::WebPageGroup::create(configuration._groupIdentifier);
319         pageConfiguration->setPageGroup(pageGroup.get());
320     }
321
322     pageConfiguration->preferenceValues().set(WebKit::WebPreferencesKey::suppressesIncrementalRenderingKey(), WebKit::WebPreferencesStore::Value(!![_configuration suppressesIncrementalRendering]));
323
324 #if PLATFORM(IOS)
325     pageConfiguration->setAlwaysRunsAtForegroundPriority([_configuration _alwaysRunsAtForegroundPriority]);
326
327     pageConfiguration->preferenceValues().set(WebKit::WebPreferencesKey::allowsInlineMediaPlaybackKey(), WebKit::WebPreferencesStore::Value(!![_configuration allowsInlineMediaPlayback]));
328     pageConfiguration->preferenceValues().set(WebKit::WebPreferencesKey::inlineMediaPlaybackRequiresPlaysInlineAttributeKey(), WebKit::WebPreferencesStore::Value(!![_configuration _inlineMediaPlaybackRequiresPlaysInlineAttribute]));
329     pageConfiguration->preferenceValues().set(WebKit::WebPreferencesKey::allowsPictureInPictureMediaPlaybackKey(), WebKit::WebPreferencesStore::Value(!![_configuration allowsPictureInPictureMediaPlayback] && shouldAllowPictureInPictureMediaPlayback()));
330     pageConfiguration->preferenceValues().set(WebKit::WebPreferencesKey::requiresUserGestureForMediaPlaybackKey(), WebKit::WebPreferencesStore::Value(!![_configuration requiresUserActionForMediaPlayback]));
331     pageConfiguration->preferenceValues().set(WebKit::WebPreferencesKey::mediaDataLoadsAutomaticallyKey(), WebKit::WebPreferencesStore::Value(!![_configuration _mediaDataLoadsAutomatically]));
332 #endif
333 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
334     pageConfiguration->preferenceValues().set(WebKit::WebPreferencesKey::allowsAirPlayForMediaPlaybackKey(), WebKit::WebPreferencesStore::Value(!![_configuration allowsAirPlayForMediaPlayback]));
335 #endif
336
337 #if PLATFORM(IOS)
338     _scrollView = adoptNS([[WKScrollView alloc] initWithFrame:bounds]);
339     [_scrollView setInternalDelegate:self];
340     [_scrollView setBouncesZoom:YES];
341
342     [self addSubview:_scrollView.get()];
343
344     _contentView = adoptNS([[WKContentView alloc] initWithFrame:bounds processPool:processPool configuration:WTF::move(pageConfiguration) webView:self]);
345
346     _page = [_contentView page];
347     _page->setDeviceOrientation(deviceOrientation());
348
349     [_contentView layer].anchorPoint = CGPointZero;
350     [_contentView setFrame:bounds];
351     [_scrollView addSubview:_contentView.get()];
352     [_scrollView addSubview:[_contentView unscaledView]];
353     [self _updateScrollViewBackground];
354
355     _viewportMetaTagWidth = -1;
356
357     [self _frameOrBoundsChanged];
358
359     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
360     [center addObserver:self selector:@selector(_keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
361     [center addObserver:self selector:@selector(_keyboardDidChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
362     [center addObserver:self selector:@selector(_keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
363     [center addObserver:self selector:@selector(_keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
364     [center addObserver:self selector:@selector(_windowDidRotate:) name:UIWindowDidRotateNotification object:nil];
365     [center addObserver:self selector:@selector(_contentSizeCategoryDidChange:) name:UIContentSizeCategoryDidChangeNotification object:nil];
366     _page->contentSizeCategoryDidChange([self _contentSizeCategory]);
367
368     [[_configuration _contentProviderRegistry] addPage:*_page];
369 #endif
370
371 #if PLATFORM(MAC)
372     _wkView = adoptNS([[WKView alloc] initWithFrame:bounds processPool:processPool configuration:WTF::move(pageConfiguration) webView:self]);
373     [self addSubview:_wkView.get()];
374     _page = WebKit::toImpl([_wkView pageRef]);
375
376 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
377     [_wkView _setAutomaticallyAdjustsContentInsets:YES];
378 #endif
379 #endif
380
381     _page->setBackgroundExtendsBeyondPage(true);
382
383     if (NSString *applicationNameForUserAgent = configuration.applicationNameForUserAgent)
384         _page->setApplicationNameForUserAgent(applicationNameForUserAgent);
385
386     _navigationState = std::make_unique<WebKit::NavigationState>(self);
387     _uiDelegate = std::make_unique<WebKit::UIDelegate>(self);
388     _page->setFindClient(std::make_unique<WebKit::FindClient>(self));
389     _page->setDiagnosticLoggingClient(std::make_unique<WebKit::DiagnosticLoggingClient>(self));
390
391     pageToViewMap().add(_page.get(), self);
392
393     return self;
394 }
395
396 - (instancetype)initWithCoder:(NSCoder *)coder
397 {
398     [self release];
399     return nil;
400 }
401
402 - (void)dealloc
403 {
404     if (_remoteObjectRegistry)
405         _page->process().processPool().removeMessageReceiver(Messages::RemoteObjectRegistry::messageReceiverName(), _page->pageID());
406
407     _page->close();
408
409     [_remoteObjectRegistry _invalidate];
410 #if PLATFORM(IOS)
411     [[_configuration _contentProviderRegistry] removePage:*_page];
412     [[NSNotificationCenter defaultCenter] removeObserver:self];
413     [_scrollView setInternalDelegate:nil];
414 #endif
415
416     pageToViewMap().remove(_page.get());
417
418     [super dealloc];
419 }
420
421 - (WKWebViewConfiguration *)configuration
422 {
423     return [[_configuration copy] autorelease];
424 }
425
426 - (WKBackForwardList *)backForwardList
427 {
428     return wrapper(_page->backForwardList());
429 }
430
431 - (id <WKNavigationDelegate>)navigationDelegate
432 {
433     return _navigationState->navigationDelegate().autorelease();
434 }
435
436 - (void)setNavigationDelegate:(id <WKNavigationDelegate>)navigationDelegate
437 {
438     _page->setNavigationClient(_navigationState->createNavigationClient());
439     _navigationState->setNavigationDelegate(navigationDelegate);
440 }
441
442 - (id <WKUIDelegate>)UIDelegate
443 {
444     return _uiDelegate->delegate().autorelease();
445 }
446
447 - (void)setUIDelegate:(id<WKUIDelegate>)UIDelegate
448 {
449     _page->setUIClient(_uiDelegate->createUIClient());
450     _uiDelegate->setDelegate(UIDelegate);
451 }
452
453 - (WKNavigation *)loadRequest:(NSURLRequest *)request
454 {
455     auto navigation = _page->loadRequest(request);
456     if (!navigation)
457         return nil;
458
459     return [wrapper(*navigation.release().leakRef()) autorelease];
460 }
461
462 - (WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL
463 {
464     if (![URL isFileURL])
465         [NSException raise:NSInvalidArgumentException format:@"%@ is not a file URL", URL];
466
467     if (![readAccessURL isFileURL])
468         [NSException raise:NSInvalidArgumentException format:@"%@ is not a file URL", readAccessURL];
469
470     auto navigation = _page->loadFile([URL _web_originalDataAsWTFString], [readAccessURL _web_originalDataAsWTFString]);
471     if (!navigation)
472         return nil;
473
474     return [wrapper(*navigation.release().leakRef()) autorelease];
475 }
476
477 - (WKNavigation *)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
478 {
479     NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
480
481     return [self loadData:data MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:baseURL];
482 }
483
484 - (WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL
485 {
486     auto navigation = _page->loadData(API::Data::createWithoutCopying(data).ptr(), MIMEType, characterEncodingName, baseURL.absoluteString);
487     if (!navigation)
488         return nil;
489
490     return [wrapper(*navigation.release().leakRef()) autorelease];
491 }
492
493 - (WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item
494 {
495     auto navigation = _page->goToBackForwardItem(&item._item);
496     if (!navigation)
497         return nil;
498
499     return [wrapper(*navigation.release().leakRef()) autorelease];
500 }
501
502 - (NSString *)title
503 {
504     return _page->pageLoadState().title();
505 }
506
507 - (NSURL *)URL
508 {
509     return [NSURL _web_URLWithWTFString:_page->pageLoadState().activeURL()];
510 }
511
512 - (BOOL)isLoading
513 {
514     return _page->pageLoadState().isLoading();
515 }
516
517 - (double)estimatedProgress
518 {
519     return _page->pageLoadState().estimatedProgress();
520 }
521
522 - (BOOL)hasOnlySecureContent
523 {
524     return _page->pageLoadState().hasOnlySecureContent();
525 }
526
527 - (NSArray *)certificateChain
528 {
529     auto certificateInfo = _page->pageLoadState().certificateInfo();
530     if (!certificateInfo)
531         return @[ ];
532
533     return (NSArray *)certificateInfo->certificateInfo().certificateChain() ?: @[ ];
534 }
535
536 - (BOOL)canGoBack
537 {
538     return _page->pageLoadState().canGoBack();
539 }
540
541 - (BOOL)canGoForward
542 {
543     return _page->pageLoadState().canGoForward();
544 }
545
546 - (WKNavigation *)goBack
547 {
548     auto navigation = _page->goBack();
549     if (!navigation)
550         return nil;
551  
552     return [wrapper(*navigation.release().leakRef()) autorelease];
553 }
554
555 - (WKNavigation *)goForward
556 {
557     auto navigation = _page->goForward();
558     if (!navigation)
559         return nil;
560
561     return [wrapper(*navigation.release().leakRef()) autorelease];
562 }
563
564 - (WKNavigation *)reload
565 {
566     auto navigation = _page->reload(false);
567     if (!navigation)
568         return nil;
569
570     return [wrapper(*navigation.release().leakRef()) autorelease];
571 }
572
573 - (WKNavigation *)reloadFromOrigin
574 {
575     auto navigation = _page->reload(true);
576     if (!navigation)
577         return nil;
578
579     return [wrapper(*navigation.release().leakRef()) autorelease];
580 }
581
582 - (void)stopLoading
583 {
584     _page->stopLoading();
585 }
586
587 static WKErrorCode callbackErrorCode(WebKit::CallbackBase::Error error)
588 {
589     switch (error) {
590     case WebKit::CallbackBase::Error::None:
591         ASSERT_NOT_REACHED();
592         return WKErrorUnknown;
593
594     case WebKit::CallbackBase::Error::Unknown:
595         return WKErrorUnknown;
596
597     case WebKit::CallbackBase::Error::ProcessExited:
598         return WKErrorWebContentProcessTerminated;
599
600     case WebKit::CallbackBase::Error::OwnerWasInvalidated:
601         return WKErrorWebViewInvalidated;
602     }
603 }
604
605 - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id, NSError *))completionHandler
606 {
607     auto handler = adoptNS([completionHandler copy]);
608
609     _page->runJavaScriptInMainFrame(javaScriptString, [handler](API::SerializedScriptValue* serializedScriptValue, bool hadException, WebKit::ScriptValueCallback::Error errorCode) {
610         if (!handler)
611             return;
612
613         if (errorCode != WebKit::ScriptValueCallback::Error::None) {
614             auto error = createNSError(callbackErrorCode(errorCode));
615             if (errorCode == WebKit::ScriptValueCallback::Error::OwnerWasInvalidated) {
616                 // The OwnerWasInvalidated callback is synchronous. We don't want to call the block from within it
617                 // because that can trigger re-entrancy bugs in WebKit.
618                 // FIXME: It would be even better if GenericCallback did this for us.
619                 dispatch_async(dispatch_get_main_queue(), [handler, error] {
620                     auto rawHandler = (void (^)(id, NSError *))handler.get();
621                     rawHandler(nil, error.get());
622                 });
623                 return;
624             }
625
626             auto rawHandler = (void (^)(id, NSError *))handler.get();
627             rawHandler(nil, error.get());
628             return;
629         }
630
631         auto rawHandler = (void (^)(id, NSError *))handler.get();
632         if (hadException) {
633             ASSERT(!serializedScriptValue);
634             rawHandler(nil, createNSError(WKErrorJavaScriptExceptionOccurred).get());
635             return;
636         }
637
638         if (!serializedScriptValue) {
639             rawHandler(nil, createNSError(WKErrorJavaScriptResultTypeIsUnsupported).get());
640             return;
641         }
642
643         id body = API::SerializedScriptValue::deserialize(*serializedScriptValue->internalRepresentation(), 0);
644         rawHandler(body, nil);
645     });
646 }
647
648 - (NSString *)customUserAgent
649 {
650     return _page->customUserAgent();
651 }
652
653 - (void)setCustomUserAgent:(NSString *)customUserAgent
654 {
655     _page->setCustomUserAgent(customUserAgent);
656 }
657
658 - (WKPageRef)_pageForTesting
659 {
660     return toAPI(_page.get());
661 }
662
663 - (BOOL)allowsLinkPreview
664 {
665 #if PLATFORM(MAC)
666     return [_wkView allowsLinkPreview];
667 #elif PLATFORM(IOS)
668     return _allowsLinkPreview;
669 #endif
670 }
671
672 - (void)setAllowsLinkPreview:(BOOL)allowsLinkPreview
673 {
674 #if PLATFORM(MAC)
675     [_wkView setAllowsLinkPreview:allowsLinkPreview];
676     return;
677 #elif PLATFORM(IOS)
678     if (_allowsLinkPreview == allowsLinkPreview)
679         return;
680
681     _allowsLinkPreview = allowsLinkPreview;
682
683 #if HAVE(LINK_PREVIEW)
684     if (_allowsLinkPreview)
685         [_contentView _registerPreview];
686     else
687         [_contentView _unregisterPreview];
688 #endif // HAVE(LINK_PREVIEW)
689 #endif // PLATFORM(IOS)
690 }
691
692 #pragma mark iOS-specific methods
693
694 #if PLATFORM(IOS)
695 - (void)setFrame:(CGRect)frame
696 {
697     CGRect oldFrame = self.frame;
698     [super setFrame:frame];
699
700     if (!CGSizeEqualToSize(oldFrame.size, frame.size))
701         [self _frameOrBoundsChanged];
702 }
703
704 - (void)setBounds:(CGRect)bounds
705 {
706     CGRect oldBounds = self.bounds;
707     [super setBounds:bounds];
708     [_customContentFixedOverlayView setFrame:self.bounds];
709
710     if (!CGSizeEqualToSize(oldBounds.size, bounds.size))
711         [self _frameOrBoundsChanged];
712 }
713
714 - (UIScrollView *)scrollView
715 {
716     return _scrollView.get();
717 }
718
719 - (WKBrowsingContextController *)browsingContextController
720 {
721     return [_contentView browsingContextController];
722 }
723
724 - (BOOL)becomeFirstResponder
725 {
726     return [_contentView becomeFirstResponder] || [super becomeFirstResponder];
727 }
728
729 static inline CGFloat floorToDevicePixel(CGFloat input, float deviceScaleFactor)
730 {
731     return CGFloor(input * deviceScaleFactor) / deviceScaleFactor;
732 }
733
734 static inline bool pointsEqualInDevicePixels(CGPoint a, CGPoint b, float deviceScaleFactor)
735 {
736     return fabs(a.x * deviceScaleFactor - b.x * deviceScaleFactor) < std::numeric_limits<float>::epsilon()
737         && fabs(a.y * deviceScaleFactor - b.y * deviceScaleFactor) < std::numeric_limits<float>::epsilon();
738 }
739
740 static CGSize roundScrollViewContentSize(const WebKit::WebPageProxy& page, CGSize contentSize)
741 {
742     float deviceScaleFactor = page.deviceScaleFactor();
743     return CGSizeMake(floorToDevicePixel(contentSize.width, deviceScaleFactor), floorToDevicePixel(contentSize.height, deviceScaleFactor));
744 }
745
746 - (UIView *)_currentContentView
747 {
748     return _customContentView ? _customContentView.get() : _contentView.get();
749 }
750
751 - (void)_setHasCustomContentView:(BOOL)pageHasCustomContentView loadedMIMEType:(const WTF::String&)mimeType
752 {
753     if (pageHasCustomContentView) {
754         [_customContentView removeFromSuperview];
755         [_customContentFixedOverlayView removeFromSuperview];
756
757         Class representationClass = [[_configuration _contentProviderRegistry] providerForMIMEType:mimeType];
758         ASSERT(representationClass);
759         _customContentView = adoptNS([[representationClass alloc] web_initWithFrame:self.bounds webView:self]);
760         _customContentFixedOverlayView = adoptNS([[UIView alloc] initWithFrame:self.bounds]);
761         [_customContentFixedOverlayView setUserInteractionEnabled:NO];
762
763         [[_contentView unscaledView] removeFromSuperview];
764         [_contentView removeFromSuperview];
765         [_scrollView addSubview:_customContentView.get()];
766         [self addSubview:_customContentFixedOverlayView.get()];
767
768         [_customContentView web_setMinimumSize:self.bounds.size];
769         [_customContentView web_setFixedOverlayView:_customContentFixedOverlayView.get()];
770     } else if (_customContentView) {
771         [_customContentView removeFromSuperview];
772         _customContentView = nullptr;
773
774         [_customContentFixedOverlayView removeFromSuperview];
775         _customContentFixedOverlayView = nullptr;
776
777         [_scrollView addSubview:_contentView.get()];
778         [_scrollView addSubview:[_contentView unscaledView]];
779         [_scrollView setContentSize:roundScrollViewContentSize(*_page, [_contentView frame].size)];
780
781         [_customContentFixedOverlayView setFrame:self.bounds];
782         [self addSubview:_customContentFixedOverlayView.get()];
783     }
784 }
785
786 - (void)_didFinishLoadingDataForCustomContentProviderWithSuggestedFilename:(const String&)suggestedFilename data:(NSData *)data
787 {
788     ASSERT(_customContentView);
789     [_customContentView web_setContentProviderData:data suggestedFilename:suggestedFilename];
790
791     // FIXME: It may make more sense for custom content providers to invoke this when they're ready,
792     // because there's no guarantee that all custom content providers will lay out synchronously.
793     _page->didLayoutForCustomContentProvider();
794 }
795
796 - (void)_setViewportMetaTagWidth:(float)newWidth
797 {
798     _viewportMetaTagWidth = newWidth;
799 }
800
801 - (void)_willInvokeUIScrollViewDelegateCallback
802 {
803     _delayUpdateVisibleContentRects = YES;
804 }
805
806 - (void)_didInvokeUIScrollViewDelegateCallback
807 {
808     _delayUpdateVisibleContentRects = NO;
809     if (_hadDelayedUpdateVisibleContentRects) {
810         _hadDelayedUpdateVisibleContentRects = NO;
811         [self _updateVisibleContentRects];
812     }
813 }
814
815 static CGFloat contentZoomScale(WKWebView *webView)
816 {
817     CGFloat scale = webView._currentContentView.layer.affineTransform.a;
818     ASSERT(scale == [webView->_scrollView zoomScale]);
819     return scale;
820 }
821
822 static WebCore::Color baseScrollViewBackgroundColor(WKWebView *webView)
823 {
824     if (webView->_customContentView)
825         return [webView->_customContentView backgroundColor].CGColor;
826
827     if (webView->_gestureController) {
828         WebCore::Color color = webView->_gestureController->backgroundColorForCurrentSnapshot();
829         if (color.isValid())
830             return color;
831     }
832
833     return webView->_page->pageExtendedBackgroundColor();
834 }
835
836 static WebCore::Color scrollViewBackgroundColor(WKWebView *webView)
837 {
838     if (!webView.opaque)
839         return WebCore::Color::transparent;
840
841     WebCore::Color color = baseScrollViewBackgroundColor(webView);
842
843     if (!color.isValid())
844         color = WebCore::Color::white;
845
846     CGFloat zoomScale = contentZoomScale(webView);
847     CGFloat minimumZoomScale = [webView->_scrollView minimumZoomScale];
848     if (zoomScale < minimumZoomScale) {
849         CGFloat slope = 12;
850         CGFloat opacity = std::max<CGFloat>(1 - slope * (minimumZoomScale - zoomScale), 0);
851         color = WebCore::colorWithOverrideAlpha(color.rgb(), opacity);
852     }
853
854     return color;
855 }
856
857 - (void)_updateScrollViewBackground
858 {
859     WebCore::Color color = scrollViewBackgroundColor(self);
860
861     if (_scrollViewBackgroundColor == color)
862         return;
863
864     _scrollViewBackgroundColor = color;
865
866     auto uiBackgroundColor = adoptNS([[UIColor alloc] initWithCGColor:cachedCGColor(color, WebCore::ColorSpaceDeviceRGB)]);
867     [_scrollView setBackgroundColor:uiBackgroundColor.get()];
868
869     // Update the indicator style based on the lightness/darkness of the background color.
870     double hue, saturation, lightness;
871     color.getHSL(hue, saturation, lightness);
872     if (lightness <= .5 && color.alpha() > 0)
873         [_scrollView setIndicatorStyle:UIScrollViewIndicatorStyleWhite];
874     else
875         [_scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault];
876 }
877
878 - (CGPoint)_adjustedContentOffset:(CGPoint)point
879 {
880     CGPoint result = point;
881     UIEdgeInsets contentInset = [self _computedContentInset];
882
883     result.x -= contentInset.left;
884     result.y -= contentInset.top;
885
886     return result;
887 }
888
889 - (UIEdgeInsets)_computedContentInset
890 {
891     if (_haveSetObscuredInsets)
892         return _obscuredInsets;
893
894     return [_scrollView contentInset];
895 }
896
897 - (void)_processDidExit
898 {
899     if (!_customContentView && _dynamicViewportUpdateMode != DynamicViewportUpdateMode::NotResizing) {
900         NSUInteger indexOfResizeAnimationView = [[_scrollView subviews] indexOfObject:_resizeAnimationView.get()];
901         [_scrollView insertSubview:_contentView.get() atIndex:indexOfResizeAnimationView];
902         [_scrollView insertSubview:[_contentView unscaledView] atIndex:indexOfResizeAnimationView + 1];
903         [_resizeAnimationView removeFromSuperview];
904         _resizeAnimationView = nil;
905
906         _resizeAnimationTransformAdjustments = CATransform3DIdentity;
907     }
908     [_contentView setFrame:self.bounds];
909     [_scrollView setBackgroundColor:[UIColor whiteColor]];
910     [_scrollView setContentOffset:[self _adjustedContentOffset:CGPointZero]];
911     [_scrollView setZoomScale:1];
912
913     _viewportMetaTagWidth = -1;
914     _hasCommittedLoadForMainFrame = NO;
915     _needsResetViewStateAfterCommitLoadForMainFrame = NO;
916     _dynamicViewportUpdateMode = DynamicViewportUpdateMode::NotResizing;
917     [_contentView setHidden:NO];
918     _needsToRestoreExposedRect = NO;
919     _needsToRestoreUnobscuredCenter = NO;
920     _scrollViewBackgroundColor = WebCore::Color();
921     _delayUpdateVisibleContentRects = NO;
922     _hadDelayedUpdateVisibleContentRects = NO;
923 }
924
925 - (void)_didCommitLoadForMainFrame
926 {
927     _firstPaintAfterCommitLoadTransactionID = downcast<WebKit::RemoteLayerTreeDrawingAreaProxy>(*_page->drawingArea()).nextLayerTreeTransactionID();
928
929     _hasCommittedLoadForMainFrame = YES;
930     _needsResetViewStateAfterCommitLoadForMainFrame = YES;
931 }
932
933 static CGPoint contentOffsetBoundedInValidRange(UIScrollView *scrollView, CGPoint contentOffset)
934 {
935     UIEdgeInsets contentInsets = scrollView.contentInset;
936     CGSize contentSize = scrollView.contentSize;
937     CGSize scrollViewSize = scrollView.bounds.size;
938
939     CGFloat maxHorizontalOffset = contentSize.width + contentInsets.right - scrollViewSize.width;
940     contentOffset.x = std::min(maxHorizontalOffset, contentOffset.x);
941     contentOffset.x = std::max(-contentInsets.left, contentOffset.x);
942
943     CGFloat maxVerticalOffset = contentSize.height + contentInsets.bottom - scrollViewSize.height;
944     contentOffset.y = std::min(maxVerticalOffset, contentOffset.y);
945     contentOffset.y = std::max(-contentInsets.top, contentOffset.y);
946     return contentOffset;
947 }
948
949 static void changeContentOffsetBoundedInValidRange(UIScrollView *scrollView, WebCore::FloatPoint contentOffset)
950 {
951     scrollView.contentOffset = contentOffsetBoundedInValidRange(scrollView, contentOffset);
952 }
953
954 - (WebCore::FloatRect)visibleRectInViewCoordinates
955 {
956     WebCore::FloatRect bounds = self.bounds;
957     bounds.moveBy([_scrollView contentOffset]);
958     WebCore::FloatRect contentViewBounds = [_contentView bounds];
959     bounds.intersect(contentViewBounds);
960     return bounds;
961 }
962
963 // WebCore stores the page scale factor as float instead of double. When we get a scale from WebCore,
964 // we need to ignore differences that are within a small rounding error on floats.
965 static inline bool areEssentiallyEqualAsFloat(float a, float b)
966 {
967     return WTF::areEssentiallyEqual(a, b);
968 }
969
970 - (void)_didCommitLayerTree:(const WebKit::RemoteLayerTreeTransaction&)layerTreeTransaction
971 {
972     if (_customContentView)
973         return;
974
975     if (_dynamicViewportUpdateMode != DynamicViewportUpdateMode::NotResizing) {
976         if (layerTreeTransaction.transactionID() >= _resizeAnimationTransformTransactionID) {
977             [_resizeAnimationView layer].sublayerTransform = _resizeAnimationTransformAdjustments;
978             if (_dynamicViewportUpdateMode == DynamicViewportUpdateMode::ResizingWithDocumentHidden) {
979                 [_contentView setHidden:NO];
980                 [self _endAnimatedResize];
981             }
982         }
983         return;
984     }
985
986     CGSize newContentSize = roundScrollViewContentSize(*_page, [_contentView frame].size);
987     [_scrollView _setContentSizePreservingContentOffsetDuringRubberband:newContentSize];
988
989     [_scrollView setMinimumZoomScale:layerTreeTransaction.minimumScaleFactor()];
990     [_scrollView setMaximumZoomScale:layerTreeTransaction.maximumScaleFactor()];
991     [_scrollView setZoomEnabled:layerTreeTransaction.allowsUserScaling()];
992     if (!layerTreeTransaction.scaleWasSetByUIProcess() && ![_scrollView isZooming] && ![_scrollView isZoomBouncing] && ![_scrollView _isAnimatingZoom])
993         [_scrollView setZoomScale:layerTreeTransaction.pageScaleFactor()];
994
995     [self _updateScrollViewBackground];
996
997     if (_gestureController)
998         _gestureController->setRenderTreeSize(layerTreeTransaction.renderTreeSize());
999
1000     if (_needsResetViewStateAfterCommitLoadForMainFrame && layerTreeTransaction.transactionID() >= _firstPaintAfterCommitLoadTransactionID) {
1001         _needsResetViewStateAfterCommitLoadForMainFrame = NO;
1002         [_scrollView setContentOffset:[self _adjustedContentOffset:CGPointZero]];
1003         [self _updateVisibleContentRects];
1004         if (_observedRenderingProgressEvents & _WKRenderingProgressEventFirstPaint)
1005             _navigationState->didFirstPaint();
1006     }
1007
1008     bool isTransactionAfterPageRestore = layerTreeTransaction.transactionID() >= _firstTransactionIDAfterPageRestore;
1009
1010     if (_needsToRestoreExposedRect && isTransactionAfterPageRestore) {
1011         _needsToRestoreExposedRect = NO;
1012
1013         if (areEssentiallyEqualAsFloat(contentZoomScale(self), _scaleToRestore)) {
1014             WebCore::FloatPoint exposedPosition = _exposedRectToRestore.location();
1015             exposedPosition.scale(_scaleToRestore, _scaleToRestore);
1016
1017             changeContentOffsetBoundedInValidRange(_scrollView.get(), exposedPosition);
1018             if (_gestureController)
1019                 _gestureController->didRestoreScrollPosition();
1020         }
1021         [self _updateVisibleContentRects];
1022     }
1023
1024     if (_needsToRestoreUnobscuredCenter && isTransactionAfterPageRestore) {
1025         _needsToRestoreUnobscuredCenter = NO;
1026
1027         if (areEssentiallyEqualAsFloat(contentZoomScale(self), _scaleToRestore)) {
1028             CGRect unobscuredRect = UIEdgeInsetsInsetRect(self.bounds, _obscuredInsets);
1029             WebCore::FloatSize unobscuredContentSizeAtNewScale(unobscuredRect.size.width / _scaleToRestore, unobscuredRect.size.height / _scaleToRestore);
1030             WebCore::FloatPoint topLeftInDocumentCoordinates(_unobscuredCenterToRestore.x() - unobscuredContentSizeAtNewScale.width() / 2, _unobscuredCenterToRestore.y() - unobscuredContentSizeAtNewScale.height() / 2);
1031
1032             topLeftInDocumentCoordinates.scale(_scaleToRestore, _scaleToRestore);
1033             topLeftInDocumentCoordinates.moveBy(WebCore::FloatPoint(-_obscuredInsets.left, -_obscuredInsets.top));
1034
1035             changeContentOffsetBoundedInValidRange(_scrollView.get(), topLeftInDocumentCoordinates);
1036             if (_gestureController)
1037                 _gestureController->didRestoreScrollPosition();
1038         }
1039         [self _updateVisibleContentRects];
1040     }
1041     
1042     if (WebKit::RemoteLayerTreeScrollingPerformanceData* scrollPerfData = _page->scrollingPerformanceData())
1043         scrollPerfData->didCommitLayerTree([self visibleRectInViewCoordinates]);
1044 }
1045
1046 - (void)_dynamicViewportUpdateChangedTargetToScale:(double)newScale position:(CGPoint)newScrollPosition nextValidLayerTreeTransactionID:(uint64_t)nextValidLayerTreeTransactionID
1047 {
1048     if (_dynamicViewportUpdateMode != DynamicViewportUpdateMode::NotResizing) {
1049         CGFloat animatingScaleTarget = [[_resizeAnimationView layer] transform].m11;
1050         double currentTargetScale = animatingScaleTarget * [[_contentView layer] transform].m11;
1051         double scale = newScale / currentTargetScale;
1052         _resizeAnimationTransformAdjustments = CATransform3DMakeScale(scale, scale, 1);
1053
1054         CGPoint newContentOffset = [self _adjustedContentOffset:CGPointMake(newScrollPosition.x * newScale, newScrollPosition.y * newScale)];
1055         CGPoint currentContentOffset = [_scrollView contentOffset];
1056
1057         _resizeAnimationTransformAdjustments.m41 = (currentContentOffset.x - newContentOffset.x) / animatingScaleTarget;
1058         _resizeAnimationTransformAdjustments.m42 = (currentContentOffset.y - newContentOffset.y) / animatingScaleTarget;
1059         _resizeAnimationTransformTransactionID = nextValidLayerTreeTransactionID;
1060     }
1061 }
1062
1063 - (void)_couldNotRestorePageState
1064 {
1065     // The gestureController may be waiting for the scroll position to be restored
1066     // in order to remove the swipe snapshot. Since the scroll position could not be
1067     // restored, tell the gestureController it was restored so that it no longer waits
1068     // for it.
1069     if (_gestureController)
1070         _gestureController->didRestoreScrollPosition();
1071 }
1072
1073 - (void)_restorePageStateToExposedRect:(WebCore::FloatRect)exposedRect scale:(double)scale
1074 {
1075     if (_dynamicViewportUpdateMode != DynamicViewportUpdateMode::NotResizing)
1076         return;
1077
1078     if (_customContentView)
1079         return;
1080
1081     _needsToRestoreUnobscuredCenter = NO;
1082     _needsToRestoreExposedRect = YES;
1083     _firstTransactionIDAfterPageRestore = downcast<WebKit::RemoteLayerTreeDrawingAreaProxy>(*_page->drawingArea()).nextLayerTreeTransactionID();
1084     _exposedRectToRestore = exposedRect;
1085     _scaleToRestore = scale;
1086 }
1087
1088 - (void)_restorePageStateToUnobscuredCenter:(WebCore::FloatPoint)center scale:(double)scale
1089 {
1090     if (_dynamicViewportUpdateMode != DynamicViewportUpdateMode::NotResizing)
1091         return;
1092
1093     if (_customContentView)
1094         return;
1095
1096     _needsToRestoreExposedRect = NO;
1097     _needsToRestoreUnobscuredCenter = YES;
1098     _firstTransactionIDAfterPageRestore = downcast<WebKit::RemoteLayerTreeDrawingAreaProxy>(*_page->drawingArea()).nextLayerTreeTransactionID();
1099     _unobscuredCenterToRestore = center;
1100     _scaleToRestore = scale;
1101 }
1102
1103 - (PassRefPtr<WebKit::ViewSnapshot>)_takeViewSnapshot
1104 {
1105     float deviceScale = WKGetScreenScaleFactor();
1106     WebCore::FloatSize snapshotSize(self.bounds.size);
1107     snapshotSize.scale(deviceScale, deviceScale);
1108
1109     CATransform3D transform = CATransform3DMakeScale(deviceScale, deviceScale, 1);
1110
1111 #if USE(IOSURFACE)
1112     auto surface = WebCore::IOSurface::create(WebCore::expandedIntSize(snapshotSize), WebCore::ColorSpaceDeviceRGB);
1113     CARenderServerRenderLayerWithTransform(MACH_PORT_NULL, self.layer.context.contextId, reinterpret_cast<uint64_t>(self.layer), surface->surface(), 0, 0, &transform);
1114
1115     return WebKit::ViewSnapshot::create(WTF::move(surface));
1116 #else
1117     uint32_t slotID = [WebKit::ViewSnapshotStore::snapshottingContext() createImageSlot:snapshotSize hasAlpha:YES];
1118
1119     if (!slotID)
1120         return nullptr;
1121
1122     CARenderServerCaptureLayerWithTransform(MACH_PORT_NULL, self.layer.context.contextId, (uint64_t)self.layer, slotID, 0, 0, &transform);
1123     WebCore::IntSize imageSize = WebCore::expandedIntSize(WebCore::FloatSize(snapshotSize));
1124     return WebKit::ViewSnapshot::create(slotID, imageSize, imageSize.width() * imageSize.height() * 4);
1125 #endif
1126 }
1127
1128 - (void)_zoomToPoint:(WebCore::FloatPoint)point atScale:(double)scale animated:(BOOL)animated
1129 {
1130     CFTimeInterval duration = 0;
1131     CGFloat zoomScale = contentZoomScale(self);
1132
1133     if (animated) {
1134         const double maximumZoomDuration = 0.4;
1135         const double minimumZoomDuration = 0.1;
1136         const double zoomDurationFactor = 0.3;
1137
1138         duration = std::min(fabs(log(zoomScale) - log(scale)) * zoomDurationFactor + minimumZoomDuration, maximumZoomDuration);
1139     }
1140
1141     if (scale != zoomScale)
1142         _page->willStartUserTriggeredZooming();
1143
1144     [_scrollView _zoomToCenter:point scale:scale duration:duration];
1145 }
1146
1147 - (void)_zoomToRect:(WebCore::FloatRect)targetRect atScale:(double)scale origin:(WebCore::FloatPoint)origin animated:(BOOL)animated
1148 {
1149     // FIXME: Some of this could be shared with _scrollToRect.
1150     const double visibleRectScaleChange = contentZoomScale(self) / scale;
1151     const WebCore::FloatRect visibleRect([self convertRect:self.bounds toView:self._currentContentView]);
1152     const WebCore::FloatRect unobscuredRect([self _contentRectForUserInteraction]);
1153
1154     const WebCore::FloatSize topLeftObscuredInsetAfterZoom((unobscuredRect.minXMinYCorner() - visibleRect.minXMinYCorner()) * visibleRectScaleChange);
1155     const WebCore::FloatSize bottomRightObscuredInsetAfterZoom((visibleRect.maxXMaxYCorner() - unobscuredRect.maxXMaxYCorner()) * visibleRectScaleChange);
1156
1157     const WebCore::FloatSize unobscuredRectSizeAfterZoom(unobscuredRect.size() * visibleRectScaleChange);
1158
1159     // Center to the target rect.
1160     WebCore::FloatPoint unobscuredRectLocationAfterZoom = targetRect.location() - (unobscuredRectSizeAfterZoom - targetRect.size()) * 0.5;
1161
1162     // Center to the tap point instead in case the target rect won't fit in a direction.
1163     if (targetRect.width() > unobscuredRectSizeAfterZoom.width())
1164         unobscuredRectLocationAfterZoom.setX(origin.x() - unobscuredRectSizeAfterZoom.width() / 2);
1165     if (targetRect.height() > unobscuredRectSizeAfterZoom.height())
1166         unobscuredRectLocationAfterZoom.setY(origin.y() - unobscuredRectSizeAfterZoom.height() / 2);
1167
1168     // We have computed where we want the unobscured rect to be. Now adjust for the obscuring insets.
1169     WebCore::FloatRect visibleRectAfterZoom(unobscuredRectLocationAfterZoom, unobscuredRectSizeAfterZoom);
1170     visibleRectAfterZoom.move(-topLeftObscuredInsetAfterZoom);
1171     visibleRectAfterZoom.expand(topLeftObscuredInsetAfterZoom + bottomRightObscuredInsetAfterZoom);
1172
1173     [self _zoomToPoint:visibleRectAfterZoom.center() atScale:scale animated:animated];
1174 }
1175
1176 static WebCore::FloatPoint constrainContentOffset(WebCore::FloatPoint contentOffset, WebCore::FloatSize contentSize, WebCore::FloatSize unobscuredContentSize)
1177 {
1178     WebCore::FloatSize maximumContentOffset = contentSize - unobscuredContentSize;
1179     contentOffset = contentOffset.shrunkTo(WebCore::FloatPoint(maximumContentOffset.width(), maximumContentOffset.height()));
1180     contentOffset = contentOffset.expandedTo(WebCore::FloatPoint());
1181     return contentOffset;
1182 }
1183
1184 - (void)_scrollToContentOffset:(WebCore::FloatPoint)contentOffsetInPageCoordinates scrollOrigin:(WebCore::IntPoint)scrollOrigin
1185 {
1186     if (_dynamicViewportUpdateMode != DynamicViewportUpdateMode::NotResizing)
1187         return;
1188
1189     WebCore::FloatPoint contentOffsetRespectingOrigin = scrollOrigin + toFloatSize(contentOffsetInPageCoordinates);
1190
1191     WebCore::FloatPoint scaledOffset = contentOffsetRespectingOrigin;
1192     CGFloat zoomScale = contentZoomScale(self);
1193     scaledOffset.scale(zoomScale, zoomScale);
1194
1195     CGPoint contentOffsetInScrollViewCoordinates = [self _adjustedContentOffset:scaledOffset];
1196     contentOffsetInScrollViewCoordinates = contentOffsetBoundedInValidRange(_scrollView.get(), contentOffsetInScrollViewCoordinates);
1197
1198     [_scrollView _stopScrollingAndZoomingAnimations];
1199
1200     if (!CGPointEqualToPoint(contentOffsetInScrollViewCoordinates, [_scrollView contentOffset]))
1201         [_scrollView setContentOffset:contentOffsetInScrollViewCoordinates];
1202     else {
1203         // If we haven't changed anything, there would not be any VisibleContentRect update sent to the content.
1204         // The WebProcess would keep the invalid contentOffset as its scroll position.
1205         // To synchronize the WebProcess with what is on screen, we send the VisibleContentRect again.
1206         _page->resendLastVisibleContentRects();
1207     }
1208 }
1209
1210 - (BOOL)_scrollToRect:(WebCore::FloatRect)targetRect origin:(WebCore::FloatPoint)origin minimumScrollDistance:(float)minimumScrollDistance
1211 {
1212     WebCore::FloatRect unobscuredContentRect([self _contentRectForUserInteraction]);
1213     WebCore::FloatPoint unobscuredContentOffset = unobscuredContentRect.location();
1214     WebCore::FloatSize contentSize([self._currentContentView bounds].size);
1215
1216     // Center the target rect in the scroll view.
1217     // If the target doesn't fit in the scroll view, center on the gesture location instead.
1218     WebCore::FloatPoint newUnobscuredContentOffset;
1219     if (targetRect.width() <= unobscuredContentRect.width())
1220         newUnobscuredContentOffset.setX(targetRect.x() - (unobscuredContentRect.width() - targetRect.width()) / 2);
1221     else
1222         newUnobscuredContentOffset.setX(origin.x() - unobscuredContentRect.width() / 2);
1223     if (targetRect.height() <= unobscuredContentRect.height())
1224         newUnobscuredContentOffset.setY(targetRect.y() - (unobscuredContentRect.height() - targetRect.height()) / 2);
1225     else
1226         newUnobscuredContentOffset.setY(origin.y() - unobscuredContentRect.height() / 2);
1227     newUnobscuredContentOffset = constrainContentOffset(newUnobscuredContentOffset, contentSize, unobscuredContentRect.size());
1228
1229     if (unobscuredContentOffset == newUnobscuredContentOffset) {
1230         if (targetRect.width() > unobscuredContentRect.width())
1231             newUnobscuredContentOffset.setX(origin.x() - unobscuredContentRect.width() / 2);
1232         if (targetRect.height() > unobscuredContentRect.height())
1233             newUnobscuredContentOffset.setY(origin.y() - unobscuredContentRect.height() / 2);
1234         newUnobscuredContentOffset = constrainContentOffset(newUnobscuredContentOffset, contentSize, unobscuredContentRect.size());
1235     }
1236
1237     WebCore::FloatSize scrollViewOffsetDelta = newUnobscuredContentOffset - unobscuredContentOffset;
1238     scrollViewOffsetDelta.scale(contentZoomScale(self));
1239
1240     float scrollDistance = scrollViewOffsetDelta.diagonalLength();
1241     if (scrollDistance < minimumScrollDistance)
1242         return false;
1243
1244     [_contentView willStartZoomOrScroll];
1245
1246     [_scrollView setContentOffset:([_scrollView contentOffset] + scrollViewOffsetDelta) animated:YES];
1247     return true;
1248 }
1249
1250 - (void)_scrollByOffset:(WebCore::FloatPoint)offset
1251 {
1252     CGPoint currentOffset = ([_scrollView _isAnimatingScroll]) ? [_scrollView _animatedTargetOffset] : [_scrollView contentOffset];
1253
1254     CGPoint boundedOffset = contentOffsetBoundedInValidRange(_scrollView.get(), currentOffset + offset);
1255     
1256     if (CGPointEqualToPoint(boundedOffset, currentOffset))
1257         return;
1258     [_contentView willStartZoomOrScroll];
1259     [_scrollView setContentOffset:boundedOffset animated:YES];
1260 }
1261
1262 - (void)_zoomOutWithOrigin:(WebCore::FloatPoint)origin animated:(BOOL)animated
1263 {
1264     [self _zoomToPoint:origin atScale:[_scrollView minimumZoomScale] animated:animated];
1265 }
1266
1267 // focusedElementRect and selectionRect are both in document coordinates.
1268 - (void)_zoomToFocusRect:(WebCore::FloatRect)focusedElementRectInDocumentCoordinates selectionRect:(WebCore::FloatRect)selectionRectInDocumentCoordinates fontSize:(float)fontSize minimumScale:(double)minimumScale maximumScale:(double)maximumScale allowScaling:(BOOL)allowScaling forceScroll:(BOOL)forceScroll
1269 {
1270     const double WKWebViewStandardFontSize = 16;
1271     const double kMinimumHeightToShowContentAboveKeyboard = 106;
1272     const CFTimeInterval UIWebFormAnimationDuration = 0.25;
1273     const double CaretOffsetFromWindowEdge = 20;
1274
1275     // Zoom around the element's bounding frame. We use a "standard" size to determine the proper frame.
1276     double scale = allowScaling ? std::min(std::max(WKWebViewStandardFontSize / fontSize, minimumScale), maximumScale) : contentZoomScale(self);
1277     CGFloat documentWidth = [_contentView bounds].size.width;
1278     scale = CGRound(documentWidth * scale) / documentWidth;
1279
1280     UIWindow *window = [_scrollView window];
1281
1282     WebCore::FloatRect focusedElementRectInNewScale = focusedElementRectInDocumentCoordinates;
1283     focusedElementRectInNewScale.scale(scale);
1284     focusedElementRectInNewScale.moveBy([_contentView frame].origin);
1285
1286     // Find the portion of the view that is visible on the screen.
1287     UIViewController *topViewController = [[[_scrollView _viewControllerForAncestor] _rootAncestorViewController] _viewControllerForSupportedInterfaceOrientations];
1288     UIView *fullScreenView = topViewController.view;
1289     if (!fullScreenView)
1290         fullScreenView = window;
1291
1292     CGRect unobscuredScrollViewRectInWebViewCoordinates = UIEdgeInsetsInsetRect([self bounds], _obscuredInsets);
1293     CGRect visibleScrollViewBoundsInWebViewCoordinates = CGRectIntersection(unobscuredScrollViewRectInWebViewCoordinates, [fullScreenView convertRect:[fullScreenView bounds] toView:self]);
1294     CGRect formAssistantFrameInWebViewCoordinates = [window convertRect:_inputViewBounds toView:self];
1295     CGRect intersectionBetweenScrollViewAndFormAssistant = CGRectIntersection(visibleScrollViewBoundsInWebViewCoordinates, formAssistantFrameInWebViewCoordinates);
1296     CGSize visibleSize = visibleScrollViewBoundsInWebViewCoordinates.size;
1297
1298     CGFloat visibleOffsetFromTop = 0;
1299     if (!CGRectIsEmpty(intersectionBetweenScrollViewAndFormAssistant)) {
1300         CGFloat heightVisibleAboveFormAssistant = CGRectGetMinY(intersectionBetweenScrollViewAndFormAssistant) - CGRectGetMinY(visibleScrollViewBoundsInWebViewCoordinates);
1301         CGFloat heightVisibleBelowFormAssistant = CGRectGetMaxY(visibleScrollViewBoundsInWebViewCoordinates) - CGRectGetMaxY(intersectionBetweenScrollViewAndFormAssistant);
1302
1303         if (heightVisibleAboveFormAssistant >= kMinimumHeightToShowContentAboveKeyboard || heightVisibleBelowFormAssistant < heightVisibleAboveFormAssistant)
1304             visibleSize.height = heightVisibleAboveFormAssistant;
1305         else {
1306             visibleSize.height = heightVisibleBelowFormAssistant;
1307             visibleOffsetFromTop = CGRectGetMaxY(intersectionBetweenScrollViewAndFormAssistant) - CGRectGetMinY(visibleScrollViewBoundsInWebViewCoordinates);
1308         }
1309     }
1310
1311     BOOL selectionRectIsNotNull = !selectionRectInDocumentCoordinates.isZero();
1312     if (!forceScroll) {
1313         CGRect currentlyVisibleRegionInWebViewCoordinates;
1314         currentlyVisibleRegionInWebViewCoordinates.origin = unobscuredScrollViewRectInWebViewCoordinates.origin;
1315         currentlyVisibleRegionInWebViewCoordinates.origin.y += visibleOffsetFromTop;
1316         currentlyVisibleRegionInWebViewCoordinates.size = visibleSize;
1317
1318         // Don't bother scrolling if the entire node is already visible, whether or not we got a selectionRect.
1319         if (CGRectContainsRect(currentlyVisibleRegionInWebViewCoordinates, [self convertRect:focusedElementRectInDocumentCoordinates fromView:_contentView.get()]))
1320             return;
1321
1322         // Don't bother scrolling if we have a valid selectionRect and it is already visible.
1323         if (selectionRectIsNotNull && CGRectContainsRect(currentlyVisibleRegionInWebViewCoordinates, [self convertRect:selectionRectInDocumentCoordinates fromView:_contentView.get()]))
1324             return;
1325     }
1326
1327     // We want to zoom to the left/top corner of the DOM node, with as much spacing on all sides as we
1328     // can get based on the visible area after zooming (workingFrame).  The spacing in either dimension is half the
1329     // difference between the size of the DOM node and the size of the visible frame.
1330     CGFloat horizontalSpaceInWebViewCoordinates = std::max((visibleSize.width - focusedElementRectInNewScale.width()) / 2.0, 0.0);
1331     CGFloat verticalSpaceInWebViewCoordinates = std::max((visibleSize.height - focusedElementRectInNewScale.height()) / 2.0, 0.0);
1332
1333     CGPoint topLeft;
1334     topLeft.x = focusedElementRectInNewScale.x() - horizontalSpaceInWebViewCoordinates;
1335     topLeft.y = focusedElementRectInNewScale.y() - verticalSpaceInWebViewCoordinates - visibleOffsetFromTop;
1336
1337     CGFloat minimumAllowableHorizontalOffsetInWebViewCoordinates = -INFINITY;
1338     CGFloat minimumAllowableVerticalOffsetInWebViewCoordinates = -INFINITY;
1339     if (selectionRectIsNotNull) {
1340         WebCore::FloatRect selectionRectInNewScale = selectionRectInDocumentCoordinates;
1341         selectionRectInNewScale.scale(scale);
1342         selectionRectInNewScale.moveBy([_contentView frame].origin);
1343         minimumAllowableHorizontalOffsetInWebViewCoordinates = CGRectGetMaxX(selectionRectInNewScale) + CaretOffsetFromWindowEdge - visibleSize.width;
1344         minimumAllowableVerticalOffsetInWebViewCoordinates = CGRectGetMaxY(selectionRectInNewScale) + CaretOffsetFromWindowEdge - visibleSize.height - visibleOffsetFromTop;
1345     }
1346
1347     WebCore::FloatRect documentBoundsInNewScale = [_contentView bounds];
1348     documentBoundsInNewScale.scale(scale);
1349     documentBoundsInNewScale.moveBy([_contentView frame].origin);
1350
1351     // Constrain the left edge in document coordinates so that:
1352     //  - it isn't so small that the scrollVisibleRect isn't visible on the screen
1353     //  - it isn't so great that the document's right edge is less than the right edge of the screen
1354     if (selectionRectIsNotNull && topLeft.x < minimumAllowableHorizontalOffsetInWebViewCoordinates)
1355         topLeft.x = minimumAllowableHorizontalOffsetInWebViewCoordinates;
1356     else {
1357         CGFloat maximumAllowableHorizontalOffset = CGRectGetMaxX(documentBoundsInNewScale) - visibleSize.width;
1358         if (topLeft.x > maximumAllowableHorizontalOffset)
1359             topLeft.x = maximumAllowableHorizontalOffset;
1360     }
1361
1362     // Constrain the top edge in document coordinates so that:
1363     //  - it isn't so small that the scrollVisibleRect isn't visible on the screen
1364     //  - it isn't so great that the document's bottom edge is higher than the top of the form assistant
1365     if (selectionRectIsNotNull && topLeft.y < minimumAllowableVerticalOffsetInWebViewCoordinates)
1366         topLeft.y = minimumAllowableVerticalOffsetInWebViewCoordinates;
1367     else {
1368         CGFloat maximumAllowableVerticalOffset = CGRectGetMaxY(documentBoundsInNewScale) - visibleSize.height;
1369         if (topLeft.y > maximumAllowableVerticalOffset)
1370             topLeft.y = maximumAllowableVerticalOffset;
1371     }
1372
1373     WebCore::FloatPoint newCenter = CGPointMake(topLeft.x + unobscuredScrollViewRectInWebViewCoordinates.size.width / 2.0, topLeft.y + unobscuredScrollViewRectInWebViewCoordinates.size.height / 2.0);
1374
1375     if (scale != contentZoomScale(self))
1376         _page->willStartUserTriggeredZooming();
1377
1378     // The newCenter has been computed in the new scale, but _zoomToCenter expected the center to be in the original scale.
1379     newCenter.scale(1 / scale, 1 / scale);
1380     [_scrollView _zoomToCenter:newCenter
1381                         scale:scale
1382                      duration:UIWebFormAnimationDuration
1383                         force:YES];
1384 }
1385
1386 - (BOOL)_zoomToRect:(WebCore::FloatRect)targetRect withOrigin:(WebCore::FloatPoint)origin fitEntireRect:(BOOL)fitEntireRect minimumScale:(double)minimumScale maximumScale:(double)maximumScale minimumScrollDistance:(float)minimumScrollDistance
1387 {
1388     const float maximumScaleFactorDeltaForPanScroll = 0.02;
1389
1390     double currentScale = contentZoomScale(self);
1391
1392     WebCore::FloatSize unobscuredContentSize([self _contentRectForUserInteraction].size);
1393     double horizontalScale = unobscuredContentSize.width() * currentScale / targetRect.width();
1394     double verticalScale = unobscuredContentSize.height() * currentScale / targetRect.height();
1395
1396     horizontalScale = std::min(std::max(horizontalScale, minimumScale), maximumScale);
1397     verticalScale = std::min(std::max(verticalScale, minimumScale), maximumScale);
1398
1399     double targetScale = fitEntireRect ? std::min(horizontalScale, verticalScale) : horizontalScale;
1400     if (fabs(targetScale - currentScale) < maximumScaleFactorDeltaForPanScroll) {
1401         if ([self _scrollToRect:targetRect origin:origin minimumScrollDistance:minimumScrollDistance])
1402             return true;
1403     } else if (targetScale != currentScale) {
1404         [self _zoomToRect:targetRect atScale:targetScale origin:origin animated:YES];
1405         return true;
1406     }
1407     
1408     return false;
1409 }
1410
1411 - (void)didMoveToWindow
1412 {
1413     _page->viewStateDidChange(WebCore::ViewState::AllFlags);
1414 }
1415
1416 - (void)setOpaque:(BOOL)opaque
1417 {
1418     BOOL oldOpaque = self.opaque;
1419
1420     [super setOpaque:opaque];
1421     [_contentView setOpaque:opaque];
1422
1423     if (oldOpaque == opaque)
1424         return;
1425
1426     _page->setDrawsBackground(opaque);
1427     [self _updateScrollViewBackground];
1428 }
1429
1430 - (void)setBackgroundColor:(UIColor *)backgroundColor
1431 {
1432     [super setBackgroundColor:backgroundColor];
1433     [_contentView setBackgroundColor:backgroundColor];
1434 }
1435
1436 #pragma mark - UIScrollViewDelegate
1437
1438 - (BOOL)usesStandardContentView
1439 {
1440     return !_customContentView;
1441 }
1442
1443 - (CGSize)scrollView:(UIScrollView*)scrollView contentSizeForZoomScale:(CGFloat)scale withProposedSize:(CGSize)proposedSize
1444 {
1445     return roundScrollViewContentSize(*_page, proposedSize);
1446 }
1447
1448 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
1449 {
1450     ASSERT(_scrollView == scrollView);
1451
1452     if (_customContentView)
1453         return _customContentView.get();
1454
1455     return _contentView.get();
1456 }
1457
1458 - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
1459 {
1460     if (![self usesStandardContentView])
1461         return;
1462
1463     if (scrollView.pinchGestureRecognizer.state == UIGestureRecognizerStateBegan) {
1464         _page->willStartUserTriggeredZooming();
1465         [_contentView scrollViewWillStartPanOrPinchGesture];
1466     }
1467     [_contentView willStartZoomOrScroll];
1468 }
1469
1470 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
1471 {
1472     if (![self usesStandardContentView])
1473         return;
1474
1475     if (scrollView.panGestureRecognizer.state == UIGestureRecognizerStateBegan)
1476         [_contentView scrollViewWillStartPanOrPinchGesture];
1477
1478     [_contentView willStartZoomOrScroll];
1479 #if ENABLE(CSS_SCROLL_SNAP) && ENABLE(ASYNC_SCROLLING)
1480     // FIXME: We will want to detect whether snapping will occur before beginning to drag. See WebPageProxy::didCommitLayerTree.
1481     WebKit::RemoteScrollingCoordinatorProxy* coordinator = _page->scrollingCoordinatorProxy();
1482     ASSERT(scrollView == _scrollView.get());
1483     CGFloat scrollDecelerationFactor = (coordinator && coordinator->shouldSetScrollViewDecelerationRateFast()) ? UIScrollViewDecelerationRateFast : [_scrollView preferredScrollDecelerationFactor];
1484     scrollView.horizontalScrollDecelerationFactor = scrollDecelerationFactor;
1485     scrollView.verticalScrollDecelerationFactor = scrollDecelerationFactor;
1486 #endif
1487 }
1488
1489 - (void)_didFinishScrolling
1490 {
1491     if (![self usesStandardContentView])
1492         return;
1493
1494     [self _updateVisibleContentRects];
1495     [_contentView didFinishScrolling];
1496 }
1497
1498 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
1499 {
1500     // Work around <rdar://problem/16374753> by avoiding deceleration while
1501     // zooming. We'll animate to the right place once the zoom finishes.
1502     if ([scrollView isZooming])
1503         *targetContentOffset = [scrollView contentOffset];
1504 #if ENABLE(CSS_SCROLL_SNAP) && ENABLE(ASYNC_SCROLLING)
1505     if (WebKit::RemoteScrollingCoordinatorProxy* coordinator = _page->scrollingCoordinatorProxy()) {
1506         // FIXME: Here, I'm finding the maximum horizontal/vertical scroll offsets. There's probably a better way to do this.
1507         CGSize maxScrollOffsets = CGSizeMake(scrollView.contentSize.width - scrollView.bounds.size.width, scrollView.contentSize.height - scrollView.bounds.size.height);
1508         
1509         CGRect fullViewRect = self.bounds;
1510
1511         UIEdgeInsets contentInset;
1512
1513         id<WKUIDelegatePrivate> uiDelegatePrivate = static_cast<id <WKUIDelegatePrivate>>([self UIDelegate]);
1514         if ([uiDelegatePrivate respondsToSelector:@selector(_webView:finalObscuredInsetsForScrollView:withVelocity:targetContentOffset:)])
1515             contentInset = [uiDelegatePrivate _webView:self finalObscuredInsetsForScrollView:scrollView withVelocity:velocity targetContentOffset:targetContentOffset];
1516         else
1517             contentInset = [self _computedContentInset];
1518
1519         CGRect unobscuredRect = UIEdgeInsetsInsetRect(fullViewRect, contentInset);
1520         
1521         coordinator->adjustTargetContentOffsetForSnapping(maxScrollOffsets, velocity, unobscuredRect.origin.y, targetContentOffset);
1522     }
1523 #endif
1524 }
1525
1526 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
1527 {
1528     // If we're decelerating, scroll offset will be updated when scrollViewDidFinishDecelerating: is called.
1529     if (!decelerate)
1530         [self _didFinishScrolling];
1531 }
1532
1533 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
1534 {
1535     [self _didFinishScrolling];
1536 }
1537
1538 - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
1539 {
1540     [self _didFinishScrolling];
1541 }
1542
1543 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
1544 {
1545     if (![self usesStandardContentView])
1546         [_customContentView scrollViewDidScroll:(UIScrollView *)scrollView];
1547
1548     [self _updateVisibleContentRects];
1549     
1550     if (WebKit::RemoteLayerTreeScrollingPerformanceData* scrollPerfData = _page->scrollingPerformanceData())
1551         scrollPerfData->didScroll([self visibleRectInViewCoordinates]);
1552 }
1553
1554 - (void)scrollViewDidZoom:(UIScrollView *)scrollView
1555 {
1556     [self _updateScrollViewBackground];
1557     [self _updateVisibleContentRects];
1558 }
1559
1560 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
1561 {
1562     ASSERT(scrollView == _scrollView);
1563     [self _updateVisibleContentRects];
1564     [_contentView didZoomToScale:scale];
1565 }
1566
1567 - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
1568 {
1569     [self _didFinishScrolling];
1570 }
1571
1572 - (void)_scrollViewDidInterruptDecelerating:(UIScrollView *)scrollView
1573 {
1574     if (![self usesStandardContentView])
1575         return;
1576
1577     [_contentView didInterruptScrolling];
1578     [self _updateVisibleContentRects];
1579 }
1580
1581 - (void)_frameOrBoundsChanged
1582 {
1583     CGRect bounds = self.bounds;
1584     [_scrollView setFrame:bounds];
1585
1586     if (_dynamicViewportUpdateMode == DynamicViewportUpdateMode::NotResizing) {
1587         if (!_overridesMinimumLayoutSize)
1588             _page->setViewportConfigurationMinimumLayoutSize(WebCore::FloatSize(bounds.size));
1589         if (!_overridesMaximumUnobscuredSize)
1590             _page->setMaximumUnobscuredSize(WebCore::FloatSize(bounds.size));
1591         if (WebKit::DrawingAreaProxy* drawingArea = _page->drawingArea())
1592             drawingArea->setSize(WebCore::IntSize(bounds.size), WebCore::IntSize(), WebCore::IntSize());
1593     }
1594
1595     [_customContentView web_setMinimumSize:bounds.size];
1596     [self _updateVisibleContentRects];
1597 }
1598
1599 // Unobscured content rect where the user can interact. When the keyboard is up, this should be the area above or below the keyboard, wherever there is enough space.
1600 - (CGRect)_contentRectForUserInteraction
1601 {
1602     // FIXME: handle split keyboard.
1603     UIEdgeInsets obscuredInsets = _obscuredInsets;
1604     obscuredInsets.bottom = std::max(_obscuredInsets.bottom, _inputViewBounds.size.height);
1605     CGRect unobscuredRect = UIEdgeInsetsInsetRect(self.bounds, obscuredInsets);
1606     return [self convertRect:unobscuredRect toView:self._currentContentView];
1607 }
1608
1609 // Ideally UIScrollView would expose this for us: <rdar://problem/21394567>.
1610 - (BOOL)_scrollViewIsRubberBanding
1611 {
1612     float deviceScaleFactor = _page->deviceScaleFactor();
1613
1614     CGPoint contentOffset = [_scrollView contentOffset];
1615     CGPoint boundedOffset = contentOffsetBoundedInValidRange(_scrollView.get(), contentOffset);
1616     return !pointsEqualInDevicePixels(contentOffset, boundedOffset, deviceScaleFactor);
1617 }
1618
1619 - (void)_updateVisibleContentRects
1620 {
1621     if (![self usesStandardContentView]) {
1622         [_customContentView web_computedContentInsetDidChange];
1623         return;
1624     }
1625
1626     if (_delayUpdateVisibleContentRects) {
1627         _hadDelayedUpdateVisibleContentRects = YES;
1628         return;
1629     }
1630
1631     if (_dynamicViewportUpdateMode != DynamicViewportUpdateMode::NotResizing)
1632         return;
1633
1634     if (_needsResetViewStateAfterCommitLoadForMainFrame)
1635         return;
1636
1637     if ([_scrollView isZoomBouncing])
1638         return;
1639
1640     CGRect fullViewRect = self.bounds;
1641     CGRect visibleRectInContentCoordinates = _frozenVisibleContentRect ? _frozenVisibleContentRect.value() : [self convertRect:fullViewRect toView:_contentView.get()];
1642
1643     CGRect unobscuredRect = UIEdgeInsetsInsetRect(fullViewRect, [self _computedContentInset]);
1644     CGRect unobscuredRectInContentCoordinates = _frozenUnobscuredContentRect ? _frozenUnobscuredContentRect.value() : [self convertRect:unobscuredRect toView:_contentView.get()];
1645
1646     CGFloat scaleFactor = contentZoomScale(self);
1647
1648     BOOL isStableState = !(_isChangingObscuredInsetsInteractively || [_scrollView isDragging] || [_scrollView isDecelerating] || [_scrollView isZooming] || [_scrollView _isAnimatingZoom] || [_scrollView _isScrollingToTop] || [self _scrollViewIsRubberBanding]);
1649
1650     // FIXME: this can be made static after we stop supporting iOS 8.x.
1651     if (isStableState && [_scrollView respondsToSelector:@selector(_isInterruptingDeceleration)])
1652         isStableState = ![_scrollView performSelector:@selector(_isInterruptingDeceleration)];
1653
1654 #if ENABLE(CSS_SCROLL_SNAP) && ENABLE(ASYNC_SCROLLING)
1655     if (isStableState) {
1656         WebKit::RemoteScrollingCoordinatorProxy* coordinator = _page->scrollingCoordinatorProxy();
1657         if (coordinator && coordinator->hasActiveSnapPoint()) {
1658             CGRect fullViewRect = self.bounds;
1659             CGRect unobscuredRect = UIEdgeInsetsInsetRect(fullViewRect, [self _computedContentInset]);
1660             
1661             CGPoint currentPoint = [_scrollView contentOffset];
1662             CGPoint activePoint = coordinator->nearestActiveContentInsetAdjustedSnapPoint(unobscuredRect.origin.y, currentPoint);
1663
1664             if (!CGPointEqualToPoint(activePoint, currentPoint)) {
1665                 RetainPtr<WKScrollView> strongScrollView = _scrollView;
1666                 dispatch_async(dispatch_get_main_queue(), [strongScrollView, activePoint] {
1667                     [strongScrollView setContentOffset:activePoint animated:NO];
1668                 });
1669             }
1670         }
1671     }
1672 #endif
1673     
1674     [_contentView didUpdateVisibleRect:visibleRectInContentCoordinates
1675         unobscuredRect:unobscuredRectInContentCoordinates
1676         unobscuredRectInScrollViewCoordinates:unobscuredRect
1677         scale:scaleFactor minimumScale:[_scrollView minimumZoomScale]
1678         inStableState:isStableState isChangingObscuredInsetsInteractively:_isChangingObscuredInsetsInteractively];
1679 }
1680
1681 - (void)_didFinishLoadForMainFrame
1682 {
1683     if (_gestureController)
1684         _gestureController->didFinishLoadForMainFrame();
1685 }
1686
1687 - (void)_didFailLoadForMainFrame
1688 {
1689     if (_gestureController)
1690         _gestureController->didFailLoadForMainFrame();
1691 }
1692
1693 - (void)_didSameDocumentNavigationForMainFrame:(WebKit::SameDocumentNavigationType)navigationType
1694 {
1695     [_customContentView web_didSameDocumentNavigation:toAPI(navigationType)];
1696
1697     if (_gestureController)
1698         _gestureController->didSameDocumentNavigationForMainFrame(navigationType);
1699 }
1700
1701 - (void)_keyboardChangedWithInfo:(NSDictionary *)keyboardInfo adjustScrollView:(BOOL)adjustScrollView
1702 {
1703     NSValue *endFrameValue = [keyboardInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
1704     if (!endFrameValue)
1705         return;
1706
1707     // The keyboard rect is always in screen coordinates. In the view services case the window does not
1708     // have the interface orientation rotation transformation; its host does. So, it makes no sense to
1709     // clip the keyboard rect against its screen.
1710     if ([[self window] _isHostedInAnotherProcess])
1711         _inputViewBounds = [self.window convertRect:[endFrameValue CGRectValue] fromWindow:nil];
1712     else
1713         _inputViewBounds = [self.window convertRect:CGRectIntersection([endFrameValue CGRectValue], self.window.screen.bounds) fromWindow:nil];
1714
1715     [self _updateVisibleContentRects];
1716
1717     if (adjustScrollView)
1718         [_scrollView _adjustForAutomaticKeyboardInfo:keyboardInfo animated:YES lastAdjustment:&_lastAdjustmentForScroller];
1719 }
1720
1721 - (BOOL)_shouldUpdateKeyboardWithInfo:(NSDictionary *)keyboardInfo
1722 {
1723     if ([_contentView isAssistingNode])
1724         return YES;
1725
1726     NSNumber *isLocalKeyboard = [keyboardInfo valueForKey:UIKeyboardIsLocalUserInfoKey];
1727     return isLocalKeyboard && !isLocalKeyboard.boolValue;
1728 }
1729
1730 - (void)_keyboardWillChangeFrame:(NSNotification *)notification
1731 {
1732     if ([self _shouldUpdateKeyboardWithInfo:notification.userInfo])
1733         [self _keyboardChangedWithInfo:notification.userInfo adjustScrollView:YES];
1734 }
1735
1736 - (void)_keyboardDidChangeFrame:(NSNotification *)notification
1737 {
1738     [self _keyboardChangedWithInfo:notification.userInfo adjustScrollView:NO];
1739 }
1740
1741 - (void)_keyboardWillShow:(NSNotification *)notification
1742 {
1743     if ([self _shouldUpdateKeyboardWithInfo:notification.userInfo])
1744         [self _keyboardChangedWithInfo:notification.userInfo adjustScrollView:YES];
1745 }
1746
1747 - (void)_keyboardWillHide:(NSNotification *)notification
1748 {
1749     // Ignore keyboard will hide notifications sent during rotation. They're just there for
1750     // backwards compatibility reasons and processing the will hide notification would
1751     // temporarily screw up the the unobscured view area.
1752     if ([[UIPeripheralHost sharedInstance] rotationState])
1753         return;
1754
1755     [self _keyboardChangedWithInfo:notification.userInfo adjustScrollView:YES];
1756 }
1757
1758 - (void)_windowDidRotate:(NSNotification *)notification
1759 {
1760     if (!_overridesInterfaceOrientation)
1761         _page->setDeviceOrientation(deviceOrientation());
1762 }
1763
1764 - (void)_contentSizeCategoryDidChange:(NSNotification *)notification
1765 {
1766     _page->contentSizeCategoryDidChange([self _contentSizeCategory]);
1767 }
1768
1769 - (NSString *)_contentSizeCategory
1770 {
1771     return [[UIApplication sharedApplication] preferredContentSizeCategory];
1772 }
1773
1774 - (void)setAllowsBackForwardNavigationGestures:(BOOL)allowsBackForwardNavigationGestures
1775 {
1776     if (_allowsBackForwardNavigationGestures == allowsBackForwardNavigationGestures)
1777         return;
1778
1779     _allowsBackForwardNavigationGestures = allowsBackForwardNavigationGestures;
1780
1781     if (allowsBackForwardNavigationGestures) {
1782         if (!_gestureController) {
1783             _gestureController = std::make_unique<WebKit::ViewGestureController>(*_page);
1784             _gestureController->installSwipeHandler(self, [self scrollView]);
1785             _gestureController->setAlternateBackForwardListSourceView([_configuration _alternateWebViewForNavigationGestures]);
1786         }
1787     } else
1788         _gestureController = nullptr;
1789
1790     _page->setShouldRecordNavigationSnapshots(allowsBackForwardNavigationGestures);
1791 }
1792
1793 - (BOOL)allowsBackForwardNavigationGestures
1794 {
1795     return _allowsBackForwardNavigationGestures;
1796 }
1797
1798 - (void)_navigationGestureDidBegin
1799 {
1800     // During a back/forward swipe, there's a view interposed between this view and the content view that has
1801     // an offset and animation on it, which results in computing incorrect rectangles. Work around by using
1802     // frozen rects during swipes.
1803     CGRect fullViewRect = self.bounds;
1804     CGRect unobscuredRect = UIEdgeInsetsInsetRect(fullViewRect, [self _computedContentInset]);
1805
1806     _frozenVisibleContentRect = [self convertRect:fullViewRect toView:_contentView.get()];
1807     _frozenUnobscuredContentRect = [self convertRect:unobscuredRect toView:_contentView.get()];
1808 }
1809
1810 - (void)_navigationGestureDidEnd
1811 {
1812     _frozenVisibleContentRect = Nullopt;
1813     _frozenUnobscuredContentRect = Nullopt;
1814 }
1815
1816 #endif // PLATFORM(IOS)
1817
1818 #pragma mark OS X-specific methods
1819
1820 #if PLATFORM(MAC)
1821
1822 - (BOOL)becomeFirstResponder
1823 {
1824     return [[self window] makeFirstResponder: _wkView.get()];
1825 }
1826
1827 - (BOOL)acceptsFirstResponder
1828 {
1829     return [_wkView acceptsFirstResponder];
1830 }
1831
1832 - (void)resizeSubviewsWithOldSize:(NSSize)oldSize
1833 {
1834     [_wkView setFrame:self.bounds];
1835 }
1836
1837 - (void)setAllowsBackForwardNavigationGestures:(BOOL)allowsBackForwardNavigationGestures
1838 {
1839     [_wkView setAllowsBackForwardNavigationGestures:allowsBackForwardNavigationGestures];
1840 }
1841
1842 - (BOOL)allowsBackForwardNavigationGestures
1843 {
1844     return [_wkView allowsBackForwardNavigationGestures];
1845 }
1846
1847 - (void)setAllowsMagnification:(BOOL)allowsMagnification
1848 {
1849     [_wkView setAllowsMagnification:allowsMagnification];
1850 }
1851
1852 - (BOOL)allowsMagnification
1853 {
1854     return [_wkView allowsMagnification];
1855 }
1856
1857 - (void)setMagnification:(CGFloat)magnification
1858 {
1859     [_wkView setMagnification:magnification];
1860 }
1861
1862 - (CGFloat)magnification
1863 {
1864     return [_wkView magnification];
1865 }
1866
1867 - (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point
1868 {
1869     [_wkView setMagnification:magnification centeredAtPoint:NSPointFromCGPoint(point)];
1870 }
1871
1872 - (BOOL)_ignoresNonWheelEvents
1873 {
1874     return [_wkView _ignoresNonWheelEvents];
1875 }
1876
1877 - (void)_setIgnoresNonWheelEvents:(BOOL)ignoresNonWheelEvents
1878 {
1879     [_wkView _setIgnoresNonWheelEvents:ignoresNonWheelEvents];
1880 }
1881
1882 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
1883 {
1884     return [_wkView draggingEntered:sender];
1885 }
1886
1887 - (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
1888 {
1889     return [_wkView draggingUpdated:sender];
1890 }
1891
1892 - (void)draggingExited:(id <NSDraggingInfo>)sender
1893 {
1894     [_wkView draggingExited:sender];
1895 }
1896
1897 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
1898 {
1899     return [_wkView prepareForDragOperation:sender];
1900 }
1901
1902 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
1903 {
1904     return [_wkView performDragOperation:sender];
1905 }
1906 #endif // PLATFORM(MAC)
1907
1908 @end
1909
1910 @implementation WKWebView (WKPrivate)
1911
1912 - (BOOL)_isEditable
1913 {
1914     return _page->isEditable();
1915 }
1916
1917 - (void)_setEditable:(BOOL)editable
1918 {
1919     _page->setEditable(editable);
1920 #if !PLATFORM(IOS)
1921     [_wkView _addFontPanelObserver];
1922 #endif
1923 }
1924
1925 - (_WKRemoteObjectRegistry *)_remoteObjectRegistry
1926 {
1927     if (!_remoteObjectRegistry) {
1928         _remoteObjectRegistry = adoptNS([[_WKRemoteObjectRegistry alloc] _initWithMessageSender:*_page]);
1929         _page->process().processPool().addMessageReceiver(Messages::RemoteObjectRegistry::messageReceiverName(), _page->pageID(), [_remoteObjectRegistry remoteObjectRegistry]);
1930     }
1931
1932     return _remoteObjectRegistry.get();
1933 }
1934
1935 - (WKBrowsingContextHandle *)_handle
1936 {
1937     return [[[WKBrowsingContextHandle alloc] _initWithPageID:_page->pageID()] autorelease];
1938 }
1939
1940 - (_WKRenderingProgressEvents)_observedRenderingProgressEvents
1941 {
1942     return _observedRenderingProgressEvents;
1943 }
1944
1945 - (id <WKHistoryDelegatePrivate>)_historyDelegate
1946 {
1947     return _navigationState->historyDelegate().autorelease();
1948 }
1949
1950 - (void)_setHistoryDelegate:(id <WKHistoryDelegatePrivate>)historyDelegate
1951 {
1952     _page->setHistoryClient(_navigationState->createHistoryClient());
1953     _navigationState->setHistoryDelegate(historyDelegate);
1954 }
1955
1956 - (NSURL *)_unreachableURL
1957 {
1958     return [NSURL _web_URLWithWTFString:_page->pageLoadState().unreachableURL()];
1959 }
1960
1961 - (void)_loadAlternateHTMLString:(NSString *)string baseURL:(NSURL *)baseURL forUnreachableURL:(NSURL *)unreachableURL
1962 {
1963     _page->loadAlternateHTMLString(string, [baseURL _web_originalDataAsWTFString], [unreachableURL _web_originalDataAsWTFString]);
1964 }
1965
1966 - (NSArray *)_certificateChain
1967 {
1968     if (WebKit::WebFrameProxy* mainFrame = _page->mainFrame())
1969         return mainFrame->certificateInfo() ? (NSArray *)mainFrame->certificateInfo()->certificateInfo().certificateChain() : nil;
1970
1971     return nil;
1972 }
1973
1974 - (NSURL *)_committedURL
1975 {
1976     return [NSURL _web_URLWithWTFString:_page->pageLoadState().url()];
1977 }
1978
1979 - (NSString *)_MIMEType
1980 {
1981     if (_page->mainFrame())
1982         return _page->mainFrame()->mimeType();
1983
1984     return nil;
1985 }
1986
1987 - (NSString *)_userAgent
1988 {
1989     return _page->userAgent();
1990 }
1991
1992 - (NSString *)_applicationNameForUserAgent
1993 {
1994     return _page->applicationNameForUserAgent();
1995 }
1996
1997 - (void)_setApplicationNameForUserAgent:(NSString *)applicationNameForUserAgent
1998 {
1999     _page->setApplicationNameForUserAgent(applicationNameForUserAgent);
2000 }
2001
2002 - (NSString *)_customUserAgent
2003 {
2004     return self.customUserAgent;
2005 }
2006
2007 - (void)_setCustomUserAgent:(NSString *)customUserAgent
2008 {
2009     self.customUserAgent = customUserAgent;
2010 }
2011
2012 - (void)_setUserContentExtensionsEnabled:(BOOL)userContentExtensionsEnabled
2013 {
2014     _page->setUserContentExtensionsEnabled(userContentExtensionsEnabled);
2015 }
2016
2017 - (BOOL)_userContentExtensionsEnabled
2018 {
2019     return _page->userContentExtensionsEnabled();
2020 }
2021
2022 - (pid_t)_webProcessIdentifier
2023 {
2024     return _page->isValid() ? _page->processIdentifier() : 0;
2025 }
2026
2027 - (void)_killWebContentProcess
2028 {
2029     if (!_page->isValid())
2030         return;
2031
2032     _page->process().terminate();
2033 }
2034
2035 - (void)_killWebContentProcessAndResetState
2036 {
2037     _page->terminateProcess();
2038 }
2039
2040 #if PLATFORM(IOS)
2041 static WebCore::FloatSize activeMinimumLayoutSize(WKWebView *webView, const CGRect& bounds)
2042 {
2043     return WebCore::FloatSize(webView->_overridesMinimumLayoutSize ? webView->_minimumLayoutSizeOverride : bounds.size);
2044 }
2045
2046 static WebCore::FloatSize activeMaximumUnobscuredSize(WKWebView *webView, const CGRect& bounds)
2047 {
2048     return WebCore::FloatSize(webView->_overridesMaximumUnobscuredSize ? webView->_maximumUnobscuredSizeOverride : bounds.size);
2049 }
2050
2051 static int32_t activeOrientation(WKWebView *webView)
2052 {
2053     return webView->_overridesInterfaceOrientation ? deviceOrientationForUIInterfaceOrientation(webView->_interfaceOrientationOverride) : webView->_page->deviceOrientation();
2054 }
2055
2056 - (void (^)(void))_retainActiveFocusedState
2057 {
2058     ++_activeFocusedStateRetainCount;
2059
2060     // FIXME: Use something like CompletionHandlerCallChecker to ensure that the returned block is called before it's released.
2061     return [[[self] {
2062         --_activeFocusedStateRetainCount;
2063     } copy] autorelease];
2064 }
2065
2066 - (void)_becomeFirstResponderWithSelectionMovingForward:(BOOL)selectingForward completionHandler:(void (^)(BOOL didBecomeFirstResponder))completionHandler
2067 {
2068     typeof(completionHandler) completionHandlerCopy = nil;
2069     if (completionHandler)
2070         completionHandlerCopy = Block_copy(completionHandler);
2071
2072     [_contentView _becomeFirstResponderWithSelectionMovingForward:selectingForward completionHandler:[completionHandlerCopy](BOOL didBecomeFirstResponder) {
2073         if (!completionHandlerCopy)
2074             return;
2075
2076         completionHandlerCopy(didBecomeFirstResponder);
2077         Block_release(completionHandlerCopy);
2078     }];
2079 }
2080
2081 - (id)_snapshotLayerContentsForBackForwardListItem:(WKBackForwardListItem *)item
2082 {
2083     if (_page->backForwardList().currentItem() == &item._item)
2084         _page->recordNavigationSnapshot(*_page->backForwardList().currentItem());
2085
2086     if (auto* viewSnapshot = item._item.snapshot())
2087         return viewSnapshot->asLayerContents();
2088
2089     return nil;
2090 }
2091
2092 #endif
2093
2094 - (void)_didRelaunchProcess
2095 {
2096 #if PLATFORM(IOS)
2097     CGRect bounds = self.bounds;
2098     WebCore::FloatSize minimalLayoutSize = activeMinimumLayoutSize(self, bounds);
2099     _page->setViewportConfigurationMinimumLayoutSize(minimalLayoutSize);
2100     _page->setMaximumUnobscuredSize(activeMaximumUnobscuredSize(self, bounds));
2101 #endif
2102 }
2103
2104 - (NSData *)_sessionStateData
2105 {
2106     WebKit::SessionState sessionState = _page->sessionState();
2107
2108     // FIXME: This should not use the legacy session state encoder.
2109     return [wrapper(*WebKit::encodeLegacySessionState(sessionState).release().leakRef()) autorelease];
2110 }
2111
2112 - (_WKSessionState *)_sessionState
2113 {
2114     return adoptNS([[_WKSessionState alloc] _initWithSessionState:_page->sessionState()]).autorelease();
2115 }
2116
2117 - (void)_restoreFromSessionStateData:(NSData *)sessionStateData
2118 {
2119     // FIXME: This should not use the legacy session state decoder.
2120     WebKit::SessionState sessionState;
2121     if (!WebKit::decodeLegacySessionState(static_cast<const uint8_t*>(sessionStateData.bytes), sessionStateData.length, sessionState))
2122         return;
2123
2124     _page->restoreFromSessionState(WTF::move(sessionState), true);
2125 }
2126
2127 - (WKNavigation *)_restoreSessionState:(_WKSessionState *)sessionState andNavigate:(BOOL)navigate
2128 {
2129     auto navigation = _page->restoreFromSessionState(sessionState->_sessionState, navigate);
2130     if (!navigation)
2131         return nil;
2132
2133     return [wrapper(*navigation.release().leakRef()) autorelease];
2134 }
2135
2136 - (void)_close
2137 {
2138     _page->close();
2139 }
2140
2141 - (BOOL)_allowsRemoteInspection
2142 {
2143 #if ENABLE(REMOTE_INSPECTOR)
2144     return _page->allowsRemoteInspection();
2145 #else
2146     return NO;
2147 #endif
2148 }
2149
2150 - (void)_setAllowsRemoteInspection:(BOOL)allow
2151 {
2152 #if ENABLE(REMOTE_INSPECTOR)
2153     _page->setAllowsRemoteInspection(allow);
2154 #endif
2155 }
2156
2157 - (BOOL)_addsVisitedLinks
2158 {
2159     return _page->addsVisitedLinks();
2160 }
2161
2162 - (void)_setAddsVisitedLinks:(BOOL)addsVisitedLinks
2163 {
2164     _page->setAddsVisitedLinks(addsVisitedLinks);
2165 }
2166
2167 - (BOOL)_networkRequestsInProgress
2168 {
2169     return _page->pageLoadState().networkRequestsInProgress();
2170 }
2171
2172 static inline WebCore::LayoutMilestones layoutMilestones(_WKRenderingProgressEvents events)
2173 {
2174     WebCore::LayoutMilestones milestones = 0;
2175
2176     if (events & _WKRenderingProgressEventFirstLayout)
2177         milestones |= WebCore::DidFirstLayout;
2178
2179     if (events & _WKRenderingProgressEventFirstVisuallyNonEmptyLayout)
2180         milestones |= WebCore::DidFirstVisuallyNonEmptyLayout;
2181
2182     if (events & _WKRenderingProgressEventFirstPaintWithSignificantArea)
2183         milestones |= WebCore::DidHitRelevantRepaintedObjectsAreaThreshold;
2184
2185     if (events & _WKRenderingProgressEventReachedSessionRestorationRenderTreeSizeThreshold)
2186         milestones |= WebCore::ReachedSessionRestorationRenderTreeSizeThreshold;
2187
2188     if (events & _WKRenderingProgressEventFirstLayoutAfterSuppressedIncrementalRendering)
2189         milestones |= WebCore::DidFirstLayoutAfterSuppressedIncrementalRendering;
2190
2191     if (events & _WKRenderingProgressEventFirstPaintAfterSuppressedIncrementalRendering)
2192         milestones |= WebCore::DidFirstPaintAfterSuppressedIncrementalRendering;
2193
2194     return milestones;
2195 }
2196
2197 - (void)_setObservedRenderingProgressEvents:(_WKRenderingProgressEvents)observedRenderingProgressEvents
2198 {
2199     _observedRenderingProgressEvents = observedRenderingProgressEvents;
2200     _page->listenForLayoutMilestones(layoutMilestones(observedRenderingProgressEvents));
2201 }
2202
2203 - (void)_getMainResourceDataWithCompletionHandler:(void (^)(NSData *, NSError *))completionHandler
2204 {
2205     auto handler = adoptNS([completionHandler copy]);
2206
2207     _page->getMainResourceDataOfFrame(_page->mainFrame(), [handler](API::Data* data, WebKit::CallbackBase::Error error) {
2208         void (^completionHandlerBlock)(NSData *, NSError *) = (void (^)(NSData *, NSError *))handler.get();
2209         if (error != WebKit::CallbackBase::Error::None) {
2210             // FIXME: Pipe a proper error in from the WebPageProxy.
2211             RetainPtr<NSError> error = adoptNS([[NSError alloc] init]);
2212             completionHandlerBlock(nil, error.get());
2213         } else
2214             completionHandlerBlock(wrapper(*data), nil);
2215     });
2216 }
2217
2218 - (void)_getWebArchiveDataWithCompletionHandler:(void (^)(NSData *, NSError *))completionHandler
2219 {
2220     auto handler = adoptNS([completionHandler copy]);
2221
2222     _page->getWebArchiveOfFrame(_page->mainFrame(), [handler](API::Data* data, WebKit::CallbackBase::Error error) {
2223         void (^completionHandlerBlock)(NSData *, NSError *) = (void (^)(NSData *, NSError *))handler.get();
2224         if (error != WebKit::CallbackBase::Error::None) {
2225             // FIXME: Pipe a proper error in from the WebPageProxy.
2226             RetainPtr<NSError> error = adoptNS([[NSError alloc] init]);
2227             completionHandlerBlock(nil, error.get());
2228         } else
2229             completionHandlerBlock(wrapper(*data), nil);
2230     });
2231 }
2232
2233 - (_WKPaginationMode)_paginationMode
2234 {
2235     switch (_page->paginationMode()) {
2236     case WebCore::Pagination::Unpaginated:
2237         return _WKPaginationModeUnpaginated;
2238     case WebCore::Pagination::LeftToRightPaginated:
2239         return _WKPaginationModeLeftToRight;
2240     case WebCore::Pagination::RightToLeftPaginated:
2241         return _WKPaginationModeRightToLeft;
2242     case WebCore::Pagination::TopToBottomPaginated:
2243         return _WKPaginationModeTopToBottom;
2244     case WebCore::Pagination::BottomToTopPaginated:
2245         return _WKPaginationModeBottomToTop;
2246     }
2247
2248     ASSERT_NOT_REACHED();
2249     return _WKPaginationModeUnpaginated;
2250 }
2251
2252 - (void)_setPaginationMode:(_WKPaginationMode)paginationMode
2253 {
2254     WebCore::Pagination::Mode mode;
2255     switch (paginationMode) {
2256     case _WKPaginationModeUnpaginated:
2257         mode = WebCore::Pagination::Unpaginated;
2258         break;
2259     case _WKPaginationModeLeftToRight:
2260         mode = WebCore::Pagination::LeftToRightPaginated;
2261         break;
2262     case _WKPaginationModeRightToLeft:
2263         mode = WebCore::Pagination::RightToLeftPaginated;
2264         break;
2265     case _WKPaginationModeTopToBottom:
2266         mode = WebCore::Pagination::TopToBottomPaginated;
2267         break;
2268     case _WKPaginationModeBottomToTop:
2269         mode = WebCore::Pagination::BottomToTopPaginated;
2270         break;
2271     default:
2272         return;
2273     }
2274
2275     _page->setPaginationMode(mode);
2276 }
2277
2278 - (BOOL)_paginationBehavesLikeColumns
2279 {
2280     return _page->paginationBehavesLikeColumns();
2281 }
2282
2283 - (void)_setPaginationBehavesLikeColumns:(BOOL)behavesLikeColumns
2284 {
2285     _page->setPaginationBehavesLikeColumns(behavesLikeColumns);
2286 }
2287
2288 - (CGFloat)_pageLength
2289 {
2290     return _page->pageLength();
2291 }
2292
2293 - (void)_setPageLength:(CGFloat)pageLength
2294 {
2295     _page->setPageLength(pageLength);
2296 }
2297
2298 - (CGFloat)_gapBetweenPages
2299 {
2300     return _page->gapBetweenPages();
2301 }
2302
2303 - (void)_setGapBetweenPages:(CGFloat)gapBetweenPages
2304 {
2305     _page->setGapBetweenPages(gapBetweenPages);
2306 }
2307
2308 - (NSUInteger)_pageCount
2309 {
2310     return _page->pageCount();
2311 }
2312
2313 - (BOOL)_supportsTextZoom
2314 {
2315     return _page->supportsTextZoom();
2316 }
2317
2318 - (double)_textZoomFactor
2319 {
2320     return _page->textZoomFactor();
2321 }
2322
2323 - (void)_setTextZoomFactor:(double)zoomFactor
2324 {
2325     _page->setTextZoomFactor(zoomFactor);
2326 }
2327
2328 - (double)_pageZoomFactor
2329 {
2330     return _page->pageZoomFactor();
2331 }
2332
2333 - (void)_setPageZoomFactor:(double)zoomFactor
2334 {
2335     _page->setPageZoomFactor(zoomFactor);
2336 }
2337
2338 - (id <_WKDiagnosticLoggingDelegate>)_diagnosticLoggingDelegate
2339 {
2340     return [static_cast<WebKit::DiagnosticLoggingClient&>(_page->diagnosticLoggingClient()).delegate().leakRef() autorelease];
2341 }
2342
2343 - (void)_setDiagnosticLoggingDelegate:(id<_WKDiagnosticLoggingDelegate>)diagnosticLoggingDelegate
2344 {
2345     static_cast<WebKit::DiagnosticLoggingClient&>(_page->diagnosticLoggingClient()).setDelegate(diagnosticLoggingDelegate);
2346 }
2347
2348 - (id <_WKFindDelegate>)_findDelegate
2349 {
2350     return [static_cast<WebKit::FindClient&>(_page->findClient()).delegate().leakRef() autorelease];
2351 }
2352
2353 - (void)_setFindDelegate:(id<_WKFindDelegate>)findDelegate
2354 {
2355     static_cast<WebKit::FindClient&>(_page->findClient()).setDelegate(findDelegate);
2356 }
2357
2358 static inline WebKit::FindOptions toFindOptions(_WKFindOptions wkFindOptions)
2359 {
2360     unsigned findOptions = 0;
2361
2362     if (wkFindOptions & _WKFindOptionsCaseInsensitive)
2363         findOptions |= WebKit::FindOptionsCaseInsensitive;
2364     if (wkFindOptions & _WKFindOptionsAtWordStarts)
2365         findOptions |= WebKit::FindOptionsAtWordStarts;
2366     if (wkFindOptions & _WKFindOptionsTreatMedialCapitalAsWordStart)
2367         findOptions |= WebKit::FindOptionsTreatMedialCapitalAsWordStart;
2368     if (wkFindOptions & _WKFindOptionsBackwards)
2369         findOptions |= WebKit::FindOptionsBackwards;
2370     if (wkFindOptions & _WKFindOptionsWrapAround)
2371         findOptions |= WebKit::FindOptionsWrapAround;
2372     if (wkFindOptions & _WKFindOptionsShowOverlay)
2373         findOptions |= WebKit::FindOptionsShowOverlay;
2374     if (wkFindOptions & _WKFindOptionsShowFindIndicator)
2375         findOptions |= WebKit::FindOptionsShowFindIndicator;
2376     if (wkFindOptions & _WKFindOptionsShowHighlight)
2377         findOptions |= WebKit::FindOptionsShowHighlight;
2378     if (wkFindOptions & _WKFindOptionsDetermineMatchIndex)
2379         findOptions |= WebKit::FindOptionsDetermineMatchIndex;
2380
2381     return static_cast<WebKit::FindOptions>(findOptions);
2382 }
2383
2384 - (void)_countStringMatches:(NSString *)string options:(_WKFindOptions)options maxCount:(NSUInteger)maxCount
2385 {
2386 #if PLATFORM(IOS)
2387     if (_customContentView) {
2388         [_customContentView web_countStringMatches:string options:options maxCount:maxCount];
2389         return;
2390     }
2391 #endif
2392     _page->countStringMatches(string, toFindOptions(options), maxCount);
2393 }
2394
2395 - (void)_findString:(NSString *)string options:(_WKFindOptions)options maxCount:(NSUInteger)maxCount
2396 {
2397 #if PLATFORM(IOS)
2398     if (_customContentView) {
2399         [_customContentView web_findString:string options:options maxCount:maxCount];
2400         return;
2401     }
2402 #endif
2403     _page->findString(string, toFindOptions(options), maxCount);
2404 }
2405
2406 - (void)_hideFindUI
2407 {
2408 #if PLATFORM(IOS)
2409     if (_customContentView) {
2410         [_customContentView web_hideFindUI];
2411         return;
2412     }
2413 #endif
2414     _page->hideFindUI();
2415 }
2416
2417 - (void)_saveBackForwardSnapshotForItem:(WKBackForwardListItem *)item
2418 {
2419     _page->recordNavigationSnapshot(item._item);
2420 }
2421
2422 - (id <_WKFormDelegate>)_formDelegate
2423 {
2424     return _formDelegate.getAutoreleased();
2425 }
2426
2427 - (void)_setFormDelegate:(id <_WKFormDelegate>)formDelegate
2428 {
2429     _formDelegate = formDelegate;
2430
2431     class FormClient : public API::FormClient {
2432     public:
2433         explicit FormClient(WKWebView *webView)
2434             : m_webView(webView)
2435         {
2436         }
2437
2438         virtual ~FormClient() { }
2439
2440         virtual void willSubmitForm(WebKit::WebPageProxy&, WebKit::WebFrameProxy&, WebKit::WebFrameProxy& sourceFrame, const Vector<std::pair<WTF::String, WTF::String>>& textFieldValues, API::Object* userData, Ref<WebKit::WebFormSubmissionListenerProxy>&& listener) override
2441         {
2442             if (userData && userData->type() != API::Object::Type::Data) {
2443                 ASSERT(!userData || userData->type() == API::Object::Type::Data);
2444                 m_webView->_page->process().connection()->markCurrentlyDispatchedMessageAsInvalid();
2445                 listener->continueSubmission();
2446                 return;
2447             }
2448
2449             auto formDelegate = m_webView->_formDelegate.get();
2450
2451             if (![formDelegate respondsToSelector:@selector(_webView:willSubmitFormValues:userObject:submissionHandler:)]) {
2452                 listener->continueSubmission();
2453                 return;
2454             }
2455
2456             auto valueMap = adoptNS([[NSMutableDictionary alloc] initWithCapacity:textFieldValues.size()]);
2457             for (const auto& pair : textFieldValues)
2458                 [valueMap setObject:pair.second forKey:pair.first];
2459
2460             NSObject <NSSecureCoding> *userObject = nil;
2461             if (API::Data* data = static_cast<API::Data*>(userData)) {
2462                 auto nsData = adoptNS([[NSData alloc] initWithBytesNoCopy:const_cast<void*>(static_cast<const void*>(data->bytes())) length:data->size() freeWhenDone:NO]);
2463                 auto unarchiver = adoptNS([[NSKeyedUnarchiver alloc] initForReadingWithData:nsData.get()]);
2464                 [unarchiver setRequiresSecureCoding:YES];
2465                 @try {
2466                     userObject = [unarchiver decodeObjectOfClass:[NSObject class] forKey:@"userObject"];
2467                 } @catch (NSException *exception) {
2468                     LOG_ERROR("Failed to decode user data: %@", exception);
2469                 }
2470             }
2471
2472             RefPtr<WebKit::WebFormSubmissionListenerProxy> localListener = WTF::move(listener);
2473             RefPtr<WebKit::CompletionHandlerCallChecker> checker = WebKit::CompletionHandlerCallChecker::create(formDelegate.get(), @selector(_webView:willSubmitFormValues:userObject:submissionHandler:));
2474             [formDelegate _webView:m_webView willSubmitFormValues:valueMap.get() userObject:userObject submissionHandler:[localListener, checker] {
2475                 checker->didCallCompletionHandler();
2476                 localListener->continueSubmission();
2477             }];
2478         }
2479
2480     private:
2481         WKWebView *m_webView;
2482     };
2483
2484     if (formDelegate)
2485         _page->setFormClient(std::make_unique<FormClient>(self));
2486     else
2487         _page->setFormClient(nullptr);
2488 }
2489
2490 - (BOOL)_isDisplayingStandaloneImageDocument
2491 {
2492     if (auto* mainFrame = _page->mainFrame())
2493         return mainFrame->isDisplayingStandaloneImageDocument();
2494     return NO;
2495 }
2496
2497 - (BOOL)_isDisplayingStandaloneMediaDocument
2498 {
2499     if (auto* mainFrame = _page->mainFrame())
2500         return mainFrame->isDisplayingStandaloneMediaDocument();
2501     return NO;
2502 }
2503
2504 - (BOOL)_isShowingNavigationGestureSnapshot
2505 {
2506     return _page->isShowingNavigationGestureSnapshot();
2507 }
2508
2509 - (_WKLayoutMode)_layoutMode
2510 {
2511 #if PLATFORM(MAC)
2512     switch ([_wkView _layoutMode]) {
2513     case kWKLayoutModeFixedSize:
2514         return _WKLayoutModeFixedSize;
2515     case kWKLayoutModeDynamicSizeComputedFromViewScale:
2516         return _WKLayoutModeDynamicSizeComputedFromViewScale;
2517     case kWKLayoutModeDynamicSizeComputedFromMinimumDocumentSize:
2518         return _WKLayoutModeDynamicSizeComputedFromMinimumDocumentSize;
2519     case kWKLayoutModeViewSize:
2520     default:
2521         return _WKLayoutModeViewSize;
2522     }
2523 #else
2524     return _page->useFixedLayout() ? _WKLayoutModeFixedSize : _WKLayoutModeViewSize;
2525 #endif
2526 }
2527
2528 - (void)_setLayoutMode:(_WKLayoutMode)layoutMode
2529 {
2530 #if PLATFORM(MAC)
2531     WKLayoutMode wkViewLayoutMode;
2532     switch (layoutMode) {
2533     case _WKLayoutModeFixedSize:
2534         wkViewLayoutMode = kWKLayoutModeFixedSize;
2535         break;
2536     case _WKLayoutModeDynamicSizeComputedFromViewScale:
2537         wkViewLayoutMode = kWKLayoutModeDynamicSizeComputedFromViewScale;
2538         break;
2539     case _WKLayoutModeDynamicSizeComputedFromMinimumDocumentSize:
2540         wkViewLayoutMode = kWKLayoutModeDynamicSizeComputedFromMinimumDocumentSize;
2541         break;
2542     case _WKLayoutModeViewSize:
2543     default:
2544         wkViewLayoutMode = kWKLayoutModeViewSize;
2545         break;
2546     }
2547     [_wkView _setLayoutMode:wkViewLayoutMode];
2548 #else
2549     _page->setUseFixedLayout(layoutMode == _WKLayoutModeFixedSize || layoutMode == _WKLayoutModeDynamicSizeComputedFromViewScale);
2550 #endif
2551 }
2552
2553 - (CGSize)_fixedLayoutSize
2554 {
2555     return _page->fixedLayoutSize();
2556 }
2557
2558 - (void)_setFixedLayoutSize:(CGSize)fixedLayoutSize
2559 {
2560     _page->setFixedLayoutSize(WebCore::expandedIntSize(WebCore::FloatSize(fixedLayoutSize)));
2561 }
2562
2563 - (CGFloat)_viewScale
2564 {
2565     return _page->viewScaleFactor();
2566 }
2567
2568 - (void)_setViewScale:(CGFloat)viewScale
2569 {
2570 #if PLATFORM(MAC)
2571     [_wkView _setViewScale:viewScale];
2572 #else
2573     if (viewScale <= 0 || isnan(viewScale) || isinf(viewScale))
2574         [NSException raise:NSInvalidArgumentException format:@"View scale should be a positive number"];
2575
2576     _page->scaleView(viewScale);
2577 #endif
2578 }
2579
2580 #pragma mark scrollperf methods
2581
2582 - (void)_setScrollPerformanceDataCollectionEnabled:(BOOL)enabled
2583 {
2584     _page->setScrollPerformanceDataCollectionEnabled(enabled);
2585 }
2586
2587 - (BOOL)_scrollPerformanceDataCollectionEnabled
2588 {
2589     return _page->scrollPerformanceDataCollectionEnabled();
2590 }
2591
2592 - (NSArray *)_scrollPerformanceData
2593 {
2594 #if PLATFORM(IOS)
2595     if (WebKit::RemoteLayerTreeScrollingPerformanceData* scrollPerfData = _page->scrollingPerformanceData())
2596         return scrollPerfData->data();
2597 #endif
2598     return nil;
2599 }
2600
2601 #pragma mark media playback restrictions
2602
2603 - (BOOL)_allowsMediaDocumentInlinePlayback
2604 {
2605 #if PLATFORM(IOS)
2606     return _page->allowsMediaDocumentInlinePlayback();
2607 #else
2608     return NO;
2609 #endif
2610 }
2611
2612 - (void)_setAllowsMediaDocumentInlinePlayback:(BOOL)flag
2613 {
2614 #if PLATFORM(IOS)
2615     _page->setAllowsMediaDocumentInlinePlayback(flag);
2616 #endif
2617 }
2618
2619 #pragma mark iOS-specific methods
2620
2621 #if PLATFORM(IOS)
2622
2623 - (CGSize)_minimumLayoutSizeOverride
2624 {
2625     ASSERT(_overridesMinimumLayoutSize);
2626     return _minimumLayoutSizeOverride;
2627 }
2628
2629 - (void)_setMinimumLayoutSizeOverride:(CGSize)minimumLayoutSizeOverride
2630 {
2631     _overridesMinimumLayoutSize = YES;
2632     if (CGSizeEqualToSize(_minimumLayoutSizeOverride, minimumLayoutSizeOverride))
2633         return;
2634
2635     _minimumLayoutSizeOverride = minimumLayoutSizeOverride;
2636     if (_dynamicViewportUpdateMode == DynamicViewportUpdateMode::NotResizing)
2637         _page->setViewportConfigurationMinimumLayoutSize(WebCore::FloatSize(minimumLayoutSizeOverride));
2638 }
2639
2640 - (UIEdgeInsets)_obscuredInsets
2641 {
2642     return _obscuredInsets;
2643 }
2644
2645 - (void)_setObscuredInsets:(UIEdgeInsets)obscuredInsets
2646 {
2647     ASSERT(obscuredInsets.top >= 0);
2648     ASSERT(obscuredInsets.left >= 0);
2649     ASSERT(obscuredInsets.bottom >= 0);
2650     ASSERT(obscuredInsets.right >= 0);
2651     
2652     _haveSetObscuredInsets = YES;
2653
2654     if (UIEdgeInsetsEqualToEdgeInsets(_obscuredInsets, obscuredInsets))
2655         return;
2656
2657     _obscuredInsets = obscuredInsets;
2658
2659     [self _updateVisibleContentRects];
2660 }
2661
2662 - (void)_setInterfaceOrientationOverride:(UIInterfaceOrientation)interfaceOrientation
2663 {
2664     if (!_overridesInterfaceOrientation)
2665         [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidRotateNotification object:nil];
2666
2667     _overridesInterfaceOrientation = YES;
2668
2669     if (interfaceOrientation == _interfaceOrientationOverride)
2670         return;
2671
2672     _interfaceOrientationOverride = interfaceOrientation;
2673
2674     if (_dynamicViewportUpdateMode == DynamicViewportUpdateMode::NotResizing)
2675         _page->setDeviceOrientation(deviceOrientationForUIInterfaceOrientation(_interfaceOrientationOverride));
2676 }
2677
2678 - (UIInterfaceOrientation)_interfaceOrientationOverride
2679 {
2680     ASSERT(_overridesInterfaceOrientation);
2681     return _interfaceOrientationOverride;
2682 }
2683
2684 - (CGSize)_maximumUnobscuredSizeOverride
2685 {
2686     ASSERT(_overridesMaximumUnobscuredSize);
2687     return _maximumUnobscuredSizeOverride;
2688 }
2689
2690 - (void)_setMaximumUnobscuredSizeOverride:(CGSize)size
2691 {
2692     ASSERT(size.width <= self.bounds.size.width && size.height <= self.bounds.size.height);
2693     _overridesMaximumUnobscuredSize = YES;
2694     if (CGSizeEqualToSize(_maximumUnobscuredSizeOverride, size))
2695         return;
2696
2697     _maximumUnobscuredSizeOverride = size;
2698     if (_dynamicViewportUpdateMode == DynamicViewportUpdateMode::NotResizing)
2699         _page->setMaximumUnobscuredSize(WebCore::FloatSize(size));
2700 }
2701
2702 - (void)_setBackgroundExtendsBeyondPage:(BOOL)backgroundExtends
2703 {
2704     _page->setBackgroundExtendsBeyondPage(backgroundExtends);
2705 }
2706
2707 - (BOOL)_backgroundExtendsBeyondPage
2708 {
2709     return _page->backgroundExtendsBeyondPage();
2710 }
2711
2712 - (void)_beginInteractiveObscuredInsetsChange
2713 {
2714     ASSERT(!_isChangingObscuredInsetsInteractively);
2715     _isChangingObscuredInsetsInteractively = YES;
2716 }
2717
2718 - (void)_endInteractiveObscuredInsetsChange
2719 {
2720     ASSERT(_isChangingObscuredInsetsInteractively);
2721     _isChangingObscuredInsetsInteractively = NO;
2722     [self _updateVisibleContentRects];
2723 }
2724
2725 - (void)_hideContentUntilNextUpdate
2726 {
2727     if (auto* area = _page->drawingArea())
2728         area->hideContentUntilAnyUpdate();
2729 }
2730
2731 - (void)_beginAnimatedResizeWithUpdates:(void (^)(void))updateBlock
2732 {
2733     CGRect oldBounds = self.bounds;
2734     WebCore::FloatRect oldUnobscuredContentRect = _page->unobscuredContentRect();
2735
2736     if (_customContentView || !_hasCommittedLoadForMainFrame || CGRectIsEmpty(oldBounds) || oldUnobscuredContentRect.isEmpty()) {
2737         updateBlock();
2738         return;
2739     }
2740
2741     _dynamicViewportUpdateMode = DynamicViewportUpdateMode::ResizingWithAnimation;
2742
2743     WebCore::FloatSize oldMinimumLayoutSize = activeMinimumLayoutSize(self, oldBounds);
2744     WebCore::FloatSize oldMaximumUnobscuredSize = activeMaximumUnobscuredSize(self, oldBounds);
2745     int32_t oldOrientation = activeOrientation(self);
2746     UIEdgeInsets oldObscuredInsets = _obscuredInsets;
2747
2748     updateBlock();
2749
2750     CGRect newBounds = self.bounds;
2751     WebCore::FloatSize newMinimumLayoutSize = activeMinimumLayoutSize(self, newBounds);
2752     WebCore::FloatSize newMaximumUnobscuredSize = activeMaximumUnobscuredSize(self, newBounds);
2753     int32_t newOrientation = activeOrientation(self);
2754     UIEdgeInsets newObscuredInsets = _obscuredInsets;
2755     CGRect futureUnobscuredRectInSelfCoordinates = UIEdgeInsetsInsetRect(newBounds, _obscuredInsets);
2756     CGRect contentViewBounds = [_contentView bounds];
2757
2758     ASSERT_WITH_MESSAGE(!(_overridesMinimumLayoutSize && newMinimumLayoutSize.isEmpty()), "Clients controlling the layout size should maintain a valid layout size to minimize layouts.");
2759     if (CGRectIsEmpty(newBounds) || newMinimumLayoutSize.isEmpty() || CGRectIsEmpty(futureUnobscuredRectInSelfCoordinates) || CGRectIsEmpty(contentViewBounds)) {
2760         _dynamicViewportUpdateMode = DynamicViewportUpdateMode::NotResizing;
2761         [self _frameOrBoundsChanged];
2762         if (_overridesMinimumLayoutSize)
2763             _page->setViewportConfigurationMinimumLayoutSize(WebCore::FloatSize(newMinimumLayoutSize));
2764         if (_overridesMaximumUnobscuredSize)
2765             _page->setMaximumUnobscuredSize(WebCore::FloatSize(newMaximumUnobscuredSize));
2766         if (_overridesInterfaceOrientation)
2767             _page->setDeviceOrientation(newOrientation);
2768         [self _updateVisibleContentRects];
2769         return;
2770     }
2771
2772     if (CGRectEqualToRect(oldBounds, newBounds)
2773         && oldMinimumLayoutSize == newMinimumLayoutSize
2774         && oldMaximumUnobscuredSize == newMaximumUnobscuredSize
2775         && oldOrientation == newOrientation
2776         && UIEdgeInsetsEqualToEdgeInsets(oldObscuredInsets, newObscuredInsets)) {
2777         _dynamicViewportUpdateMode = DynamicViewportUpdateMode::NotResizing;
2778         [self _updateVisibleContentRects];
2779         return;
2780     }
2781
2782     _resizeAnimationTransformAdjustments = CATransform3DIdentity;
2783
2784     NSUInteger indexOfContentView = [[_scrollView subviews] indexOfObject:_contentView.get()];
2785     _resizeAnimationView = adoptNS([[UIView alloc] init]);
2786     [_scrollView insertSubview:_resizeAnimationView.get() atIndex:indexOfContentView];
2787     [_resizeAnimationView addSubview:_contentView.get()];
2788     [_resizeAnimationView addSubview:[_contentView unscaledView]];
2789
2790     CGSize contentSizeInContentViewCoordinates = contentViewBounds.size;
2791     [_scrollView setMinimumZoomScale:std::min(newMinimumLayoutSize.width() / contentSizeInContentViewCoordinates.width, [_scrollView minimumZoomScale])];
2792     [_scrollView setMaximumZoomScale:std::max(newMinimumLayoutSize.width() / contentSizeInContentViewCoordinates.width, [_scrollView maximumZoomScale])];
2793
2794     // Compute the new scale to keep the current content width in the scrollview.
2795     CGFloat oldWebViewWidthInContentViewCoordinates = oldUnobscuredContentRect.width();
2796     CGFloat visibleContentViewWidthInContentCoordinates = std::min(contentSizeInContentViewCoordinates.width, oldWebViewWidthInContentViewCoordinates);
2797     CGFloat targetScale = newMinimumLayoutSize.width() / visibleContentViewWidthInContentCoordinates;
2798     CGFloat resizeAnimationViewAnimationScale = targetScale / contentZoomScale(self);
2799     [_resizeAnimationView setTransform:CGAffineTransformMakeScale(resizeAnimationViewAnimationScale, resizeAnimationViewAnimationScale)];
2800
2801     // Compute a new position to keep the content centered.
2802     CGPoint originalContentCenter = oldUnobscuredContentRect.center();
2803     CGPoint originalContentCenterInSelfCoordinates = [self convertPoint:originalContentCenter fromView:_contentView.get()];
2804     CGPoint futureUnobscuredRectCenterInSelfCoordinates = CGPointMake(futureUnobscuredRectInSelfCoordinates.origin.x + futureUnobscuredRectInSelfCoordinates.size.width / 2, futureUnobscuredRectInSelfCoordinates.origin.y + futureUnobscuredRectInSelfCoordinates.size.height / 2);
2805
2806     CGPoint originalContentOffset = [_scrollView contentOffset];
2807     CGPoint contentOffset = originalContentOffset;
2808     contentOffset.x += (originalContentCenterInSelfCoordinates.x - futureUnobscuredRectCenterInSelfCoordinates.x);
2809     contentOffset.y += (originalContentCenterInSelfCoordinates.y - futureUnobscuredRectCenterInSelfCoordinates.y);
2810
2811     // Limit the new offset within the scrollview, we do not want to rubber band programmatically.
2812     CGSize futureContentSizeInSelfCoordinates = CGSizeMake(contentSizeInContentViewCoordinates.width * targetScale, contentSizeInContentViewCoordinates.height * targetScale);
2813     CGFloat maxHorizontalOffset = futureContentSizeInSelfCoordinates.width - newBounds.size.width + _obscuredInsets.right;
2814     contentOffset.x = std::min(contentOffset.x, maxHorizontalOffset);
2815     CGFloat maxVerticalOffset = futureContentSizeInSelfCoordinates.height - newBounds.size.height + _obscuredInsets.bottom;
2816     contentOffset.y = std::min(contentOffset.y, maxVerticalOffset);
2817
2818     contentOffset.x = std::max(contentOffset.x, -_obscuredInsets.left);
2819     contentOffset.y = std::max(contentOffset.y, -_obscuredInsets.top);
2820
2821     // Make the top/bottom edges "sticky" within 1 pixel.
2822     if (oldUnobscuredContentRect.maxY() > contentSizeInContentViewCoordinates.height - 1)
2823         contentOffset.y = maxVerticalOffset;
2824     if (oldUnobscuredContentRect.y() < 1)
2825         contentOffset.y = -_obscuredInsets.top;
2826
2827     // FIXME: if we have content centered after double tap to zoom, we should also try to keep that rect in view.
2828     [_scrollView setContentSize:roundScrollViewContentSize(*_page, futureContentSizeInSelfCoordinates)];
2829     [_scrollView setContentOffset:contentOffset];
2830
2831     CGRect visibleRectInContentCoordinates = [self convertRect:newBounds toView:_contentView.get()];
2832     CGRect unobscuredRectInContentCoordinates = [self convertRect:futureUnobscuredRectInSelfCoordinates toView:_contentView.get()];
2833
2834     _page->dynamicViewportSizeUpdate(newMinimumLayoutSize, newMaximumUnobscuredSize, visibleRectInContentCoordinates, unobscuredRectInContentCoordinates, futureUnobscuredRectInSelfCoordinates, targetScale, newOrientation);
2835     if (WebKit::DrawingAreaProxy* drawingArea = _page->drawingArea())
2836         drawingArea->setSize(WebCore::IntSize(newBounds.size), WebCore::IntSize(), WebCore::IntSize());
2837 }
2838
2839 - (void)_endAnimatedResize
2840 {
2841     if (_dynamicViewportUpdateMode == DynamicViewportUpdateMode::NotResizing)
2842         return;
2843
2844     _page->synchronizeDynamicViewportUpdate();
2845
2846     NSUInteger indexOfResizeAnimationView = [[_scrollView subviews] indexOfObject:_resizeAnimationView.get()];
2847     [_scrollView insertSubview:_contentView.get() atIndex:indexOfResizeAnimationView];
2848     [_scrollView insertSubview:[_contentView unscaledView] atIndex:indexOfResizeAnimationView + 1];
2849
2850     CALayer *contentViewLayer = [_contentView layer];
2851     CGFloat adjustmentScale = _resizeAnimationTransformAdjustments.m11;
2852     contentViewLayer.sublayerTransform = CATransform3DIdentity;
2853
2854     CGFloat animatingScaleTarget = [[_resizeAnimationView layer] transform].m11;
2855     CALayer *contentLayer = [_contentView layer];
2856     CATransform3D contentLayerTransform = contentLayer.transform;
2857     CGFloat currentScale = [[_resizeAnimationView layer] transform].m11 * contentLayerTransform.m11;
2858
2859     // We cannot use [UIScrollView setZoomScale:] directly because the UIScrollView delegate would get a callback with
2860     // an invalid contentOffset. The real content offset is only set below.
2861     // Since there is no public API for setting both the zoomScale and the contentOffset, we set the zoomScale manually
2862     // on the zoom layer and then only change the contentOffset.
2863     CGFloat adjustedScale = adjustmentScale * currentScale;
2864     contentLayerTransform.m11 = adjustedScale;
2865     contentLayerTransform.m22 = adjustedScale;
2866     contentLayer.transform = contentLayerTransform;
2867
2868     CGPoint currentScrollOffset = [_scrollView contentOffset];
2869     double horizontalScrollAdjustement = _resizeAnimationTransformAdjustments.m41 * animatingScaleTarget;
2870     double verticalScrollAdjustment = _resizeAnimationTransformAdjustments.m42 * animatingScaleTarget;
2871
2872     [_scrollView setContentSize:roundScrollViewContentSize(*_page, [_contentView frame].size)];
2873     [_scrollView setContentOffset:CGPointMake(currentScrollOffset.x - horizontalScrollAdjustement, currentScrollOffset.y - verticalScrollAdjustment)];
2874
2875     [_resizeAnimationView removeFromSuperview];
2876     _resizeAnimationView = nil;
2877     _resizeAnimationTransformAdjustments = CATransform3DIdentity;
2878
2879     _dynamicViewportUpdateMode = DynamicViewportUpdateMode::NotResizing;
2880     [_contentView setHidden:NO];
2881     [self _updateVisibleContentRects];
2882
2883     while (!_snapshotsDeferredDuringResize.isEmpty())
2884         _snapshotsDeferredDuringResize.takeLast()();
2885 }
2886
2887 - (void)_resizeWhileHidingContentWithUpdates:(void (^)(void))updateBlock
2888 {
2889     [self _beginAnimatedResizeWithUpdates:updateBlock];
2890     if (_dynamicViewportUpdateMode == DynamicViewportUpdateMode::ResizingWithAnimation) {
2891         [_contentView setHidden:YES];
2892         _dynamicViewportUpdateMode = DynamicViewportUpdateMode::ResizingWithDocumentHidden;
2893     }
2894 }
2895
2896 - (void)_setOverlaidAccessoryViewsInset:(CGSize)inset
2897 {
2898     [_customContentView web_setOverlaidAccessoryViewsInset:inset];
2899 }
2900
2901 - (void)_snapshotRect:(CGRect)rectInViewCoordinates intoImageOfWidth:(CGFloat)imageWidth completionHandler:(void(^)(CGImageRef))completionHandler
2902 {
2903     if (_dynamicViewportUpdateMode != DynamicViewportUpdateMode::NotResizing) {
2904         // Defer snapshotting until after the current resize completes.
2905         void (^copiedCompletionHandler)(CGImageRef) = [completionHandler copy];
2906         RetainPtr<WKWebView> retainedSelf = self;
2907         _snapshotsDeferredDuringResize.append([retainedSelf, rectInViewCoordinates, imageWidth, copiedCompletionHandler] {
2908             [retainedSelf _snapshotRect:rectInViewCoordinates intoImageOfWidth:imageWidth completionHandler:copiedCompletionHandler];
2909             [copiedCompletionHandler release];
2910         });
2911         return;
2912     }
2913
2914     CGRect snapshotRectInContentCoordinates = [self convertRect:rectInViewCoordinates toView:self._currentContentView];
2915     CGFloat imageScale = imageWidth / snapshotRectInContentCoordinates.size.width;
2916     CGFloat imageHeight = imageScale * snapshotRectInContentCoordinates.size.height;
2917     CGSize imageSize = CGSizeMake(imageWidth, imageHeight);
2918
2919 #if USE(IOSURFACE)
2920     // If we are parented and thus won't incur a significant penalty from paging in tiles, snapshot the view hierarchy directly.
2921 #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 90000
2922     if (CADisplay *display = self.window.screen._display) {
2923 #else
2924     if (self.window) {
2925 #endif
2926         auto surface = WebCore::IOSurface::create(WebCore::expandedIntSize(WebCore::FloatSize(imageSize)), WebCore::ColorSpaceDeviceRGB);
2927         CGFloat imageScaleInViewCoordinates = imageWidth / rectInViewCoordinates.size.width;
2928         CATransform3D transform = CATransform3DMakeScale(imageScaleInViewCoordinates, imageScaleInViewCoordinates, 1);
2929         transform = CATransform3DTranslate(transform, -rectInViewCoordinates.origin.x, -rectInViewCoordinates.origin.y, 0);
2930 #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 90000
2931         CARenderServerRenderDisplayLayerWithTransformAndTimeOffset(MACH_PORT_NULL, (CFStringRef)display.name, self.layer.context.contextId, reinterpret_cast<uint64_t>(self.layer), surface->surface(), 0, 0, &transform, 0);
2932 #else
2933         CARenderServerRenderLayerWithTransform(MACH_PORT_NULL, self.layer.context.contextId, reinterpret_cast<uint64_t>(self.layer), surface->surface(), 0, 0, &transform); 
2934 #endif
2935         completionHandler(surface->createImage().get());
2936         return;
2937     }
2938 #endif
2939     
2940     if (_customContentView) {
2941         UIGraphicsBeginImageContextWithOptions(imageSize, YES, 1);
2942
2943         UIView *customContentView = _customContentView.get();
2944         [customContentView.backgroundColor set];
2945         UIRectFill(CGRectMake(0, 0, imageWidth, imageHeight));
2946
2947         CGContextRef context = UIGraphicsGetCurrentContext();
2948         CGContextTranslateCTM(context, -snapshotRectInContentCoordinates.origin.x * imageScale, -snapshotRectInContentCoordinates.origin.y * imageScale);
2949         CGContextScaleCTM(context, imageScale, imageScale);
2950         [customContentView.layer renderInContext:context];
2951
2952         completionHandler([UIGraphicsGetImageFromCurrentImageContext() CGImage]);
2953
2954         UIGraphicsEndImageContext();
2955         return;
2956     }
2957
2958     void(^copiedCompletionHandler)(CGImageRef) = [completionHandler copy];
2959     _page->takeSnapshot(WebCore::enclosingIntRect(snapshotRectInContentCoordinates), WebCore::expandedIntSize(WebCore::FloatSize(imageSize)), WebKit::SnapshotOptionsExcludeDeviceScaleFactor, [=](const WebKit::ShareableBitmap::Handle& imageHandle, WebKit::CallbackBase::Error) {
2960         if (imageHandle.isNull()) {
2961             copiedCompletionHandler(nullptr);
2962             [copiedCompletionHandler release];
2963             return;
2964         }
2965
2966         RefPtr<WebKit::ShareableBitmap> bitmap = WebKit::ShareableBitmap::create(imageHandle, WebKit::SharedMemory::Protection::ReadOnly);
2967
2968         if (!bitmap) {
2969             copiedCompletionHandler(nullptr);
2970             [copiedCompletionHandler release];
2971             return;
2972         }
2973
2974         RetainPtr<CGImageRef> cgImage;
2975         cgImage = bitmap->makeCGImage();
2976         copiedCompletionHandler(cgImage.get());
2977         [copiedCompletionHandler release];
2978     });
2979 }
2980
2981 - (void)_overrideLayoutParametersWithMinimumLayoutSize:(CGSize)minimumLayoutSize minimumLayoutSizeForMinimalUI:(CGSize)minimumLayoutSizeForMinimalUI maximumUnobscuredSizeOverride:(CGSize)maximumUnobscuredSizeOverride
2982 {
2983     UNUSED_PARAM(minimumLayoutSizeForMinimalUI);
2984     [self _overrideLayoutParametersWithMinimumLayoutSize:minimumLayoutSize maximumUnobscuredSizeOverride:maximumUnobscuredSizeOverride];
2985 }
2986
2987 - (void)_overrideLayoutParametersWithMinimumLayoutSize:(CGSize)minimumLayoutSize maximumUnobscuredSizeOverride:(CGSize)maximumUnobscuredSizeOverride
2988 {
2989     [self _setMinimumLayoutSizeOverride:minimumLayoutSize];
2990     [self _setMaximumUnobscuredSizeOverride:maximumUnobscuredSizeOverride];
2991 }
2992
2993 - (UIView *)_viewForFindUI
2994 {
2995     return [self viewForZoomingInScrollView:[self scrollView]];
2996 }
2997
2998 - (BOOL)_isDisplayingPDF
2999 {
3000     return [_customContentView isKindOfClass:[WKPDFView class]];
3001 }
3002
3003 - (NSData *)_dataForDisplayedPDF
3004 {
3005     if (![self _isDisplayingPDF])
3006         return nil;
3007     CGPDFDocumentRef pdfDocument = [(WKPDFView *)_customContentView pdfDocument];
3008     return [(NSData *)CGDataProviderCopyData(CGPDFDocumentGetDataProvider(pdfDocument)) autorelease];
3009 }
3010
3011 - (NSString *)_suggestedFilenameForDisplayedPDF
3012 {
3013     if (![self _isDisplayingPDF])
3014         return nil;
3015     return [(WKPDFView *)_customContentView.get() suggestedFilename];
3016 }
3017
3018 - (CGFloat)_viewportMetaTagWidth
3019 {
3020     return _viewportMetaTagWidth;
3021 }
3022
3023 - (CGRect)_contentVisibleRect
3024 {
3025     return [self convertRect:[self bounds] toView:self._currentContentView];
3026 }
3027
3028 - (CGPoint)_convertPointFromContentsToView:(CGPoint)point
3029 {
3030     return [self convertPoint:point fromView:self._currentContentView];
3031 }
3032
3033 - (CGPoint)_convertPointFromViewToContents:(CGPoint)point
3034 {
3035     return [self convertPoint:point toView:self._currentContentView];
3036 }
3037
3038 - (_WKWebViewPrintFormatter *)_webViewPrintFormatter
3039 {
3040     UIViewPrintFormatter *viewPrintFormatter = self.viewPrintFormatter;
3041     ASSERT([viewPrintFormatter isKindOfClass:[_WKWebViewPrintFormatter class]]);
3042     return (_WKWebViewPrintFormatter *)viewPrintFormatter;
3043 }
3044
3045 #else // #if PLATFORM(IOS)
3046
3047 #pragma mark - OS X-specific methods
3048
3049 - (NSColor *)_pageExtendedBackgroundColor
3050 {
3051     WebCore::Color color = _page->pageExtendedBackgroundColor();
3052     if (!color.isValid())
3053         return nil;
3054
3055     return nsColor(color);
3056 }
3057
3058 - (BOOL)_drawsTransparentBackground
3059 {
3060     return _page->drawsTransparentBackground();
3061 }
3062
3063 - (void)_setDrawsTransparentBackground:(BOOL)drawsTransparentBackground
3064 {
3065     _page->setDrawsTransparentBackground(drawsTransparentBackground);
3066 }
3067
3068 - (void)_setOverrideDeviceScaleFactor:(CGFloat)deviceScaleFactor
3069 {
3070     [_wkView _setOverrideDeviceScaleFactor:deviceScaleFactor];
3071 }
3072
3073 - (CGFloat)_overrideDeviceScaleFactor
3074 {
3075     return [_wkView _overrideDeviceScaleFactor];
3076 }
3077
3078 - (void)_setTopContentInset:(CGFloat)contentInset
3079 {
3080     [_wkView _setTopContentInset:contentInset];
3081 }
3082
3083 - (CGFloat)_topContentInset
3084 {
3085     return [_wkView _topContentInset];
3086 }
3087
3088 - (BOOL)_windowOcclusionDetectionEnabled
3089 {
3090     return [_wkView windowOcclusionDetectionEnabled];
3091 }
3092
3093 - (void)_setWindowOcclusionDetectionEnabled:(BOOL)flag
3094 {
3095     [_wkView setWindowOcclusionDetectionEnabled:flag];
3096 }
3097
3098 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
3099
3100 - (void)_setAutomaticallyAdjustsContentInsets:(BOOL)automaticallyAdjustsContentInsets
3101 {
3102     [_wkView _setAutomaticallyAdjustsContentInsets:automaticallyAdjustsContentInsets];
3103 }
3104
3105 - (BOOL)_automaticallyAdjustsContentInsets
3106 {
3107     return [_wkView _automaticallyAdjustsContentInsets];
3108 }
3109
3110 #endif
3111
3112 #endif
3113
3114 @end
3115
3116 #if !TARGET_OS_IPHONE
3117
3118 @implementation WKWebView (WKIBActions)
3119
3120 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)item
3121 {
3122     SEL action = item.action;
3123
3124     if (action == @selector(goBack:))
3125         return !!_page->backForwardList().backItem();
3126
3127     if (action == @selector(goForward:))
3128         return !!_page->backForwardList().forwardItem();
3129
3130     if (action == @selector(stopLoading:)) {
3131         // FIXME: Return no if we're stopped.
3132         return YES;
3133     }
3134
3135     if (action == @selector(reload:) || action == @selector(reloadFromOrigin:)) {
3136         // FIXME: Return no if we're loading.
3137         return YES;
3138     }
3139
3140     return NO;
3141 }
3142
3143 - (IBAction)goBack:(id)sender
3144 {
3145     [self goBack];
3146 }
3147
3148 - (IBAction)goForward:(id)sender
3149 {
3150     [self goForward];
3151 }
3152
3153 - (IBAction)reload:(id)sender
3154 {
3155     [self reload];
3156 }
3157
3158 - (IBAction)reloadFromOrigin:(id)sender
3159 {
3160     [self reloadFromOrigin];
3161 }
3162
3163 - (IBAction)stopLoading:(id)sender
3164 {
3165     _page->stopLoading();
3166 }
3167
3168 @end
3169
3170 #endif
3171
3172 #if PLATFORM(IOS)
3173 @implementation WKWebView (_WKWebViewPrintFormatter)
3174
3175 - (Class)_printFormatterClass
3176 {
3177     return [_WKWebViewPrintFormatter class];
3178 }
3179
3180 - (NSInteger)_computePageCountAndStartDrawingToPDFForFrame:(_WKFrameHandle *)frame printInfo:(const WebKit::PrintInfo&)printInfo firstPage:(uint32_t)firstPage computedTotalScaleFactor:(double&)totalScaleFactor
3181 {
3182     if ([self _isDisplayingPDF])
3183         return CGPDFDocumentGetNumberOfPages([(WKPDFView *)_customContentView pdfDocument]);
3184
3185     _pageIsPrintingToPDF = YES;
3186     Vector<WebCore::IntRect> pageRects;
3187     uint64_t frameID = frame ? frame._frameID : _page->mainFrame()->frameID();
3188     if (!_page->sendSync(Messages::WebPage::ComputePagesForPrintingAndStartDrawingToPDF(frameID, printInfo, firstPage), Messages::WebPage::ComputePagesForPrintingAndStartDrawingToPDF::Reply(pageRects, totalScaleFactor)))
3189         return 0;
3190     return pageRects.size();
3191 }
3192
3193 - (void)_endPrinting
3194 {
3195     _pageIsPrintingToPDF = NO;
3196     _printedDocument = nullptr;
3197     _page->send(Messages::WebPage::EndPrinting());
3198 }
3199
3200 // FIXME: milliseconds::max() overflows when converted to nanoseconds, causing condition_variable::wait_for() to believe
3201 // a timeout occurred on any spurious wakeup. Use nanoseconds::max() (converted to ms) to avoid this. We should perhaps
3202 // change waitForAndDispatchImmediately() to take nanoseconds to avoid this issue.
3203 static constexpr std::chrono::milliseconds didFinishLoadingTimeout = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::nanoseconds::max());
3204
3205 - (CGPDFDocumentRef)_printedDocument
3206 {
3207     if ([self _isDisplayingPDF]) {
3208         ASSERT(!_pageIsPrintingToPDF);
3209         return [(WKPDFView *)_customContentView pdfDocument];
3210     }
3211
3212     if (_pageIsPrintingToPDF) {
3213         if (!_page->process().connection()->waitForAndDispatchImmediately<Messages::WebPageProxy::DidFinishDrawingPagesToPDF>(_page->pageID(), didFinishLoadingTimeout)) {
3214             ASSERT_NOT_REACHED();
3215             return nullptr;
3216         }
3217         ASSERT(!_pageIsPrintingToPDF);
3218     }
3219     return _printedDocument.get();
3220 }
3221
3222 - (void)_setPrintedDocument:(CGPDFDocumentRef)printedDocument
3223 {
3224     if (!_pageIsPrintingToPDF)
3225         return;
3226     ASSERT(![self _isDisplayingPDF]);
3227     _printedDocument = printedDocument;
3228     _pageIsPrintingToPDF = NO;
3229 }
3230
3231 @end
3232 #endif
3233
3234 #endif // WK_API_ENABLED