Changeset 223558 in webkit


Ignore:
Timestamp:
Oct 17, 2017 11:21:37 AM (6 years ago)
Author:
commit-queue@webkit.org
Message:

Cache API implementation should be able to compute storage size for WebKit client applications.
https://bugs.webkit.org/show_bug.cgi?id=178350

Patch by Youenn Fablet <youenn@apple.com> on 2017-10-17
Reviewed by Chris Dumez.

Source/WebCore:

  • page/SecurityOriginData.h:

(WebCore::SecurityOriginData::equals const):

Source/WebKit:

When gathering data from DOM Cache, we compute the size as follows:

  • If Caches object is not persistent, size is zero
  • If Caches object is persistent, we use the size computed by NetworkCache::Storage.

Covered by updated tests.

  • NetworkProcess/cache/CacheStorageEngine.cpp:

(WebKit::CacheStorage::ReadOriginsTaskCounter::create):
(WebKit::CacheStorage::ReadOriginsTaskCounter::addOrigin):
(WebKit::CacheStorage::Engine::fetchEntries):

  • NetworkProcess/cache/CacheStorageEngineCaches.cpp:

(WebKit::CacheStorage::Caches::storageSize const):

  • NetworkProcess/cache/CacheStorageEngineCaches.h:
  • UIProcess/API/C/WKWebsiteDataStoreRef.cpp:

(WKWebsiteDataStoreGetFetchCacheSizeForOrigin):

  • UIProcess/API/C/WKWebsiteDataStoreRef.h:

Tools:

Adding support for a domCacheSize getter.

  • WebKitTestRunner/InjectedBundle/Bindings/TestRunner.idl:
  • WebKitTestRunner/InjectedBundle/TestRunner.cpp:

(WTR::TestRunner::domCacheSize):

  • WebKitTestRunner/InjectedBundle/TestRunner.h:
  • WebKitTestRunner/TestController.cpp:

(WTR::FetchCacheSizeForOriginCallbackContext::FetchCacheSizeForOriginCallbackContext):
(WTR::fetchCacheSizeForOriginCallback):
(WTR::TestController::domCacheSize):

  • WebKitTestRunner/TestController.h:
  • WebKitTestRunner/TestInvocation.cpp:

(WTR::TestInvocation::didReceiveSynchronousMessageFromInjectedBundle):

LayoutTests:

  • http/tests/cache-storage/cache-clearing-origin.https.html:
  • http/tests/cache-storage/cache-representation.https.html:
Location:
trunk
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r223557 r223558  
     12017-10-17  Youenn Fablet  <youenn@apple.com>
     2
     3        Cache API implementation should be able to compute storage size for WebKit client applications.
     4        https://bugs.webkit.org/show_bug.cgi?id=178350
     5
     6        Reviewed by Chris Dumez.
     7
     8        * http/tests/cache-storage/cache-clearing-origin.https.html:
     9        * http/tests/cache-storage/cache-representation.https.html:
     10
    1112017-10-17  Ryan Haddad  <ryanhaddad@apple.com>
    212
  • trunk/LayoutTests/http/tests/cache-storage/cache-clearing-origin.https.html

    r223299 r223558  
    3131    var keys = await self.caches.keys();
    3232    assert_not_equals(keys.length, 0, "keys should not be empty");
     33    assert_true(testRunner.domCacheSize(window.location.origin) > 0, "Actual origin cache size is not zero");
    3334
    3435    testRunner.clearDOMCache(window.location.origin);
    3536    assert_false(testRunner.hasDOMCache(window.location.origin), "Actual origin cache is cleared");
     37    assert_equals(testRunner.domCacheSize(window.location.origin), 0, "Actual origin cache size is zero");
    3638
    3739    keys = await self.caches.keys();
  • trunk/LayoutTests/http/tests/cache-storage/cache-representation.https.html

    r221710 r223558  
    1919            assert_equals(!!caches["persistent"].length, hasPersistent, "persistent");
    2020            assert_equals(!!caches["removed"].length, hasRemoved, "removed");
     21
     22           if (!hasPersistent)
     23               assert_equals(testRunner.domCacheSize(results.origin), 0, "non persistent cache storage size is zero");
     24
    2125        }, name);
    2226    }
  • trunk/Source/WebCore/ChangeLog

    r223553 r223558  
     12017-10-17  Youenn Fablet  <youenn@apple.com>
     2
     3        Cache API implementation should be able to compute storage size for WebKit client applications.
     4        https://bugs.webkit.org/show_bug.cgi?id=178350
     5
     6        Reviewed by Chris Dumez.
     7
     8        * page/SecurityOriginData.h:
     9        (WebCore::SecurityOriginData::equals const):
     10
    1112017-10-17  Daniel Bates  <dabates@apple.com>
    212
  • trunk/Source/WebKit/ChangeLog

    r223476 r223558  
     12017-10-17  Youenn Fablet  <youenn@apple.com>
     2
     3        Cache API implementation should be able to compute storage size for WebKit client applications.
     4        https://bugs.webkit.org/show_bug.cgi?id=178350
     5
     6        Reviewed by Chris Dumez.
     7
     8        When gathering data from DOM Cache, we compute the size as follows:
     9        - If Caches object is not persistent, size is zero
     10        - If Caches object is persistent, we use the size computed by NetworkCache::Storage.
     11        Covered by updated tests.
     12
     13        * NetworkProcess/cache/CacheStorageEngine.cpp:
     14        (WebKit::CacheStorage::ReadOriginsTaskCounter::create):
     15        (WebKit::CacheStorage::ReadOriginsTaskCounter::addOrigin):
     16        (WebKit::CacheStorage::Engine::fetchEntries):
     17        * NetworkProcess/cache/CacheStorageEngineCaches.cpp:
     18        (WebKit::CacheStorage::Caches::storageSize const):
     19        * NetworkProcess/cache/CacheStorageEngineCaches.h:
     20        * UIProcess/API/C/WKWebsiteDataStoreRef.cpp:
     21        (WKWebsiteDataStoreGetFetchCacheSizeForOrigin):
     22        * UIProcess/API/C/WKWebsiteDataStoreRef.h:
     23
    1242017-10-17  Keith Miller  <keith_miller@apple.com>
    225
  • trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngine.cpp

    r223299 r223558  
    349349    static Ref<ReadOriginsTaskCounter> create(WTF::CompletionHandler<void(Vector<WebsiteData::Entry>)>&& callback)
    350350    {
    351         return adoptRef(*new ReadOriginsTaskCounter(WTFMove(callback)));}
     351        return adoptRef(*new ReadOriginsTaskCounter(WTFMove(callback)));
     352    }
    352353
    353354    ~ReadOriginsTaskCounter()
     
    356357    }
    357358
    358     void addOrigin(WebCore::SecurityOriginData&& origin)
     359    void addOrigin(WebCore::SecurityOriginData&& origin, uint64_t size)
    359360    {
    360         m_entries.append(WebsiteData::Entry { WTFMove(origin), WebsiteDataType::DOMCache, 0 });
     361        m_entries.append(WebsiteData::Entry { WTFMove(origin), WebsiteDataType::DOMCache, size });
    361362    }
    362363
     
    371372};
    372373
    373 void Engine::fetchEntries(bool /* shouldComputeSize */, WTF::CompletionHandler<void(Vector<WebsiteData::Entry>)>&& completionHandler)
     374void Engine::fetchEntries(bool shouldComputeSize, WTF::CompletionHandler<void(Vector<WebsiteData::Entry>)>&& completionHandler)
    374375{
    375376    if (!shouldPersist()) {
     
    385386        if (!WebCore::fileIsDirectory(folderPath, WebCore::ShouldFollowSymbolicLinks::No))
    386387            continue;
    387         Caches::retrieveOriginFromDirectory(folderPath, *m_ioQueue, [taskCounter = taskCounter.copyRef()] (std::optional<WebCore::SecurityOriginData>&& origin) mutable {
     388        Caches::retrieveOriginFromDirectory(folderPath, *m_ioQueue, [protectedThis = makeRef(*this), shouldComputeSize, taskCounter = taskCounter.copyRef()] (std::optional<WebCore::SecurityOriginData>&& origin) mutable {
    388389            ASSERT(RunLoop::isMain());
    389             if (origin)
    390                 taskCounter->addOrigin(WTFMove(origin.value()));
     390            if (!origin)
     391                return;
     392
     393            if (!shouldComputeSize) {
     394                taskCounter->addOrigin(WTFMove(origin.value()), 0);
     395                return;
     396            }
     397
     398            auto cacheOrigin = origin->securityOrigin()->toString();
     399            protectedThis->readCachesFromDisk(cacheOrigin, [origin = WTFMove(origin.value()), taskCounter = WTFMove(taskCounter)] (CachesOrError&& result) mutable {
     400                if (!result.hasValue())
     401                    return;
     402                taskCounter->addOrigin(WTFMove(origin), result.value().get().storageSize());
     403
     404            });
    391405        });
    392406    }
  • trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngineCaches.cpp

    r223299 r223558  
    531531}
    532532
     533uint64_t Caches::storageSize() const
     534{
     535    ASSERT(m_isInitialized);
     536    if (!shouldPersist())
     537        return 0;
     538    return m_storage->approximateSize();
     539}
     540
    533541} // namespace CacheStorage
    534542
  • trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngineCaches.h

    r223299 r223558  
    7474    void clearMemoryRepresentation();
    7575
     76    uint64_t storageSize() const;
     77
    7678private:
    7779    Caches(Engine&, String&& origin, String&& rootPath, uint64_t quota);
  • trunk/Source/WebKit/UIProcess/API/C/WKWebsiteDataStoreRef.cpp

    r223299 r223558  
    3030#include "APIWebsiteDataStore.h"
    3131#include "WKAPICast.h"
     32#include "WKSecurityOriginRef.h"
    3233#include "WebResourceLoadStatisticsStore.h"
    3334#include "WebResourceLoadStatisticsTelemetry.h"
    3435#include "WebsiteData.h"
     36#include "WebsiteDataFetchOption.h"
    3537#include "WebsiteDataRecord.h"
    3638#include "WebsiteDataType.h"
     
    369371    });
    370372}
     373
     374void WKWebsiteDataStoreGetFetchCacheSizeForOrigin(WKWebsiteDataStoreRef dataStoreRef, WKStringRef origin, void* context, WKWebsiteDataStoreGetFetchCacheSizeForOriginFunction callback)
     375{
     376    OptionSet<WebKit::WebsiteDataFetchOption> fetchOptions = WebKit::WebsiteDataFetchOption::ComputeSizes;
     377
     378    WebKit::toImpl(dataStoreRef)->websiteDataStore().fetchData(WebKit::WebsiteDataType::DOMCache, fetchOptions, [origin, context, callback] (auto dataRecords) {
     379        auto originData = WebCore::SecurityOriginData::fromSecurityOrigin(WebCore::SecurityOrigin::createFromString(WebKit::toImpl(origin)->string()));
     380        for (auto& dataRecord : dataRecords) {
     381            for (const auto& recordOrigin : dataRecord.origins) {
     382                if (originData == recordOrigin) {
     383                    callback(dataRecord.size ? dataRecord.size->totalSize : 0, context);
     384                    return;
     385                }
     386
     387            }
     388        }
     389        callback(0, context);
     390    });
     391}
  • trunk/Source/WebKit/UIProcess/API/C/WKWebsiteDataStoreRef.h

    r223299 r223558  
    8282WK_EXPORT void WKWebsiteDataStoreGetFetchCacheOrigins(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreGetFetchCacheOriginsFunction function);
    8383
     84typedef void (*WKWebsiteDataStoreGetFetchCacheSizeForOriginFunction)(uint64_t, void*);
     85WK_EXPORT void WKWebsiteDataStoreGetFetchCacheSizeForOrigin(WKWebsiteDataStoreRef dataStoreRef, WKStringRef origin, void* context, WKWebsiteDataStoreGetFetchCacheSizeForOriginFunction function);
     86
    8487#ifdef __cplusplus
    8588}
  • trunk/Tools/ChangeLog

    r223536 r223558  
     12017-10-17  Youenn Fablet  <youenn@apple.com>
     2
     3        Cache API implementation should be able to compute storage size for WebKit client applications.
     4        https://bugs.webkit.org/show_bug.cgi?id=178350
     5
     6        Reviewed by Chris Dumez.
     7
     8        Adding support for a domCacheSize getter.
     9
     10        * WebKitTestRunner/InjectedBundle/Bindings/TestRunner.idl:
     11        * WebKitTestRunner/InjectedBundle/TestRunner.cpp:
     12        (WTR::TestRunner::domCacheSize):
     13        * WebKitTestRunner/InjectedBundle/TestRunner.h:
     14        * WebKitTestRunner/TestController.cpp:
     15        (WTR::FetchCacheSizeForOriginCallbackContext::FetchCacheSizeForOriginCallbackContext):
     16        (WTR::fetchCacheSizeForOriginCallback):
     17        (WTR::TestController::domCacheSize):
     18        * WebKitTestRunner/TestController.h:
     19        * WebKitTestRunner/TestInvocation.cpp:
     20        (WTR::TestInvocation::didReceiveSynchronousMessageFromInjectedBundle):
     21
    1222017-10-17  Nael Ouedraogo  <nael.ouedraogo@crf.canon.fr>
    223
  • trunk/Tools/WebKitTestRunner/InjectedBundle/Bindings/TestRunner.idl

    r223299 r223558  
    6060    void clearDOMCache(DOMString origin);
    6161    boolean hasDOMCache(DOMString origin);
     62    unsigned long domCacheSize(DOMString origin);
    6263
    6364    // Special options.
  • trunk/Tools/WebKitTestRunner/InjectedBundle/TestRunner.cpp

    r223433 r223558  
    18391839}
    18401840
     1841uint64_t TestRunner::domCacheSize(JSStringRef origin)
     1842{
     1843    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("DOMCacheSize"));
     1844    WKRetainPtr<WKStringRef> messageBody(AdoptWK, WKStringCreateWithJSString(origin));
     1845    WKTypeRef returnData = 0;
     1846    WKBundlePagePostSynchronousMessageForTesting(InjectedBundle::singleton().page()->page(), messageName.get(), messageBody.get(), &returnData);
     1847    return WKUInt64GetValue(static_cast<WKUInt64Ref>(returnData));
     1848}
     1849
    18411850} // namespace WTR
  • trunk/Tools/WebKitTestRunner/InjectedBundle/TestRunner.h

    r223433 r223558  
    166166    void clearDOMCache(JSStringRef origin);
    167167    bool hasDOMCache(JSStringRef origin);
     168    uint64_t domCacheSize(JSStringRef origin);
    168169
    169170    // Failed load condition testing
  • trunk/Tools/WebKitTestRunner/TestController.cpp

    r223502 r223558  
    23742374}
    23752375
     2376struct FetchCacheSizeForOriginCallbackContext {
     2377    explicit FetchCacheSizeForOriginCallbackContext(TestController& controller)
     2378        : testController(controller)
     2379    {
     2380    }
     2381
     2382    TestController& testController;
     2383
     2384    bool done { false };
     2385    uint64_t result { 0 };
     2386};
     2387
     2388static void fetchCacheSizeForOriginCallback(uint64_t size, void* userData)
     2389{
     2390    auto* context = static_cast<FetchCacheSizeForOriginCallbackContext*>(userData);
     2391    context->done = true;
     2392    context->result = size;
     2393    context->testController.notifyDone();
     2394}
     2395
     2396uint64_t TestController::domCacheSize(WKStringRef origin)
     2397{
     2398    auto* dataStore = WKContextGetWebsiteDataStore(platformContext());
     2399    FetchCacheSizeForOriginCallbackContext context(*this);
     2400    WKWebsiteDataStoreGetFetchCacheSizeForOrigin(dataStore, origin, &context, fetchCacheSizeForOriginCallback);
     2401    if (!context.done)
     2402        runUntil(context.done, m_currentInvocation->shortTimeout());
     2403    return context.result;
     2404}
     2405
    23762406#if !PLATFORM(COCOA) || !WK_API_ENABLED
    23772407
  • trunk/Tools/WebKitTestRunner/TestController.h

    r223299 r223558  
    184184    void statisticsResetToConsistentState();
    185185
    186     bool hasDOMCache(WKStringRef);
    187 
    188186    WKArrayRef openPanelFileURLs() const { return m_openPanelFileURLs.get(); }
    189187    void setOpenPanelFileURLs(WKArrayRef fileURLs) { m_openPanelFileURLs = fileURLs; }
     
    192190
    193191    void removeAllSessionCredentials();
     192
    194193    void clearDOMCache(WKStringRef origin);
     194    bool hasDOMCache(WKStringRef origin);
     195    uint64_t domCacheSize(WKStringRef origin);
    195196
    196197private:
  • trunk/Tools/WebKitTestRunner/TestInvocation.cpp

    r223299 r223558  
    12201220    }
    12211221
     1222    if (WKStringIsEqualToUTF8CString(messageName, "DOMCacheSize")) {
     1223        ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
     1224        WKStringRef origin = static_cast<WKStringRef>(messageBody);
     1225
     1226        auto domCacheSize = TestController::singleton().domCacheSize(origin);
     1227        WKRetainPtr<WKTypeRef> result(AdoptWK, WKUInt64Create(domCacheSize));
     1228        return result;
     1229    }
     1230
    12221231    ASSERT_NOT_REACHED();
    12231232    return nullptr;
Note: See TracChangeset for help on using the changeset viewer.