Title: [197721] trunk/Source
Revision
197721
Author
[email protected]
Date
2016-03-07 18:56:23 -0800 (Mon, 07 Mar 2016)

Log Message

Reduce startup and shutdown cost of resource load statistics
https://bugs.webkit.org/show_bug.cgi?id=155120
<rdar://problem/25010167>

Reviewed by Andy Estes.

Source/WebCore:

Move all file-related code out of WebCore.

* loader/ResourceLoadStatisticsStore.cpp:
(WebCore::ResourceLoadStatisticsStore::create): Deleted path overload.
(WebCore::ResourceLoadStatisticsStore::createEncoderFromData): Added.
(WebCore::ResourceLoadStatisticsStore::readDataFromDecoder): Added.
(WebCore::ResourceLoadStatisticsStore::ResourceLoadStatisticsStore): Deleted.
(WebCore::ResourceLoadStatisticsStore::writeDataToDisk): Deleted.
(WebCore::ResourceLoadStatisticsStore::setStatisticsStorageDirectory): Deleted.
(WebCore::ResourceLoadStatisticsStore::persistentStoragePath): Deleted.
(WebCore::ResourceLoadStatisticsStore::readDataFromDiskIfNeeded): Deleted.
(WebCore::ResourceLoadStatisticsStore::createDecoderFromDisk): Deleted.
(WebCore::ResourceLoadStatisticsStore::writeEncoderToDisk): Deleted.
* loader/ResourceLoadStatisticsStore.h:
(WebCore::ResourceLoadStatisticsStore::clear): Added.

Source/WebKit/mac:

Remove the Resource Load Statistics stuff from WK1, now that it is up and
running in WK2.

* WebView/WebView.mm:
(-[WebView _preferencesChanged:]): Remove call to read resource load
statistics from disk.
(+[WebView _applicationWillTerminate]): Remove call to write resource load
statistics to disk.

Source/WebKit2:

Use a dedicated WorkQueue to process resource load statistics data. Allow
processing to load (and save) previously stored statistics asynchronously so
that it does not delay startup.
        
Now that we have a more rational storage situation, get rid of the 'writeToDisk'
method since it is no longer needed in the WK2 layer.

Move all of the Resource Load Statistics file handling code from WebCore to this
API layer.

* UIProcess/WebResourceLoadStatisticsStore.cpp:
(WebKit::WebResourceLoadStatisticsStore::WebResourceLoadStatisticsStore): Initialize
member variable with storage path, rather than passing to WebCore code.
(WebKit::WebResourceLoadStatisticsStore::resourceLoadStatisticsUpdated): Revised to
use the new WorkQueue code.
(WebKit::WebResourceLoadStatisticsStore::setResourceLoadStatisticsEnabled): When
activating the feature, purge any old statistics before loading from disk.
(WebKit::WebResourceLoadStatisticsStore::readDataFromDiskIfNeeded): Added.
(WebKit::WebResourceLoadStatisticsStore::processWillOpenConnection): Added.
(WebKit::WebResourceLoadStatisticsStore::processDidCloseConnection): Added.
(WebKit::WebResourceLoadStatisticsStore::applicationWillTerminate): Make sure all
of the WorkQueue tasks are done before terminating.
(WebKit::WebResourceLoadStatisticsStore::persistentStoragePath): Moved from WebCore.
(WebKit::WebResourceLoadStatisticsStore::writeEncoderToDisk): Ditto.
(WebKit::WebResourceLoadStatisticsStore::createDecoderFromDisk): Ditto.
(WebKit::WebResourceLoadStatisticsStore::writeToDisk): Deleted.
* UIProcess/WebResourceLoadStatisticsStore.h:
(WebKit::WebResourceLoadStatisticsStore::coreStore):
* UIProcess/WebsiteData/WebsiteDataStore.cpp:
(WebKit::WebsiteDataStore::webProcessWillOpenConnection): Call new WebResourceLoadStatisticsStore code.
(WebKit::WebsiteDataStore::webProcessDidCloseConnection): Ditto.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (197720 => 197721)


--- trunk/Source/WebCore/ChangeLog	2016-03-08 02:40:12 UTC (rev 197720)
+++ trunk/Source/WebCore/ChangeLog	2016-03-08 02:56:23 UTC (rev 197721)
@@ -1,3 +1,27 @@
+2016-03-07  Brent Fulgham  <[email protected]>
+
+        Reduce startup and shutdown cost of resource load statistics
+        https://bugs.webkit.org/show_bug.cgi?id=155120
+        <rdar://problem/25010167>
+
+        Reviewed by Andy Estes.
+
+        Move all file-related code out of WebCore.
+
+        * loader/ResourceLoadStatisticsStore.cpp:
+        (WebCore::ResourceLoadStatisticsStore::create): Deleted path overload.
+        (WebCore::ResourceLoadStatisticsStore::createEncoderFromData): Added.
+        (WebCore::ResourceLoadStatisticsStore::readDataFromDecoder): Added.
+        (WebCore::ResourceLoadStatisticsStore::ResourceLoadStatisticsStore): Deleted.
+        (WebCore::ResourceLoadStatisticsStore::writeDataToDisk): Deleted.
+        (WebCore::ResourceLoadStatisticsStore::setStatisticsStorageDirectory): Deleted.
+        (WebCore::ResourceLoadStatisticsStore::persistentStoragePath): Deleted.
+        (WebCore::ResourceLoadStatisticsStore::readDataFromDiskIfNeeded): Deleted.
+        (WebCore::ResourceLoadStatisticsStore::createDecoderFromDisk): Deleted.
+        (WebCore::ResourceLoadStatisticsStore::writeEncoderToDisk): Deleted.
+        * loader/ResourceLoadStatisticsStore.h:
+        (WebCore::ResourceLoadStatisticsStore::clear): Added.
+
 2016-03-07  Zalan Bujtas  <[email protected]>
 
         Crash in WebCore::RenderElement::containingBlockForObjectInFlow

Modified: trunk/Source/WebCore/loader/ResourceLoadObserver.cpp (197720 => 197721)


--- trunk/Source/WebCore/loader/ResourceLoadObserver.cpp	2016-03-08 02:40:12 UTC (rev 197720)
+++ trunk/Source/WebCore/loader/ResourceLoadObserver.cpp	2016-03-08 02:56:23 UTC (rev 197721)
@@ -62,6 +62,9 @@
     if (!Settings::resourceLoadStatisticsEnabled())
         return;
 
+    if (!m_store)
+        return;
+
     ASSERT(frame.document());
     ASSERT(topFrame.document());
     ASSERT(topFrame.page());
@@ -148,6 +151,9 @@
     if (!Settings::resourceLoadStatisticsEnabled())
         return;
 
+    if (!m_store)
+        return;
+
     bool needPrivacy = (frame && frame->page()) ? frame->page()->usesEphemeralSession() : false;
     if (needPrivacy)
         return;
@@ -208,6 +214,9 @@
     if (!Settings::resourceLoadStatisticsEnabled())
         return;
 
+    if (!m_store)
+        return;
+
     bool needPrivacy = document.page() ? document.page()->usesEphemeralSession() : false;
     if (needPrivacy)
         return;
@@ -261,7 +270,7 @@
 
 String ResourceLoadObserver::statisticsForOrigin(const String& origin)
 {
-    return m_store->statisticsForOrigin(origin);
+    return m_store ? m_store->statisticsForOrigin(origin) : emptyString();
 }
 
 }

Modified: trunk/Source/WebCore/loader/ResourceLoadStatisticsStore.cpp (197720 => 197721)


--- trunk/Source/WebCore/loader/ResourceLoadStatisticsStore.cpp	2016-03-08 02:40:12 UTC (rev 197720)
+++ trunk/Source/WebCore/loader/ResourceLoadStatisticsStore.cpp	2016-03-08 02:56:23 UTC (rev 197721)
@@ -41,21 +41,11 @@
 
 namespace WebCore {
 
-Ref<ResourceLoadStatisticsStore> ResourceLoadStatisticsStore::create(const String& resourceLoadStatisticsDirectory)
-{
-    return adoptRef(*new ResourceLoadStatisticsStore(resourceLoadStatisticsDirectory));
-}
-
 Ref<ResourceLoadStatisticsStore> ResourceLoadStatisticsStore::create()
 {
     return adoptRef(*new ResourceLoadStatisticsStore());
 }
     
-ResourceLoadStatisticsStore::ResourceLoadStatisticsStore(const String& resourceLoadStatisticsDirectory)
-    : m_storagePath(resourceLoadStatisticsDirectory)
-{
-}
-    
 bool ResourceLoadStatisticsStore::isPrevalentResource(const String& primaryDomain) const
 {
     auto mapEntry = m_resourceStatisticsMap.find(primaryDomain);
@@ -76,43 +66,24 @@
 
 typedef HashMap<String, ResourceLoadStatistics>::KeyValuePairType StatisticsValue;
 
-void ResourceLoadStatisticsStore::writeDataToDisk()
+std::unique_ptr<KeyedEncoder> ResourceLoadStatisticsStore::createEncoderFromData()
 {
     auto encoder = KeyedEncoder::encoder();
     
     encoder->encodeObjects("browsingStatistics", m_resourceStatisticsMap.begin(), m_resourceStatisticsMap.end(), [this](KeyedEncoder& encoderInner, const StatisticsValue& origin) {
         origin.value.encode(encoderInner);
     });
-    
-    writeEncoderToDisk(*encoder.get(), "full_browsing_session");
-}
 
-void ResourceLoadStatisticsStore::setStatisticsStorageDirectory(const String& path)
-{
-    m_storagePath = path;
-    readDataFromDiskIfNeeded();
+    return encoder;
 }
 
-String ResourceLoadStatisticsStore::persistentStoragePath(const String& label) const
+void ResourceLoadStatisticsStore::readDataFromDecoder(KeyedDecoder& decoder)
 {
-    if (m_storagePath.isEmpty())
-        return emptyString();
-
-    // TODO Decide what to call this file
-    return pathByAppendingComponent(m_storagePath, label + "_resourceLog.plist");
-}
-
-void ResourceLoadStatisticsStore::readDataFromDiskIfNeeded()
-{
     if (m_resourceStatisticsMap.size())
         return;
 
-    auto decoder = createDecoderFromDisk("full_browsing_session");
-    if (!decoder)
-        return;
-
     Vector<ResourceLoadStatistics> loadedStatistics;
-    bool succeeded = decoder->decodeObjects("browsingStatistics", loadedStatistics, [this](KeyedDecoder& decoderInner, ResourceLoadStatistics& statistics) {
+    bool succeeded = decoder.decodeObjects("browsingStatistics", loadedStatistics, [this](KeyedDecoder& decoderInner, ResourceLoadStatistics& statistics) {
         return statistics.decode(decoderInner);
     });
 
@@ -123,48 +94,6 @@
         m_resourceStatisticsMap.set(statistics.highLevelDomain, statistics);
 }
 
-std::unique_ptr<KeyedDecoder> ResourceLoadStatisticsStore::createDecoderFromDisk(const String& label) const
-{
-    String resourceLog = persistentStoragePath(label);
-    if (resourceLog.isEmpty())
-        return nullptr;
-    
-    RefPtr<SharedBuffer> rawData = SharedBuffer::createWithContentsOfFile(resourceLog);
-    if (!rawData)
-        return nullptr;
-    
-    return KeyedDecoder::decoder(reinterpret_cast<const uint8_t*>(rawData->data()), rawData->size());
-}
-    
-void ResourceLoadStatisticsStore::writeEncoderToDisk(KeyedEncoder& encoder, const String& label) const
-{
-#if LOG_STATISTICS_TO_FILE
-    RefPtr<SharedBuffer> rawData = encoder.finishEncoding();
-    if (!rawData)
-        return;
-    
-    String resourceLog = persistentStoragePath(label);
-    if (resourceLog.isEmpty())
-        return;
-
-    if (!m_storagePath.isEmpty())
-        makeAllDirectories(m_storagePath);
-
-    auto handle = openFile(resourceLog, OpenForWrite);
-    if (!handle)
-        return;
-    
-    int64_t writtenBytes = writeToFile(handle, rawData->data(), rawData->size());
-    closeFile(handle);
-    
-    if (writtenBytes != static_cast<int64_t>(rawData->size()))
-        WTFLogAlways("ResourceLoadStatistics: We only wrote %lld out of %d bytes to disk", writtenBytes, rawData->size());
-#else
-    UNUSED_PARAM(encoder);
-    UNUSED_PARAM(label);
-#endif
-}
-
 String ResourceLoadStatisticsStore::statisticsForOrigin(const String& origin)
 {
     auto iter = m_resourceStatisticsMap.find(origin);

Modified: trunk/Source/WebCore/loader/ResourceLoadStatisticsStore.h (197720 => 197721)


--- trunk/Source/WebCore/loader/ResourceLoadStatisticsStore.h	2016-03-08 02:40:12 UTC (rev 197720)
+++ trunk/Source/WebCore/loader/ResourceLoadStatisticsStore.h	2016-03-08 02:56:23 UTC (rev 197721)
@@ -38,17 +38,16 @@
 
 class ResourceLoadStatisticsStore : public RefCounted<ResourceLoadStatisticsStore> {
 public:
-    WEBCORE_EXPORT static Ref<ResourceLoadStatisticsStore> create(const String& resourceLoadStatisticsDirectory);
     WEBCORE_EXPORT static Ref<ResourceLoadStatisticsStore> create();
 
-    WEBCORE_EXPORT void writeDataToDisk();
-    WEBCORE_EXPORT void readDataFromDiskIfNeeded();
-    WEBCORE_EXPORT void setStatisticsStorageDirectory(const String&);
+    WEBCORE_EXPORT std::unique_ptr<KeyedEncoder> createEncoderFromData();
+    WEBCORE_EXPORT void readDataFromDecoder(KeyedDecoder&);
 
     WEBCORE_EXPORT String statisticsForOrigin(const String&);
 
     bool isEmpty() const { return m_resourceStatisticsMap.isEmpty(); }
     size_t size() const { return m_resourceStatisticsMap.size(); }
+    void clear() { m_resourceStatisticsMap.clear(); }
 
     ResourceLoadStatistics& resourceStatisticsForPrimaryDomain(const String&);
 
@@ -63,14 +62,8 @@
 
 private:
     ResourceLoadStatisticsStore() = default;
-    ResourceLoadStatisticsStore(const String& resourceLoadStatisticsDirectory);
-    String persistentStoragePath(const String& label) const;
 
-    std::unique_ptr<KeyedDecoder> createDecoderFromDisk(const String& label) const;
-    void writeEncoderToDisk(KeyedEncoder&, const String& label) const;
-
     HashMap<String, ResourceLoadStatistics> m_resourceStatisticsMap;
-    String m_storagePath;
     std::function<void()> m_dataAddedHandler;
 };
     

Modified: trunk/Source/WebKit/mac/ChangeLog (197720 => 197721)


--- trunk/Source/WebKit/mac/ChangeLog	2016-03-08 02:40:12 UTC (rev 197720)
+++ trunk/Source/WebKit/mac/ChangeLog	2016-03-08 02:56:23 UTC (rev 197721)
@@ -1,3 +1,20 @@
+2016-03-07  Brent Fulgham  <[email protected]>
+
+        Reduce startup and shutdown cost of resource load statistics
+        https://bugs.webkit.org/show_bug.cgi?id=155120
+        <rdar://problem/25010167>
+
+        Reviewed by Andy Estes.
+
+        Remove the Resource Load Statistics stuff from WK1, now that it is up and
+        running in WK2.
+
+        * WebView/WebView.mm:
+        (-[WebView _preferencesChanged:]): Remove call to read resource load
+        statistics from disk.
+        (+[WebView _applicationWillTerminate]): Remove call to write resource load
+        statistics to disk.
+
 2016-03-06  Andreas Kling  <[email protected]>
 
         Reduce page cache capacity from 3 to 2.

Modified: trunk/Source/WebKit/mac/WebView/WebView.mm (197720 => 197721)


--- trunk/Source/WebKit/mac/WebView/WebView.mm	2016-03-08 02:40:12 UTC (rev 197720)
+++ trunk/Source/WebKit/mac/WebView/WebView.mm	2016-03-08 02:56:23 UTC (rev 197721)
@@ -759,15 +759,7 @@
     if (initialized)
         return;
     
-    NSString *appName = [[NSBundle mainBundle] bundleIdentifier];
-    if (!appName)
-        appName = [[NSProcessInfo processInfo] processName];
-    
-    ASSERT(appName);
-    
-    NSString *supportDirectory = [NSString _webkit_localStorageDirectoryWithBundleIdentifier:appName];
-
-    resourceLoadStatisticsStore = &WebCore::ResourceLoadStatisticsStore::create(supportDirectory).leakRef();
+    resourceLoadStatisticsStore = &WebCore::ResourceLoadStatisticsStore::create().leakRef();
     ResourceLoadObserver::sharedObserver().setStatisticsStore(*resourceLoadStatisticsStore);
     
     initialized = YES;
@@ -2480,8 +2472,6 @@
     settings.setHiddenPageCSSAnimationSuspensionEnabled([preferences hiddenPageCSSAnimationSuspensionEnabled]);
 
     settings.setResourceLoadStatisticsEnabled([preferences resourceLoadStatisticsEnabled]);
-    if (resourceLoadStatisticsStore)
-        resourceLoadStatisticsStore->readDataFromDiskIfNeeded();
 
 #if ENABLE(GAMEPAD)
     RuntimeEnabledFeatures::sharedFeatures().setGamepadsEnabled([preferences gamepadsEnabled]);
@@ -4851,9 +4841,6 @@
 {   
     applicationIsTerminating = YES;
 
-    if (resourceLoadStatisticsStore)
-        resourceLoadStatisticsStore->writeDataToDisk();
-
     if (fastDocumentTeardownEnabled())
         [self closeAllWebViews];
 

Modified: trunk/Source/WebKit2/ChangeLog (197720 => 197721)


--- trunk/Source/WebKit2/ChangeLog	2016-03-08 02:40:12 UTC (rev 197720)
+++ trunk/Source/WebKit2/ChangeLog	2016-03-08 02:56:23 UTC (rev 197721)
@@ -1,3 +1,43 @@
+2016-03-07  Brent Fulgham  <[email protected]>
+
+        Reduce startup and shutdown cost of resource load statistics
+        https://bugs.webkit.org/show_bug.cgi?id=155120
+        <rdar://problem/25010167>
+
+        Reviewed by Andy Estes.
+
+        Use a dedicated WorkQueue to process resource load statistics data. Allow
+        processing to load (and save) previously stored statistics asynchronously so
+        that it does not delay startup.
+        
+        Now that we have a more rational storage situation, get rid of the 'writeToDisk'
+        method since it is no longer needed in the WK2 layer.
+
+        Move all of the Resource Load Statistics file handling code from WebCore to this
+        API layer.
+
+        * UIProcess/WebResourceLoadStatisticsStore.cpp:
+        (WebKit::WebResourceLoadStatisticsStore::WebResourceLoadStatisticsStore): Initialize
+        member variable with storage path, rather than passing to WebCore code.
+        (WebKit::WebResourceLoadStatisticsStore::resourceLoadStatisticsUpdated): Revised to
+        use the new WorkQueue code.
+        (WebKit::WebResourceLoadStatisticsStore::setResourceLoadStatisticsEnabled): When
+        activating the feature, purge any old statistics before loading from disk.
+        (WebKit::WebResourceLoadStatisticsStore::readDataFromDiskIfNeeded): Added.
+        (WebKit::WebResourceLoadStatisticsStore::processWillOpenConnection): Added.
+        (WebKit::WebResourceLoadStatisticsStore::processDidCloseConnection): Added.
+        (WebKit::WebResourceLoadStatisticsStore::applicationWillTerminate): Make sure all
+        of the WorkQueue tasks are done before terminating.
+        (WebKit::WebResourceLoadStatisticsStore::persistentStoragePath): Moved from WebCore.
+        (WebKit::WebResourceLoadStatisticsStore::writeEncoderToDisk): Ditto.
+        (WebKit::WebResourceLoadStatisticsStore::createDecoderFromDisk): Ditto.
+        (WebKit::WebResourceLoadStatisticsStore::writeToDisk): Deleted.
+        * UIProcess/WebResourceLoadStatisticsStore.h:
+        (WebKit::WebResourceLoadStatisticsStore::coreStore):
+        * UIProcess/WebsiteData/WebsiteDataStore.cpp:
+        (WebKit::WebsiteDataStore::webProcessWillOpenConnection): Call new WebResourceLoadStatisticsStore code.
+        (WebKit::WebsiteDataStore::webProcessDidCloseConnection): Ditto.
+
 2016-03-07  Alex Christensen  <[email protected]>
 
         Fix cookies with private browsing and NetworkSession

Modified: trunk/Source/WebKit2/UIProcess/WebResourceLoadStatisticsStore.cpp (197720 => 197721)


--- trunk/Source/WebKit2/UIProcess/WebResourceLoadStatisticsStore.cpp	2016-03-08 02:40:12 UTC (rev 197720)
+++ trunk/Source/WebKit2/UIProcess/WebResourceLoadStatisticsStore.cpp	2016-03-08 02:56:23 UTC (rev 197721)
@@ -29,7 +29,9 @@
 #include "WebProcessMessages.h"
 #include "WebProcessPool.h"
 #include "WebResourceLoadStatisticsStoreMessages.h"
+#include <WebCore/KeyedCoding.h>
 #include <WebCore/ResourceLoadStatisticsStore.h>
+#include <wtf/threads/BinarySemaphore.h>
 
 using namespace WebCore;
 
@@ -41,7 +43,9 @@
 }
 
 WebResourceLoadStatisticsStore::WebResourceLoadStatisticsStore(const String& resourceLoadStatisticsDirectory)
-    : m_resourceStatisticsStore(WebCore::ResourceLoadStatisticsStore::create(resourceLoadStatisticsDirectory))
+    : m_resourceStatisticsStore(WebCore::ResourceLoadStatisticsStore::create())
+    , m_statisticsQueue(WorkQueue::create("WebResourceLoadStatisticsStore Process Data Queue"))
+    , m_storagePath(resourceLoadStatisticsDirectory)
 {
 }
 
@@ -52,6 +56,11 @@
 void WebResourceLoadStatisticsStore::resourceLoadStatisticsUpdated(const Vector<WebCore::ResourceLoadStatistics>& origins)
 {
     coreStore().mergeStatistics(origins);
+    // TODO: Analyze statistics to recognize prevalent domains. <rdar://problem/24913272>
+    // TODO: Notify individual WebProcesses of prevalent domains. <rdar://problem/24703099>
+    auto encoder = coreStore().createEncoderFromData();
+    
+    writeEncoderToDisk(*encoder.get(), "full_browsing_session");
 }
 
 void WebResourceLoadStatisticsStore::setResourceLoadStatisticsEnabled(bool enabled)
@@ -74,24 +83,82 @@
     if (!m_resourceLoadStatisticsEnabled)
         return;
 
-    coreStore().readDataFromDiskIfNeeded();
+    RefPtr<WebResourceLoadStatisticsStore> self(this);
+    m_statisticsQueue->dispatch([self] {
+        self->coreStore().clear();
+
+        auto decoder = self->createDecoderFromDisk("full_browsing_session");
+        if (!decoder)
+            return;
+
+        self->coreStore().readDataFromDecoder(*decoder);
+    });
 }
 
-void WebResourceLoadStatisticsStore::writeToDisk()
+void WebResourceLoadStatisticsStore::processWillOpenConnection(WebProcessProxy&, IPC::Connection& connection)
 {
-    if (!m_resourceLoadStatisticsEnabled)
-        return;
-    
-    coreStore().writeDataToDisk();
+    connection.addWorkQueueMessageReceiver(Messages::WebResourceLoadStatisticsStore::messageReceiverName(), &m_statisticsQueue.get(), this);
 }
 
+void WebResourceLoadStatisticsStore::processDidCloseConnection(WebProcessProxy&, IPC::Connection& connection)
+{
+    connection.removeWorkQueueMessageReceiver(Messages::WebResourceLoadStatisticsStore::messageReceiverName());
+}
+
 void WebResourceLoadStatisticsStore::applicationWillTerminate()
 {
-    if (!m_resourceLoadStatisticsEnabled)
+    BinarySemaphore semaphore;
+    m_statisticsQueue->dispatch([this, &semaphore] {
+        // Make sure any ongoing work in our queue is finished before we terminate.
+        semaphore.signal();
+    });
+    semaphore.wait(std::numeric_limits<double>::max());
+}
+
+String WebResourceLoadStatisticsStore::persistentStoragePath(const String& label) const
+{
+    if (m_storagePath.isEmpty())
+        return emptyString();
+    
+    // TODO Decide what to call this file
+    return pathByAppendingComponent(m_storagePath, label + "_resourceLog.plist");
+}
+
+void WebResourceLoadStatisticsStore::writeEncoderToDisk(KeyedEncoder& encoder, const String& label) const
+{
+    RefPtr<SharedBuffer> rawData = encoder.finishEncoding();
+    if (!rawData)
         return;
+    
+    String resourceLog = persistentStoragePath(label);
+    if (resourceLog.isEmpty())
+        return;
+    
+    if (!m_storagePath.isEmpty())
+        makeAllDirectories(m_storagePath);
+    
+    auto handle = openFile(resourceLog, OpenForWrite);
+    if (!handle)
+        return;
+    
+    int64_t writtenBytes = writeToFile(handle, rawData->data(), rawData->size());
+    closeFile(handle);
+    
+    if (writtenBytes != static_cast<int64_t>(rawData->size()))
+        WTFLogAlways("WebResourceLoadStatisticsStore: We only wrote %d out of %d bytes to disk", static_cast<unsigned>(writtenBytes), rawData->size());
+}
 
-    // FIXME(154642): TEMPORARY CODE: This should not be done in one long operation when exiting.
-    writeToDisk();
+std::unique_ptr<KeyedDecoder> WebResourceLoadStatisticsStore::createDecoderFromDisk(const String& label) const
+{
+    String resourceLog = persistentStoragePath(label);
+    if (resourceLog.isEmpty())
+        return nullptr;
+    
+    RefPtr<SharedBuffer> rawData = SharedBuffer::createWithContentsOfFile(resourceLog);
+    if (!rawData)
+        return nullptr;
+    
+    return KeyedDecoder::decoder(reinterpret_cast<const uint8_t*>(rawData->data()), rawData->size());
 }
 
 } // namespace WebKit

Modified: trunk/Source/WebKit2/UIProcess/WebResourceLoadStatisticsStore.h (197720 => 197721)


--- trunk/Source/WebKit2/UIProcess/WebResourceLoadStatisticsStore.h	2016-03-08 02:40:12 UTC (rev 197720)
+++ trunk/Source/WebKit2/UIProcess/WebResourceLoadStatisticsStore.h	2016-03-08 02:56:23 UTC (rev 197721)
@@ -33,13 +33,20 @@
 #include <wtf/Vector.h>
 #include <wtf/text/WTFString.h>
 
+namespace WTF {
+class WorkQueue;
+}
+
 namespace WebCore {
+class KeyedDecoder;
+class KeyedEncoder;
 struct ResourceLoadStatistics;
 }
 
 namespace WebKit {
 
 class WebProcessPool;
+class WebProcessProxy;
 
 class WebResourceLoadStatisticsStore : public IPC::Connection::WorkQueueMessageReceiver {
 public:
@@ -51,22 +58,31 @@
     
     void resourceLoadStatisticsUpdated(const Vector<WebCore::ResourceLoadStatistics>& origins);
 
+    void processWillOpenConnection(WebProcessProxy&, IPC::Connection&);
+    void processDidCloseConnection(WebProcessProxy&, IPC::Connection&);
     void applicationWillTerminate();
 
-    void writeToDisk();
     void readDataFromDiskIfNeeded();
 
     void mergeStatistics(const Vector<WebCore::ResourceLoadStatistics>&);
 
     WebCore::ResourceLoadStatisticsStore& coreStore() { return m_resourceStatisticsStore.get(); }
-    
+    const WebCore::ResourceLoadStatisticsStore& coreStore() const { return m_resourceStatisticsStore.get(); }
+
 private:
     explicit WebResourceLoadStatisticsStore(const String&);
-    
+
+    String persistentStoragePath(const String& label) const;
+
     // IPC::MessageReceiver
     void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
-    
+
+    void writeEncoderToDisk(WebCore::KeyedEncoder&, const String& label) const;
+    std::unique_ptr<WebCore::KeyedDecoder> createDecoderFromDisk(const String& label) const;
+
     Ref<WebCore::ResourceLoadStatisticsStore> m_resourceStatisticsStore;
+    Ref<WTF::WorkQueue> m_statisticsQueue;
+    String m_storagePath;
     bool m_resourceLoadStatisticsEnabled { false };
 };
 

Modified: trunk/Source/WebKit2/UIProcess/WebsiteData/WebsiteDataStore.cpp (197720 => 197721)


--- trunk/Source/WebKit2/UIProcess/WebsiteData/WebsiteDataStore.cpp	2016-03-08 02:40:12 UTC (rev 197720)
+++ trunk/Source/WebKit2/UIProcess/WebsiteData/WebsiteDataStore.cpp	2016-03-08 02:56:23 UTC (rev 197721)
@@ -981,7 +981,8 @@
     if (m_storageManager)
         m_storageManager->processWillOpenConnection(webProcessProxy, connection);
 
-    connection.addWorkQueueMessageReceiver(Messages::WebResourceLoadStatisticsStore::messageReceiverName(), &m_queue.get(), m_resourceLoadStatistics.get());
+    if (m_resourceLoadStatistics)
+        m_resourceLoadStatistics->processWillOpenConnection(webProcessProxy, connection);
 }
 
 void WebsiteDataStore::webPageWillOpenConnection(WebPageProxy& webPageProxy, IPC::Connection& connection)
@@ -998,7 +999,8 @@
 
 void WebsiteDataStore::webProcessDidCloseConnection(WebProcessProxy& webProcessProxy, IPC::Connection& connection)
 {
-    connection.removeWorkQueueMessageReceiver(Messages::WebResourceLoadStatisticsStore::messageReceiverName());
+    if (m_resourceLoadStatistics)
+        m_resourceLoadStatistics->processDidCloseConnection(webProcessProxy, connection);
 
     if (m_storageManager)
         m_storageManager->processDidCloseConnection(webProcessProxy, connection);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to