Truly independent web browser
LibWeb: Pass content blocker strings as AK spans
Andreas Kling706c0b50f46c
diff --git a/Libraries/LibWeb/ContentBlocker/Rust/cbindgen.toml b/Libraries/LibWeb/ContentBlocker/Rust/cbindgen.tomlindex 61e56a9a921..cec7fa6caf6 100644--- a/Libraries/LibWeb/ContentBlocker/Rust/cbindgen.toml+++ b/Libraries/LibWeb/ContentBlocker/Rust/cbindgen.toml@@ -11,6 +11,7 @@ line_length = 120tab_width = 4no_includes = truesys_includes = ["stdint.h", "stddef.h"]+includes = ["AK/Span.h"]usize_is_size_t = true[parse]@@ -21,6 +22,13 @@ all_features = false[export]include = ["ContentBlockerString"]+# Opaque to C++, which names it by a void pointer (see below).+exclude = ["ContentBlockerEngine"]++[export.rename]+# An engine is opaque to C++, which holds it by pointer.+"ContentBlockerEngine" = "void"+"Span" = "AK::ReadonlySpan"[export.mangle]rename_types = "PascalCase"diff --git a/Libraries/LibWeb/ContentBlocker/Rust/src/lib.rs b/Libraries/LibWeb/ContentBlocker/Rust/src/lib.rsindex 16de68a6043..289f087beb3 100644--- a/Libraries/LibWeb/ContentBlocker/Rust/src/lib.rs+++ b/Libraries/LibWeb/ContentBlocker/Rust/src/lib.rs@@ -16,7 +16,6 @@ mod rust_panic;use crate::rust_panic::abort_on_panic;use std::collections::HashMap;use std::collections::HashSet;-use std::ffi::c_void;#[repr(C)]pub struct ContentBlockerString {@@ -24,7 +23,7 @@ pub struct ContentBlockerString {length: usize,}-struct ContentBlockerEngine {+pub struct ContentBlockerEngine {engine: adblock::engine::Engine,generic_selector_list_rules: GenericSelectorListRules,}@@ -41,26 +40,9 @@ enum SelectorKey {Id(String),}-unsafe fn bytes_from_raw<'a>(bytes: *const u8, len: usize) -> Option<&'a [u8]> {- if len == 0 {- return Some(&[]);- }- if bytes.is_null() {- return None;- }- Some(unsafe { ak::slice_from_raw_parts(bytes, len) })-}--unsafe fn string_from_raw<'a>(bytes: *const u8, len: usize) -> Option<&'a str> {- let bytes = unsafe { bytes_from_raw(bytes, len)? };- std::str::from_utf8(bytes).ok()-}--unsafe fn engine_from_raw<'a>(engine: *const c_void) -> Option<&'a ContentBlockerEngine> {- if engine.is_null() {- return None;- }- Some(unsafe { &*engine.cast::<ContentBlockerEngine>() })+/// The bytes as a string, or none where they are not UTF-8.+fn as_str(bytes: ak::Span<'_, u8>) -> Option<&str> {+ std::str::from_utf8(bytes.as_slice()).ok()}fn string_to_ffi(string: String) -> ContentBlockerString {@@ -357,13 +339,11 @@ fn has_generic_cosmetic_selectors_for_url()}-/// # Safety-/// - `rules` and `rules_len` must point to a valid UTF-8 string-/// - The returned pointer must be freed with `rust_content_blocker_free`+/// An engine for `rules`, which C++ frees with `rust_content_blocker_free`, or null where the rules are not UTF-8.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_content_blocker_create(rules: *const u8, rules_len: usize) -> *mut c_void {+pub extern "C" fn rust_content_blocker_create(rules: ak::Span<'_, u8>) -> *mut ContentBlockerEngine {abort_on_panic(|| {- let Some(rules) = (unsafe { string_from_raw(rules, rules_len) }) else {+ let Some(rules) = as_str(rules) else {return std::ptr::null_mut();};@@ -375,45 +355,39 @@ pub unsafe extern "C" fn rust_content_blocker_create(rules: *const u8, rules_lenengine,generic_selector_list_rules,};- Box::into_raw(Box::new(engine)).cast()+ Box::into_raw(Box::new(engine))})}/// # Safety/// - `engine` must be null or a valid pointer returned by `rust_content_blocker_create`#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_content_blocker_free(engine: *mut c_void) {+pub unsafe extern "C" fn rust_content_blocker_free(engine: *mut ContentBlockerEngine) {abort_on_panic(|| {if !engine.is_null() {- drop(unsafe { Box::from_raw(engine.cast::<ContentBlockerEngine>()) });+ drop(unsafe { Box::from_raw(engine) });}});}-/// # Safety-/// - `engine` must be null or a valid pointer returned by `rust_content_blocker_create`-/// - String pointers and lengths must point to valid UTF-8 strings#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_content_blocker_matches(- engine: *const c_void,- url: *const u8,- url_len: usize,- source_url: *const u8,- source_url_len: usize,- request_type: *const u8,- request_type_len: usize,+pub extern "C" fn rust_content_blocker_matches(+ engine: Option<&ContentBlockerEngine>,+ url: ak::Span<'_, u8>,+ source_url: ak::Span<'_, u8>,+ request_type: ak::Span<'_, u8>,) -> bool {abort_on_panic(|| {- let Some(engine) = (unsafe { engine_from_raw(engine) }) else {+ let Some(engine) = engine else {return false;};- let Some(url) = (unsafe { string_from_raw(url, url_len) }) else {+ let Some(url) = as_str(url) else {return false;};- let Some(source_url) = (unsafe { string_from_raw(source_url, source_url_len) }) else {+ let Some(source_url) = as_str(source_url) else {return false;};- let Some(request_type) = (unsafe { string_from_raw(request_type, request_type_len) }) else {+ let Some(request_type) = as_str(request_type) else {return false;};@@ -426,31 +400,25 @@ pub unsafe extern "C" fn rust_content_blocker_matches(})}-/// # Safety-/// - `engine` must be null or a valid pointer returned by `rust_content_blocker_create`-/// - String pointers and lengths must point to valid UTF-8 strings-/// - The returned string must be freed with `rust_content_blocker_free_string`+/// C++ frees the returned string with `rust_content_blocker_free_string`.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_content_blocker_cosmetic_css(- engine: *const c_void,- url: *const u8,- url_len: usize,- classes: *const u8,- classes_len: usize,- ids: *const u8,- ids_len: usize,+pub extern "C" fn rust_content_blocker_cosmetic_css(+ engine: Option<&ContentBlockerEngine>,+ url: ak::Span<'_, u8>,+ classes: ak::Span<'_, u8>,+ ids: ak::Span<'_, u8>,) -> ContentBlockerString {abort_on_panic(|| {- let Some(engine) = (unsafe { engine_from_raw(engine) }) else {+ let Some(engine) = engine else {return string_to_ffi(String::new());};- let Some(url) = (unsafe { string_from_raw(url, url_len) }) else {+ let Some(url) = as_str(url) else {return string_to_ffi(String::new());};- let Some(classes) = (unsafe { string_from_raw(classes, classes_len) }) else {+ let Some(classes) = as_str(classes) else {return string_to_ffi(String::new());};- let Some(ids) = (unsafe { string_from_raw(ids, ids_len) }) else {+ let Some(ids) = as_str(ids) else {return string_to_ffi(String::new());};@@ -460,30 +428,24 @@ pub unsafe extern "C" fn rust_content_blocker_cosmetic_css(})}-/// # Safety-/// - `engine` must be null or a valid pointer returned by `rust_content_blocker_create`-/// - String pointers and lengths must point to valid UTF-8 strings#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_content_blocker_has_generic_cosmetic_selectors(- engine: *const c_void,- url: *const u8,- url_len: usize,- classes: *const u8,- classes_len: usize,- ids: *const u8,- ids_len: usize,+pub extern "C" fn rust_content_blocker_has_generic_cosmetic_selectors(+ engine: Option<&ContentBlockerEngine>,+ url: ak::Span<'_, u8>,+ classes: ak::Span<'_, u8>,+ ids: ak::Span<'_, u8>,) -> bool {abort_on_panic(|| {- let Some(engine) = (unsafe { engine_from_raw(engine) }) else {+ let Some(engine) = engine else {return false;};- let Some(url) = (unsafe { string_from_raw(url, url_len) }) else {+ let Some(url) = as_str(url) else {return false;};- let Some(classes) = (unsafe { string_from_raw(classes, classes_len) }) else {+ let Some(classes) = as_str(classes) else {return false;};- let Some(ids) = (unsafe { string_from_raw(ids, ids_len) }) else {+ let Some(ids) = as_str(ids) else {return false;};diff --git a/Libraries/LibWeb/Loader/ContentBlocker.cpp b/Libraries/LibWeb/Loader/ContentBlocker.cppindex b8fae59b073..bccd55ddf64 100644--- a/Libraries/LibWeb/Loader/ContentBlocker.cpp+++ b/Libraries/LibWeb/Loader/ContentBlocker.cpp@@ -161,9 +161,7 @@ ErrorOr<void> ContentBlocker::set_rules_from_bytes(ReadonlyBytes rules_bytes)return {};}- auto* engine = ContentBlocking::FFI::rust_content_blocker_create(- rules_bytes.data(),- rules_bytes.size());+ auto* engine = ContentBlocking::FFI::rust_content_blocker_create(rules_bytes);if (!engine)return Error::from_string_literal("Failed to create content blocker");@@ -200,12 +198,9 @@ bool ContentBlocker::is_filtered(URL::URL const& url, URL::URL const& source_urlreturn ContentBlocking::FFI::rust_content_blocker_matches(m_engine,- reinterpret_cast<u8 const*>(url_string.characters()),- url_string.length(),- reinterpret_cast<u8 const*>(source_url_string.characters()),- source_url_string.length(),- reinterpret_cast<u8 const*>(request_type.characters_without_null_termination()),- request_type.length());+ url_string.bytes(),+ source_url_string.bytes(),+ request_type.bytes());}Utf16String ContentBlocker::cosmetic_style_sheet_for_url(URL::URL const& url) const@@ -232,12 +227,9 @@ Utf16String ContentBlocker::cosmetic_style_sheet_for_url(URL::URL const& url, Rereturn take_rust_utf16_string(ContentBlocking::FFI::rust_content_blocker_cosmetic_css(m_engine,- reinterpret_cast<u8 const*>(url_string.characters()),- url_string.length(),- reinterpret_cast<u8 const*>(classes_bytes.characters_without_null_termination()),- classes_bytes.length(),- reinterpret_cast<u8 const*>(ids_bytes.characters_without_null_termination()),- ids_bytes.length()));+ url_string.bytes(),+ classes_bytes.bytes(),+ ids_bytes.bytes()));}bool ContentBlocker::has_generic_cosmetic_selectors_for_url(URL::URL const& url, ReadonlySpan<Utf16FlyString> classes, ReadonlySpan<Utf16FlyString> ids) const@@ -259,12 +251,9 @@ bool ContentBlocker::has_generic_cosmetic_selectors_for_url(URL::URL const& url,return ContentBlocking::FFI::rust_content_blocker_has_generic_cosmetic_selectors(m_engine,- reinterpret_cast<u8 const*>(url_string.characters()),- url_string.length(),- reinterpret_cast<u8 const*>(classes_bytes.characters_without_null_termination()),- classes_bytes.length(),- reinterpret_cast<u8 const*>(ids_bytes.characters_without_null_termination()),- ids_bytes.length());+ url_string.bytes(),+ classes_bytes.bytes(),+ ids_bytes.bytes());}ContentBlocker::ResourceType ContentBlocker::resource_type_from_fetch_metadata(Optional<Fetch::Infrastructure::Request::Destination> const& destination, Optional<Fetch::Infrastructure::Request::InitiatorType> const& initiator_type, Fetch::Infrastructure::Request::Mode mode)