Optimizing loads when creating new pages
[WebKit-https.git] / Source / WebKit / UIProcess / Cocoa / UIDelegate.mm
1 /*
2  * Copyright (C) 2014-2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "UIDelegate.h"
28
29 #import "APIFrameInfo.h"
30 #import "APIHitTestResult.h"
31 #import "CompletionHandlerCallChecker.h"
32 #import "MediaCaptureUtilities.h"
33 #import "NativeWebWheelEvent.h"
34 #import "NavigationActionData.h"
35 #import "UserMediaPermissionCheckProxy.h"
36 #import "UserMediaPermissionRequestProxy.h"
37 #import "WKFrameInfoInternal.h"
38 #import "WKNSData.h"
39 #import "WKNSDictionary.h"
40 #import "WKNavigationActionInternal.h"
41 #import "WKOpenPanelParametersInternal.h"
42 #import "WKSecurityOriginInternal.h"
43 #import "WKUIDelegatePrivate.h"
44 #import "WKWebViewConfigurationInternal.h"
45 #import "WKWebViewInternal.h"
46 #import "WKWindowFeaturesInternal.h"
47 #import "WebEventFactory.h"
48 #import "WebOpenPanelResultListenerProxy.h"
49 #import "WebProcessProxy.h"
50 #import "_WKContextMenuElementInfo.h"
51 #import "_WKFrameHandleInternal.h"
52 #import "_WKHitTestResultInternal.h"
53 #import <WebCore/FontAttributes.h>
54 #import <WebCore/SecurityOriginData.h>
55 #import <wtf/BlockPtr.h>
56 #import <wtf/URL.h>
57
58 #if HAVE(AUTHORIZATION_STATUS_FOR_MEDIA_TYPE)
59 #import <AVFoundation/AVCaptureDevice.h>
60 #import <AVFoundation/AVMediaFormat.h>
61 #import <wtf/SoftLinking.h>
62
63 SOFT_LINK_FRAMEWORK(AVFoundation);
64 SOFT_LINK_CLASS(AVFoundation, AVCaptureDevice);
65 SOFT_LINK_CONSTANT(AVFoundation, AVMediaTypeAudio, NSString *);
66 SOFT_LINK_CONSTANT(AVFoundation, AVMediaTypeVideo, NSString *);
67 #endif
68
69 namespace WebKit {
70
71 UIDelegate::UIDelegate(WKWebView *webView)
72     : m_webView(webView)
73 {
74 }
75
76 UIDelegate::~UIDelegate()
77 {
78 }
79
80 #if ENABLE(CONTEXT_MENUS)
81 std::unique_ptr<API::ContextMenuClient> UIDelegate::createContextMenuClient()
82 {
83     return std::make_unique<ContextMenuClient>(*this);
84 }
85 #endif
86
87 std::unique_ptr<API::UIClient> UIDelegate::createUIClient()
88 {
89     return std::make_unique<UIClient>(*this);
90 }
91
92 RetainPtr<id <WKUIDelegate> > UIDelegate::delegate()
93 {
94     return m_delegate.get();
95 }
96
97 void UIDelegate::setDelegate(id <WKUIDelegate> delegate)
98 {
99     m_delegate = delegate;
100
101     m_delegateMethods.webViewCreateWebViewWithConfigurationForNavigationActionWindowFeatures = [delegate respondsToSelector:@selector(webView:createWebViewWithConfiguration:forNavigationAction:windowFeatures:)];
102     m_delegateMethods.webViewCreateWebViewWithConfigurationForNavigationActionWindowFeaturesAsync = [delegate respondsToSelector:@selector(_webView:createWebViewWithConfiguration:forNavigationAction:windowFeatures:completionHandler:)];
103     m_delegateMethods.webViewRunJavaScriptAlertPanelWithMessageInitiatedByFrameCompletionHandler = [delegate respondsToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:)];
104     m_delegateMethods.webViewRunJavaScriptConfirmPanelWithMessageInitiatedByFrameCompletionHandler = [delegate respondsToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:)];
105     m_delegateMethods.webViewRunJavaScriptTextInputPanelWithPromptDefaultTextInitiatedByFrameCompletionHandler = [delegate respondsToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:)];
106     m_delegateMethods.webViewRequestStorageAccessPanelForTopPrivatelyControlledDomainUnderFirstPartyTopPrivatelyControlledDomainCompletionHandler = [delegate respondsToSelector:@selector(_webView:requestStorageAccessPanelForDomain:underCurrentDomain:completionHandler:)];
107     m_delegateMethods.webViewRunBeforeUnloadConfirmPanelWithMessageInitiatedByFrameCompletionHandler = [delegate respondsToSelector:@selector(_webView:runBeforeUnloadConfirmPanelWithMessage:initiatedByFrame:completionHandler:)];
108     m_delegateMethods.webViewRequestGeolocationPermissionForFrameDecisionHandler = [delegate respondsToSelector:@selector(_webView:requestGeolocationPermissionForFrame:decisionHandler:)];
109     m_delegateMethods.webViewDidResignInputElementStrongPasswordAppearanceWithUserInfo = [delegate respondsToSelector:@selector(_webView:didResignInputElementStrongPasswordAppearanceWithUserInfo:)];
110     m_delegateMethods.webViewTakeFocus = [delegate respondsToSelector:@selector(_webView:takeFocus:)];
111
112 #if PLATFORM(MAC)
113     m_delegateMethods.showWebView = [delegate respondsToSelector:@selector(_showWebView:)];
114     m_delegateMethods.focusWebView = [delegate respondsToSelector:@selector(_focusWebView:)];
115     m_delegateMethods.unfocusWebView = [delegate respondsToSelector:@selector(_unfocusWebView:)];
116     m_delegateMethods.webViewRunModal = [delegate respondsToSelector:@selector(_webViewRunModal:)];
117     m_delegateMethods.webViewDidScroll = [delegate respondsToSelector:@selector(_webViewDidScroll:)];
118     m_delegateMethods.webViewGetToolbarsAreVisibleWithCompletionHandler = [delegate respondsToSelector:@selector(_webView:getToolbarsAreVisibleWithCompletionHandler:)];
119     m_delegateMethods.webViewDidNotHandleWheelEvent = [delegate respondsToSelector:@selector(_webView:didNotHandleWheelEvent:)];
120     m_delegateMethods.webViewSetResizable = [delegate respondsToSelector:@selector(_webView:setResizable:)];
121     m_delegateMethods.webViewGetWindowFrameWithCompletionHandler = [delegate respondsToSelector:@selector(_webView:getWindowFrameWithCompletionHandler:)];
122     m_delegateMethods.webViewSetWindowFrame = [delegate respondsToSelector:@selector(_webView:setWindowFrame:)];
123     m_delegateMethods.webViewUnavailablePlugInButtonClicked = [delegate respondsToSelector:@selector(_webView:unavailablePlugInButtonClickedWithReason:plugInInfo:)];
124     m_delegateMethods.webViewHandleAutoplayEventWithFlags = [delegate respondsToSelector:@selector(_webView:handleAutoplayEvent:withFlags:)];
125     m_delegateMethods.webViewDidClickAutoFillButtonWithUserInfo = [delegate respondsToSelector:@selector(_webView:didClickAutoFillButtonWithUserInfo:)];
126     m_delegateMethods.webViewDrawHeaderInRectForPageWithTitleURL = [delegate respondsToSelector:@selector(_webView:drawHeaderInRect:forPageWithTitle:URL:)];
127     m_delegateMethods.webViewDrawFooterInRectForPageWithTitleURL = [delegate respondsToSelector:@selector(_webView:drawFooterInRect:forPageWithTitle:URL:)];
128     m_delegateMethods.webViewHeaderHeight = [delegate respondsToSelector:@selector(_webViewHeaderHeight:)];
129     m_delegateMethods.webViewFooterHeight = [delegate respondsToSelector:@selector(_webViewFooterHeight:)];
130     m_delegateMethods.webViewMouseDidMoveOverElementWithFlagsUserInfo = [delegate respondsToSelector:@selector(_webView:mouseDidMoveOverElement:withFlags:userInfo:)];
131     m_delegateMethods.webViewDidExceedBackgroundResourceLimitWhileInForeground = [delegate respondsToSelector:@selector(_webView:didExceedBackgroundResourceLimitWhileInForeground:)];
132     m_delegateMethods.webViewSaveDataToFileSuggestedFilenameMimeTypeOriginatingURL = [delegate respondsToSelector:@selector(_webView:saveDataToFile:suggestedFilename:mimeType:originatingURL:)];
133     m_delegateMethods.webViewRunOpenPanelWithParametersInitiatedByFrameCompletionHandler = [delegate respondsToSelector:@selector(webView:runOpenPanelWithParameters:initiatedByFrame:completionHandler:)];
134     m_delegateMethods.webViewRequestNotificationPermissionForSecurityOriginDecisionHandler = [delegate respondsToSelector:@selector(_webView:requestNotificationPermissionForSecurityOrigin:decisionHandler:)];
135 #endif
136 #if ENABLE(DEVICE_ORIENTATION)
137     m_delegateMethods.webViewShouldAllowDeviceOrientationAndMotionAccessRequestedByFrameDecisionHandler = [delegate respondsToSelector:@selector(_webView:shouldAllowDeviceOrientationAndMotionAccessRequestedByFrame:decisionHandler:)];
138 #endif
139     m_delegateMethods.webViewDecideDatabaseQuotaForSecurityOriginCurrentQuotaCurrentOriginUsageCurrentDatabaseUsageExpectedUsageDecisionHandler = [delegate respondsToSelector:@selector(_webView:decideDatabaseQuotaForSecurityOrigin:currentQuota:currentOriginUsage:currentDatabaseUsage:expectedUsage:decisionHandler:)];
140     m_delegateMethods.webViewDecideDatabaseQuotaForSecurityOriginDatabaseNameDisplayNameCurrentQuotaCurrentOriginUsageCurrentDatabaseUsageExpectedUsageDecisionHandler = [delegate respondsToSelector:@selector(_webView:decideDatabaseQuotaForSecurityOrigin:databaseName:displayName:currentQuota:currentOriginUsage:currentDatabaseUsage:expectedUsage:decisionHandler:)];
141     m_delegateMethods.webViewDecideWebApplicationCacheQuotaForSecurityOriginCurrentQuotaTotalBytesNeeded = [delegate respondsToSelector:@selector(_webView:decideWebApplicationCacheQuotaForSecurityOrigin:currentQuota:totalBytesNeeded:decisionHandler:)];
142     m_delegateMethods.webViewPrintFrame = [delegate respondsToSelector:@selector(_webView:printFrame:)];
143     m_delegateMethods.webViewDidClose = [delegate respondsToSelector:@selector(webViewDidClose:)];
144     m_delegateMethods.webViewClose = [delegate respondsToSelector:@selector(_webViewClose:)];
145     m_delegateMethods.webViewFullscreenMayReturnToInline = [delegate respondsToSelector:@selector(_webViewFullscreenMayReturnToInline:)];
146     m_delegateMethods.webViewDidEnterFullscreen = [delegate respondsToSelector:@selector(_webViewDidEnterFullscreen:)];
147     m_delegateMethods.webViewDidExitFullscreen = [delegate respondsToSelector:@selector(_webViewDidExitFullscreen:)];
148 #if PLATFORM(IOS_FAMILY)
149 #if HAVE(APP_LINKS)
150     m_delegateMethods.webViewShouldIncludeAppLinkActionsForElement = [delegate respondsToSelector:@selector(_webView:shouldIncludeAppLinkActionsForElement:)];
151 #endif
152     m_delegateMethods.webViewActionsForElementDefaultActions = [delegate respondsToSelector:@selector(_webView:actionsForElement:defaultActions:)];
153     m_delegateMethods.webViewDidNotHandleTapAsClickAtPoint = [delegate respondsToSelector:@selector(_webView:didNotHandleTapAsClickAtPoint:)];
154     m_delegateMethods.presentingViewControllerForWebView = [delegate respondsToSelector:@selector(_presentingViewControllerForWebView:)];
155 #endif
156     m_delegateMethods.webViewRequestMediaCaptureAuthorizationForFrameDecisionHandler = [delegate respondsToSelector:@selector(_webView:requestUserMediaAuthorizationForDevices:url:mainFrameURL:decisionHandler:)] || [delegate respondsToSelector:@selector(_webView:requestMediaCaptureAuthorization:decisionHandler:)];
157     m_delegateMethods.webViewIsMediaCaptureAuthorizedForFrameDecisionHandler = [delegate respondsToSelector:@selector(_webView:checkUserMediaPermissionForURL:mainFrameURL:frameIdentifier:decisionHandler:)] || [delegate respondsToSelector:@selector(_webView:includeSensitiveMediaDeviceDetails:)];
158
159     m_delegateMethods.webViewMediaCaptureStateDidChange = [delegate respondsToSelector:@selector(_webView:mediaCaptureStateDidChange:)];
160     m_delegateMethods.webViewDidChangeFontAttributes = [delegate respondsToSelector:@selector(_webView:didChangeFontAttributes:)];
161     m_delegateMethods.dataDetectionContextForWebView = [delegate respondsToSelector:@selector(_dataDetectionContextForWebView:)];
162     m_delegateMethods.webViewImageOrMediaDocumentSizeChanged = [delegate respondsToSelector:@selector(_webView:imageOrMediaDocumentSizeChanged:)];
163
164 #if ENABLE(POINTER_LOCK)
165     m_delegateMethods.webViewRequestPointerLock = [delegate respondsToSelector:@selector(_webViewRequestPointerLock:)];
166     m_delegateMethods.webViewDidRequestPointerLockCompletionHandler = [delegate respondsToSelector:@selector(_webViewDidRequestPointerLock:completionHandler:)];
167     m_delegateMethods.webViewDidLosePointerLock = [delegate respondsToSelector:@selector(_webViewDidLosePointerLock:)];
168 #endif
169 #if ENABLE(CONTEXT_MENUS)
170     m_delegateMethods.webViewContextMenuForElement = [delegate respondsToSelector:@selector(_webView:contextMenu:forElement:)];
171     m_delegateMethods.webViewContextMenuForElementUserInfo = [delegate respondsToSelector:@selector(_webView:contextMenu:forElement:userInfo:)];
172     m_delegateMethods.webViewGetContextMenuFromProposedMenuForElementUserInfoCompletionHandler = [delegate respondsToSelector:@selector(_webView:getContextMenuFromProposedMenu:forElement:userInfo:completionHandler:)];
173 #endif
174     
175     m_delegateMethods.webViewHasVideoInPictureInPictureDidChange = [delegate respondsToSelector:@selector(_webView:hasVideoInPictureInPictureDidChange:)];
176     m_delegateMethods.webViewDidShowSafeBrowsingWarning = [delegate respondsToSelector:@selector(_webViewDidShowSafeBrowsingWarning:)];
177 }
178
179 #if ENABLE(CONTEXT_MENUS)
180 UIDelegate::ContextMenuClient::ContextMenuClient(UIDelegate& uiDelegate)
181     : m_uiDelegate(uiDelegate)
182 {
183 }
184
185 UIDelegate::ContextMenuClient::~ContextMenuClient()
186 {
187 }
188
189 void UIDelegate::ContextMenuClient::menuFromProposedMenu(WebPageProxy&, NSMenu *menu, const WebHitTestResultData&, API::Object* userInfo, CompletionHandler<void(RetainPtr<NSMenu>&&)>&& completionHandler)
190 {
191     if (!m_uiDelegate.m_delegateMethods.webViewContextMenuForElement
192         && !m_uiDelegate.m_delegateMethods.webViewContextMenuForElementUserInfo
193         && !m_uiDelegate.m_delegateMethods.webViewGetContextMenuFromProposedMenuForElementUserInfoCompletionHandler)
194         return completionHandler(menu);
195
196     auto delegate = m_uiDelegate.m_delegate.get();
197     if (!delegate)
198         return completionHandler(menu);
199
200     auto contextMenuElementInfo = adoptNS([[_WKContextMenuElementInfo alloc] init]);
201
202     if (m_uiDelegate.m_delegateMethods.webViewGetContextMenuFromProposedMenuForElementUserInfoCompletionHandler) {
203         auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:getContextMenuFromProposedMenu:forElement:userInfo:completionHandler:));
204         [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView getContextMenuFromProposedMenu:menu forElement:contextMenuElementInfo.get() userInfo:userInfo ? static_cast<id <NSSecureCoding>>(userInfo->wrapper()) : nil completionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)] (NSMenu *menu) mutable {
205             if (checker->completionHandlerHasBeenCalled())
206                 return;
207             checker->didCallCompletionHandler();
208             completionHandler(menu);
209         }).get()];
210         return;
211     }
212     
213     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
214     if (m_uiDelegate.m_delegateMethods.webViewContextMenuForElement)
215         return completionHandler([(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView contextMenu:menu forElement:contextMenuElementInfo.get()]);
216
217     completionHandler([(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView contextMenu:menu forElement:contextMenuElementInfo.get() userInfo:userInfo ? static_cast<id <NSSecureCoding>>(userInfo->wrapper()) : nil]);
218     ALLOW_DEPRECATED_DECLARATIONS_END
219 }
220 #endif
221
222 UIDelegate::UIClient::UIClient(UIDelegate& uiDelegate)
223     : m_uiDelegate(uiDelegate)
224 {
225 }
226
227 UIDelegate::UIClient::~UIClient()
228 {
229 }
230
231 void UIDelegate::UIClient::createNewPage(WebKit::WebPageProxy&, WebCore::WindowFeatures&& windowFeatures, Ref<API::NavigationAction>&& navigationAction, CompletionHandler<void(RefPtr<WebPageProxy>&&)>&& completionHandler)
232 {
233     auto delegate = m_uiDelegate.m_delegate.get();
234     ASSERT(delegate);
235
236     auto configuration = adoptNS([m_uiDelegate.m_webView->_configuration copy]);
237     [configuration _setRelatedWebView:m_uiDelegate.m_webView];
238
239     auto apiWindowFeatures = API::WindowFeatures::create(windowFeatures);
240
241     if (m_uiDelegate.m_delegateMethods.webViewCreateWebViewWithConfigurationForNavigationActionWindowFeaturesAsync) {
242         auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:createWebViewWithConfiguration:forNavigationAction:windowFeatures:completionHandler:));
243
244         [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView createWebViewWithConfiguration:configuration.get() forNavigationAction:wrapper(navigationAction) windowFeatures:wrapper(apiWindowFeatures) completionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker), relatedWebView = RetainPtr<WKWebView>(m_uiDelegate.m_webView)] (WKWebView *webView) mutable {
245             if (checker->completionHandlerHasBeenCalled())
246                 return;
247             checker->didCallCompletionHandler();
248
249             if (!webView) {
250                 completionHandler(nullptr);
251                 return;
252             }
253
254             if ([webView->_configuration _relatedWebView] != relatedWebView.get())
255                 [NSException raise:NSInternalInconsistencyException format:@"Returned WKWebView was not created with the given configuration."];
256
257             completionHandler(webView->_page.get());
258         }).get()];
259         return;
260     }
261     if (!m_uiDelegate.m_delegateMethods.webViewCreateWebViewWithConfigurationForNavigationActionWindowFeatures)
262         return completionHandler(nullptr);
263
264     RetainPtr<WKWebView> webView = [delegate webView:m_uiDelegate.m_webView createWebViewWithConfiguration:configuration.get() forNavigationAction:wrapper(navigationAction) windowFeatures:wrapper(apiWindowFeatures)];
265     if (!webView)
266         return completionHandler(nullptr);
267
268     if ([webView->_configuration _relatedWebView] != m_uiDelegate.m_webView)
269         [NSException raise:NSInternalInconsistencyException format:@"Returned WKWebView was not created with the given configuration."];
270     completionHandler(webView->_page.get());
271 }
272
273 void UIDelegate::UIClient::runJavaScriptAlert(WebPageProxy*, const WTF::String& message, WebFrameProxy* webFrameProxy, const WebCore::SecurityOriginData& securityOriginData, Function<void()>&& completionHandler)
274 {
275     if (!m_uiDelegate.m_delegateMethods.webViewRunJavaScriptAlertPanelWithMessageInitiatedByFrameCompletionHandler) {
276         completionHandler();
277         return;
278     }
279
280     auto delegate = m_uiDelegate.m_delegate.get();
281     if (!delegate) {
282         completionHandler();
283         return;
284     }
285
286     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:));
287     [delegate webView:m_uiDelegate.m_webView runJavaScriptAlertPanelWithMessage:message initiatedByFrame:wrapper(API::FrameInfo::create(*webFrameProxy, securityOriginData.securityOrigin())) completionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)] {
288         if (checker->completionHandlerHasBeenCalled())
289             return;
290         completionHandler();
291         checker->didCallCompletionHandler();
292     }).get()];
293 }
294
295 void UIDelegate::UIClient::runJavaScriptConfirm(WebPageProxy*, const WTF::String& message, WebFrameProxy* webFrameProxy, const WebCore::SecurityOriginData& securityOriginData, Function<void(bool)>&& completionHandler)
296 {
297     if (!m_uiDelegate.m_delegateMethods.webViewRunJavaScriptConfirmPanelWithMessageInitiatedByFrameCompletionHandler) {
298         completionHandler(false);
299         return;
300     }
301
302     auto delegate = m_uiDelegate.m_delegate.get();
303     if (!delegate) {
304         completionHandler(false);
305         return;
306     }
307
308     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:));
309     [delegate webView:m_uiDelegate.m_webView runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:wrapper(API::FrameInfo::create(*webFrameProxy, securityOriginData.securityOrigin())) completionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)] (BOOL result) mutable {
310         if (checker->completionHandlerHasBeenCalled())
311             return;
312         completionHandler(result);
313         checker->didCallCompletionHandler();
314     }).get()];
315 }
316
317 void UIDelegate::UIClient::runJavaScriptPrompt(WebPageProxy*, const WTF::String& message, const WTF::String& defaultValue, WebFrameProxy* webFrameProxy, const WebCore::SecurityOriginData& securityOriginData, Function<void(const WTF::String&)>&& completionHandler)
318 {
319     if (!m_uiDelegate.m_delegateMethods.webViewRunJavaScriptTextInputPanelWithPromptDefaultTextInitiatedByFrameCompletionHandler) {
320         completionHandler(String());
321         return;
322     }
323
324     auto delegate = m_uiDelegate.m_delegate.get();
325     if (!delegate) {
326         completionHandler(String());
327         return;
328     }
329
330     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:));
331     [delegate webView:m_uiDelegate.m_webView runJavaScriptTextInputPanelWithPrompt:message defaultText:defaultValue initiatedByFrame:wrapper(API::FrameInfo::create(*webFrameProxy, securityOriginData.securityOrigin())) completionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)] (NSString *result) mutable {
332         if (checker->completionHandlerHasBeenCalled())
333             return;
334         completionHandler(result);
335         checker->didCallCompletionHandler();
336     }).get()];
337 }
338
339 void UIDelegate::UIClient::requestStorageAccessConfirm(WebPageProxy&, WebFrameProxy*, const WTF::String& requestingDomain, const WTF::String& currentDomain, CompletionHandler<void(bool)>&& completionHandler)
340 {
341     if (!m_uiDelegate.m_delegateMethods.webViewRequestStorageAccessPanelForTopPrivatelyControlledDomainUnderFirstPartyTopPrivatelyControlledDomainCompletionHandler) {
342         completionHandler(true);
343         return;
344     }
345
346     auto delegate = m_uiDelegate.m_delegate.get();
347     if (!delegate) {
348         completionHandler(true);
349         return;
350     }
351     
352     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:requestStorageAccessPanelForDomain:underCurrentDomain:completionHandler:));
353     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView requestStorageAccessPanelForDomain:requestingDomain underCurrentDomain:currentDomain completionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)] (BOOL result) mutable {
354         if (checker->completionHandlerHasBeenCalled())
355             return;
356         completionHandler(result);
357         checker->didCallCompletionHandler();
358     }).get()];
359 }
360
361 void UIDelegate::UIClient::decidePolicyForGeolocationPermissionRequest(WebKit::WebPageProxy&, WebKit::WebFrameProxy& frame, API::SecurityOrigin& securityOrigin, Function<void(bool)>& completionHandler)
362 {
363     if (!m_uiDelegate.m_delegateMethods.webViewRequestGeolocationPermissionForFrameDecisionHandler)
364         return;
365     
366     auto delegate = m_uiDelegate.m_delegate.get();
367     if (!delegate)
368         return;
369
370     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:requestGeolocationPermissionForFrame:decisionHandler:));
371     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView requestGeolocationPermissionForFrame:wrapper(API::FrameInfo::create(frame, securityOrigin.securityOrigin())) decisionHandler:makeBlockPtr([completionHandler = std::exchange(completionHandler, nullptr), checker = WTFMove(checker)] (BOOL result) mutable {
372         if (checker->completionHandlerHasBeenCalled())
373             return;
374         checker->didCallCompletionHandler();
375         completionHandler(result);
376     }).get()];
377 }
378
379 void UIDelegate::UIClient::didResignInputElementStrongPasswordAppearance(WebPageProxy&, API::Object* userInfo)
380 {
381     if (!m_uiDelegate.m_delegateMethods.webViewDidResignInputElementStrongPasswordAppearanceWithUserInfo)
382         return;
383
384     auto delegate = m_uiDelegate.m_delegate.get();
385     if (!delegate)
386         return;
387
388     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView didResignInputElementStrongPasswordAppearanceWithUserInfo:userInfo ? static_cast<id <NSSecureCoding>>(userInfo->wrapper()) : nil];
389 }
390
391 bool UIDelegate::UIClient::canRunBeforeUnloadConfirmPanel() const
392 {
393     return m_uiDelegate.m_delegateMethods.webViewRunBeforeUnloadConfirmPanelWithMessageInitiatedByFrameCompletionHandler;
394 }
395
396 void UIDelegate::UIClient::runBeforeUnloadConfirmPanel(WebPageProxy*, const WTF::String& message, WebFrameProxy* webFrameProxy, const WebCore::SecurityOriginData& securityOriginData, Function<void(bool)>&& completionHandler)
397 {
398     if (!m_uiDelegate.m_delegateMethods.webViewRunBeforeUnloadConfirmPanelWithMessageInitiatedByFrameCompletionHandler) {
399         completionHandler(false);
400         return;
401     }
402
403     auto delegate = m_uiDelegate.m_delegate.get();
404     if (!delegate) {
405         completionHandler(false);
406         return;
407     }
408
409     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:runBeforeUnloadConfirmPanelWithMessage:initiatedByFrame:completionHandler:));
410     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView runBeforeUnloadConfirmPanelWithMessage:message initiatedByFrame:wrapper(API::FrameInfo::create(*webFrameProxy, securityOriginData.securityOrigin())) completionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)] (BOOL result) mutable {
411         if (checker->completionHandlerHasBeenCalled())
412             return;
413         completionHandler(result);
414         checker->didCallCompletionHandler();
415     }).get()];
416 }
417
418 void UIDelegate::UIClient::exceededDatabaseQuota(WebPageProxy*, WebFrameProxy*, API::SecurityOrigin* securityOrigin, const WTF::String& databaseName, const WTF::String& displayName, unsigned long long currentQuota, unsigned long long currentOriginUsage, unsigned long long currentUsage, unsigned long long expectedUsage, Function<void (unsigned long long)>&& completionHandler)
419 {
420     if (!m_uiDelegate.m_delegateMethods.webViewDecideDatabaseQuotaForSecurityOriginCurrentQuotaCurrentOriginUsageCurrentDatabaseUsageExpectedUsageDecisionHandler && !m_uiDelegate.m_delegateMethods.webViewDecideDatabaseQuotaForSecurityOriginDatabaseNameDisplayNameCurrentQuotaCurrentOriginUsageCurrentDatabaseUsageExpectedUsageDecisionHandler) {
421
422         // Use 50 MB as the default database quota.
423         unsigned long long defaultPerOriginDatabaseQuota = 50 * 1024 * 1024;
424
425         completionHandler(defaultPerOriginDatabaseQuota);
426         return;
427     }
428
429     auto delegate = m_uiDelegate.m_delegate.get();
430     if (!delegate) {
431         completionHandler(currentQuota);
432         return;
433     }
434
435     ASSERT(securityOrigin);
436
437     if (m_uiDelegate.m_delegateMethods.webViewDecideDatabaseQuotaForSecurityOriginDatabaseNameDisplayNameCurrentQuotaCurrentOriginUsageCurrentDatabaseUsageExpectedUsageDecisionHandler) {
438         auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:decideDatabaseQuotaForSecurityOrigin:databaseName:displayName:currentQuota:currentOriginUsage:currentDatabaseUsage:expectedUsage:decisionHandler:));
439         [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView decideDatabaseQuotaForSecurityOrigin:wrapper(*securityOrigin) databaseName:databaseName displayName:displayName currentQuota:currentQuota currentOriginUsage:currentOriginUsage currentDatabaseUsage:currentUsage expectedUsage:expectedUsage decisionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)](unsigned long long newQuota) {
440             if (checker->completionHandlerHasBeenCalled())
441                 return;
442             checker->didCallCompletionHandler();
443             completionHandler(newQuota);
444         }).get()];
445         return;
446     }
447
448     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:decideDatabaseQuotaForSecurityOrigin:currentQuota:currentOriginUsage:currentDatabaseUsage:expectedUsage:decisionHandler:));
449     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView decideDatabaseQuotaForSecurityOrigin:wrapper(*securityOrigin) currentQuota:currentQuota currentOriginUsage:currentOriginUsage currentDatabaseUsage:currentUsage expectedUsage:expectedUsage decisionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)](unsigned long long newQuota) {
450         if (checker->completionHandlerHasBeenCalled())
451             return;
452         checker->didCallCompletionHandler();
453         completionHandler(newQuota);
454     }).get()];
455 }
456
457 static inline _WKFocusDirection toWKFocusDirection(WKFocusDirection direction)
458 {
459     switch (direction) {
460     case kWKFocusDirectionBackward:
461         return _WKFocusDirectionBackward;
462     case kWKFocusDirectionForward:
463         return _WKFocusDirectionForward;
464     }
465     ASSERT_NOT_REACHED();
466     return _WKFocusDirectionForward;
467 }
468
469 bool UIDelegate::UIClient::takeFocus(WebPageProxy*, WKFocusDirection direction)
470 {
471     if (!m_uiDelegate.m_delegateMethods.webViewTakeFocus)
472         return false;
473     
474     auto delegate = m_uiDelegate.m_delegate.get();
475     if (!delegate)
476         return false;
477     
478     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView takeFocus:toWKFocusDirection(direction)];
479     return true;
480 }
481
482 #if PLATFORM(MAC)
483 bool UIDelegate::UIClient::canRunModal() const
484 {
485     return m_uiDelegate.m_delegateMethods.webViewRunModal;
486 }
487
488 void UIDelegate::UIClient::runModal(WebPageProxy&)
489 {
490     if (!m_uiDelegate.m_delegateMethods.webViewRunModal)
491         return;
492     
493     auto delegate = m_uiDelegate.m_delegate.get();
494     if (!delegate)
495         return;
496
497     [(id <WKUIDelegatePrivate>)delegate _webViewRunModal:m_uiDelegate.m_webView];
498 }
499
500 float UIDelegate::UIClient::headerHeight(WebPageProxy&, WebFrameProxy& webFrameProxy)
501 {
502     if (!m_uiDelegate.m_delegateMethods.webViewHeaderHeight)
503         return 0;
504     
505     auto delegate = m_uiDelegate.m_delegate.get();
506     if (!delegate)
507         return 0;
508     
509     return [(id <WKUIDelegatePrivate>)delegate _webViewHeaderHeight:m_uiDelegate.m_webView];
510 }
511
512 float UIDelegate::UIClient::footerHeight(WebPageProxy&, WebFrameProxy&)
513 {
514     if (!m_uiDelegate.m_delegateMethods.webViewFooterHeight)
515         return 0;
516     
517     auto delegate = m_uiDelegate.m_delegate.get();
518     if (!delegate)
519         return 0;
520     
521     return [(id <WKUIDelegatePrivate>)delegate _webViewFooterHeight:m_uiDelegate.m_webView];
522 }
523
524 void UIDelegate::UIClient::drawHeader(WebPageProxy&, WebFrameProxy& frame, WebCore::FloatRect&& rect)
525 {
526     if (!m_uiDelegate.m_delegateMethods.webViewDrawHeaderInRectForPageWithTitleURL)
527         return;
528     
529     auto delegate = m_uiDelegate.m_delegate.get();
530     if (!delegate)
531         return;
532     
533     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView drawHeaderInRect:rect forPageWithTitle:frame.title() URL:frame.url()];
534 }
535
536 void UIDelegate::UIClient::drawFooter(WebPageProxy&, WebFrameProxy& frame, WebCore::FloatRect&& rect)
537 {
538     if (!m_uiDelegate.m_delegateMethods.webViewDrawFooterInRectForPageWithTitleURL)
539         return;
540     
541     auto delegate = m_uiDelegate.m_delegate.get();
542     if (!delegate)
543         return;
544     
545     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView drawFooterInRect:rect forPageWithTitle:frame.title() URL:frame.url()];
546 }
547
548 void UIDelegate::UIClient::pageDidScroll(WebPageProxy*)
549 {
550     if (!m_uiDelegate.m_delegateMethods.webViewDidScroll)
551         return;
552     
553     auto delegate = m_uiDelegate.m_delegate.get();
554     if (!delegate)
555         return;
556     
557     [(id <WKUIDelegatePrivate>)delegate _webViewDidScroll:m_uiDelegate.m_webView];
558 }
559
560 void UIDelegate::UIClient::focus(WebPageProxy*)
561 {
562     if (!m_uiDelegate.m_delegateMethods.focusWebView)
563         return;
564     
565     auto delegate = m_uiDelegate.m_delegate.get();
566     if (!delegate)
567         return;
568     
569     [(id <WKUIDelegatePrivate>)delegate _focusWebView:m_uiDelegate.m_webView];
570 }
571
572 void UIDelegate::UIClient::unfocus(WebPageProxy*)
573 {
574     if (!m_uiDelegate.m_delegateMethods.unfocusWebView)
575         return;
576     
577     auto delegate = m_uiDelegate.m_delegate.get();
578     if (!delegate)
579         return;
580     
581     [(id <WKUIDelegatePrivate>)delegate _unfocusWebView:m_uiDelegate.m_webView];
582 }
583
584 static _WKPlugInUnavailabilityReason toWKPlugInUnavailabilityReason(WKPluginUnavailabilityReason reason)
585 {
586     switch (reason) {
587     case kWKPluginUnavailabilityReasonPluginMissing:
588         return _WKPlugInUnavailabilityReasonPluginMissing;
589     case kWKPluginUnavailabilityReasonPluginCrashed:
590         return _WKPlugInUnavailabilityReasonPluginCrashed;
591     case kWKPluginUnavailabilityReasonInsecurePluginVersion:
592         return _WKPlugInUnavailabilityReasonInsecurePluginVersion;
593     }
594     ASSERT_NOT_REACHED();
595     return _WKPlugInUnavailabilityReasonPluginMissing;
596 }
597     
598 void UIDelegate::UIClient::unavailablePluginButtonClicked(WebPageProxy&, WKPluginUnavailabilityReason reason, API::Dictionary& plugInInfo)
599 {
600     if (!m_uiDelegate.m_delegateMethods.webViewUnavailablePlugInButtonClicked)
601         return;
602     
603     auto delegate = m_uiDelegate.m_delegate.get();
604     if (!delegate)
605         return;
606
607     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView unavailablePlugInButtonClickedWithReason:toWKPlugInUnavailabilityReason(reason) plugInInfo:wrapper(plugInInfo)];
608 }
609     
610 static _WKResourceLimit toWKResourceLimit(WKResourceLimit limit)
611 {
612     switch (limit) {
613     case kWKResourceLimitMemory:
614         return _WKResourceLimitMemory;
615     case kWKResourceLimitCPU:
616         return _WKResourceLimitCPU;
617     }
618     ASSERT_NOT_REACHED();
619     return _WKResourceLimitMemory;
620 }
621
622 void UIDelegate::UIClient::didExceedBackgroundResourceLimitWhileInForeground(WebPageProxy&, WKResourceLimit limit)
623 {
624     if (!m_uiDelegate.m_delegateMethods.webViewDidExceedBackgroundResourceLimitWhileInForeground)
625         return;
626     
627     auto delegate = m_uiDelegate.m_delegate.get();
628     if (!delegate)
629         return;
630     
631     [static_cast<id <WKUIDelegatePrivate>>(delegate) _webView:m_uiDelegate.m_webView didExceedBackgroundResourceLimitWhileInForeground:toWKResourceLimit(limit)];
632 }
633
634 void UIDelegate::UIClient::didNotHandleWheelEvent(WebPageProxy*, const NativeWebWheelEvent& event)
635 {
636     if (!m_uiDelegate.m_delegateMethods.webViewDidNotHandleWheelEvent)
637         return;
638     
639     auto delegate = m_uiDelegate.m_delegate.get();
640     if (!delegate)
641         return;
642     
643     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView didNotHandleWheelEvent:event.nativeEvent()];
644 }
645
646 void UIDelegate::UIClient::setIsResizable(WebKit::WebPageProxy&, bool resizable)
647 {
648     if (!m_uiDelegate.m_delegateMethods.webViewSetResizable)
649         return;
650     
651     auto delegate = m_uiDelegate.m_delegate.get();
652     if (!delegate)
653         return;
654     
655     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView setResizable:resizable];
656 }
657
658 void UIDelegate::UIClient::setWindowFrame(WebKit::WebPageProxy&, const WebCore::FloatRect& frame)
659 {
660     if (!m_uiDelegate.m_delegateMethods.webViewSetWindowFrame)
661         return;
662     
663     auto delegate = m_uiDelegate.m_delegate.get();
664     if (!delegate)
665         return;
666     
667     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView setWindowFrame:frame];
668 }
669
670 void UIDelegate::UIClient::windowFrame(WebKit::WebPageProxy&, Function<void(WebCore::FloatRect)>&& completionHandler)
671 {
672     if (!m_uiDelegate.m_delegateMethods.webViewGetWindowFrameWithCompletionHandler)
673         return completionHandler({ });
674     
675     auto delegate = m_uiDelegate.m_delegate.get();
676     if (!delegate)
677         return completionHandler({ });
678     
679     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView getWindowFrameWithCompletionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:getWindowFrameWithCompletionHandler:))](CGRect frame) {
680         if (checker->completionHandlerHasBeenCalled())
681             return;
682         checker->didCallCompletionHandler();
683         completionHandler(frame);
684     }).get()];
685 }
686
687 void UIDelegate::UIClient::mouseDidMoveOverElement(WebPageProxy&, const WebHitTestResultData& data, OptionSet<WebEvent::Modifier> modifiers, API::Object* userInfo)
688 {
689     if (!m_uiDelegate.m_delegateMethods.webViewMouseDidMoveOverElementWithFlagsUserInfo)
690         return;
691
692     auto delegate = m_uiDelegate.m_delegate.get();
693     if (!delegate)
694         return;
695
696     auto apiHitTestResult = API::HitTestResult::create(data);
697     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView mouseDidMoveOverElement:wrapper(apiHitTestResult.get()) withFlags:WebEventFactory::toNSEventModifierFlags(modifiers) userInfo:userInfo ? static_cast<id <NSSecureCoding>>(userInfo->wrapper()) : nil];
698 }
699
700 static _WKAutoplayEventFlags toWKAutoplayEventFlags(OptionSet<WebCore::AutoplayEventFlags> flags)
701 {
702     _WKAutoplayEventFlags wkFlags = _WKAutoplayEventFlagsNone;
703     if (flags.contains(WebCore::AutoplayEventFlags::HasAudio))
704         wkFlags |= _WKAutoplayEventFlagsHasAudio;
705     if (flags.contains(WebCore::AutoplayEventFlags::PlaybackWasPrevented))
706         wkFlags |= _WKAutoplayEventFlagsPlaybackWasPrevented;
707     if (flags.contains(WebCore::AutoplayEventFlags::MediaIsMainContent))
708         wkFlags |= _WKAutoplayEventFlagsMediaIsMainContent;
709     
710     return wkFlags;
711 }
712
713 static _WKAutoplayEvent toWKAutoplayEvent(WebCore::AutoplayEvent event)
714 {
715     switch (event) {
716     case WebCore::AutoplayEvent::DidPreventMediaFromPlaying:
717         return _WKAutoplayEventDidPreventFromAutoplaying;
718     case WebCore::AutoplayEvent::DidPlayMediaWithUserGesture:
719         return _WKAutoplayEventDidPlayMediaWithUserGesture;
720     case WebCore::AutoplayEvent::DidAutoplayMediaPastThresholdWithoutUserInterference:
721         return _WKAutoplayEventDidAutoplayMediaPastThresholdWithoutUserInterference;
722     case WebCore::AutoplayEvent::UserDidInterfereWithPlayback:
723         return _WKAutoplayEventUserDidInterfereWithPlayback;
724     }
725     ASSERT_NOT_REACHED();
726     return _WKAutoplayEventDidPlayMediaWithUserGesture;
727 }
728
729 void UIDelegate::UIClient::toolbarsAreVisible(WebPageProxy&, Function<void(bool)>&& completionHandler)
730 {
731     if (!m_uiDelegate.m_delegateMethods.webViewGetToolbarsAreVisibleWithCompletionHandler)
732         return completionHandler(true);
733     
734     auto delegate = m_uiDelegate.m_delegate.get();
735     if (!delegate)
736         return completionHandler(true);
737     
738     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView getToolbarsAreVisibleWithCompletionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:getToolbarsAreVisibleWithCompletionHandler:))](BOOL visible) {
739         if (checker->completionHandlerHasBeenCalled())
740             return;
741         checker->didCallCompletionHandler();
742         completionHandler(visible);
743     }).get()];
744 }
745
746 void UIDelegate::UIClient::didClickAutoFillButton(WebPageProxy&, API::Object* userInfo)
747 {
748     if (!m_uiDelegate.m_delegateMethods.webViewDidClickAutoFillButtonWithUserInfo)
749         return;
750     
751     auto delegate = m_uiDelegate.m_delegate.get();
752     if (!delegate)
753         return;
754     
755     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView didClickAutoFillButtonWithUserInfo:userInfo ? static_cast<id <NSSecureCoding>>(userInfo->wrapper()) : nil];
756 }
757
758 void UIDelegate::UIClient::handleAutoplayEvent(WebPageProxy&, WebCore::AutoplayEvent event, OptionSet<WebCore::AutoplayEventFlags> flags)
759 {
760     if (!m_uiDelegate.m_delegateMethods.webViewHandleAutoplayEventWithFlags)
761         return;
762     
763     auto delegate = m_uiDelegate.m_delegate.get();
764     if (!delegate)
765         return;
766     
767     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView handleAutoplayEvent:toWKAutoplayEvent(event) withFlags:toWKAutoplayEventFlags(flags)];
768 }
769
770 void UIDelegate::UIClient::showPage(WebPageProxy*)
771 {
772     if (!m_uiDelegate.m_delegateMethods.showWebView)
773         return;
774
775     auto delegate = m_uiDelegate.m_delegate.get();
776     if (!delegate)
777         return;
778     
779     [(id <WKUIDelegatePrivate>)delegate _showWebView:m_uiDelegate.m_webView];
780 }
781     
782 void UIDelegate::UIClient::saveDataToFileInDownloadsFolder(WebPageProxy*, const WTF::String& suggestedFilename, const WTF::String& mimeType, const URL& originatingURL, API::Data& data)
783 {
784     if (!m_uiDelegate.m_delegateMethods.webViewSaveDataToFileSuggestedFilenameMimeTypeOriginatingURL)
785         return;
786     
787     auto delegate = m_uiDelegate.m_delegate.get();
788     if (!delegate)
789         return;
790
791     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView saveDataToFile:wrapper(data) suggestedFilename:suggestedFilename mimeType:mimeType originatingURL:originatingURL];
792 }
793
794 void UIDelegate::UIClient::decidePolicyForNotificationPermissionRequest(WebKit::WebPageProxy&, API::SecurityOrigin& securityOrigin, WTF::Function<void(bool)>&& completionHandler)
795 {
796     if (!m_uiDelegate.m_delegateMethods.webViewRequestNotificationPermissionForSecurityOriginDecisionHandler)
797         return completionHandler(false);
798     
799     auto delegate = m_uiDelegate.m_delegate.get();
800     if (!delegate)
801         return completionHandler(false);
802
803     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:requestNotificationPermissionForSecurityOrigin:decisionHandler:));
804     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView requestNotificationPermissionForSecurityOrigin:wrapper(securityOrigin) decisionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)] (BOOL result) mutable {
805         if (checker->completionHandlerHasBeenCalled())
806             return;
807         checker->didCallCompletionHandler();
808         completionHandler(result);
809     }).get()];
810 }
811
812 bool UIDelegate::UIClient::runOpenPanel(WebPageProxy*, WebFrameProxy* webFrameProxy, const WebCore::SecurityOriginData& securityOriginData, API::OpenPanelParameters* openPanelParameters, WebOpenPanelResultListenerProxy* listener)
813 {
814     if (!m_uiDelegate.m_delegateMethods.webViewRunOpenPanelWithParametersInitiatedByFrameCompletionHandler)
815         return false;
816
817     auto delegate = m_uiDelegate.m_delegate.get();
818     if (!delegate)
819         return false;
820
821     auto frame = API::FrameInfo::create(*webFrameProxy, securityOriginData.securityOrigin());
822
823     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(webView:runOpenPanelWithParameters:initiatedByFrame:completionHandler:));
824
825     [delegate webView:m_uiDelegate.m_webView runOpenPanelWithParameters:wrapper(*openPanelParameters) initiatedByFrame:wrapper(frame) completionHandler:makeBlockPtr([checker = WTFMove(checker), listener = WTFMove(listener)] (NSArray *URLs) mutable {
826         if (checker->completionHandlerHasBeenCalled())
827             return;
828         checker->didCallCompletionHandler();
829
830         if (!URLs) {
831             listener->cancel();
832             return;
833         }
834
835         Vector<String> filenames;
836         for (NSURL *url in URLs)
837             filenames.append(url.path);
838
839         listener->chooseFiles(filenames);
840     }).get()];
841
842     return true;
843 }
844 #endif
845
846 #if ENABLE(DEVICE_ORIENTATION)
847 void UIDelegate::UIClient::shouldAllowDeviceOrientationAndMotionAccess(WebKit::WebPageProxy&, WebFrameProxy& webFrameProxy, const WebCore::SecurityOriginData& securityOriginData, CompletionHandler<void(bool)>&& completionHandler)
848 {
849     if (!m_uiDelegate.m_delegateMethods.webViewShouldAllowDeviceOrientationAndMotionAccessRequestedByFrameDecisionHandler)
850         return completionHandler(true);
851
852     auto delegate = m_uiDelegate.m_delegate.get();
853     if (!delegate)
854         return completionHandler(true);
855
856     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:shouldAllowDeviceOrientationAndMotionAccessRequestedByFrame:decisionHandler:));
857     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView shouldAllowDeviceOrientationAndMotionAccessRequestedByFrame:wrapper(API::FrameInfo::create(webFrameProxy, securityOriginData.securityOrigin())) decisionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)] (BOOL granted) mutable {
858         if (checker->completionHandlerHasBeenCalled())
859             return;
860         checker->didCallCompletionHandler();
861         completionHandler(granted);
862     }).get()];
863 }
864 #endif
865
866 void UIDelegate::UIClient::didChangeFontAttributes(const WebCore::FontAttributes& fontAttributes)
867 {
868     if (!needsFontAttributes())
869         return;
870
871     auto privateUIDelegate = (id <WKUIDelegatePrivate>)m_uiDelegate.m_delegate.get();
872     [privateUIDelegate _webView:m_uiDelegate.m_webView didChangeFontAttributes:fontAttributes.createDictionary().get()];
873 }
874
875 #if ENABLE(MEDIA_STREAM)
876 static void requestUserMediaAuthorizationForFrame(const WebFrameProxy& frame, API::SecurityOrigin& topLevelOrigin, UserMediaPermissionRequestProxy& request, id <WKUIDelegatePrivate> delegate, WKWebView& webView)
877 {
878     auto checker = CompletionHandlerCallChecker::create(delegate, @selector(_webView:requestMediaCaptureAuthorization:decisionHandler:));
879     auto decisionHandler = makeBlockPtr([protectedRequest = makeRef(request), checker = WTFMove(checker)](BOOL authorized) {
880         if (checker->completionHandlerHasBeenCalled())
881             return;
882         checker->didCallCompletionHandler();
883
884         if (!authorized) {
885             protectedRequest->deny(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied);
886             return;
887         }
888         const String& videoDeviceUID = (protectedRequest->requiresVideoCapture() || protectedRequest->requiresDisplayCapture()) ? protectedRequest->videoDeviceUIDs().first() : String();
889         const String& audioDeviceUID = protectedRequest->requiresAudioCapture() ? protectedRequest->audioDeviceUIDs().first() : String();
890         protectedRequest->allow(audioDeviceUID, videoDeviceUID);
891     });
892
893     _WKCaptureDevices devices = 0;
894     if (request.requiresAudioCapture())
895         devices |= _WKCaptureDeviceMicrophone;
896     if (request.requiresVideoCapture())
897         devices |= _WKCaptureDeviceCamera;
898     if (request.requiresDisplayCapture()) {
899         devices |= _WKCaptureDeviceDisplay;
900         ASSERT(!(devices & _WKCaptureDeviceCamera));
901     }
902
903     const auto* mainFrame = frame.page()->mainFrame();
904
905     // FIXME: Provide a specific delegate for display capture.
906     if (!request.requiresDisplayCapture() && [delegate respondsToSelector:@selector(_webView:requestMediaCaptureAuthorization:decisionHandler:)]) {
907
908         [delegate _webView:&webView requestMediaCaptureAuthorization:devices decisionHandler:decisionHandler.get()];
909         return;
910     }
911
912     URL requestFrameURL { URL(), frame.url() };
913     URL mainFrameURL { URL(), mainFrame->url() };
914
915     [delegate _webView:&webView requestUserMediaAuthorizationForDevices:devices url:requestFrameURL mainFrameURL:mainFrameURL decisionHandler:decisionHandler.get()];
916 }
917 #endif
918
919 void UIDelegate::UIClient::decidePolicyForUserMediaPermissionRequest(WebPageProxy& page, WebFrameProxy& frame, API::SecurityOrigin& userMediaOrigin, API::SecurityOrigin& topLevelOrigin, UserMediaPermissionRequestProxy& request)
920 {
921 #if ENABLE(MEDIA_STREAM)
922     auto delegate = m_uiDelegate.m_delegate.get();
923     if (!delegate || !m_uiDelegate.m_delegateMethods.webViewRequestMediaCaptureAuthorizationForFrameDecisionHandler) {
924         request.deny(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::UserMediaDisabled);
925         return;
926     }
927
928     bool requiresAudioCapture = request.requiresAudioCapture();
929     bool requiresVideoCapture = request.requiresVideoCapture();
930     bool requiresDisplayCapture = request.requiresDisplayCapture();
931     if (!requiresAudioCapture && !requiresVideoCapture && !requiresDisplayCapture) {
932         request.deny(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoConstraints);
933         return;
934     }
935
936 #if HAVE(AUTHORIZATION_STATUS_FOR_MEDIA_TYPE)
937     bool usingMockCaptureDevices = page.preferences().mockCaptureDevicesEnabled();
938     auto requestCameraAuthorization = makeBlockPtr([this, &frame, protectedRequest = makeRef(request), webView = RetainPtr<WKWebView>(m_uiDelegate.m_webView), topLevelOrigin = makeRef(topLevelOrigin), usingMockCaptureDevices]() mutable {
939
940         if (!protectedRequest->requiresVideoCapture()) {
941             requestUserMediaAuthorizationForFrame(frame, topLevelOrigin, protectedRequest, (id <WKUIDelegatePrivate>)m_uiDelegate.m_delegate.get(), *webView.get());
942             return;
943         }
944         AVAuthorizationStatus cameraAuthorizationStatus = usingMockCaptureDevices ? AVAuthorizationStatusAuthorized : [getAVCaptureDeviceClass() authorizationStatusForMediaType:getAVMediaTypeVideo()];
945         switch (cameraAuthorizationStatus) {
946         case AVAuthorizationStatusAuthorized:
947             requestUserMediaAuthorizationForFrame(frame, topLevelOrigin, protectedRequest, (id <WKUIDelegatePrivate>)m_uiDelegate.m_delegate.get(), *webView.get());
948             break;
949         case AVAuthorizationStatusDenied:
950         case AVAuthorizationStatusRestricted:
951             protectedRequest->deny(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied);
952             return;
953         case AVAuthorizationStatusNotDetermined:
954             auto decisionHandler = makeBlockPtr([this, &frame, protectedRequest = makeRef(protectedRequest.get()), webView = RetainPtr<WKWebView>(m_uiDelegate.m_webView), topLevelOrigin = WTFMove(topLevelOrigin)](BOOL authorized) {
955                 if (!authorized) {
956                     protectedRequest->deny(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied);
957                     return;
958                 }
959                 requestUserMediaAuthorizationForFrame(frame, topLevelOrigin, protectedRequest, (id <WKUIDelegatePrivate>)m_uiDelegate.m_delegate.get(), *webView.get());
960             });
961
962             [getAVCaptureDeviceClass() requestAccessForMediaType:getAVMediaTypeVideo() completionHandler:decisionHandler.get()];
963             break;
964         }
965     });
966
967     if (requiresAudioCapture) {
968         AVAuthorizationStatus microphoneAuthorizationStatus = usingMockCaptureDevices ? AVAuthorizationStatusAuthorized : [getAVCaptureDeviceClass() authorizationStatusForMediaType:getAVMediaTypeAudio()];
969         switch (microphoneAuthorizationStatus) {
970         case AVAuthorizationStatusAuthorized:
971             requestCameraAuthorization();
972             break;
973         case AVAuthorizationStatusDenied:
974         case AVAuthorizationStatusRestricted:
975             request.deny(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied);
976             return;
977         case AVAuthorizationStatusNotDetermined:
978             auto decisionHandler = makeBlockPtr([protectedRequest = makeRef(request), requestCameraAuthorization](BOOL authorized) {
979                 if (!authorized) {
980                     protectedRequest->deny(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied);
981                     return;
982                 }
983                 requestCameraAuthorization();
984             });
985
986             [getAVCaptureDeviceClass() requestAccessForMediaType:getAVMediaTypeAudio() completionHandler:decisionHandler.get()];
987             break;
988         }
989     } else
990         requestCameraAuthorization();
991 #else
992     requestUserMediaAuthorizationForFrame(frame, topLevelOrigin, request, (id <WKUIDelegatePrivate>)m_uiDelegate.m_delegate.get(), *m_uiDelegate.m_webView);
993 #endif
994 #endif
995 }
996
997 void UIDelegate::UIClient::checkUserMediaPermissionForOrigin(WebPageProxy& page, WebFrameProxy& frame, API::SecurityOrigin& userMediaOrigin, API::SecurityOrigin& topLevelOrigin, UserMediaPermissionCheckProxy& request)
998 {
999     auto delegate = m_uiDelegate.m_delegate.get();
1000     if (!delegate || !m_uiDelegate.m_delegateMethods.webViewIsMediaCaptureAuthorizedForFrameDecisionHandler) {
1001         request.setUserMediaAccessInfo(false);
1002         return;
1003     }
1004
1005     const auto* mainFrame = frame.page()->mainFrame();
1006
1007     if ([delegate respondsToSelector:@selector(_webView:includeSensitiveMediaDeviceDetails:)]) {
1008         auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:includeSensitiveMediaDeviceDetails:));
1009         auto decisionHandler = makeBlockPtr([protectedRequest = makeRef(request), checker = WTFMove(checker)](BOOL includeSensitiveDetails) {
1010             if (checker->completionHandlerHasBeenCalled())
1011                 return;
1012             checker->didCallCompletionHandler();
1013
1014             protectedRequest->setUserMediaAccessInfo(includeSensitiveDetails);
1015         });
1016
1017         [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView includeSensitiveMediaDeviceDetails:decisionHandler.get()];
1018         return;
1019     }
1020
1021     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:checkUserMediaPermissionForURL:mainFrameURL:frameIdentifier:decisionHandler:));
1022     auto decisionHandler = makeBlockPtr([protectedRequest = makeRef(request), checker = WTFMove(checker)](NSString*, BOOL authorized) {
1023         if (checker->completionHandlerHasBeenCalled())
1024             return;
1025         checker->didCallCompletionHandler();
1026
1027         protectedRequest->setUserMediaAccessInfo(authorized);
1028     });
1029
1030     URL requestFrameURL { URL(), frame.url() };
1031     URL mainFrameURL { URL(), mainFrame->url() };
1032
1033     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView checkUserMediaPermissionForURL:requestFrameURL mainFrameURL:mainFrameURL frameIdentifier:frame.frameID() decisionHandler:decisionHandler.get()];
1034 }
1035
1036 void UIDelegate::UIClient::mediaCaptureStateDidChange(WebCore::MediaProducer::MediaStateFlags state)
1037 {
1038     WKWebView *webView = m_uiDelegate.m_webView;
1039
1040     [webView didChangeValueForKey:@"mediaCaptureState"];
1041
1042     auto delegate = m_uiDelegate.m_delegate.get();
1043     if (!delegate || !m_uiDelegate.m_delegateMethods.webViewMediaCaptureStateDidChange)
1044         return;
1045
1046     [(id <WKUIDelegatePrivate>)delegate _webView:webView mediaCaptureStateDidChange:toWKMediaCaptureState(state)];
1047 }
1048
1049 void UIDelegate::UIClient::reachedApplicationCacheOriginQuota(WebPageProxy*, const WebCore::SecurityOrigin& securityOrigin, uint64_t currentQuota, uint64_t totalBytesNeeded, Function<void (unsigned long long)>&& completionHandler)
1050 {
1051     if (!m_uiDelegate.m_delegateMethods.webViewDecideWebApplicationCacheQuotaForSecurityOriginCurrentQuotaTotalBytesNeeded) {
1052         completionHandler(currentQuota);
1053         return;
1054     }
1055
1056     auto delegate = m_uiDelegate.m_delegate.get();
1057     if (!delegate) {
1058         completionHandler(currentQuota);
1059         return;
1060     }
1061
1062     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:decideWebApplicationCacheQuotaForSecurityOrigin:currentQuota:totalBytesNeeded:decisionHandler:));
1063     auto apiOrigin = API::SecurityOrigin::create(securityOrigin);
1064     
1065     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView decideWebApplicationCacheQuotaForSecurityOrigin:wrapper(apiOrigin.get()) currentQuota:currentQuota totalBytesNeeded:totalBytesNeeded decisionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)](unsigned long long newQuota) {
1066         if (checker->completionHandlerHasBeenCalled())
1067             return;
1068         checker->didCallCompletionHandler();
1069         completionHandler(newQuota);
1070     }).get()];
1071 }
1072
1073 void UIDelegate::UIClient::printFrame(WebPageProxy&, WebFrameProxy& webFrameProxy)
1074 {
1075     if (!m_uiDelegate.m_delegateMethods.webViewPrintFrame)
1076         return;
1077
1078     auto delegate = m_uiDelegate.m_delegate.get();
1079     if (!delegate)
1080         return;
1081
1082     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView printFrame:wrapper(API::FrameHandle::create(webFrameProxy.frameID()))];
1083 }
1084
1085 void UIDelegate::UIClient::close(WebPageProxy*)
1086 {
1087     if (m_uiDelegate.m_delegateMethods.webViewClose) {
1088         auto delegate = m_uiDelegate.m_delegate.get();
1089         if (!delegate)
1090             return;
1091
1092         [(id <WKUIDelegatePrivate>)delegate _webViewClose:m_uiDelegate.m_webView];
1093         return;
1094     }
1095
1096     if (!m_uiDelegate.m_delegateMethods.webViewDidClose)
1097         return;
1098
1099     auto delegate = m_uiDelegate.m_delegate.get();
1100     if (!delegate)
1101         return;
1102
1103     [delegate webViewDidClose:m_uiDelegate.m_webView];
1104 }
1105
1106 void UIDelegate::UIClient::fullscreenMayReturnToInline(WebPageProxy*)
1107 {
1108     if (!m_uiDelegate.m_delegateMethods.webViewFullscreenMayReturnToInline)
1109         return;
1110     
1111     auto delegate = m_uiDelegate.m_delegate.get();
1112     if (!delegate)
1113         return;
1114     
1115     [(id <WKUIDelegatePrivate>)delegate _webViewFullscreenMayReturnToInline:m_uiDelegate.m_webView];
1116 }
1117
1118 void UIDelegate::UIClient::didEnterFullscreen(WebPageProxy*)
1119 {
1120     if (!m_uiDelegate.m_delegateMethods.webViewDidEnterFullscreen)
1121         return;
1122
1123     auto delegate = m_uiDelegate.m_delegate.get();
1124     if (!delegate)
1125         return;
1126
1127     [(id <WKUIDelegatePrivate>)delegate _webViewDidEnterFullscreen:m_uiDelegate.m_webView];
1128 }
1129
1130 void UIDelegate::UIClient::didExitFullscreen(WebPageProxy*)
1131 {
1132     if (!m_uiDelegate.m_delegateMethods.webViewDidExitFullscreen)
1133         return;
1134
1135     auto delegate = m_uiDelegate.m_delegate.get();
1136     if (!delegate)
1137         return;
1138
1139     [(id <WKUIDelegatePrivate>)delegate _webViewDidExitFullscreen:m_uiDelegate.m_webView];
1140 }
1141     
1142 #if PLATFORM(IOS_FAMILY)
1143 #if HAVE(APP_LINKS)
1144 bool UIDelegate::UIClient::shouldIncludeAppLinkActionsForElement(_WKActivatedElementInfo *elementInfo)
1145 {
1146     if (!m_uiDelegate.m_delegateMethods.webViewShouldIncludeAppLinkActionsForElement)
1147         return true;
1148
1149     auto delegate = m_uiDelegate.m_delegate.get();
1150     if (!delegate)
1151         return true;
1152
1153     return [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView shouldIncludeAppLinkActionsForElement:elementInfo];
1154 }
1155 #endif
1156
1157 RetainPtr<NSArray> UIDelegate::UIClient::actionsForElement(_WKActivatedElementInfo *elementInfo, RetainPtr<NSArray> defaultActions)
1158 {
1159     if (!m_uiDelegate.m_delegateMethods.webViewActionsForElementDefaultActions)
1160         return defaultActions;
1161
1162     auto delegate = m_uiDelegate.m_delegate.get();
1163     if (!delegate)
1164         return defaultActions;
1165
1166     return [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView actionsForElement:elementInfo defaultActions:defaultActions.get()];
1167 }
1168
1169 void UIDelegate::UIClient::didNotHandleTapAsClick(const WebCore::IntPoint& point)
1170 {
1171     if (!m_uiDelegate.m_delegateMethods.webViewDidNotHandleTapAsClickAtPoint)
1172         return;
1173
1174     auto delegate = m_uiDelegate.m_delegate.get();
1175     if (!delegate)
1176         return;
1177
1178     [static_cast<id <WKUIDelegatePrivate>>(delegate) _webView:m_uiDelegate.m_webView didNotHandleTapAsClickAtPoint:point];
1179 }
1180
1181 UIViewController *UIDelegate::UIClient::presentingViewController()
1182 {
1183     if (!m_uiDelegate.m_delegateMethods.presentingViewControllerForWebView)
1184         return nullptr;
1185
1186     auto delegate = m_uiDelegate.m_delegate.get();
1187     if (!delegate)
1188         return nullptr;
1189
1190     return [static_cast<id <WKUIDelegatePrivate>>(delegate) _presentingViewControllerForWebView:m_uiDelegate.m_webView];
1191 }
1192
1193 #endif
1194
1195 NSDictionary *UIDelegate::UIClient::dataDetectionContext()
1196 {
1197     if (!m_uiDelegate.m_delegateMethods.dataDetectionContextForWebView)
1198         return nullptr;
1199
1200     auto delegate = m_uiDelegate.m_delegate.get();
1201     if (!delegate)
1202         return nullptr;
1203
1204     return [static_cast<id <WKUIDelegatePrivate>>(delegate) _dataDetectionContextForWebView:m_uiDelegate.m_webView];
1205 }
1206
1207 #if ENABLE(POINTER_LOCK)
1208
1209 void UIDelegate::UIClient::requestPointerLock(WebPageProxy* page)
1210 {
1211     if (!m_uiDelegate.m_delegateMethods.webViewRequestPointerLock && !m_uiDelegate.m_delegateMethods.webViewDidRequestPointerLockCompletionHandler)
1212         return;
1213
1214     auto delegate = m_uiDelegate.m_delegate.get();
1215     if (!delegate)
1216         return;
1217
1218     if (m_uiDelegate.m_delegateMethods.webViewRequestPointerLock) {
1219         [static_cast<id <WKUIDelegatePrivate>>(delegate) _webViewRequestPointerLock:m_uiDelegate.m_webView];
1220         return;
1221     }
1222
1223     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webViewDidRequestPointerLock:completionHandler:));
1224     [static_cast<id <WKUIDelegatePrivate>>(delegate) _webViewDidRequestPointerLock:m_uiDelegate.m_webView completionHandler:makeBlockPtr([checker = WTFMove(checker), page = makeRefPtr(page)] (BOOL allow) {
1225         if (checker->completionHandlerHasBeenCalled())
1226             return;
1227         checker->didCallCompletionHandler();
1228
1229         if (allow)
1230             page->didAllowPointerLock();
1231         else
1232             page->didDenyPointerLock();
1233     }).get()];
1234 }
1235
1236 void UIDelegate::UIClient::didLosePointerLock(WebPageProxy*)
1237 {
1238     if (!m_uiDelegate.m_delegateMethods.webViewDidLosePointerLock)
1239         return;
1240
1241     auto delegate = m_uiDelegate.m_delegate.get();
1242     if (!delegate)
1243         return;
1244
1245     [static_cast<id <WKUIDelegatePrivate>>(delegate) _webViewDidLosePointerLock:m_uiDelegate.m_webView];
1246 }
1247
1248 #endif
1249     
1250 void UIDelegate::UIClient::didShowSafeBrowsingWarning()
1251 {
1252     if (!m_uiDelegate.m_delegateMethods.webViewDidShowSafeBrowsingWarning)
1253         return;
1254
1255     auto delegate = m_uiDelegate.m_delegate.get();
1256     if (!delegate)
1257         return;
1258
1259     [static_cast<id <WKUIDelegatePrivate>>(delegate) _webViewDidShowSafeBrowsingWarning:m_uiDelegate.m_webView];
1260 }
1261
1262 void UIDelegate::UIClient::hasVideoInPictureInPictureDidChange(WebPageProxy*, bool hasVideoInPictureInPicture)
1263 {
1264     if (!m_uiDelegate.m_delegateMethods.webViewHasVideoInPictureInPictureDidChange)
1265         return;
1266     
1267     auto delegate = m_uiDelegate.m_delegate.get();
1268     if (!delegate)
1269         return;
1270     
1271     [static_cast<id <WKUIDelegatePrivate>>(delegate) _webView:m_uiDelegate.m_webView hasVideoInPictureInPictureDidChange:hasVideoInPictureInPicture];
1272 }
1273
1274 void UIDelegate::UIClient::imageOrMediaDocumentSizeChanged(const WebCore::IntSize& newSize)
1275 {
1276     if (!m_uiDelegate.m_delegateMethods.webViewImageOrMediaDocumentSizeChanged)
1277         return;
1278
1279     auto delegate = m_uiDelegate.m_delegate.get();
1280     if (!delegate)
1281         return;
1282
1283     [static_cast<id <WKUIDelegatePrivate>>(delegate) _webView:m_uiDelegate.m_webView imageOrMediaDocumentSizeChanged:newSize];
1284 }
1285
1286 } // namespace WebKit