#include "extensions/browser/api/declarative_net_request/utils.h"
#include <memory>
#include <set>
#include <string_view>
#include <utility>
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/hash/hash.h"
#include "base/metrics/histogram_functions.h"
#include "base/ranges/algorithm.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "components/url_pattern_index/url_pattern_index.h"
#include "components/version_info/channel.h"
#include "components/web_cache/browser/web_cache_manager.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/api/declarative_net_request/composite_matcher.h"
#include "extensions/browser/api/declarative_net_request/constants.h"
#include "extensions/browser/api/declarative_net_request/flat/extension_ruleset_generated.h"
#include "extensions/browser/api/declarative_net_request/ruleset_matcher.h"
#include "extensions/browser/api/web_request/web_request_info.h"
#include "extensions/browser/api/web_request/web_request_resource_type.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/common/api/declarative_net_request/constants.h"
#include "extensions/common/api/declarative_net_request/dnr_manifest_data.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/features/feature_channel.h"
#include "extensions/common/permissions/api_permission.h"
#include "extensions/common/permissions/permissions_data.h"
#include "third_party/flatbuffers/src/include/flatbuffers/flatbuffers.h"
namespace extensions::declarative_net_request {
namespace {
dnr_api;
flat_rule;
constexpr int kIndexedRulesetFormatVersion = …;
static_assert …;
constexpr int kInvalidIndexedRulesetFormatVersion = …;
int g_indexed_ruleset_format_version_for_testing = …;
constexpr int kInvalidOverrideChecksumForTest = …;
int g_override_checksum_for_test = …;
constexpr int kInvalidRuleLimit = …;
int g_static_guaranteed_minimum_for_testing = …;
int g_global_static_rule_limit_for_testing = …;
int g_regex_rule_limit_for_testing = …;
int g_dynamic_rule_limit_for_testing = …;
int g_unsafe_dynamic_rule_limit_for_testing = …;
int g_session_rule_limit_for_testing = …;
int g_unsafe_session_rule_limit_for_testing = …;
int g_disabled_static_rule_limit_for_testing = …;
int GetIndexedRulesetFormatVersion() { … }
std::string GetVersionHeader() { … }
std::string JoinString(base::span<const char* const> parts) { … }
}
std::string GetVersionHeaderForTesting() { … }
int GetIndexedRulesetFormatVersionForTesting() { … }
ScopedIncrementRulesetVersion CreateScopedIncrementRulesetVersionForTesting() { … }
bool StripVersionHeaderAndParseVersion(std::string* ruleset_data) { … }
int GetChecksum(base::span<const uint8_t> data) { … }
void OverrideGetChecksumForTest(int checksum) { … }
std::string GetIndexedRulesetData(base::span<const uint8_t> data) { … }
bool PersistIndexedRuleset(const base::FilePath& path,
base::span<const uint8_t> data) { … }
void ClearRendererCacheOnNavigation() { … }
void LogReadDynamicRulesStatus(ReadJSONRulesResult::Status status) { … }
dnr_api::ResourceType GetDNRResourceType(WebRequestResourceType resource_type) { … }
WebRequestResourceType GetWebRequestResourceType(
dnr_api::ResourceType resource_type) { … }
dnr_api::RequestDetails CreateRequestDetails(const WebRequestInfo& request) { … }
re2::RE2::Options CreateRE2Options(bool is_case_sensitive,
bool require_capturing) { … }
flat::ActionType ConvertToFlatActionType(dnr_api::RuleActionType action_type) { … }
std::string GetPublicRulesetID(const Extension& extension,
RulesetID ruleset_id) { … }
std::vector<std::string> GetPublicRulesetIDs(const Extension& extension,
const CompositeMatcher& matcher) { … }
int GetStaticGuaranteedMinimumRuleCount() { … }
int GetGlobalStaticRuleLimit() { … }
int GetMaximumRulesPerRuleset() { … }
int GetDynamicRuleLimit() { … }
int GetUnsafeDynamicRuleLimit() { … }
int GetSessionRuleLimit() { … }
int GetUnsafeSessionRuleLimit() { … }
int GetRegexRuleLimit() { … }
int GetDisabledStaticRuleLimit() { … }
ScopedRuleLimitOverride CreateScopedStaticGuaranteedMinimumOverrideForTesting(
int minimum) { … }
ScopedRuleLimitOverride CreateScopedGlobalStaticRuleLimitOverrideForTesting(
int limit) { … }
ScopedRuleLimitOverride CreateScopedRegexRuleLimitOverrideForTesting(
int limit) { … }
ScopedRuleLimitOverride
CreateScopedDynamicRuleLimitOverrideForTesting(
int limit) { … }
ScopedRuleLimitOverride
CreateScopedUnsafeDynamicRuleLimitOverrideForTesting(
int limit) { … }
ScopedRuleLimitOverride
CreateScopedSessionRuleLimitOverrideForTesting(
int limit) { … }
ScopedRuleLimitOverride
CreateScopedUnsafeSessionRuleLimitOverrideForTesting(
int limit) { … }
ScopedRuleLimitOverride CreateScopedDisabledStaticRuleLimitOverrideForTesting(
int limit) { … }
size_t GetEnabledStaticRuleCount(const CompositeMatcher* composite_matcher) { … }
bool HasAnyDNRPermission(const Extension& extension) { … }
bool HasDNRFeedbackPermission(const Extension* extension,
const std::optional<int>& tab_id) { … }
std::string GetParseError(ParseResult error_reason, int rule_id) { … }
flat_rule::ElementType GetElementType(WebRequestResourceType web_request_type) { … }
flat_rule::ElementType GetElementType(dnr_api::ResourceType resource_type) { … }
flat_rule::RequestMethod GetRequestMethod(bool http_or_https,
const std::string& method) { … }
flat_rule::RequestMethod GetRequestMethod(
dnr_api::RequestMethod request_method) { … }
flat_rule::RequestMethod GetRequestMethod(
bool http_or_https,
dnr_api::RequestMethod request_method) { … }
bool IsRuleSafe(const api::declarative_net_request::Rule& rule) { … }
bool IsRuleSafe(const flat::UrlRuleMetadata& url_rule_metadata) { … }
bool IsResponseHeaderMatchingEnabled() { … }
bool IsHeaderSubstitutionEnabled() { … }
}