<!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>[179708] 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/179708">179708</a></dd>
<dt>Author</dt> <dd>antti@apple.com</dd>
<dt>Date</dt> <dd>2015-02-05 14:17:22 -0800 (Thu, 05 Feb 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Switch to file backed buffer when resource is cached to disk
https://bugs.webkit.org/show_bug.cgi?id=141295

Reviewed by Chris Dumez.

Wire the DidCacheResource mechanism to the new disk cache.

* NetworkProcess/NetworkResourceLoader.cpp:
(WebKit::NetworkResourceLoader::didFinishLoading):

    Send DidCacheResource message to the web process so it can switch the resource to file backing.

* NetworkProcess/cache/NetworkCache.cpp:
(WebKit::NetworkCache::store):
(WebKit::NetworkCache::update):
* NetworkProcess/cache/NetworkCache.h:
* NetworkProcess/cache/NetworkCacheStorage.h:
(WebKit::DispatchPtr::DispatchPtr):
* NetworkProcess/cache/NetworkCacheStorageCocoa.mm:
(WebKit::NetworkCacheStorage::Data::Data):
(WebKit::mapFile):
(WebKit::decodeEntry):
(WebKit::retrieveActive):
(WebKit::NetworkCacheStorage::retrieve):
(WebKit::NetworkCacheStorage::store):

    Map files larger than a memory page after a successful store.

(WebKit::NetworkCacheStorage::update):
(WebKit::encodeEntry): Deleted.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2NetworkProcessNetworkResourceLoadercpp">trunk/Source/WebKit2/NetworkProcess/NetworkResourceLoader.cpp</a></li>
<li><a href="#trunkSourceWebKit2NetworkProcesscacheNetworkCachecpp">trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.cpp</a></li>
<li><a href="#trunkSourceWebKit2NetworkProcesscacheNetworkCacheh">trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.h</a></li>
<li><a href="#trunkSourceWebKit2NetworkProcesscacheNetworkCacheStorageh">trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.h</a></li>
<li><a href="#trunkSourceWebKit2NetworkProcesscacheNetworkCacheStorageCocoamm">trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorageCocoa.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (179707 => 179708)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2015-02-05 22:13:10 UTC (rev 179707)
+++ trunk/Source/WebKit2/ChangeLog        2015-02-05 22:17:22 UTC (rev 179708)
</span><span class="lines">@@ -1,3 +1,36 @@
</span><ins>+2015-02-05  Antti Koivisto  &lt;antti@apple.com&gt;
+
+        Switch to file backed buffer when resource is cached to disk
+        https://bugs.webkit.org/show_bug.cgi?id=141295
+
+        Reviewed by Chris Dumez.
+
+        Wire the DidCacheResource mechanism to the new disk cache.
+
+        * NetworkProcess/NetworkResourceLoader.cpp:
+        (WebKit::NetworkResourceLoader::didFinishLoading):
+
+            Send DidCacheResource message to the web process so it can switch the resource to file backing.
+
+        * NetworkProcess/cache/NetworkCache.cpp:
+        (WebKit::NetworkCache::store):
+        (WebKit::NetworkCache::update):
+        * NetworkProcess/cache/NetworkCache.h:
+        * NetworkProcess/cache/NetworkCacheStorage.h:
+        (WebKit::DispatchPtr::DispatchPtr):
+        * NetworkProcess/cache/NetworkCacheStorageCocoa.mm:
+        (WebKit::NetworkCacheStorage::Data::Data):
+        (WebKit::mapFile):
+        (WebKit::decodeEntry):
+        (WebKit::retrieveActive):
+        (WebKit::NetworkCacheStorage::retrieve):
+        (WebKit::NetworkCacheStorage::store):
+
+            Map files larger than a memory page after a successful store.
+
+        (WebKit::NetworkCacheStorage::update):
+        (WebKit::encodeEntry): Deleted.
+
</ins><span class="cx"> 2015-02-05  Chris Dumez  &lt;cdumez@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [WK2] Properly check for mmap() error case
</span></span></pre></div>
<a id="trunkSourceWebKit2NetworkProcessNetworkResourceLoadercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/NetworkProcess/NetworkResourceLoader.cpp (179707 => 179708)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/NetworkProcess/NetworkResourceLoader.cpp        2015-02-05 22:13:10 UTC (rev 179707)
+++ trunk/Source/WebKit2/NetworkProcess/NetworkResourceLoader.cpp        2015-02-05 22:17:22 UTC (rev 179708)
</span><span class="lines">@@ -336,8 +336,19 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         bool isPrivate = sessionID().isEphemeral();
</span><del>-        if (hasCacheableRedirect &amp;&amp; !isPrivate)
-            NetworkCache::singleton().store(originalRequest(), m_response, m_bufferedDataForCache.release());
</del><ins>+        if (hasCacheableRedirect &amp;&amp; !isPrivate) {
+            // Keep the connection alive.
+            RefPtr&lt;NetworkConnectionToWebProcess&gt; connection(connectionToWebProcess());
+            RefPtr&lt;NetworkResourceLoader&gt; loader(this);
+            NetworkCache::singleton().store(originalRequest(), m_response, WTF::move(m_bufferedDataForCache), [loader, connection](NetworkCache::MappedBody&amp; mappedBody) {
+#if ENABLE(SHAREABLE_RESOURCE)
+                if (mappedBody.shareableResourceHandle.isNull())
+                    return;
+                LOG(NetworkCache, &quot;(NetworkProcess) sending DidCacheResource&quot;);
+                loader-&gt;send(Messages::NetworkProcessConnection::DidCacheResource(loader-&gt;originalRequest(), mappedBody.shareableResourceHandle, loader-&gt;sessionID()));
+#endif
+            });
+        }
</ins><span class="cx">     }
</span><span class="cx"> #endif
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2NetworkProcesscacheNetworkCachecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.cpp (179707 => 179708)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.cpp        2015-02-05 22:13:10 UTC (rev 179707)
+++ trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.cpp        2015-02-05 22:17:22 UTC (rev 179708)
</span><span class="lines">@@ -281,7 +281,7 @@
</span><span class="cx">     return false;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void NetworkCache::store(const WebCore::ResourceRequest&amp; originalRequest, const WebCore::ResourceResponse&amp; response, PassRefPtr&lt;WebCore::SharedBuffer&gt; responseData)
</del><ins>+void NetworkCache::store(const WebCore::ResourceRequest&amp; originalRequest, const WebCore::ResourceResponse&amp; response, RefPtr&lt;WebCore::SharedBuffer&gt;&amp;&amp; responseData, std::function&lt;void (MappedBody&amp;)&gt; completionHandler)
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(isEnabled());
</span><span class="cx">     ASSERT(responseData);
</span><span class="lines">@@ -294,9 +294,19 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     auto key = makeCacheKey(originalRequest);
</span><del>-    auto storageEntry = encodeStorageEntry(originalRequest, response, responseData);
</del><ins>+    auto storageEntry = encodeStorageEntry(originalRequest, response, WTF::move(responseData));
</ins><span class="cx"> 
</span><del>-    m_storage-&gt;store(key, storageEntry, [](bool success) {
</del><ins>+    m_storage-&gt;store(key, storageEntry, [completionHandler](bool success, const NetworkCacheStorage::Data&amp; bodyData) {
+        MappedBody mappedBody;
+#if ENABLE(SHAREABLE_RESOURCE)
+        if (bodyData.isMap()) {
+            RefPtr&lt;SharedMemory&gt; sharedMemory = SharedMemory::createFromVMBuffer(const_cast&lt;uint8_t*&gt;(bodyData.data()), bodyData.size());
+            mappedBody.shareableResource = sharedMemory ? ShareableResource::create(WTF::move(sharedMemory), 0, bodyData.size()) : nullptr;
+            if (mappedBody.shareableResource)
+                mappedBody.shareableResource-&gt;createHandle(mappedBody.shareableResourceHandle);
+        }
+#endif
+        completionHandler(mappedBody);
</ins><span class="cx">         LOG(NetworkCache, &quot;(NetworkProcess) store success=%d&quot;, success);
</span><span class="cx">     });
</span><span class="cx"> }
</span><span class="lines">@@ -311,7 +321,7 @@
</span><span class="cx">     auto key = makeCacheKey(originalRequest);
</span><span class="cx">     auto updateEntry = encodeStorageEntry(originalRequest, response, entry.buffer);
</span><span class="cx"> 
</span><del>-    m_storage-&gt;update(key, updateEntry, entry.storageEntry, [](bool success) {
</del><ins>+    m_storage-&gt;update(key, updateEntry, entry.storageEntry, [](bool success, const NetworkCacheStorage::Data&amp;) {
</ins><span class="cx">         LOG(NetworkCache, &quot;(NetworkProcess) updated, success=%d&quot;, success);
</span><span class="cx">     });
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebKit2NetworkProcesscacheNetworkCacheh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.h (179707 => 179708)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.h        2015-02-05 22:13:10 UTC (rev 179707)
+++ trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.h        2015-02-05 22:17:22 UTC (rev 179708)
</span><span class="lines">@@ -63,7 +63,14 @@
</span><span class="cx">     };
</span><span class="cx">     // Completion handler may get called back synchronously on failure.
</span><span class="cx">     void retrieve(const WebCore::ResourceRequest&amp;, std::function&lt;void (std::unique_ptr&lt;Entry&gt;)&gt;);
</span><del>-    void store(const WebCore::ResourceRequest&amp;, const WebCore::ResourceResponse&amp;, PassRefPtr&lt;WebCore::SharedBuffer&gt;);
</del><ins>+
+    struct MappedBody {
+#if ENABLE(SHAREABLE_RESOURCE)
+        RefPtr&lt;ShareableResource&gt; shareableResource;
+        ShareableResource::Handle shareableResourceHandle;
+#endif
+    };
+    void store(const WebCore::ResourceRequest&amp;, const WebCore::ResourceResponse&amp;, RefPtr&lt;WebCore::SharedBuffer&gt;&amp;&amp;, std::function&lt;void (MappedBody&amp;)&gt;);
</ins><span class="cx">     void update(const WebCore::ResourceRequest&amp;, const Entry&amp;, const WebCore::ResourceResponse&amp; validatingResponse);
</span><span class="cx"> 
</span><span class="cx">     void clear();
</span></span></pre></div>
<a id="trunkSourceWebKit2NetworkProcesscacheNetworkCacheStorageh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.h (179707 => 179708)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.h        2015-02-05 22:13:10 UTC (rev 179707)
+++ trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.h        2015-02-05 22:17:22 UTC (rev 179708)
</span><span class="lines">@@ -59,6 +59,12 @@
</span><span class="cx">         : m_ptr(nullptr)
</span><span class="cx">     {
</span><span class="cx">     }
</span><ins>+    DispatchPtr(T ptr)
+        : m_ptr(ptr)
+    {
+        if (m_ptr)
+            dispatch_retain(m_ptr);
+    }
</ins><span class="cx">     DispatchPtr(const DispatchPtr&amp; other)
</span><span class="cx">         : m_ptr(other.m_ptr)
</span><span class="cx">     {
</span><span class="lines">@@ -137,10 +143,13 @@
</span><span class="cx">         Data body;
</span><span class="cx">     };
</span><span class="cx">     // This may call completion handler synchronously on failure.
</span><del>-    void retrieve(const NetworkCacheKey&amp;, unsigned priority, std::function&lt;bool (std::unique_ptr&lt;Entry&gt;)&gt;);
-    void store(const NetworkCacheKey&amp;, const Entry&amp;, std::function&lt;void (bool success)&gt;);
-    void update(const NetworkCacheKey&amp;, const Entry&amp; updateEntry, const Entry&amp; existingEntry, std::function&lt;void (bool success)&gt;);
</del><ins>+    typedef std::function&lt;bool (std::unique_ptr&lt;Entry&gt;)&gt; RetrieveCompletionHandler;
+    void retrieve(const NetworkCacheKey&amp;, unsigned priority, RetrieveCompletionHandler&amp;&amp;);
</ins><span class="cx"> 
</span><ins>+    typedef std::function&lt;void (bool success, const Data&amp; mappedBody)&gt; StoreCompletionHandler;
+    void store(const NetworkCacheKey&amp;, const Entry&amp;, StoreCompletionHandler&amp;&amp;);
+    void update(const NetworkCacheKey&amp;, const Entry&amp; updateEntry, const Entry&amp; existingEntry, StoreCompletionHandler&amp;&amp;);
+
</ins><span class="cx">     void setMaximumSize(size_t);
</span><span class="cx">     void clear();
</span><span class="cx"> 
</span><span class="lines">@@ -156,7 +165,7 @@
</span><span class="cx"> 
</span><span class="cx">     struct RetrieveOperation {
</span><span class="cx">         NetworkCacheKey key;
</span><del>-        std::function&lt;bool (std::unique_ptr&lt;Entry&gt;)&gt; completionHandler;
</del><ins>+        RetrieveCompletionHandler completionHandler;
</ins><span class="cx">     };
</span><span class="cx">     void dispatchRetrieveOperation(std::unique_ptr&lt;const RetrieveOperation&gt;);
</span><span class="cx">     void dispatchPendingRetrieveOperations();
</span><span class="lines">@@ -164,14 +173,14 @@
</span><span class="cx">     struct StoreOperation {
</span><span class="cx">         NetworkCacheKey key;
</span><span class="cx">         Entry entry;
</span><del>-        std::function&lt;void (bool success)&gt; completionHandler;
</del><ins>+        StoreCompletionHandler completionHandler;
</ins><span class="cx">     };
</span><span class="cx"> 
</span><span class="cx">     struct UpdateOperation {
</span><span class="cx">         NetworkCacheKey key;
</span><span class="cx">         Entry entry;
</span><span class="cx">         Entry existingEntry;
</span><del>-        std::function&lt;void (bool success)&gt; completionHandler;
</del><ins>+        StoreCompletionHandler completionHandler;
</ins><span class="cx">     };
</span><span class="cx"> 
</span><span class="cx">     const String m_directoryPath;
</span></span></pre></div>
<a id="trunkSourceWebKit2NetworkProcesscacheNetworkCacheStorageCocoamm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorageCocoa.mm (179707 => 179708)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorageCocoa.mm        2015-02-05 22:13:10 UTC (rev 179707)
+++ trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorageCocoa.mm        2015-02-05 22:17:22 UTC (rev 179708)
</span><span class="lines">@@ -81,6 +81,8 @@
</span><span class="cx"> 
</span><span class="cx"> NetworkCacheStorage::Data::Data(DispatchPtr&lt;dispatch_data_t&gt; dispatchData, Backing backing)
</span><span class="cx"> {
</span><ins>+    if (!dispatchData)
+        return;
</ins><span class="cx">     const void* data;
</span><span class="cx">     m_dispatchData = adoptDispatch(dispatch_data_create_map(dispatchData.get(), &amp;data, &amp;m_size));
</span><span class="cx">     m_data = static_cast&lt;const uint8_t*&gt;(data);
</span><span class="lines">@@ -164,7 +166,7 @@
</span><span class="cx"> 
</span><span class="cx">     switch (type) {
</span><span class="cx">     case FileOpenType::Create:
</span><del>-        oflag = O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK;
</del><ins>+        oflag = O_RDWR | O_CREAT | O_TRUNC | O_NONBLOCK;
</ins><span class="cx">         mode = S_IRUSR | S_IWUSR;
</span><span class="cx">         WebCore::makeAllDirectories(directoryPathForKey(key, cachePath));
</span><span class="cx">         break;
</span><span class="lines">@@ -250,6 +252,17 @@
</span><span class="cx">     return success;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static DispatchPtr&lt;dispatch_data_t&gt; mapFile(int fd, size_t offset, size_t size)
+{
+    void* map = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, offset);
+    if (map == MAP_FAILED)
+        return nullptr;
+    auto bodyMap = adoptDispatch(dispatch_data_create(map, size, dispatch_get_main_queue(), [map, size] {
+        munmap(map, size);
+    }));
+    return bodyMap;
+}
+
</ins><span class="cx"> static std::unique_ptr&lt;NetworkCacheStorage::Entry&gt; decodeEntry(dispatch_data_t fileData, int fd, const NetworkCacheKey&amp; key)
</span><span class="cx"> {
</span><span class="cx">     EntryMetaData metaData;
</span><span class="lines">@@ -270,17 +283,13 @@
</span><span class="cx">         LOG(NetworkCacheStorage, &quot;(NetworkProcess) header checksum mismatch&quot;);
</span><span class="cx">         return nullptr;
</span><span class="cx">     }
</span><del>-    size_t mapSize = metaData.bodySize;
-    void* map = mmap(nullptr, mapSize, PROT_READ, MAP_PRIVATE, fd, metaData.bodyOffset);
-    if (!map) {
</del><ins>+
+    auto bodyData = mapFile(fd, metaData.bodyOffset, metaData.bodySize);
+    if (!bodyData) {
</ins><span class="cx">         LOG(NetworkCacheStorage, &quot;(NetworkProcess) map failed&quot;);
</span><span class="cx">         return nullptr;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    auto bodyData = adoptDispatch(dispatch_data_create(map, metaData.bodySize, dispatch_get_main_queue(), [map, mapSize] {
-        munmap(map, mapSize);
-    }));
-
</del><span class="cx">     if (metaData.bodyChecksum != hashData(bodyData.get())) {
</span><span class="cx">         LOG(NetworkCacheStorage, &quot;(NetworkProcess) data checksum mismatch&quot;);
</span><span class="cx">         return nullptr;
</span><span class="lines">@@ -331,12 +340,6 @@
</span><span class="cx">     return adoptDispatch(dispatch_data_create_concat(headerData.get(), alignmentData.get()));
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static DispatchPtr&lt;dispatch_data_t&gt; encodeEntry(const NetworkCacheKey&amp; key, const NetworkCacheStorage::Entry&amp; entry)
-{
-    auto encodedHeader = encodeEntryHeader(key, entry);
-    return adoptDispatch(dispatch_data_create_concat(encodedHeader.get(), entry.body.dispatchData()));
-}
-
</del><span class="cx"> void NetworkCacheStorage::removeEntry(const NetworkCacheKey&amp; key)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(RunLoop::isMain());
</span><span class="lines">@@ -407,7 +410,7 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-template &lt;class T&gt; bool retrieveActive(const T&amp; operations, const NetworkCacheKey&amp; key, std::function&lt;bool (std::unique_ptr&lt;NetworkCacheStorage::Entry&gt;)&gt;&amp; completionHandler)
</del><ins>+template &lt;class T&gt; bool retrieveActive(const T&amp; operations, const NetworkCacheKey&amp; key, NetworkCacheStorage::RetrieveCompletionHandler&amp; completionHandler)
</ins><span class="cx"> {
</span><span class="cx">     for (auto&amp; operation : operations) {
</span><span class="cx">         if (operation-&gt;key == key) {
</span><span class="lines">@@ -422,7 +425,7 @@
</span><span class="cx">     return false;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void NetworkCacheStorage::retrieve(const NetworkCacheKey&amp; key, unsigned priority, std::function&lt;bool (std::unique_ptr&lt;Entry&gt;)&gt; completionHandler)
</del><ins>+void NetworkCacheStorage::retrieve(const NetworkCacheKey&amp; key, unsigned priority, RetrieveCompletionHandler&amp;&amp; completionHandler)
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(RunLoop::isMain());
</span><span class="cx">     ASSERT(priority &lt;= maximumRetrievePriority);
</span><span class="lines">@@ -438,28 +441,32 @@
</span><span class="cx">         return;
</span><span class="cx"> 
</span><span class="cx">     // Fetch from disk.
</span><del>-    m_pendingRetrieveOperationsByPriority[priority].append(std::make_unique&lt;RetrieveOperation&gt;(RetrieveOperation { key, completionHandler }));
</del><ins>+    m_pendingRetrieveOperationsByPriority[priority].append(std::make_unique&lt;RetrieveOperation&gt;(RetrieveOperation { key, WTF::move(completionHandler) }));
</ins><span class="cx">     dispatchPendingRetrieveOperations();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void NetworkCacheStorage::store(const NetworkCacheKey&amp; key, const Entry&amp; entry, std::function&lt;void (bool success)&gt; completionHandler)
</del><ins>+void NetworkCacheStorage::store(const NetworkCacheKey&amp; key, const Entry&amp; entry, StoreCompletionHandler&amp;&amp; completionHandler)
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(RunLoop::isMain());
</span><span class="cx"> 
</span><span class="cx">     m_contentsFilter.add(key.hash());
</span><span class="cx">     ++m_approximateEntryCount;
</span><span class="cx"> 
</span><del>-    auto storeOperation = std::make_unique&lt;StoreOperation&gt;(StoreOperation { key, entry, completionHandler });
</del><ins>+    auto storeOperation = std::make_unique&lt;StoreOperation&gt;(StoreOperation { key, entry, WTF::move(completionHandler) });
</ins><span class="cx">     auto&amp; store = *storeOperation;
</span><span class="cx">     m_activeStoreOperations.add(WTF::move(storeOperation));
</span><span class="cx"> 
</span><span class="cx">     StringCapture cachePathCapture(m_directoryPath);
</span><span class="cx">     dispatch_async(m_backgroundIOQueue.get(), [this, &amp;store, cachePathCapture] {
</span><del>-        auto data = encodeEntry(store.key, store.entry);
</del><ins>+        auto encodedHeader = encodeEntryHeader(store.key, store.entry);
+        auto writeData = adoptDispatch(dispatch_data_create_concat(encodedHeader.get(), store.entry.body.dispatchData()));
</ins><span class="cx"> 
</span><ins>+        size_t bodyOffset = dispatch_data_get_size(encodedHeader.get());
+        size_t bodySize = store.entry.body.size();
+
</ins><span class="cx">         int fd;
</span><span class="cx">         auto channel = openFileForKey(store.key, FileOpenType::Create, cachePathCapture.string(), fd);
</span><del>-        dispatch_io_write(channel.get(), 0, data.get(), dispatch_get_main_queue(), [this, &amp;store](bool done, dispatch_data_t, int error) {
</del><ins>+        dispatch_io_write(channel.get(), 0, writeData.get(), dispatch_get_main_queue(), [this, &amp;store, fd, bodyOffset, bodySize](bool done, dispatch_data_t, int error) {
</ins><span class="cx">             ASSERT_UNUSED(done, done);
</span><span class="cx">             LOG(NetworkCacheStorage, &quot;(NetworkProcess) write complete error=%d&quot;, error);
</span><span class="cx">             if (error) {
</span><span class="lines">@@ -469,8 +476,12 @@
</span><span class="cx">                     --m_approximateEntryCount;
</span><span class="cx">             }
</span><span class="cx"> 
</span><del>-            store.completionHandler(!error);
</del><ins>+            bool shouldMapBody = !error &amp;&amp; bodySize &gt;= vm_page_size;
+            auto bodyMap = shouldMapBody ? mapFile(fd, bodyOffset, bodySize) : nullptr;
</ins><span class="cx"> 
</span><ins>+            Data bodyData(bodyMap, Data::Backing::Map);
+            store.completionHandler(!error, bodyData);
+
</ins><span class="cx">             m_activeStoreOperations.remove(&amp;store);
</span><span class="cx">         });
</span><span class="cx">     });
</span><span class="lines">@@ -478,17 +489,17 @@
</span><span class="cx">     shrinkIfNeeded();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void NetworkCacheStorage::update(const NetworkCacheKey&amp; key, const Entry&amp; updateEntry, const Entry&amp; existingEntry, std::function&lt;void (bool success)&gt; completionHandler)
</del><ins>+void NetworkCacheStorage::update(const NetworkCacheKey&amp; key, const Entry&amp; updateEntry, const Entry&amp; existingEntry, StoreCompletionHandler&amp;&amp; completionHandler)
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(RunLoop::isMain());
</span><span class="cx"> 
</span><span class="cx">     if (!m_contentsFilter.mayContain(key.hash())) {
</span><span class="cx">         LOG(NetworkCacheStorage, &quot;(NetworkProcess) existing entry not found, storing full entry&quot;);
</span><del>-        store(key, updateEntry, completionHandler);
</del><ins>+        store(key, updateEntry, WTF::move(completionHandler));
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    auto updateOperation = std::make_unique&lt;UpdateOperation&gt;(UpdateOperation { key, updateEntry, existingEntry, completionHandler });
</del><ins>+    auto updateOperation = std::make_unique&lt;UpdateOperation&gt;(UpdateOperation { key, updateEntry, existingEntry, WTF::move(completionHandler) });
</ins><span class="cx">     auto&amp; update = *updateOperation;
</span><span class="cx">     m_activeUpdateOperations.add(WTF::move(updateOperation));
</span><span class="cx"> 
</span><span class="lines">@@ -502,7 +513,7 @@
</span><span class="cx">         if (pageRoundedHeaderSizeChanged) {
</span><span class="cx">             LOG(NetworkCacheStorage, &quot;(NetworkProcess) page-rounded header size changed, storing full entry&quot;);
</span><span class="cx">             dispatch_async(dispatch_get_main_queue(), [this, &amp;update] {
</span><del>-                store(update.key, update.entry, update.completionHandler);
</del><ins>+                store(update.key, update.entry, WTF::move(update.completionHandler));
</ins><span class="cx"> 
</span><span class="cx">                 ASSERT(m_activeUpdateOperations.contains(&amp;update));
</span><span class="cx">                 m_activeUpdateOperations.remove(&amp;update);
</span><span class="lines">@@ -519,7 +530,7 @@
</span><span class="cx">             if (error)
</span><span class="cx">                 removeEntry(update.key);
</span><span class="cx"> 
</span><del>-            update.completionHandler(!error);
</del><ins>+            update.completionHandler(!error, Data());
</ins><span class="cx"> 
</span><span class="cx">             ASSERT(m_activeUpdateOperations.contains(&amp;update));
</span><span class="cx">             m_activeUpdateOperations.remove(&amp;update);
</span></span></pre>
</div>
</div>

</body>
</html>