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
5#ifndef EXTENSIONS_COMMMON_URL_PATTERN_SET_H_
6#define EXTENSIONS_COMMMON_URL_PATTERN_SET_H_
7
8#include <set>
9
10#include "base/memory/scoped_ptr.h"
11#include "extensions/common/url_pattern.h"
12
13class GURL;
14
15namespace base {
16class ListValue;
17class Value;
18}
19
20namespace extensions {
21
22// Represents the set of URLs an extension uses for web content.
23class URLPatternSet {
24 public:
25  typedef std::set<URLPattern>::const_iterator const_iterator;
26  typedef std::set<URLPattern>::iterator iterator;
27
28  // Clears |out| and populates the set with |set1| - |set2|.
29  static void CreateDifference(const URLPatternSet& set1,
30                               const URLPatternSet& set2,
31                               URLPatternSet* out);
32
33  // Clears |out| and populates the set with the intersection of |set1|
34  // and |set2|.
35  static void CreateIntersection(const URLPatternSet& set1,
36                                 const URLPatternSet& set2,
37                                 URLPatternSet* out);
38
39  // Clears |out| and populates the set with the union of |set1| and |set2|.
40  static void CreateUnion(const URLPatternSet& set1,
41                          const URLPatternSet& set2,
42                          URLPatternSet* out);
43
44  // Clears |out| and populates it with the union of all sets in |sets|.
45  static void CreateUnion(const std::vector<URLPatternSet>& sets,
46                          URLPatternSet* out);
47
48  URLPatternSet();
49  URLPatternSet(const URLPatternSet& rhs);
50  explicit URLPatternSet(const std::set<URLPattern>& patterns);
51  ~URLPatternSet();
52
53  URLPatternSet& operator=(const URLPatternSet& rhs);
54  bool operator==(const URLPatternSet& rhs) const;
55
56  bool is_empty() const;
57  size_t size() const;
58  const std::set<URLPattern>& patterns() const { return patterns_; }
59  const_iterator begin() const { return patterns_.begin(); }
60  const_iterator end() const { return patterns_.end(); }
61
62  // Adds a pattern to the set. Returns true if a new pattern was inserted,
63  // false if the pattern was already in the set.
64  bool AddPattern(const URLPattern& pattern);
65
66  // Adds all patterns from |set| into this.
67  void AddPatterns(const URLPatternSet& set);
68
69  void ClearPatterns();
70
71  // Returns true if every URL that matches |set| is matched by this. In other
72  // words, if every pattern in |set| is encompassed by a pattern in this.
73  bool Contains(const URLPatternSet& set) const;
74
75  // Returns true if any pattern in this set encompasses |pattern|.
76  bool ContainsPattern(const URLPattern& pattern) const;
77
78  // Test if the extent contains a URL.
79  bool MatchesURL(const GURL& url) const;
80
81  bool MatchesSecurityOrigin(const GURL& origin) const;
82
83  // Returns true if there is a single URL that would be in two extents.
84  bool OverlapsWith(const URLPatternSet& other) const;
85
86  // Converts to and from Value for serialization to preferences.
87  scoped_ptr<base::ListValue> ToValue() const;
88  bool Populate(const base::ListValue& value,
89                int valid_schemes,
90                bool allow_file_access,
91                std::string* error);
92
93  bool Populate(const std::vector<std::string>& patterns,
94                int valid_schemes,
95                bool allow_file_access,
96                std::string* error);
97
98 private:
99  // The list of URL patterns that comprise the extent.
100  std::set<URLPattern> patterns_;
101};
102
103}  // namespace extensions
104
105#endif  // EXTENSIONS_COMMMON_URL_PATTERN_SET_H_
106