Title: [262592] trunk/Source/WebKit
Revision
262592
Author
commit-qu...@webkit.org
Date
2020-06-04 20:28:46 -0700 (Thu, 04 Jun 2020)

Log Message

Add WKUIDelegate SPI to confirm before opening a PDF
https://bugs.webkit.org/show_bug.cgi?id=212795
<rdar://problem/58715847>

Patch by Alex Christensen <achristen...@webkit.org> on 2020-06-04
Reviewed by Tim Horton.

* UIProcess/API/APIUIClient.h:
(API::UIClient::confirmPDFOpening):
* UIProcess/API/Cocoa/WKUIDelegatePrivate.h:
* UIProcess/Cocoa/UIDelegate.h:
* UIProcess/Cocoa/UIDelegate.mm:
(WebKit::UIDelegate::setDelegate):
(WebKit::UIDelegate::UIClient::confirmPDFOpening):
* UIProcess/WebPageProxy.h:
* UIProcess/WebPageProxy.messages.in:
* UIProcess/ios/WebPageProxyIOS.mm:
(WebKit::WebPageProxy::savePDFToTemporaryFolderAndOpenWithNativeApplication):
(WebKit::WebPageProxy::openPDFFromTemporaryFolderWithNativeApplication):
* UIProcess/mac/WebPageProxyMac.mm:
(WebKit::WebPageProxy::savePDFToTemporaryFolderAndOpenWithNativeApplication):
(WebKit::WebPageProxy::openPDFFromTemporaryFolderWithNativeApplication):
* WebProcess/Plugins/PDF/PDFPlugin.mm:
(WebKit::PDFPlugin::openWithNativeApplication):
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::savePDFToTemporaryFolderAndOpenWithNativeApplication):
* WebProcess/WebPage/WebPage.h:

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (262591 => 262592)


--- trunk/Source/WebKit/ChangeLog	2020-06-05 02:40:58 UTC (rev 262591)
+++ trunk/Source/WebKit/ChangeLog	2020-06-05 03:28:46 UTC (rev 262592)
@@ -1,3 +1,32 @@
+2020-06-04  Alex Christensen  <achristen...@webkit.org>
+
+        Add WKUIDelegate SPI to confirm before opening a PDF
+        https://bugs.webkit.org/show_bug.cgi?id=212795
+        <rdar://problem/58715847>
+
+        Reviewed by Tim Horton.
+
+        * UIProcess/API/APIUIClient.h:
+        (API::UIClient::confirmPDFOpening):
+        * UIProcess/API/Cocoa/WKUIDelegatePrivate.h:
+        * UIProcess/Cocoa/UIDelegate.h:
+        * UIProcess/Cocoa/UIDelegate.mm:
+        (WebKit::UIDelegate::setDelegate):
+        (WebKit::UIDelegate::UIClient::confirmPDFOpening):
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/WebPageProxy.messages.in:
+        * UIProcess/ios/WebPageProxyIOS.mm:
+        (WebKit::WebPageProxy::savePDFToTemporaryFolderAndOpenWithNativeApplication):
+        (WebKit::WebPageProxy::openPDFFromTemporaryFolderWithNativeApplication):
+        * UIProcess/mac/WebPageProxyMac.mm:
+        (WebKit::WebPageProxy::savePDFToTemporaryFolderAndOpenWithNativeApplication):
+        (WebKit::WebPageProxy::openPDFFromTemporaryFolderWithNativeApplication):
+        * WebProcess/Plugins/PDF/PDFPlugin.mm:
+        (WebKit::PDFPlugin::openWithNativeApplication):
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::savePDFToTemporaryFolderAndOpenWithNativeApplication):
+        * WebProcess/WebPage/WebPage.h:
+
 2020-06-04  Chris Dumez  <cdu...@apple.com>
 
         UIProcess may crash after its prewarmed WebProcess gets terminated

Modified: trunk/Source/WebKit/UIProcess/API/APIUIClient.h (262591 => 262592)


--- trunk/Source/WebKit/UIProcess/API/APIUIClient.h	2020-06-05 02:40:58 UTC (rev 262591)
+++ trunk/Source/WebKit/UIProcess/API/APIUIClient.h	2020-06-05 03:28:46 UTC (rev 262592)
@@ -196,6 +196,8 @@
     
     virtual void didShowSafeBrowsingWarning() { }
 
+    virtual void confirmPDFOpening(WebKit::WebPageProxy&, WebKit::FrameInfoData&&, CompletionHandler<void(bool)>&& completionHandler) { completionHandler(true); }
+
 #if ENABLE(WEB_AUTHN)
     virtual void runWebAuthenticationPanel(WebKit::WebPageProxy&, WebAuthenticationPanel&, WebKit::WebFrameProxy&, WebKit::FrameInfoData&&, CompletionHandler<void(WebKit::WebAuthenticationPanelResult)>&& completionHandler) { completionHandler(WebKit::WebAuthenticationPanelResult::Unavailable); }
 #endif

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKUIDelegatePrivate.h (262591 => 262592)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKUIDelegatePrivate.h	2020-06-05 02:40:58 UTC (rev 262591)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKUIDelegatePrivate.h	2020-06-05 03:28:46 UTC (rev 262592)
@@ -115,6 +115,7 @@
 - (void)_webViewDidShowSafeBrowsingWarning:(WKWebView *)webView WK_API_AVAILABLE(macos(10.14.4), ios(12.2));
 - (void)_webViewDidLosePointerLock:(WKWebView *)webView WK_API_AVAILABLE(macos(10.12.3));
 - (void)_webView:(WKWebView *)webView hasVideoInPictureInPictureDidChange:(BOOL)hasVideoInPictureInPicture WK_API_AVAILABLE(macos(10.13), ios(11.0));
+- (void)_webView:(WKWebView *)webView shouldAllowPDFToOpenFromFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
 
 - (void)_webView:(WKWebView *)webView imageOrMediaDocumentSizeChanged:(CGSize)size WK_API_AVAILABLE(macos(10.12), ios(10.0));
 - (NSDictionary *)_dataDetectionContextForWebView:(WKWebView *)webView WK_API_AVAILABLE(macos(10.12), ios(10.0));

Modified: trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.h (262591 => 262592)


--- trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.h	2020-06-05 02:40:58 UTC (rev 262591)
+++ trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.h	2020-06-05 03:28:46 UTC (rev 262592)
@@ -154,6 +154,7 @@
 
         void imageOrMediaDocumentSizeChanged(const WebCore::IntSize&) final;
         void didShowSafeBrowsingWarning() final;
+        void confirmPDFOpening(WebPageProxy&, FrameInfoData&&, CompletionHandler<void(bool)>&&) final;
 #if ENABLE(WEB_AUTHN)
         void runWebAuthenticationPanel(WebPageProxy&, API::WebAuthenticationPanel&, WebFrameProxy&, FrameInfoData&&, CompletionHandler<void(WebAuthenticationPanelResult)>&&) final;
 #endif
@@ -238,6 +239,7 @@
 #endif
         bool webViewHasVideoInPictureInPictureDidChange : 1;
         bool webViewDidShowSafeBrowsingWarning : 1;
+        bool webViewShouldAllowPDFToOpenFromFrameCompletionHandler : 1;
 #if ENABLE(WEB_AUTHN)
         bool webViewRunWebAuthenticationPanelInitiatedByFrameCompletionHandler : 1;
 #endif

Modified: trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.mm (262591 => 262592)


--- trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.mm	2020-06-05 02:40:58 UTC (rev 262591)
+++ trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.mm	2020-06-05 03:28:46 UTC (rev 262592)
@@ -171,6 +171,8 @@
     
     m_delegateMethods.webViewHasVideoInPictureInPictureDidChange = [delegate respondsToSelector:@selector(_webView:hasVideoInPictureInPictureDidChange:)];
     m_delegateMethods.webViewDidShowSafeBrowsingWarning = [delegate respondsToSelector:@selector(_webViewDidShowSafeBrowsingWarning:)];
+    m_delegateMethods.webViewShouldAllowPDFToOpenFromFrameCompletionHandler = [delegate respondsToSelector:@selector(_webView:shouldAllowPDFToOpenFromFrame:completionHandler:)];
+
 #if ENABLE(WEB_AUTHN)
     m_delegateMethods.webViewRunWebAuthenticationPanelInitiatedByFrameCompletionHandler = [delegate respondsToSelector:@selector(_webView:runWebAuthenticationPanel:initiatedByFrame:completionHandler:)];
 #endif
@@ -1268,6 +1270,24 @@
     [static_cast<id <WKUIDelegatePrivate>>(delegate) _webViewDidShowSafeBrowsingWarning:m_uiDelegate.m_webView];
 }
 
+void UIDelegate::UIClient::confirmPDFOpening(WebPageProxy& page, FrameInfoData&& frameInfo, CompletionHandler<void(bool)>&& completionHandler)
+{
+    if (!m_uiDelegate.m_delegateMethods.webViewShouldAllowPDFToOpenFromFrameCompletionHandler)
+        return completionHandler(true);
+
+    auto delegate = m_uiDelegate.m_delegate.get();
+    if (!delegate)
+        return completionHandler(true);
+
+    auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:shouldAllowPDFToOpenFromFrame:completionHandler:));
+    [static_cast<id <WKUIDelegatePrivate>>(delegate) _webView:m_uiDelegate.m_webView shouldAllowPDFToOpenFromFrame:wrapper(API::FrameInfo::create(WTFMove(frameInfo), &page)) completionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)] (BOOL result) mutable {
+        if (checker->completionHandlerHasBeenCalled())
+            return;
+        checker->didCallCompletionHandler();
+        completionHandler(result);
+    }).get()];
+}
+
 #if ENABLE(WEB_AUTHN)
 
 static WebAuthenticationPanelResult webAuthenticationPanelResult(_WKWebAuthenticationPanelResult result)

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.h (262591 => 262592)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.h	2020-06-05 02:40:58 UTC (rev 262591)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.h	2020-06-05 03:28:46 UTC (rev 262592)
@@ -1295,8 +1295,8 @@
     void saveDataToFileInDownloadsFolder(String&& suggestedFilename, String&& mimeType, URL&& originatingURL, API::Data&);
     void savePDFToFileInDownloadsFolder(String&& suggestedFilename, URL&& originatingURL, const IPC::DataReference&);
 #if PLATFORM(COCOA)
-    void savePDFToTemporaryFolderAndOpenWithNativeApplication(const String& suggestedFilename, const String& originatingURLString, const IPC::DataReference&, const String& pdfUUID);
-    void openPDFFromTemporaryFolderWithNativeApplication(const String& pdfUUID);
+    void savePDFToTemporaryFolderAndOpenWithNativeApplication(const String& suggestedFilename, FrameInfoData&&, const IPC::DataReference&, const String& pdfUUID);
+    void openPDFFromTemporaryFolderWithNativeApplication(FrameInfoData&&, const String& pdfUUID);
 #endif
 
 #if ENABLE(PDFKIT_PLUGIN)

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.messages.in (262591 => 262592)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.messages.in	2020-06-05 02:40:58 UTC (rev 262591)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.messages.in	2020-06-05 03:28:46 UTC (rev 262592)
@@ -440,8 +440,8 @@
     SavePDFToFileInDownloadsFolder(String suggestedFilename, URL originatingURL, IPC::DataReference data)
 
 #if PLATFORM(COCOA)
-    SavePDFToTemporaryFolderAndOpenWithNativeApplication(String suggestedFilename, String originatingURLString, IPC::DataReference data, String pdfUUID)
-    OpenPDFFromTemporaryFolderWithNativeApplication(String pdfUUID)
+    SavePDFToTemporaryFolderAndOpenWithNativeApplication(String suggestedFilename, struct WebKit::FrameInfoData sourceFrameInfo, IPC::DataReference data, String pdfUUID)
+    OpenPDFFromTemporaryFolderWithNativeApplication(struct WebKit::FrameInfoData sourceFrameInfo, String pdfUUID)
 #endif
 
 #if ENABLE(PDFKIT_PLUGIN)

Modified: trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm (262591 => 262592)


--- trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm	2020-06-05 02:40:58 UTC (rev 262591)
+++ trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm	2020-06-05 03:28:46 UTC (rev 262592)
@@ -1087,12 +1087,12 @@
     pageClient().didPerformDictionaryLookup(dictionaryPopupInfo);
 }
 
-void WebPageProxy::savePDFToTemporaryFolderAndOpenWithNativeApplication(const String&, const String&, const IPC::DataReference&, const String&)
+void WebPageProxy::savePDFToTemporaryFolderAndOpenWithNativeApplication(const String&, FrameInfoData&&, const IPC::DataReference&, const String&)
 {
     notImplemented();
 }
 
-void WebPageProxy::openPDFFromTemporaryFolderWithNativeApplication(const String&)
+void WebPageProxy::openPDFFromTemporaryFolderWithNativeApplication(FrameInfoData&&, const String&)
 {
     notImplemented();
 }

Modified: trunk/Source/WebKit/UIProcess/mac/WebPageProxyMac.mm (262591 => 262592)


--- trunk/Source/WebKit/UIProcess/mac/WebPageProxyMac.mm	2020-06-05 02:40:58 UTC (rev 262591)
+++ trunk/Source/WebKit/UIProcess/mac/WebPageProxyMac.mm	2020-06-05 03:28:46 UTC (rev 262592)
@@ -34,6 +34,7 @@
 #import "DataReference.h"
 #import "EditorState.h"
 #import "FontInfo.h"
+#import "FrameInfoData.h"
 #import "InsertTextOptions.h"
 #import "MenuUtilities.h"
 #import "NativeWebKeyboardEvent.h"
@@ -477,13 +478,10 @@
     return path;
 }
 
-void WebPageProxy::savePDFToTemporaryFolderAndOpenWithNativeApplication(const String& suggestedFilename, const String& originatingURLString, const IPC::DataReference& data, const String& pdfUUID)
+void WebPageProxy::savePDFToTemporaryFolderAndOpenWithNativeApplication(const String& suggestedFilename, FrameInfoData&& frameInfo, const IPC::DataReference& data, const String& pdfUUID)
 {
     MESSAGE_CHECK(TemporaryPDFFileMap::isValidKey(pdfUUID));
 
-    // FIXME: Write originatingURLString to the file's originating URL metadata (perhaps FileSystem::setMetadataURL()?).
-    UNUSED_PARAM(originatingURLString);
-
     if (data.isEmpty()) {
         WTFLogAlways("Cannot save empty PDF file to the temporary directory.");
         return;
@@ -495,28 +493,32 @@
         return;
     }
 
-    NSString *nsPath = pathToPDFOnDisk(sanitizedFilename);
+    auto nsPath = retainPtr(pathToPDFOnDisk(sanitizedFilename));
 
     if (!nsPath)
         return;
 
-    RetainPtr<NSNumber> permissions = adoptNS([[NSNumber alloc] initWithInt:S_IRUSR]);
-    RetainPtr<NSDictionary> fileAttributes = adoptNS([[NSDictionary alloc] initWithObjectsAndKeys:permissions.get(), NSFilePosixPermissions, nil]);
-    RetainPtr<NSData> nsData = adoptNS([[NSData alloc] initWithBytesNoCopy:(void*)data.data() length:data.size() freeWhenDone:NO]);
+    auto permissions = adoptNS([[NSNumber alloc] initWithInt:S_IRUSR]);
+    auto fileAttributes = adoptNS([[NSDictionary alloc] initWithObjectsAndKeys:permissions.get(), NSFilePosixPermissions, nil]);
+    auto nsData = adoptNS([[NSData alloc] initWithBytesNoCopy:(void*)data.data() length:data.size() freeWhenDone:NO]);
 
-    if (![[NSFileManager defaultManager] createFileAtPath:nsPath contents:nsData.get() attributes:fileAttributes.get()]) {
+    if (![[NSFileManager defaultManager] createFileAtPath:nsPath.get() contents:nsData.get() attributes:fileAttributes.get()]) {
         WTFLogAlways("Cannot create PDF file in the temporary directory (%s).", sanitizedFilename.utf8().data());
         return;
     }
+    auto originatingURLString = frameInfo.request.url().string();
+    FileSystem::setMetadataURL(nsPath.get(), originatingURLString);
 
-    m_temporaryPDFFiles.add(pdfUUID, nsPath);
+    m_temporaryPDFFiles.add(pdfUUID, nsPath.get());
 
-    ALLOW_DEPRECATED_DECLARATIONS_BEGIN
-    [[NSWorkspace sharedWorkspace] openFile:nsPath];
-    ALLOW_DEPRECATED_DECLARATIONS_END
+    m_uiClient->confirmPDFOpening(*this, WTFMove(frameInfo), [nsPath = WTFMove(nsPath)] (bool allowed) {
+        if (allowed)
+            return;
+        [[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:nsPath.get() isDirectory:NO]];
+    });
 }
 
-void WebPageProxy::openPDFFromTemporaryFolderWithNativeApplication(const String& pdfUUID)
+void WebPageProxy::openPDFFromTemporaryFolderWithNativeApplication(FrameInfoData&& frameInfo, const String& pdfUUID)
 {
     MESSAGE_CHECK(TemporaryPDFFileMap::isValidKey(pdfUUID));
 
@@ -525,9 +527,11 @@
     if (!pdfFilename.endsWithIgnoringASCIICase(".pdf"))
         return;
 
-    ALLOW_DEPRECATED_DECLARATIONS_BEGIN
-    [[NSWorkspace sharedWorkspace] openFile:pdfFilename];
-    ALLOW_DEPRECATED_DECLARATIONS_END
+    m_uiClient->confirmPDFOpening(*this, WTFMove(frameInfo), [pdfFilename = WTFMove(pdfFilename)] (bool allowed) {
+        if (allowed)
+            return;
+        [[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:pdfFilename isDirectory:NO]];
+    });
 }
 
 #if ENABLE(PDFKIT_PLUGIN)

Modified: trunk/Source/WebKit/WebProcess/Plugins/PDF/PDFPlugin.mm (262591 => 262592)


--- trunk/Source/WebKit/WebProcess/Plugins/PDF/PDFPlugin.mm	2020-06-05 02:40:58 UTC (rev 262591)
+++ trunk/Source/WebKit/WebProcess/Plugins/PDF/PDFPlugin.mm	2020-06-05 03:28:46 UTC (rev 262592)
@@ -30,6 +30,7 @@
 
 #import "ArgumentCoders.h"
 #import "DataReference.h"
+#import "FrameInfoData.h"
 #import "Logging.h"
 #import "PDFAnnotationTextWidgetDetails.h"
 #import "PDFContextMenu.h"
@@ -2398,11 +2399,11 @@
         m_temporaryPDFUUID = createCanonicalUUIDString();
         ASSERT(m_temporaryPDFUUID);
 
-        m_frame.page()->savePDFToTemporaryFolderAndOpenWithNativeApplication(m_suggestedFilename, m_frame.url().string(), static_cast<const unsigned char *>([data bytes]), [data length], m_temporaryPDFUUID);
+        m_frame.page()->savePDFToTemporaryFolderAndOpenWithNativeApplication(m_suggestedFilename, m_frame.info(), static_cast<const unsigned char *>([data bytes]), [data length], m_temporaryPDFUUID);
         return;
     }
 
-    m_frame.page()->send(Messages::WebPageProxy::OpenPDFFromTemporaryFolderWithNativeApplication(m_temporaryPDFUUID));
+    m_frame.page()->send(Messages::WebPageProxy::OpenPDFFromTemporaryFolderWithNativeApplication(m_frame.info(), m_temporaryPDFUUID));
 }
 
 void PDFPlugin::writeItemsToPasteboard(NSString *pasteboardName, NSArray *items, NSArray *types)

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp (262591 => 262592)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2020-06-05 02:40:58 UTC (rev 262591)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2020-06-05 03:28:46 UTC (rev 262592)
@@ -5054,9 +5054,9 @@
 }
 
 #if PLATFORM(COCOA)
-void WebPage::savePDFToTemporaryFolderAndOpenWithNativeApplication(const String& suggestedFilename, const String& originatingURLString, const uint8_t* data, unsigned long size, const String& pdfUUID)
+void WebPage::savePDFToTemporaryFolderAndOpenWithNativeApplication(const String& suggestedFilename, FrameInfoData&& frameInfo, const uint8_t* data, unsigned long size, const String& pdfUUID)
 {
-    send(Messages::WebPageProxy::SavePDFToTemporaryFolderAndOpenWithNativeApplication(suggestedFilename, originatingURLString, IPC::DataReference(data, size), pdfUUID));
+    send(Messages::WebPageProxy::SavePDFToTemporaryFolderAndOpenWithNativeApplication(suggestedFilename, frameInfo, IPC::DataReference(data, size), pdfUUID));
 }
 #endif
 

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.h (262591 => 262592)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.h	2020-06-05 02:40:58 UTC (rev 262591)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.h	2020-06-05 03:28:46 UTC (rev 262592)
@@ -1068,7 +1068,7 @@
     void savePDFToFileInDownloadsFolder(const String& suggestedFilename, const URL& originatingURL, const uint8_t* data, unsigned long size);
 
 #if PLATFORM(COCOA)
-    void savePDFToTemporaryFolderAndOpenWithNativeApplication(const String& suggestedFilename, const String& originatingURLString, const uint8_t* data, unsigned long size, const String& pdfUUID);
+    void savePDFToTemporaryFolderAndOpenWithNativeApplication(const String& suggestedFilename, FrameInfoData&&, const uint8_t* data, unsigned long size, const String& pdfUUID);
 #endif
 
     bool mainFrameIsScrollable() const { return m_mainFrameIsScrollable; }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to