<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[214406] trunk/Source/WebKit2</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/214406">214406</a></dd>
<dt>Author</dt> <dd>aestes@apple.com</dd>
<dt>Date</dt> <dd>2017-03-25 17:53:24 -0700 (Sat, 25 Mar 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>[iOS] Use snapshotting instead of printing to draw single-page PDFs
https://bugs.webkit.org/show_bug.cgi?id=170103
&lt;rdar://problem/30542960&gt;

Reviewed by Tim Horton.

* WebProcess/WebPage/WebPage.cpp:
(WebKit::paintSnapshotAtSize): Moved the painting logic from WebPage::snapshotAtSize() to here.
(WebKit::WebPage::snapshotAtSize): Changed to call paintSnapshotAtSize() with the
WebImage's graphics context.
(WebKit::WebPage::pdfSnapshotAtSize): Created a CGPDFGraphicsContext, passed it to
paintSnapshotAtSize(), and returned the context's data.
* WebProcess/WebPage/WebPage.h: Made snapshotAtSize() and snapshotNode() private, changed
their return values from PassRefPtr to RefPtr, and declared pdfSnapshotAtSize().
* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::computePagesForPrintingAndDrawToPDF): If snapshotting the first page,
returned a page count of 1 and created a PDF using pdfSnapshotAtSize().</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2WebProcessWebPageWebPagecpp">trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp</a></li>
<li><a href="#trunkSourceWebKit2WebProcessWebPageWebPageh">trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h</a></li>
<li><a href="#trunkSourceWebKit2WebProcessWebPageiosWebPageIOSmm">trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (214405 => 214406)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2017-03-25 23:08:37 UTC (rev 214405)
+++ trunk/Source/WebKit2/ChangeLog        2017-03-26 00:53:24 UTC (rev 214406)
</span><span class="lines">@@ -1,3 +1,23 @@
</span><ins>+2017-03-25  Andy Estes  &lt;aestes@apple.com&gt;
+
+        [iOS] Use snapshotting instead of printing to draw single-page PDFs
+        https://bugs.webkit.org/show_bug.cgi?id=170103
+        &lt;rdar://problem/30542960&gt;
+
+        Reviewed by Tim Horton.
+
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::paintSnapshotAtSize): Moved the painting logic from WebPage::snapshotAtSize() to here.
+        (WebKit::WebPage::snapshotAtSize): Changed to call paintSnapshotAtSize() with the
+        WebImage's graphics context.
+        (WebKit::WebPage::pdfSnapshotAtSize): Created a CGPDFGraphicsContext, passed it to
+        paintSnapshotAtSize(), and returned the context's data.
+        * WebProcess/WebPage/WebPage.h: Made snapshotAtSize() and snapshotNode() private, changed
+        their return values from PassRefPtr to RefPtr, and declared pdfSnapshotAtSize().
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::WebPage::computePagesForPrintingAndDrawToPDF): If snapshotting the first page,
+        returned a page count of 1 and created a PDF using pdfSnapshotAtSize().
+
</ins><span class="cx"> 2017-03-25  John Wilander  &lt;wilander@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Re-enable the web process' keychain access to fix client certificate authentication
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessWebPageWebPagecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp (214405 => 214406)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp        2017-03-25 23:08:37 UTC (rev 214405)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp        2017-03-26 00:53:24 UTC (rev 214406)
</span><span class="lines">@@ -1945,42 +1945,30 @@
</span><span class="cx">     return snapshotAtSize(rect, bitmapSize, options);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-PassRefPtr&lt;WebImage&gt; WebPage::snapshotAtSize(const IntRect&amp; rect, const IntSize&amp; bitmapSize, SnapshotOptions options)
</del><ins>+static void paintSnapshotAtSize(const IntRect&amp; rect, const IntSize&amp; bitmapSize, SnapshotOptions options, Frame&amp; frame, FrameView&amp; frameView, GraphicsContext&amp; graphicsContext)
</ins><span class="cx"> {
</span><del>-    Frame* coreFrame = m_mainFrame-&gt;coreFrame();
-    if (!coreFrame)
-        return nullptr;
-
-    FrameView* frameView = coreFrame-&gt;view();
-    if (!frameView)
-        return nullptr;
-
</del><span class="cx">     IntRect snapshotRect = rect;
</span><span class="cx">     float horizontalScaleFactor = static_cast&lt;float&gt;(bitmapSize.width()) / rect.width();
</span><span class="cx">     float verticalScaleFactor = static_cast&lt;float&gt;(bitmapSize.height()) / rect.height();
</span><span class="cx">     float scaleFactor = std::max(horizontalScaleFactor, verticalScaleFactor);
</span><span class="cx"> 
</span><del>-    auto snapshot = WebImage::create(bitmapSize, snapshotOptionsToImageOptions(options));
-
-    auto graphicsContext = snapshot-&gt;bitmap().createGraphicsContext();
-
</del><span class="cx">     if (options &amp; SnapshotOptionsPrinting) {
</span><del>-        PrintContext::spoolAllPagesWithBoundaries(*coreFrame, *graphicsContext, snapshotRect.size());
-        return snapshot;
</del><ins>+        PrintContext::spoolAllPagesWithBoundaries(frame, graphicsContext, snapshotRect.size());
+        return;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    Color documentBackgroundColor = frameView-&gt;documentBackgroundColor();
-    Color backgroundColor = (coreFrame-&gt;settings().backgroundShouldExtendBeyondPage() &amp;&amp; documentBackgroundColor.isValid()) ? documentBackgroundColor : frameView-&gt;baseBackgroundColor();
-    graphicsContext-&gt;fillRect(IntRect(IntPoint(), bitmapSize), backgroundColor);
</del><ins>+    Color documentBackgroundColor = frameView.documentBackgroundColor();
+    Color backgroundColor = (frame.settings().backgroundShouldExtendBeyondPage() &amp;&amp; documentBackgroundColor.isValid()) ? documentBackgroundColor : frameView.baseBackgroundColor();
+    graphicsContext.fillRect(IntRect(IntPoint(), bitmapSize), backgroundColor);
</ins><span class="cx"> 
</span><span class="cx">     if (!(options &amp; SnapshotOptionsExcludeDeviceScaleFactor)) {
</span><del>-        double deviceScaleFactor = corePage()-&gt;deviceScaleFactor();
-        graphicsContext-&gt;applyDeviceScaleFactor(deviceScaleFactor);
</del><ins>+        double deviceScaleFactor = frame.page()-&gt;deviceScaleFactor();
+        graphicsContext.applyDeviceScaleFactor(deviceScaleFactor);
</ins><span class="cx">         scaleFactor /= deviceScaleFactor;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    graphicsContext-&gt;scale(scaleFactor);
-    graphicsContext-&gt;translate(-snapshotRect.x(), -snapshotRect.y());
</del><ins>+    graphicsContext.scale(scaleFactor);
+    graphicsContext.translate(-snapshotRect.x(), -snapshotRect.y());
</ins><span class="cx"> 
</span><span class="cx">     FrameView::SelectionInSnapshot shouldPaintSelection = FrameView::IncludeSelection;
</span><span class="cx">     if (options &amp; SnapshotOptionsExcludeSelectionHighlighting)
</span><span class="lines">@@ -1990,18 +1978,35 @@
</span><span class="cx">     if (options &amp; SnapshotOptionsInViewCoordinates)
</span><span class="cx">         coordinateSpace = FrameView::ViewCoordinates;
</span><span class="cx"> 
</span><del>-    frameView-&gt;paintContentsForSnapshot(*graphicsContext, snapshotRect, shouldPaintSelection, coordinateSpace);
</del><ins>+    frameView.paintContentsForSnapshot(graphicsContext, snapshotRect, shouldPaintSelection, coordinateSpace);
</ins><span class="cx"> 
</span><span class="cx">     if (options &amp; SnapshotOptionsPaintSelectionRectangle) {
</span><del>-        FloatRect selectionRectangle = m_mainFrame-&gt;coreFrame()-&gt;selection().selectionBounds();
-        graphicsContext-&gt;setStrokeColor(Color(0xFF, 0, 0));
-        graphicsContext-&gt;strokeRect(selectionRectangle, 1);
</del><ins>+        FloatRect selectionRectangle = frame.selection().selectionBounds();
+        graphicsContext.setStrokeColor(Color(0xFF, 0, 0));
+        graphicsContext.strokeRect(selectionRectangle, 1);
</ins><span class="cx">     }
</span><del>-    
</del><ins>+}
+
+RefPtr&lt;WebImage&gt; WebPage::snapshotAtSize(const IntRect&amp; rect, const IntSize&amp; bitmapSize, SnapshotOptions options)
+{
+    Frame* coreFrame = m_mainFrame-&gt;coreFrame();
+    if (!coreFrame)
+        return nullptr;
+
+    FrameView* frameView = coreFrame-&gt;view();
+    if (!frameView)
+        return nullptr;
+
+    auto snapshot = WebImage::create(bitmapSize, snapshotOptionsToImageOptions(options));
+    auto graphicsContext = snapshot-&gt;bitmap().createGraphicsContext();
+
+    paintSnapshotAtSize(rect, bitmapSize, options, *coreFrame, *frameView, *graphicsContext);
+
</ins><span class="cx">     return snapshot;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-PassRefPtr&lt;WebImage&gt; WebPage::snapshotNode(WebCore::Node&amp; node, SnapshotOptions options, unsigned maximumPixelCount)
</del><ins>+#if USE(CF)
+RetainPtr&lt;CFDataRef&gt; WebPage::pdfSnapshotAtSize(const IntRect&amp; rect, const IntSize&amp; bitmapSize, SnapshotOptions options)
</ins><span class="cx"> {
</span><span class="cx">     Frame* coreFrame = m_mainFrame-&gt;coreFrame();
</span><span class="cx">     if (!coreFrame)
</span><span class="lines">@@ -2011,6 +2016,38 @@
</span><span class="cx">     if (!frameView)
</span><span class="cx">         return nullptr;
</span><span class="cx"> 
</span><ins>+    auto data = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, 0));
+
+#if USE(CG)
+    auto dataConsumer = adoptCF(CGDataConsumerCreateWithCFData(data.get()));
+    auto mediaBox = CGRectMake(0, 0, bitmapSize.width(), bitmapSize.height());
+    auto pdfContext = adoptCF(CGPDFContextCreate(dataConsumer.get(), &amp;mediaBox, nullptr));
+
+    CGPDFContextBeginPage(pdfContext.get(), nullptr);
+
+    GraphicsContext graphicsContext { pdfContext.get() };
+    graphicsContext.scale({ 1, -1 });
+    graphicsContext.translate(0, -bitmapSize.height());
+    paintSnapshotAtSize(rect, bitmapSize, options, *coreFrame, *frameView, graphicsContext);
+
+    CGPDFContextEndPage(pdfContext.get());
+    CGPDFContextClose(pdfContext.get());
+#endif
+
+    return WTFMove(data);
+}
+#endif
+
+RefPtr&lt;WebImage&gt; WebPage::snapshotNode(WebCore::Node&amp; node, SnapshotOptions options, unsigned maximumPixelCount)
+{
+    Frame* coreFrame = m_mainFrame-&gt;coreFrame();
+    if (!coreFrame)
+        return nullptr;
+
+    FrameView* frameView = coreFrame-&gt;view();
+    if (!frameView)
+        return nullptr;
+
</ins><span class="cx">     if (!node.renderer())
</span><span class="cx">         return nullptr;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessWebPageWebPageh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h (214405 => 214406)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h        2017-03-25 23:08:37 UTC (rev 214405)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h        2017-03-26 00:53:24 UTC (rev 214406)
</span><span class="lines">@@ -490,8 +490,6 @@
</span><span class="cx"> #endif
</span><span class="cx">     
</span><span class="cx">     PassRefPtr&lt;WebImage&gt; scaledSnapshotWithOptions(const WebCore::IntRect&amp;, double additionalScaleFactor, SnapshotOptions);
</span><del>-    PassRefPtr&lt;WebImage&gt; snapshotAtSize(const WebCore::IntRect&amp;, const WebCore::IntSize&amp; bitmapSize, SnapshotOptions);
-    PassRefPtr&lt;WebImage&gt; snapshotNode(WebCore::Node&amp;, SnapshotOptions, unsigned maximumPixelCount = std::numeric_limits&lt;unsigned&gt;::max());
</del><span class="cx"> 
</span><span class="cx">     static const WebEvent* currentEvent();
</span><span class="cx"> 
</span><span class="lines">@@ -1274,6 +1272,12 @@
</span><span class="cx">     void urlSchemeHandlerTaskDidReceiveData(uint64_t handlerIdentifier, uint64_t taskIdentifier, const IPC::DataReference&amp;);
</span><span class="cx">     void urlSchemeHandlerTaskDidComplete(uint64_t handlerIdentifier, uint64_t taskIdentifier, const WebCore::ResourceError&amp;);
</span><span class="cx"> 
</span><ins>+    RefPtr&lt;WebImage&gt; snapshotAtSize(const WebCore::IntRect&amp;, const WebCore::IntSize&amp; bitmapSize, SnapshotOptions);
+    RefPtr&lt;WebImage&gt; snapshotNode(WebCore::Node&amp;, SnapshotOptions, unsigned maximumPixelCount = std::numeric_limits&lt;unsigned&gt;::max());
+#if USE(CF)
+    RetainPtr&lt;CFDataRef&gt; pdfSnapshotAtSize(const WebCore::IntRect&amp;, const WebCore::IntSize&amp; bitmapSize, SnapshotOptions);
+#endif
+
</ins><span class="cx">     uint64_t m_pageID;
</span><span class="cx"> 
</span><span class="cx">     std::unique_ptr&lt;WebCore::Page&gt; m_page;
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessWebPageiosWebPageIOSmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm (214405 => 214406)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm        2017-03-25 23:08:37 UTC (rev 214405)
+++ trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm        2017-03-26 00:53:24 UTC (rev 214406)
</span><span class="lines">@@ -3252,12 +3252,21 @@
</span><span class="cx"> 
</span><span class="cx"> void WebPage::computePagesForPrintingAndDrawToPDF(uint64_t frameID, const PrintInfo&amp; printInfo, uint64_t callbackID, PassRefPtr&lt;Messages::WebPage::ComputePagesForPrintingAndDrawToPDF::DelayedReply&gt; reply)
</span><span class="cx"> {
</span><ins>+    if (printInfo.snapshotFirstPage) {
+        reply-&gt;send(1);
+        IntSize snapshotSize { FloatSize { printInfo.availablePaperWidth, printInfo.availablePaperHeight } };
+        IntRect snapshotRect { {0, 0}, snapshotSize };
+        auto pdfData = pdfSnapshotAtSize(snapshotRect, snapshotSize, 0);
+        send(Messages::WebPageProxy::DrawToPDFCallback(IPC::DataReference(CFDataGetBytePtr(pdfData.get()), CFDataGetLength(pdfData.get())), callbackID));
+        return;
+    }
+
</ins><span class="cx">     Vector&lt;WebCore::IntRect&gt; pageRects;
</span><span class="cx">     double totalScaleFactor;
</span><span class="cx">     computePagesForPrintingImpl(frameID, printInfo, pageRects, totalScaleFactor);
</span><span class="cx"> 
</span><span class="cx">     ASSERT(pageRects.size() &gt;= 1);
</span><del>-    std::size_t pageCount = printInfo.snapshotFirstPage ? 1 : pageRects.size();
</del><ins>+    std::size_t pageCount = pageRects.size();
</ins><span class="cx">     ASSERT(pageCount &lt;= std::numeric_limits&lt;uint32_t&gt;::max());
</span><span class="cx">     reply-&gt;send(pageCount);
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>