Skip to content

Commit

Permalink
Add logic to convert declarativeNetRequest rules to WebKit content bl…
Browse files Browse the repository at this point in the history
…ocker rules

https://bugs.webkit.org/show_bug.cgi?id=264982
rdar://118528800

Reviewed by Timothy Hatcher.

This PR also gets rid of all of the `__kindof`s in the CocoaHelpers template functions. It is causing compile errors
on older OSes.

* Source/WebKit/UIProcess/Extensions/Cocoa/_WKWebExtensionDeclarativeNetRequestRule.h: Added.
* Source/WebKit/UIProcess/Extensions/Cocoa/_WKWebExtensionDeclarativeNetRequestRule.mm: Added.
(-[_WKWebExtensionDeclarativeNetRequestRule _validateHeaderInfoDictionary:]): Validates the header info dictionary.
(isHeaderNameValid): Checks the name of the header against a list of known headers.
(isArrayOfDomainsValid): Makes sure there are domains and they can all be converted to ASCII.
(isArrayOfExcludedDomainsValid): Makes sure all domains can be converted to ASCII.
(-[_WKWebExtensionDeclarativeNetRequestRule removeInvalidResourceTypesForKey:]): Remove any resource types WebKit can't handle.
(-[_WKWebExtensionDeclarativeNetRequestRule ruleInWebKitFormat]): Convert the rule to WebKit format.
(-[_WKWebExtensionDeclarativeNetRequestRule _convertedRulesForWebKitActionType:chromeActionType:]): Convert action type from Chrome
to WebKit format.
(-[_WKWebExtensionDeclarativeNetRequestRule _webKitRuleWithWebKitActionType:chromeActionType:chromeResourceTypes:]): Create a content blocking
rule with the given parameters.
(-[_WKWebExtensionDeclarativeNetRequestRule _chromeResourceTypeToWebKitLoadContext]): Convert a resource type to a load context.
(-[_WKWebExtensionDeclarativeNetRequestRule _chromeDomainTypeToWebKitDomainType]): Convert a Chrome domain type to a WebKit domain type.
(-[_WKWebExtensionDeclarativeNetRequestRule _chromeResourceTypeToWebKitResourceType]): Convert resource types.
(-[_WKWebExtensionDeclarativeNetRequestRule _resourcesToTargetWhenNoneAreSpecifiedInRule]):
(-[_WKWebExtensionDeclarativeNetRequestRule _allChromeResourceTypes]):
(-[_WKWebExtensionDeclarativeNetRequestRule _convertedResourceTypesForChromeResourceTypes:]):
(-[_WKWebExtensionDeclarativeNetRequestRule _regexURLFilterForChromeURLFilter:]):
(-[_WKWebExtensionDeclarativeNetRequestRule compare:]): Compare two rules.
(priorityForRuleType):
(-[_WKWebExtensionDeclarativeNetRequestRule description]): Print out information about the rule.
* Source/WebKit/UIProcess/Extensions/Cocoa/_WKWebExtensionDeclarativeNetRequestTranslator.h: Added.
* Source/WebKit/UIProcess/Extensions/Cocoa/_WKWebExtensionDeclarativeNetRequestTranslator.mm: Added.
(+[_WKWebExtensionDeclarativeNetRequestTranslator translateRules:errorStrings:]): Translate the rules.
(+[_WKWebExtensionDeclarativeNetRequestTranslator jsonObjectsFromData:errorStrings:]): Perform JSON parsing.
(+[_WKWebExtensionDeclarativeNetRequestTranslator jsonStringForJSONObject:]): Ditto.
* Source/WebKit/WebKit.xcodeproj/project.pbxproj: Add the new files.

Canonical link: https://commits.webkit.org/270861@main
  • Loading branch information
b-weinstein committed Nov 17, 2023
1 parent ef43e42 commit f3d8dee
Show file tree
Hide file tree
Showing 7 changed files with 1,254 additions and 16 deletions.
20 changes: 10 additions & 10 deletions Source/WebKit/Platform/cocoa/CocoaHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,24 @@ class Data;

namespace WebKit {

template<typename T> T *filterObjects(T *container, bool NS_NOESCAPE (^block)(__kindof id key, __kindof id value));
template<> NSArray *filterObjects<NSArray>(NSArray *, bool NS_NOESCAPE (^block)(__kindof id key, __kindof id value));
template<> NSDictionary *filterObjects<NSDictionary>(NSDictionary *, bool NS_NOESCAPE (^block)(__kindof id key, __kindof id value));
template<> NSSet *filterObjects<NSSet>(NSSet *, bool NS_NOESCAPE (^block)(__kindof id key, __kindof id value));
template<typename T> T *filterObjects(T *container, bool NS_NOESCAPE (^block)(id key, id value));
template<> NSArray *filterObjects<NSArray>(NSArray *, bool NS_NOESCAPE (^block)(id key, id value));
template<> NSDictionary *filterObjects<NSDictionary>(NSDictionary *, bool NS_NOESCAPE (^block)(id key, id value));
template<> NSSet *filterObjects<NSSet>(NSSet *, bool NS_NOESCAPE (^block)(id key, id value));

template<typename T>
T *filterObjects(const RetainPtr<T>& container, bool NS_NOESCAPE (^block)(__kindof id key, __kindof id value))
T *filterObjects(const RetainPtr<T>& container, bool NS_NOESCAPE (^block)(id key, id value))
{
return filterObjects<T>(container.get(), block);
}

template<typename T> T *mapObjects(T *container, __kindof id NS_NOESCAPE (^block)(__kindof id key, __kindof id value));
template<> NSArray *mapObjects<NSArray>(NSArray *, __kindof id NS_NOESCAPE (^block)(__kindof id key, __kindof id value));
template<> NSDictionary *mapObjects<NSDictionary>(NSDictionary *, __kindof id NS_NOESCAPE (^block)(__kindof id key, __kindof id value));
template<> NSSet *mapObjects<NSSet>(NSSet *, __kindof id NS_NOESCAPE (^block)(__kindof id key, __kindof id value));
template<typename T> T *mapObjects(T *container, id NS_NOESCAPE (^block)(id key, id value));
template<> NSArray *mapObjects<NSArray>(NSArray *, id NS_NOESCAPE (^block)(id key, id value));
template<> NSDictionary *mapObjects<NSDictionary>(NSDictionary *, id NS_NOESCAPE (^block)(id key, id value));
template<> NSSet *mapObjects<NSSet>(NSSet *, id NS_NOESCAPE (^block)(id key, id value));

template<typename T>
T *mapObjects(const RetainPtr<T>& container, __kindof id NS_NOESCAPE (^block)(__kindof id key, __kindof id value))
T *mapObjects(const RetainPtr<T>& container, id NS_NOESCAPE (^block)(id key, id value))
{
return mapObjects<T>(container.get(), block);
}
Expand Down
12 changes: 6 additions & 6 deletions Source/WebKit/Platform/cocoa/CocoaHelpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
static NSString * const privacyPreservingDescriptionKey = @"privacyPreservingDescription";

template<>
NSArray *filterObjects<NSArray>(NSArray *array, bool NS_NOESCAPE (^block)(__kindof id key, __kindof id value))
NSArray *filterObjects<NSArray>(NSArray *array, bool NS_NOESCAPE (^block)(id key, id value))
{
if (!array)
return nil;
Expand All @@ -57,7 +57,7 @@
}

template<>
NSDictionary *filterObjects<NSDictionary>(NSDictionary *dictionary, bool NS_NOESCAPE (^block)(__kindof id key, __kindof id value))
NSDictionary *filterObjects<NSDictionary>(NSDictionary *dictionary, bool NS_NOESCAPE (^block)(id key, id value))
{
if (!dictionary)
return nil;
Expand All @@ -76,7 +76,7 @@
}

template<>
NSSet *filterObjects<NSSet>(NSSet *set, bool NS_NOESCAPE (^block)(__kindof id key, __kindof id value))
NSSet *filterObjects<NSSet>(NSSet *set, bool NS_NOESCAPE (^block)(id key, id value))
{
if (!set)
return nil;
Expand All @@ -90,7 +90,7 @@
}

template<>
NSArray *mapObjects<NSArray>(NSArray *array, __kindof id NS_NOESCAPE (^block)(__kindof id key, __kindof id value))
NSArray *mapObjects<NSArray>(NSArray *array, id NS_NOESCAPE (^block)(id key, id value))
{
if (!array)
return nil;
Expand Down Expand Up @@ -118,7 +118,7 @@
}

template<>
NSDictionary *mapObjects<NSDictionary>(NSDictionary *dictionary, __kindof id NS_NOESCAPE (^block)(__kindof id key, __kindof id value))
NSDictionary *mapObjects<NSDictionary>(NSDictionary *dictionary, id NS_NOESCAPE (^block)(id key, id value))
{
if (!dictionary)
return nil;
Expand All @@ -137,7 +137,7 @@
}

template<>
NSSet *mapObjects<NSSet>(NSSet *set, __kindof id NS_NOESCAPE (^block)(__kindof id key, __kindof id value))
NSSet *mapObjects<NSSet>(NSSet *set, id NS_NOESCAPE (^block)(id key, id value))
{
if (!set)
return nil;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#if ENABLE(WK_WEB_EXTENSIONS)

@interface _WKWebExtensionDeclarativeNetRequestRule : NSObject

- (instancetype)initWithDictionary:(NSDictionary *)ruleDictionary errorString:(NSString **)outErrorString NS_DESIGNATED_INITIALIZER;

- (instancetype)init NS_UNAVAILABLE;

- (NSComparisonResult)compare:(_WKWebExtensionDeclarativeNetRequestRule *)rule;

@property (nonatomic, readonly) NSInteger ruleID;
@property (nonatomic, readonly) NSInteger priority;
@property (nonatomic, readonly, copy) NSDictionary *action;
@property (nonatomic, readonly, copy) NSDictionary *condition;

@property (nonatomic, readonly) NSArray<NSDictionary<NSString *, id> *> *ruleInWebKitFormat;

@end

#endif // ENABLE(WK_WEB_EXTENSIONS)
Loading

0 comments on commit f3d8dee

Please sign in to comment.