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 | |
101 static int64 GetStartTimeMsEpoch(const DownloadItem& item) { | 72 static int64 GetStartTimeMsEpoch(const DownloadItem& item) { |
102 return (item.GetStartTime() - base::Time::UnixEpoch()).InMilliseconds(); | 73 return (item.GetStartTime() - base::Time::UnixEpoch()).InMilliseconds(); |
103 } | 74 } |
104 | 75 |
105 static int64 GetEndTimeMsEpoch(const DownloadItem& item) { | 76 static int64 GetEndTimeMsEpoch(const DownloadItem& item) { |
106 return (item.GetEndTime() - base::Time::UnixEpoch()).InMilliseconds(); | 77 return (item.GetEndTime() - base::Time::UnixEpoch()).InMilliseconds(); |
107 } | 78 } |
108 | 79 |
109 std::string TimeToISO8601(const base::Time& t) { | 80 std::string TimeToISO8601(const base::Time& t) { |
110 base::Time::Exploded exploded; | 81 base::Time::Exploded exploded; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 ValueType left_value = accessor.Run(left); | 201 ValueType left_value = accessor.Run(left); |
231 ValueType right_value = accessor.Run(right); | 202 ValueType right_value = accessor.Run(right); |
232 if (left_value > right_value) return GT; | 203 if (left_value > right_value) return GT; |
233 if (left_value < right_value) return LT; | 204 if (left_value < right_value) return LT; |
234 DCHECK_EQ(left_value, right_value); | 205 DCHECK_EQ(left_value, right_value); |
235 return EQ; | 206 return EQ; |
236 } | 207 } |
237 | 208 |
238 } // anonymous namespace | 209 } // anonymous namespace |
239 | 210 |
| 211 // static |
| 212 bool DownloadQuery::MatchesQuery(const std::vector<base::string16>& query_terms, |
| 213 const DownloadItem& item) { |
| 214 DCHECK(!query_terms.empty()); |
| 215 base::string16 url_raw(base::UTF8ToUTF16(item.GetOriginalUrl().spec())); |
| 216 base::string16 url_formatted = url_raw; |
| 217 if (item.GetBrowserContext()) { |
| 218 Profile* profile = Profile::FromBrowserContext(item.GetBrowserContext()); |
| 219 url_formatted = url_formatter::FormatUrl( |
| 220 item.GetOriginalUrl(), |
| 221 profile->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
| 222 } |
| 223 base::string16 path(item.GetTargetFilePath().LossyDisplayName()); |
| 224 |
| 225 for (std::vector<base::string16>::const_iterator it = query_terms.begin(); |
| 226 it != query_terms.end(); ++it) { |
| 227 base::string16 term = base::i18n::ToLower(*it); |
| 228 if (!base::i18n::StringSearchIgnoringCaseAndAccents( |
| 229 term, url_raw, NULL, NULL) && |
| 230 !base::i18n::StringSearchIgnoringCaseAndAccents( |
| 231 term, url_formatted, NULL, NULL) && |
| 232 !base::i18n::StringSearchIgnoringCaseAndAccents( |
| 233 term, path, NULL, NULL)) { |
| 234 return false; |
| 235 } |
| 236 } |
| 237 return true; |
| 238 } |
| 239 |
240 DownloadQuery::DownloadQuery() : limit_(kuint32max), skip_(0U) {} | 240 DownloadQuery::DownloadQuery() : limit_(kuint32max), skip_(0U) {} |
241 DownloadQuery::~DownloadQuery() {} | 241 DownloadQuery::~DownloadQuery() {} |
242 | 242 |
243 // AddFilter() pushes a new FilterCallback to filters_. Most FilterCallbacks are | 243 // AddFilter() pushes a new FilterCallback to filters_. Most FilterCallbacks are |
244 // Callbacks to FieldMatches<>(). Search() iterates over given DownloadItems, | 244 // Callbacks to FieldMatches<>(). Search() iterates over given DownloadItems, |
245 // 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 |
246 // zero or more FilterCallbacks. | 246 // zero or more FilterCallbacks. |
247 | 247 |
248 bool DownloadQuery::AddFilter(const DownloadQuery::FilterCallback& value) { | 248 bool DownloadQuery::AddFilter(const DownloadQuery::FilterCallback& value) { |
249 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... |
439 results->begin() + std::min(limit_ + skip_, results->size()), | 439 results->begin() + std::min(limit_ + skip_, results->size()), |
440 results->end(), | 440 results->end(), |
441 DownloadComparator(sorters_)); | 441 DownloadComparator(sorters_)); |
442 } | 442 } |
443 | 443 |
444 results->erase(results->begin(), results->begin() + skip_); | 444 results->erase(results->begin(), results->begin() + skip_); |
445 | 445 |
446 if (results->size() > limit_) | 446 if (results->size() > limit_) |
447 results->resize(limit_); | 447 results->resize(limit_); |
448 } | 448 } |
OLD | NEW |