chromium/extensions/browser/api/declarative_net_request/utils.cc

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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;

// The ruleset format version of the flatbuffer schema. Increment this whenever
// making an incompatible change to the schema at extension_ruleset.fbs or
// url_pattern_index.fbs. Whenever an extension with an indexed ruleset format
// version different from the one currently used by Chrome is loaded, the
// extension ruleset will be reindexed.
constexpr int kIndexedRulesetFormatVersion =;

// This static assert is meant to catch cases where
// url_pattern_index::kUrlPatternIndexFormatVersion is incremented without
// updating 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() {}

// Returns the header to be used for indexed rulesets. This depends on the
// current ruleset format version.
std::string GetVersionHeader() {}

// Helper to ensure pointers to string literals can be used with
// base::JoinString.
std::string JoinString(base::span<const char* const> parts) {}

}  // namespace

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) {}

// Maps WebRequestResourceType to dnr_api::ResourceType.
dnr_api::ResourceType GetDNRResourceType(WebRequestResourceType resource_type) {}

// Maps dnr_api::ResourceType to WebRequestResourceType.
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(  // IN-TEST
    int limit) {}

ScopedRuleLimitOverride
CreateScopedUnsafeDynamicRuleLimitOverrideForTesting(  // IN-TEST
    int limit) {}

ScopedRuleLimitOverride
CreateScopedSessionRuleLimitOverrideForTesting(  // IN-TEST
    int limit) {}

ScopedRuleLimitOverride
CreateScopedUnsafeSessionRuleLimitOverrideForTesting(  // IN-TEST
    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) {}

// TODO(crbug.com/40869593): Add a parameter that allows more specific strings
// for error messages that can pinpoint the error within a single rule.
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) {}

// Maps an HTTP request method string to flat_rule::RequestMethod.
// Returns `flat::RequestMethod_NON_HTTP` for non-HTTP(s) requests, and
// `flat::RequestMethod_OTHER_HTTP` for HTTP(s) requests with an unknown
// request method.
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() {}

}  // namespace extensions::declarative_net_request