Use WTF::Function instead of std::function in NetworkProcess code
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 9 Jun 2017 22:38:40 +0000 (22:38 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 9 Jun 2017 22:38:40 +0000 (22:38 +0000)
https://bugs.webkit.org/show_bug.cgi?id=173182

Reviewed by Alex Christensen.

Use WTF::Function instead of std::function in NetworkProcess code to avoid
unnecessary copying.

* NetworkProcess/NetworkProcess.h:
* NetworkProcess/cache/NetworkCacheIOChannel.h:
* NetworkProcess/cache/NetworkCacheIOChannelCocoa.mm:
(WebKit::NetworkCache::IOChannel::read):
(WebKit::NetworkCache::IOChannel::write):
* NetworkProcess/cache/NetworkCacheIOChannelSoup.cpp:
(WebKit::NetworkCache::IOChannel::read):
(WebKit::NetworkCache::IOChannel::readSyncInThread):
(WebKit::NetworkCache::IOChannel::write):
* NetworkProcess/cache/NetworkCacheSpeculativeLoad.h:
* NetworkProcess/cache/NetworkCacheStatistics.h:
* NetworkProcess/cocoa/NetworkProcessCocoa.mm:
(WebKit::clearNSURLCache):
(WebKit::NetworkProcess::clearDiskCache):
* NetworkProcess/soup/NetworkProcessSoup.cpp:
(WebKit::NetworkProcess::clearDiskCache):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@218024 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit2/ChangeLog
Source/WebKit2/NetworkProcess/NetworkProcess.h
Source/WebKit2/NetworkProcess/cache/NetworkCacheIOChannel.h
Source/WebKit2/NetworkProcess/cache/NetworkCacheIOChannelCocoa.mm
Source/WebKit2/NetworkProcess/cache/NetworkCacheIOChannelSoup.cpp
Source/WebKit2/NetworkProcess/cache/NetworkCacheSpeculativeLoad.h
Source/WebKit2/NetworkProcess/cache/NetworkCacheStatistics.h
Source/WebKit2/NetworkProcess/cocoa/NetworkProcessCocoa.mm
Source/WebKit2/NetworkProcess/soup/NetworkProcessSoup.cpp

index 8ac9505..fa5c17b 100644 (file)
@@ -1,3 +1,30 @@
+2017-06-09  Chris Dumez  <cdumez@apple.com>
+
+        Use WTF::Function instead of std::function in NetworkProcess code
+        https://bugs.webkit.org/show_bug.cgi?id=173182
+
+        Reviewed by Alex Christensen.
+
+        Use WTF::Function instead of std::function in NetworkProcess code to avoid
+        unnecessary copying.
+
+        * NetworkProcess/NetworkProcess.h:
+        * NetworkProcess/cache/NetworkCacheIOChannel.h:
+        * NetworkProcess/cache/NetworkCacheIOChannelCocoa.mm:
+        (WebKit::NetworkCache::IOChannel::read):
+        (WebKit::NetworkCache::IOChannel::write):
+        * NetworkProcess/cache/NetworkCacheIOChannelSoup.cpp:
+        (WebKit::NetworkCache::IOChannel::read):
+        (WebKit::NetworkCache::IOChannel::readSyncInThread):
+        (WebKit::NetworkCache::IOChannel::write):
+        * NetworkProcess/cache/NetworkCacheSpeculativeLoad.h:
+        * NetworkProcess/cache/NetworkCacheStatistics.h:
+        * NetworkProcess/cocoa/NetworkProcessCocoa.mm:
+        (WebKit::clearNSURLCache):
+        (WebKit::NetworkProcess::clearDiskCache):
+        * NetworkProcess/soup/NetworkProcessSoup.cpp:
+        (WebKit::NetworkProcess::clearDiskCache):
+
 2017-06-09  Tim Horton  <timothy_horton@apple.com>
 
         REGRESSION (r213584): WeChat app can not switch to next text field
index f050037..290f1e6 100644 (file)
@@ -170,7 +170,7 @@ private:
     void clearCachedCredentials();
 
     // FIXME: This should take a session ID so we can identify which disk cache to delete.
-    void clearDiskCache(std::chrono::system_clock::time_point modifiedSince, std::function<void ()> completionHandler);
+    void clearDiskCache(std::chrono::system_clock::time_point modifiedSince, Function<void ()>&& completionHandler);
 
     void downloadRequest(WebCore::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedFilename);
     void resumeDownload(WebCore::SessionID, DownloadID, const IPC::DataReference& resumeData, const String& path, const SandboxExtension::Handle&);
index f19db6f..204cefd 100644 (file)
@@ -29,7 +29,7 @@
 #if ENABLE(NETWORK_CACHE)
 
 #include "NetworkCacheData.h"
-#include <functional>
+#include <wtf/Function.h>
 #include <wtf/ThreadSafeRefCounted.h>
 #include <wtf/WorkQueue.h>
 #include <wtf/text/WTFString.h>
@@ -48,8 +48,8 @@ public:
 
     // Using nullptr as queue submits the result to the main queue.
     // FIXME: We should add WorkQueue::main() instead.
-    void read(size_t offset, size_t, WorkQueue*, std::function<void (Data&, int error)>);
-    void write(size_t offset, const Data&, WorkQueue*, std::function<void (int error)>);
+    void read(size_t offset, size_t, WorkQueue*, Function<void (Data&, int error)>&&);
+    void write(size_t offset, const Data&, WorkQueue*, Function<void (int error)>&&);
 
     const String& path() const { return m_path; }
     Type type() const { return m_type; }
@@ -62,7 +62,7 @@ private:
     IOChannel(const String& filePath, IOChannel::Type);
 
 #if USE(SOUP)
-    void readSyncInThread(size_t offset, size_t, WorkQueue*, std::function<void (Data&, int error)>);
+    void readSyncInThread(size_t offset, size_t, WorkQueue*, Function<void (Data&, int error)>&&);
 #endif
 
     String m_path;
index 99f1c40..e9711bf 100644 (file)
@@ -33,6 +33,7 @@
 #include <mach/vm_param.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
+#include <wtf/BlockPtr.h>
 #include <wtf/text/CString.h>
 
 namespace WebKit {
@@ -92,12 +93,12 @@ Ref<IOChannel> IOChannel::open(const String& filePath, IOChannel::Type type)
     return adoptRef(*new IOChannel(filePath, type));
 }
 
-void IOChannel::read(size_t offset, size_t size, WorkQueue* queue, std::function<void (Data&, int error)> completionHandler)
+void IOChannel::read(size_t offset, size_t size, WorkQueue* queue, Function<void (Data&, int error)>&& completionHandler)
 {
     RefPtr<IOChannel> channel(this);
     bool didCallCompletionHandler = false;
     auto dispatchQueue = queue ? queue->dispatchQueue() : dispatch_get_main_queue();
-    dispatch_io_read(m_dispatchIO.get(), offset, size, dispatchQueue, [channel, completionHandler, didCallCompletionHandler](bool done, dispatch_data_t fileData, int error) mutable {
+    dispatch_io_read(m_dispatchIO.get(), offset, size, dispatchQueue, BlockPtr<void(bool, dispatch_data_t, int)>::fromCallable([channel, completionHandler = WTFMove(completionHandler), didCallCompletionHandler](bool done, dispatch_data_t fileData, int error) mutable {
         ASSERT_UNUSED(done, done || !didCallCompletionHandler);
         if (didCallCompletionHandler)
             return;
@@ -105,18 +106,18 @@ void IOChannel::read(size_t offset, size_t size, WorkQueue* queue, std::function
         Data data(fileDataPtr);
         completionHandler(data, error);
         didCallCompletionHandler = true;
-    });
+    }).get());
 }
 
-void IOChannel::write(size_t offset, const Data& data, WorkQueue* queue, std::function<void (int error)> completionHandler)
+void IOChannel::write(size_t offset, const Data& data, WorkQueue* queue, Function<void (int error)>&& completionHandler)
 {
     RefPtr<IOChannel> channel(this);
     auto dispatchData = data.dispatchData();
     auto dispatchQueue = queue ? queue->dispatchQueue() : dispatch_get_main_queue();
-    dispatch_io_write(m_dispatchIO.get(), offset, dispatchData, dispatchQueue, [channel, completionHandler](bool done, dispatch_data_t fileData, int error) {
+    dispatch_io_write(m_dispatchIO.get(), offset, dispatchData, dispatchQueue, BlockPtr<void(bool, dispatch_data_t, int)>::fromCallable([channel, completionHandler = WTFMove(completionHandler)](bool done, dispatch_data_t fileData, int error) {
         ASSERT_UNUSED(done, done);
         completionHandler(error);
-    });
+    }).get());
 }
 
 }
index e7cf303..3c43e87 100644 (file)
@@ -110,7 +110,7 @@ struct ReadAsyncData {
     GRefPtr<SoupBuffer> buffer;
     RefPtr<WorkQueue> queue;
     size_t bytesToRead;
-    std::function<void (Data&, int error)> completionHandler;
+    Function<void (Data&, int error)> completionHandler;
     Data data;
 };
 
@@ -153,11 +153,11 @@ static void inputStreamReadReadyCallback(GInputStream* stream, GAsyncResult* res
         reinterpret_cast<GAsyncReadyCallback>(inputStreamReadReadyCallback), asyncData.release());
 }
 
-void IOChannel::read(size_t offset, size_t size, WorkQueue* queue, std::function<void (Data&, int error)> completionHandler)
+void IOChannel::read(size_t offset, size_t size, WorkQueue* queue, Function<void (Data&, int error)>&& completionHandler)
 {
     RefPtr<IOChannel> channel(this);
     if (!m_inputStream) {
-        runTaskInQueue([channel, completionHandler] {
+        runTaskInQueue([channel, completionHandler = WTFMove(completionHandler)] {
             Data data;
             completionHandler(data, -1);
         }, queue);
@@ -165,26 +165,26 @@ void IOChannel::read(size_t offset, size_t size, WorkQueue* queue, std::function
     }
 
     if (!isMainThread()) {
-        readSyncInThread(offset, size, queue, completionHandler);
+        readSyncInThread(offset, size, queue, WTFMove(completionHandler));
         return;
     }
 
     size_t bufferSize = std::min(size, gDefaultReadBufferSize);
     uint8_t* bufferData = static_cast<uint8_t*>(fastMalloc(bufferSize));
     GRefPtr<SoupBuffer> buffer = adoptGRef(soup_buffer_new_with_owner(bufferData, bufferSize, bufferData, fastFree));
-    ReadAsyncData* asyncData = new ReadAsyncData { this, buffer.get(), queue, size, completionHandler, { } };
+    ReadAsyncData* asyncData = new ReadAsyncData { this, buffer.get(), queue, size, WTFMove(completionHandler), { } };
 
     // FIXME: implement offset.
     g_input_stream_read_async(m_inputStream.get(), const_cast<char*>(buffer->data), bufferSize, RunLoopSourcePriority::DiskCacheRead, nullptr,
         reinterpret_cast<GAsyncReadyCallback>(inputStreamReadReadyCallback), asyncData);
 }
 
-void IOChannel::readSyncInThread(size_t offset, size_t size, WorkQueue* queue, std::function<void (Data&, int error)> completionHandler)
+void IOChannel::readSyncInThread(size_t offset, size_t size, WorkQueue* queue, Function<void (Data&, int error)>&& completionHandler)
 {
     ASSERT(!isMainThread());
 
     RefPtr<IOChannel> channel(this);
-    Thread::create("IOChannel::readSync", [channel, size, queue, completionHandler] {
+    Thread::create("IOChannel::readSync", [channel, size, queue, completionHandler = WTFMove(completionHandler)] () mutable {
         size_t bufferSize = std::min(size, gDefaultReadBufferSize);
         uint8_t* bufferData = static_cast<uint8_t*>(fastMalloc(bufferSize));
         GRefPtr<SoupBuffer> readBuffer = adoptGRef(soup_buffer_new_with_owner(bufferData, bufferSize, bufferData, fastFree));
@@ -195,7 +195,7 @@ void IOChannel::readSyncInThread(size_t offset, size_t size, WorkQueue* queue, s
             // FIXME: implement offset.
             gssize bytesRead = g_input_stream_read(channel->m_inputStream.get(), const_cast<char*>(readBuffer->data), bytesToRead, nullptr, nullptr);
             if (bytesRead == -1) {
-                runTaskInQueue([channel, completionHandler] {
+                runTaskInQueue([channel, completionHandler = WTFMove(completionHandler)] {
                     Data data;
                     completionHandler(data, -1);
                 }, queue);
@@ -213,7 +213,7 @@ void IOChannel::readSyncInThread(size_t offset, size_t size, WorkQueue* queue, s
         } while (pendingBytesToRead);
 
         GRefPtr<SoupBuffer> bufferCapture = data.soupBuffer();
-        runTaskInQueue([channel, bufferCapture, completionHandler] {
+        runTaskInQueue([channel, bufferCapture, completionHandler = WTFMove(completionHandler)] {
             GRefPtr<SoupBuffer> buffer = bufferCapture;
             Data data = { WTFMove(buffer) };
             completionHandler(data, 0);
@@ -225,7 +225,7 @@ struct WriteAsyncData {
     RefPtr<IOChannel> channel;
     GRefPtr<SoupBuffer> buffer;
     RefPtr<WorkQueue> queue;
-    std::function<void (int error)> completionHandler;
+    Function<void (int error)> completionHandler;
 };
 
 static void outputStreamWriteReadyCallback(GOutputStream* stream, GAsyncResult* result, gpointer userData)
@@ -256,11 +256,11 @@ static void outputStreamWriteReadyCallback(GOutputStream* stream, GAsyncResult*
         reinterpret_cast<GAsyncReadyCallback>(outputStreamWriteReadyCallback), asyncData.release());
 }
 
-void IOChannel::write(size_t offset, const Data& data, WorkQueue* queue, std::function<void (int error)> completionHandler)
+void IOChannel::write(size_t offset, const Data& data, WorkQueue* queue, Function<void (int error)>&& completionHandler)
 {
     RefPtr<IOChannel> channel(this);
     if (!m_outputStream && !m_ioStream) {
-        runTaskInQueue([channel, completionHandler] {
+        runTaskInQueue([channel, completionHandler = WTFMove(completionHandler)] {
             completionHandler(-1);
         }, queue);
         return;
@@ -268,13 +268,13 @@ void IOChannel::write(size_t offset, const Data& data, WorkQueue* queue, std::fu
 
     GOutputStream* stream = m_outputStream ? m_outputStream.get() : g_io_stream_get_output_stream(G_IO_STREAM(m_ioStream.get()));
     if (!stream) {
-        runTaskInQueue([channel, completionHandler] {
+        runTaskInQueue([channel, completionHandler = WTFMove(completionHandler)] {
             completionHandler(-1);
         }, queue);
         return;
     }
 
-    WriteAsyncData* asyncData = new WriteAsyncData { this, data.soupBuffer(), queue, completionHandler };
+    WriteAsyncData* asyncData = new WriteAsyncData { this, data.soupBuffer(), queue, WTFMove(completionHandler) };
     // FIXME: implement offset.
     g_output_stream_write_async(stream, asyncData->buffer->data, data.size(), RunLoopSourcePriority::DiskCacheWrite, nullptr,
         reinterpret_cast<GAsyncReadyCallback>(outputStreamWriteReadyCallback), asyncData);
index a3fe906..8eb17fd 100644 (file)
@@ -44,7 +44,7 @@ namespace NetworkCache {
 class SpeculativeLoad final : public NetworkLoadClient {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    typedef std::function<void (std::unique_ptr<NetworkCache::Entry>)> RevalidationCompletionHandler;
+    typedef Function<void (std::unique_ptr<NetworkCache::Entry>)> RevalidationCompletionHandler;
     SpeculativeLoad(const GlobalFrameID&, const WebCore::ResourceRequest&, std::unique_ptr<NetworkCache::Entry>, RevalidationCompletionHandler&&);
 
     virtual ~SpeculativeLoad();
index b67c260..dc319c5 100644 (file)
@@ -66,7 +66,7 @@ private:
     void addStoreDecisionsToDatabase(const HashMap<String, NetworkCache::StoreDecision>&);
     void writeTimerFired();
 
-    typedef std::function<void (bool wasEverRequested, const std::optional<StoreDecision>&)> RequestedCompletionHandler;
+    typedef Function<void (bool wasEverRequested, const std::optional<StoreDecision>&)> RequestedCompletionHandler;
     enum class NeedUncachedReason { No, Yes };
     void queryWasEverRequested(const String&, NeedUncachedReason, RequestedCompletionHandler&&);
     void markAsRequested(const String& hash);
index 15e271d..e7ca889 100644 (file)
@@ -40,6 +40,7 @@
 #import <WebCore/SecurityOrigin.h>
 #import <WebCore/SecurityOriginData.h>
 #import <WebKitSystemInterface.h>
+#import <wtf/BlockPtr.h>
 
 namespace WebKit {
 
@@ -167,36 +168,36 @@ void NetworkProcess::clearHSTSCache(WebCore::NetworkStorageSession& session, std
     _CFNetworkResetHSTSHostsSinceDate(session.platformSession(), (__bridge CFDateRef)date);
 }
 
-static void clearNSURLCache(dispatch_group_t group, std::chrono::system_clock::time_point modifiedSince, const std::function<void ()>& completionHandler)
+static void clearNSURLCache(dispatch_group_t group, std::chrono::system_clock::time_point modifiedSince, Function<void ()>&& completionHandler)
 {
-    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), [modifiedSince, completionHandler] {
+    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), BlockPtr<void()>::fromCallable([modifiedSince, completionHandler = WTFMove(completionHandler)] () mutable {
         NSURLCache *cache = [NSURLCache sharedURLCache];
 
         NSTimeInterval timeInterval = std::chrono::duration_cast<std::chrono::duration<double>>(modifiedSince.time_since_epoch()).count();
         NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
         [cache removeCachedResponsesSinceDate:date];
 
-        dispatch_async(dispatch_get_main_queue(), [completionHandler] {
+        dispatch_async(dispatch_get_main_queue(), BlockPtr<void()>::fromCallable([completionHandler = WTFMove(completionHandler)] {
             completionHandler();
-        });
-    });
+        }).get());
+    }).get());
 }
 
-void NetworkProcess::clearDiskCache(std::chrono::system_clock::time_point modifiedSince, std::function<void ()> completionHandler)
+void NetworkProcess::clearDiskCache(std::chrono::system_clock::time_point modifiedSince, Function<void ()>&& completionHandler)
 {
     if (!m_clearCacheDispatchGroup)
         m_clearCacheDispatchGroup = dispatch_group_create();
 
 #if ENABLE(NETWORK_CACHE)
     auto group = m_clearCacheDispatchGroup;
-    dispatch_group_async(group, dispatch_get_main_queue(), [group, modifiedSince, completionHandler] {
-        NetworkCache::singleton().clear(modifiedSince, [group, modifiedSince, completionHandler] {
+    dispatch_group_async(group, dispatch_get_main_queue(), BlockPtr<void()>::fromCallable([group, modifiedSince, completionHandler = WTFMove(completionHandler)] () mutable {
+        NetworkCache::singleton().clear(modifiedSince, [group, modifiedSince, completionHandler = WTFMove(completionHandler)] () mutable {
             // FIXME: Probably not necessary.
-            clearNSURLCache(group, modifiedSince, completionHandler);
+            clearNSURLCache(group, modifiedSince, WTFMove(completionHandler));
         });
-    });
+    }).get());
 #else
-    clearNSURLCache(m_clearCacheDispatchGroup, modifiedSince, completionHandler);
+    clearNSURLCache(m_clearCacheDispatchGroup, modifiedSince, WTFMove(completionHandler));
 #endif
 }
 
index eb48e1f..3d2de91 100644 (file)
@@ -156,7 +156,7 @@ void NetworkProcess::clearCacheForAllOrigins(uint32_t cachesToClear)
     clearDiskCache(std::chrono::system_clock::time_point::min(), [] { });
 }
 
-void NetworkProcess::clearDiskCache(std::chrono::system_clock::time_point modifiedSince, std::function<void ()> completionHandler)
+void NetworkProcess::clearDiskCache(std::chrono::system_clock::time_point modifiedSince, Function<void ()>&& completionHandler)
 {
     NetworkCache::singleton().clear(modifiedSince, WTFMove(completionHandler));
 }