url_pattern_set.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (c) 2012 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 EXTENSIONS_COMMON_URL_PATTERN_SET_H_
  5. #define EXTENSIONS_COMMON_URL_PATTERN_SET_H_
  6. #include <stddef.h>
  7. #include <iosfwd>
  8. #include <memory>
  9. #include <set>
  10. #include "extensions/common/url_pattern.h"
  11. class GURL;
  12. namespace base {
  13. class ListValue;
  14. class Value;
  15. }
  16. namespace url {
  17. class Origin;
  18. }
  19. namespace extensions {
  20. // Represents the set of URLs an extension uses for web content.
  21. class URLPatternSet {
  22. public:
  23. typedef std::set<URLPattern>::const_iterator const_iterator;
  24. typedef std::set<URLPattern>::iterator iterator;
  25. // Returns |set1| - |set2|.
  26. static URLPatternSet CreateDifference(const URLPatternSet& set1,
  27. const URLPatternSet& set2);
  28. enum class IntersectionBehavior {
  29. // For the following descriptions, consider the two URLPatternSets:
  30. // Set 1: {"https://example.com/*", "https://*.google.com/*", "http://*/*"}
  31. // Set 2: {"https://example.com/*", "https://google.com/maps",
  32. // "*://chromium.org/*"}
  33. // Only includes patterns that are exactly in both sets. The intersection of
  34. // the two sets above is {"https://example.com/*"}, since that is the only
  35. // pattern that appears exactly in each.
  36. kStringComparison,
  37. // Includes patterns that are effectively contained by both sets. The
  38. // intersection of the two sets above is
  39. // {
  40. // "https://example.com/*" (contained exactly by each set)
  41. // "https://google.com/maps" (contained exactly by set 2 and a strict
  42. // subset of https://*.google.com/* in set 1)
  43. // }
  44. kPatternsContainedByBoth,
  45. // Includes patterns that are contained by both sets and creates new
  46. // patterns to represent the intersection of any others. The intersection of
  47. // the two sets above is
  48. // {
  49. // "https://example.com/*" (contained exactly by each set)
  50. // "https://google.com/maps" (contained exactly by set 2 and a strict
  51. // subset of https://*.google.com/* in set 1)
  52. // "http://chromium.org/*" (the overlap between "http://*/*" in set 1 and
  53. // *://chromium.org/*" in set 2).
  54. // }
  55. // Note that this is the most computationally expensive - potentially
  56. // O(n^2) - since it can require comparing each pattern in one set to every
  57. // pattern in the other set.
  58. kDetailed,
  59. };
  60. // Returns the intersection of |set1| and |set2| according to
  61. // |intersection_behavior|.
  62. static URLPatternSet CreateIntersection(
  63. const URLPatternSet& set1,
  64. const URLPatternSet& set2,
  65. IntersectionBehavior intersection_behavior);
  66. // Returns the union of |set1| and |set2|.
  67. static URLPatternSet CreateUnion(const URLPatternSet& set1,
  68. const URLPatternSet& set2);
  69. URLPatternSet();
  70. URLPatternSet(URLPatternSet&& rhs);
  71. explicit URLPatternSet(const std::set<URLPattern>& patterns);
  72. URLPatternSet(const URLPatternSet&) = delete;
  73. URLPatternSet& operator=(const URLPatternSet&) = delete;
  74. ~URLPatternSet();
  75. URLPatternSet& operator=(URLPatternSet&& rhs);
  76. bool operator==(const URLPatternSet& rhs) const;
  77. bool is_empty() const;
  78. size_t size() const;
  79. const std::set<URLPattern>& patterns() const { return patterns_; }
  80. const_iterator begin() const { return patterns_.begin(); }
  81. const_iterator end() const { return patterns_.end(); }
  82. iterator erase(iterator iter) { return patterns_.erase(iter); }
  83. // Returns a copy of this URLPatternSet; not instrumented as a copy
  84. // constructor to avoid accidental/unnecessary copies.
  85. URLPatternSet Clone() const;
  86. // Adds a pattern to the set. Returns true if a new pattern was inserted,
  87. // false if the pattern was already in the set.
  88. bool AddPattern(const URLPattern& pattern);
  89. // Adds all patterns from |set| into this.
  90. void AddPatterns(const URLPatternSet& set);
  91. void ClearPatterns();
  92. // Adds a pattern based on |origin| to the set.
  93. bool AddOrigin(int valid_schemes, const GURL& origin);
  94. bool AddOrigin(int valid_schemes, const url::Origin& origin);
  95. // Returns true if every URL that matches |set| is matched by this. In other
  96. // words, if every pattern in |set| is encompassed by a pattern in this.
  97. bool Contains(const URLPatternSet& set) const;
  98. // Returns true if any pattern in this set encompasses |pattern|.
  99. bool ContainsPattern(const URLPattern& pattern) const;
  100. // Test if the extent contains a URL.
  101. bool MatchesURL(const GURL& url) const;
  102. // Test if the extent matches all URLs (for example, <all_urls>).
  103. bool MatchesAllURLs() const;
  104. bool MatchesSecurityOrigin(const GURL& origin) const;
  105. // Returns true if there is a single URL that would be in two extents.
  106. bool OverlapsWith(const URLPatternSet& other) const;
  107. // Converts to and from Value for serialization to preferences.
  108. std::unique_ptr<base::ListValue> ToValue() const;
  109. bool Populate(const base::ListValue& value,
  110. int valid_schemes,
  111. bool allow_file_access,
  112. std::string* error);
  113. // Converts to and from a vector of strings.
  114. std::unique_ptr<std::vector<std::string>> ToStringVector() const;
  115. bool Populate(const std::vector<std::string>& patterns,
  116. int valid_schemes,
  117. bool allow_file_access,
  118. std::string* error);
  119. private:
  120. // The list of URL patterns that comprise the extent.
  121. std::set<URLPattern> patterns_;
  122. };
  123. std::ostream& operator<<(std::ostream& out,
  124. const URLPatternSet& url_pattern_set);
  125. } // namespace extensions
  126. #endif // EXTENSIONS_COMMON_URL_PATTERN_SET_H_