| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/string16.h" | 17 #include "base/string16.h" |
| 18 #include "base/string_split.h" | 18 #include "base/string_split.h" |
| 19 #include "base/time.h" | 19 #include "base/time.h" |
| 20 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 21 #include "base/values.h" | 21 #include "base/values.h" |
| 22 #include "content/public/browser/download_item.h" | 22 #include "content/public/browser/download_item.h" |
| 23 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
| 24 #include "unicode/regex.h" | 24 #include "re2/re2.h" |
| 25 | 25 |
| 26 using content::DownloadDangerType; | 26 using content::DownloadDangerType; |
| 27 using content::DownloadItem; | 27 using content::DownloadItem; |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // Templatized base::Value::GetAs*(). | 31 // Templatized base::Value::GetAs*(). |
| 32 template <typename T> bool GetAs(const base::Value& in, T* out); | 32 template <typename T> bool GetAs(const base::Value& in, T* out); |
| 33 template<> bool GetAs(const base::Value& in, bool* out) { | 33 template<> bool GetAs(const base::Value& in, bool* out) { |
| 34 return in.GetAsBoolean(out); | 34 return in.GetAsBoolean(out); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 const base::Value& value, ComparisonType cmptype, | 122 const base::Value& value, ComparisonType cmptype, |
| 123 ValueType (*accessor)(const DownloadItem&)) { | 123 ValueType (*accessor)(const DownloadItem&)) { |
| 124 ValueType cpp_value; | 124 ValueType cpp_value; |
| 125 if (!GetAs(value, &cpp_value)) return DownloadQuery::FilterCallback(); | 125 if (!GetAs(value, &cpp_value)) return DownloadQuery::FilterCallback(); |
| 126 return base::Bind(&FieldMatches<ValueType>, cpp_value, cmptype, | 126 return base::Bind(&FieldMatches<ValueType>, cpp_value, cmptype, |
| 127 base::Bind(accessor)); | 127 base::Bind(accessor)); |
| 128 } | 128 } |
| 129 | 129 |
| 130 // Returns true if |accessor.Run(item)| matches |pattern|. | 130 // Returns true if |accessor.Run(item)| matches |pattern|. |
| 131 static bool FindRegex( | 131 static bool FindRegex( |
| 132 icu::RegexPattern* pattern, | 132 RE2* pattern, |
| 133 const base::Callback<std::string(const DownloadItem&)>& accessor, | 133 const base::Callback<std::string(const DownloadItem&)>& accessor, |
| 134 const DownloadItem& item) { | 134 const DownloadItem& item) { |
| 135 icu::UnicodeString input(accessor.Run(item).c_str()); | 135 return RE2::PartialMatch(accessor.Run(item), *pattern); |
| 136 UErrorCode status = U_ZERO_ERROR; | |
| 137 scoped_ptr<icu::RegexMatcher> matcher(pattern->matcher(input, status)); | |
| 138 return matcher->find() == TRUE; // Ugh, VS complains bool != UBool. | |
| 139 } | 136 } |
| 140 | 137 |
| 141 // Helper for building a Callback to FindRegex(). | 138 // Helper for building a Callback to FindRegex(). |
| 142 DownloadQuery::FilterCallback BuildRegexFilter( | 139 DownloadQuery::FilterCallback BuildRegexFilter( |
| 143 const base::Value& regex_value, | 140 const base::Value& regex_value, |
| 144 std::string (*accessor)(const DownloadItem&)) { | 141 std::string (*accessor)(const DownloadItem&)) { |
| 145 std::string regex_str; | 142 std::string regex_str; |
| 146 if (!GetAs(regex_value, ®ex_str)) return DownloadQuery::FilterCallback(); | 143 if (!GetAs(regex_value, ®ex_str)) return DownloadQuery::FilterCallback(); |
| 147 UParseError re_err; | 144 scoped_ptr<RE2> pattern(new RE2(regex_str)); |
| 148 UErrorCode re_status = U_ZERO_ERROR; | 145 if (!pattern->ok()) return DownloadQuery::FilterCallback(); |
| 149 scoped_ptr<icu::RegexPattern> pattern(icu::RegexPattern::compile( | |
| 150 icu::UnicodeString::fromUTF8(regex_str.c_str()), re_err, re_status)); | |
| 151 if (!U_SUCCESS(re_status)) return DownloadQuery::FilterCallback(); | |
| 152 return base::Bind(&FindRegex, base::Owned(pattern.release()), | 146 return base::Bind(&FindRegex, base::Owned(pattern.release()), |
| 153 base::Bind(accessor)); | 147 base::Bind(accessor)); |
| 154 } | 148 } |
| 155 | 149 |
| 156 // Returns a ComparisonType to indicate whether a field in |left| is less than, | 150 // Returns a ComparisonType to indicate whether a field in |left| is less than, |
| 157 // greater than or equal to the same field in |right|. | 151 // greater than or equal to the same field in |right|. |
| 158 template<typename ValueType> | 152 template<typename ValueType> |
| 159 static ComparisonType Compare( | 153 static ComparisonType Compare( |
| 160 const base::Callback<ValueType(const DownloadItem&)>& accessor, | 154 const base::Callback<ValueType(const DownloadItem&)>& accessor, |
| 161 const DownloadItem& left, const DownloadItem& right) { | 155 const DownloadItem& left, const DownloadItem& right) { |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 | 343 |
| 350 void DownloadQuery::FinishSearch(DownloadQuery::DownloadVector* results) const { | 344 void DownloadQuery::FinishSearch(DownloadQuery::DownloadVector* results) const { |
| 351 if (!sorters_.empty()) | 345 if (!sorters_.empty()) |
| 352 std::partial_sort(results->begin(), | 346 std::partial_sort(results->begin(), |
| 353 results->begin() + std::min(limit_, results->size()), | 347 results->begin() + std::min(limit_, results->size()), |
| 354 results->end(), | 348 results->end(), |
| 355 DownloadComparator(sorters_)); | 349 DownloadComparator(sorters_)); |
| 356 if (results->size() > limit_) | 350 if (results->size() > limit_) |
| 357 results->resize(limit_); | 351 results->resize(limit_); |
| 358 } | 352 } |
| OLD | NEW |