| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/common/extensions/url_pattern_set.h" | 5 #include "chrome/common/extensions/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" |
| 11 #include "base/memory/linked_ptr.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "chrome/common/extensions/extension_error_utils.h" | 13 #include "chrome/common/extensions/extension_error_utils.h" |
| 13 #include "chrome/common/extensions/url_pattern.h" | 14 #include "chrome/common/extensions/url_pattern.h" |
| 14 #include "content/public/common/url_constants.h" | 15 #include "content/public/common/url_constants.h" |
| 15 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const char kInvalidURLPatternError[] = "Invalid url pattern '*'"; | 20 const char kInvalidURLPatternError[] = "Invalid url pattern '*'"; |
| 20 | 21 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 46 void URLPatternSet::CreateUnion(const URLPatternSet& set1, | 47 void URLPatternSet::CreateUnion(const URLPatternSet& set1, |
| 47 const URLPatternSet& set2, | 48 const URLPatternSet& set2, |
| 48 URLPatternSet* out) { | 49 URLPatternSet* out) { |
| 49 out->ClearPatterns(); | 50 out->ClearPatterns(); |
| 50 std::set_union(set1.patterns_.begin(), set1.patterns_.end(), | 51 std::set_union(set1.patterns_.begin(), set1.patterns_.end(), |
| 51 set2.patterns_.begin(), set2.patterns_.end(), | 52 set2.patterns_.begin(), set2.patterns_.end(), |
| 52 std::inserter<std::set<URLPattern> >( | 53 std::inserter<std::set<URLPattern> >( |
| 53 out->patterns_, out->patterns_.begin())); | 54 out->patterns_, out->patterns_.begin())); |
| 54 } | 55 } |
| 55 | 56 |
| 57 // static |
| 58 void URLPatternSet::CreateUnion(const std::vector<URLPatternSet>& sets, |
| 59 URLPatternSet* out) { |
| 60 out->ClearPatterns(); |
| 61 if (sets.empty()) |
| 62 return; |
| 63 |
| 64 // N-way union algorithm is basic O(nlog(n)) merge algorithm. |
| 65 // |
| 66 // Do the first merge step into a working set so that we don't mutate any of |
| 67 // the input. |
| 68 std::vector<URLPatternSet> working; |
| 69 for (size_t i = 0; i < sets.size(); i += 2) { |
| 70 if (i + 1 < sets.size()) { |
| 71 URLPatternSet u; |
| 72 URLPatternSet::CreateUnion(sets[i], sets[i + 1], &u); |
| 73 working.push_back(u); |
| 74 } else { |
| 75 working.push_back(sets[i]); |
| 76 } |
| 77 } |
| 78 |
| 79 for (size_t skip = 1; skip < working.size(); skip *= 2) { |
| 80 for (size_t i = 0; i < (working.size() - skip); i += skip) { |
| 81 URLPatternSet u; |
| 82 URLPatternSet::CreateUnion(working[i], working[i + skip], &u); |
| 83 working[i].patterns_.swap(u.patterns_); |
| 84 } |
| 85 } |
| 86 |
| 87 out->patterns_.swap(working[0].patterns_); |
| 88 } |
| 89 |
| 56 URLPatternSet::URLPatternSet() {} | 90 URLPatternSet::URLPatternSet() {} |
| 57 | 91 |
| 58 URLPatternSet::URLPatternSet(const URLPatternSet& rhs) | 92 URLPatternSet::URLPatternSet(const URLPatternSet& rhs) |
| 59 : patterns_(rhs.patterns_) {} | 93 : patterns_(rhs.patterns_) {} |
| 60 | 94 |
| 61 URLPatternSet::URLPatternSet(const std::set<URLPattern>& patterns) | 95 URLPatternSet::URLPatternSet(const std::set<URLPattern>& patterns) |
| 62 : patterns_(patterns) {} | 96 : patterns_(patterns) {} |
| 63 | 97 |
| 64 URLPatternSet::~URLPatternSet() {} | 98 URLPatternSet::~URLPatternSet() {} |
| 65 | 99 |
| 66 URLPatternSet& URLPatternSet::operator=(const URLPatternSet& rhs) { | 100 URLPatternSet& URLPatternSet::operator=(const URLPatternSet& rhs) { |
| 67 patterns_ = rhs.patterns_; | 101 patterns_ = rhs.patterns_; |
| 68 return *this; | 102 return *this; |
| 69 } | 103 } |
| 70 | 104 |
| 71 bool URLPatternSet::operator==(const URLPatternSet& other) const { | 105 bool URLPatternSet::operator==(const URLPatternSet& other) const { |
| 72 return patterns_ == other.patterns_; | 106 return patterns_ == other.patterns_; |
| 73 } | 107 } |
| 74 | 108 |
| 75 bool URLPatternSet::is_empty() const { | 109 bool URLPatternSet::is_empty() const { |
| 76 return patterns_.empty(); | 110 return patterns_.empty(); |
| 77 } | 111 } |
| 78 | 112 |
| 113 size_t URLPatternSet::size() const { |
| 114 return patterns_.size(); |
| 115 } |
| 116 |
| 79 void URLPatternSet::AddPattern(const URLPattern& pattern) { | 117 void URLPatternSet::AddPattern(const URLPattern& pattern) { |
| 80 patterns_.insert(pattern); | 118 patterns_.insert(pattern); |
| 81 } | 119 } |
| 82 | 120 |
| 83 void URLPatternSet::ClearPatterns() { | 121 void URLPatternSet::ClearPatterns() { |
| 84 patterns_.clear(); | 122 patterns_.clear(); |
| 85 } | 123 } |
| 86 | 124 |
| 87 bool URLPatternSet::Contains(const URLPatternSet& set) const { | 125 bool URLPatternSet::Contains(const URLPatternSet& set) const { |
| 88 return std::includes(patterns_.begin(), patterns_.end(), | 126 return std::includes(patterns_.begin(), patterns_.end(), |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 return false; | 190 return false; |
| 153 } | 191 } |
| 154 if (!allow_file_access && pattern.MatchesScheme(chrome::kFileScheme)) { | 192 if (!allow_file_access && pattern.MatchesScheme(chrome::kFileScheme)) { |
| 155 pattern.SetValidSchemes( | 193 pattern.SetValidSchemes( |
| 156 pattern.valid_schemes() & ~URLPattern::SCHEME_FILE); | 194 pattern.valid_schemes() & ~URLPattern::SCHEME_FILE); |
| 157 } | 195 } |
| 158 AddPattern(pattern); | 196 AddPattern(pattern); |
| 159 } | 197 } |
| 160 return true; | 198 return true; |
| 161 } | 199 } |
| OLD | NEW |