Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: chrome/browser/download/download_query.cc

Issue 16924017: A few minor changes to the chrome.downloads extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r214130 Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 } 43 }
44 template<> bool GetAs(const base::Value& in, int* out) { 44 template<> bool GetAs(const base::Value& in, int* out) {
45 return in.GetAsInteger(out); 45 return in.GetAsInteger(out);
46 } 46 }
47 template<> bool GetAs(const base::Value& in, std::string* out) { 47 template<> bool GetAs(const base::Value& in, std::string* out) {
48 return in.GetAsString(out); 48 return in.GetAsString(out);
49 } 49 }
50 template<> bool GetAs(const base::Value& in, string16* out) { 50 template<> bool GetAs(const base::Value& in, string16* out) {
51 return in.GetAsString(out); 51 return in.GetAsString(out);
52 } 52 }
53 template<> bool GetAs(const base::Value& in, std::vector<string16>* out) {
54 out->clear();
55 const base::ListValue* list = NULL;
56 if (!in.GetAsList(&list))
57 return false;
58 for (size_t i = 0; i < list->GetSize(); ++i) {
59 string16 element;
60 if (!list->GetString(i, &element)) {
61 out->clear();
62 return false;
63 }
64 out->push_back(element);
65 }
66 return true;
67 }
53 68
54 // The next several functions are helpers for making Callbacks that access 69 // The next several functions are helpers for making Callbacks that access
55 // DownloadItem fields. 70 // DownloadItem fields.
56 71
57 static bool MatchesQuery(const string16& query, const DownloadItem& item) { 72 static bool MatchesQuery(
58 if (query.empty()) 73 const std::vector<string16>& query_terms,
59 return true; 74 const DownloadItem& item) {
60 75 DCHECK(!query_terms.empty());
61 DCHECK_EQ(query, base::i18n::ToLower(query));
62
63 string16 url_raw(UTF8ToUTF16(item.GetOriginalUrl().spec())); 76 string16 url_raw(UTF8ToUTF16(item.GetOriginalUrl().spec()));
64 if (base::i18n::StringSearchIgnoringCaseAndAccents(
65 query, url_raw, NULL, NULL)) {
66 return true;
67 }
68
69 string16 url_formatted = url_raw; 77 string16 url_formatted = url_raw;
70 if (item.GetBrowserContext()) { 78 if (item.GetBrowserContext()) {
71 Profile* profile = Profile::FromBrowserContext(item.GetBrowserContext()); 79 Profile* profile = Profile::FromBrowserContext(item.GetBrowserContext());
72 url_formatted = net::FormatUrl( 80 url_formatted = net::FormatUrl(
73 item.GetOriginalUrl(), 81 item.GetOriginalUrl(),
74 profile->GetPrefs()->GetString(prefs::kAcceptLanguages)); 82 profile->GetPrefs()->GetString(prefs::kAcceptLanguages));
75 } 83 }
76 if (base::i18n::StringSearchIgnoringCaseAndAccents( 84 string16 path(item.GetTargetFilePath().LossyDisplayName());
77 query, url_formatted, NULL, NULL)) { 85
78 return true; 86 for (std::vector<string16>::const_iterator it = query_terms.begin();
87 it != query_terms.end(); ++it) {
88 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 }
79 } 97 }
80 98 return true;
81 string16 path(item.GetTargetFilePath().LossyDisplayName());
82 return base::i18n::StringSearchIgnoringCaseAndAccents(
83 query, path, NULL, NULL);
84 } 99 }
85 100
86 static int64 GetStartTimeMsEpoch(const DownloadItem& item) { 101 static int64 GetStartTimeMsEpoch(const DownloadItem& item) {
87 return (item.GetStartTime() - base::Time::UnixEpoch()).InMilliseconds(); 102 return (item.GetStartTime() - base::Time::UnixEpoch()).InMilliseconds();
88 } 103 }
89 104
90 static int64 GetEndTimeMsEpoch(const DownloadItem& item) { 105 static int64 GetEndTimeMsEpoch(const DownloadItem& item) {
91 return (item.GetEndTime() - base::Time::UnixEpoch()).InMilliseconds(); 106 return (item.GetEndTime() - base::Time::UnixEpoch()).InMilliseconds();
92 } 107 }
93 108
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 return AddFilter(BuildFilter<bool>(value, EQ, &GetExists)); 276 return AddFilter(BuildFilter<bool>(value, EQ, &GetExists));
262 case FILTER_FILENAME: 277 case FILTER_FILENAME:
263 return AddFilter(BuildFilter<string16>(value, EQ, &GetFilename)); 278 return AddFilter(BuildFilter<string16>(value, EQ, &GetFilename));
264 case FILTER_FILENAME_REGEX: 279 case FILTER_FILENAME_REGEX:
265 return AddFilter(BuildRegexFilter(value, &GetFilenameUTF8)); 280 return AddFilter(BuildRegexFilter(value, &GetFilenameUTF8));
266 case FILTER_MIME: 281 case FILTER_MIME:
267 return AddFilter(BuildFilter<std::string>(value, EQ, &GetMimeType)); 282 return AddFilter(BuildFilter<std::string>(value, EQ, &GetMimeType));
268 case FILTER_PAUSED: 283 case FILTER_PAUSED:
269 return AddFilter(BuildFilter<bool>(value, EQ, &IsPaused)); 284 return AddFilter(BuildFilter<bool>(value, EQ, &IsPaused));
270 case FILTER_QUERY: { 285 case FILTER_QUERY: {
271 string16 query; 286 std::vector<string16> query_terms;
272 return GetAs(value, &query) && 287 return GetAs(value, &query_terms) &&
273 AddFilter(base::Bind(&MatchesQuery, query)); 288 (query_terms.empty() ||
289 AddFilter(base::Bind(&MatchesQuery, query_terms)));
274 } 290 }
275 case FILTER_ENDED_AFTER: 291 case FILTER_ENDED_AFTER:
276 return AddFilter(BuildFilter<std::string>(value, GT, &GetEndTime)); 292 return AddFilter(BuildFilter<std::string>(value, GT, &GetEndTime));
277 case FILTER_ENDED_BEFORE: 293 case FILTER_ENDED_BEFORE:
278 return AddFilter(BuildFilter<std::string>(value, LT, &GetEndTime)); 294 return AddFilter(BuildFilter<std::string>(value, LT, &GetEndTime));
279 case FILTER_END_TIME: 295 case FILTER_END_TIME:
280 return AddFilter(BuildFilter<std::string>(value, EQ, &GetEndTime)); 296 return AddFilter(BuildFilter<std::string>(value, EQ, &GetEndTime));
281 case FILTER_STARTED_AFTER: 297 case FILTER_STARTED_AFTER:
282 return AddFilter(BuildFilter<std::string>(value, GT, &GetStartTime)); 298 return AddFilter(BuildFilter<std::string>(value, GT, &GetStartTime));
283 case FILTER_STARTED_BEFORE: 299 case FILTER_STARTED_BEFORE:
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 432
417 void DownloadQuery::FinishSearch(DownloadQuery::DownloadVector* results) const { 433 void DownloadQuery::FinishSearch(DownloadQuery::DownloadVector* results) const {
418 if (!sorters_.empty()) 434 if (!sorters_.empty())
419 std::partial_sort(results->begin(), 435 std::partial_sort(results->begin(),
420 results->begin() + std::min(limit_, results->size()), 436 results->begin() + std::min(limit_, results->size()),
421 results->end(), 437 results->end(),
422 DownloadComparator(sorters_)); 438 DownloadComparator(sorters_));
423 if (results->size() > limit_) 439 if (results->size() > limit_)
424 results->resize(limit_); 440 results->resize(limit_);
425 } 441 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_query.h ('k') | chrome/browser/download/download_query_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698