#include "components/url_pattern_index/url_pattern_index.h"
#include <limits>
#include <string>
#include <string_view>
#include <utility>
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ref.h"
#include "base/no_destructor.h"
#include "base/not_fatal_until.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_util.h"
#include "base/trace_event/trace_event.h"
#include "components/url_pattern_index/ngram_extractor.h"
#include "components/url_pattern_index/url_pattern.h"
#include "components/url_pattern_index/url_rule_util.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_constants.h"
#include "url/url_util.h"
namespace url_pattern_index {
namespace {
FlatUrlRuleList;
ActivationTypeMap;
ElementTypeMap;
const ActivationTypeMap& GetActivationTypeMap() { … }
const ElementTypeMap& GetElementTypeMap() { … }
flat::ActivationType ProtoToFlatActivationType(proto::ActivationType type) { … }
flat::ElementType ProtoToFlatElementType(proto::ElementType type) { … }
std::string_view ToStringPiece(const flatbuffers::String* string) { … }
bool HasNoUpperAscii(std::string_view string) { … }
bool UrlRuleDescendingPriorityComparator(const flat::UrlRule* lhs,
const flat::UrlRule* rhs) { … }
template <typename T>
int GetKeysMask(const T& map) { … }
class UrlRuleFlatBufferConverter { … };
}
bool OffsetVectorCompare::operator()(
const std::vector<FlatStringOffset>& a,
const std::vector<FlatStringOffset>& b) const { … }
UrlRuleOffset SerializeUrlRule(const proto::UrlRule& rule,
flatbuffers::FlatBufferBuilder* builder,
FlatDomainMap* domain_map) { … }
int CompareDomains(std::string_view lhs_domain, std::string_view rhs_domain) { … }
UrlPatternIndexBuilder::UrlPatternIndexBuilder(
flatbuffers::FlatBufferBuilder* flat_builder)
: … { … }
UrlPatternIndexBuilder::~UrlPatternIndexBuilder() = default;
void UrlPatternIndexBuilder::IndexUrlRule(UrlRuleOffset offset) { … }
UrlPatternIndexOffset UrlPatternIndexBuilder::Finish() { … }
NGram UrlPatternIndexBuilder::GetMostDistinctiveNGram(
std::string_view pattern) { … }
namespace {
FlatNGramIndex;
size_t GetLongestMatchingSubdomain(std::string_view host,
const FlatDomains& domains) { … }
const flat::UrlRule* FindMatchAmongCandidates(
const FlatUrlRuleList* sorted_candidates,
const UrlPattern::UrlInfo& url,
const url::Origin& document_origin,
flat::ElementType element_type,
flat::ActivationType activation_type,
flat::RequestMethod request_method,
bool is_third_party,
bool disable_generic_rules,
const UrlPatternIndexMatcher::EmbedderConditionsMatcher&
embedder_conditions_matcher,
std::vector<const flat::UrlRule*>* matched_rules,
const base::flat_set<int>& disabled_rule_ids) { … }
const flat::UrlRule* FindMatchInFlatUrlPatternIndex(
const flat::UrlPatternIndex& index,
const UrlPattern::UrlInfo& url,
const url::Origin& document_origin,
flat::ElementType element_type,
flat::ActivationType activation_type,
flat::RequestMethod request_method,
bool is_third_party,
bool disable_generic_rules,
const UrlPatternIndexMatcher::EmbedderConditionsMatcher&
embedder_conditions_matcher,
UrlPatternIndexMatcher::FindRuleStrategy strategy,
std::vector<const flat::UrlRule*>* matched_rules,
const base::flat_set<int>& disabled_rule_ids) { … }
}
bool IsRuleGeneric(const flat::UrlRule& rule) { … }
bool DoesHostMatchDomainLists(
std::string_view host,
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*
domains_included,
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*
domains_excluded) { … }
bool DoesURLMatchRequestDomainList(const UrlPattern::UrlInfo& url,
const flat::UrlRule& rule) { … }
bool DoesOriginMatchInitiatorDomainList(const url::Origin& origin,
const flat::UrlRule& rule) { … }
bool DoesRuleFlagsMatch(const flat::UrlRule& rule,
flat::ElementType element_type,
flat::ActivationType activation_type,
flat::RequestMethod request_method,
bool is_third_party,
const UrlPatternIndexMatcher::EmbedderConditionsMatcher&
embedder_conditions_matcher) { … }
UrlPatternIndexMatcher::UrlPatternIndexMatcher(
const flat::UrlPatternIndex* flat_index)
: … { … }
UrlPatternIndexMatcher::~UrlPatternIndexMatcher() = default;
UrlPatternIndexMatcher::UrlPatternIndexMatcher(UrlPatternIndexMatcher&&) =
default;
UrlPatternIndexMatcher& UrlPatternIndexMatcher::operator=(
UrlPatternIndexMatcher&&) = default;
size_t UrlPatternIndexMatcher::GetRulesCount() const { … }
const flat::UrlRule* UrlPatternIndexMatcher::FindMatch(
const GURL& url,
const url::Origin& first_party_origin,
proto::ElementType element_type,
proto::ActivationType activation_type,
bool is_third_party,
bool disable_generic_rules,
const EmbedderConditionsMatcher& embedder_conditions_matcher,
FindRuleStrategy strategy,
const base::flat_set<int>& disabled_rule_ids) const { … }
const flat::UrlRule* UrlPatternIndexMatcher::FindMatch(
const GURL& url,
const url::Origin& first_party_origin,
flat::ElementType element_type,
flat::ActivationType activation_type,
flat::RequestMethod request_method,
bool is_third_party,
bool disable_generic_rules,
const EmbedderConditionsMatcher& embedder_conditions_matcher,
FindRuleStrategy strategy,
const base::flat_set<int>& disabled_rule_ids) const { … }
std::vector<const flat::UrlRule*> UrlPatternIndexMatcher::FindAllMatches(
const GURL& url,
const url::Origin& first_party_origin,
proto::ElementType element_type,
proto::ActivationType activation_type,
bool is_third_party,
bool disable_generic_rules,
const EmbedderConditionsMatcher& embedder_conditions_matcher,
const base::flat_set<int>& disabled_rule_ids) const { … }
std::vector<const flat::UrlRule*> UrlPatternIndexMatcher::FindAllMatches(
const GURL& url,
const url::Origin& first_party_origin,
flat::ElementType element_type,
flat::ActivationType activation_type,
flat::RequestMethod request_method,
bool is_third_party,
bool disable_generic_rules,
const EmbedderConditionsMatcher& embedder_conditions_matcher,
const base::flat_set<int>& disabled_rule_ids) const { … }
}