Home | History | Annotate | Download | only in common
      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_COMMON_URL_PATTERN_SET_H_
      6 #define EXTENSIONS_COMMON_URL_PATTERN_SET_H_
      7 
      8 #include <iosfwd>
      9 #include <set>
     10 
     11 #include "base/memory/scoped_ptr.h"
     12 #include "extensions/common/url_pattern.h"
     13 
     14 class GURL;
     15 
     16 namespace base {
     17 class ListValue;
     18 class Value;
     19 }
     20 
     21 namespace extensions {
     22 
     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;
     28 
     29   // Clears |out| and populates the set with |set1| - |set2|.
     30   static void CreateDifference(const URLPatternSet& set1,
     31                                const URLPatternSet& set2,
     32                                URLPatternSet* out);
     33 
     34   // Clears |out| and populates the set with the intersection of |set1|
     35   // and |set2|.
     36   static void CreateIntersection(const URLPatternSet& set1,
     37                                  const URLPatternSet& set2,
     38                                  URLPatternSet* out);
     39 
     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,
     43                           URLPatternSet* out);
     44 
     45   // Clears |out| and populates it with the union of all sets in |sets|.
     46   static void CreateUnion(const std::vector<URLPatternSet>& sets,
     47                           URLPatternSet* out);
     48 
     49   URLPatternSet();
     50   URLPatternSet(const URLPatternSet& rhs);
     51   explicit URLPatternSet(const std::set<URLPattern>& patterns);
     52   ~URLPatternSet();
     53 
     54   URLPatternSet& operator=(const URLPatternSet& rhs);
     55   bool operator==(const URLPatternSet& rhs) const;
     56 
     57   bool is_empty() const;
     58   size_t size() 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(); }
     62 
     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);
     66 
     67   // Adds all patterns from |set| into this.
     68   void AddPatterns(const URLPatternSet& set);
     69 
     70   void ClearPatterns();
     71 
     72   // Adds a pattern based on |origin| to the set.
     73   bool AddOrigin(int valid_schemes, const GURL& origin);
     74 
     75   // Returns true if every URL that matches |set| is matched by this. In other
     76   // words, if every pattern in |set| is encompassed by a pattern in this.
     77   bool Contains(const URLPatternSet& set) const;
     78 
     79   // Returns true if any pattern in this set encompasses |pattern|.
     80   bool ContainsPattern(const URLPattern& pattern) const;
     81 
     82   // Test if the extent contains a URL.
     83   bool MatchesURL(const GURL& url) const;
     84 
     85   // Test if the extent matches all URLs (for example, <all_urls>).
     86   bool MatchesAllURLs() const;
     87 
     88   bool MatchesSecurityOrigin(const GURL& origin) const;
     89 
     90   // Returns true if there is a single URL that would be in two extents.
     91   bool OverlapsWith(const URLPatternSet& other) const;
     92 
     93   // Converts to and from Value for serialization to preferences.
     94   scoped_ptr<base::ListValue> ToValue() const;
     95   bool Populate(const base::ListValue& value,
     96                 int valid_schemes,
     97                 bool allow_file_access,
     98                 std::string* error);
     99 
    100   // Converts to and from a vector of strings.
    101   scoped_ptr<std::vector<std::string> > ToStringVector() const;
    102   bool Populate(const std::vector<std::string>& patterns,
    103                 int valid_schemes,
    104                 bool allow_file_access,
    105                 std::string* error);
    106 
    107  private:
    108   // The list of URL patterns that comprise the extent.
    109   std::set<URLPattern> patterns_;
    110 };
    111 
    112 std::ostream& operator<<(std::ostream& out,
    113                          const URLPatternSet& url_pattern_set);
    114 
    115 }  // namespace extensions
    116 
    117 #endif  // EXTENSIONS_COMMON_URL_PATTERN_SET_H_
    118