Title: [272592] trunk/Source/WebKit
Revision
272592
Author
[email protected]
Date
2021-02-09 10:38:58 -0800 (Tue, 09 Feb 2021)

Log Message

Use CompletionHandler instead of DrawToPDFCallback
https://bugs.webkit.org/show_bug.cgi?id=220120

Reviewed by Chris Dumez.

Along the way I made all asynchronous message replies use 8 fewer bytes by using the destination ID for the listener ID
instead of encoding 0 as the destination ID and then encoding the listener ID.  This was needed in order to make implementing
waitForAsyncCallbackAndDispatchImmediately possible, which looks at the destination IDs of incoming messages.
I also made it so that if we are just snapshotting the first page, we don't send a synchronous message because we already know the page count:
1!

* Platform/IPC/Connection.cpp:
(IPC::Connection::dispatchMessage):
* Platform/IPC/Connection.h:
(IPC::Connection::sendWithAsyncReply):
(IPC::Connection::waitForAndDispatchImmediately):
(IPC::Connection::waitForAsyncCallbackAndDispatchImmediately):
* Platform/IPC/HandleMessage.h:
(IPC::handleMessageAsync):
(IPC::handleMessageAsyncWantsConnection):
* Platform/IPC/JSIPCBinding.cpp:
(IPC::jsValueForDecodedArgumentValue):
* Platform/IPC/JSIPCBinding.h:
* Platform/IPC/MessageSender.h:
(IPC::MessageSender::sendWithAsyncReply):
* Scripts/webkit/messages.py:
* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView createPDFWithConfiguration:completionHandler:]):
* UIProcess/AuxiliaryProcessProxy.h:
(WebKit::AuxiliaryProcessProxy::sendWithAsyncReply):
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::drawToPDF):
(WebKit::WebPageProxy::getLoadDecisionForIcon):
(WebKit::WebPageProxy::drawToPDFCallback): Deleted.
* UIProcess/WebPageProxy.h:
* UIProcess/WebPageProxy.messages.in:
* UIProcess/ios/WKContentView.mm:
(-[WKContentView _processDidExit]):
(-[WKContentView _wk_pageCountForPrintFormatter:]):
(-[WKContentView _waitForDrawToPDFCallback]):
(-[WKContentView _wk_printedDocument]):
* UIProcess/ios/WebPageProxyIOS.mm:
(WebKit::WebPageProxy::computePagesForPrintingAndDrawToPDF):
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::drawToPDF):
* WebProcess/WebPage/WebPage.h:
* WebProcess/WebPage/WebPage.messages.in:
* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::computePagesForPrintingiOS):
(WebKit::WebPage::drawToPDFiOS):
(WebKit::WebPage::computePagesForPrintingAndDrawToPDF): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (272591 => 272592)


--- trunk/Source/WebKit/ChangeLog	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/ChangeLog	2021-02-09 18:38:58 UTC (rev 272592)
@@ -1,3 +1,57 @@
+2021-02-09  Alex Christensen  <[email protected]>
+
+        Use CompletionHandler instead of DrawToPDFCallback
+        https://bugs.webkit.org/show_bug.cgi?id=220120
+
+        Reviewed by Chris Dumez.
+
+        Along the way I made all asynchronous message replies use 8 fewer bytes by using the destination ID for the listener ID
+        instead of encoding 0 as the destination ID and then encoding the listener ID.  This was needed in order to make implementing
+        waitForAsyncCallbackAndDispatchImmediately possible, which looks at the destination IDs of incoming messages.
+        I also made it so that if we are just snapshotting the first page, we don't send a synchronous message because we already know the page count:
+        1!
+
+        * Platform/IPC/Connection.cpp:
+        (IPC::Connection::dispatchMessage):
+        * Platform/IPC/Connection.h:
+        (IPC::Connection::sendWithAsyncReply):
+        (IPC::Connection::waitForAndDispatchImmediately):
+        (IPC::Connection::waitForAsyncCallbackAndDispatchImmediately):
+        * Platform/IPC/HandleMessage.h:
+        (IPC::handleMessageAsync):
+        (IPC::handleMessageAsyncWantsConnection):
+        * Platform/IPC/JSIPCBinding.cpp:
+        (IPC::jsValueForDecodedArgumentValue):
+        * Platform/IPC/JSIPCBinding.h:
+        * Platform/IPC/MessageSender.h:
+        (IPC::MessageSender::sendWithAsyncReply):
+        * Scripts/webkit/messages.py:
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (-[WKWebView createPDFWithConfiguration:completionHandler:]):
+        * UIProcess/AuxiliaryProcessProxy.h:
+        (WebKit::AuxiliaryProcessProxy::sendWithAsyncReply):
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::drawToPDF):
+        (WebKit::WebPageProxy::getLoadDecisionForIcon):
+        (WebKit::WebPageProxy::drawToPDFCallback): Deleted.
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/WebPageProxy.messages.in:
+        * UIProcess/ios/WKContentView.mm:
+        (-[WKContentView _processDidExit]):
+        (-[WKContentView _wk_pageCountForPrintFormatter:]):
+        (-[WKContentView _waitForDrawToPDFCallback]):
+        (-[WKContentView _wk_printedDocument]):
+        * UIProcess/ios/WebPageProxyIOS.mm:
+        (WebKit::WebPageProxy::computePagesForPrintingAndDrawToPDF):
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::drawToPDF):
+        * WebProcess/WebPage/WebPage.h:
+        * WebProcess/WebPage/WebPage.messages.in:
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::WebPage::computePagesForPrintingiOS):
+        (WebKit::WebPage::drawToPDFiOS):
+        (WebKit::WebPage::computePagesForPrintingAndDrawToPDF): Deleted.
+
 2021-02-09  Per Arne  <[email protected]>
 
         [macOS] Deny mach-lookup to the fonts service

Modified: trunk/Source/WebKit/Platform/IPC/Connection.cpp (272591 => 272592)


--- trunk/Source/WebKit/Platform/IPC/Connection.cpp	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/Platform/IPC/Connection.cpp	2021-02-09 18:38:58 UTC (rev 272592)
@@ -1006,13 +1006,7 @@
 {
     RELEASE_ASSERT(isValid());
     if (decoder.messageReceiverName() == ReceiverName::AsyncReply) {
-        Optional<uint64_t> listenerID;
-        decoder >> listenerID;
-        if (!listenerID) {
-            ASSERT_NOT_REACHED();
-            return;
-        }
-        auto handler = takeAsyncReplyHandler(*this, *listenerID);
+        auto handler = takeAsyncReplyHandler(*this, decoder.destinationID());
         if (!handler) {
             ASSERT_NOT_REACHED();
             return;

Modified: trunk/Source/WebKit/Platform/IPC/Connection.h (272591 => 272592)


--- trunk/Source/WebKit/Platform/IPC/Connection.h	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/Platform/IPC/Connection.h	2021-02-09 18:38:58 UTC (rev 272592)
@@ -225,19 +225,20 @@
     void markCurrentlyDispatchedMessageAsInvalid();
 
     void postConnectionDidCloseOnConnectionWorkQueue();
-    template<typename T, typename C> void sendWithAsyncReply(T&& message, C&& completionHandler, uint64_t destinationID = 0, OptionSet<SendOption> = { }); // Thread-safe.
+    template<typename T, typename C> uint64_t sendWithAsyncReply(T&& message, C&& completionHandler, uint64_t destinationID = 0, OptionSet<SendOption> = { }); // Thread-safe.
     template<typename T> bool send(T&& message, uint64_t destinationID, OptionSet<SendOption> sendOptions = { }); // Thread-safe.
     // Sync senders should check the SendSyncResult for true/false in case they need to know if the result was really received.
     // Sync senders should hold on to the SendSyncResult in case they reference the contents of the reply via DataRefererence / ArrayReference.
     using SendSyncResult = std::unique_ptr<Decoder>;
     template<typename T> SendSyncResult sendSync(T&& message, typename T::Reply&& reply, uint64_t destinationID, Seconds timeout = Seconds::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { }); // Main thread only.
-    template<typename T> bool waitForAndDispatchImmediately(uint64_t destinationID, Seconds timeout, OptionSet<WaitForOption> waitForOptions = { }); // Main thread only.
-    
+    template<typename> bool waitForAndDispatchImmediately(uint64_t destinationID, Seconds timeout, OptionSet<WaitForOption> waitForOptions = { }); // Main thread only.
+    template<typename> bool waitForAsyncCallbackAndDispatchImmediately(uint64_t callbackID, Seconds timeout); // Main thread only.
+
     // Thread-safe.
     template<typename T, typename C, typename U>
-    void sendWithAsyncReply(T&& message, C&& completionHandler, ObjectIdentifier<U> destinationID = { }, OptionSet<SendOption> sendOptions = { })
+    uint64_t sendWithAsyncReply(T&& message, C&& completionHandler, ObjectIdentifier<U> destinationID = { }, OptionSet<SendOption> sendOptions = { })
     {
-        sendWithAsyncReply<T, C>(WTFMove(message), WTFMove(completionHandler), destinationID.toUInt64(), sendOptions);
+        return sendWithAsyncReply<T, C>(WTFMove(message), WTFMove(completionHandler), destinationID.toUInt64(), sendOptions);
     }
     
     // Thread-safe.
@@ -515,7 +516,7 @@
 CompletionHandler<void(Decoder*)> takeAsyncReplyHandler(Connection&, uint64_t);
 
 template<typename T, typename C>
-void Connection::sendWithAsyncReply(T&& message, C&& completionHandler, uint64_t destinationID, OptionSet<SendOption> sendOptions)
+uint64_t Connection::sendWithAsyncReply(T&& message, C&& completionHandler, uint64_t destinationID, OptionSet<SendOption> sendOptions)
 {
     COMPILE_ASSERT(!T::isSync, AsyncMessageExpected);
 
@@ -530,6 +531,7 @@
     *encoder << listenerID;
     *encoder << message.arguments();
     sendMessage(WTFMove(encoder), sendOptions);
+    return listenerID;
 }
 
 template<size_t i, typename A, typename B> struct TupleMover {
@@ -593,6 +595,24 @@
     return true;
 }
 
+template<typename T> bool Connection::waitForAsyncCallbackAndDispatchImmediately(uint64_t destinationID, Seconds timeout)
+{
+    RELEASE_ASSERT(RunLoop::isMain());
+    std::unique_ptr<Decoder> decoder = waitForMessage(T::asyncMessageReplyName(), destinationID, timeout, { });
+    if (!decoder)
+        return false;
+
+    ASSERT(decoder->messageReceiverName() == ReceiverName::AsyncReply);
+    ASSERT(decoder->destinationID() == destinationID);
+    auto handler = takeAsyncReplyHandler(*this, decoder->destinationID());
+    if (!handler) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    handler(decoder.get());
+    return true;
+}
+
 class UnboundedSynchronousIPCScope {
 public:
     UnboundedSynchronousIPCScope()

Modified: trunk/Source/WebKit/Platform/IPC/HandleMessage.h (272591 => 272592)


--- trunk/Source/WebKit/Platform/IPC/HandleMessage.h	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/Platform/IPC/HandleMessage.h	2021-02-09 18:38:58 UTC (rev 272592)
@@ -173,7 +173,7 @@
         decoder.markInvalid();
         return;
     }
-    
+
     Optional<typename CodingType<typename T::Arguments>::Type> arguments;
     decoder >> arguments;
     if (!arguments) {
@@ -182,8 +182,7 @@
     }
 
     typename T::AsyncReply completionHandler = [listenerID = *listenerID, connection = makeRef(connection)] (auto&&... args) mutable {
-        auto encoder = makeUnique<Encoder>(T::asyncMessageReplyName(), 0);
-        *encoder << listenerID;
+        auto encoder = makeUnique<Encoder>(T::asyncMessageReplyName(), listenerID);
         T::send(WTFMove(encoder), WTFMove(connection), args...);
     };
     callMemberFunction(WTFMove(*arguments), WTFMove(completionHandler), object, function);
@@ -192,13 +191,6 @@
 template<typename T, typename C, typename MF>
 void handleMessageAsyncWantsConnection(Connection& connection, Decoder& decoder, C* object, MF function)
 {
-    Optional<uint64_t> listenerID;
-    decoder >> listenerID;
-    if (!listenerID) {
-        decoder.markInvalid();
-        return;
-    }
-
     Optional<typename CodingType<typename T::Arguments>::Type> arguments;
     decoder >> arguments;
     if (!arguments) {
@@ -206,9 +198,8 @@
         return;
     }
 
-    typename T::AsyncReply completionHandler = [listenerID = *listenerID, connection = makeRef(connection)] (auto&&... args) mutable {
-        auto encoder = makeUnique<Encoder>(T::asyncMessageReplyName(), 0);
-        *encoder << listenerID;
+    typename T::AsyncReply completionHandler = [listenerID = decoder.destinationID(), connection = makeRef(connection)] (auto&&... args) mutable {
+        auto encoder = makeUnique<Encoder>(T::asyncMessageReplyName(), listenerID);
         T::send(WTFMove(encoder), WTFMove(connection), args...);
     };
     callMemberFunction(connection, WTFMove(*arguments), WTFMove(completionHandler), object, function);

Modified: trunk/Source/WebKit/Platform/IPC/JSIPCBinding.cpp (272591 => 272592)


--- trunk/Source/WebKit/Platform/IPC/JSIPCBinding.cpp	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/Platform/IPC/JSIPCBinding.cpp	2021-02-09 18:38:58 UTC (rev 272592)
@@ -157,6 +157,12 @@
     return jsValueForDecodedNumericArgumentValue(globalObject, value, "uint64_t");
 }
 
+template<>
+JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, size_t value)
+{
+    return jsValueForDecodedNumericArgumentValue(globalObject, value, "size_t");
+}
+
 template<typename RectType>
 JSC::JSValue jsValueForDecodedArgumentRect(JSC::JSGlobalObject* globalObject, const RectType& value, const String& type)
 {

Modified: trunk/Source/WebKit/Platform/IPC/JSIPCBinding.h (272591 => 272592)


--- trunk/Source/WebKit/Platform/IPC/JSIPCBinding.h	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/Platform/IPC/JSIPCBinding.h	2021-02-09 18:38:58 UTC (rev 272592)
@@ -86,6 +86,7 @@
 template<> JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject*, uint16_t);
 template<> JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject*, uint32_t);
 template<> JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject*, uint64_t);
+template<> JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject*, size_t);
 
 template<typename U>
 JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, const ObjectIdentifier<U>& value)

Modified: trunk/Source/WebKit/Platform/IPC/MessageSender.h (272591 => 272592)


--- trunk/Source/WebKit/Platform/IPC/MessageSender.h	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/Platform/IPC/MessageSender.h	2021-02-09 18:38:58 UTC (rev 272592)
@@ -80,13 +80,13 @@
     }
 
     template<typename T, typename C>
-    void sendWithAsyncReply(T&& message, C&& completionHandler, OptionSet<SendOption> sendOptions = { })
+    uint64_t sendWithAsyncReply(T&& message, C&& completionHandler, OptionSet<SendOption> sendOptions = { })
     {
-        sendWithAsyncReply(WTFMove(message), WTFMove(completionHandler), messageSenderDestinationID(), sendOptions);
+        return sendWithAsyncReply(WTFMove(message), WTFMove(completionHandler), messageSenderDestinationID(), sendOptions);
     }
 
     template<typename T, typename C>
-    void sendWithAsyncReply(T&& message, C&& completionHandler, uint64_t destinationID, OptionSet<SendOption> sendOptions = { })
+    uint64_t sendWithAsyncReply(T&& message, C&& completionHandler, uint64_t destinationID, OptionSet<SendOption> sendOptions = { })
     {
         COMPILE_ASSERT(!T::isSync, AsyncMessageExpected);
 
@@ -100,6 +100,7 @@
             else
                 T::cancelReply(WTFMove(completionHandler));
         }, listenerID }});
+        return listenerID;
     }
 
     virtual bool sendMessage(std::unique_ptr<Encoder>, OptionSet<SendOption>, Optional<std::pair<CompletionHandler<void(IPC::Decoder*)>, uint64_t>>&& = WTF::nullopt);

Modified: trunk/Source/WebKit/Scripts/webkit/messages.py (272591 => 272592)


--- trunk/Source/WebKit/Scripts/webkit/messages.py	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/Scripts/webkit/messages.py	2021-02-09 18:38:58 UTC (rev 272592)
@@ -128,6 +128,7 @@
         'int16_t',
         'int32_t',
         'int64_t',
+        'size_t',
     ])
 
     if type in builtin_types:

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm (272591 => 272592)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm	2021-02-09 18:38:58 UTC (rev 272592)
@@ -1473,9 +1473,8 @@
     if (pdfConfiguration && !CGRectIsNull(pdfConfiguration.rect))
         floatRect = WebCore::FloatRect(pdfConfiguration.rect);
 
-    auto handler = makeBlockPtr(completionHandler);
-    _page->drawToPDF(frameID, floatRect, [retainedSelf = retainPtr(self), handler = WTFMove(handler)](const IPC::DataReference& pdfData, WebKit::CallbackBase::Error error) {
-        if (error != WebKit::CallbackBase::Error::None) {
+    _page->drawToPDF(frameID, floatRect, [handler = makeBlockPtr(completionHandler)](const IPC::DataReference& pdfData) {
+        if (pdfData.isEmpty()) {
             handler(nil, createNSError(WKErrorUnknown).get());
             return;
         }

Modified: trunk/Source/WebKit/UIProcess/AuxiliaryProcessProxy.h (272591 => 272592)


--- trunk/Source/WebKit/UIProcess/AuxiliaryProcessProxy.h	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/UIProcess/AuxiliaryProcessProxy.h	2021-02-09 18:38:58 UTC (rev 272592)
@@ -57,7 +57,7 @@
     template<typename T> SendSyncResult sendSync(T&& message, typename T::Reply&&, uint64_t destinationID, Seconds timeout = 1_s, OptionSet<IPC::SendSyncOption> sendSyncOptions = { });
 
     enum class ShouldStartProcessThrottlerActivity : bool { No, Yes };
-    template<typename T, typename C> void sendWithAsyncReply(T&&, C&&, uint64_t destinationID = 0, OptionSet<IPC::SendOption> = { }, ShouldStartProcessThrottlerActivity = ShouldStartProcessThrottlerActivity::Yes);
+    template<typename T, typename C> uint64_t sendWithAsyncReply(T&&, C&&, uint64_t destinationID = 0, OptionSet<IPC::SendOption> = { }, ShouldStartProcessThrottlerActivity = ShouldStartProcessThrottlerActivity::Yes);
     
     template<typename T, typename U>
     bool send(T&& message, ObjectIdentifier<U> destinationID, OptionSet<IPC::SendOption> sendOptions = { })
@@ -182,7 +182,7 @@
 }
 
 template<typename T, typename C>
-void AuxiliaryProcessProxy::sendWithAsyncReply(T&& message, C&& completionHandler, uint64_t destinationID, OptionSet<IPC::SendOption> sendOptions, ShouldStartProcessThrottlerActivity shouldStartProcessThrottlerActivity)
+uint64_t AuxiliaryProcessProxy::sendWithAsyncReply(T&& message, C&& completionHandler, uint64_t destinationID, OptionSet<IPC::SendOption> sendOptions, ShouldStartProcessThrottlerActivity shouldStartProcessThrottlerActivity)
 {
     COMPILE_ASSERT(!T::isSync, AsyncMessageExpected);
 
@@ -196,6 +196,7 @@
         else
             T::cancelReply(WTFMove(completionHandler));
     }, listenerID }}, shouldStartProcessThrottlerActivity);
+    return listenerID;
 }
     
 } // namespace WebKit

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.cpp (272591 => 272592)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2021-02-09 18:38:58 UTC (rev 272592)
@@ -8598,23 +8598,14 @@
 #endif
 
 #if PLATFORM(COCOA)
-void WebPageProxy::drawToPDF(FrameIdentifier frameID, const Optional<FloatRect>& rect, DrawToPDFCallback::CallbackFunction&& callback)
+void WebPageProxy::drawToPDF(FrameIdentifier frameID, const Optional<FloatRect>& rect, CompletionHandler<void(const IPC::DataReference&)>&& callback)
 {
     if (!hasRunningProcess()) {
-        callback(IPC::DataReference(), CallbackBase::Error::OwnerWasInvalidated);
+        callback({ });
         return;
     }
-
-    auto callbackID = m_callbacks.put(WTFMove(callback), m_process->throttler().backgroundActivity("WebPageProxy::drawToPDF"_s));
-    send(Messages::WebPage::DrawToPDF(frameID, rect, callbackID));
+    sendWithAsyncReply(Messages::WebPage::DrawToPDF(frameID, rect), WTFMove(callback));
 }
-
-void WebPageProxy::drawToPDFCallback(const IPC::DataReference& pdfData, CallbackID callbackID)
-{
-    auto callback = m_callbacks.take<DrawToPDFCallback>(callbackID);
-    RELEASE_ASSERT(callback);
-    callback->performCallbackWithReturnValue(pdfData);
-}
 #endif // PLATFORM(COCOA)
 
 void WebPageProxy::updateBackingStoreDiscardableState()
@@ -9513,8 +9504,10 @@
             return;
         }
 
-        if (!callback)
-            return sendWithAsyncReply(Messages::WebPage::DidGetLoadDecisionForIcon(false, loadIdentifier), [](auto) { });
+        if (!callback) {
+            sendWithAsyncReply(Messages::WebPage::DidGetLoadDecisionForIcon(false, loadIdentifier), [](auto) { });
+            return;
+        }
         sendWithAsyncReply(Messages::WebPage::DidGetLoadDecisionForIcon(true, loadIdentifier), [callback = WTFMove(callback)](const IPC::DataReference& iconData) mutable {
             callback(API::Data::create(iconData).get());
         });

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.h (272591 => 272592)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.h	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.h	2021-02-09 18:38:58 UTC (rev 272592)
@@ -406,7 +406,6 @@
 #endif
 
 #if PLATFORM(COCOA)
-using DrawToPDFCallback = GenericCallback<const IPC::DataReference&>;
 typedef GenericCallback<bool, bool, String, double, double, uint64_t> NowPlayingInfoCallback;
 #endif
 
@@ -1301,10 +1300,9 @@
 #if PLATFORM(COCOA)
     void drawRectToImage(WebFrameProxy*, const PrintInfo&, const WebCore::IntRect&, const WebCore::IntSize&, Ref<ImageCallback>&&);
     void drawPagesToPDF(WebFrameProxy*, const PrintInfo&, uint32_t first, uint32_t count, Ref<DataCallback>&&);
-    void drawToPDF(WebCore::FrameIdentifier, const Optional<WebCore::FloatRect>&, DrawToPDFCallback::CallbackFunction&&);
-    void drawToPDFCallback(const IPC::DataReference& pdfData, WebKit::CallbackID);
+    void drawToPDF(WebCore::FrameIdentifier, const Optional<WebCore::FloatRect>&, CompletionHandler<void(const IPC::DataReference&)>&&);
 #if PLATFORM(IOS_FAMILY)
-    uint32_t computePagesForPrintingAndDrawToPDF(WebCore::FrameIdentifier, const PrintInfo&, DrawToPDFCallback::CallbackFunction&&);
+    std::pair<size_t, uint64_t> computePagesForPrintingAndDrawToPDF(WebCore::FrameIdentifier, const PrintInfo&, CompletionHandler<void(const IPC::DataReference&)>&&);
 #endif
 #elif PLATFORM(GTK)
     void drawPagesForPrinting(WebFrameProxy*, const PrintInfo&, Ref<PrintFinishedCallback>&&);

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.messages.in (272591 => 272592)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.messages.in	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.messages.in	2021-02-09 18:38:58 UTC (rev 272592)
@@ -191,7 +191,6 @@
     PrintFinishedCallback(WebCore::ResourceError error, WebKit::CallbackID callbackID)
 #endif
 #if PLATFORM(COCOA)
-    DrawToPDFCallback(IPC::DataReference pdfData, WebKit::CallbackID callbackID)
     NowPlayingInfoCallback(bool active, bool registeredAsNowPlayingApplication, String title, double duration, double elapsedTime, uint64_t uniqueIdentifier, WebKit::CallbackID callbackID)
 #endif
 

Modified: trunk/Source/WebKit/UIProcess/ios/WKContentView.mm (272591 => 272592)


--- trunk/Source/WebKit/UIProcess/ios/WKContentView.mm	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentView.mm	2021-02-09 18:38:58 UTC (rev 272592)
@@ -49,6 +49,7 @@
 #import "WebFrameProxy.h"
 #import "WebKit2Initialize.h"
 #import "WebPageGroup.h"
+#import "WebPageMessages.h"
 #import "WebPageProxyMessages.h"
 #import "WebProcessPool.h"
 #import "_WKFrameHandleInternal.h"
@@ -143,7 +144,7 @@
     RetainPtr<NSUndoManager> _undoManager;
     RetainPtr<WKQuirkyNSUndoManager> _quirkyUndoManager;
 
-    BOOL _isPrintingToPDF;
+    uint64_t _pdfPrintCallbackID;
     RetainPtr<CGPDFDocumentRef> _printedDocument;
 }
 
@@ -620,7 +621,7 @@
     [self _removeVisibilityPropagationView];
 #endif
 
-    _isPrintingToPDF = NO;
+    _pdfPrintCallbackID = 0;
 }
 
 #if ENABLE(GPU_PROCESS)
@@ -791,7 +792,7 @@
 
 - (NSUInteger)_wk_pageCountForPrintFormatter:(_WKWebViewPrintFormatter *)printFormatter
 {
-    if (_isPrintingToPDF)
+    if (_pdfPrintCallbackID)
         [self _waitForDrawToPDFCallback];
 
     WebCore::FrameIdentifier frameID;
@@ -824,11 +825,10 @@
     printInfo.availablePaperWidth = CGRectGetWidth(printingRect);
     printInfo.availablePaperHeight = CGRectGetHeight(printingRect);
 
-    _isPrintingToPDF = YES;
     auto retainedSelf = retainPtr(self);
-    return _page->computePagesForPrintingAndDrawToPDF(frameID, printInfo, [retainedSelf](const IPC::DataReference& pdfData, WebKit::CallbackBase::Error error) {
-        retainedSelf->_isPrintingToPDF = NO;
-        if (error != WebKit::CallbackBase::Error::None)
+    auto pair = _page->computePagesForPrintingAndDrawToPDF(frameID, printInfo, [retainedSelf](const IPC::DataReference& pdfData) {
+        retainedSelf->_pdfPrintCallbackID = 0;
+        if (pdfData.isEmpty())
             return;
 
         auto data = "" pdfData.data(), pdfData.size()));
@@ -835,19 +835,22 @@
         auto dataProvider = adoptCF(CGDataProviderCreateWithCFData(data.get()));
         retainedSelf->_printedDocument = adoptCF(CGPDFDocumentCreateWithProvider(dataProvider.get()));
     });
+    _pdfPrintCallbackID = pair.second;
+    return pair.first;
 }
 
 - (BOOL)_waitForDrawToPDFCallback
 {
-    if (!_page->process().connection()->waitForAndDispatchImmediately<Messages::WebPageProxy::DrawToPDFCallback>(_page->webPageID(), Seconds::infinity()))
+    ASSERT(_pdfPrintCallbackID);
+    if (!_page->process().connection()->waitForAsyncCallbackAndDispatchImmediately<Messages::WebPage::DrawToPDFiOS>(std::exchange(_pdfPrintCallbackID, 0), Seconds::infinity()))
         return false;
-    ASSERT(!_isPrintingToPDF);
+    ASSERT(!_pdfPrintCallbackID);
     return true;
 }
 
 - (CGPDFDocumentRef)_wk_printedDocument
 {
-    if (_isPrintingToPDF) {
+    if (_pdfPrintCallbackID) {
         if (![self _waitForDrawToPDFCallback])
             return nullptr;
     }

Modified: trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm (272591 => 272592)


--- trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm	2021-02-09 18:38:58 UTC (rev 272592)
@@ -1046,17 +1046,22 @@
     pageClient().handleSmartMagnificationInformationForPotentialTap(requestID, renderRect, fitEntireRect, viewportMinimumScale, viewportMaximumScale, nodeIsRootLevel);
 }
 
-uint32_t WebPageProxy::computePagesForPrintingAndDrawToPDF(FrameIdentifier frameID, const PrintInfo& printInfo, DrawToPDFCallback::CallbackFunction&& callback)
+std::pair<size_t, uint64_t> WebPageProxy::computePagesForPrintingAndDrawToPDF(FrameIdentifier frameID, const PrintInfo& printInfo, CompletionHandler<void(const IPC::DataReference&)>&& callback)
 {
     if (!hasRunningProcess()) {
-        callback(IPC::DataReference(), CallbackBase::Error::OwnerWasInvalidated);
-        return 0;
+        callback({ });
+        return { 0, 0 };
     }
 
-    uint32_t pageCount = 0;
-    auto callbackID = m_callbacks.put(WTFMove(callback), m_process->throttler().backgroundActivity("WebPageProxy::computePagesForPrintingAndDrawToPDF"_s));
-    sendSync(Messages::WebPage::ComputePagesForPrintingAndDrawToPDF(frameID, printInfo, callbackID), Messages::WebPage::ComputePagesForPrintingAndDrawToPDF::Reply(pageCount), Seconds::infinity());
-    return pageCount;
+    size_t pageCount = 0;
+    if (printInfo.snapshotFirstPage)
+        pageCount = 1;
+    else
+        sendSync(Messages::WebPage::ComputePagesForPrintingiOS(frameID, printInfo), Messages::WebPage::ComputePagesForPrintingiOS::Reply(pageCount), Seconds::infinity());
+
+    auto callbackID = sendWithAsyncReply(Messages::WebPage::DrawToPDFiOS(frameID, printInfo, pageCount), WTFMove(callback));
+    
+    return { pageCount, callbackID };
 }
 
 void WebPageProxy::contentSizeCategoryDidChange(const String& contentSizeCategory)

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp (272591 => 272592)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2021-02-09 18:38:58 UTC (rev 272592)
@@ -5100,7 +5100,7 @@
 }
 
 #if PLATFORM(COCOA)
-void WebPage::drawToPDF(FrameIdentifier frameID, const Optional<FloatRect>& rect, CallbackID callbackID)
+void WebPage::drawToPDF(FrameIdentifier frameID, const Optional<FloatRect>& rect, CompletionHandler<void(const IPC::DataReference&)>&& completionHandler)
 {
     auto& frameView = *m_page->mainFrame().view();
     IntSize snapshotSize;
@@ -5125,7 +5125,7 @@
     frameView.setLayoutViewportOverrideRect(originalLayoutViewportOverrideRect);
     frameView.setPaintBehavior(originalPaintBehavior);
 
-    send(Messages::WebPageProxy::DrawToPDFCallback(IPC::DataReference(CFDataGetBytePtr(pdfData.get()), CFDataGetLength(pdfData.get())), callbackID));
+    completionHandler(IPC::DataReference(CFDataGetBytePtr(pdfData.get()), CFDataGetLength(pdfData.get())));
 }
 
 void WebPage::drawRectToImage(FrameIdentifier frameID, const PrintInfo& printInfo, const IntRect& rect, const WebCore::IntSize& imageSize, CallbackID callbackID)

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.h (272591 => 272592)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.h	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.h	2021-02-09 18:38:58 UTC (rev 272592)
@@ -971,10 +971,11 @@
 #endif
 
 #if PLATFORM(IOS_FAMILY)
-    void computePagesForPrintingAndDrawToPDF(WebCore::FrameIdentifier, const PrintInfo&, CallbackID, Messages::WebPage::ComputePagesForPrintingAndDrawToPDFDelayedReply&&);
+    void computePagesForPrintingiOS(WebCore::FrameIdentifier, const PrintInfo&, Messages::WebPage::ComputePagesForPrintingiOSDelayedReply&&);
+    void drawToPDFiOS(WebCore::FrameIdentifier, const PrintInfo&, size_t, Messages::WebPage::DrawToPDFiOSAsyncReply&&);
 #endif
 
-    void drawToPDF(WebCore::FrameIdentifier, const Optional<WebCore::FloatRect>&, CallbackID);
+    void drawToPDF(WebCore::FrameIdentifier, const Optional<WebCore::FloatRect>&, CompletionHandler<void(const IPC::DataReference&)>&&);
 
 #if PLATFORM(GTK)
     void drawPagesForPrinting(WebCore::FrameIdentifier, const PrintInfo&, CallbackID);

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in (272591 => 272592)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in	2021-02-09 18:38:58 UTC (rev 272592)
@@ -402,9 +402,10 @@
     DrawRectToImage(WebCore::FrameIdentifier frameID, struct WebKit::PrintInfo printInfo, WebCore::IntRect rect, WebCore::IntSize imageSize, WebKit::CallbackID callbackID)
     DrawPagesToPDF(WebCore::FrameIdentifier frameID, struct WebKit::PrintInfo printInfo, uint32_t first, uint32_t count, WebKit::CallbackID callbackID)
 #if PLATFORM(IOS_FAMILY)
-    ComputePagesForPrintingAndDrawToPDF(WebCore::FrameIdentifier frameID, struct WebKit::PrintInfo printInfo, WebKit::CallbackID callbackID) -> (uint32_t pageCount) Synchronous
+    ComputePagesForPrintingiOS(WebCore::FrameIdentifier frameID, struct WebKit::PrintInfo printInfo) -> (size_t pageCount) Synchronous
+    DrawToPDFiOS(WebCore::FrameIdentifier frameID, struct WebKit::PrintInfo printInfo, size_t pageCount) -> (IPC::DataReference data) Async
 #endif
-    DrawToPDF(WebCore::FrameIdentifier frameID, Optional<WebCore::FloatRect> rect, WebKit::CallbackID callbackID)
+    DrawToPDF(WebCore::FrameIdentifier frameID, Optional<WebCore::FloatRect> rect) -> (IPC::DataReference data) Async
 #endif
 #if PLATFORM(GTK)
     DrawPagesForPrinting(WebCore::FrameIdentifier frameID, struct WebKit::PrintInfo printInfo, WebKit::CallbackID callbackID)

Modified: trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm (272591 => 272592)


--- trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm	2021-02-09 18:35:55 UTC (rev 272591)
+++ trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm	2021-02-09 18:38:58 UTC (rev 272592)
@@ -3971,10 +3971,22 @@
 }
 #endif
 
-void WebPage::computePagesForPrintingAndDrawToPDF(WebCore::FrameIdentifier frameID, const PrintInfo& printInfo, CallbackID callbackID, Messages::WebPage::ComputePagesForPrintingAndDrawToPDF::DelayedReply&& reply)
+void WebPage::computePagesForPrintingiOS(WebCore::FrameIdentifier frameID, const PrintInfo& printInfo, Messages::WebPage::ComputePagesForPrintingiOS::DelayedReply&& reply)
 {
+    ASSERT_WITH_MESSAGE(!printInfo.snapshotFirstPage, "If we are just snapshotting the first page, we don't need a synchronous message to determine the page count, which is 1.");
+
+    Vector<WebCore::IntRect> pageRects;
+    double totalScaleFactor;
+    auto margin = printInfo.margin;
+    computePagesForPrintingImpl(frameID, printInfo, pageRects, totalScaleFactor, margin);
+
+    ASSERT(pageRects.size() >= 1);
+    reply(pageRects.size());
+}
+
+void WebPage::drawToPDFiOS(WebCore::FrameIdentifier frameID, const PrintInfo& printInfo, size_t pageCount, Messages::WebPage::DrawToPDFiOSAsyncReply&& reply)
+{
     if (printInfo.snapshotFirstPage) {
-        reply(1);
         IntSize snapshotSize { FloatSize { printInfo.availablePaperWidth, printInfo.availablePaperHeight } };
         IntRect snapshotRect { {0, 0}, snapshotSize };
 
@@ -3985,23 +3997,13 @@
         auto pdfData = pdfSnapshotAtSize(snapshotRect, snapshotSize, 0);
 
         frameView.setLayoutViewportOverrideRect(originalLayoutViewportOverrideRect);
-        send(Messages::WebPageProxy::DrawToPDFCallback(IPC::DataReference(CFDataGetBytePtr(pdfData.get()), CFDataGetLength(pdfData.get())), callbackID));
+        reply(IPC::DataReference(CFDataGetBytePtr(pdfData.get()), CFDataGetLength(pdfData.get())));
         return;
     }
 
-    Vector<WebCore::IntRect> pageRects;
-    double totalScaleFactor;
-    auto margin = printInfo.margin;
-    computePagesForPrintingImpl(frameID, printInfo, pageRects, totalScaleFactor, margin);
-
-    ASSERT(pageRects.size() >= 1);
-    std::size_t pageCount = pageRects.size();
-    ASSERT(pageCount <= std::numeric_limits<uint32_t>::max());
-    reply(pageCount);
-
     RetainPtr<CFMutableDataRef> pdfPageData;
     drawPagesToPDFImpl(frameID, printInfo, 0, pageCount, pdfPageData);
-    send(Messages::WebPageProxy::DrawToPDFCallback(IPC::DataReference(CFDataGetBytePtr(pdfPageData.get()), CFDataGetLength(pdfPageData.get())), callbackID));
+    reply(IPC::DataReference(CFDataGetBytePtr(pdfPageData.get()), CFDataGetLength(pdfPageData.get())));
 
     endPrinting();
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to