Modified: trunk/Source/WebKit/ChangeLog (275548 => 275549)
--- trunk/Source/WebKit/ChangeLog 2021-04-06 20:52:12 UTC (rev 275548)
+++ trunk/Source/WebKit/ChangeLog 2021-04-06 20:57:44 UTC (rev 275549)
@@ -1,3 +1,27 @@
+2021-04-06 Ryosuke Niwa <[email protected]>
+
+ Delay the end of printing while PrintContext is in use.
+ https://bugs.webkit.org/show_bug.cgi?id=224229
+
+ Reviewed by Antti Koivisto.
+
+ Added a new RAII object, PrintContextAccessScope, to delay the call to endPrinting
+ and deployed in various IPC messages used during printing.
+
+ * WebProcess/WebPage/WebPage.cpp:
+ (WebKit::WebPage::beginPrinting): Deployed PrintContextAccessScope.
+ (WebKit::WebPage::endPrinting): Exit early without ending printing if there is an active
+ PrintContextAccessScope in the stack.
+ (WebKit::WebPage::endPrintingImmediately): Extracted from endPrintingImmediately.
+ (WebKit::WebPage::computePagesForPrinting): Deployed PrintContextAccessScope.
+ (WebKit::WebPage::computePagesForPrintingImpl): Ditto.
+ (WebKit::WebPage::drawRectToImage): Ditto.
+ (WebKit::WebPage::drawPagesToPDF): Ditto.
+ * WebProcess/WebPage/WebPage.h:
+ (WebKit::WebPage::PrintContextAccessScope): Added.
+ (WebKit::WebPage::PrintContextAccessScope::PrintContextAccessScope): Added.
+ (WebKit::WebPage::PrintContextAccessScope::~PrintContextAccessScope): Added.
+
2021-04-06 Wenson Hsieh <[email protected]>
REGRESSION (r274610): Unable to drag images when image extraction is enabled
Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp (275548 => 275549)
--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp 2021-04-06 20:52:12 UTC (rev 275548)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp 2021-04-06 20:57:44 UTC (rev 275549)
@@ -5094,6 +5094,8 @@
void WebPage::beginPrinting(FrameIdentifier frameID, const PrintInfo& printInfo)
{
+ PrintContextAccessScope scope { *this };
+
WebFrame* frame = WebProcess::singleton().webFrame(frameID);
if (!frame)
return;
@@ -5115,6 +5117,7 @@
freezeLayerTree(LayerTreeFreezeReason::Printing);
auto computedPageSize = m_printContext->computedPageSize(FloatSize(printInfo.availablePaperWidth, printInfo.availablePaperHeight), printInfo.margin);
+
m_printContext->begin(computedPageSize.width(), computedPageSize.height());
// PrintContext::begin() performed a synchronous layout which might have executed a
@@ -5136,6 +5139,18 @@
void WebPage::endPrinting()
{
+ if (m_inActivePrintContextAccessScope) {
+ m_shouldEndPrintingImmediately = true;
+ return;
+ }
+ endPrintingImmediately();
+}
+
+void WebPage::endPrintingImmediately()
+{
+ RELEASE_ASSERT(!m_inActivePrintContextAccessScope);
+ m_shouldEndPrintingImmediately = false;
+
unfreezeLayerTree(LayerTreeFreezeReason::Printing);
if (m_printContext) {
@@ -5146,6 +5161,7 @@
void WebPage::computePagesForPrinting(FrameIdentifier frameID, const PrintInfo& printInfo, CompletionHandler<void(const Vector<WebCore::IntRect>&, double, const WebCore::FloatBoxExtent&)>&& completionHandler)
{
+ PrintContextAccessScope scope { *this };
Vector<IntRect> resultPageRects;
double resultTotalScaleFactorForPrinting = 1;
auto computedPageMargin = printInfo.margin;
@@ -5160,6 +5176,7 @@
beginPrinting(frameID, printInfo);
if (m_printContext) {
+ PrintContextAccessScope scope { *this };
resultPageRects = m_printContext->pageRects();
computedPageMargin = m_printContext->computedPageMargin(printInfo.margin);
auto computedPageSize = m_printContext->computedPageSize(FloatSize(printInfo.availablePaperWidth, printInfo.availablePaperHeight), printInfo.margin);
@@ -5206,6 +5223,7 @@
void WebPage::drawRectToImage(FrameIdentifier frameID, const PrintInfo& printInfo, const IntRect& rect, const WebCore::IntSize& imageSize, CompletionHandler<void(const WebKit::ShareableBitmap::Handle&)>&& completionHandler)
{
+ PrintContextAccessScope scope { *this };
WebFrame* frame = WebProcess::singleton().webFrame(frameID);
Frame* coreFrame = frame ? frame->coreFrame() : 0;
@@ -5256,6 +5274,7 @@
void WebPage::drawPagesToPDF(FrameIdentifier frameID, const PrintInfo& printInfo, uint32_t first, uint32_t count, CompletionHandler<void(const IPC::DataReference&)>&& callback)
{
+ PrintContextAccessScope scope { *this };
RetainPtr<CFMutableDataRef> pdfPageData;
drawPagesToPDFImpl(frameID, printInfo, first, count, pdfPageData);
callback({ CFDataGetBytePtr(pdfPageData.get()), static_cast<size_t>(CFDataGetLength(pdfPageData.get())) });
Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.h (275548 => 275549)
--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.h 2021-04-06 20:52:12 UTC (rev 275548)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.h 2021-04-06 20:57:44 UTC (rev 275549)
@@ -1659,6 +1659,8 @@
void drawPagesToPDFFromPDFDocument(CGContextRef, PDFDocument *, const PrintInfo&, uint32_t first, uint32_t count);
#endif
+ void endPrintingImmediately();
+
#if HAVE(APP_ACCENT_COLORS)
void setAccentColor(WebCore::Color);
#endif
@@ -2035,6 +2037,31 @@
#endif
std::unique_ptr<WebCore::PrintContext> m_printContext;
+ bool m_inActivePrintContextAccessScope { false };
+ bool m_shouldEndPrintingImmediately { false };
+
+ class PrintContextAccessScope {
+ public:
+ PrintContextAccessScope(WebPage& webPage)
+ : m_webPage { webPage }
+ , m_wasInActivePrintContextAccessScope { webPage.m_inActivePrintContextAccessScope }
+ {
+ m_webPage->m_inActivePrintContextAccessScope = true;
+ }
+
+ ~PrintContextAccessScope()
+ {
+ m_webPage->m_inActivePrintContextAccessScope = m_wasInActivePrintContextAccessScope;
+ if (!m_wasInActivePrintContextAccessScope && m_webPage->m_shouldEndPrintingImmediately)
+ m_webPage->endPrintingImmediately();
+ }
+ private:
+ Ref<WebPage> m_webPage;
+ const bool m_wasInActivePrintContextAccessScope;
+ };
+
+ friend class PrintContextAccessScope;
+
#if PLATFORM(GTK)
RefPtr<WebPrintOperationGtk> m_printOperation;
#endif