• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "net/base/scheme_host_port_matcher.h"
6 
7 #include "base/containers/adapters.h"
8 #include "base/containers/contains.h"
9 #include "base/strings/string_tokenizer.h"
10 #include "base/strings/string_util.h"
11 
12 namespace net {
13 
14 SchemeHostPortMatcher::SchemeHostPortMatcher() = default;
15 SchemeHostPortMatcher::SchemeHostPortMatcher(SchemeHostPortMatcher&& rhs) =
16     default;
17 SchemeHostPortMatcher& SchemeHostPortMatcher::operator=(
18     SchemeHostPortMatcher&& rhs) = default;
19 SchemeHostPortMatcher::~SchemeHostPortMatcher() = default;
20 
21 // Declares SchemeHostPortMatcher::kParseRuleListDelimiterList[], not a
22 // redefinition. This is needed for link.
23 // static
24 constexpr char SchemeHostPortMatcher::kParseRuleListDelimiterList[];
25 
26 // Declares SchemeHostPortMatcher::kPrintRuleListDelimiter, not a
27 // redefinition. This is needed for link.
28 // static
29 constexpr char SchemeHostPortMatcher::kPrintRuleListDelimiter;
30 
31 // static
FromRawString(const std::string & raw)32 SchemeHostPortMatcher SchemeHostPortMatcher::FromRawString(
33     const std::string& raw) {
34   SchemeHostPortMatcher result;
35 
36   base::StringTokenizer entries(raw, kParseRuleListDelimiterList);
37   while (entries.GetNext()) {
38     auto rule = SchemeHostPortMatcherRule::FromUntrimmedRawString(
39         entries.token_piece());
40     if (rule) {
41       result.AddAsLastRule(std::move(rule));
42     }
43   }
44 
45   return result;
46 }
47 
AddAsFirstRule(std::unique_ptr<SchemeHostPortMatcherRule> rule)48 void SchemeHostPortMatcher::AddAsFirstRule(
49     std::unique_ptr<SchemeHostPortMatcherRule> rule) {
50   DCHECK(rule);
51   rules_.insert(rules_.begin(), std::move(rule));
52 }
53 
AddAsLastRule(std::unique_ptr<SchemeHostPortMatcherRule> rule)54 void SchemeHostPortMatcher::AddAsLastRule(
55     std::unique_ptr<SchemeHostPortMatcherRule> rule) {
56   DCHECK(rule);
57   rules_.push_back(std::move(rule));
58 }
59 
ReplaceRule(size_t index,std::unique_ptr<SchemeHostPortMatcherRule> rule)60 void SchemeHostPortMatcher::ReplaceRule(
61     size_t index,
62     std::unique_ptr<SchemeHostPortMatcherRule> rule) {
63   DCHECK_LT(index, rules_.size());
64   rules_[index] = std::move(rule);
65 }
66 
Includes(const GURL & url) const67 bool SchemeHostPortMatcher::Includes(const GURL& url) const {
68   return Evaluate(url) == SchemeHostPortMatcherResult::kInclude;
69 }
70 
Evaluate(const GURL & url) const71 SchemeHostPortMatcherResult SchemeHostPortMatcher::Evaluate(
72     const GURL& url) const {
73   // Later rules override earlier rules, so evaluating the rule list can be
74   // done by iterating over it in reverse and short-circuiting when a match is
75   // found.
76   //
77   // The order of evaluation generally doesn't matter if all the rules are
78   // positive rules, so matches are just additive.
79   //
80   // However when mixing positive and negative rules, evaluation order makes a
81   // difference.
82   for (const auto& rule : base::Reversed(rules_)) {
83     SchemeHostPortMatcherResult result = rule->Evaluate(url);
84     if (result != SchemeHostPortMatcherResult::kNoMatch)
85       return result;
86   }
87 
88   return SchemeHostPortMatcherResult::kNoMatch;
89 }
90 
ToString() const91 std::string SchemeHostPortMatcher::ToString() const {
92   std::string result;
93   for (const auto& rule : rules_) {
94     DCHECK(!base::Contains(rule->ToString(), kParseRuleListDelimiterList));
95     result += rule->ToString();
96     result.push_back(kPrintRuleListDelimiter);
97   }
98   return result;
99 }
100 
Clear()101 void SchemeHostPortMatcher::Clear() {
102   rules_.clear();
103 }
104 
105 }  // namespace net
106