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_
11 #include "base/memory/scoped_ptr.h"
12 #include "extensions/common/url_pattern.h"
21 namespace extensions
{
23 // Represents the set of URLs an extension uses for web content.
26 typedef std::set
<URLPattern
>::const_iterator const_iterator
;
27 typedef std::set
<URLPattern
>::iterator iterator
;
29 // Clears |out| and populates the set with |set1| - |set2|.
30 static void CreateDifference(const URLPatternSet
& set1
,
31 const URLPatternSet
& set2
,
34 // Clears |out| and populates the set with the intersection of |set1|
36 static void CreateIntersection(const URLPatternSet
& set1
,
37 const URLPatternSet
& set2
,
40 // Clears |out| and populates the set with the union of |set1| and |set2|.
41 static void CreateUnion(const URLPatternSet
& set1
,
42 const URLPatternSet
& set2
,
45 // Clears |out| and populates it with the union of all sets in |sets|.
46 static void CreateUnion(const std::vector
<URLPatternSet
>& sets
,
50 URLPatternSet(const URLPatternSet
& rhs
);
51 explicit URLPatternSet(const std::set
<URLPattern
>& patterns
);
54 URLPatternSet
& operator=(const URLPatternSet
& rhs
);
55 bool operator==(const URLPatternSet
& rhs
) const;
57 bool is_empty() const;
59 const std::set
<URLPattern
>& patterns() const { return patterns_
; }
60 const_iterator
begin() const { return patterns_
.begin(); }
61 const_iterator
end() const { return patterns_
.end(); }
63 // Adds a pattern to the set. Returns true if a new pattern was inserted,
64 // false if the pattern was already in the set.
65 bool AddPattern(const URLPattern
& pattern
);
67 // Adds all patterns from |set| into this.
68 void AddPatterns(const URLPatternSet
& set
);
72 // Returns true if every URL that matches |set| is matched by this. In other
73 // words, if every pattern in |set| is encompassed by a pattern in this.
74 bool Contains(const URLPatternSet
& set
) const;
76 // Returns true if any pattern in this set encompasses |pattern|.
77 bool ContainsPattern(const URLPattern
& pattern
) const;
79 // Test if the extent contains a URL.
80 bool MatchesURL(const GURL
& url
) const;
82 // Test if the extent matches all URLs (for example, <all_urls>).
83 bool MatchesAllURLs() const;
85 bool MatchesSecurityOrigin(const GURL
& origin
) const;
87 // Returns true if there is a single URL that would be in two extents.
88 bool OverlapsWith(const URLPatternSet
& other
) const;
90 // Converts to and from Value for serialization to preferences.
91 scoped_ptr
<base::ListValue
> ToValue() const;
92 bool Populate(const base::ListValue
& value
,
94 bool allow_file_access
,
97 // Converts to and from a vector of strings.
98 scoped_ptr
<std::vector
<std::string
> > ToStringVector() const;
99 bool Populate(const std::vector
<std::string
>& patterns
,
101 bool allow_file_access
,
105 // The list of URL patterns that comprise the extent.
106 std::set
<URLPattern
> patterns_
;
109 std::ostream
& operator<<(std::ostream
& out
,
110 const URLPatternSet
& url_pattern_set
);
112 } // namespace extensions
114 #endif // EXTENSIONS_COMMON_URL_PATTERN_SET_H_