Changeset 254397 in webkit


Ignore:
Timestamp:
Jan 11, 2020 6:57:57 AM (4 years ago)
Author:
Kate Cheney
Message:

Add correct grandfathering functionality to the ITP database backend
https://bugs.webkit.org/show_bug.cgi?id=205844
<rdar://problem/58360450>

Reviewed by John Wilander.

Source/WebKit:

This patch adds functionality for grandfathering statistics upon
creation of a new ITP database file. The memory store relies on
the presence of the ITP plist to indicate whether the data should
be grandfathered. Since the database does not use a plist, this patch
adds a boolean variable to track whether the database file previously
existed or not to indicate when to grandfather.

  • NetworkProcess/Classifier/ResourceLoadStatisticsDatabaseStore.cpp:

(WebKit::ResourceLoadStatisticsDatabaseStore::openITPDatabase):

  • NetworkProcess/Classifier/ResourceLoadStatisticsDatabaseStore.h:

The boolean variable m_isNewResourceLoadStatisticDatabaseFile is set
to true if the database file did not previously exist, indicating ITP
is being used for the first time and data should be grandfathered.

  • NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp:

(WebKit::WebResourceLoadStatisticsStore::populateMemoryStoreFromDisk):
This is where the grandfathering happens. The call to logTestingEvent
is for API testing purposes only.

(WebKit::WebResourceLoadStatisticsStore::scheduleClearInMemoryAndPersistent):
If a scheduleClearInMemoryAndPersistent is called, the plist is
deleted, and data will be grandfathered upon recreation. To mimic
this, the ITP database store is cleared and becomes "new" again, so
the boolean is set to true.

  • UIProcess/API/C/WKPreferences.cpp:

(WKPreferencesSetIsITPDatabaseEnabled):
(WKPreferencesGetIsITPDatabaseEnabled):

  • UIProcess/API/C/WKPreferencesRef.h:
  • UIProcess/API/Cocoa/WKPreferences.mm:

(-[WKPreferences _isITPDatabaseEnabled]):

  • UIProcess/API/Cocoa/WKPreferencesPrivate.h:

In order to properly run the grandfathering
API tests, the 'isITPDatabaseEnabled' boolean needs to be exposed from
Web Preferences.

  • UIProcess/Cocoa/WebProcessPoolCocoa.mm:

The API tests rely on the ITP database flag being set correctly. Since
being on-by-default, the ITP database flag does not show up in the
user defaults menu if enabled. This patch fixes this by first checking
to see if the value is in the defaults menu, otherwise returning the
default preferences value. This ensures the preferences value matches
the actual store being initialized.

Tools:

In order to test both the ITP database and memory stores, this patch
had to restructure the grandfathering API tests.

If the test is for the database backend (ends in *Database), it will
check if the default store matches. If so, it continues on with the
normal test. If not, it has to use an API call to reconstruct the
new store and ensure the grandfathering still happens. Vice versa for
the memory store tests.

  • TestWebKitAPI/Tests/WebKitCocoa/ResourceLoadStatistics.mm:

(isITPDatabaseEnabled):
(TEST):

Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r254392 r254397  
     12020-01-11  Kate Cheney  <katherine_cheney@apple.com>
     2
     3        Add correct grandfathering functionality to the ITP database backend
     4        https://bugs.webkit.org/show_bug.cgi?id=205844
     5        <rdar://problem/58360450>
     6
     7        Reviewed by John Wilander.
     8
     9        This patch adds functionality for grandfathering statistics upon
     10        creation of a new ITP database file. The memory store relies on
     11        the presence of the ITP plist to indicate whether the data should
     12        be grandfathered. Since the database does not use a plist, this patch
     13        adds a boolean variable to track whether the database file previously
     14        existed or not to indicate when to grandfather.
     15
     16        * NetworkProcess/Classifier/ResourceLoadStatisticsDatabaseStore.cpp:
     17        (WebKit::ResourceLoadStatisticsDatabaseStore::openITPDatabase):
     18        * NetworkProcess/Classifier/ResourceLoadStatisticsDatabaseStore.h:
     19        The boolean variable m_isNewResourceLoadStatisticDatabaseFile is set
     20        to true if the database file did not previously exist, indicating ITP
     21        is being used for the first time and data should be grandfathered.
     22
     23        * NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp:
     24        (WebKit::WebResourceLoadStatisticsStore::populateMemoryStoreFromDisk):
     25        This is where the grandfathering happens. The call to logTestingEvent
     26        is for API testing purposes only.
     27
     28        (WebKit::WebResourceLoadStatisticsStore::scheduleClearInMemoryAndPersistent):
     29        If a scheduleClearInMemoryAndPersistent is called, the plist is
     30        deleted, and data will be grandfathered upon recreation. To mimic
     31        this, the ITP database store is cleared and becomes "new" again, so
     32        the boolean is set to true.
     33
     34        * UIProcess/API/C/WKPreferences.cpp:
     35        (WKPreferencesSetIsITPDatabaseEnabled):
     36        (WKPreferencesGetIsITPDatabaseEnabled):
     37        * UIProcess/API/C/WKPreferencesRef.h:
     38        * UIProcess/API/Cocoa/WKPreferences.mm:
     39        (-[WKPreferences _isITPDatabaseEnabled]):
     40        * UIProcess/API/Cocoa/WKPreferencesPrivate.h:
     41        In order to properly run the grandfathering
     42        API tests, the 'isITPDatabaseEnabled' boolean needs to be exposed from
     43        Web Preferences.
     44
     45        * UIProcess/Cocoa/WebProcessPoolCocoa.mm:
     46        The API tests rely on the ITP database flag being set correctly. Since
     47        being on-by-default, the ITP database flag does not show up in the
     48        user defaults menu if enabled. This patch fixes this by first checking
     49        to see if the value is in the defaults menu, otherwise returning the
     50        default preferences value. This ensures the preferences value matches
     51        the actual store being initialized.
     52
    1532020-01-10  Eric Carlson  <eric.carlson@apple.com>
    254
  • trunk/Source/WebKit/NetworkProcess/Classifier/ResourceLoadStatisticsDatabaseStore.cpp

    r254296 r254397  
    310310void ResourceLoadStatisticsDatabaseStore::openITPDatabase()
    311311{
     312    if (!FileSystem::fileExists(m_storageDirectoryPath))
     313        m_isNewResourceLoadStatisticsDatabaseFile = true;
     314    else
     315        m_isNewResourceLoadStatisticsDatabaseFile = false;
     316
    312317    if (!m_database.open(m_storageDirectoryPath)) {
    313318        RELEASE_LOG_ERROR(Network, "%p - ResourceLoadStatisticsDatabaseStore::open failed, error message: %{public}s, database path: %{public}s", this, m_database.lastErrorMsg(), m_storageDirectoryPath.utf8().data());
  • trunk/Source/WebKit/NetworkProcess/Classifier/ResourceLoadStatisticsDatabaseStore.h

    r254296 r254397  
    136136    void resourceToString(StringBuilder&, const String&) const;
    137137    Seconds getMostRecentlyUpdatedTimestamp(const RegistrableDomain&, const TopFrameDomain&) const;
     138    bool isNewResourceLoadStatisticsDatabaseFile() const { return m_isNewResourceLoadStatisticsDatabaseFile; }
     139    void setIsNewResourceLoadStatisticsDatabaseFile(bool isNewResourceLoadStatisticsDatabaseFile) { m_isNewResourceLoadStatisticsDatabaseFile = isNewResourceLoadStatisticsDatabaseFile; }
    138140
    139141private:
     
    257259    mutable WebCore::SQLiteStatement m_getMostRecentlyUpdatedTimestampStatement;
    258260    PAL::SessionID m_sessionID;
     261    bool m_isNewResourceLoadStatisticsDatabaseFile { false };
    259262};
    260263
  • trunk/Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp

    r254296 r254397  
    231231                postTaskReply(WTFMove(completionHandler));
    232232            });
    233         else
     233        else if (is<ResourceLoadStatisticsDatabaseStore>(*m_statisticsStore)) {
     234            auto& databaseStore = downcast<ResourceLoadStatisticsDatabaseStore>(*m_statisticsStore);
     235            if (databaseStore.isNewResourceLoadStatisticsDatabaseFile()) {
     236                m_statisticsStore->grandfatherExistingWebsiteData([protectedThis = WTFMove(protectedThis), completionHandler = WTFMove(completionHandler)]() mutable {
     237                    postTaskReply(WTFMove(completionHandler));
     238                });
     239                databaseStore.setIsNewResourceLoadStatisticsDatabaseFile(false);
     240            } else
     241                postTaskReply([this, protectedThis = WTFMove(protectedThis), completionHandler = WTFMove(completionHandler)]() mutable {
     242                    logTestingEvent("PopulatedWithoutGrandfathering"_s);
     243                    completionHandler();
     244                });
     245        } else
    234246            postTaskReply(WTFMove(completionHandler));
    235247    });
     
    925937        m_statisticsStore->clear([this, protectedThis = protectedThis.copyRef(), shouldGrandfather, callbackAggregator = callbackAggregator.copyRef()] () mutable {
    926938            if (shouldGrandfather == ShouldGrandfatherStatistics::Yes) {
    927                 if (m_statisticsStore)
     939                if (m_statisticsStore) {
    928940                    m_statisticsStore->grandfatherExistingWebsiteData([callbackAggregator = WTFMove(callbackAggregator)]() mutable { });
    929                 else
     941                    if (is<ResourceLoadStatisticsDatabaseStore>(*m_statisticsStore))
     942                        downcast<ResourceLoadStatisticsDatabaseStore>(*m_statisticsStore).setIsNewResourceLoadStatisticsDatabaseFile(true);
     943                } else
    930944                    RELEASE_LOG(ResourceLoadStatistics, "WebResourceLoadStatisticsStore::scheduleClearInMemoryAndPersistent After being cleared, m_statisticsStore is null when trying to grandfather data.");
    931945            }
  • trunk/Source/WebKit/UIProcess/API/C/WKPreferences.cpp

    r254130 r254397  
    119119}
    120120
     121void WKPreferencesSetIsITPDatabaseEnabled(WKPreferencesRef preferencesRef, bool isITPDatabaseEnabled)
     122{
     123    toImpl(preferencesRef)->setIsITPDatabaseEnabled(isITPDatabaseEnabled);
     124}
     125
     126bool WKPreferencesGetIsITPDatabaseEnabled(WKPreferencesRef preferencesRef)
     127{
     128    return toImpl(preferencesRef)->isITPDatabaseEnabled();
     129}
     130
    121131void WKPreferencesSetOfflineWebApplicationCacheEnabled(WKPreferencesRef preferencesRef, bool offlineWebApplicationCacheEnabled)
    122132{
  • trunk/Source/WebKit/UIProcess/API/C/WKPreferencesRef.h

    r254130 r254397  
    7979
    8080// Defaults to true.
     81WK_EXPORT void WKPreferencesSetIsITPDatabaseEnabled(WKPreferencesRef preferences, bool isITPDatabaseEnabled);
     82WK_EXPORT bool WKPreferencesGetIsITPDatabaseEnabled(WKPreferencesRef preferences);
     83
     84// Defaults to true.
    8185WK_EXPORT void WKPreferencesSetOfflineWebApplicationCacheEnabled(WKPreferencesRef preferences, bool offlineWebApplicationCacheEnabled);
    8286WK_EXPORT bool WKPreferencesGetOfflineWebApplicationCacheEnabled(WKPreferencesRef preferences);
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKPreferences.mm

    r251859 r254397  
    903903}
    904904
     905- (BOOL)_isITPDatabaseEnabled
     906{
     907    return _preferences->isITPDatabaseEnabled();
     908}
     909
    905910#if PLATFORM(MAC)
    906911- (void)_setJavaEnabledForLocalFiles:(BOOL)enabled
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKPreferencesPrivate.h

    r252174 r254397  
    162162@property (nonatomic, setter=_setAcceleratedCompositingEnabled:) BOOL _acceleratedCompositingEnabled WK_API_AVAILABLE(macos(10.13.4), ios(WK_IOS_TBA));
    163163@property (nonatomic, setter=_setRequestAnimationFrameEnabled:) BOOL _requestAnimationFrameEnabled WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
     164@property (nonatomic, readonly) BOOL _isITPDatabaseEnabled WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
    164165
    165166#if !TARGET_OS_IPHONE
  • trunk/Source/WebKit/UIProcess/Cocoa/WebProcessPoolCocoa.mm

    r254373 r254397  
    402402    parameters.storageAccessAPIEnabled = storageAccessAPIEnabled();
    403403
    404     parameters.shouldEnableITPDatabase = [defaults boolForKey:[NSString stringWithFormat:@"InternalDebug%@", WebPreferencesKey::isITPDatabaseEnabledKey().createCFString().get()]];
     404    NSNumber *databaseEnabledValue = [defaults objectForKey:[NSString stringWithFormat:@"InternalDebug%@", WebPreferencesKey::isITPDatabaseEnabledKey().createCFString().get()]];
     405    if (databaseEnabledValue)
     406        parameters.shouldEnableITPDatabase = databaseEnabledValue.boolValue;
     407    else
     408        parameters.shouldEnableITPDatabase = m_defaultPageGroup->preferences().isITPDatabaseEnabled();
    405409
    406410    parameters.enableAdClickAttributionDebugMode = [defaults boolForKey:[NSString stringWithFormat:@"Experimental%@", WebPreferencesKey::adClickAttributionDebugModeEnabledKey().createCFString().get()]];
  • trunk/Tools/ChangeLog

    r254389 r254397  
     12020-01-11  Kate Cheney  <katherine_cheney@apple.com>
     2
     3        Add correct grandfathering functionality to the ITP database backend
     4        https://bugs.webkit.org/show_bug.cgi?id=205844
     5        <rdar://problem/58360450>
     6
     7        Reviewed by John Wilander.
     8
     9        In order to test both the ITP database and memory stores, this patch
     10        had to restructure the grandfathering API tests.
     11
     12        If the test is for the database backend (ends in *Database), it will
     13        check if the default store matches. If so, it continues on with the
     14        normal test. If not, it has to use an API call to reconstruct the
     15        new store and ensure the grandfathering still happens. Vice versa for
     16        the memory store tests.
     17
     18        * TestWebKitAPI/Tests/WebKitCocoa/ResourceLoadStatistics.mm:
     19        (isITPDatabaseEnabled):
     20        (TEST):
     21
    1222020-01-10  Myles C. Maxfield  <mmaxfield@apple.com>
    223
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ResourceLoadStatistics.mm

    r253863 r254397  
    7070@end
    7171
     72bool isITPDatabaseEnabled()
     73{
     74    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     75    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     76    auto defaultDatabaseEnabled = [configuration preferences]._isITPDatabaseEnabled;
     77
     78    NSNumber *databaseEnabledValue = [defaults objectForKey:@"InternalDebugIsITPDatabaseEnabled"];
     79    if (databaseEnabledValue)
     80        return databaseEnabledValue.boolValue;
     81
     82    return defaultDatabaseEnabled;
     83}
     84
    7285TEST(ResourceLoadStatistics, GrandfatherCallback)
    7386{
     
    8497
    8598    [dataStore _setResourceLoadStatisticsTestingCallback:^(WKWebsiteDataStore *, NSString *message) {
    86         ASSERT_TRUE([message isEqualToString:@"Grandfathered"]);
     99        // Only if the database store is not default-enabled do we need to check that this first message is correct.
     100        if (!isITPDatabaseEnabled())
     101            ASSERT_TRUE([message isEqualToString:@"Grandfathered"]);
    87102        grandfatheredFlag = true;
    88103    }];
     
    92107    [dataStore _setResourceLoadStatisticsEnabled:YES];
    93108    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
     109
     110    if (isITPDatabaseEnabled()) {
     111        TestWebKitAPI::Util::run(&grandfatheredFlag);
     112
     113        // Since the database store is enabled, we need to disable it to test this functionality with the plist.
     114        // We already have a NetworkProcess up and running with ITP enabled. We now make another call initializing
     115        // the memory store and testing grandfathering.
     116        grandfatheredFlag = false;
     117        [dataStore _setResourceLoadStatisticsTestingCallback:^(WKWebsiteDataStore *, NSString *message) {
     118            ASSERT_TRUE([message isEqualToString:@"Grandfathered"]);
     119            grandfatheredFlag = true;
     120        }];
     121
     122        [dataStore _setUseITPDatabase:false completionHandler: ^(void) {
     123            doneFlag = true;
     124        }];
     125
     126        TestWebKitAPI::Util::run(&doneFlag);
     127    }
    94128
    95129    TestWebKitAPI::Util::run(&grandfatheredFlag);
     
    104138
    105139    grandfatheredFlag = false;
     140    doneFlag = false;
    106141    [dataStore removeDataOfTypes:[WKWebsiteDataStore _allWebsiteDataTypesIncludingPrivate]  modifiedSince:[NSDate distantPast] completionHandler:^ {
    107142        doneFlag = true;
     
    135170}
    136171
     172TEST(ResourceLoadStatistics, GrandfatherCallbackDatabase)
     173{
     174    auto *dataStore = [WKWebsiteDataStore defaultDataStore];
     175
     176    NSURL *statisticsDirectoryURL = [NSURL fileURLWithPath:[@"~/Library/WebKit/TestWebKitAPI/WebsiteData/ResourceLoadStatistics" stringByExpandingTildeInPath] isDirectory:YES];
     177    NSURL *fileURL = [statisticsDirectoryURL URLByAppendingPathComponent:@"observations.db"];
     178    [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
     179    [[NSFileManager defaultManager] removeItemAtURL:statisticsDirectoryURL error:nil];
     180    EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:statisticsDirectoryURL.path]);
     181
     182    static bool grandfatheredFlag;
     183    static bool doneFlag;
     184
     185    [dataStore _setResourceLoadStatisticsTestingCallback:^(WKWebsiteDataStore *, NSString *message) {
     186        // Only if the database store is default-enabled do we need to check that this first message is correct.
     187        if (isITPDatabaseEnabled())
     188            ASSERT_TRUE([message isEqualToString:@"Grandfathered"]);
     189        grandfatheredFlag = true;
     190    }];
     191
     192    auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
     193    [dataStore _setResourceLoadStatisticsEnabled:YES];
     194    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
     195
     196    if (!isITPDatabaseEnabled()) {
     197        TestWebKitAPI::Util::run(&grandfatheredFlag);
     198
     199        // Since the database store is not enabled, we need to update the store to use a database instead of a plist for this test.
     200        // We already have a NetworkProcess up and running with ITP enabled. We now make another call initializing
     201        // the database store and testing grandfathering.
     202        grandfatheredFlag = false;
     203        [dataStore _setResourceLoadStatisticsTestingCallback:^(WKWebsiteDataStore *, NSString *message) {
     204            ASSERT_TRUE([message isEqualToString:@"Grandfathered"]);
     205            grandfatheredFlag = true;
     206        }];
     207
     208        [dataStore _setUseITPDatabase:true completionHandler: ^(void) {
     209            doneFlag = true;
     210        }];
     211
     212        TestWebKitAPI::Util::run(&doneFlag);
     213    }
     214
     215    TestWebKitAPI::Util::run(&grandfatheredFlag);
     216
     217    // Spin the runloop until the resource load statistics file has written to disk.
     218    // If the test enters a spin loop here, it has failed.
     219    while (true) {
     220        if ([[NSFileManager defaultManager] fileExistsAtPath:fileURL.path])
     221            break;
     222        TestWebKitAPI::Util::spinRunLoop(1);
     223    }
     224
     225    grandfatheredFlag = false;
     226    doneFlag = false;
     227    [dataStore removeDataOfTypes:[WKWebsiteDataStore _allWebsiteDataTypesIncludingPrivate]  modifiedSince:[NSDate distantPast] completionHandler:^ {
     228        doneFlag = true;
     229    }];
     230
     231    TestWebKitAPI::Util::run(&doneFlag);
     232    TestWebKitAPI::Util::spinRunLoop(10);
     233
     234    // The website data store remove should have completed, but since we removed all of the data types that are monitored by resource load statistics,
     235    // no grandfathering call should have been made. Note that the database file will not be deleted like in the persistent storage case, only cleared.
     236    EXPECT_FALSE(grandfatheredFlag);
     237
     238    doneFlag = false;
     239    [dataStore removeDataOfTypes:[NSSet setWithObjects:WKWebsiteDataTypeCookies, _WKWebsiteDataTypeResourceLoadStatistics, nil] modifiedSince:[NSDate distantPast] completionHandler:^ {
     240        doneFlag = true;
     241    }];
     242
     243    // Since we did not remove every data type covered by resource load statistics, we do expect that grandfathering took place again.
     244    // If the test hangs waiting on either of these conditions, it has failed.
     245    TestWebKitAPI::Util::run(&grandfatheredFlag);
     246    TestWebKitAPI::Util::run(&doneFlag);
     247}
     248
    137249TEST(ResourceLoadStatistics, ShouldNotGrandfatherOnStartup)
    138250{
     
    149261
    150262    static bool callbackFlag;
     263    static bool doneFlag;
     264
    151265    [dataStore _setResourceLoadStatisticsTestingCallback:^(WKWebsiteDataStore *, NSString *message) {
    152         ASSERT_TRUE([message isEqualToString:@"PopulatedWithoutGrandfathering"]);
     266        // Only if the database store is not default-enabled do we need to check that this first message is correct.
     267        if (!isITPDatabaseEnabled())
     268            ASSERT_TRUE([message isEqualToString:@"PopulatedWithoutGrandfathering"]);
    153269        callbackFlag = true;
    154270    }];
     
    159275    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
    160276
     277    if (isITPDatabaseEnabled()) {
     278        TestWebKitAPI::Util::run(&callbackFlag);
     279
     280        // Since the database store is enabled, we need to disable it to test this functionality with the plist.
     281        // We already have a NetworkProcess up and running with ITP enabled. We now make another call initializing
     282        // the memory store and testing grandfathering.
     283        callbackFlag = false;
     284        [dataStore _setResourceLoadStatisticsTestingCallback:^(WKWebsiteDataStore *, NSString *message) {
     285            ASSERT_TRUE([message isEqualToString:@"PopulatedWithoutGrandfathering"]);
     286            callbackFlag = true;
     287        }];
     288
     289        [dataStore _setUseITPDatabase:false completionHandler: ^(void) {
     290            doneFlag = true;
     291        }];
     292
     293        TestWebKitAPI::Util::run(&doneFlag);
     294    }
     295
    161296    TestWebKitAPI::Util::run(&callbackFlag);
    162297}
    163298
    164 TEST(ResourceLoadStatistics, ChildProcessesNotLaunched)
    165 {
    166     // Ensure the shared process pool exists so the data store operations we're about to do work with it.
    167     WKProcessPool *sharedProcessPool = [WKProcessPool _sharedProcessPool];
    168 
    169     EXPECT_EQ((size_t)0, [sharedProcessPool _pluginProcessCount]);
    170 
     299TEST(ResourceLoadStatistics, ShouldNotGrandfatherOnStartupDatabase)
     300{
    171301    auto *dataStore = [WKWebsiteDataStore defaultDataStore];
    172302
    173303    NSURL *statisticsDirectoryURL = [NSURL fileURLWithPath:[@"~/Library/WebKit/TestWebKitAPI/WebsiteData/ResourceLoadStatistics" stringByExpandingTildeInPath] isDirectory:YES];
    174     NSURL *targetURL = [statisticsDirectoryURL URLByAppendingPathComponent:@"full_browsing_session_resourceLog.plist"];
     304    NSURL *targetURL = [statisticsDirectoryURL URLByAppendingPathComponent:@"observations.db"];
    175305    NSURL *testResourceURL = [[NSBundle mainBundle] URLForResource:@"EmptyGrandfatheredResourceLoadStatistics" withExtension:@"plist" subdirectory:@"TestWebKitAPI.resources"];
    176306
     
    180310    EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:targetURL.path]);
    181311
     312    static bool callbackFlag;
    182313    static bool doneFlag;
     314
    183315    [dataStore _setResourceLoadStatisticsTestingCallback:^(WKWebsiteDataStore *, NSString *message) {
    184         EXPECT_TRUE([message isEqualToString:@"PopulatedWithoutGrandfathering"]);
    185         doneFlag = true;
     316        // Only if the database store is default-enabled do we need to check that this first message is correct.
     317        if (isITPDatabaseEnabled())
     318            ASSERT_TRUE([message isEqualToString:@"PopulatedWithoutGrandfathering"]);
     319        callbackFlag = true;
    186320    }];
    187321
     
    191325    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
    192326
    193     TestWebKitAPI::Util::run(&doneFlag);
     327    if (!isITPDatabaseEnabled()) {
     328        TestWebKitAPI::Util::run(&callbackFlag);
     329
     330        // Since the database store is not enabled, we need to update the store to use a database instead of a plist for this test.
     331        // We already have a NetworkProcess up and running with ITP enabled. We now make another call initializing
     332        // the database store and testing grandfathering.
     333        callbackFlag = false;
     334        [dataStore _setResourceLoadStatisticsTestingCallback:^(WKWebsiteDataStore *, NSString *message) {
     335            ASSERT_TRUE([message isEqualToString:@"PopulatedWithoutGrandfathering"]);
     336            callbackFlag = true;
     337        }];
     338
     339        [dataStore _setUseITPDatabase:true completionHandler: ^(void) {
     340            doneFlag = true;
     341        }];
     342
     343        TestWebKitAPI::Util::run(&doneFlag);
     344    }
     345
     346    TestWebKitAPI::Util::run(&callbackFlag);
     347}
     348
     349TEST(ResourceLoadStatistics, ChildProcessesNotLaunched)
     350{
     351    // Ensure the shared process pool exists so the data store operations we're about to do work with it.
     352    WKProcessPool *sharedProcessPool = [WKProcessPool _sharedProcessPool];
     353
     354    EXPECT_EQ((size_t)0, [sharedProcessPool _pluginProcessCount]);
     355
     356    auto *dataStore = [WKWebsiteDataStore defaultDataStore];
     357
     358    NSURL *statisticsDirectoryURL = [NSURL fileURLWithPath:[@"~/Library/WebKit/TestWebKitAPI/WebsiteData/ResourceLoadStatistics" stringByExpandingTildeInPath] isDirectory:YES];
     359    NSURL *targetURL = [statisticsDirectoryURL URLByAppendingPathComponent:@"full_browsing_session_resourceLog.plist"];
     360    NSURL *testResourceURL = [[NSBundle mainBundle] URLForResource:@"EmptyGrandfatheredResourceLoadStatistics" withExtension:@"plist" subdirectory:@"TestWebKitAPI.resources"];
     361
     362    [[NSFileManager defaultManager] createDirectoryAtURL:statisticsDirectoryURL withIntermediateDirectories:YES attributes:nil error:nil];
     363    [[NSFileManager defaultManager] copyItemAtURL:testResourceURL toURL:targetURL error:nil];
     364   
     365    EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:targetURL.path]);
     366
     367    static bool callbackFlag;
     368    static bool doneFlag;
     369    [dataStore _setResourceLoadStatisticsTestingCallback:^(WKWebsiteDataStore *, NSString *message) {
     370        // Only if the database store is not default-enabled do we need to check that this first message is correct.
     371        if (!isITPDatabaseEnabled())
     372            EXPECT_TRUE([message isEqualToString:@"PopulatedWithoutGrandfathering"]);
     373        callbackFlag = true;
     374    }];
     375
     376    // We need an active NetworkProcess to perform ResourceLoadStatistics operations.
     377    auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
     378    [dataStore _setResourceLoadStatisticsEnabled:YES];
     379    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
     380
     381    if (isITPDatabaseEnabled()) {
     382        TestWebKitAPI::Util::run(&callbackFlag);
     383
     384        // Since the database store is enabled, we need to disable it to test this functionality with the plist.
     385        // We already have a NetworkProcess up and running with ITP enabled. We now make another call initializing
     386        // the memory store and testing grandfathering.
     387        callbackFlag = false;
     388        [dataStore _setResourceLoadStatisticsTestingCallback:^(WKWebsiteDataStore *, NSString *message) {
     389            ASSERT_TRUE([message isEqualToString:@"PopulatedWithoutGrandfathering"]);
     390            callbackFlag = true;
     391        }];
     392
     393        [dataStore _setUseITPDatabase:false completionHandler: ^(void) {
     394            doneFlag = true;
     395        }];
     396
     397        TestWebKitAPI::Util::run(&doneFlag);
     398    }
     399
     400    TestWebKitAPI::Util::run(&callbackFlag);
     401
     402    EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:targetURL.path]);
     403
     404    webView.clear();
     405
     406    EXPECT_EQ((size_t)0, [sharedProcessPool _pluginProcessCount]);
     407}
     408
     409TEST(ResourceLoadStatistics, ChildProcessesNotLaunchedDatabase)
     410{
     411    // Ensure the shared process pool exists so the data store operations we're about to do work with it.
     412    WKProcessPool *sharedProcessPool = [WKProcessPool _sharedProcessPool];
     413
     414    EXPECT_EQ((size_t)0, [sharedProcessPool _pluginProcessCount]);
     415
     416    auto *dataStore = [WKWebsiteDataStore defaultDataStore];
     417
     418    NSURL *statisticsDirectoryURL = [NSURL fileURLWithPath:[@"~/Library/WebKit/TestWebKitAPI/WebsiteData/ResourceLoadStatistics" stringByExpandingTildeInPath] isDirectory:YES];
     419    NSURL *targetURL = [statisticsDirectoryURL URLByAppendingPathComponent:@"observations.db"];
     420
     421    NSURL *testResourceURL = [[NSBundle mainBundle] URLForResource:@"EmptyGrandfatheredResourceLoadStatistics" withExtension:@"plist" subdirectory:@"TestWebKitAPI.resources"];
     422
     423    [[NSFileManager defaultManager] createDirectoryAtURL:statisticsDirectoryURL withIntermediateDirectories:YES attributes:nil error:nil];
     424    [[NSFileManager defaultManager] copyItemAtURL:testResourceURL toURL:targetURL error:nil];
     425
     426    EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:targetURL.path]);
     427
     428    static bool callbackFlag;
     429    static bool doneFlag;
     430
     431    [dataStore _setResourceLoadStatisticsTestingCallback:^(WKWebsiteDataStore *, NSString *message) {
     432        // Only if the database store is default-enabled do we need to check that this first message is correct.
     433        if (isITPDatabaseEnabled())
     434            EXPECT_TRUE([message isEqualToString:@"PopulatedWithoutGrandfathering"]);
     435        callbackFlag = true;
     436    }];
     437
     438    // We need an active NetworkProcess to perform ResourceLoadStatistics operations.
     439    auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
     440    [dataStore _setResourceLoadStatisticsEnabled:YES];
     441    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
     442
     443    if (!isITPDatabaseEnabled()) {
     444        TestWebKitAPI::Util::run(&callbackFlag);
     445
     446        // Since the database store is not enabled, we need to update the store to use a database instead of a plist for this test.
     447        // We already have a NetworkProcess up and running with ITP enabled. We now make another call initializing
     448        // the database store and testing grandfathering.
     449        callbackFlag = false;
     450        [dataStore _setResourceLoadStatisticsTestingCallback:^(WKWebsiteDataStore *, NSString *message) {
     451            ASSERT_TRUE([message isEqualToString:@"PopulatedWithoutGrandfathering"]);
     452            callbackFlag = true;
     453        }];
     454
     455        [dataStore _setUseITPDatabase:true completionHandler: ^(void) {
     456            doneFlag = true;
     457        }];
     458
     459        TestWebKitAPI::Util::run(&doneFlag);
     460    }
     461
     462    TestWebKitAPI::Util::run(&callbackFlag);
    194463
    195464    EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:targetURL.path]);
Note: See TracChangeset for help on using the changeset viewer.