Changeset 273075 in webkit


Ignore:
Timestamp:
Feb 18, 2021 8:06:49 AM (3 years ago)
Author:
achristensen@apple.com
Message:

Add SPI for getting size of PDF document during printing
https://bugs.webkit.org/show_bug.cgi?id=222064
<rdar://problem/74370891>

Reviewed by Geoff Garen and Tim Horton.

Source/WebKit:

When printing a PDF, we want to know its page size. If the print is initiated by the web process (such as when window.print is called
or if the PDF contains JavaScript that tells the browser to print it) then we would like that information with the print request so we
don't have to ask the web process, which is hung waiting for a synchronous message reply. If the print is initiated by the UI process
(such as if the user prints from a menu or keyboard shortcut) then we need a way to ask what the size of a PDF is.

I reused the SPI in WKUIDelegatePrivate because it was only briefly adopted before we knew we needed this and then reverted.
I'm also adding an SPI way to get from a WKPageRef to a WKWebView so this can be adopted without redoing all of Safari.

Covered by API tests.

  • UIProcess/API/APIUIClient.h:

(API::UIClient::printFrame):

  • UIProcess/API/C/WKPage.cpp:

(WKPageSetPageUIClient):

  • UIProcess/API/Cocoa/WKUIDelegatePrivate.h:
  • UIProcess/API/Cocoa/WKWebView.mm:

(-[WKWebView _getPDFFirstPageSizeInFrame:completionHandler:]):

  • UIProcess/API/Cocoa/WKWebViewPrivate.h:
  • UIProcess/Cocoa/UIDelegate.h:
  • UIProcess/Cocoa/UIDelegate.mm:

(WebKit::UIDelegate::setDelegate):
(WebKit::UIDelegate::UIClient::printFrame):

  • UIProcess/WebPageProxy.cpp:

(WebKit::WebPageProxy::printFrame):
(WebKit::WebPageProxy::getPDFFirstPageSize):

  • UIProcess/WebPageProxy.h:
  • UIProcess/WebPageProxy.messages.in:
  • WebProcess/Plugins/PDF/PDFPlugin.h:
  • WebProcess/Plugins/PDF/PDFPlugin.mm:

(WebKit::PDFPlugin::pdfDocumentSizeForPrinting const):

  • WebProcess/Plugins/Plugin.cpp:

(WebKit::Plugin::pdfDocumentSizeForPrinting const):

  • WebProcess/Plugins/Plugin.h:
  • WebProcess/WebCoreSupport/WebChromeClient.cpp:

(WebKit::WebChromeClient::print):

  • WebProcess/WebPage/Cocoa/WebPageCocoa.mm:

(WebKit::WebPage::getPDFFirstPageSize):

  • WebProcess/WebPage/WebPage.cpp:

(WebKit::WebPage::getPDFFirstPageSize):

  • WebProcess/WebPage/WebPage.h:
  • WebProcess/WebPage/WebPage.messages.in:

Tools:

  • TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
  • TestWebKitAPI/Tests/WebKit/test_print.pdf: Added.
  • TestWebKitAPI/Tests/WebKitCocoa/WKPDFView.mm:

(-[PrintUIDelegate _webView:printFrame:pdfFirstPageSize:completionHandler:]):
(-[PrintUIDelegate waitForPageSize]):
(-[PrintUIDelegate lastPrintedFrame]):
(TEST):

Location:
trunk
Files:
1 added
27 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r273074 r273075  
     12021-02-18  Alex Christensen  <achristensen@webkit.org>
     2
     3        Add SPI for getting size of PDF document during printing
     4        https://bugs.webkit.org/show_bug.cgi?id=222064
     5        <rdar://problem/74370891>
     6
     7        Reviewed by Geoff Garen and Tim Horton.
     8
     9        When printing a PDF, we want to know its page size.  If the print is initiated by the web process (such as when window.print is called
     10        or if the PDF contains JavaScript that tells the browser to print it) then we would like that information with the print request so we
     11        don't have to ask the web process, which is hung waiting for a synchronous message reply.  If the print is initiated by the UI process
     12        (such as if the user prints from a menu or keyboard shortcut) then we need a way to ask what the size of a PDF is.
     13
     14        I reused the SPI in WKUIDelegatePrivate because it was only briefly adopted before we knew we needed this and then reverted.
     15        I'm also adding an SPI way to get from a WKPageRef to a WKWebView so this can be adopted without redoing all of Safari.
     16
     17        Covered by API tests.
     18
     19        * UIProcess/API/APIUIClient.h:
     20        (API::UIClient::printFrame):
     21        * UIProcess/API/C/WKPage.cpp:
     22        (WKPageSetPageUIClient):
     23        * UIProcess/API/Cocoa/WKUIDelegatePrivate.h:
     24        * UIProcess/API/Cocoa/WKWebView.mm:
     25        (-[WKWebView _getPDFFirstPageSizeInFrame:completionHandler:]):
     26        * UIProcess/API/Cocoa/WKWebViewPrivate.h:
     27        * UIProcess/Cocoa/UIDelegate.h:
     28        * UIProcess/Cocoa/UIDelegate.mm:
     29        (WebKit::UIDelegate::setDelegate):
     30        (WebKit::UIDelegate::UIClient::printFrame):
     31        * UIProcess/WebPageProxy.cpp:
     32        (WebKit::WebPageProxy::printFrame):
     33        (WebKit::WebPageProxy::getPDFFirstPageSize):
     34        * UIProcess/WebPageProxy.h:
     35        * UIProcess/WebPageProxy.messages.in:
     36        * WebProcess/Plugins/PDF/PDFPlugin.h:
     37        * WebProcess/Plugins/PDF/PDFPlugin.mm:
     38        (WebKit::PDFPlugin::pdfDocumentSizeForPrinting const):
     39        * WebProcess/Plugins/Plugin.cpp:
     40        (WebKit::Plugin::pdfDocumentSizeForPrinting const):
     41        * WebProcess/Plugins/Plugin.h:
     42        * WebProcess/WebCoreSupport/WebChromeClient.cpp:
     43        (WebKit::WebChromeClient::print):
     44        * WebProcess/WebPage/Cocoa/WebPageCocoa.mm:
     45        (WebKit::WebPage::getPDFFirstPageSize):
     46        * WebProcess/WebPage/WebPage.cpp:
     47        (WebKit::WebPage::getPDFFirstPageSize):
     48        * WebProcess/WebPage/WebPage.h:
     49        * WebProcess/WebPage/WebPage.messages.in:
     50
    1512021-02-18  Kimmo Kinnunen  <kkinnunen@apple.com>
    252
  • trunk/Source/WebKit/UIProcess/API/APIUIClient.h

    r272573 r273075  
    146146    virtual void drawHeader(WebKit::WebPageProxy&, WebKit::WebFrameProxy&, WebCore::FloatRect&&) { }
    147147    virtual void drawFooter(WebKit::WebPageProxy&, WebKit::WebFrameProxy&, WebCore::FloatRect&&) { }
    148     virtual void printFrame(WebKit::WebPageProxy&, WebKit::WebFrameProxy&, CompletionHandler<void()>&& completionHandler) { completionHandler(); }
     148    virtual void printFrame(WebKit::WebPageProxy&, WebKit::WebFrameProxy&, const WebCore::FloatSize& pdfFirstPageSize, CompletionHandler<void()>&& completionHandler) { completionHandler(); }
    149149
    150150    virtual bool canRunModal() const { return false; }
  • trunk/Source/WebKit/UIProcess/API/C/WKPage.cpp

    r272784 r273075  
    19721972        }
    19731973
    1974         void printFrame(WebPageProxy& page, WebFrameProxy& frame, CompletionHandler<void()>&& completionHandler) final
     1974        void printFrame(WebPageProxy& page, WebFrameProxy& frame, const WebCore::FloatSize&, CompletionHandler<void()>&& completionHandler) final
    19751975        {
    19761976            if (m_client.printFrame)
  • trunk/Source/WebKit/UIProcess/API/C/mac/WKPagePrivateMac.h

    r242325 r273075  
    3636#ifdef __OBJC__
    3737
     38@class WKWebView;
    3839@class _WKRemoteObjectRegistry;
    3940
     
    6465WK_EXPORT WKNavigation *WKPageLoadFileReturningNavigation(WKPageRef page, WKURLRef fileURL, WKURLRef resourceDirectoryURL);
    6566
     67WK_EXPORT WKWebView *WKPageGetWebView(WKPageRef page);
     68
    6669#endif // __OBJC__
    6770
  • trunk/Source/WebKit/UIProcess/API/C/mac/WKPagePrivateMac.mm

    r249962 r273075  
    3333#import "WKNavigationInternal.h"
    3434#import "WKViewInternal.h"
     35#import "WKWebViewInternal.h"
    3536#import "WebPageGroup.h"
    3637#import "WebPageProxy.h"
     
    148149}
    149150
     151WKWebView *WKPageGetWebView(WKPageRef page)
     152{
     153    if (!page)
     154        return nil;
     155    return fromWebPageProxy(*WebKit::toImpl(page));
     156}
     157
    150158#if PLATFORM(MAC)
    151159bool WKPageIsPlayingVideoInEnhancedFullscreen(WKPageRef pageRef)
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKUIDelegatePrivate.h

    r271813 r273075  
    108108
    109109- (void)_webView:(WKWebView *)webView printFrame:(_WKFrameHandle *)frame;
    110 - (void)_webView:(WKWebView *)webView printFrame:(_WKFrameHandle *)frame completionHandler:(void (^)(void))completionHandler WK_API_AVAILABLE(macos(11.0), ios(14.0));
     110- (void)_webView:(WKWebView *)webView printFrame:(_WKFrameHandle *)frame pdfFirstPageSize:(CGSize)size completionHandler:(void (^)(void))completionHandler WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
    111111
    112112- (void)_webViewClose:(WKWebView *)webView;
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm

    r273040 r273075  
    22602260}
    22612261
     2262- (void)_getPDFFirstPageSizeInFrame:(_WKFrameHandle *)frame completionHandler:(void(^)(CGSize))completionHandler
     2263{
     2264    _page->getPDFFirstPageSize(frame->_frameHandle->frameID(), [completionHandler = makeBlockPtr(completionHandler)](WebCore::FloatSize size) {
     2265        completionHandler(static_cast<CGSize>(size));
     2266    });
     2267}
     2268
    22622269- (NSData *)_sessionStateData
    22632270{
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h

    r273040 r273075  
    351351
    352352- (void)_takePDFSnapshotWithConfiguration:(WKSnapshotConfiguration *)snapshotConfiguration completionHandler:(void (^)(NSData *pdfSnapshotData, NSError *error))completionHandler WK_API_AVAILABLE(macos(10.15.4), ios(13.4));
     353- (void)_getPDFFirstPageSizeInFrame:(_WKFrameHandle *)frame completionHandler:(void(^)(CGSize))completionHandler WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
    353354
    354355- (void)_getProcessDisplayNameWithCompletionHandler:(void (^)(NSString *))completionHandler WK_API_AVAILABLE(macos(11.0), ios(14.0));
  • trunk/Source/WebKit/UIProcess/API/glib/WebKitUIClient.cpp

    r272574 r273075  
    330330    }
    331331
    332     void printFrame(WebPageProxy&, WebFrameProxy& frame, CompletionHandler<void()>&& completionHandler) final
     332    void printFrame(WebPageProxy&, WebFrameProxy& frame, const WebCore::FloatSize&, CompletionHandler<void()>&& completionHandler) final
    333333    {
    334334        webkitWebViewPrintFrame(m_webView, &frame);
  • trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.h

    r271424 r273075  
    138138        void checkUserMediaPermissionForOrigin(WebPageProxy&, WebFrameProxy&, API::SecurityOrigin&, API::SecurityOrigin&, UserMediaPermissionCheckProxy&) final;
    139139        void mediaCaptureStateDidChange(WebCore::MediaProducer::MediaStateFlags) final;
    140         void printFrame(WebPageProxy&, WebFrameProxy&, CompletionHandler<void()>&&) final;
     140        void printFrame(WebPageProxy&, WebFrameProxy&, const WebCore::FloatSize& pdfFirstPageSize, CompletionHandler<void()>&&) final;
    141141#if PLATFORM(IOS_FAMILY)
    142142#if HAVE(APP_LINKS)
     
    218218        bool webViewDecideWebApplicationCacheQuotaForSecurityOriginCurrentQuotaTotalBytesNeeded : 1;
    219219        bool webViewPrintFrame : 1;
    220         bool webViewPrintFrameCompletionHandler : 1;
     220        bool webViewPrintFramePDFFirstPageSizeCompletionHandler : 1;
    221221        bool webViewDidClose : 1;
    222222        bool webViewClose : 1;
  • trunk/Source/WebKit/UIProcess/Cocoa/UIDelegate.mm

    r271424 r273075  
    145145    m_delegateMethods.webViewDecideWebApplicationCacheQuotaForSecurityOriginCurrentQuotaTotalBytesNeeded = [delegate respondsToSelector:@selector(_webView:decideWebApplicationCacheQuotaForSecurityOrigin:currentQuota:totalBytesNeeded:decisionHandler:)];
    146146    m_delegateMethods.webViewPrintFrame = [delegate respondsToSelector:@selector(_webView:printFrame:)];
    147     m_delegateMethods.webViewPrintFrameCompletionHandler = [delegate respondsToSelector:@selector(_webView:printFrame:completionHandler:)];
     147    m_delegateMethods.webViewPrintFramePDFFirstPageSizeCompletionHandler = [delegate respondsToSelector:@selector(_webView:printFrame:pdfFirstPageSize:completionHandler:)];
    148148    m_delegateMethods.webViewDidClose = [delegate respondsToSelector:@selector(webViewDidClose:)];
    149149    m_delegateMethods.webViewClose = [delegate respondsToSelector:@selector(_webViewClose:)];
     
    11941194}
    11951195
    1196 void UIDelegate::UIClient::printFrame(WebPageProxy&, WebFrameProxy& webFrameProxy, CompletionHandler<void()>&& completionHandler)
     1196void UIDelegate::UIClient::printFrame(WebPageProxy&, WebFrameProxy& webFrameProxy, const WebCore::FloatSize& pdfFirstPageSize, CompletionHandler<void()>&& completionHandler)
    11971197{
    11981198    if (!m_uiDelegate)
     
    12041204
    12051205    auto handle = API::FrameHandle::create(webFrameProxy.frameID());
    1206     if (m_uiDelegate->m_delegateMethods.webViewPrintFrameCompletionHandler) {
    1207         auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:printFrame:completionHandler:));
    1208         [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate->m_webView.get().get() printFrame:wrapper(handle) completionHandler:makeBlockPtr([checker = WTFMove(checker), completionHandler = WTFMove(completionHandler)] () mutable {
     1206    if (m_uiDelegate->m_delegateMethods.webViewPrintFramePDFFirstPageSizeCompletionHandler) {
     1207        auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(_webView:printFrame:pdfFirstPageSize:completionHandler:));
     1208        [(id <WKUIDelegatePrivate>)delegate _webView:m_uiDelegate->m_webView.get().get() printFrame:wrapper(handle) pdfFirstPageSize:static_cast<CGSize>(pdfFirstPageSize) completionHandler:makeBlockPtr([checker = WTFMove(checker), completionHandler = WTFMove(completionHandler)] () mutable {
    12091209            if (checker->completionHandlerHasBeenCalled())
    12101210                return;
  • trunk/Source/WebKit/UIProcess/WebPageProxy.cpp

    r273048 r273075  
    59255925}
    59265926   
    5927 void WebPageProxy::printFrame(FrameIdentifier frameID, const String& title, CompletionHandler<void()>&& completionHandler)
     5927void WebPageProxy::printFrame(FrameIdentifier frameID, const String& title, const WebCore::FloatSize& pdfFirstPageSize, CompletionHandler<void()>&& completionHandler)
    59285928{
    59295929    ASSERT(!m_isPerformingDOMPrintOperation);
     
    59355935    frame->didChangeTitle(title);
    59365936
    5937     m_uiClient->printFrame(*this, *frame, [this, protectedThis = makeRef(*this), completionHandler = WTFMove(completionHandler)] () mutable {
     5937    m_uiClient->printFrame(*this, *frame, pdfFirstPageSize, [this, protectedThis = makeRef(*this), completionHandler = WTFMove(completionHandler)] () mutable {
    59385938        endPrinting(); // Send a message synchronously while m_isPerformingDOMPrintOperation is still true.
    59395939        m_isPerformingDOMPrintOperation = false;
     
    85468546#endif // PLATFORM(COCOA)
    85478547
     8548void WebPageProxy::getPDFFirstPageSize(WebCore::FrameIdentifier frameID, CompletionHandler<void(WebCore::FloatSize)>&& completionHandler)
     8549{
     8550    sendWithAsyncReply(Messages::WebPage::GetPDFFirstPageSize(frameID), WTFMove(completionHandler));
     8551}
     8552
    85488553void WebPageProxy::updateBackingStoreDiscardableState()
    85498554{
  • trunk/Source/WebKit/UIProcess/WebPageProxy.h

    r273023 r273075  
    12901290    void endPrinting();
    12911291    uint64_t computePagesForPrinting(WebFrameProxy*, const PrintInfo&, CompletionHandler<void(const Vector<WebCore::IntRect>&, double, const WebCore::FloatBoxExtent&)>&&);
     1292    void getPDFFirstPageSize(WebCore::FrameIdentifier, CompletionHandler<void(WebCore::FloatSize)>&&);
    12921293#if PLATFORM(COCOA)
    12931294    uint64_t drawRectToImage(WebFrameProxy*, const PrintInfo&, const WebCore::IntRect&, const WebCore::IntSize&, CompletionHandler<void(const WebKit::ShareableBitmap::Handle&)>&&);
     
    19901991    void showShareSheet(const WebCore::ShareDataWithParsedURL&, CompletionHandler<void(bool)>&&);
    19911992    void showContactPicker(const WebCore::ContactsRequestData&, CompletionHandler<void(Optional<Vector<WebCore::ContactInfo>>&&)>&&);
    1992     void printFrame(WebCore::FrameIdentifier, const String&, CompletionHandler<void()>&&);
     1993    void printFrame(WebCore::FrameIdentifier, const String&, const WebCore::FloatSize&,  CompletionHandler<void()>&&);
    19931994    void exceededDatabaseQuota(WebCore::FrameIdentifier, const String& originIdentifier, const String& databaseName, const String& displayName, uint64_t currentQuota, uint64_t currentOriginUsage, uint64_t currentDatabaseUsage, uint64_t expectedUsage, Messages::WebPageProxy::ExceededDatabaseQuotaDelayedReply&&);
    19941995    void reachedApplicationCacheOriginQuota(const String& originIdentifier, uint64_t currentQuota, uint64_t totalBytesNeeded, Messages::WebPageProxy::ReachedApplicationCacheOriginQuotaDelayedReply&&);
  • trunk/Source/WebKit/UIProcess/WebPageProxy.messages.in

    r272925 r273075  
    7171    ShowShareSheet(struct WebCore::ShareDataWithParsedURL shareData) -> (bool granted) Async
    7272    ShowContactPicker(struct WebCore::ContactsRequestData requestData) -> (Optional<Vector<WebCore::ContactInfo>> info) Async
    73     PrintFrame(WebCore::FrameIdentifier frameID, String title) -> () Synchronous
     73    PrintFrame(WebCore::FrameIdentifier frameID, String title, WebCore::FloatSize pdfFirstPageSize) -> () Synchronous
    7474    RunModal()
    7575    NotifyScrollerThumbIsVisibleInRect(WebCore::IntRect scrollerThumb)
  • trunk/Source/WebKit/WebProcess/Plugins/PDF/PDFPlugin.h

    r272324 r273075  
    214214   
    215215    RetainPtr<PDFDocument> pdfDocumentForPrinting() const final { return m_pdfDocument; }
     216    WebCore::FloatSize pdfDocumentSizeForPrinting() const final;
    216217    NSObject *accessibilityObject() const final;
    217218    id accessibilityAssociatedPluginParentForElement(WebCore::Element*) const final;
  • trunk/Source/WebKit/WebProcess/Plugins/PDF/PDFPlugin.mm

    r271124 r273075  
    15511551}
    15521552
     1553FloatSize PDFPlugin::pdfDocumentSizeForPrinting() const
     1554{
     1555    return FloatSize([[m_pdfDocument pageAtIndex:0] boundsForBox:kPDFDisplayBoxCropBox].size);
     1556}
     1557
    15531558JSObjectRef PDFPlugin::makeJSPDFDoc(JSContextRef ctx)
    15541559{
  • trunk/Source/WebKit/WebProcess/Plugins/Plugin.cpp

    r268989 r273075  
    128128}
    129129
     130#if PLATFORM(COCOA)
     131WebCore::FloatSize Plugin::pdfDocumentSizeForPrinting() const
     132{
     133    return { };
     134}
     135#endif
     136
    130137} // namespace WebKit
  • trunk/Source/WebKit/WebProcess/Plugins/Plugin.h

    r269785 r273075  
    5656class Element;
    5757class FloatPoint;
     58class FloatSize;
    5859class GraphicsContext;
    5960class IntPoint;
     
    276277#if PLATFORM(COCOA)
    277278    virtual RetainPtr<PDFDocument> pdfDocumentForPrinting() const { return 0; }
     279    virtual WebCore::FloatSize pdfDocumentSizeForPrinting() const;
    278280    virtual NSObject *accessibilityObject() const { return 0; }
    279281    virtual id accessibilityAssociatedPluginParentForElement(WebCore::Element*) const { return nullptr; }
  • trunk/Source/WebKit/WebProcess/WebCoreSupport/WebChromeClient.cpp

    r272925 r273075  
    4040#include "NetworkProcessConnection.h"
    4141#include "PageBanner.h"
     42#include "PluginView.h"
    4243#include "RemoteRenderingBackendProxy.h"
    4344#include "SharedBufferCopy.h"
     
    737738#endif
    738739
     740    WebCore::FloatSize pdfFirstPageSize;
     741#if PLATFORM(COCOA)
     742    if (auto* pluginView = WebPage::pluginViewForFrame(&frame)) {
     743        if (auto* plugin = pluginView->plugin())
     744            pdfFirstPageSize = plugin->pdfDocumentSizeForPrinting();
     745    }
     746#endif
     747
    739748    auto truncatedTitle = truncateFromEnd(title, maxTitleLength);
    740     m_page.sendSyncWithDelayedReply(Messages::WebPageProxy::PrintFrame(webFrame->frameID(), truncatedTitle.string), Messages::WebPageProxy::PrintFrame::Reply());
     749    m_page.sendSyncWithDelayedReply(Messages::WebPageProxy::PrintFrame(webFrame->frameID(), truncatedTitle.string, pdfFirstPageSize), Messages::WebPageProxy::PrintFrame::Reply());
    741750}
    742751
  • trunk/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm

    r272605 r273075  
    417417}
    418418
     419void WebPage::getPDFFirstPageSize(WebCore::FrameIdentifier frameID, CompletionHandler<void(WebCore::FloatSize)>&& completionHandler)
     420{
     421    auto* webFrame = WebProcess::singleton().webFrame(frameID);
     422    if (!webFrame)
     423        return completionHandler({ });
     424
     425    auto* coreFrame = webFrame->coreFrame();
     426    if (!coreFrame)
     427        return completionHandler({ });
     428
     429    auto* pluginView = pluginViewForFrame(coreFrame);
     430    if (!pluginView)
     431        return completionHandler({ });
     432   
     433    auto* plugin = pluginView->plugin();
     434    if (!plugin)
     435        return completionHandler({ });
     436
     437    completionHandler(FloatSize(plugin->pdfDocumentSizeForPrinting()));
     438}
     439
    419440} // namespace WebKit
    420441
  • trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp

    r273023 r273075  
    71437143
    71447144#if !PLATFORM(COCOA)
     7145void WebPage::getPDFFirstPageSize(WebCore::FrameIdentifier, CompletionHandler<void(WebCore::FloatSize)>&& completionHandler)
     7146{
     7147    completionHandler({ });
     7148}
     7149
    71457150void WebPage::getProcessDisplayName(CompletionHandler<void(String&&)>&& completionHandler)
    71467151{
  • trunk/Source/WebKit/WebProcess/WebPage/WebPage.h

    r273023 r273075  
    15091509    void loadSimulatedRequestAndResponse(LoadParameters&&, WebCore::ResourceResponse&&);
    15101510    void navigateToPDFLinkWithSimulatedClick(const String& url, WebCore::IntPoint documentPoint, WebCore::IntPoint screenPoint);
     1511    void getPDFFirstPageSize(WebCore::FrameIdentifier, CompletionHandler<void(WebCore::FloatSize)>&&);
    15111512    void reload(uint64_t navigationID, uint32_t reloadOptions, SandboxExtension::Handle&&);
    15121513    void goToBackForwardItem(uint64_t navigationID, const WebCore::BackForwardItemIdentifier&, WebCore::FrameLoadType, WebCore::ShouldTreatAsContinuingLoad, Optional<WebsitePoliciesData>&&);
  • trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in

    r273023 r273075  
    184184
    185185    NavigateToPDFLinkWithSimulatedClick(String url, WebCore::IntPoint documentPoint, WebCore::IntPoint screenPoint)
     186    GetPDFFirstPageSize(WebCore::FrameIdentifier frameID) -> (WebCore::FloatSize size) Async
    186187
    187188    Reload(uint64_t navigationID, uint32_t reloadOptions, WebKit::SandboxExtension::Handle sandboxExtensionHandle)
  • trunk/Tools/ChangeLog

    r273067 r273075  
     12021-02-18  Alex Christensen  <achristensen@webkit.org>
     2
     3        Add SPI for getting size of PDF document during printing
     4        https://bugs.webkit.org/show_bug.cgi?id=222064
     5        <rdar://problem/74370891>
     6
     7        Reviewed by Geoff Garen and Tim Horton.
     8
     9        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
     10        * TestWebKitAPI/Tests/WebKit/test_print.pdf: Added.
     11        * TestWebKitAPI/Tests/WebKitCocoa/WKPDFView.mm:
     12        (-[PrintUIDelegate _webView:printFrame:pdfFirstPageSize:completionHandler:]):
     13        (-[PrintUIDelegate waitForPageSize]):
     14        (-[PrintUIDelegate lastPrintedFrame]):
     15        (TEST):
     16
    1172021-02-18  Antoine Quint  <graouts@webkit.org>
    218
  • trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj

    r273067 r273075  
    462462                5C69BDD51F82A7EF000F4F4B /* JavaScriptDuringNavigation.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5C69BDD41F82A7EB000F4F4B /* JavaScriptDuringNavigation.mm */; };
    463463                5C6E27A7224EEBEA00128736 /* URLCanonicalization.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5C6E27A6224EEBEA00128736 /* URLCanonicalization.mm */; };
     464                5C7101C725DD98B600686200 /* test_print.pdf in Copy Resources */ = {isa = PBXBuildFile; fileRef = 5C7101C625DD988700686200 /* test_print.pdf */; };
    464465                5C7148952123A40A00FDE3C5 /* WKWebsiteDatastore.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5C7148942123A40700FDE3C5 /* WKWebsiteDatastore.mm */; };
    465466                5C75716122124C5200B9E5AC /* BundleRetainPagePlugIn.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5C75715F221249BD00B9E5AC /* BundleRetainPagePlugIn.mm */; };
     
    16511652                                524BBCA119E30C77002F1AF1 /* test.mp4 in Copy Resources */,
    16521653                                7AE9E5091AE5AE8B00CF874B /* test.pdf in Copy Resources */,
     1654                                5C7101C725DD98B600686200 /* test_print.pdf in Copy Resources */,
    16531655                                2E9896151D8F093800739892 /* text-and-password-inputs.html in Copy Resources */,
    16541656                                F4CD74C620FDACFA00DE3794 /* text-with-async-script.html in Copy Resources */,
     
    22492251                5C69BDD41F82A7EB000F4F4B /* JavaScriptDuringNavigation.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = JavaScriptDuringNavigation.mm; sourceTree = "<group>"; };
    22502252                5C6E27A6224EEBEA00128736 /* URLCanonicalization.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = URLCanonicalization.mm; sourceTree = "<group>"; };
     2253                5C7101C625DD988700686200 /* test_print.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = test_print.pdf; sourceTree = "<group>"; };
    22512254                5C7148942123A40700FDE3C5 /* WKWebsiteDatastore.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WKWebsiteDatastore.mm; sourceTree = "<group>"; };
    22522255                5C72E8CD244FFCE300381EB7 /* TestLegacyDownloadDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestLegacyDownloadDelegate.h; path = cocoa/TestLegacyDownloadDelegate.h; sourceTree = "<group>"; };
     
    42324235                                7673499A1930182E00E44DF9 /* StopLoadingDuringDidFailProvisionalLoad_bundle.cpp */,
    42334236                                1AE72F47173EB214006362F0 /* TerminateTwice.cpp */,
     4237                                5C7101C625DD988700686200 /* test_print.pdf */,
    42344238                                CE3524F11B142B8D0028A7C5 /* TextFieldDidBeginAndEndEditing.cpp */,
    42354239                                CE3524F21B142B8D0028A7C5 /* TextFieldDidBeginAndEndEditing_Bundle.cpp */,
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/UIDelegate.mm

    r273062 r273075  
    450450}
    451451
    452 - (void)_webView:(WKWebView *)webView printFrame:(_WKFrameHandle *)frame completionHandler:(void (^)(void))completionHandler
     452- (void)_webView:(WKWebView *)webView printFrame:(_WKFrameHandle *)frame pdfFirstPageSize:(CGSize)size completionHandler:(void (^)(void))completionHandler
    453453{
    454454    completionHandler();
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKPDFView.mm

    r273062 r273075  
    4444#endif
    4545
    46 #if PLATFORM(IOS) || ENABLE(UI_PROCESS_PDF_HUD)
     46#if PLATFORM(IOS) || ENABLE(UI_PROCESS_PDF_HUD) || PLATFORM(MAC)
    4747static NSData *pdfData()
    4848{
     
    373373}
    374374
     375#endif // ENABLE(UI_PROCESS_PDF_HUD)
     376
     377#if PLATFORM(MAC)
     378
     379@interface PrintUIDelegate : NSObject <WKUIDelegate>
     380
     381- (NSSize)waitForPageSize;
     382- (_WKFrameHandle *)lastPrintedFrame;
     383
     384@end
     385
     386@implementation PrintUIDelegate {
     387    NSSize _pageSize;
     388    bool _receivedSize;
     389    RetainPtr<_WKFrameHandle> _lastPrintedFrame;
     390}
     391
     392- (void)_webView:(WKWebView *)webView printFrame:(_WKFrameHandle *)frame pdfFirstPageSize:(CGSize)size completionHandler:(void (^)(void))completionHandler
     393{
     394    _pageSize = size;
     395    _receivedSize = true;
     396    _lastPrintedFrame = frame;
     397    completionHandler();
     398}
     399
     400- (NSSize)waitForPageSize
     401{
     402    _receivedSize = false;
     403    while (!_receivedSize)
     404        TestWebKitAPI::Util::spinRunLoop();
     405    return _pageSize;
     406}
     407
     408- (_WKFrameHandle *)lastPrintedFrame
     409{
     410    return _lastPrintedFrame.get();
     411}
     412
     413@end
     414
     415TEST(PDF, PrintSize)
     416{
     417    auto configuration = [[WKWebViewConfiguration new] autorelease];
     418    auto schemeHandler = [[TestURLSchemeHandler new] autorelease];
     419    [configuration setURLSchemeHandler:schemeHandler forURLScheme:@"test"];
     420    auto webView = [[[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration] autorelease];
     421    auto delegate = [[PrintUIDelegate new] autorelease];
     422    webView.UIDelegate = delegate;
     423
     424    schemeHandler.startURLSchemeTaskHandler = ^(WKWebView *, id<WKURLSchemeTask> task) {
     425        auto url = task.request.URL;
     426        NSData *data;
     427        NSString *mimeType;
     428        if ([url.path isEqualToString:@"/main.html"]) {
     429            mimeType = @"text/html";
     430            const char* html = "<br/><iframe src='test.pdf' id='pdfframe'></iframe>";
     431            data = [NSData dataWithBytes:html length:strlen(html)];
     432        } else if ([url.path isEqualToString:@"/test.pdf"]) {
     433            mimeType = @"application/pdf";
     434            data = pdfData();
     435        } else {
     436            EXPECT_WK_STREQ(url.path, "/test_print.pdf");
     437            mimeType = @"application/pdf";
     438            data = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"test_print" withExtension:@"pdf" subdirectory:@"TestWebKitAPI.resources"]];
     439        }
     440        NSURLResponse *response = [[[NSURLResponse alloc] initWithURL:url MIMEType:mimeType expectedContentLength:data.length textEncodingName:nil] autorelease];
     441        [task didReceiveResponse:response];
     442        [task didReceiveData:data];
     443        [task didFinish];
     444    };
     445
     446    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"test:///test_print.pdf"]]];
     447    auto size = [delegate waitForPageSize];
     448    EXPECT_EQ(size.height, 792.0);
     449    EXPECT_EQ(size.width, 612.0);
     450
     451    __block bool receivedSize = false;
     452    [webView _getPDFFirstPageSizeInFrame:webView._mainFrame completionHandler:^(CGSize requestedSize) {
     453        EXPECT_EQ(requestedSize.height, 792.0);
     454        EXPECT_EQ(requestedSize.width, 612.0);
     455        receivedSize = true;
     456    }];
     457    TestWebKitAPI::Util::run(&receivedSize);
     458
     459    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"test:///main.html"]]];
     460    [webView _test_waitForDidFinishNavigation];
     461    [webView evaluateJavaScript:@"window.print()" completionHandler:nil];
     462    auto mainFrameSize = [delegate waitForPageSize];
     463    EXPECT_EQ(mainFrameSize.height, 0.0);
     464    EXPECT_EQ(mainFrameSize.width, 0.0);
     465   
     466    receivedSize = false;
     467    [webView _getPDFFirstPageSizeInFrame:webView._mainFrame completionHandler:^(CGSize requestedSize) {
     468        EXPECT_EQ(requestedSize.height, 0.0);
     469        EXPECT_EQ(requestedSize.width, 0.0);
     470        receivedSize = true;
     471    }];
     472    TestWebKitAPI::Util::run(&receivedSize);
     473
     474    [webView evaluateJavaScript:@"pdfframe.contentWindow.print()" completionHandler:nil];
     475    auto pdfFrameSize = [delegate waitForPageSize];
     476    EXPECT_NEAR(pdfFrameSize.height, 28.799999, .00001);
     477    EXPECT_NEAR(pdfFrameSize.width, 129.600006, .00001);
     478
     479    receivedSize = false;
     480    [webView _getPDFFirstPageSizeInFrame:delegate.lastPrintedFrame completionHandler:^(CGSize requestedSize) {
     481        EXPECT_NEAR(requestedSize.height, 28.799999, .00001);
     482        EXPECT_NEAR(requestedSize.width, 129.600006, .00001);
     483        receivedSize = true;
     484    }];
     485    TestWebKitAPI::Util::run(&receivedSize);
     486}
     487
    375488#endif
Note: See TracChangeset for help on using the changeset viewer.