2 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #import "WKWebViewInternal.h"
31 #import "APIFormClient.h"
32 #import "FindClient.h"
33 #import "LegacySessionStateCoding.h"
34 #import "NavigationState.h"
35 #import "RemoteLayerTreeTransaction.h"
36 #import "RemoteObjectRegistry.h"
37 #import "RemoteObjectRegistryMessages.h"
38 #import "UIDelegate.h"
39 #import "ViewGestureController.h"
40 #import "ViewSnapshotStore.h"
41 #import "WKBackForwardListInternal.h"
42 #import "WKBackForwardListItemInternal.h"
43 #import "WKBrowsingContextHandleInternal.h"
44 #import "WKErrorInternal.h"
45 #import "WKHistoryDelegatePrivate.h"
47 #import "WKNSURLExtras.h"
48 #import "WKNavigationDelegate.h"
49 #import "WKNavigationInternal.h"
50 #import "WKPreferencesInternal.h"
51 #import "WKProcessPoolInternal.h"
52 #import "WKUIDelegate.h"
53 #import "WKUIDelegatePrivate.h"
54 #import "WKUserContentControllerInternal.h"
55 #import "WKWebViewConfigurationInternal.h"
56 #import "WKWebViewContentProvider.h"
57 #import "WebBackForwardList.h"
58 #import "WebCertificateInfo.h"
59 #import "WebContext.h"
60 #import "WebFormSubmissionListenerProxy.h"
61 #import "WebKitSystemInterface.h"
62 #import "WebPageGroup.h"
63 #import "WebPageProxy.h"
64 #import "WebPreferencesKeys.h"
65 #import "WebProcessProxy.h"
66 #import "WebSerializedScriptValue.h"
67 #import "_WKFindDelegate.h"
68 #import "_WKFormDelegate.h"
69 #import "_WKRemoteObjectRegistryInternal.h"
70 #import "_WKSessionStateInternal.h"
71 #import "_WKVisitedLinkProviderInternal.h"
72 #import "_WKWebsiteDataStoreInternal.h"
73 #import <JavaScriptCore/JSContext.h>
74 #import <JavaScriptCore/JSValue.h>
75 #import <wtf/HashMap.h>
76 #import <wtf/NeverDestroyed.h>
77 #import <wtf/RetainPtr.h>
80 #import "_WKFrameHandleInternal.h"
81 #import "_WKWebViewPrintFormatter.h"
83 #import "ProcessThrottler.h"
84 #import "RemoteLayerTreeDrawingAreaProxy.h"
86 #import "WKScrollView.h"
87 #import "WKWebViewContentProviderRegistry.h"
88 #import "WebPageMessages.h"
89 #import <CoreGraphics/CGFloat.h>
90 #import <CoreGraphics/CGPDFDocumentPrivate.h>
91 #import <UIKit/UIApplication.h>
92 #import <UIKit/UIDevice_Private.h>
93 #import <UIKit/UIPeripheralHost_Private.h>
94 #import <UIKit/UIWindow_Private.h>
95 #import <QuartzCore/CARenderServer.h>
96 #import <QuartzCore/QuartzCorePrivate.h>
97 #import <WebCore/InspectorOverlay.h>
99 @interface UIScrollView (UIScrollViewInternal)
100 - (void)_adjustForAutomaticKeyboardInfo:(NSDictionary*)info animated:(BOOL)animated lastAdjustment:(CGFloat*)lastAdjustment;
101 - (BOOL)_isScrollingToTop;
104 @interface UIPeripheralHost(UIKitInternal)
105 - (CGFloat)getVerticalOverlapForView:(UIView *)view usingKeyboardInfo:(NSDictionary *)info;
108 @interface UIView (UIViewInternal)
109 - (UIViewController *)_viewControllerForAncestor;
112 @interface UIWindow (UIWindowInternal)
113 - (BOOL)_isHostedInAnotherProcess;
116 @interface UIViewController (UIViewControllerInternal)
117 - (UIViewController *)_rootAncestorViewController;
118 - (UIViewController *)_viewControllerForSupportedInterfaceOrientations;
123 #import "WKViewInternal.h"
124 #import <WebCore/ColorMac.h>
128 static HashMap<WebKit::WebPageProxy*, WKWebView *>& pageToViewMap()
130 static NeverDestroyed<HashMap<WebKit::WebPageProxy*, WKWebView *>> map;
134 WKWebView* fromWebPageProxy(WebKit::WebPageProxy& page)
136 return pageToViewMap().get(&page);
139 @implementation WKWebView {
140 std::unique_ptr<WebKit::NavigationState> _navigationState;
141 std::unique_ptr<WebKit::UIDelegate> _uiDelegate;
143 RetainPtr<_WKRemoteObjectRegistry> _remoteObjectRegistry;
144 _WKRenderingProgressEvents _observedRenderingProgressEvents;
146 WebKit::WeakObjCPtr<id <_WKFormDelegate>> _formDelegate;
148 RetainPtr<WKScrollView> _scrollView;
149 RetainPtr<WKContentView> _contentView;
151 BOOL _overridesMinimumLayoutSize;
152 CGSize _minimumLayoutSizeOverride;
153 BOOL _overridesMinimumLayoutSizeForMinimalUI;
154 CGSize _minimumLayoutSizeOverrideForMinimalUI;
155 BOOL _overridesMaximumUnobscuredSize;
156 CGSize _maximumUnobscuredSizeOverride;
158 BOOL _needsToNotifyDelegateAboutMinimalUI;
159 CGRect _inputViewBounds;
160 CGFloat _viewportMetaTagWidth;
162 UIEdgeInsets _obscuredInsets;
163 bool _isChangingObscuredInsetsInteractively;
165 UIInterfaceOrientation _interfaceOrientationOverride;
166 BOOL _overridesInterfaceOrientation;
168 BOOL _needsResetViewStateAfterCommitLoadForMainFrame;
169 uint64_t _firstPaintAfterCommitLoadTransactionID;
170 BOOL _isAnimatingResize;
171 CATransform3D _resizeAnimationTransformAdjustments;
172 RetainPtr<UIView> _resizeAnimationView;
173 CGFloat _lastAdjustmentForScroller;
175 BOOL _needsToRestoreExposedRect;
176 WebCore::FloatRect _exposedRectToRestore;
177 BOOL _needsToRestoreUnobscuredCenter;
178 WebCore::FloatPoint _unobscuredCenterToRestore;
179 uint64_t _firstTransactionIDAfterPageRestore;
180 double _scaleToRestore;
182 std::unique_ptr<WebKit::ViewGestureController> _gestureController;
183 BOOL _allowsBackForwardNavigationGestures;
185 RetainPtr<UIView <WKWebViewContentProvider>> _customContentView;
186 RetainPtr<UIView> _customContentFixedOverlayView;
188 WebCore::Color _scrollViewBackgroundColor;
190 BOOL _delayUpdateVisibleContentRects;
191 BOOL _hadDelayedUpdateVisibleContentRects;
193 BOOL _pageIsPrintingToPDF;
194 RetainPtr<CGPDFDocumentRef> _printedDocument;
197 RetainPtr<WKView> _wkView;
201 - (instancetype)initWithFrame:(CGRect)frame
203 return [self initWithFrame:frame configuration:adoptNS([[WKWebViewConfiguration alloc] init]).get()];
207 static int32_t deviceOrientationForUIInterfaceOrientation(UIInterfaceOrientation orientation)
209 switch (orientation) {
210 case UIInterfaceOrientationUnknown:
211 case UIInterfaceOrientationPortrait:
213 case UIInterfaceOrientationPortraitUpsideDown:
215 case UIInterfaceOrientationLandscapeLeft:
217 case UIInterfaceOrientationLandscapeRight:
222 static int32_t deviceOrientation()
224 return deviceOrientationForUIInterfaceOrientation([[UIApplication sharedApplication] statusBarOrientation]);
228 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
230 if (!(self = [super initWithFrame:frame]))
233 _configuration = adoptNS([configuration copy]);
235 if (WKWebView *relatedWebView = [_configuration _relatedWebView]) {
236 WKProcessPool *processPool = [_configuration processPool];
237 WKProcessPool *relatedWebViewProcessPool = [relatedWebView->_configuration processPool];
238 if (processPool && processPool != relatedWebViewProcessPool)
239 [NSException raise:NSInvalidArgumentException format:@"Related web view %@ has process pool %@ but configuration specifies a different process pool %@", relatedWebView, relatedWebViewProcessPool, configuration.processPool];
241 [_configuration setProcessPool:relatedWebViewProcessPool];
244 [_configuration _validate];
246 CGRect bounds = self.bounds;
248 WebKit::WebContext& context = *[_configuration processPool]->_context;
250 WebKit::WebPageConfiguration webPageConfiguration;
251 webPageConfiguration.preferences = [_configuration preferences]->_preferences.get();
252 if (WKWebView *relatedWebView = [_configuration _relatedWebView])
253 webPageConfiguration.relatedPage = relatedWebView->_page.get();
255 webPageConfiguration.userContentController = [_configuration userContentController]->_userContentControllerProxy.get();
256 webPageConfiguration.visitedLinkProvider = [_configuration _visitedLinkProvider]->_visitedLinkProvider.get();
257 webPageConfiguration.session = [_configuration _websiteDataStore]->_session.get();
259 RefPtr<WebKit::WebPageGroup> pageGroup;
260 NSString *groupIdentifier = configuration._groupIdentifier;
261 if (groupIdentifier.length) {
262 pageGroup = WebKit::WebPageGroup::create(configuration._groupIdentifier);
263 webPageConfiguration.pageGroup = pageGroup.get();
266 webPageConfiguration.preferenceValues.set(WebKit::WebPreferencesKey::suppressesIncrementalRenderingKey(), WebKit::WebPreferencesStore::Value(!![_configuration suppressesIncrementalRendering]));
269 webPageConfiguration.preferenceValues.set(WebKit::WebPreferencesKey::mediaPlaybackAllowsInlineKey(), WebKit::WebPreferencesStore::Value(!![_configuration allowsInlineMediaPlayback]));
270 webPageConfiguration.preferenceValues.set(WebKit::WebPreferencesKey::mediaPlaybackRequiresUserGestureKey(), WebKit::WebPreferencesStore::Value(!![_configuration mediaPlaybackRequiresUserAction]));
271 webPageConfiguration.preferenceValues.set(WebKit::WebPreferencesKey::mediaPlaybackAllowsAirPlayKey(), WebKit::WebPreferencesStore::Value(!![_configuration mediaPlaybackAllowsAirPlay]));
275 _scrollView = adoptNS([[WKScrollView alloc] initWithFrame:bounds]);
276 [_scrollView setInternalDelegate:self];
277 [_scrollView setBouncesZoom:YES];
279 [self addSubview:_scrollView.get()];
280 [_scrollView setBackgroundColor:[UIColor whiteColor]];
282 _contentView = adoptNS([[WKContentView alloc] initWithFrame:bounds context:context configuration:WTF::move(webPageConfiguration) webView:self]);
284 _page = [_contentView page];
285 _page->setApplicationNameForUserAgent([@"Mobile/" stringByAppendingString:[UIDevice currentDevice].buildVersion]);
286 _page->setDeviceOrientation(deviceOrientation());
288 [_contentView layer].anchorPoint = CGPointZero;
289 [_contentView setFrame:bounds];
290 [_scrollView addSubview:_contentView.get()];
291 _viewportMetaTagWidth = -1;
293 [self _frameOrBoundsChanged];
295 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
296 [center addObserver:self selector:@selector(_keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
297 [center addObserver:self selector:@selector(_keyboardDidChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
298 [center addObserver:self selector:@selector(_keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
299 [center addObserver:self selector:@selector(_keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
300 [center addObserver:self selector:@selector(_windowDidRotate:) name:UIWindowDidRotateNotification object:nil];
301 [center addObserver:self selector:@selector(_contentSizeCategoryDidChange:) name:UIContentSizeCategoryDidChangeNotification object:nil];
302 _page->contentSizeCategoryDidChange([self _contentSizeCategory]);
304 [[_configuration _contentProviderRegistry] addPage:*_page];
308 _wkView = [[WKView alloc] initWithFrame:bounds context:context configuration:WTF::move(webPageConfiguration) webView:self];
309 [self addSubview:_wkView.get()];
310 _page = WebKit::toImpl([_wkView pageRef]);
312 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
313 [_wkView _setAutomaticallyAdjustsContentInsets:YES];
317 _page->setBackgroundExtendsBeyondPage(true);
319 _navigationState = std::make_unique<WebKit::NavigationState>(self);
320 _page->setPolicyClient(_navigationState->createPolicyClient());
321 _page->setLoaderClient(_navigationState->createLoaderClient());
323 _uiDelegate = std::make_unique<WebKit::UIDelegate>(self);
324 _page->setUIClient(_uiDelegate->createUIClient());
326 _page->setFindClient(std::make_unique<WebKit::FindClient>(self));
328 pageToViewMap().add(_page.get(), self);
333 - (instancetype)initWithCoder:(NSCoder *)coder
343 [_remoteObjectRegistry _invalidate];
345 [[_configuration _contentProviderRegistry] removePage:*_page];
346 [[NSNotificationCenter defaultCenter] removeObserver:self];
349 pageToViewMap().remove(_page.get());
354 - (WKWebViewConfiguration *)configuration
356 return [[_configuration copy] autorelease];
359 - (WKBackForwardList *)backForwardList
361 return wrapper(_page->backForwardList());
364 - (id <WKNavigationDelegate>)navigationDelegate
366 return _navigationState->navigationDelegate().autorelease();
369 - (void)setNavigationDelegate:(id <WKNavigationDelegate>)navigationDelegate
371 _navigationState->setNavigationDelegate(navigationDelegate);
374 - (id <WKUIDelegate>)UIDelegate
376 return _uiDelegate->delegate().autorelease();
379 - (void)setUIDelegate:(id<WKUIDelegate>)UIDelegate
381 _uiDelegate->setDelegate(UIDelegate);
384 - (WKNavigation *)loadRequest:(NSURLRequest *)request
386 uint64_t navigationID = _page->loadRequest(request);
387 auto navigation = _navigationState->createLoadRequestNavigation(navigationID, request);
389 return navigation.autorelease();
392 - (WKNavigation *)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
394 uint64_t navigationID = _page->loadHTMLString(string, baseURL.absoluteString);
395 auto navigation = _navigationState->createLoadDataNavigation(navigationID);
397 return navigation.autorelease();
400 - (WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item
402 uint64_t navigationID = _page->goToBackForwardItem(&item._item);
404 auto navigation = _navigationState->createBackForwardNavigation(navigationID, item._item);
406 return navigation.autorelease();
411 return _page->pageLoadState().title();
416 return [NSURL _web_URLWithWTFString:_page->pageLoadState().activeURL()];
421 return _page->pageLoadState().isLoading();
424 - (double)estimatedProgress
426 return _page->pageLoadState().estimatedProgress();
429 - (BOOL)hasOnlySecureContent
431 return _page->pageLoadState().hasOnlySecureContent();
436 return _page->pageLoadState().canGoBack();
441 return _page->pageLoadState().canGoForward();
444 - (WKNavigation *)goBack
446 uint64_t navigationID = _page->goBack();
450 ASSERT(_page->backForwardList().currentItem());
451 auto navigation = _navigationState->createBackForwardNavigation(navigationID, *_page->backForwardList().currentItem());
453 return navigation.autorelease();
456 - (WKNavigation *)goForward
458 uint64_t navigationID = _page->goForward();
462 ASSERT(_page->backForwardList().currentItem());
463 auto navigation = _navigationState->createBackForwardNavigation(navigationID, *_page->backForwardList().currentItem());
465 return navigation.autorelease();
468 - (WKNavigation *)reload
470 uint64_t navigationID = _page->reload(false);
471 ASSERT(navigationID);
473 auto navigation = _navigationState->createReloadNavigation(navigationID);
474 return navigation.autorelease();
477 - (WKNavigation *)reloadFromOrigin
479 uint64_t navigationID = _page->reload(true);
480 ASSERT(navigationID);
482 auto navigation = _navigationState->createReloadNavigation(navigationID);
483 return navigation.autorelease();
488 _page->stopLoading();
491 static WKErrorCode callbackErrorCode(WebKit::CallbackBase::Error error)
494 case WebKit::CallbackBase::Error::None:
495 ASSERT_NOT_REACHED();
496 return WKErrorUnknown;
498 case WebKit::CallbackBase::Error::Unknown:
499 return WKErrorUnknown;
501 case WebKit::CallbackBase::Error::ProcessExited:
502 return WKErrorWebContentProcessTerminated;
504 case WebKit::CallbackBase::Error::OwnerWasInvalidated:
505 return WKErrorWebViewInvalidated;
509 - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id, NSError *))completionHandler
511 auto handler = adoptNS([completionHandler copy]);
513 _page->runJavaScriptInMainFrame(javaScriptString, [handler](WebKit::WebSerializedScriptValue* serializedScriptValue, WebKit::ScriptValueCallback::Error errorCode) {
517 auto completionHandler = (void (^)(id, NSError *))handler.get();
519 if (errorCode != WebKit::ScriptValueCallback::Error::None) {
520 auto error = createNSError(callbackErrorCode(errorCode));
521 if (errorCode == WebKit::ScriptValueCallback::Error::OwnerWasInvalidated) {
522 // The OwnerWasInvalidated callback is synchronous. We don't want to call the block from within it
523 // because that can trigger re-entrancy bugs in WebKit.
524 // FIXME: It would be even better if GenericCallback did this for us.
525 dispatch_async(dispatch_get_main_queue(), [completionHandler, error] {
526 completionHandler(nil, error.get());
531 completionHandler(nil, error.get());
535 if (!serializedScriptValue) {
536 completionHandler(nil, createNSError(WKErrorJavaScriptExceptionOccurred).get());
540 auto context = adoptNS([[JSContext alloc] init]);
541 JSValueRef valueRef = serializedScriptValue->deserialize([context JSGlobalContextRef], 0);
542 JSValue *value = [JSValue valueWithJSValueRef:valueRef inContext:context.get()];
544 completionHandler([value toObject], nil);
548 #pragma mark iOS-specific methods
551 - (void)setFrame:(CGRect)frame
553 CGRect oldFrame = self.frame;
554 [super setFrame:frame];
556 if (!CGSizeEqualToSize(oldFrame.size, frame.size))
557 [self _frameOrBoundsChanged];
560 - (void)setBounds:(CGRect)bounds
562 CGRect oldBounds = self.bounds;
563 [super setBounds:bounds];
564 [_customContentFixedOverlayView setFrame:self.bounds];
566 if (!CGSizeEqualToSize(oldBounds.size, bounds.size))
567 [self _frameOrBoundsChanged];
570 - (UIScrollView *)scrollView
572 return _scrollView.get();
575 - (WKBrowsingContextController *)browsingContextController
577 return [_contentView browsingContextController];
580 static inline CGFloat floorToDevicePixel(CGFloat input, float deviceScaleFactor)
582 return CGFloor(input * deviceScaleFactor) / deviceScaleFactor;
585 static CGSize roundScrollViewContentSize(const WebKit::WebPageProxy& page, CGSize contentSize)
587 float deviceScaleFactor = page.deviceScaleFactor();
588 return CGSizeMake(floorToDevicePixel(contentSize.width, deviceScaleFactor), floorToDevicePixel(contentSize.height, deviceScaleFactor));
591 - (UIView *)_currentContentView
593 return _customContentView ? _customContentView.get() : _contentView.get();
596 - (void)_setHasCustomContentView:(BOOL)pageHasCustomContentView loadedMIMEType:(const WTF::String&)mimeType
598 if (pageHasCustomContentView) {
599 [_customContentView removeFromSuperview];
600 [_customContentFixedOverlayView removeFromSuperview];
602 Class representationClass = [[_configuration _contentProviderRegistry] providerForMIMEType:mimeType];
603 ASSERT(representationClass);
604 _customContentView = adoptNS([[representationClass alloc] web_initWithFrame:self.bounds webView:self]);
605 _customContentFixedOverlayView = adoptNS([[UIView alloc] initWithFrame:self.bounds]);
606 [_customContentFixedOverlayView setUserInteractionEnabled:NO];
608 [_contentView removeFromSuperview];
609 [_scrollView addSubview:_customContentView.get()];
610 [self addSubview:_customContentFixedOverlayView.get()];
612 [_customContentView web_setMinimumSize:self.bounds.size];
613 [_customContentView web_setFixedOverlayView:_customContentFixedOverlayView.get()];
614 } else if (_customContentView) {
615 [_customContentView removeFromSuperview];
616 _customContentView = nullptr;
618 [_customContentFixedOverlayView removeFromSuperview];
619 _customContentFixedOverlayView = nullptr;
621 [_scrollView addSubview:_contentView.get()];
622 [_scrollView setContentSize:roundScrollViewContentSize(*_page, [_contentView frame].size)];
624 [_customContentFixedOverlayView setFrame:self.bounds];
625 [self addSubview:_customContentFixedOverlayView.get()];
629 - (void)_didFinishLoadingDataForCustomContentProviderWithSuggestedFilename:(const String&)suggestedFilename data:(NSData *)data
631 ASSERT(_customContentView);
632 [_customContentView web_setContentProviderData:data suggestedFilename:suggestedFilename];
635 - (void)_setViewportMetaTagWidth:(float)newWidth
637 _viewportMetaTagWidth = newWidth;
640 - (void)_willInvokeUIScrollViewDelegateCallback
642 _delayUpdateVisibleContentRects = YES;
645 - (void)_didInvokeUIScrollViewDelegateCallback
647 _delayUpdateVisibleContentRects = NO;
648 if (_hadDelayedUpdateVisibleContentRects) {
649 _hadDelayedUpdateVisibleContentRects = NO;
650 [self _updateVisibleContentRects];
654 static CGFloat contentZoomScale(WKWebView* webView)
656 CGFloat scale = [[webView._currentContentView layer] affineTransform].a;
657 ASSERT(scale == [webView->_scrollView zoomScale]);
661 - (void)_updateScrollViewBackground
663 WebCore::Color color;
664 if (_customContentView)
665 color = [_customContentView backgroundColor].CGColor;
667 color = _page->pageExtendedBackgroundColor();
669 CGFloat zoomScale = contentZoomScale(self);
670 CGFloat minimumZoomScale = [_scrollView minimumZoomScale];
671 if (zoomScale < minimumZoomScale) {
673 CGFloat opacity = std::max<CGFloat>(1 - slope * (minimumZoomScale - zoomScale), 0);
674 color = WebCore::colorWithOverrideAlpha(color.rgb(), opacity);
677 if (_scrollViewBackgroundColor == color)
680 _scrollViewBackgroundColor = color;
682 auto uiBackgroundColor = adoptNS([[UIColor alloc] initWithCGColor:cachedCGColor(color, WebCore::ColorSpaceDeviceRGB)]);
683 [_scrollView setBackgroundColor:uiBackgroundColor.get()];
686 - (void)_setUsesMinimalUI:(BOOL)usesMinimalUI
688 _usesMinimalUI = usesMinimalUI;
689 _needsToNotifyDelegateAboutMinimalUI = YES;
692 - (BOOL)_usesMinimalUI
694 return _usesMinimalUI;
697 - (CGPoint)_adjustedContentOffset:(CGPoint)point
699 CGPoint result = point;
700 UIEdgeInsets contentInset = [self _computedContentInset];
702 result.x -= contentInset.left;
703 result.y -= contentInset.top;
708 - (UIEdgeInsets)_computedContentInset
710 if (!UIEdgeInsetsEqualToEdgeInsets(_obscuredInsets, UIEdgeInsetsZero))
711 return _obscuredInsets;
713 return [_scrollView contentInset];
716 - (void)_processDidExit
718 if (!_customContentView && _isAnimatingResize) {
719 NSUInteger indexOfResizeAnimationView = [[_scrollView subviews] indexOfObject:_resizeAnimationView.get()];
720 [_scrollView insertSubview:_contentView.get() atIndex:indexOfResizeAnimationView];
721 [_resizeAnimationView removeFromSuperview];
722 _resizeAnimationView = nil;
724 _isAnimatingResize = NO;
725 _resizeAnimationTransformAdjustments = CATransform3DIdentity;
727 [_contentView setFrame:self.bounds];
728 [_scrollView setBackgroundColor:[UIColor whiteColor]];
729 [_scrollView setContentOffset:[self _adjustedContentOffset:CGPointZero]];
730 [_scrollView setZoomScale:1];
732 _viewportMetaTagWidth = -1;
733 _needsResetViewStateAfterCommitLoadForMainFrame = NO;
734 _needsToRestoreExposedRect = NO;
735 _needsToRestoreUnobscuredCenter = NO;
736 _scrollViewBackgroundColor = WebCore::Color();
737 _delayUpdateVisibleContentRects = NO;
738 _hadDelayedUpdateVisibleContentRects = NO;
741 - (void)_didCommitLoadForMainFrame
743 _firstPaintAfterCommitLoadTransactionID = toRemoteLayerTreeDrawingAreaProxy(_page->drawingArea())->nextLayerTreeTransactionID();
745 _needsResetViewStateAfterCommitLoadForMainFrame = YES;
749 static void changeContentOffsetBoundedInValidRange(UIScrollView *scrollView, WebCore::FloatPoint contentOffset)
751 UIEdgeInsets contentInsets = scrollView.contentInset;
752 CGSize contentSize = scrollView.contentSize;
753 CGSize scrollViewSize = scrollView.bounds.size;
755 float maxHorizontalOffset = contentSize.width + contentInsets.right - scrollViewSize.width;
756 if (contentOffset.x() > maxHorizontalOffset)
757 contentOffset.setX(maxHorizontalOffset);
758 float maxVerticalOffset = contentSize.height + contentInsets.bottom - scrollViewSize.height;
759 if (contentOffset.y() > maxVerticalOffset)
760 contentOffset.setY(maxVerticalOffset);
761 if (contentOffset.x() < -contentInsets.left)
762 contentOffset.setX(-contentInsets.left);
763 if (contentOffset.y() < -contentInsets.top)
764 contentOffset.setY(-contentInsets.top);
765 scrollView.contentOffset = contentOffset;
768 - (void)_didCommitLayerTree:(const WebKit::RemoteLayerTreeTransaction&)layerTreeTransaction
770 if (_customContentView)
773 if (_isAnimatingResize) {
774 [_resizeAnimationView layer].sublayerTransform = _resizeAnimationTransformAdjustments;
778 CGSize newContentSize = roundScrollViewContentSize(*_page, [_contentView frame].size);
779 [_scrollView _setContentSizePreservingContentOffsetDuringRubberband:newContentSize];
781 [_scrollView setMinimumZoomScale:layerTreeTransaction.minimumScaleFactor()];
782 [_scrollView setMaximumZoomScale:layerTreeTransaction.maximumScaleFactor()];
783 [_scrollView setZoomEnabled:layerTreeTransaction.allowsUserScaling()];
784 if (!layerTreeTransaction.scaleWasSetByUIProcess() && ![_scrollView isZooming] && ![_scrollView isZoomBouncing] && ![_scrollView _isAnimatingZoom])
785 [_scrollView setZoomScale:layerTreeTransaction.pageScaleFactor()];
787 [self _updateScrollViewBackground];
789 if (_gestureController)
790 _gestureController->setRenderTreeSize(layerTreeTransaction.renderTreeSize());
792 if (_needsToNotifyDelegateAboutMinimalUI || _needsResetViewStateAfterCommitLoadForMainFrame) {
793 _needsToNotifyDelegateAboutMinimalUI = NO;
795 auto delegate = _uiDelegate->delegate();
796 if ([delegate respondsToSelector:@selector(_webView:usesMinimalUI:)])
797 [static_cast<id <WKUIDelegatePrivate>>(delegate.get()) _webView:self usesMinimalUI:_usesMinimalUI];
800 if (_needsResetViewStateAfterCommitLoadForMainFrame && layerTreeTransaction.transactionID() >= _firstPaintAfterCommitLoadTransactionID) {
801 _needsResetViewStateAfterCommitLoadForMainFrame = NO;
802 [_scrollView setContentOffset:[self _adjustedContentOffset:CGPointZero]];
803 [self _updateVisibleContentRects];
806 if (_needsToRestoreExposedRect && layerTreeTransaction.transactionID() >= _firstTransactionIDAfterPageRestore) {
807 _needsToRestoreExposedRect = NO;
809 if (withinEpsilon(contentZoomScale(self), _scaleToRestore)) {
810 WebCore::FloatPoint exposedPosition = _exposedRectToRestore.location();
811 exposedPosition.scale(_scaleToRestore, _scaleToRestore);
813 changeContentOffsetBoundedInValidRange(_scrollView.get(), exposedPosition);
815 [self _updateVisibleContentRects];
818 if (_needsToRestoreUnobscuredCenter && layerTreeTransaction.transactionID() >= _firstTransactionIDAfterPageRestore) {
819 _needsToRestoreUnobscuredCenter = NO;
821 if (withinEpsilon(contentZoomScale(self), _scaleToRestore)) {
822 CGRect unobscuredRect = UIEdgeInsetsInsetRect(self.bounds, _obscuredInsets);
823 WebCore::FloatSize unobscuredContentSizeAtNewScale(unobscuredRect.size.width / _scaleToRestore, unobscuredRect.size.height / _scaleToRestore);
824 WebCore::FloatPoint topLeftInDocumentCoordinate(_unobscuredCenterToRestore.x() - unobscuredContentSizeAtNewScale.width() / 2, _unobscuredCenterToRestore.y() - unobscuredContentSizeAtNewScale.height() / 2);
826 topLeftInDocumentCoordinate.scale(_scaleToRestore, _scaleToRestore);
827 topLeftInDocumentCoordinate.moveBy(WebCore::FloatPoint(-_obscuredInsets.left, -_obscuredInsets.top));
829 changeContentOffsetBoundedInValidRange(_scrollView.get(), topLeftInDocumentCoordinate);
831 [self _updateVisibleContentRects];
835 - (void)_dynamicViewportUpdateChangedTargetToScale:(double)newScale position:(CGPoint)newScrollPosition
837 if (_isAnimatingResize) {
838 CGFloat animatingScaleTarget = [[_resizeAnimationView layer] transform].m11;
839 double currentTargetScale = animatingScaleTarget * [[_contentView layer] transform].m11;
840 double scale = newScale / currentTargetScale;
841 _resizeAnimationTransformAdjustments = CATransform3DMakeScale(scale, scale, 0);
843 CGPoint newContentOffset = [self _adjustedContentOffset:CGPointMake(newScrollPosition.x * newScale, newScrollPosition.y * newScale)];
844 CGPoint currentContentOffset = [_scrollView contentOffset];
846 _resizeAnimationTransformAdjustments.m41 = (currentContentOffset.x - newContentOffset.x) / animatingScaleTarget;
847 _resizeAnimationTransformAdjustments.m42 = (currentContentOffset.y - newContentOffset.y) / animatingScaleTarget;
851 - (void)_restorePageStateToExposedRect:(WebCore::FloatRect)exposedRect scale:(double)scale
853 if (_isAnimatingResize)
856 if (_customContentView)
859 _needsToRestoreUnobscuredCenter = NO;
860 _needsToRestoreExposedRect = YES;
861 _firstTransactionIDAfterPageRestore = toRemoteLayerTreeDrawingAreaProxy(_page->drawingArea())->nextLayerTreeTransactionID();
862 _exposedRectToRestore = exposedRect;
863 _scaleToRestore = scale;
866 - (void)_restorePageStateToUnobscuredCenter:(WebCore::FloatPoint)center scale:(double)scale
868 if (_isAnimatingResize)
871 if (_customContentView)
874 _needsToRestoreExposedRect = NO;
875 _needsToRestoreUnobscuredCenter = YES;
876 _firstTransactionIDAfterPageRestore = toRemoteLayerTreeDrawingAreaProxy(_page->drawingArea())->nextLayerTreeTransactionID();
877 _unobscuredCenterToRestore = center;
878 _scaleToRestore = scale;
881 - (WebKit::ViewSnapshot)_takeViewSnapshot
883 float deviceScale = WKGetScreenScaleFactor();
884 CGSize snapshotSize = self.bounds.size;
885 snapshotSize.width *= deviceScale;
886 snapshotSize.height *= deviceScale;
888 WebKit::ViewSnapshot snapshot;
889 snapshot.slotID = [WebKit::ViewSnapshotStore::snapshottingContext() createImageSlot:snapshotSize hasAlpha:YES];
891 CATransform3D transform = CATransform3DMakeScale(deviceScale, deviceScale, 1);
892 CARenderServerCaptureLayerWithTransform(MACH_PORT_NULL, self.layer.context.contextId, (uint64_t)self.layer, snapshot.slotID, 0, 0, &transform);
894 snapshot.size = WebCore::expandedIntSize(WebCore::FloatSize(snapshotSize));
895 snapshot.imageSizeInBytes = snapshotSize.width * snapshotSize.height * 4;
896 snapshot.backgroundColor = _page->pageExtendedBackgroundColor();
901 - (void)_zoomToPoint:(WebCore::FloatPoint)point atScale:(double)scale
903 double maximumZoomDuration = 0.4;
904 double minimumZoomDuration = 0.1;
905 double zoomDurationFactor = 0.3;
907 CGFloat zoomScale = contentZoomScale(self);
908 CFTimeInterval duration = std::min(fabs(log(zoomScale) - log(scale)) * zoomDurationFactor + minimumZoomDuration, maximumZoomDuration);
910 if (scale != zoomScale)
911 _page->willStartUserTriggeredZooming();
913 [_scrollView _zoomToCenter:point scale:scale duration:duration];
916 - (void)_zoomToRect:(WebCore::FloatRect)targetRect atScale:(double)scale origin:(WebCore::FloatPoint)origin
918 // FIMXE: Some of this could be shared with _scrollToRect.
919 const double visibleRectScaleChange = contentZoomScale(self) / scale;
920 const WebCore::FloatRect visibleRect([self convertRect:self.bounds toView:self._currentContentView]);
921 const WebCore::FloatRect unobscuredRect([self _contentRectForUserInteraction]);
923 const WebCore::FloatSize topLeftObscuredInsetAfterZoom((unobscuredRect.minXMinYCorner() - visibleRect.minXMinYCorner()) * visibleRectScaleChange);
924 const WebCore::FloatSize bottomRightObscuredInsetAfterZoom((visibleRect.maxXMaxYCorner() - unobscuredRect.maxXMaxYCorner()) * visibleRectScaleChange);
926 const WebCore::FloatSize unobscuredRectSizeAfterZoom(unobscuredRect.size() * visibleRectScaleChange);
928 // Center to the target rect.
929 WebCore::FloatPoint unobscuredRectLocationAfterZoom = targetRect.location() - (unobscuredRectSizeAfterZoom - targetRect.size()) * 0.5;
931 // Center to the tap point instead in case the target rect won't fit in a direction.
932 if (targetRect.width() > unobscuredRectSizeAfterZoom.width())
933 unobscuredRectLocationAfterZoom.setX(origin.x() - unobscuredRectSizeAfterZoom.width() / 2);
934 if (targetRect.height() > unobscuredRectSizeAfterZoom.height())
935 unobscuredRectLocationAfterZoom.setY(origin.y() - unobscuredRectSizeAfterZoom.height() / 2);
937 // We have computed where we want the unobscured rect to be. Now adjust for the obscuring insets.
938 WebCore::FloatRect visibleRectAfterZoom(unobscuredRectLocationAfterZoom, unobscuredRectSizeAfterZoom);
939 visibleRectAfterZoom.move(-topLeftObscuredInsetAfterZoom);
940 visibleRectAfterZoom.expand(topLeftObscuredInsetAfterZoom + bottomRightObscuredInsetAfterZoom);
942 [self _zoomToPoint:visibleRectAfterZoom.center() atScale:scale];
945 static WebCore::FloatPoint constrainContentOffset(WebCore::FloatPoint contentOffset, WebCore::FloatSize contentSize, WebCore::FloatSize unobscuredContentSize)
947 WebCore::FloatSize maximumContentOffset = contentSize - unobscuredContentSize;
948 contentOffset = contentOffset.shrunkTo(WebCore::FloatPoint(maximumContentOffset.width(), maximumContentOffset.height()));
949 contentOffset = contentOffset.expandedTo(WebCore::FloatPoint());
950 return contentOffset;
953 - (void)_scrollToContentOffset:(WebCore::FloatPoint)contentOffset
955 if (_isAnimatingResize)
958 [_scrollView _stopScrollingAndZoomingAnimations];
960 WebCore::FloatPoint scaledOffset = contentOffset;
961 CGFloat zoomScale = contentZoomScale(self);
962 scaledOffset.scale(zoomScale, zoomScale);
964 [_scrollView setContentOffset:[self _adjustedContentOffset:scaledOffset]];
967 - (BOOL)_scrollToRect:(WebCore::FloatRect)targetRect origin:(WebCore::FloatPoint)origin minimumScrollDistance:(float)minimumScrollDistance
969 WebCore::FloatRect unobscuredContentRect([self _contentRectForUserInteraction]);
970 WebCore::FloatPoint unobscuredContentOffset = unobscuredContentRect.location();
971 WebCore::FloatSize contentSize([self._currentContentView bounds].size);
973 // Center the target rect in the scroll view.
974 // If the target doesn't fit in the scroll view, center on the gesture location instead.
975 WebCore::FloatPoint newUnobscuredContentOffset;
976 if (targetRect.width() <= unobscuredContentRect.width())
977 newUnobscuredContentOffset.setX(targetRect.x() - (unobscuredContentRect.width() - targetRect.width()) / 2);
979 newUnobscuredContentOffset.setX(origin.x() - unobscuredContentRect.width() / 2);
980 if (targetRect.height() <= unobscuredContentRect.height())
981 newUnobscuredContentOffset.setY(targetRect.y() - (unobscuredContentRect.height() - targetRect.height()) / 2);
983 newUnobscuredContentOffset.setY(origin.y() - unobscuredContentRect.height() / 2);
984 newUnobscuredContentOffset = constrainContentOffset(newUnobscuredContentOffset, contentSize, unobscuredContentRect.size());
986 if (unobscuredContentOffset == newUnobscuredContentOffset) {
987 if (targetRect.width() > unobscuredContentRect.width())
988 newUnobscuredContentOffset.setX(origin.x() - unobscuredContentRect.width() / 2);
989 if (targetRect.height() > unobscuredContentRect.height())
990 newUnobscuredContentOffset.setY(origin.y() - unobscuredContentRect.height() / 2);
991 newUnobscuredContentOffset = constrainContentOffset(newUnobscuredContentOffset, contentSize, unobscuredContentRect.size());
994 WebCore::FloatSize scrollViewOffsetDelta = newUnobscuredContentOffset - unobscuredContentOffset;
995 scrollViewOffsetDelta.scale(contentZoomScale(self));
997 float scrollDistance = scrollViewOffsetDelta.diagonalLength();
998 if (scrollDistance < minimumScrollDistance)
1001 [_scrollView setContentOffset:([_scrollView contentOffset] + scrollViewOffsetDelta) animated:YES];
1005 - (void)_zoomOutWithOrigin:(WebCore::FloatPoint)origin
1007 [self _zoomToPoint:origin atScale:[_scrollView minimumZoomScale]];
1010 // focusedElementRect and selectionRect are both in document coordinates.
1011 - (void)_zoomToFocusRect:(WebCore::FloatRect)focusedElementRectInDocumentCoordinates selectionRect:(WebCore::FloatRect)selectionRectInDocumentCoordinates fontSize:(float)fontSize minimumScale:(double)minimumScale maximumScale:(double)maximumScale allowScaling:(BOOL)allowScaling forceScroll:(BOOL)forceScroll
1013 const double WKWebViewStandardFontSize = 16;
1014 const double kMinimumHeightToShowContentAboveKeyboard = 106;
1015 const CFTimeInterval UIWebFormAnimationDuration = 0.25;
1016 const double CaretOffsetFromWindowEdge = 20;
1018 // Zoom around the element's bounding frame. We use a "standard" size to determine the proper frame.
1019 double scale = allowScaling ? std::min(std::max(WKWebViewStandardFontSize / fontSize, minimumScale), maximumScale) : contentZoomScale(self);
1020 CGFloat documentWidth = [_contentView bounds].size.width;
1021 scale = CGRound(documentWidth * scale) / documentWidth;
1023 UIWindow *window = [_scrollView window];
1025 WebCore::FloatRect focusedElementRectInNewScale = focusedElementRectInDocumentCoordinates;
1026 focusedElementRectInNewScale.scale(scale);
1027 focusedElementRectInNewScale.moveBy([_contentView frame].origin);
1029 // Find the portion of the view that is visible on the screen.
1030 UIViewController *topViewController = [[[_scrollView _viewControllerForAncestor] _rootAncestorViewController] _viewControllerForSupportedInterfaceOrientations];
1031 UIView *fullScreenView = topViewController.view;
1032 if (!fullScreenView)
1033 fullScreenView = window;
1035 CGRect unobscuredScrollViewRectInWebViewCoordinates = UIEdgeInsetsInsetRect([self bounds], _obscuredInsets);
1036 CGRect visibleScrollViewBoundsInWebViewCoordinates = CGRectIntersection(unobscuredScrollViewRectInWebViewCoordinates, [fullScreenView convertRect:[fullScreenView bounds] toView:self]);
1037 CGRect formAssistantFrameInWebViewCoordinates = [window convertRect:_inputViewBounds toView:self];
1038 CGRect intersectionBetweenScrollViewAndFormAssistant = CGRectIntersection(visibleScrollViewBoundsInWebViewCoordinates, formAssistantFrameInWebViewCoordinates);
1039 CGSize visibleSize = visibleScrollViewBoundsInWebViewCoordinates.size;
1041 CGFloat visibleOffsetFromTop = 0;
1042 if (!CGRectIsEmpty(intersectionBetweenScrollViewAndFormAssistant)) {
1043 CGFloat heightVisibleAboveFormAssistant = CGRectGetMinY(intersectionBetweenScrollViewAndFormAssistant) - CGRectGetMinY(visibleScrollViewBoundsInWebViewCoordinates);
1044 CGFloat heightVisibleBelowFormAssistant = CGRectGetMaxY(visibleScrollViewBoundsInWebViewCoordinates) - CGRectGetMaxY(intersectionBetweenScrollViewAndFormAssistant);
1046 if (heightVisibleAboveFormAssistant >= kMinimumHeightToShowContentAboveKeyboard || heightVisibleBelowFormAssistant < heightVisibleAboveFormAssistant)
1047 visibleSize.height = heightVisibleAboveFormAssistant;
1049 visibleSize.height = heightVisibleBelowFormAssistant;
1050 visibleOffsetFromTop = CGRectGetMaxY(intersectionBetweenScrollViewAndFormAssistant) - CGRectGetMinY(visibleScrollViewBoundsInWebViewCoordinates);
1054 BOOL selectionRectIsNotNull = !selectionRectInDocumentCoordinates.isZero();
1056 CGRect currentlyVisibleRegionInWebViewCoordinates;
1057 currentlyVisibleRegionInWebViewCoordinates.origin = unobscuredScrollViewRectInWebViewCoordinates.origin;
1058 currentlyVisibleRegionInWebViewCoordinates.origin.y += visibleOffsetFromTop;
1059 currentlyVisibleRegionInWebViewCoordinates.size = visibleSize;
1061 // Don't bother scrolling if the entire node is already visible, whether or not we got a selectionRect.
1062 if (CGRectContainsRect(currentlyVisibleRegionInWebViewCoordinates, [self convertRect:focusedElementRectInDocumentCoordinates fromView:_contentView.get()]))
1065 // Don't bother scrolling if we have a valid selectionRect and it is already visible.
1066 if (selectionRectIsNotNull && CGRectContainsRect(currentlyVisibleRegionInWebViewCoordinates, [self convertRect:selectionRectInDocumentCoordinates fromView:_contentView.get()]))
1070 // We want to zoom to the left/top corner of the DOM node, with as much spacing on all sides as we
1071 // can get based on the visible area after zooming (workingFrame). The spacing in either dimension is half the
1072 // difference between the size of the DOM node and the size of the visible frame.
1073 CGFloat horizontalSpaceInWebViewCoordinates = std::max((visibleSize.width - focusedElementRectInNewScale.width()) / 2.0, 0.0);
1074 CGFloat verticalSpaceInWebViewCoordinates = std::max((visibleSize.height - focusedElementRectInNewScale.height()) / 2.0, 0.0);
1077 topLeft.x = focusedElementRectInNewScale.x() - horizontalSpaceInWebViewCoordinates;
1078 topLeft.y = focusedElementRectInNewScale.y() - verticalSpaceInWebViewCoordinates - visibleOffsetFromTop;
1080 CGFloat minimumAllowableHorizontalOffsetInWebViewCoordinates = -INFINITY;
1081 CGFloat minimumAllowableVerticalOffsetInWebViewCoordinates = -INFINITY;
1082 if (selectionRectIsNotNull) {
1083 WebCore::FloatRect selectionRectInNewScale = selectionRectInDocumentCoordinates;
1084 selectionRectInNewScale.scale(scale);
1085 selectionRectInNewScale.moveBy([_contentView frame].origin);
1086 minimumAllowableHorizontalOffsetInWebViewCoordinates = CGRectGetMaxX(selectionRectInNewScale) + CaretOffsetFromWindowEdge - visibleSize.width;
1087 minimumAllowableVerticalOffsetInWebViewCoordinates = CGRectGetMaxY(selectionRectInNewScale) + CaretOffsetFromWindowEdge - visibleSize.height - visibleOffsetFromTop;
1090 WebCore::FloatRect documentBoundsInNewScale = [_contentView bounds];
1091 documentBoundsInNewScale.scale(scale);
1092 documentBoundsInNewScale.moveBy([_contentView frame].origin);
1094 // Constrain the left edge in document coordinates so that:
1095 // - it isn't so small that the scrollVisibleRect isn't visible on the screen
1096 // - it isn't so great that the document's right edge is less than the right edge of the screen
1097 if (selectionRectIsNotNull && topLeft.x < minimumAllowableHorizontalOffsetInWebViewCoordinates)
1098 topLeft.x = minimumAllowableHorizontalOffsetInWebViewCoordinates;
1100 CGFloat maximumAllowableHorizontalOffset = CGRectGetMaxX(documentBoundsInNewScale) - visibleSize.width;
1101 if (topLeft.x > maximumAllowableHorizontalOffset)
1102 topLeft.x = maximumAllowableHorizontalOffset;
1105 // Constrain the top edge in document coordinates so that:
1106 // - it isn't so small that the scrollVisibleRect isn't visible on the screen
1107 // - it isn't so great that the document's bottom edge is higher than the top of the form assistant
1108 if (selectionRectIsNotNull && topLeft.y < minimumAllowableVerticalOffsetInWebViewCoordinates)
1109 topLeft.y = minimumAllowableVerticalOffsetInWebViewCoordinates;
1111 CGFloat maximumAllowableVerticalOffset = CGRectGetMaxY(documentBoundsInNewScale) - visibleSize.height;
1112 if (topLeft.y > maximumAllowableVerticalOffset)
1113 topLeft.y = maximumAllowableVerticalOffset;
1116 WebCore::FloatPoint newCenter = CGPointMake(topLeft.x + unobscuredScrollViewRectInWebViewCoordinates.size.width / 2.0, topLeft.y + unobscuredScrollViewRectInWebViewCoordinates.size.height / 2.0);
1118 if (scale != contentZoomScale(self))
1119 _page->willStartUserTriggeredZooming();
1121 // The newCenter has been computed in the new scale, but _zoomToCenter expected the center to be in the original scale.
1122 newCenter.scale(1 / scale, 1 / scale);
1123 [_scrollView _zoomToCenter:newCenter
1125 duration:UIWebFormAnimationDuration
1129 - (BOOL)_zoomToRect:(WebCore::FloatRect)targetRect withOrigin:(WebCore::FloatPoint)origin fitEntireRect:(BOOL)fitEntireRect minimumScale:(double)minimumScale maximumScale:(double)maximumScale minimumScrollDistance:(float)minimumScrollDistance
1131 const float maximumScaleFactorDeltaForPanScroll = 0.02;
1133 double currentScale = contentZoomScale(self);
1135 WebCore::FloatSize unobscuredContentSize([self _contentRectForUserInteraction].size);
1136 double horizontalScale = unobscuredContentSize.width() * currentScale / targetRect.width();
1137 double verticalScale = unobscuredContentSize.height() * currentScale / targetRect.height();
1139 horizontalScale = std::min(std::max(horizontalScale, minimumScale), maximumScale);
1140 verticalScale = std::min(std::max(verticalScale, minimumScale), maximumScale);
1142 double targetScale = fitEntireRect ? std::min(horizontalScale, verticalScale) : horizontalScale;
1143 if (fabs(targetScale - currentScale) < maximumScaleFactorDeltaForPanScroll) {
1144 if ([self _scrollToRect:targetRect origin:origin minimumScrollDistance:minimumScrollDistance])
1146 } else if (targetScale != currentScale) {
1147 [self _zoomToRect:targetRect atScale:targetScale origin:origin];
1154 - (void)didMoveToWindow
1156 _page->viewStateDidChange(WebCore::ViewState::IsInWindow);
1159 #pragma mark - UIScrollViewDelegate
1161 - (BOOL)usesStandardContentView
1163 return !_customContentView;
1166 - (CGSize)scrollView:(UIScrollView*)scrollView contentSizeForZoomScale:(CGFloat)scale withProposedSize:(CGSize)proposedSize
1168 return roundScrollViewContentSize(*_page, proposedSize);
1171 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
1173 ASSERT(_scrollView == scrollView);
1175 if (_customContentView)
1176 return _customContentView.get();
1178 return _contentView.get();
1181 - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
1183 if (![self usesStandardContentView])
1186 if (scrollView.pinchGestureRecognizer.state == UIGestureRecognizerStateBegan) {
1187 _page->willStartUserTriggeredZooming();
1188 [_contentView scrollViewWillStartPanOrPinchGesture];
1190 [_contentView willStartZoomOrScroll];
1193 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
1195 if (![self usesStandardContentView])
1198 if (scrollView.panGestureRecognizer.state == UIGestureRecognizerStateBegan)
1199 [_contentView scrollViewWillStartPanOrPinchGesture];
1200 [_contentView willStartZoomOrScroll];
1203 - (void)_didFinishScrolling
1205 if (![self usesStandardContentView])
1208 [self _updateVisibleContentRects];
1209 [_contentView didFinishScrolling];
1212 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
1214 // Work around <rdar://problem/16374753> by avoiding deceleration while
1215 // zooming. We'll animate to the right place once the zoom finishes.
1216 if ([scrollView isZooming])
1217 *targetContentOffset = [scrollView contentOffset];
1220 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
1222 // If we're decelerating, scroll offset will be updated when scrollViewDidFinishDecelerating: is called.
1224 [self _didFinishScrolling];
1227 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
1229 [self _didFinishScrolling];
1232 - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
1234 [self _didFinishScrolling];
1237 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
1239 if (![self usesStandardContentView])
1240 [_customContentView scrollViewDidScroll:(UIScrollView *)scrollView];
1242 [self _updateVisibleContentRects];
1245 - (void)scrollViewDidZoom:(UIScrollView *)scrollView
1247 [self _updateScrollViewBackground];
1248 [self _updateVisibleContentRects];
1251 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
1253 ASSERT(scrollView == _scrollView);
1254 [self _updateVisibleContentRects];
1255 [_contentView didZoomToScale:scale];
1258 - (void)_frameOrBoundsChanged
1260 CGRect bounds = self.bounds;
1261 if (!_isAnimatingResize) {
1262 if (!_overridesMinimumLayoutSize)
1263 _page->setViewportConfigurationMinimumLayoutSize(WebCore::FloatSize(bounds.size));
1264 if (!_overridesMinimumLayoutSizeForMinimalUI)
1265 _page->setViewportConfigurationMinimumLayoutSizeForMinimalUI(WebCore::FloatSize(bounds.size));
1266 if (!_overridesMaximumUnobscuredSize)
1267 _page->setMaximumUnobscuredSize(WebCore::FloatSize(bounds.size));
1270 [_scrollView setFrame:bounds];
1271 [_contentView setMinimumSize:bounds.size];
1272 [_customContentView web_setMinimumSize:bounds.size];
1273 [self _updateVisibleContentRects];
1276 // Unobscured content rect where the user can interact. When the keyboard is up, this should be the area above or bellow the keyboard, wherever there is enough space.
1277 - (CGRect)_contentRectForUserInteraction
1279 // FIXME: handle split keyboard.
1280 UIEdgeInsets obscuredInsets = _obscuredInsets;
1281 obscuredInsets.bottom = std::max(_obscuredInsets.bottom, _inputViewBounds.size.height);
1282 CGRect unobscuredRect = UIEdgeInsetsInsetRect(self.bounds, obscuredInsets);
1283 return [self convertRect:unobscuredRect toView:self._currentContentView];
1286 - (void)_updateVisibleContentRects
1288 if (![self usesStandardContentView]) {
1289 [_customContentView web_computedContentInsetDidChange];
1293 if (_delayUpdateVisibleContentRects) {
1294 _hadDelayedUpdateVisibleContentRects = YES;
1298 if (_isAnimatingResize)
1301 if (_needsResetViewStateAfterCommitLoadForMainFrame)
1304 CGRect fullViewRect = self.bounds;
1305 CGRect visibleRectInContentCoordinates = [self convertRect:fullViewRect toView:_contentView.get()];
1307 CGRect unobscuredRect = UIEdgeInsetsInsetRect(fullViewRect, [self _computedContentInset]);
1308 CGRect unobscuredRectInContentCoordinates = [self convertRect:unobscuredRect toView:_contentView.get()];
1310 CGFloat scaleFactor = contentZoomScale(self);
1312 BOOL isStableState = !(_isChangingObscuredInsetsInteractively || [_scrollView isDragging] || [_scrollView isDecelerating] || [_scrollView isZooming] || [_scrollView isZoomBouncing] || [_scrollView _isAnimatingZoom] || [_scrollView _isScrollingToTop]);
1313 [_contentView didUpdateVisibleRect:visibleRectInContentCoordinates
1314 unobscuredRect:unobscuredRectInContentCoordinates
1315 unobscuredRectInScrollViewCoordinates:unobscuredRect
1316 scale:scaleFactor minimumScale:[_scrollView minimumZoomScale]
1317 inStableState:isStableState isChangingObscuredInsetsInteractively:_isChangingObscuredInsetsInteractively];
1320 - (void)_keyboardChangedWithInfo:(NSDictionary *)keyboardInfo adjustScrollView:(BOOL)adjustScrollView
1322 NSValue *endFrameValue = [keyboardInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
1326 // The keyboard rect is always in screen coordinates. In the view services case the window does not
1327 // have the interface orientation rotation transformation; its host does. So, it makes no sense to
1328 // clip the keyboard rect against its screen.
1329 if ([[self window] _isHostedInAnotherProcess])
1330 _inputViewBounds = [self.window convertRect:[endFrameValue CGRectValue] fromWindow:nil];
1332 _inputViewBounds = [self.window convertRect:CGRectIntersection([endFrameValue CGRectValue], self.window.screen.bounds) fromWindow:nil];
1334 [self _updateVisibleContentRects];
1336 if (adjustScrollView)
1337 [_scrollView _adjustForAutomaticKeyboardInfo:keyboardInfo animated:YES lastAdjustment:&_lastAdjustmentForScroller];
1340 - (void)_keyboardWillChangeFrame:(NSNotification *)notification
1342 if ([_contentView isAssistingNode])
1343 [self _keyboardChangedWithInfo:notification.userInfo adjustScrollView:YES];
1346 - (void)_keyboardDidChangeFrame:(NSNotification *)notification
1348 [self _keyboardChangedWithInfo:notification.userInfo adjustScrollView:NO];
1351 - (void)_keyboardWillShow:(NSNotification *)notification
1353 if ([_contentView isAssistingNode])
1354 [self _keyboardChangedWithInfo:notification.userInfo adjustScrollView:YES];
1357 - (void)_keyboardWillHide:(NSNotification *)notification
1359 // Ignore keyboard will hide notifications sent during rotation. They're just there for
1360 // backwards compatibility reasons and processing the will hide notification would
1361 // temporarily screw up the the unobscured view area.
1362 if ([[UIPeripheralHost sharedInstance] rotationState])
1365 [self _keyboardChangedWithInfo:notification.userInfo adjustScrollView:YES];
1368 - (void)_windowDidRotate:(NSNotification *)notification
1370 if (!_overridesInterfaceOrientation)
1371 _page->setDeviceOrientation(deviceOrientation());
1374 - (void)_contentSizeCategoryDidChange:(NSNotification *)notification
1376 _page->contentSizeCategoryDidChange([self _contentSizeCategory]);
1379 - (NSString *)_contentSizeCategory
1381 return [[UIApplication sharedApplication] preferredContentSizeCategory];
1384 - (void)setAllowsBackForwardNavigationGestures:(BOOL)allowsBackForwardNavigationGestures
1386 if (_allowsBackForwardNavigationGestures == allowsBackForwardNavigationGestures)
1389 _allowsBackForwardNavigationGestures = allowsBackForwardNavigationGestures;
1391 if (allowsBackForwardNavigationGestures) {
1392 if (!_gestureController) {
1393 _gestureController = std::make_unique<WebKit::ViewGestureController>(*_page);
1394 _gestureController->installSwipeHandler(self, [self scrollView]);
1397 _gestureController = nullptr;
1399 _page->setShouldRecordNavigationSnapshots(allowsBackForwardNavigationGestures);
1402 - (BOOL)allowsBackForwardNavigationGestures
1404 return _allowsBackForwardNavigationGestures;
1409 #pragma mark OS X-specific methods
1413 - (void)resizeSubviewsWithOldSize:(NSSize)oldSize
1415 [_wkView setFrame:self.bounds];
1418 - (void)setAllowsBackForwardNavigationGestures:(BOOL)allowsBackForwardNavigationGestures
1420 [_wkView setAllowsBackForwardNavigationGestures:allowsBackForwardNavigationGestures];
1423 - (BOOL)allowsBackForwardNavigationGestures
1425 return [_wkView allowsBackForwardNavigationGestures];
1428 - (void)setAllowsMagnification:(BOOL)allowsMagnification
1430 [_wkView setAllowsMagnification:allowsMagnification];
1433 - (BOOL)allowsMagnification
1435 return [_wkView allowsMagnification];
1438 - (void)setMagnification:(CGFloat)magnification
1440 [_wkView setMagnification:magnification];
1443 - (CGFloat)magnification
1445 return [_wkView magnification];
1448 - (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point
1450 [_wkView setMagnification:magnification centeredAtPoint:NSPointFromCGPoint(point)];
1457 @implementation WKWebView (WKPrivate)
1459 - (_WKRemoteObjectRegistry *)_remoteObjectRegistry
1461 if (!_remoteObjectRegistry) {
1462 _remoteObjectRegistry = adoptNS([[_WKRemoteObjectRegistry alloc] _initWithMessageSender:*_page]);
1463 _page->process().context().addMessageReceiver(Messages::RemoteObjectRegistry::messageReceiverName(), _page->pageID(), [_remoteObjectRegistry remoteObjectRegistry]);
1466 return _remoteObjectRegistry.get();
1469 - (WKBrowsingContextHandle *)_handle
1471 return [[[WKBrowsingContextHandle alloc] _initWithPageID:_page->pageID()] autorelease];
1474 - (_WKRenderingProgressEvents)_observedRenderingProgressEvents
1476 return _observedRenderingProgressEvents;
1479 - (id <WKHistoryDelegatePrivate>)_historyDelegate
1481 return _navigationState->historyDelegate().autorelease();
1484 - (void)_setHistoryDelegate:(id <WKHistoryDelegatePrivate>)historyDelegate
1486 _navigationState->setHistoryDelegate(historyDelegate);
1489 - (NSURL *)_unreachableURL
1491 return [NSURL _web_URLWithWTFString:_page->pageLoadState().unreachableURL()];
1494 - (void)_loadAlternateHTMLString:(NSString *)string baseURL:(NSURL *)baseURL forUnreachableURL:(NSURL *)unreachableURL
1496 _page->loadAlternateHTMLString(string, [baseURL _web_originalDataAsWTFString], [unreachableURL _web_originalDataAsWTFString]);
1499 - (NSArray *)_certificateChain
1501 if (WebKit::WebFrameProxy* mainFrame = _page->mainFrame())
1502 return mainFrame->certificateInfo() ? (NSArray *)mainFrame->certificateInfo()->certificateInfo().certificateChain() : nil;
1507 - (NSURL *)_committedURL
1509 return [NSURL _web_URLWithWTFString:_page->pageLoadState().url()];
1512 - (NSString *)_MIMEType
1514 if (_page->mainFrame())
1515 return _page->mainFrame()->mimeType();
1520 - (NSString *)_applicationNameForUserAgent
1522 return _page->applicationNameForUserAgent();
1525 - (void)_setApplicationNameForUserAgent:(NSString *)applicationNameForUserAgent
1527 _page->setApplicationNameForUserAgent(applicationNameForUserAgent);
1530 - (NSString *)_customUserAgent
1532 return _page->customUserAgent();
1535 - (void)_setCustomUserAgent:(NSString *)_customUserAgent
1537 _page->setCustomUserAgent(_customUserAgent);
1540 - (pid_t)_webProcessIdentifier
1542 return _page->isValid() ? _page->processIdentifier() : 0;
1545 - (void)_killWebContentProcess
1547 if (!_page->isValid())
1550 _page->process().terminate();
1554 static WebCore::FloatSize activeMinimumLayoutSize(WKWebView *webView, const CGRect& bounds)
1556 return WebCore::FloatSize(webView->_overridesMinimumLayoutSize ? webView->_minimumLayoutSizeOverride : bounds.size);
1559 static WebCore::FloatSize activeMinimumLayoutSizeForMinimalUI(WKWebView *webView, WebCore::FloatSize minimumLayoutSize)
1561 return webView->_overridesMinimumLayoutSizeForMinimalUI ? WebCore::FloatSize(webView->_minimumLayoutSizeOverrideForMinimalUI) : minimumLayoutSize;
1564 static WebCore::FloatSize activeMaximumUnobscuredSize(WKWebView *webView, const CGRect& bounds)
1566 return WebCore::FloatSize(webView->_overridesMaximumUnobscuredSize ? webView->_maximumUnobscuredSizeOverride : bounds.size);
1569 static int32_t activeOrientation(WKWebView *webView)
1571 return webView->_overridesInterfaceOrientation ? deviceOrientationForUIInterfaceOrientation(webView->_interfaceOrientationOverride) : webView->_page->deviceOrientation();
1575 - (void)_didRelaunchProcess
1578 CGRect bounds = self.bounds;
1579 WebCore::FloatSize minimalLayoutSize = activeMinimumLayoutSize(self, bounds);
1580 _page->setViewportConfigurationMinimumLayoutSize(minimalLayoutSize);
1581 _page->setViewportConfigurationMinimumLayoutSizeForMinimalUI(activeMinimumLayoutSizeForMinimalUI(self, minimalLayoutSize));
1582 _page->setMaximumUnobscuredSize(activeMaximumUnobscuredSize(self, bounds));
1586 - (NSData *)_sessionStateData
1588 WebKit::SessionState sessionState = _page->sessionState();
1590 // FIXME: This should not use the legacy session state encoder.
1591 return [wrapper(*WebKit::encodeLegacySessionState(sessionState).release().leakRef()) autorelease];
1594 // FIXME: This should return a _WKSessionState object.
1597 return adoptNS([[_WKSessionState alloc] _initWithSessionState:_page->sessionState()]).autorelease();
1600 - (void)_restoreFromSessionStateData:(NSData *)sessionStateData
1602 // FIXME: This should not use the legacy session state decoder.
1603 WebKit::SessionState sessionState;
1604 if (!WebKit::decodeLegacySessionState(static_cast<const uint8_t*>(sessionStateData.bytes), sessionStateData.length, sessionState))
1607 if (uint64_t navigationID = _page->restoreFromSessionState(WTF::move(sessionState), true)) {
1608 // FIXME: This is not necessarily always a reload navigation.
1609 _navigationState->createReloadNavigation(navigationID);
1613 // FIXME: Remove this once nobody is using it.
1614 - (void)_restoreFromSessionState:(id)sessionState
1616 [self _restoreSessionState:sessionState andNavigate:YES];
1619 - (WKNavigation *)_restoreSessionState:(_WKSessionState *)sessionState andNavigate:(BOOL)navigate
1621 if (uint64_t navigationID = _page->restoreFromSessionState(sessionState->_sessionState, navigate)) {
1622 // FIXME: This is not necessarily always a reload navigation.
1623 return _navigationState->createReloadNavigation(navigationID).autorelease();
1634 - (BOOL)_allowsRemoteInspection
1636 #if ENABLE(REMOTE_INSPECTOR)
1637 return _page->allowsRemoteInspection();
1643 - (void)_setAllowsRemoteInspection:(BOOL)allow
1645 #if ENABLE(REMOTE_INSPECTOR)
1646 _page->setAllowsRemoteInspection(allow);
1650 - (BOOL)_addsVisitedLinks
1652 return _page->addsVisitedLinks();
1655 - (void)_setAddsVisitedLinks:(BOOL)addsVisitedLinks
1657 _page->setAddsVisitedLinks(addsVisitedLinks);
1660 - (BOOL)_networkRequestsInProgress
1662 return _page->pageLoadState().networkRequestsInProgress();
1665 static inline WebCore::LayoutMilestones layoutMilestones(_WKRenderingProgressEvents events)
1667 WebCore::LayoutMilestones milestones = 0;
1669 if (events & _WKRenderingProgressEventFirstLayout)
1670 milestones |= WebCore::DidFirstLayout;
1672 if (events & _WKRenderingProgressEventFirstPaintWithSignificantArea)
1673 milestones |= WebCore::DidHitRelevantRepaintedObjectsAreaThreshold;
1678 - (void)_setObservedRenderingProgressEvents:(_WKRenderingProgressEvents)observedRenderingProgressEvents
1680 _observedRenderingProgressEvents = observedRenderingProgressEvents;
1681 _page->listenForLayoutMilestones(layoutMilestones(observedRenderingProgressEvents));
1684 - (void)_getMainResourceDataWithCompletionHandler:(void (^)(NSData *, NSError *))completionHandler
1686 auto handler = adoptNS([completionHandler copy]);
1688 _page->getMainResourceDataOfFrame(_page->mainFrame(), [handler](API::Data* data, WebKit::CallbackBase::Error error) {
1689 void (^completionHandlerBlock)(NSData *, NSError *) = (void (^)(NSData *, NSError *))handler.get();
1690 if (error != WebKit::CallbackBase::Error::None) {
1691 // FIXME: Pipe a proper error in from the WebPageProxy.
1692 RetainPtr<NSError> error = adoptNS([[NSError alloc] init]);
1693 completionHandlerBlock(nil, error.get());
1695 completionHandlerBlock(wrapper(*data), nil);
1699 - (void)_getWebArchiveDataWithCompletionHandler:(void (^)(NSData *, NSError *))completionHandler
1701 auto handler = adoptNS([completionHandler copy]);
1703 _page->getWebArchiveOfFrame(_page->mainFrame(), [handler](API::Data* data, WebKit::CallbackBase::Error error) {
1704 void (^completionHandlerBlock)(NSData *, NSError *) = (void (^)(NSData *, NSError *))handler.get();
1705 if (error != WebKit::CallbackBase::Error::None) {
1706 // FIXME: Pipe a proper error in from the WebPageProxy.
1707 RetainPtr<NSError> error = adoptNS([[NSError alloc] init]);
1708 completionHandlerBlock(nil, error.get());
1710 completionHandlerBlock(wrapper(*data), nil);
1714 - (_WKPaginationMode)_paginationMode
1716 switch (_page->paginationMode()) {
1717 case WebCore::Pagination::Unpaginated:
1718 return _WKPaginationModeUnpaginated;
1719 case WebCore::Pagination::LeftToRightPaginated:
1720 return _WKPaginationModeLeftToRight;
1721 case WebCore::Pagination::RightToLeftPaginated:
1722 return _WKPaginationModeRightToLeft;
1723 case WebCore::Pagination::TopToBottomPaginated:
1724 return _WKPaginationModeTopToBottom;
1725 case WebCore::Pagination::BottomToTopPaginated:
1726 return _WKPaginationModeBottomToTop;
1729 ASSERT_NOT_REACHED();
1730 return _WKPaginationModeUnpaginated;
1733 - (void)_setPaginationMode:(_WKPaginationMode)paginationMode
1735 WebCore::Pagination::Mode mode;
1736 switch (paginationMode) {
1737 case _WKPaginationModeUnpaginated:
1738 mode = WebCore::Pagination::Unpaginated;
1740 case _WKPaginationModeLeftToRight:
1741 mode = WebCore::Pagination::LeftToRightPaginated;
1743 case _WKPaginationModeRightToLeft:
1744 mode = WebCore::Pagination::RightToLeftPaginated;
1746 case _WKPaginationModeTopToBottom:
1747 mode = WebCore::Pagination::TopToBottomPaginated;
1749 case _WKPaginationModeBottomToTop:
1750 mode = WebCore::Pagination::BottomToTopPaginated;
1756 _page->setPaginationMode(mode);
1759 - (BOOL)_paginationBehavesLikeColumns
1761 return _page->paginationBehavesLikeColumns();
1764 - (void)_setPaginationBehavesLikeColumns:(BOOL)behavesLikeColumns
1766 _page->setPaginationBehavesLikeColumns(behavesLikeColumns);
1769 - (CGFloat)_pageLength
1771 return _page->pageLength();
1774 - (void)_setPageLength:(CGFloat)pageLength
1776 _page->setPageLength(pageLength);
1779 - (CGFloat)_gapBetweenPages
1781 return _page->gapBetweenPages();
1784 - (void)_setGapBetweenPages:(CGFloat)gapBetweenPages
1786 _page->setGapBetweenPages(gapBetweenPages);
1789 - (NSUInteger)_pageCount
1791 return _page->pageCount();
1794 - (BOOL)_supportsTextZoom
1796 return _page->supportsTextZoom();
1799 - (double)_textZoomFactor
1801 return _page->textZoomFactor();
1804 - (void)_setTextZoomFactor:(double)zoomFactor
1806 _page->setTextZoomFactor(zoomFactor);
1809 - (double)_pageZoomFactor
1811 return _page->pageZoomFactor();
1814 - (void)_setPageZoomFactor:(double)zoomFactor
1816 _page->setPageZoomFactor(zoomFactor);
1819 - (id <_WKFindDelegate>)_findDelegate
1821 return [static_cast<WebKit::FindClient&>(_page->findClient()).delegate().leakRef() autorelease];
1824 - (void)_setFindDelegate:(id<_WKFindDelegate>)findDelegate
1826 static_cast<WebKit::FindClient&>(_page->findClient()).setDelegate(findDelegate);
1829 static inline WebKit::FindOptions toFindOptions(_WKFindOptions wkFindOptions)
1831 unsigned findOptions = 0;
1833 if (wkFindOptions & _WKFindOptionsCaseInsensitive)
1834 findOptions |= WebKit::FindOptionsCaseInsensitive;
1835 if (wkFindOptions & _WKFindOptionsAtWordStarts)
1836 findOptions |= WebKit::FindOptionsAtWordStarts;
1837 if (wkFindOptions & _WKFindOptionsTreatMedialCapitalAsWordStart)
1838 findOptions |= WebKit::FindOptionsTreatMedialCapitalAsWordStart;
1839 if (wkFindOptions & _WKFindOptionsBackwards)
1840 findOptions |= WebKit::FindOptionsBackwards;
1841 if (wkFindOptions & _WKFindOptionsWrapAround)
1842 findOptions |= WebKit::FindOptionsWrapAround;
1843 if (wkFindOptions & _WKFindOptionsShowOverlay)
1844 findOptions |= WebKit::FindOptionsShowOverlay;
1845 if (wkFindOptions & _WKFindOptionsShowFindIndicator)
1846 findOptions |= WebKit::FindOptionsShowFindIndicator;
1847 if (wkFindOptions & _WKFindOptionsShowHighlight)
1848 findOptions |= WebKit::FindOptionsShowHighlight;
1849 if (wkFindOptions & _WKFindOptionsDetermineMatchIndex)
1850 findOptions |= WebKit::FindOptionsDetermineMatchIndex;
1852 return static_cast<WebKit::FindOptions>(findOptions);
1855 - (void)_countStringMatches:(NSString *)string options:(_WKFindOptions)options maxCount:(NSUInteger)maxCount
1857 _page->countStringMatches(string, toFindOptions(options), maxCount);
1860 - (void)_findString:(NSString *)string options:(_WKFindOptions)options maxCount:(NSUInteger)maxCount
1862 _page->findString(string, toFindOptions(options), maxCount);
1867 _page->hideFindUI();
1870 - (id <_WKFormDelegate>)_formDelegate
1872 return _formDelegate.getAutoreleased();
1875 - (void)_setFormDelegate:(id <_WKFormDelegate>)formDelegate
1877 _formDelegate = formDelegate;
1879 class FormClient : public API::FormClient {
1881 explicit FormClient(WKWebView *webView)
1882 : m_webView(webView)
1886 virtual ~FormClient() { }
1888 virtual bool willSubmitForm(WebKit::WebPageProxy*, WebKit::WebFrameProxy*, WebKit::WebFrameProxy* sourceFrame, const Vector<std::pair<WTF::String, WTF::String>>& textFieldValues, API::Object* userData, WebKit::WebFormSubmissionListenerProxy* listener) override
1890 if (userData && userData->type() != API::Object::Type::Data) {
1891 ASSERT(!userData || userData->type() == API::Object::Type::Data);
1892 m_webView->_page->process().connection()->markCurrentlyDispatchedMessageAsInvalid();
1896 auto formDelegate = m_webView->_formDelegate.get();
1898 if (![formDelegate respondsToSelector:@selector(_webView:willSubmitFormValues:userObject:submissionHandler:)])
1901 auto valueMap = adoptNS([[NSMutableDictionary alloc] initWithCapacity:textFieldValues.size()]);
1902 for (const auto& pair : textFieldValues)
1903 [valueMap setObject:pair.second forKey:pair.first];
1905 NSObject <NSSecureCoding> *userObject = nil;
1906 if (API::Data* data = static_cast<API::Data*>(userData)) {
1907 auto nsData = adoptNS([[NSData alloc] initWithBytesNoCopy:const_cast<void*>(static_cast<const void*>(data->bytes())) length:data->size() freeWhenDone:NO]);
1908 auto unarchiver = adoptNS([[NSKeyedUnarchiver alloc] initForReadingWithData:nsData.get()]);
1909 [unarchiver setRequiresSecureCoding:YES];
1911 userObject = [unarchiver decodeObjectOfClass:[NSObject class] forKey:@"userObject"];
1912 } @catch (NSException *exception) {
1913 LOG_ERROR("Failed to decode user data: %@", exception);
1917 [formDelegate _webView:m_webView willSubmitFormValues:valueMap.get() userObject:userObject submissionHandler:^{
1918 listener->continueSubmission();
1924 WKWebView *m_webView;
1928 _page->setFormClient(std::make_unique<FormClient>(self));
1930 _page->setFormClient(nullptr);
1933 - (BOOL)_isDisplayingStandaloneImageDocument
1935 if (auto* mainFrame = _page->mainFrame())
1936 return mainFrame->isDisplayingStandaloneImageDocument();
1940 #pragma mark iOS-specific methods
1944 - (CGSize)_minimumLayoutSizeOverride
1946 ASSERT(_overridesMinimumLayoutSize);
1947 return _minimumLayoutSizeOverride;
1950 - (void)_setMinimumLayoutSizeOverride:(CGSize)minimumLayoutSizeOverride
1952 _overridesMinimumLayoutSize = YES;
1953 if (CGSizeEqualToSize(_minimumLayoutSizeOverride, minimumLayoutSizeOverride))
1956 _minimumLayoutSizeOverride = minimumLayoutSizeOverride;
1957 if (!_isAnimatingResize)
1958 _page->setViewportConfigurationMinimumLayoutSize(WebCore::FloatSize(minimumLayoutSizeOverride));
1961 - (CGSize)_minimumLayoutSizeOverrideForMinimalUI
1963 ASSERT(_overridesMinimumLayoutSizeForMinimalUI);
1964 return _minimumLayoutSizeOverrideForMinimalUI;
1967 - (void)_setMinimumLayoutSizeOverrideForMinimalUI:(CGSize)size
1969 _overridesMinimumLayoutSizeForMinimalUI = YES;
1970 if (CGSizeEqualToSize(_minimumLayoutSizeOverrideForMinimalUI, size))
1973 _minimumLayoutSizeOverrideForMinimalUI = size;
1974 if (!_isAnimatingResize)
1975 _page->setViewportConfigurationMinimumLayoutSizeForMinimalUI(WebCore::FloatSize(size));
1978 - (UIEdgeInsets)_obscuredInsets
1980 return _obscuredInsets;
1983 - (void)_setObscuredInsets:(UIEdgeInsets)obscuredInsets
1985 ASSERT(obscuredInsets.top >= 0);
1986 ASSERT(obscuredInsets.left >= 0);
1987 ASSERT(obscuredInsets.bottom >= 0);
1988 ASSERT(obscuredInsets.right >= 0);
1990 if (UIEdgeInsetsEqualToEdgeInsets(_obscuredInsets, obscuredInsets))
1993 _obscuredInsets = obscuredInsets;
1995 [self _updateVisibleContentRects];
1998 - (void)_setInterfaceOrientationOverride:(UIInterfaceOrientation)interfaceOrientation
2000 if (!_overridesInterfaceOrientation)
2001 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidRotateNotification object:nil];
2003 _overridesInterfaceOrientation = YES;
2005 if (interfaceOrientation == _interfaceOrientationOverride)
2008 _interfaceOrientationOverride = interfaceOrientation;
2010 if (!_isAnimatingResize)
2011 _page->setDeviceOrientation(deviceOrientationForUIInterfaceOrientation(_interfaceOrientationOverride));
2014 - (UIInterfaceOrientation)_interfaceOrientationOverride
2016 ASSERT(_overridesInterfaceOrientation);
2017 return _interfaceOrientationOverride;
2020 - (CGSize)_maximumUnobscuredSizeOverride
2022 ASSERT(_overridesMaximumUnobscuredSize);
2023 return _maximumUnobscuredSizeOverride;
2026 - (void)_setMaximumUnobscuredSizeOverride:(CGSize)size
2028 ASSERT(size.width <= self.bounds.size.width && size.height <= self.bounds.size.height);
2029 _overridesMaximumUnobscuredSize = YES;
2030 if (CGSizeEqualToSize(_maximumUnobscuredSizeOverride, size))
2033 _maximumUnobscuredSizeOverride = size;
2034 if (!_isAnimatingResize)
2035 _page->setMaximumUnobscuredSize(WebCore::FloatSize(size));
2038 - (void)_setBackgroundExtendsBeyondPage:(BOOL)backgroundExtends
2040 _page->setBackgroundExtendsBeyondPage(backgroundExtends);
2043 - (BOOL)_backgroundExtendsBeyondPage
2045 return _page->backgroundExtendsBeyondPage();
2048 - (void)_beginInteractiveObscuredInsetsChange
2050 ASSERT(!_isChangingObscuredInsetsInteractively);
2051 _isChangingObscuredInsetsInteractively = YES;
2054 - (void)_endInteractiveObscuredInsetsChange
2056 ASSERT(_isChangingObscuredInsetsInteractively);
2057 _isChangingObscuredInsetsInteractively = NO;
2058 [self _updateVisibleContentRects];
2061 - (void)_beginAnimatedResizeWithUpdates:(void (^)(void))updateBlock
2063 if (_customContentView) {
2068 _isAnimatingResize = YES;
2070 CGRect oldBounds = self.bounds;
2071 WebCore::FloatSize oldMinimumLayoutSize = activeMinimumLayoutSize(self, oldBounds);
2072 WebCore::FloatSize oldMinimumLayoutSizeForMinimalUI = activeMinimumLayoutSizeForMinimalUI(self, oldMinimumLayoutSize);
2073 WebCore::FloatSize oldMaximumUnobscuredSize = activeMaximumUnobscuredSize(self, oldBounds);
2074 int32_t oldOrientation = activeOrientation(self);
2075 UIEdgeInsets oldObscuredInsets = _obscuredInsets;
2076 WebCore::FloatRect oldUnobscuredContentRect = _page->unobscuredContentRect();
2080 CGRect newBounds = self.bounds;
2081 WebCore::FloatSize newMinimumLayoutSize = activeMinimumLayoutSize(self, newBounds);
2082 WebCore::FloatSize newMinimumLayoutSizeForMinimalUI = activeMinimumLayoutSizeForMinimalUI(self, newMinimumLayoutSize);
2083 WebCore::FloatSize newMaximumUnobscuredSize = activeMaximumUnobscuredSize(self, newBounds);
2084 int32_t newOrientation = activeOrientation(self);
2085 UIEdgeInsets newObscuredInsets = _obscuredInsets;
2087 if (CGRectEqualToRect(oldBounds, newBounds)
2088 && oldMinimumLayoutSize == newMinimumLayoutSize
2089 && oldMinimumLayoutSizeForMinimalUI == newMinimumLayoutSizeForMinimalUI
2090 && oldMaximumUnobscuredSize == newMaximumUnobscuredSize
2091 && oldOrientation == newOrientation
2092 && UIEdgeInsetsEqualToEdgeInsets(oldObscuredInsets, newObscuredInsets)) {
2093 _isAnimatingResize = NO;
2094 [self _updateVisibleContentRects];
2098 _resizeAnimationTransformAdjustments = CATransform3DIdentity;
2100 NSUInteger indexOfContentView = [[_scrollView subviews] indexOfObject:_contentView.get()];
2101 _resizeAnimationView = adoptNS([[UIView alloc] init]);
2102 [_scrollView insertSubview:_resizeAnimationView.get() atIndex:indexOfContentView];
2103 [_resizeAnimationView addSubview:_contentView.get()];
2105 CGSize contentSizeInContentViewCoordinates = [_contentView bounds].size;
2106 [_scrollView setMinimumZoomScale:std::min(newMinimumLayoutSize.width() / contentSizeInContentViewCoordinates.width, [_scrollView minimumZoomScale])];
2107 [_scrollView setMaximumZoomScale:std::max(newMinimumLayoutSize.width() / contentSizeInContentViewCoordinates.width, [_scrollView maximumZoomScale])];
2109 // Compute the new scale to keep the current content width in the scrollview.
2110 CGFloat oldWebViewWidthInContentViewCoordinates = oldUnobscuredContentRect.width();
2111 CGFloat visibleContentViewWidthInContentCoordinates = std::min(contentSizeInContentViewCoordinates.width, oldWebViewWidthInContentViewCoordinates);
2112 CGFloat targetScale = newMinimumLayoutSize.width() / visibleContentViewWidthInContentCoordinates;
2113 CGFloat resizeAnimationViewAnimationScale = targetScale / contentZoomScale(self);
2114 [_resizeAnimationView setTransform:CGAffineTransformMakeScale(resizeAnimationViewAnimationScale, resizeAnimationViewAnimationScale)];
2116 // Compute a new position to keep the content centered.
2117 CGPoint originalContentCenter = oldUnobscuredContentRect.center();
2118 CGPoint originalContentCenterInSelfCoordinates = [self convertPoint:originalContentCenter fromView:_contentView.get()];
2119 CGRect futureUnobscuredRectInSelfCoordinates = UIEdgeInsetsInsetRect(newBounds, _obscuredInsets);
2120 CGPoint futureUnobscuredRectCenterInSelfCoordinates = CGPointMake(futureUnobscuredRectInSelfCoordinates.origin.x + futureUnobscuredRectInSelfCoordinates.size.width / 2, futureUnobscuredRectInSelfCoordinates.origin.y + futureUnobscuredRectInSelfCoordinates.size.height / 2);
2122 CGPoint originalContentOffset = [_scrollView contentOffset];
2123 CGPoint contentOffset = originalContentOffset;
2124 contentOffset.x += (originalContentCenterInSelfCoordinates.x - futureUnobscuredRectCenterInSelfCoordinates.x);
2125 contentOffset.y += (originalContentCenterInSelfCoordinates.y - futureUnobscuredRectCenterInSelfCoordinates.y);
2127 // Limit the new offset within the scrollview, we do not want to rubber band programmatically.
2128 CGSize futureContentSizeInSelfCoordinates = CGSizeMake(contentSizeInContentViewCoordinates.width * targetScale, contentSizeInContentViewCoordinates.height * targetScale);
2129 CGFloat maxHorizontalOffset = futureContentSizeInSelfCoordinates.width - newBounds.size.width + _obscuredInsets.right;
2130 contentOffset.x = std::min(contentOffset.x, maxHorizontalOffset);
2131 CGFloat maxVerticalOffset = futureContentSizeInSelfCoordinates.height - newBounds.size.height + _obscuredInsets.bottom;
2132 contentOffset.y = std::min(contentOffset.y, maxVerticalOffset);
2134 contentOffset.x = std::max(contentOffset.x, -_obscuredInsets.left);
2135 contentOffset.y = std::max(contentOffset.y, -_obscuredInsets.top);
2137 // Make the top/bottom edges "sticky" within 1 pixel.
2138 if (oldUnobscuredContentRect.maxY() > contentSizeInContentViewCoordinates.height - 1)
2139 contentOffset.y = maxVerticalOffset;
2140 if (oldUnobscuredContentRect.y() < 1)
2141 contentOffset.y = -_obscuredInsets.top;
2143 // FIXME: if we have content centered after double tap to zoom, we should also try to keep that rect in view.
2144 [_scrollView setContentSize:roundScrollViewContentSize(*_page, futureContentSizeInSelfCoordinates)];
2145 [_scrollView setContentOffset:contentOffset];
2147 CGRect visibleRectInContentCoordinates = [self convertRect:newBounds toView:_contentView.get()];
2148 CGRect unobscuredRectInContentCoordinates = [self convertRect:futureUnobscuredRectInSelfCoordinates toView:_contentView.get()];
2150 _page->dynamicViewportSizeUpdate(newMinimumLayoutSize, newMinimumLayoutSizeForMinimalUI, newMaximumUnobscuredSize, visibleRectInContentCoordinates, unobscuredRectInContentCoordinates, futureUnobscuredRectInSelfCoordinates, targetScale, newOrientation);
2153 - (void)_endAnimatedResize
2155 if (!_isAnimatingResize)
2158 _page->synchronizeDynamicViewportUpdate();
2160 NSUInteger indexOfResizeAnimationView = [[_scrollView subviews] indexOfObject:_resizeAnimationView.get()];
2161 [_scrollView insertSubview:_contentView.get() atIndex:indexOfResizeAnimationView];
2163 CALayer *contentViewLayer = [_contentView layer];
2164 CGFloat adjustmentScale = _resizeAnimationTransformAdjustments.m11;
2165 contentViewLayer.sublayerTransform = CATransform3DIdentity;
2167 CGFloat animatingScaleTarget = [[_resizeAnimationView layer] transform].m11;
2168 CALayer *contentLayer = [_contentView layer];
2169 CATransform3D contentLayerTransform = contentLayer.transform;
2170 CGFloat currentScale = [[_resizeAnimationView layer] transform].m11 * contentLayerTransform.m11;
2172 // We cannot use [UIScrollView setZoomScale:] directly because the UIScrollView delegate would get a callback with
2173 // an invalid contentOffset. The real content offset is only set below.
2174 // Since there is no public API for setting both the zoomScale and the contentOffset, we set the zoomScale manually
2175 // on the zoom layer and then only change the contentOffset.
2176 CGFloat adjustedScale = adjustmentScale * currentScale;
2177 contentLayerTransform.m11 = adjustedScale;
2178 contentLayerTransform.m22 = adjustedScale;
2179 contentLayer.transform = contentLayerTransform;
2181 CGPoint currentScrollOffset = [_scrollView contentOffset];
2182 double horizontalScrollAdjustement = _resizeAnimationTransformAdjustments.m41 * animatingScaleTarget;
2183 double verticalScrollAdjustment = _resizeAnimationTransformAdjustments.m42 * animatingScaleTarget;
2185 [_scrollView setContentSize:roundScrollViewContentSize(*_page, [_contentView frame].size)];
2186 [_scrollView setContentOffset:CGPointMake(currentScrollOffset.x - horizontalScrollAdjustement, currentScrollOffset.y - verticalScrollAdjustment)];
2188 [_resizeAnimationView removeFromSuperview];
2189 _resizeAnimationView = nil;
2190 _resizeAnimationTransformAdjustments = CATransform3DIdentity;
2192 _isAnimatingResize = NO;
2193 [self _updateVisibleContentRects];
2196 - (void)_setOverlaidAccessoryViewsInset:(CGSize)inset
2198 [_customContentView web_setOverlaidAccessoryViewsInset:inset];
2201 - (void)_snapshotRect:(CGRect)rectInViewCoordinates intoImageOfWidth:(CGFloat)imageWidth completionHandler:(void(^)(CGImageRef))completionHandler
2203 CGRect snapshotRectInContentCoordinates = [self convertRect:rectInViewCoordinates toView:_contentView.get()];
2204 CGFloat imageHeight = imageWidth / snapshotRectInContentCoordinates.size.width * snapshotRectInContentCoordinates.size.height;
2205 CGSize imageSize = CGSizeMake(imageWidth, imageHeight);
2207 void(^copiedCompletionHandler)(CGImageRef) = [completionHandler copy];
2208 _page->takeSnapshot(WebCore::enclosingIntRect(snapshotRectInContentCoordinates), WebCore::expandedIntSize(WebCore::FloatSize(imageSize)), WebKit::SnapshotOptionsExcludeDeviceScaleFactor, [=](const WebKit::ShareableBitmap::Handle& imageHandle, WebKit::CallbackBase::Error) {
2209 if (imageHandle.isNull()) {
2210 copiedCompletionHandler(nullptr);
2211 [copiedCompletionHandler release];
2215 RefPtr<WebKit::ShareableBitmap> bitmap = WebKit::ShareableBitmap::create(imageHandle, WebKit::SharedMemory::ReadOnly);
2218 copiedCompletionHandler(nullptr);
2219 [copiedCompletionHandler release];
2223 RetainPtr<CGImageRef> cgImage;
2224 cgImage = bitmap->makeCGImage();
2225 copiedCompletionHandler(cgImage.get());
2226 [copiedCompletionHandler release];
2230 - (void)_overrideLayoutParametersWithMinimumLayoutSize:(CGSize)minimumLayoutSize minimumLayoutSizeForMinimalUI:(CGSize)minimumLayoutSizeForMinimalUI maximumUnobscuredSizeOverride:(CGSize)maximumUnobscuredSizeOverride
2232 // FIXME: After Safari is updated to use this function instead of setting the parameters separately, we should remove
2233 // the individual setters and send a single message to send everything at once to the WebProcess.
2234 self._minimumLayoutSizeOverride = minimumLayoutSize;
2235 self._minimumLayoutSizeOverrideForMinimalUI = minimumLayoutSizeForMinimalUI;
2236 self._maximumUnobscuredSizeOverride = maximumUnobscuredSizeOverride;
2239 - (UIView *)_viewForFindUI
2241 return [self viewForZoomingInScrollView:[self scrollView]];
2244 - (BOOL)_isDisplayingPDF
2246 return [_customContentView isKindOfClass:[WKPDFView class]];
2249 - (NSData *)_dataForDisplayedPDF
2251 if (![self _isDisplayingPDF])
2253 CGPDFDocumentRef pdfDocument = [(WKPDFView *)_customContentView pdfDocument];
2254 return [(NSData *)CGDataProviderCopyData(CGPDFDocumentGetDataProvider(pdfDocument)) autorelease];
2257 - (NSString *)_suggestedFilenameForDisplayedPDF
2259 if (![self _isDisplayingPDF])
2261 return [(WKPDFView *)_customContentView.get() suggestedFilename];
2264 - (CGFloat)_viewportMetaTagWidth
2266 return _viewportMetaTagWidth;
2269 - (_WKWebViewPrintFormatter *)_webViewPrintFormatter
2271 UIViewPrintFormatter *viewPrintFormatter = self.viewPrintFormatter;
2272 ASSERT([viewPrintFormatter isKindOfClass:[_WKWebViewPrintFormatter class]]);
2273 return (_WKWebViewPrintFormatter *)viewPrintFormatter;
2278 #pragma mark - OS X-specific methods
2280 - (NSColor *)_pageExtendedBackgroundColor
2282 WebCore::Color color = _page->pageExtendedBackgroundColor();
2283 if (!color.isValid())
2286 return nsColor(color);
2289 - (BOOL)_drawsTransparentBackground
2291 return _page->drawsTransparentBackground();
2294 - (void)_setDrawsTransparentBackground:(BOOL)drawsTransparentBackground
2296 _page->setDrawsTransparentBackground(drawsTransparentBackground);
2299 - (void)_setTopContentInset:(CGFloat)contentInset
2301 [_wkView _setTopContentInset:contentInset];
2304 - (CGFloat)_topContentInset
2306 return [_wkView _topContentInset];
2309 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
2311 - (void)_setAutomaticallyAdjustsContentInsets:(BOOL)automaticallyAdjustsContentInsets
2313 [_wkView _setAutomaticallyAdjustsContentInsets:automaticallyAdjustsContentInsets];
2316 - (BOOL)_automaticallyAdjustsContentInsets
2318 return [_wkView _automaticallyAdjustsContentInsets];
2327 #if !TARGET_OS_IPHONE
2329 @implementation WKWebView (WKIBActions)
2331 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)item
2333 SEL action = item.action;
2335 if (action == @selector(goBack:))
2336 return !!_page->backForwardList().backItem();
2338 if (action == @selector(goForward:))
2339 return !!_page->backForwardList().forwardItem();
2341 if (action == @selector(stopLoading:)) {
2342 // FIXME: Return no if we're stopped.
2346 if (action == @selector(reload:) || action == @selector(reloadFromOrigin:)) {
2347 // FIXME: Return no if we're loading.
2354 - (IBAction)goBack:(id)sender
2359 - (IBAction)goForward:(id)sender
2364 - (IBAction)reload:(id)sender
2369 - (IBAction)reloadFromOrigin:(id)sender
2371 [self reloadFromOrigin];
2374 - (IBAction)stopLoading:(id)sender
2376 _page->stopLoading();
2384 @implementation WKWebView (_WKWebViewPrintFormatter)
2386 - (Class)_printFormatterClass
2388 return [_WKWebViewPrintFormatter class];
2391 - (NSInteger)_computePageCountAndStartDrawingToPDFForFrame:(_WKFrameHandle *)frame printInfo:(const WebKit::PrintInfo&)printInfo firstPage:(uint32_t)firstPage computedTotalScaleFactor:(double&)totalScaleFactor
2393 if ([self _isDisplayingPDF])
2394 return CGPDFDocumentGetNumberOfPages([(WKPDFView *)_customContentView pdfDocument]);
2396 _pageIsPrintingToPDF = YES;
2397 Vector<WebCore::IntRect> pageRects;
2398 uint64_t frameID = frame ? frame._frameID : _page->mainFrame()->frameID();
2399 if (!_page->sendSync(Messages::WebPage::ComputePagesForPrintingAndStartDrawingToPDF(frameID, printInfo, firstPage), Messages::WebPage::ComputePagesForPrintingAndStartDrawingToPDF::Reply(pageRects, totalScaleFactor)))
2401 return pageRects.size();
2404 - (void)_endPrinting
2406 _pageIsPrintingToPDF = NO;
2407 _printedDocument = nullptr;
2408 _page->send(Messages::WebPage::EndPrinting());
2411 // FIXME: milliseconds::max() overflows when converted to nanoseconds, causing condition_variable::wait_for() to believe
2412 // a timeout occurred on any spurious wakeup. Use nanoseconds::max() (converted to ms) to avoid this. We should perhaps
2413 // change waitForAndDispatchImmediately() to take nanoseconds to avoid this issue.
2414 static constexpr std::chrono::milliseconds didFinishLoadingTimeout = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::nanoseconds::max());
2416 - (CGPDFDocumentRef)_printedDocument
2418 if ([self _isDisplayingPDF]) {
2419 ASSERT(!_pageIsPrintingToPDF);
2420 return [(WKPDFView *)_customContentView pdfDocument];
2423 if (_pageIsPrintingToPDF) {
2424 if (!_page->process().connection()->waitForAndDispatchImmediately<Messages::WebPageProxy::DidFinishDrawingPagesToPDF>(_page->pageID(), didFinishLoadingTimeout)) {
2425 ASSERT_NOT_REACHED();
2428 ASSERT(!_pageIsPrintingToPDF);
2430 return _printedDocument.get();
2433 - (void)_setPrintedDocument:(CGPDFDocumentRef)printedDocument
2435 if (!_pageIsPrintingToPDF)
2437 ASSERT(![self _isDisplayingPDF]);
2438 _printedDocument = printedDocument;
2439 _pageIsPrintingToPDF = NO;
2445 #endif // WK_API_ENABLED