url_util.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2018 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 COMPONENTS_URL_MATCHER_URL_UTIL_H_
  5. #define COMPONENTS_URL_MATCHER_URL_UTIL_H_
  6. #include "base/memory/scoped_refptr.h"
  7. #include "base/values.h"
  8. #include "components/url_matcher/url_matcher.h"
  9. #include "components/url_matcher/url_matcher_export.h"
  10. class GURL;
  11. namespace url_matcher {
  12. namespace util {
  13. // Converts a ValueList `value` of strings into a vector. Returns true if
  14. // successful.
  15. bool GetAsStringVector(const base::Value* value, std::vector<std::string>* out);
  16. // Normalizes a URL for matching purposes.
  17. URL_MATCHER_EXPORT GURL Normalize(const GURL& url);
  18. // Helper function to extract the underlying URL wrapped by services such as
  19. // Google AMP or Google Translate. Returns an empty GURL if `url` doesn't match
  20. // a known format.
  21. URL_MATCHER_EXPORT GURL GetEmbeddedURL(const GURL& url);
  22. // Utility struct used to represent a url filter scheme into its components.
  23. struct URL_MATCHER_EXPORT FilterComponents {
  24. FilterComponents();
  25. FilterComponents(const FilterComponents&) = delete;
  26. FilterComponents(FilterComponents&&);
  27. FilterComponents& operator=(const FilterComponents&) = delete;
  28. FilterComponents& operator=(FilterComponents&&) = default;
  29. ~FilterComponents();
  30. // Returns true if `this` represents the "*" filter.
  31. bool IsWildcard() const;
  32. std::string scheme;
  33. std::string host;
  34. uint16_t port = 0;
  35. std::string path;
  36. std::string query;
  37. // Number of conditions that a url needs to match it to be considered a match
  38. // for this filter.
  39. int number_of_url_matching_conditions = 0;
  40. bool match_subdomains = true;
  41. bool allow = true;
  42. };
  43. // Creates a condition set that can be used with the `url_matcher`. `id` needs
  44. // to be a unique number that will be returned by the `url_matcher` if the URL
  45. // matches that condition set. `allow` indicates if it is an allow-list (true)
  46. // or block-list (false) filter.
  47. URL_MATCHER_EXPORT scoped_refptr<url_matcher::URLMatcherConditionSet>
  48. CreateConditionSet(url_matcher::URLMatcher* url_matcher,
  49. base::MatcherStringPattern::ID id,
  50. const std::string& scheme,
  51. const std::string& host,
  52. bool match_subdomains,
  53. uint16_t port,
  54. const std::string& path,
  55. const std::string& query,
  56. bool allow);
  57. // Splits a URL filter into its components. A GURL isn't used because these
  58. // can be invalid URLs e.g. "google.com".
  59. // Returns false if the URL couldn't be parsed. In case false is returned,
  60. // the values of output parameters are undefined.
  61. // The `filter` should have the format described at
  62. // http://www.chromium.org/administrators/url-blocklist-filter-format and
  63. // accepts wildcards. The `host` is preprocessed so it can be passed to
  64. // URLMatcher for the appropriate condition. The optional username and password
  65. // are ignored. `match_subdomains` specifies whether the filter should include
  66. // subdomains of the hostname (if it is one.) `port` is 0 if none is explicitly
  67. // defined. `path` does not include query parameters. `query` contains the query
  68. // parameters ('?' not included). All arguments are mandatory.
  69. URL_MATCHER_EXPORT bool FilterToComponents(const std::string& filter,
  70. std::string* scheme,
  71. std::string* host,
  72. bool* match_subdomains,
  73. uint16_t* port,
  74. std::string* path,
  75. std::string* query);
  76. // Adds the filters in `patterns` to `url_matcher` as a ConditionSet::Vector.
  77. // `matcher` is the URLMatcher where filters are added.
  78. // `allow` specifies whether the filter accepts or blocks the macthed urls.
  79. // `id` is the id of given to the filter being added.
  80. // `patterns` is a list of url schemes following the format described
  81. // http://www.chromium.org/administrators/url-blocklist-filter-format and
  82. // accepts wildcards.
  83. // `filters` is an optional map of id to FilterComponent where the generated
  84. // FilterComponent will be added.
  85. URL_MATCHER_EXPORT void AddFilters(
  86. url_matcher::URLMatcher* matcher,
  87. bool allow,
  88. base::MatcherStringPattern::ID* id,
  89. const base::Value::List& patterns,
  90. std::map<base::MatcherStringPattern::ID,
  91. url_matcher::util::FilterComponents>* filters = nullptr);
  92. // Adds the filters in `patterns` to `url_matcher` as a ConditionSet::Vector.
  93. // `matcher` is the URLMatcher where filters are added.
  94. // `allow` specifies whether the filter accepts or blocks the macthed urls.
  95. // `id` is the id of given to the filter being added.
  96. // `patterns` is a list of url schemes following the format described
  97. // http://www.chromium.org/administrators/url-blocklist-filter-format and
  98. // accepts wildcards.
  99. // `filters` is an optional map of id to FilterComponent where the generated
  100. // FilterComponent will be added.
  101. URL_MATCHER_EXPORT void AddFilters(
  102. url_matcher::URLMatcher* matcher,
  103. bool allow,
  104. base::MatcherStringPattern::ID* id,
  105. const std::vector<std::string>& patterns,
  106. std::map<base::MatcherStringPattern::ID,
  107. url_matcher::util::FilterComponents>* filters = nullptr);
  108. URL_MATCHER_EXPORT void AddAllowFilters(url_matcher::URLMatcher* matcher,
  109. const base::Value::List& patterns);
  110. URL_MATCHER_EXPORT void AddAllowFilters(
  111. url_matcher::URLMatcher* matcher,
  112. const std::vector<std::string>& patterns);
  113. } // namespace util
  114. } // namespace url_matcher
  115. #endif // COMPONENTS_URL_MATCHER_URL_UTIL_H_