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 "chrome/browser/download/download_query.h" | 5 #include "chrome/browser/download/download_query.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "base/strings/string_split.h" | 22 #include "base/strings/string_split.h" |
23 #include "base/time.h" | 23 #include "base/time.h" |
24 #include "base/utf_string_conversions.h" | 24 #include "base/utf_string_conversions.h" |
25 #include "base/values.h" | 25 #include "base/values.h" |
26 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
27 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
28 #include "content/public/browser/content_browser_client.h" | 28 #include "content/public/browser/content_browser_client.h" |
29 #include "content/public/browser/download_item.h" | 29 #include "content/public/browser/download_item.h" |
30 #include "googleurl/src/gurl.h" | 30 #include "googleurl/src/gurl.h" |
31 #include "net/base/net_util.h" | 31 #include "net/base/net_util.h" |
32 #include "third_party/icu/public/i18n/unicode/regex.h" | 32 #include "third_party/re2/re2/re2.h" |
33 | 33 |
34 using content::DownloadDangerType; | 34 using content::DownloadDangerType; |
35 using content::DownloadItem; | 35 using content::DownloadItem; |
36 | 36 |
37 namespace { | 37 namespace { |
38 | 38 |
39 // Templatized base::Value::GetAs*(). | 39 // Templatized base::Value::GetAs*(). |
40 template <typename T> bool GetAs(const base::Value& in, T* out); | 40 template <typename T> bool GetAs(const base::Value& in, T* out); |
41 template<> bool GetAs(const base::Value& in, bool* out) { | 41 template<> bool GetAs(const base::Value& in, bool* out) { |
42 return in.GetAsBoolean(out); | 42 return in.GetAsBoolean(out); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 const base::Value& value, ComparisonType cmptype, | 181 const base::Value& value, ComparisonType cmptype, |
182 ValueType (*accessor)(const DownloadItem&)) { | 182 ValueType (*accessor)(const DownloadItem&)) { |
183 ValueType cpp_value; | 183 ValueType cpp_value; |
184 if (!GetAs(value, &cpp_value)) return DownloadQuery::FilterCallback(); | 184 if (!GetAs(value, &cpp_value)) return DownloadQuery::FilterCallback(); |
185 return base::Bind(&FieldMatches<ValueType>, cpp_value, cmptype, | 185 return base::Bind(&FieldMatches<ValueType>, cpp_value, cmptype, |
186 base::Bind(accessor)); | 186 base::Bind(accessor)); |
187 } | 187 } |
188 | 188 |
189 // Returns true if |accessor.Run(item)| matches |pattern|. | 189 // Returns true if |accessor.Run(item)| matches |pattern|. |
190 static bool FindRegex( | 190 static bool FindRegex( |
191 icu::RegexPattern* pattern, | 191 RE2* pattern, |
192 const base::Callback<std::string(const DownloadItem&)>& accessor, | 192 const base::Callback<std::string(const DownloadItem&)>& accessor, |
193 const DownloadItem& item) { | 193 const DownloadItem& item) { |
194 icu::UnicodeString input(accessor.Run(item).c_str()); | 194 return RE2::PartialMatch(accessor.Run(item), *pattern); |
195 UErrorCode status = U_ZERO_ERROR; | |
196 scoped_ptr<icu::RegexMatcher> matcher(pattern->matcher(input, status)); | |
197 return matcher->find() == TRUE; // Ugh, VS complains bool != UBool. | |
198 } | 195 } |
199 | 196 |
200 // Helper for building a Callback to FindRegex(). | 197 // Helper for building a Callback to FindRegex(). |
201 DownloadQuery::FilterCallback BuildRegexFilter( | 198 DownloadQuery::FilterCallback BuildRegexFilter( |
202 const base::Value& regex_value, | 199 const base::Value& regex_value, |
203 std::string (*accessor)(const DownloadItem&)) { | 200 std::string (*accessor)(const DownloadItem&)) { |
204 std::string regex_str; | 201 std::string regex_str; |
205 if (!GetAs(regex_value, ®ex_str)) return DownloadQuery::FilterCallback(); | 202 if (!GetAs(regex_value, ®ex_str)) return DownloadQuery::FilterCallback(); |
206 UParseError re_err; | 203 scoped_ptr<RE2> pattern(new RE2(regex_str)); |
207 UErrorCode re_status = U_ZERO_ERROR; | 204 if (!pattern->ok()) return DownloadQuery::FilterCallback(); |
208 scoped_ptr<icu::RegexPattern> pattern(icu::RegexPattern::compile( | |
209 icu::UnicodeString::fromUTF8(regex_str.c_str()), re_err, re_status)); | |
210 if (!U_SUCCESS(re_status)) return DownloadQuery::FilterCallback(); | |
211 return base::Bind(&FindRegex, base::Owned(pattern.release()), | 205 return base::Bind(&FindRegex, base::Owned(pattern.release()), |
212 base::Bind(accessor)); | 206 base::Bind(accessor)); |
213 } | 207 } |
214 | 208 |
215 // Returns a ComparisonType to indicate whether a field in |left| is less than, | 209 // Returns a ComparisonType to indicate whether a field in |left| is less than, |
216 // greater than or equal to the same field in |right|. | 210 // greater than or equal to the same field in |right|. |
217 template<typename ValueType> | 211 template<typename ValueType> |
218 static ComparisonType Compare( | 212 static ComparisonType Compare( |
219 const base::Callback<ValueType(const DownloadItem&)>& accessor, | 213 const base::Callback<ValueType(const DownloadItem&)>& accessor, |
220 const DownloadItem& left, const DownloadItem& right) { | 214 const DownloadItem& left, const DownloadItem& right) { |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 | 416 |
423 void DownloadQuery::FinishSearch(DownloadQuery::DownloadVector* results) const { | 417 void DownloadQuery::FinishSearch(DownloadQuery::DownloadVector* results) const { |
424 if (!sorters_.empty()) | 418 if (!sorters_.empty()) |
425 std::partial_sort(results->begin(), | 419 std::partial_sort(results->begin(), |
426 results->begin() + std::min(limit_, results->size()), | 420 results->begin() + std::min(limit_, results->size()), |
427 results->end(), | 421 results->end(), |
428 DownloadComparator(sorters_)); | 422 DownloadComparator(sorters_)); |
429 if (results->size() > limit_) | 423 if (results->size() > limit_) |
430 results->resize(limit_); | 424 results->resize(limit_); |
431 } | 425 } |
OLD | NEW |