xref: /aosp_15_r20/external/cronet/net/base/scheme_host_port_matcher.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_BASE_SCHEME_HOST_PORT_MATCHER_H_
6 #define NET_BASE_SCHEME_HOST_PORT_MATCHER_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "net/base/net_export.h"
12 #include "net/base/scheme_host_port_matcher_rule.h"
13 
14 namespace net {
15 
16 // SchemeHostPortMatcher holds an ordered list of rules for matching URLs, that
17 // is serializable to a string.
18 //
19 // Rules are evaluated in reverse, and each can be either an "include this URL"
20 // or an "exclude this URL".
21 //
22 // In a simple configuration, all rules are "include this URL" so evaluation
23 // order doesn't matter. When combining include and exclude rules,
24 // later rules will have precedence over earlier rules.
25 class NET_EXPORT SchemeHostPortMatcher {
26  public:
27   using RuleList = std::vector<std::unique_ptr<SchemeHostPortMatcherRule>>;
28 
29   // Note: This class is movable but not copiable.
30   SchemeHostPortMatcher();
31   SchemeHostPortMatcher(SchemeHostPortMatcher&& rhs);
32   SchemeHostPortMatcher& operator=(SchemeHostPortMatcher&& rhs);
33   ~SchemeHostPortMatcher();
34 
35   // The delimiter used by |ToString()|.
36   constexpr static char kPrintRuleListDelimiter = ';';
37 
38   // The delimiters to use when parsing rules.
39   constexpr static char kParseRuleListDelimiterList[] = ",;";
40 
41   // Creates a SchemeHostPortMatcher by best-effort parsing each of the
42   // |kParseRuleListDelimiterList| separated rules. Any rules that could not be
43   // parsed are silently rejected. It only parses all the rule types that appear
44   // in scheme_host_port_rules.h, types with other serializations will need to
45   // be handled by the caller.
46   static SchemeHostPortMatcher FromRawString(const std::string& raw);
47 
48   // Returns the current list of rules.
rules()49   const RuleList& rules() const { return rules_; }
50 
51   // Add rule to the matcher as the first one in the rule list.
52   void AddAsFirstRule(std::unique_ptr<SchemeHostPortMatcherRule> rule);
53 
54   // Add rule to the matcher as the last one in the rule list.
55   void AddAsLastRule(std::unique_ptr<SchemeHostPortMatcherRule> rule);
56 
57   // Replace rule on |index| in the internal RuleList.
58   void ReplaceRule(size_t index,
59                    std::unique_ptr<SchemeHostPortMatcherRule> rule);
60 
61   // Returns true if |url| was positively matched by the rules.
62   bool Includes(const GURL& url) const;
63 
64   // Returns the result of evaluating the rule list. Prefer using Includes()
65   // over this function. Evaluate() can be used when the caller needs to
66   // distinguish when matches were due to negative rules.
67   SchemeHostPortMatcherResult Evaluate(const GURL& url) const;
68 
69   // Serializes the rules to a string representation. The serialized
70   // representation is a |kPrintRuleListDelimiter| delimited list of rules,
71   // where each rule defines its own serialization.
72   std::string ToString() const;
73 
74   // Removes all the rules.
75   void Clear();
76 
77 #if !BUILDFLAG(CRONET_BUILD)
78   // Cronet disables tracing and doesn't provide an implementation of
79   // base::trace_event::EstimateMemoryUsage. Having this conditional is
80   // preferred over a fake implementation to avoid reporting fake metrics.
81 
82   // Estimates dynamic memory usage.
83   // See base/trace_event/memory_usage_estimator.h for more info.
84   size_t EstimateMemoryUsage() const;
85 #endif  // !BUILDFLAG(CRONET_BUILD)
86 
87  private:
88   RuleList rules_;
89 };
90 
91 }  // namespace net
92 
93 #endif  // NET_BASE_SCHEME_HOST_PORT_MATCHER_H_
94