Replace the local substring matcher with the adblock-rust engine exposed through the dedicated content blocker Rust FFI. Keep the previous engine when a replacement list cannot be parsed. Pass shared list buffers directly into Rust instead of building a duplicate C++ vector of lines first. Generate cosmetic CSS through the Rust matcher, including generic class and id selectors collected from shadow-including descendants. Keep a supplemental index for generic cosmetic selector-list rules. adblock-rust indexes these rules under the first class or id token only, so also key them by later simple class and id selectors in the list. Update ContentBlocker coverage for rule options, exceptions, third-party checks, blob and file URLs, invalid list replacement, filtering toggles, cosmetic CSS, and generic selector-list cosmetics.
69 lines
2.2 KiB
C++
69 lines
2.2 KiB
C++
/*
|
|
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
|
|
* Copyright (c) 2025, Tim Ledbetter <tim.ledbetter@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Error.h>
|
|
#include <AK/Noncopyable.h>
|
|
#include <AK/String.h>
|
|
#include <LibURL/URL.h>
|
|
#include <LibWeb/Export.h>
|
|
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
|
|
#include <LibWeb/Forward.h>
|
|
|
|
namespace Web {
|
|
|
|
class WEB_API ContentBlocker {
|
|
AK_MAKE_NONCOPYABLE(ContentBlocker);
|
|
AK_MAKE_NONMOVABLE(ContentBlocker);
|
|
|
|
public:
|
|
enum class ResourceType : u8 {
|
|
Document,
|
|
Font,
|
|
Image,
|
|
Media,
|
|
Object,
|
|
Other,
|
|
Ping,
|
|
Script,
|
|
Stylesheet,
|
|
Subdocument,
|
|
WebSocket,
|
|
XMLHttpRequest,
|
|
};
|
|
|
|
static ContentBlocker& the();
|
|
|
|
bool has_rules() const { return m_engine != nullptr; }
|
|
bool has_cosmetic_rules() const { return m_has_cosmetic_rules; }
|
|
bool filtering_enabled() const { return m_filtering_enabled; }
|
|
void set_filtering_enabled(bool const enabled) { m_filtering_enabled = enabled; }
|
|
|
|
bool is_filtered(URL::URL const&) const;
|
|
bool is_filtered(URL::URL const&, URL::URL const& source_url, ResourceType) const;
|
|
bool is_filtered(URL::URL const&, URL::URL const& source_url, Optional<Fetch::Infrastructure::Request::Destination> const&, Optional<Fetch::Infrastructure::Request::InitiatorType> const&, Fetch::Infrastructure::Request::Mode) const;
|
|
ErrorOr<void> set_patterns(ReadonlySpan<String>);
|
|
ErrorOr<void> set_rules_from_bytes(ReadonlyBytes);
|
|
|
|
String cosmetic_style_sheet_for_document(DOM::Document const&) const;
|
|
String cosmetic_style_sheet_for_url(URL::URL const&) const;
|
|
String cosmetic_style_sheet_for_url(URL::URL const&, ReadonlySpan<String> classes, ReadonlySpan<String> ids) const;
|
|
|
|
static ResourceType resource_type_from_fetch_metadata(Optional<Fetch::Infrastructure::Request::Destination> const&, Optional<Fetch::Infrastructure::Request::InitiatorType> const&, Fetch::Infrastructure::Request::Mode);
|
|
static URL::URL source_url_for_matching(URL::URL const&);
|
|
|
|
private:
|
|
ContentBlocker();
|
|
~ContentBlocker();
|
|
|
|
bool m_filtering_enabled { true };
|
|
bool m_has_cosmetic_rules { false };
|
|
void* m_engine { nullptr };
|
|
};
|
|
|
|
}
|