⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 286545 in webkit


Ignore:
Timestamp:
Dec 6, 2021, 9:46:01 AM (5 years ago)
Author:
commit-queue@webkit.org
Message:

WKWebpagePreferences._activeContentRuleListActionPatterns should be an NSDictionary of identifier to allowed patterns
https://bugs.webkit.org/show_bug.cgi?id=233842

Patch by Alex Christensen <achristensen@webkit.org> on 2021-12-06
Reviewed by Timothy Hatcher.

Source/WebCore:

There's no need for nil to match everything because a pattern can be written to quickly match everything.
There is a need for different extensions (with different identifiers) to have different active action permissions, though.

  • contentextensions/ContentExtensionsBackend.cpp:

(WebCore::ContentExtensions::ContentExtensionsBackend::processContentRuleListsForLoad):

  • loader/DocumentLoader.cpp:

(WebCore::DocumentLoader::DocumentLoader):
(WebCore::DocumentLoader::setActiveContentRuleListActionPatterns):
(WebCore::DocumentLoader::allowsActiveContentRuleListActionsForURL const):

  • loader/DocumentLoader.h:

Source/WebKit:

  • Shared/WebsitePoliciesData.cpp:

(WebKit::WebsitePoliciesData::decode):

  • Shared/WebsitePoliciesData.h:
  • UIProcess/API/APIWebsitePolicies.h:
  • UIProcess/API/Cocoa/WKWebpagePreferences.mm:

(-[WKWebpagePreferences _setActiveContentRuleListActionPatterns:]):
(-[WKWebpagePreferences _activeContentRuleListActionPatterns]):

  • UIProcess/API/Cocoa/WKWebpagePreferencesPrivate.h:

Tools:

  • TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm:

(navigationDelegateAllowingActiveActionsOnTestHost):
(TEST_F):

Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r286544 r286545  
     12021-12-06  Alex Christensen  <achristensen@webkit.org>
     2
     3        WKWebpagePreferences._activeContentRuleListActionPatterns should be an NSDictionary of identifier to allowed patterns
     4        https://bugs.webkit.org/show_bug.cgi?id=233842
     5
     6        Reviewed by Timothy Hatcher.
     7
     8        There's no need for nil to match everything because a pattern can be written to quickly match everything.
     9        There is a need for different extensions (with different identifiers) to have different active action permissions, though.
     10
     11        * contentextensions/ContentExtensionsBackend.cpp:
     12        (WebCore::ContentExtensions::ContentExtensionsBackend::processContentRuleListsForLoad):
     13        * loader/DocumentLoader.cpp:
     14        (WebCore::DocumentLoader::DocumentLoader):
     15        (WebCore::DocumentLoader::setActiveContentRuleListActionPatterns):
     16        (WebCore::DocumentLoader::allowsActiveContentRuleListActionsForURL const):
     17        * loader/DocumentLoader.h:
     18
    1192021-12-06  Antoine Quint  <graouts@webkit.org>
    220
  • trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.cpp

    r286402 r286545  
    229229                RELEASE_ASSERT_NOT_REACHED();
    230230            }, [&] (const ModifyHeadersAction& action) {
    231                 if (initiatingDocumentLoader.allowsActiveContentRuleListActionsForURL(url))
     231                if (initiatingDocumentLoader.allowsActiveContentRuleListActionsForURL(contentRuleListIdentifier, url))
    232232                    results.summary.modifyHeadersActions.append(action);
    233233            }, [&] (const RedirectAction& redirectAction) {
    234                 if (initiatingDocumentLoader.allowsActiveContentRuleListActionsForURL(url))
    235                     results.summary.redirectActions.append({ redirectAction, m_contentExtensions.get(actionsFromContentRuleList.contentRuleListIdentifier)->extensionBaseURL() });
     234                if (initiatingDocumentLoader.allowsActiveContentRuleListActionsForURL(contentRuleListIdentifier, url))
     235                    results.summary.redirectActions.append({ redirectAction, m_contentExtensions.get(contentRuleListIdentifier)->extensionBaseURL() });
    236236            }), action.data());
    237237        }
  • trunk/Source/WebCore/loader/DocumentLoader.cpp

    r286012 r286545  
    175175    , m_substituteResourceDeliveryTimer(*this, &DocumentLoader::substituteResourceDeliveryTimerFired)
    176176    , m_applicationCacheHost(makeUnique<ApplicationCacheHost>(*this))
    177     , m_activeContentRuleListActionPatterns(Vector<UserContentURLPattern>())
    178 {
    179     // FIXME: Vector default constructor shouldn't need to know the size of the elements,
    180     // so m_activeContentRuleListActionPatterns ought to be able to be initialized with an initializer list in the header without including UserContentURLPattern.h.
     177{
    181178}
    182179
     
    24382435#endif // ENABLE(CONTENT_FILTERING)
    24392436
    2440 void DocumentLoader::setActiveContentRuleListActionPatterns(const std::optional<HashSet<String>>& patterns)
    2441 {
    2442     if (!patterns) {
    2443         m_activeContentRuleListActionPatterns = std::nullopt;
    2444         return;
    2445     }
    2446     Vector<WebCore::UserContentURLPattern> patternVector;
    2447     patternVector.reserveInitialCapacity(patterns->size());
    2448     for (auto& patternString : *patterns) {
    2449         WebCore::UserContentURLPattern parsedPattern(patternString);
    2450         if (parsedPattern.isValid())
    2451             patternVector.uncheckedAppend(WTFMove(parsedPattern));
    2452     }
    2453 
    2454     m_activeContentRuleListActionPatterns = WTFMove(patternVector);
    2455 }
    2456 
    2457 bool DocumentLoader::allowsActiveContentRuleListActionsForURL(const URL& url) const
    2458 {
    2459     if (!m_activeContentRuleListActionPatterns)
    2460         return true;
    2461     for (const auto& pattern : *m_activeContentRuleListActionPatterns) {
     2437void DocumentLoader::setActiveContentRuleListActionPatterns(const HashMap<String, Vector<String>>& patterns)
     2438{
     2439    HashMap<String, Vector<UserContentURLPattern>> parsedPatternMap;
     2440
     2441    for (auto& pair : patterns) {
     2442        Vector<UserContentURLPattern> patternVector;
     2443        patternVector.reserveInitialCapacity(pair.value.size());
     2444        for (auto& patternString : pair.value) {
     2445            UserContentURLPattern parsedPattern(patternString);
     2446            if (parsedPattern.isValid())
     2447                patternVector.uncheckedAppend(WTFMove(parsedPattern));
     2448        }
     2449        parsedPatternMap.set(pair.key, WTFMove(patternVector));
     2450    }
     2451
     2452    m_activeContentRuleListActionPatterns = WTFMove(parsedPatternMap);
     2453}
     2454
     2455bool DocumentLoader::allowsActiveContentRuleListActionsForURL(const String& contentRuleListIdentifier, const URL& url) const
     2456{
     2457    for (const auto& pattern : m_activeContentRuleListActionPatterns.get(contentRuleListIdentifier)) {
    24622458        if (pattern.matches(url))
    24632459            return true;
  • trunk/Source/WebCore/loader/DocumentLoader.h

    r286012 r286545  
    303303    void setUserContentExtensionsEnabled(bool enabled) { m_userContentExtensionsEnabled = enabled; }
    304304
    305     bool allowsActiveContentRuleListActionsForURL(const URL&) const;
    306     WEBCORE_EXPORT void setActiveContentRuleListActionPatterns(const std::optional<HashSet<String>>&);
     305    bool allowsActiveContentRuleListActionsForURL(const String& contentRuleListIdentifier, const URL&) const;
     306    WEBCORE_EXPORT void setActiveContentRuleListActionPatterns(const HashMap<String, Vector<String>>&);
    307307
    308308#if ENABLE(DEVICE_ORIENTATION)
     
    656656    String m_customNavigatorPlatform;
    657657    bool m_userContentExtensionsEnabled { true };
    658     std::optional<Vector<UserContentURLPattern>> m_activeContentRuleListActionPatterns;
     658    HashMap<String, Vector<UserContentURLPattern>> m_activeContentRuleListActionPatterns;
    659659#if ENABLE(DEVICE_ORIENTATION)
    660660    DeviceOrientationOrMotionPermissionState m_deviceOrientationAndMotionAccessState { DeviceOrientationOrMotionPermissionState::Prompt };
  • trunk/Source/WebKit/ChangeLog

    r286538 r286545  
     12021-12-06  Alex Christensen  <achristensen@webkit.org>
     2
     3        WKWebpagePreferences._activeContentRuleListActionPatterns should be an NSDictionary of identifier to allowed patterns
     4        https://bugs.webkit.org/show_bug.cgi?id=233842
     5
     6        Reviewed by Timothy Hatcher.
     7
     8        * Shared/WebsitePoliciesData.cpp:
     9        (WebKit::WebsitePoliciesData::decode):
     10        * Shared/WebsitePoliciesData.h:
     11        * UIProcess/API/APIWebsitePolicies.h:
     12        * UIProcess/API/Cocoa/WKWebpagePreferences.mm:
     13        (-[WKWebpagePreferences _setActiveContentRuleListActionPatterns:]):
     14        (-[WKWebpagePreferences _activeContentRuleListActionPatterns]):
     15        * UIProcess/API/Cocoa/WKWebpagePreferencesPrivate.h:
     16
    1172021-12-05  Said Abou-Hallawa  <said@apple.com>
    218
  • trunk/Source/WebKit/Shared/WebsitePoliciesData.cpp

    r285980 r286545  
    6565        return std::nullopt;
    6666
    67     std::optional<std::optional<HashSet<String>>> activeContentRuleListActionPatterns;
     67    std::optional<HashMap<WTF::String, Vector<WTF::String>>> activeContentRuleListActionPatterns;
    6868    decoder >> activeContentRuleListActionPatterns;
    6969    if (!activeContentRuleListActionPatterns)
  • trunk/Source/WebKit/Shared/WebsitePoliciesData.h

    r285980 r286545  
    5454
    5555    bool contentBlockersEnabled { true };
    56     std::optional<HashSet<String>> activeContentRuleListActionPatterns { HashSet<String>() };
     56    HashMap<WTF::String, Vector<WTF::String>> activeContentRuleListActionPatterns;
    5757    OptionSet<WebsiteAutoplayQuirk> allowedAutoplayQuirks;
    5858    WebsiteAutoplayPolicy autoplayPolicy { WebsiteAutoplayPolicy::Default };
  • trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.h

    r285980 r286545  
    6464    void setContentBlockersEnabled(bool enabled) { m_contentBlockersEnabled = enabled; }
    6565   
    66     void setActiveContentRuleListActionPatterns(std::optional<HashSet<WTF::String>>&& patterns) { m_activeContentRuleListActionPatterns = WTFMove(patterns); }
    67     const std::optional<HashSet<WTF::String>>& activeContentRuleListActionPatterns() const { return m_activeContentRuleListActionPatterns; }
     66    void setActiveContentRuleListActionPatterns(HashMap<WTF::String, Vector<WTF::String>>&& patterns) { m_activeContentRuleListActionPatterns = WTFMove(patterns); }
     67    const HashMap<WTF::String, Vector<WTF::String>>& activeContentRuleListActionPatterns() const { return m_activeContentRuleListActionPatterns; }
    6868   
    6969    OptionSet<WebKit::WebsiteAutoplayQuirk> allowedAutoplayQuirks() const { return m_allowedAutoplayQuirks; }
     
    141141    // FIXME: replace most or all of these members with a WebsitePoliciesData.
    142142    bool m_contentBlockersEnabled { true };
    143     std::optional<HashSet<WTF::String>> m_activeContentRuleListActionPatterns { HashSet<WTF::String>() };
     143    HashMap<WTF::String, Vector<WTF::String>> m_activeContentRuleListActionPatterns;
    144144    OptionSet<WebKit::WebsiteAutoplayQuirk> m_allowedAutoplayQuirks;
    145145    WebKit::WebsiteAutoplayPolicy m_autoplayPolicy { WebKit::WebsiteAutoplayPolicy::Default };
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebpagePreferences.mm

    r286267 r286545  
    142142}
    143143
    144 - (void)_setActiveContentRuleListActionPatterns:(NSSet<NSString *> *)patterns
    145 {
    146     if (!patterns) {
    147         _websitePolicies->setActiveContentRuleListActionPatterns(std::nullopt);
    148         return;
    149     }
    150 
    151     HashSet<String> patternHashSet;
    152     patternHashSet.reserveInitialCapacity(patterns.count);
    153     for (NSString *pattern in patterns)
    154         patternHashSet.add(pattern);
    155     _websitePolicies->setActiveContentRuleListActionPatterns(WTFMove(patternHashSet));
    156 }
    157 
    158 - (NSSet<NSString *> *)_activeContentRuleListActionPatterns
    159 {
    160     const auto& patterns = _websitePolicies->activeContentRuleListActionPatterns();
    161     if (!patterns)
    162         return nil;
    163 
    164     NSMutableSet<NSString *> *set = [NSMutableSet set];
    165     for (const auto& pattern : *patterns)
    166         [set addObject:pattern];
    167     return set;
     144- (void)_setActiveContentRuleListActionPatterns:(NSDictionary<NSString *, NSSet<NSString *> *> *)patterns
     145{
     146    __block HashMap<String, Vector<String>> map;
     147    [patterns enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSSet<NSString *> *value, BOOL *) {
     148        Vector<String> vector;
     149        vector.reserveInitialCapacity(value.count);
     150        for (NSString *pattern in value)
     151            vector.uncheckedAppend(pattern);
     152        map.add(key, WTFMove(vector));
     153    }];
     154    _websitePolicies->setActiveContentRuleListActionPatterns(WTFMove(map));
     155}
     156
     157- (NSDictionary<NSString *, NSSet<NSString *> *> *)_activeContentRuleListActionPatterns
     158{
     159    NSMutableDictionary<NSString *, NSSet<NSString *> *> *dictionary = [NSMutableDictionary dictionary];
     160    for (const auto& pair : _websitePolicies->activeContentRuleListActionPatterns()) {
     161        NSMutableSet<NSString *> *set = [NSMutableSet set];
     162        for (const auto& pattern : pair.value)
     163            [set addObject:pattern];
     164        [dictionary setObject:set forKey:pair.key];
     165    }
     166    return dictionary;
    168167}
    169168
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebpagePreferencesPrivate.h

    r285980 r286545  
    7070
    7171@property (nonatomic, setter=_setContentBlockersEnabled:) BOOL _contentBlockersEnabled;
    72 @property (nonatomic, copy, setter=_setActiveContentRuleListActionPatterns:) NSSet<NSString *> *_activeContentRuleListActionPatterns;
     72@property (nonatomic, copy, setter=_setActiveContentRuleListActionPatterns:) NSDictionary<NSString *, NSSet<NSString *> *> *_activeContentRuleListActionPatterns WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
    7373@property (nonatomic, setter=_setAllowedAutoplayQuirks:) _WKWebsiteAutoplayQuirk _allowedAutoplayQuirks;
    7474@property (nonatomic, setter=_setAutoplayPolicy:) _WKWebsiteAutoplayPolicy _autoplayPolicy;
  • trunk/Tools/ChangeLog

    r286538 r286545  
     12021-12-06  Alex Christensen  <achristensen@webkit.org>
     2
     3        WKWebpagePreferences._activeContentRuleListActionPatterns should be an NSDictionary of identifier to allowed patterns
     4        https://bugs.webkit.org/show_bug.cgi?id=233842
     5
     6        Reviewed by Timothy Hatcher.
     7
     8        * TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm:
     9        (navigationDelegateAllowingActiveActionsOnTestHost):
     10        (TEST_F):
     11
    1122021-12-05  Said Abou-Hallawa  <said@apple.com>
    213
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKContentExtensionStore.mm

    r286492 r286545  
    500500    static auto delegate = adoptNS([TestNavigationDelegate new]);
    501501    delegate.get().decidePolicyForNavigationActionWithPreferences = ^(WKNavigationAction *, WKWebpagePreferences *preferences, void (^decisionHandler)(WKNavigationActionPolicy, WKWebpagePreferences *)) {
    502         preferences._activeContentRuleListActionPatterns = [NSSet setWithObject:@"*://testhost/*"];
     502        preferences._activeContentRuleListActionPatterns = [NSDictionary dictionaryWithObject:[NSSet setWithObject:@"*://testhost/*"] forKey:@"testidentifier"];
    503503        decisionHandler(WKNavigationActionPolicyAllow, preferences);
    504504    };
     
    723723        switch (delegateAction) {
    724724        case DelegateAction::AllowAll:
    725             preferences._activeContentRuleListActionPatterns = nil;
     725            preferences._activeContentRuleListActionPatterns = [NSDictionary dictionaryWithObject:[NSSet setWithObject:@"*://*/*"] forKey:@"testidentifier"];
    726726            break;
    727727        case DelegateAction::AllowNone:
    728             preferences._activeContentRuleListActionPatterns = [NSSet set];
     728            preferences._activeContentRuleListActionPatterns = [NSDictionary dictionary];
    729729            break;
    730730        case DelegateAction::AllowTestHost:
    731             preferences._activeContentRuleListActionPatterns = [NSSet setWithObject:@"*://testhost/*"];
     731            preferences._activeContentRuleListActionPatterns = [NSDictionary dictionaryWithObject:[NSSet setWithObject:@"*://testhost/*"] forKey:@"testidentifier"];
    732732            break;
    733733        }
     
    774774    auto delegate = adoptNS([TestNavigationDelegate new]);
    775775    delegate.get().decidePolicyForNavigationActionWithPreferences = ^(WKNavigationAction *, WKWebpagePreferences *preferences, void (^decisionHandler)(WKNavigationActionPolicy, WKWebpagePreferences *)) {
    776         preferences._activeContentRuleListActionPatterns = nil;
     776        preferences._activeContentRuleListActionPatterns = [NSDictionary dictionaryWithObject:[NSSet setWithObject:@"*://testhost/*"] forKey:@"testidentifier"];
    777777        decisionHandler(WKNavigationActionPolicyAllow, preferences);
    778778    };
Note: See TracChangeset for help on using the changeset viewer.