File: ruleset_matcher.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (128 lines) | stat: -rw-r--r-- 4,714 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// 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.

#ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_RULESET_MATCHER_H_
#define EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_RULESET_MATCHER_H_

#include <cstdint>
#include <memory>
#include <optional>
#include <string>

#include "base/containers/flat_set.h"
#include "base/memory/raw_ptr.h"
#include "extensions/browser/api/declarative_net_request/constants.h"
#include "extensions/browser/api/declarative_net_request/extension_url_pattern_index_matcher.h"
#include "extensions/browser/api/declarative_net_request/flat/extension_ruleset_generated.h"
#include "extensions/browser/api/declarative_net_request/regex_rules_matcher.h"
#include "extensions/common/api/declarative_net_request/constants.h"

namespace content {
class NavigationHandle;
class RenderFrameHost;
}  // namespace content

namespace extensions::declarative_net_request {

struct RuleCounts;

namespace flat {
struct ExtensionIndexedRuleset;
struct UrlRuleMetadata;
}  // namespace flat

// RulesetMatcher encapsulates the Declarative Net Request API ruleset
// corresponding to a single RulesetSource. Since this class is immutable, it is
// thread-safe.
// TODO(karandeepb): Rename to RulesetSourceMatcher since this no longer
// inherits from RulesetMatcherBase.
class RulesetMatcher {
 public:
  RulesetMatcher(std::string ruleset_data,
                 RulesetID id,
                 const ExtensionId& extension_id);

  RulesetMatcher(const RulesetMatcher&) = delete;
  RulesetMatcher& operator=(const RulesetMatcher&) = delete;

  ~RulesetMatcher();

  // Returns an action to be performed on the request. `stage` denotes both the
  // request stage and which pool of rules are to be matched against the
  // request.
  std::optional<RequestAction> GetAction(const RequestParams& params,
                                         RulesetMatchingStage stage) const;

  // Returns a list of actions corresponding to all matched
  // modifyHeaders rules with priority greater than `min_priority` if specified.
  std::vector<RequestAction> GetModifyHeadersActions(
      const RequestParams& params,
      RulesetMatchingStage stage,
      std::optional<uint64_t> min_priority) const;

  bool IsExtraHeadersMatcher() const;

  // Returns the total rule count for this ruleset, across all request matching
  // stages
  size_t GetRulesCount() const;
  std::optional<size_t> GetUnsafeRulesCount() const;

  // Returns the regex rule count for this ruleset, across all request matching
  // stages
  size_t GetRegexRulesCount() const;

  // Returns a RuleCounts object for this matcher containing the total rule
  // count, the unsafe rule count and the regex rule count.
  RuleCounts GetRuleCounts() const;

  void OnRenderFrameCreated(content::RenderFrameHost* host);
  void OnRenderFrameDeleted(content::RenderFrameHost* host);
  void OnDidFinishNavigation(content::NavigationHandle* navigation_handle);

  // ID of the ruleset. Each extension can have multiple rulesets with
  // their own unique ids.
  RulesetID id() const { return id_; }

  // Returns the tracked highest priority matching allowsAllRequests action, if
  // any, for `host`.
  std::optional<RequestAction> GetAllowlistedFrameActionForTesting(
      content::RenderFrameHost* host) const;

  // Set the disabled rule ids to the ruleset matcher.
  void SetDisabledRuleIds(base::flat_set<int> disabled_rule_ids);

  // Returns the disabled rule ids for testing.
  const base::flat_set<int>& GetDisabledRuleIdsForTesting() const;

  // Returns the total rule count for rules within this ruleset to be matched
  // for the given request matching `stage`.
  size_t GetRulesCount(RulesetMatchingStage stage) const;

 private:
  // Returns the regex rule count for rules within this ruleset to be matched
  // for the given request matching `stage`.
  size_t GetRegexRulesCount(RulesetMatchingStage stage) const;

  const std::string ruleset_data_;

  const raw_ptr<const flat::ExtensionIndexedRuleset> root_;

  const RulesetID id_;

  // The number of unsafe rules for this matcher. Computed only for dynamic and
  // session scoped rulesets as all rules for static rulesets are considered
  // "safe".
  std::optional<size_t> unsafe_rule_count_ = std::nullopt;

  // Underlying matcher for filter-list style rules supported using the
  // `url_pattern_index` component.
  ExtensionUrlPatternIndexMatcher url_matcher_;

  // Underlying matcher for regex rules.
  RegexRulesMatcher regex_matcher_;
};

}  // namespace extensions::declarative_net_request

#endif  // EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_RULESET_MATCHER_H_