OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/common/url_pattern_set.h" | 5 #include "extensions/common/url_pattern_set.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 const char kInvalidURLPatternError[] = "Invalid url pattern '*'"; | 23 const char kInvalidURLPatternError[] = "Invalid url pattern '*'"; |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 // static | 27 // static |
28 void URLPatternSet::CreateDifference(const URLPatternSet& set1, | 28 void URLPatternSet::CreateDifference(const URLPatternSet& set1, |
29 const URLPatternSet& set2, | 29 const URLPatternSet& set2, |
30 URLPatternSet* out) { | 30 URLPatternSet* out) { |
31 out->ClearPatterns(); | 31 out->ClearPatterns(); |
32 out->patterns_ = base::STLSetDifference<std::set<URLPattern> >( | 32 for (URLPatternSet::const_iterator pattern1 = set1.patterns_.begin(); |
33 set1.patterns_, set2.patterns_); | 33 pattern1 != set1.patterns_.end(); ++pattern1) { |
| 34 if (!set2.ContainsPattern(*pattern1)) |
| 35 out->InsertPattern(*pattern1); |
| 36 } |
34 } | 37 } |
35 | 38 |
36 // static | 39 // static |
37 void URLPatternSet::CreateIntersection(const URLPatternSet& set1, | 40 void URLPatternSet::CreateIntersection(const URLPatternSet& set1, |
38 const URLPatternSet& set2, | 41 const URLPatternSet& set2, |
39 URLPatternSet* out) { | 42 URLPatternSet* out) { |
40 out->ClearPatterns(); | 43 out->ClearPatterns(); |
41 std::set_intersection(set1.patterns_.begin(), set1.patterns_.end(), | 44 for (URLPatternSet::const_iterator pattern1 = set1.patterns_.begin(); |
42 set2.patterns_.begin(), set2.patterns_.end(), | 45 pattern1 != set1.patterns_.end(); ++pattern1) { |
43 std::inserter<std::set<URLPattern> >( | 46 for (URLPatternSet::const_iterator pattern2 = set2.patterns_.begin(); |
44 out->patterns_, out->patterns_.begin())); | 47 pattern2 != set2.patterns_.end(); ++pattern2) { |
| 48 if (pattern1->Contains(*pattern2)) { |
| 49 out->InsertPattern(*pattern2); |
| 50 } else if (pattern2->Contains(*pattern1)) { |
| 51 out->InsertPattern(*pattern1); |
| 52 } |
| 53 } |
| 54 } |
45 } | 55 } |
46 | 56 |
47 // static | 57 // static |
48 void URLPatternSet::CreateUnion(const URLPatternSet& set1, | 58 void URLPatternSet::CreateUnion(const URLPatternSet& set1, |
49 const URLPatternSet& set2, | 59 const URLPatternSet& set2, |
50 URLPatternSet* out) { | 60 URLPatternSet* out) { |
51 out->ClearPatterns(); | 61 out->ClearPatterns(); |
52 std::set_union(set1.patterns_.begin(), set1.patterns_.end(), | 62 out->AddPatterns(set1); |
53 set2.patterns_.begin(), set2.patterns_.end(), | 63 out->AddPatterns(set2); |
54 std::inserter<std::set<URLPattern> >( | |
55 out->patterns_, out->patterns_.begin())); | |
56 } | 64 } |
57 | 65 |
58 // static | 66 // static |
59 void URLPatternSet::CreateUnion(const std::vector<URLPatternSet>& sets, | 67 void URLPatternSet::CreateUnion(const std::vector<URLPatternSet>& sets, |
60 URLPatternSet* out) { | 68 URLPatternSet* out) { |
61 out->ClearPatterns(); | 69 out->ClearPatterns(); |
62 if (sets.empty()) | 70 if (sets.empty()) |
63 return; | 71 return; |
64 | 72 |
65 // N-way union algorithm is basic O(nlog(n)) merge algorithm. | 73 // N-way union algorithm is basic O(nlog(n)) merge algorithm. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 117 |
110 bool URLPatternSet::is_empty() const { | 118 bool URLPatternSet::is_empty() const { |
111 return patterns_.empty(); | 119 return patterns_.empty(); |
112 } | 120 } |
113 | 121 |
114 size_t URLPatternSet::size() const { | 122 size_t URLPatternSet::size() const { |
115 return patterns_.size(); | 123 return patterns_.size(); |
116 } | 124 } |
117 | 125 |
118 bool URLPatternSet::AddPattern(const URLPattern& pattern) { | 126 bool URLPatternSet::AddPattern(const URLPattern& pattern) { |
119 return patterns_.insert(pattern).second; | 127 // Adding a more specific pattern should be a no-op. |
| 128 if (ContainsPattern(pattern)) |
| 129 return false; |
| 130 |
| 131 // Adding a more general pattern should remove the more specific patterns |
| 132 for (URLPatternSet::const_iterator it = patterns_.begin(); |
| 133 it != patterns_.end();) { |
| 134 if (pattern.Contains(*it)) |
| 135 patterns_.erase(it++); |
| 136 else |
| 137 ++it; |
| 138 } |
| 139 return InsertPattern(pattern); |
120 } | 140 } |
121 | 141 |
122 void URLPatternSet::AddPatterns(const URLPatternSet& set) { | 142 void URLPatternSet::AddPatterns(const URLPatternSet& set) { |
123 patterns_.insert(set.patterns().begin(), | 143 for (URLPatternSet::const_iterator pattern = set.patterns_.begin(); |
124 set.patterns().end()); | 144 pattern != set.patterns_.end(); ++pattern) { |
| 145 AddPattern(*pattern); |
| 146 } |
125 } | 147 } |
126 | 148 |
127 void URLPatternSet::ClearPatterns() { | 149 void URLPatternSet::ClearPatterns() { |
128 patterns_.clear(); | 150 patterns_.clear(); |
129 } | 151 } |
130 | 152 |
131 bool URLPatternSet::Contains(const URLPatternSet& other) const { | 153 bool URLPatternSet::Contains(const URLPatternSet& other) const { |
132 for (URLPatternSet::const_iterator it = other.begin(); | 154 for (URLPatternSet::const_iterator it = other.begin(); |
133 it != other.end(); ++it) { | 155 it != other.end(); ++it) { |
134 if (!ContainsPattern(*it)) | 156 if (!ContainsPattern(*it)) |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 std::vector<std::string> patterns; | 244 std::vector<std::string> patterns; |
223 for (size_t i = 0; i < value.GetSize(); ++i) { | 245 for (size_t i = 0; i < value.GetSize(); ++i) { |
224 std::string item; | 246 std::string item; |
225 if (!value.GetString(i, &item)) | 247 if (!value.GetString(i, &item)) |
226 return false; | 248 return false; |
227 patterns.push_back(item); | 249 patterns.push_back(item); |
228 } | 250 } |
229 return Populate(patterns, valid_schemes, allow_file_access, error); | 251 return Populate(patterns, valid_schemes, allow_file_access, error); |
230 } | 252 } |
231 | 253 |
| 254 bool URLPatternSet::InsertPattern(const URLPattern& pattern) { |
| 255 return patterns_.insert(pattern).second; |
| 256 } |
| 257 |
232 } // namespace extensions | 258 } // namespace extensions |
OLD | NEW |