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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 return false; | 62 return false; |
63 } | 63 } |
64 out->push_back(element); | 64 out->push_back(element); |
65 } | 65 } |
66 return true; | 66 return true; |
67 } | 67 } |
68 | 68 |
69 // The next several functions are helpers for making Callbacks that access | 69 // The next several functions are helpers for making Callbacks that access |
70 // DownloadItem fields. | 70 // DownloadItem fields. |
71 | 71 |
| 72 static bool MatchesQuery( |
| 73 const std::vector<base::string16>& query_terms, |
| 74 const DownloadItem& item) { |
| 75 DCHECK(!query_terms.empty()); |
| 76 base::string16 url_raw(base::UTF8ToUTF16(item.GetOriginalUrl().spec())); |
| 77 base::string16 url_formatted = url_raw; |
| 78 if (item.GetBrowserContext()) { |
| 79 Profile* profile = Profile::FromBrowserContext(item.GetBrowserContext()); |
| 80 url_formatted = url_formatter::FormatUrl( |
| 81 item.GetOriginalUrl(), |
| 82 profile->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
| 83 } |
| 84 base::string16 path(item.GetTargetFilePath().LossyDisplayName()); |
| 85 |
| 86 for (std::vector<base::string16>::const_iterator it = query_terms.begin(); |
| 87 it != query_terms.end(); ++it) { |
| 88 base::string16 term = base::i18n::ToLower(*it); |
| 89 if (!base::i18n::StringSearchIgnoringCaseAndAccents( |
| 90 term, url_raw, NULL, NULL) && |
| 91 !base::i18n::StringSearchIgnoringCaseAndAccents( |
| 92 term, url_formatted, NULL, NULL) && |
| 93 !base::i18n::StringSearchIgnoringCaseAndAccents( |
| 94 term, path, NULL, NULL)) { |
| 95 return false; |
| 96 } |
| 97 } |
| 98 return true; |
| 99 } |
| 100 |
72 static int64 GetStartTimeMsEpoch(const DownloadItem& item) { | 101 static int64 GetStartTimeMsEpoch(const DownloadItem& item) { |
73 return (item.GetStartTime() - base::Time::UnixEpoch()).InMilliseconds(); | 102 return (item.GetStartTime() - base::Time::UnixEpoch()).InMilliseconds(); |
74 } | 103 } |
75 | 104 |
76 static int64 GetEndTimeMsEpoch(const DownloadItem& item) { | 105 static int64 GetEndTimeMsEpoch(const DownloadItem& item) { |
77 return (item.GetEndTime() - base::Time::UnixEpoch()).InMilliseconds(); | 106 return (item.GetEndTime() - base::Time::UnixEpoch()).InMilliseconds(); |
78 } | 107 } |
79 | 108 |
80 std::string TimeToISO8601(const base::Time& t) { | 109 std::string TimeToISO8601(const base::Time& t) { |
81 base::Time::Exploded exploded; | 110 base::Time::Exploded exploded; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 ValueType left_value = accessor.Run(left); | 230 ValueType left_value = accessor.Run(left); |
202 ValueType right_value = accessor.Run(right); | 231 ValueType right_value = accessor.Run(right); |
203 if (left_value > right_value) return GT; | 232 if (left_value > right_value) return GT; |
204 if (left_value < right_value) return LT; | 233 if (left_value < right_value) return LT; |
205 DCHECK_EQ(left_value, right_value); | 234 DCHECK_EQ(left_value, right_value); |
206 return EQ; | 235 return EQ; |
207 } | 236 } |
208 | 237 |
209 } // anonymous namespace | 238 } // anonymous namespace |
210 | 239 |
211 // static | |
212 bool DownloadQuery::MatchesQuery(const std::vector<base::string16>& query_terms, | |
213 const DownloadItem& item) { | |
214 if (query_terms.empty()) | |
215 return true; | |
216 | |
217 base::string16 url_raw(base::UTF8ToUTF16(item.GetOriginalUrl().spec())); | |
218 base::string16 url_formatted = url_raw; | |
219 if (item.GetBrowserContext()) { | |
220 Profile* profile = Profile::FromBrowserContext(item.GetBrowserContext()); | |
221 url_formatted = url_formatter::FormatUrl( | |
222 item.GetOriginalUrl(), | |
223 profile->GetPrefs()->GetString(prefs::kAcceptLanguages)); | |
224 } | |
225 base::string16 path(item.GetTargetFilePath().LossyDisplayName()); | |
226 | |
227 for (std::vector<base::string16>::const_iterator it = query_terms.begin(); | |
228 it != query_terms.end(); ++it) { | |
229 base::string16 term = base::i18n::ToLower(*it); | |
230 if (!base::i18n::StringSearchIgnoringCaseAndAccents( | |
231 term, url_raw, NULL, NULL) && | |
232 !base::i18n::StringSearchIgnoringCaseAndAccents( | |
233 term, url_formatted, NULL, NULL) && | |
234 !base::i18n::StringSearchIgnoringCaseAndAccents( | |
235 term, path, NULL, NULL)) { | |
236 return false; | |
237 } | |
238 } | |
239 return true; | |
240 } | |
241 | |
242 DownloadQuery::DownloadQuery() : limit_(kuint32max), skip_(0U) {} | 240 DownloadQuery::DownloadQuery() : limit_(kuint32max), skip_(0U) {} |
243 DownloadQuery::~DownloadQuery() {} | 241 DownloadQuery::~DownloadQuery() {} |
244 | 242 |
245 // AddFilter() pushes a new FilterCallback to filters_. Most FilterCallbacks are | 243 // AddFilter() pushes a new FilterCallback to filters_. Most FilterCallbacks are |
246 // Callbacks to FieldMatches<>(). Search() iterates over given DownloadItems, | 244 // Callbacks to FieldMatches<>(). Search() iterates over given DownloadItems, |
247 // discarding items for which any filter returns false. A DownloadQuery may have | 245 // discarding items for which any filter returns false. A DownloadQuery may have |
248 // zero or more FilterCallbacks. | 246 // zero or more FilterCallbacks. |
249 | 247 |
250 bool DownloadQuery::AddFilter(const DownloadQuery::FilterCallback& value) { | 248 bool DownloadQuery::AddFilter(const DownloadQuery::FilterCallback& value) { |
251 if (value.is_null()) return false; | 249 if (value.is_null()) return false; |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 results->begin() + std::min(limit_ + skip_, results->size()), | 439 results->begin() + std::min(limit_ + skip_, results->size()), |
442 results->end(), | 440 results->end(), |
443 DownloadComparator(sorters_)); | 441 DownloadComparator(sorters_)); |
444 } | 442 } |
445 | 443 |
446 results->erase(results->begin(), results->begin() + skip_); | 444 results->erase(results->begin(), results->begin() + skip_); |
447 | 445 |
448 if (results->size() > limit_) | 446 if (results->size() > limit_) |
449 results->resize(limit_); | 447 results->resize(limit_); |
450 } | 448 } |
OLD | NEW |