Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / common / url_pattern_set.h
blobce16dd3d8caf5d0421acc70d2f2f81fcdf8fb16f
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.
5 #ifndef EXTENSIONS_COMMON_URL_PATTERN_SET_H_
6 #define EXTENSIONS_COMMON_URL_PATTERN_SET_H_
8 #include <iosfwd>
9 #include <set>
11 #include "base/memory/scoped_ptr.h"
12 #include "extensions/common/url_pattern.h"
14 class GURL;
16 namespace base {
17 class ListValue;
18 class Value;
21 namespace extensions {
23 // Represents the set of URLs an extension uses for web content.
24 class URLPatternSet {
25 public:
26 typedef std::set<URLPattern>::const_iterator const_iterator;
27 typedef std::set<URLPattern>::iterator iterator;
29 // Returns |set1| - |set2|.
30 static URLPatternSet CreateDifference(const URLPatternSet& set1,
31 const URLPatternSet& set2);
33 // Returns the intersection of |set1| and |set2|.
34 static URLPatternSet CreateIntersection(const URLPatternSet& set1,
35 const URLPatternSet& set2);
37 // Creates an intersection result where result has every element that is
38 // contained by both |set1| and |set2|. This is different than
39 // CreateIntersection(), which does string comparisons. For example, the
40 // semantic intersection of ("http://*.google.com/*") and
41 // ("http://google.com/*") is ("http://google.com/*"), but the result from
42 // CreateIntersection() would be ().
43 // TODO(devlin): This is weird. We probably want to use mostly
44 // CreateSemanticIntersection().
45 static URLPatternSet CreateSemanticIntersection(const URLPatternSet& set1,
46 const URLPatternSet& set2);
48 // Returns the union of |set1| and |set2|.
49 static URLPatternSet CreateUnion(const URLPatternSet& set1,
50 const URLPatternSet& set2);
52 // Returns the union of all sets in |sets|.
53 static URLPatternSet CreateUnion(const std::vector<URLPatternSet>& sets);
55 URLPatternSet();
56 URLPatternSet(const URLPatternSet& rhs);
57 explicit URLPatternSet(const std::set<URLPattern>& patterns);
58 ~URLPatternSet();
60 URLPatternSet& operator=(const URLPatternSet& rhs);
61 bool operator==(const URLPatternSet& rhs) const;
63 bool is_empty() const;
64 size_t size() const;
65 const std::set<URLPattern>& patterns() const { return patterns_; }
66 const_iterator begin() const { return patterns_.begin(); }
67 const_iterator end() const { return patterns_.end(); }
69 // Adds a pattern to the set. Returns true if a new pattern was inserted,
70 // false if the pattern was already in the set.
71 bool AddPattern(const URLPattern& pattern);
73 // Adds all patterns from |set| into this.
74 void AddPatterns(const URLPatternSet& set);
76 void ClearPatterns();
78 // Adds a pattern based on |origin| to the set.
79 bool AddOrigin(int valid_schemes, const GURL& origin);
81 // Returns true if every URL that matches |set| is matched by this. In other
82 // words, if every pattern in |set| is encompassed by a pattern in this.
83 bool Contains(const URLPatternSet& set) const;
85 // Returns true if any pattern in this set encompasses |pattern|.
86 bool ContainsPattern(const URLPattern& pattern) const;
88 // Test if the extent contains a URL.
89 bool MatchesURL(const GURL& url) const;
91 // Test if the extent matches all URLs (for example, <all_urls>).
92 bool MatchesAllURLs() const;
94 bool MatchesSecurityOrigin(const GURL& origin) const;
96 // Returns true if there is a single URL that would be in two extents.
97 bool OverlapsWith(const URLPatternSet& other) const;
99 // Converts to and from Value for serialization to preferences.
100 scoped_ptr<base::ListValue> ToValue() const;
101 bool Populate(const base::ListValue& value,
102 int valid_schemes,
103 bool allow_file_access,
104 std::string* error);
106 // Converts to and from a vector of strings.
107 scoped_ptr<std::vector<std::string> > ToStringVector() const;
108 bool Populate(const std::vector<std::string>& patterns,
109 int valid_schemes,
110 bool allow_file_access,
111 std::string* error);
113 private:
114 // The list of URL patterns that comprise the extent.
115 std::set<URLPattern> patterns_;
118 std::ostream& operator<<(std::ostream& out,
119 const URLPatternSet& url_pattern_set);
121 } // namespace extensions
123 #endif // EXTENSIONS_COMMON_URL_PATTERN_SET_H_