57e0adc00da20597938f6fbb4e518e2874d3ea6c
[WebKit-https.git] / Source / WebKit2 / UIProcess / Cocoa / UIDelegate.mm
1 /*
2  * Copyright (C) 2014 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 #if WK_API_ENABLED
30
31 #import "CompletionHandlerCallChecker.h"
32 #import "NavigationActionData.h"
33 #import "SecurityOriginData.h"
34 #import "WKFrameInfoInternal.h"
35 #import "WKNavigationActionInternal.h"
36 #import "WKSecurityOriginInternal.h"
37 #import "WKWebViewConfigurationInternal.h"
38 #import "WKWebViewInternal.h"
39 #import "WKWindowFeaturesInternal.h"
40 #import "WKUIDelegatePrivate.h"
41 #import "_WKFrameHandleInternal.h"
42 #import <WebCore/URL.h>
43
44 namespace WebKit {
45
46 UIDelegate::UIDelegate(WKWebView *webView)
47     : m_webView(webView)
48 {
49 }
50
51 UIDelegate::~UIDelegate()
52 {
53 }
54
55 std::unique_ptr<API::UIClient> UIDelegate::createUIClient()
56 {
57     return std::make_unique<UIClient>(*this);
58 }
59
60 RetainPtr<id <WKUIDelegate> > UIDelegate::delegate()
61 {
62     return m_delegate.get();
63 }
64
65 void UIDelegate::setDelegate(id <WKUIDelegate> delegate)
66 {
67     m_delegate = delegate;
68
69     m_delegateMethods.webViewCreateWebViewWithConfigurationForNavigationActionWindowFeatures = [delegate respondsToSelector:@selector(webView:createWebViewWithConfiguration:forNavigationAction:windowFeatures:)];
70     m_delegateMethods.webViewRunJavaScriptAlertPanelWithMessageInitiatedByFrameCompletionHandler = [delegate respondsToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:)];
71     m_delegateMethods.webViewRunJavaScriptConfirmPanelWithMessageInitiatedByFrameCompletionHandler = [delegate respondsToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:)];
72     m_delegateMethods.webViewRunJavaScriptTextInputPanelWithPromptDefaultTextInitiatedByFrameCompletionHandler = [delegate respondsToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:)];
73     m_delegateMethods.webViewDecideDatabaseQuotaForSecurityOriginCurrentQuotaCurrentOriginUsageCurrentDatabaseUsageExpectedUsageDecisionHandler = [delegate respondsToSelector:@selector(_webView:decideDatabaseQuotaForSecurityOrigin:currentQuota:currentOriginUsage:currentDatabaseUsage:expectedUsage:decisionHandler:)];
74     m_delegateMethods.webViewDecideWebApplicationCacheQuotaForSecurityOriginCurrentQuotaTotalBytesNeeded = [delegate respondsToSelector:@selector(_webView:decideWebApplicationCacheQuotaForSecurityOrigin:currentQuota:totalBytesNeeded:decisionHandler:)];
75     m_delegateMethods.webViewPrintFrame = [delegate respondsToSelector:@selector(_webView:printFrame:)];
76     m_delegateMethods.webViewDidClose = [delegate respondsToSelector:@selector(webViewDidClose:)];
77     m_delegateMethods.webViewClose = [delegate respondsToSelector:@selector(_webViewClose:)];
78     m_delegateMethods.webViewFullscreenMayReturnToInline = [delegate respondsToSelector:@selector(_webViewFullscreenMayReturnToInline:)];
79     m_delegateMethods.webViewDidEnterFullscreen = [delegate respondsToSelector:@selector(_webViewDidEnterFullscreen:)];
80     m_delegateMethods.webViewDidExitFullscreen = [delegate respondsToSelector:@selector(_webViewDidExitFullscreen:)];
81 #if PLATFORM(IOS)
82 #if HAVE(APP_LINKS)
83     m_delegateMethods.webViewShouldIncludeAppLinkActionsForElement = [delegate respondsToSelector:@selector(_webView:shouldIncludeAppLinkActionsForElement:)];
84 #endif
85     m_delegateMethods.webViewActionsForElementDefaultActions = [delegate respondsToSelector:@selector(_webView:actionsForElement:defaultActions:)];
86     m_delegateMethods.webViewDidNotHandleTapAsClickAtPoint = [delegate respondsToSelector:@selector(_webView:didNotHandleTapAsClickAtPoint:)];
87 #endif
88 }
89
90 UIDelegate::UIClient::UIClient(UIDelegate& uiDelegate)
91     : m_uiDelegate(uiDelegate)
92 {
93 }
94
95 UIDelegate::UIClient::~UIClient()
96 {
97 }
98
99 PassRefPtr<WebKit::WebPageProxy> UIDelegate::UIClient::createNewPage(WebKit::WebPageProxy*, WebKit::WebFrameProxy* initiatingFrame, const WebKit::SecurityOriginData& securityOriginData, const WebCore::ResourceRequest& request, const WebCore::WindowFeatures& windowFeatures, const WebKit::NavigationActionData& navigationActionData)
100 {
101     if (!m_uiDelegate.m_delegateMethods.webViewCreateWebViewWithConfigurationForNavigationActionWindowFeatures)
102         return nullptr;
103
104     auto delegate = m_uiDelegate.m_delegate.get();
105     if (!delegate)
106         return nullptr;
107
108     auto configuration = adoptNS([m_uiDelegate.m_webView->_configuration copy]);
109     [configuration _setRelatedWebView:m_uiDelegate.m_webView];
110
111     auto sourceFrameInfo = API::FrameInfo::create(*initiatingFrame, securityOriginData.securityOrigin());
112
113     bool shouldOpenAppLinks = !protocolHostAndPortAreEqual(WebCore::URL(WebCore::ParsedURLString, initiatingFrame->url()), request.url());
114     auto navigationAction = API::NavigationAction::create(navigationActionData, sourceFrameInfo.ptr(), nullptr, request, WebCore::URL(), shouldOpenAppLinks);
115
116     RetainPtr<WKWebView> webView = [delegate.get() webView:m_uiDelegate.m_webView createWebViewWithConfiguration:configuration.get() forNavigationAction:wrapper(navigationAction) windowFeatures:adoptNS([[WKWindowFeatures alloc] _initWithWindowFeatures:windowFeatures]).get()];
117
118     if (!webView)
119         return nullptr;
120
121     if ([webView->_configuration _relatedWebView] != m_uiDelegate.m_webView)
122         [NSException raise:NSInternalInconsistencyException format:@"Returned WKWebView was not created with the given configuration."];
123
124     return webView->_page.get();
125 }
126
127 void UIDelegate::UIClient::runJavaScriptAlert(WebKit::WebPageProxy*, const WTF::String& message, WebKit::WebFrameProxy* webFrameProxy, const WebKit::SecurityOriginData& securityOriginData, std::function<void ()> completionHandler)
128 {
129     if (!m_uiDelegate.m_delegateMethods.webViewRunJavaScriptAlertPanelWithMessageInitiatedByFrameCompletionHandler) {
130         completionHandler();
131         return;
132     }
133
134     auto delegate = m_uiDelegate.m_delegate.get();
135     if (!delegate) {
136         completionHandler();
137         return;
138     }
139
140     RefPtr<CompletionHandlerCallChecker> checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:));
141     [delegate webView:m_uiDelegate.m_webView runJavaScriptAlertPanelWithMessage:message initiatedByFrame:wrapper(API::FrameInfo::create(*webFrameProxy, securityOriginData.securityOrigin())) completionHandler:[completionHandler, checker] {
142         completionHandler();
143         checker->didCallCompletionHandler();
144     }];
145 }
146
147 void UIDelegate::UIClient::runJavaScriptConfirm(WebKit::WebPageProxy*, const WTF::String& message, WebKit::WebFrameProxy* webFrameProxy, const WebKit::SecurityOriginData& securityOriginData, std::function<void (bool)> completionHandler)
148 {
149     if (!m_uiDelegate.m_delegateMethods.webViewRunJavaScriptConfirmPanelWithMessageInitiatedByFrameCompletionHandler) {
150         completionHandler(false);
151         return;
152     }
153
154     auto delegate = m_uiDelegate.m_delegate.get();
155     if (!delegate) {
156         completionHandler(false);
157         return;
158     }
159
160     RefPtr<CompletionHandlerCallChecker> checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:));
161     [delegate webView:m_uiDelegate.m_webView runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:wrapper(API::FrameInfo::create(*webFrameProxy, securityOriginData.securityOrigin())) completionHandler:[completionHandler, checker](BOOL result) {
162         completionHandler(result);
163         checker->didCallCompletionHandler();
164     }];
165 }
166
167 void UIDelegate::UIClient::runJavaScriptPrompt(WebKit::WebPageProxy*, const WTF::String& message, const WTF::String& defaultValue, WebKit::WebFrameProxy* webFrameProxy, const WebKit::SecurityOriginData& securityOriginData, std::function<void (const WTF::String&)> completionHandler)
168 {
169     if (!m_uiDelegate.m_delegateMethods.webViewRunJavaScriptTextInputPanelWithPromptDefaultTextInitiatedByFrameCompletionHandler) {
170         completionHandler(String());
171         return;
172     }
173
174     auto delegate = m_uiDelegate.m_delegate.get();
175     if (!delegate) {
176         completionHandler(String());
177         return;
178     }
179
180     RefPtr<CompletionHandlerCallChecker> checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:));
181     [delegate webView:m_uiDelegate.m_webView runJavaScriptTextInputPanelWithPrompt:message defaultText:defaultValue initiatedByFrame:wrapper(API::FrameInfo::create(*webFrameProxy, securityOriginData.securityOrigin())) completionHandler:[completionHandler, checker](NSString *result) {
182         completionHandler(result);
183         checker->didCallCompletionHandler();
184     }];
185 }
186
187 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, std::function<void (unsigned long long)> completionHandler)
188 {
189     if (!m_uiDelegate.m_delegateMethods.webViewDecideDatabaseQuotaForSecurityOriginCurrentQuotaCurrentOriginUsageCurrentDatabaseUsageExpectedUsageDecisionHandler) {
190         completionHandler(currentQuota);
191         return;
192     }
193
194     auto delegate = m_uiDelegate.m_delegate.get();
195     if (!delegate) {
196         completionHandler(currentQuota);
197         return;
198     }
199
200     ASSERT(securityOrigin);
201     RefPtr<CompletionHandlerCallChecker> checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:decideDatabaseQuotaForSecurityOrigin:currentQuota:currentOriginUsage:currentDatabaseUsage:expectedUsage:decisionHandler:));
202     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView decideDatabaseQuotaForSecurityOrigin:wrapper(*securityOrigin) currentQuota:currentQuota currentOriginUsage:currentOriginUsage currentDatabaseUsage:currentUsage expectedUsage:expectedUsage decisionHandler:[completionHandler, checker](unsigned long long newQuota) {
203         checker->didCallCompletionHandler();
204         completionHandler(newQuota);
205     }];
206 }
207
208 void UIDelegate::UIClient::reachedApplicationCacheOriginQuota(WebPageProxy*, const WebCore::SecurityOrigin& securityOrigin, uint64_t currentQuota, uint64_t totalBytesNeeded, std::function<void (unsigned long long)> completionHandler)
209 {
210     if (!m_uiDelegate.m_delegateMethods.webViewDecideWebApplicationCacheQuotaForSecurityOriginCurrentQuotaTotalBytesNeeded) {
211         completionHandler(currentQuota);
212         return;
213     }
214
215     auto delegate = m_uiDelegate.m_delegate.get();
216     if (!delegate) {
217         completionHandler(currentQuota);
218         return;
219     }
220
221     RefPtr<CompletionHandlerCallChecker> checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:decideWebApplicationCacheQuotaForSecurityOrigin:currentQuota:totalBytesNeeded:decisionHandler:));
222     RefPtr<API::SecurityOrigin> apiOrigin = API::SecurityOrigin::create(securityOrigin);
223     
224     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView decideWebApplicationCacheQuotaForSecurityOrigin:wrapper(*apiOrigin) currentQuota:currentQuota totalBytesNeeded:totalBytesNeeded decisionHandler:[completionHandler, checker](unsigned long long newQuota) {
225         checker->didCallCompletionHandler();
226         completionHandler(newQuota);
227     }];
228 }
229
230 void UIDelegate::UIClient::printFrame(WebKit::WebPageProxy*, WebKit::WebFrameProxy* webFrameProxy)
231 {
232     ASSERT_ARG(webFrameProxy, webFrameProxy);
233
234     if (!m_uiDelegate.m_delegateMethods.webViewPrintFrame)
235         return;
236
237     auto delegate = m_uiDelegate.m_delegate.get();
238     if (!delegate)
239         return;
240
241     [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView printFrame:wrapper(API::FrameHandle::create(webFrameProxy->frameID()))];
242 }
243
244 void UIDelegate::UIClient::close(WebKit::WebPageProxy*)
245 {
246     if (m_uiDelegate.m_delegateMethods.webViewClose) {
247         auto delegate = m_uiDelegate.m_delegate.get();
248         if (!delegate)
249             return;
250
251         [(id <WKUIDelegatePrivate>)delegate _webViewClose:m_uiDelegate.m_webView];
252         return;
253     }
254
255     if (!m_uiDelegate.m_delegateMethods.webViewDidClose)
256         return;
257
258     auto delegate = m_uiDelegate.m_delegate.get();
259     if (!delegate)
260         return;
261
262     [delegate webViewDidClose:m_uiDelegate.m_webView];
263 }
264
265 void UIDelegate::UIClient::fullscreenMayReturnToInline(WebKit::WebPageProxy*)
266 {
267     if (!m_uiDelegate.m_delegateMethods.webViewFullscreenMayReturnToInline)
268         return;
269     
270     auto delegate = m_uiDelegate.m_delegate.get();
271     if (!delegate)
272         return;
273     
274     [(id <WKUIDelegatePrivate>)delegate _webViewFullscreenMayReturnToInline:m_uiDelegate.m_webView];
275 }
276
277 void UIDelegate::UIClient::didEnterFullscreen(WebKit::WebPageProxy*)
278 {
279     if (!m_uiDelegate.m_delegateMethods.webViewDidEnterFullscreen)
280         return;
281
282     auto delegate = m_uiDelegate.m_delegate.get();
283     if (!delegate)
284         return;
285
286     [(id <WKUIDelegatePrivate>)delegate _webViewDidEnterFullscreen:m_uiDelegate.m_webView];
287 }
288
289 void UIDelegate::UIClient::didExitFullscreen(WebKit::WebPageProxy*)
290 {
291     if (!m_uiDelegate.m_delegateMethods.webViewDidExitFullscreen)
292         return;
293
294     auto delegate = m_uiDelegate.m_delegate.get();
295     if (!delegate)
296         return;
297
298     [(id <WKUIDelegatePrivate>)delegate _webViewDidExitFullscreen:m_uiDelegate.m_webView];
299 }
300     
301 #if PLATFORM(IOS)
302 #if HAVE(APP_LINKS)
303 bool UIDelegate::UIClient::shouldIncludeAppLinkActionsForElement(_WKActivatedElementInfo *elementInfo)
304 {
305     if (!m_uiDelegate.m_delegateMethods.webViewShouldIncludeAppLinkActionsForElement)
306         return true;
307
308     auto delegate = m_uiDelegate.m_delegate.get();
309     if (!delegate)
310         return true;
311
312     return [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView shouldIncludeAppLinkActionsForElement:elementInfo];
313 }
314 #endif
315
316 RetainPtr<NSArray> UIDelegate::UIClient::actionsForElement(_WKActivatedElementInfo *elementInfo, RetainPtr<NSArray> defaultActions)
317 {
318     if (!m_uiDelegate.m_delegateMethods.webViewActionsForElementDefaultActions)
319         return WTF::move(defaultActions);
320
321     auto delegate = m_uiDelegate.m_delegate.get();
322     if (!delegate)
323         return defaultActions;
324
325     return [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate.m_webView actionsForElement:elementInfo defaultActions:defaultActions.get()];
326 }
327
328 void UIDelegate::UIClient::didNotHandleTapAsClick(const WebCore::IntPoint& point)
329 {
330     if (!m_uiDelegate.m_delegateMethods.webViewDidNotHandleTapAsClickAtPoint)
331         return;
332
333     auto delegate = m_uiDelegate.m_delegate.get();
334     if (!delegate)
335         return;
336
337     [static_cast<id <WKUIDelegatePrivate>>(delegate) _webView:m_uiDelegate.m_webView didNotHandleTapAsClickAtPoint:point];
338 }
339 #endif
340
341 } // namespace WebKit
342
343 #endif // WK_API_ENABLED