#include "extensions/browser/api/declarative_net_request/rules_monitor_service.h"
#include <utility>
#include <vector>
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/containers/queue.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/ranges/algorithm.h"
#include "base/task/sequenced_task_runner.h"
#include "base/threading/thread_restrictions.h"
#include "content/public/browser/browser_task_traits.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/file_backed_ruleset_source.h"
#include "extensions/browser/api/declarative_net_request/file_sequence_helper.h"
#include "extensions/browser/api/declarative_net_request/parse_info.h"
#include "extensions/browser/api/declarative_net_request/rule_counts.h"
#include "extensions/browser/api/declarative_net_request/ruleset_manager.h"
#include "extensions/browser/api/declarative_net_request/ruleset_matcher.h"
#include "extensions/browser/api/web_request/extension_web_request_event_router.h"
#include "extensions/browser/api/web_request/permission_helper.h"
#include "extensions/browser/api/web_request/web_request_event_router_factory.h"
#include "extensions/browser/disable_reason.h"
#include "extensions/browser/extension_file_task_runner.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_prefs_factory.h"
#include "extensions/browser/extension_registry_factory.h"
#include "extensions/browser/unloaded_extension_reason.h"
#include "extensions/browser/warning_service.h"
#include "extensions/browser/warning_service_factory.h"
#include "extensions/browser/warning_set.h"
#include "extensions/common/api/declarative_net_request.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/extension_id.h"
#include "extensions/common/permissions/api_permission.h"
#include "tools/json_schema_compiler/util.h"
namespace extensions {
namespace declarative_net_request {
namespace {
dnr_api;
static base::LazyInstance<
BrowserContextKeyedAPIFactory<RulesMonitorService>>::Leaky g_factory = …;
bool RulesetInfoCompareByID(const RulesetInfo& lhs, const RulesetInfo& rhs) { … }
void LogLoadRulesetResult(LoadRulesetResult result) { … }
bool ShouldReleaseAllocationOnUnload(const ExtensionPrefs* prefs,
const Extension& extension,
UnloadedExtensionReason reason) { … }
std::unique_ptr<RulesetMatcher> CreateSessionScopedMatcher(
const ExtensionId& extension_id,
std::vector<api::declarative_net_request::Rule> rules,
std::string* error) { … }
HostPermissionsAlwaysRequired GetHostPermissionsAlwaysRequired(
const Extension& extension) { … }
}
class RulesMonitorService::FileSequenceBridge { … };
class RulesMonitorService::ApiCallQueue { … };
BrowserContextKeyedAPIFactory<RulesMonitorService>*
RulesMonitorService::GetFactoryInstance() { … }
std::unique_ptr<RulesMonitorService>
RulesMonitorService::CreateInstanceForTesting(
content::BrowserContext* context) { … }
RulesMonitorService* RulesMonitorService::Get(
content::BrowserContext* browser_context) { … }
void RulesMonitorService::UpdateDynamicRules(
const Extension& extension,
std::vector<int> rule_ids_to_remove,
std::vector<api::declarative_net_request::Rule> rules_to_add,
ApiCallback callback) { … }
void RulesMonitorService::UpdateEnabledStaticRulesets(
const Extension& extension,
std::set<RulesetID> ids_to_disable,
std::set<RulesetID> ids_to_enable,
ApiCallback callback) { … }
void RulesMonitorService::UpdateStaticRules(const Extension& extension,
RulesetID ruleset_id,
RuleIdsToUpdate rule_ids_to_update,
ApiCallback callback) { … }
void RulesMonitorService::GetDisabledRuleIds(
const Extension& extension,
RulesetID ruleset_id,
ApiCallbackToGetDisabledRuleIds callback) { … }
const base::Value::List& RulesMonitorService::GetSessionRulesValue(
const ExtensionId& extension_id) const { … }
std::vector<api::declarative_net_request::Rule>
RulesMonitorService::GetSessionRules(const ExtensionId& extension_id) const { … }
void RulesMonitorService::UpdateSessionRules(
const Extension& extension,
std::vector<int> rule_ids_to_remove,
std::vector<api::declarative_net_request::Rule> rules_to_add,
ApiCallback callback) { … }
RuleCounts RulesMonitorService::GetRuleCounts(const ExtensionId& extension_id,
RulesetID id) const { … }
RulesMonitorService::RulesMonitorService(
content::BrowserContext* browser_context)
: … { … }
RulesMonitorService::~RulesMonitorService() = default;
void RulesMonitorService::OnExtensionWillBeInstalled(
content::BrowserContext* browser_context,
const Extension* extension,
bool is_update,
const std::string& old_name) { … }
void RulesMonitorService::OnExtensionLoaded(
content::BrowserContext* browser_context,
const Extension* extension) { … }
void RulesMonitorService::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionReason reason) { … }
void RulesMonitorService::OnExtensionUninstalled(
content::BrowserContext* browser_context,
const Extension* extension,
UninstallReason reason) { … }
void RulesMonitorService::UpdateDynamicRulesInternal(
const ExtensionId& extension_id,
std::vector<int> rule_ids_to_remove,
std::vector<api::declarative_net_request::Rule> rules_to_add,
ApiCallback callback) { … }
void RulesMonitorService::UpdateSessionRulesInternal(
const ExtensionId& extension_id,
std::vector<int> rule_ids_to_remove,
std::vector<api::declarative_net_request::Rule> rules_to_add,
ApiCallback callback) { … }
void RulesMonitorService::UpdateEnabledStaticRulesetsInternal(
const ExtensionId& extension_id,
std::set<RulesetID> ids_to_disable,
std::set<RulesetID> ids_to_enable,
ApiCallback callback) { … }
void RulesMonitorService::UpdateStaticRulesInternal(
const ExtensionId& extension_id,
RulesetID ruleset_id,
RuleIdsToUpdate rule_ids_to_update,
ApiCallback callback) { … }
void RulesMonitorService::GetDisabledRuleIdsInternal(
const ExtensionId& extension_id,
RulesetID ruleset_id,
ApiCallbackToGetDisabledRuleIds callback) { … }
void RulesMonitorService::OnInitialRulesetsLoadedFromDisk(
LoadRequestData load_data) { … }
void RulesMonitorService::OnNewStaticRulesetsLoaded(
ApiCallback callback,
std::set<RulesetID> ids_to_disable,
std::set<RulesetID> ids_to_enable,
LoadRequestData load_data) { … }
void RulesMonitorService::OnDynamicRulesUpdated(
ApiCallback callback,
LoadRequestData load_data,
std::optional<std::string> error) { … }
void RulesMonitorService::RemoveCompositeMatcher(
const ExtensionId& extension_id) { … }
void RulesMonitorService::AddCompositeMatcher(
const Extension& extension,
CompositeMatcher::MatcherList matchers) { … }
void RulesMonitorService::UpdateRulesetMatcher(
const Extension& extension,
std::unique_ptr<RulesetMatcher> ruleset_matcher) { … }
void RulesMonitorService::LogMetricsAndUpdateChecksumsIfNeeded(
const LoadRequestData& load_data) { … }
}
template <>
void BrowserContextKeyedAPIFactory<
declarative_net_request::RulesMonitorService>::
DeclareFactoryDependencies() { … }
}