Merge pull request #14475 from brave/adblock-rust-ios-content-blocking
Add content blocking conversion method for iOS
This commit is contained in:
@@ -127,6 +127,7 @@ template("cargo_build") {
|
||||
args += [ "--mac_deployment_target=" + mac_deployment_target ]
|
||||
} else if (is_ios) {
|
||||
args += [ "--ios_deployment_target=" + ios_deployment_target ]
|
||||
args += [ "--features=" + "ios" ]
|
||||
}
|
||||
|
||||
if (defined(_rustflags)) {
|
||||
|
||||
Generated
+1
@@ -21,6 +21,7 @@ dependencies = [
|
||||
"rmp-serde 0.15.5",
|
||||
"seahash",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"twoway",
|
||||
"url",
|
||||
]
|
||||
|
||||
@@ -18,6 +18,9 @@ crate-type = [ "staticlib" ]
|
||||
name = "brave_rust"
|
||||
path = "lib.rs"
|
||||
|
||||
[features]
|
||||
ios = ["adblock-ffi/ios"]
|
||||
|
||||
[profile.dev]
|
||||
panic = "abort"
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ name = "adblock"
|
||||
|
||||
[features]
|
||||
cbindgen = []
|
||||
ios = ["adblock/content-blocking"]
|
||||
|
||||
[profile.dev]
|
||||
panic = "abort"
|
||||
|
||||
@@ -30,3 +30,4 @@ features = ["cbindgen"]
|
||||
prefix = "C_"
|
||||
|
||||
[defines]
|
||||
"feature = ios" = "BUILDFLAG(IS_IOS)"
|
||||
|
||||
@@ -184,4 +184,8 @@ char* engine_hidden_class_id_selectors(struct C_Engine* engine,
|
||||
const char* const* exceptions,
|
||||
size_t exceptions_size);
|
||||
|
||||
#if BUILDFLAG(IS_IOS)
|
||||
char* convert_rules_to_content_blocking(const char* rules);
|
||||
#endif
|
||||
|
||||
#endif /* BRAVE_COMPONENTS_ADBLOCK_RUST_FFI_SRC_LIB_H_ */
|
||||
|
||||
@@ -373,3 +373,20 @@ pub unsafe extern "C" fn engine_hidden_class_id_selectors(
|
||||
.expect("Error: CString::new()")
|
||||
.into_raw()
|
||||
}
|
||||
|
||||
#[cfg(feature = "ios")]
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn convert_rules_to_content_blocking(rules: *const c_char) -> *mut c_char {
|
||||
let rules = CStr::from_ptr(rules).to_str().unwrap_or_else(|_| {
|
||||
eprintln!("Failed to parse filter list with invalid UTF-8 content");
|
||||
""
|
||||
});
|
||||
let mut filter_set = adblock::lists::FilterSet::new(true);
|
||||
filter_set.add_filter_list(&rules, Default::default());
|
||||
// `unwrap` is safe here because `into_content_blocking` only panics if the `FilterSet` was not
|
||||
// created in debug mode
|
||||
let (cb_rules, _) = filter_set.into_content_blocking().unwrap();
|
||||
CString::new(serde_json::to_string(&cb_rules).unwrap_or_else(|_| "".into()))
|
||||
.expect("Error: CString::new()")
|
||||
.into_raw()
|
||||
}
|
||||
|
||||
@@ -16,6 +16,16 @@ bool SetDomainResolver(DomainResolverCallback resolver) {
|
||||
return set_domain_resolver(resolver);
|
||||
}
|
||||
|
||||
#if BUILDFLAG(IS_IOS)
|
||||
const std::string ConvertRulesToContentBlockingRules(const std::string& rules) {
|
||||
char* content_blocking_json =
|
||||
convert_rules_to_content_blocking(rules.c_str());
|
||||
const std::string result = std::string(content_blocking_json);
|
||||
c_char_buffer_destroy(content_blocking_json);
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
FilterListMetadata::FilterListMetadata() = default;
|
||||
|
||||
FilterListMetadata::FilterListMetadata(C_FilterListMetadata* metadata) {
|
||||
|
||||
@@ -42,6 +42,11 @@ typedef C_DomainResolverCallback DomainResolverCallback;
|
||||
|
||||
bool ADBLOCK_EXPORT SetDomainResolver(DomainResolverCallback resolver);
|
||||
|
||||
#if BUILDFLAG(IS_IOS)
|
||||
const std::string ADBLOCK_EXPORT
|
||||
ConvertRulesToContentBlockingRules(const std::string& rules);
|
||||
#endif
|
||||
|
||||
typedef ADBLOCK_EXPORT struct FilterListMetadata {
|
||||
FilterListMetadata();
|
||||
explicit FilterListMetadata(C_FilterListMetadata* metadata);
|
||||
|
||||
@@ -100,6 +100,10 @@ OBJC_EXPORT
|
||||
/// The default domain resolver.
|
||||
@property(class, readonly) DomainResolverCallback defaultDomainResolver;
|
||||
|
||||
/// Converts ABP rules/filter sets into Content Blocker rules that can be used
|
||||
/// with ``WKWebView``
|
||||
+ (NSString*)contentBlockerRulesFromFilterSet:(NSString*)filterSet;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
@@ -131,4 +131,9 @@
|
||||
return brave_shields::AdBlockServiceDomainResolver;
|
||||
}
|
||||
|
||||
+ (NSString*)contentBlockerRulesFromFilterSet:(NSString*)filterSet {
|
||||
return base::SysUTF8ToNSString(adblock::ConvertRulesToContentBlockingRules(
|
||||
base::SysNSStringToUTF8(filterSet)));
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -63,6 +63,8 @@ def run_cargo(command, args):
|
||||
cargo_args.append("--manifest-path=" + args.manifest_path)
|
||||
cargo_args.append("--target-dir=" + args.build_path)
|
||||
cargo_args.append("--target=" + args.target)
|
||||
if command == "build" and args.features is not None:
|
||||
cargo_args.append("--features=" + args.features)
|
||||
subprocess.check_call(cargo_args, env=env)
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
@@ -111,6 +113,7 @@ def parse_args():
|
||||
parser.add_option("--rust_flag", action="append",
|
||||
dest="rust_flags",
|
||||
default=[])
|
||||
parser.add_option('--features')
|
||||
|
||||
options, _ = parser.parse_args()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user