scheme_host_port_matcher.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_BASE_SCHEME_HOST_PORT_MATCHER_H_
  5. #define NET_BASE_SCHEME_HOST_PORT_MATCHER_H_
  6. #include <string>
  7. #include <vector>
  8. #include "net/base/net_export.h"
  9. #include "net/base/scheme_host_port_matcher_rule.h"
  10. namespace net {
  11. // SchemeHostPortMatcher holds an ordered list of rules for matching URLs, that
  12. // is serializable to a string.
  13. //
  14. // Rules are evaluated in reverse, and each can be either an "include this URL"
  15. // or an "exclude this URL".
  16. //
  17. // In a simple configuration, all rules are "include this URL" so evaluation
  18. // order doesn't matter. When combining include and exclude rules,
  19. // later rules will have precedence over earlier rules.
  20. class NET_EXPORT SchemeHostPortMatcher {
  21. public:
  22. using RuleList = std::vector<std::unique_ptr<SchemeHostPortMatcherRule>>;
  23. // Note: This class is movable but not copiable.
  24. SchemeHostPortMatcher() = default;
  25. SchemeHostPortMatcher(SchemeHostPortMatcher&& rhs) = default;
  26. SchemeHostPortMatcher& operator=(SchemeHostPortMatcher&& rhs) = default;
  27. ~SchemeHostPortMatcher() = default;
  28. // The delimiter used by |ToString()|.
  29. constexpr static char kPrintRuleListDelimiter = ';';
  30. // The delimiters to use when parsing rules.
  31. constexpr static char kParseRuleListDelimiterList[] = ",;";
  32. // Creates a SchemeHostPortMatcher by best-effort parsing each of the
  33. // |kParseRuleListDelimiterList| separated rules. Any rules that could not be
  34. // parsed are silently rejected. It only parses all the rule types that appear
  35. // in scheme_host_port_rules.h, types with other serializations will need to
  36. // be handled by the caller.
  37. static SchemeHostPortMatcher FromRawString(const std::string& raw);
  38. // Returns the current list of rules.
  39. const RuleList& rules() const { return rules_; }
  40. // Add rule to the matcher as the first one in the rule list.
  41. void AddAsFirstRule(std::unique_ptr<SchemeHostPortMatcherRule> rule);
  42. // Add rule to the matcher as the last one in the rule list.
  43. void AddAsLastRule(std::unique_ptr<SchemeHostPortMatcherRule> rule);
  44. // Replace rule on |index| in the internal RuleList.
  45. void ReplaceRule(size_t index,
  46. std::unique_ptr<SchemeHostPortMatcherRule> rule);
  47. // Returns true if |url| was positively matched by the rules.
  48. bool Includes(const GURL& url) const;
  49. // Returns the result of evaluating the rule list. Prefer using Includes()
  50. // over this function. Evaluate() can be used when the caller needs to
  51. // distinguish when matches were due to negative rules.
  52. SchemeHostPortMatcherResult Evaluate(const GURL& url) const;
  53. // Serializes the rules to a string representation. The serialized
  54. // representation is a |kPrintRuleListDelimiter| delimited list of rules,
  55. // where each rule defines its own serialization.
  56. std::string ToString() const;
  57. // Removes all the rules.
  58. void Clear();
  59. private:
  60. RuleList rules_;
  61. };
  62. } // namespace net
  63. #endif // NET_BASE_SCHEME_HOST_PORT_MATCHER_H_