| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 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); |
| 43 } | 43 } |
| 44 template<> bool GetAs(const base::Value& in, int* out) { | 44 template <> |
| 45 return in.GetAsInteger(out); | 45 bool GetAs(const base::Value& in, double* out) { |
| 46 return in.GetAsDouble(out); |
| 46 } | 47 } |
| 47 template<> bool GetAs(const base::Value& in, std::string* out) { | 48 template<> bool GetAs(const base::Value& in, std::string* out) { |
| 48 return in.GetAsString(out); | 49 return in.GetAsString(out); |
| 49 } | 50 } |
| 50 template<> bool GetAs(const base::Value& in, base::string16* out) { | 51 template<> bool GetAs(const base::Value& in, base::string16* out) { |
| 51 return in.GetAsString(out); | 52 return in.GetAsString(out); |
| 52 } | 53 } |
| 53 template<> bool GetAs(const base::Value& in, std::vector<base::string16>* out) { | 54 template<> bool GetAs(const base::Value& in, std::vector<base::string16>* out) { |
| 54 out->clear(); | 55 out->clear(); |
| 55 const base::ListValue* list = NULL; | 56 const base::ListValue* list = NULL; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 } | 119 } |
| 119 | 120 |
| 120 static DownloadItem::DownloadState GetState(const DownloadItem& item) { | 121 static DownloadItem::DownloadState GetState(const DownloadItem& item) { |
| 121 return item.GetState(); | 122 return item.GetState(); |
| 122 } | 123 } |
| 123 | 124 |
| 124 static DownloadDangerType GetDangerType(const DownloadItem& item) { | 125 static DownloadDangerType GetDangerType(const DownloadItem& item) { |
| 125 return item.GetDangerType(); | 126 return item.GetDangerType(); |
| 126 } | 127 } |
| 127 | 128 |
| 128 static int GetReceivedBytes(const DownloadItem& item) { | 129 static double GetReceivedBytes(const DownloadItem& item) { |
| 129 return item.GetReceivedBytes(); | 130 return item.GetReceivedBytes(); |
| 130 } | 131 } |
| 131 | 132 |
| 132 static int GetTotalBytes(const DownloadItem& item) { | 133 static double GetTotalBytes(const DownloadItem& item) { |
| 133 return item.GetTotalBytes(); | 134 return item.GetTotalBytes(); |
| 134 } | 135 } |
| 135 | 136 |
| 136 static std::string GetMimeType(const DownloadItem& item) { | 137 static std::string GetMimeType(const DownloadItem& item) { |
| 137 return item.GetMimeType(); | 138 return item.GetMimeType(); |
| 138 } | 139 } |
| 139 | 140 |
| 140 static bool IsPaused(const DownloadItem& item) { | 141 static bool IsPaused(const DownloadItem& item) { |
| 141 return item.IsPaused(); | 142 return item.IsPaused(); |
| 142 } | 143 } |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 | 257 |
| 257 void DownloadQuery::AddFilter(DownloadDangerType danger) { | 258 void DownloadQuery::AddFilter(DownloadDangerType danger) { |
| 258 AddFilter(base::Bind(&FieldMatches<DownloadDangerType>, danger, EQ, | 259 AddFilter(base::Bind(&FieldMatches<DownloadDangerType>, danger, EQ, |
| 259 base::Bind(&GetDangerType))); | 260 base::Bind(&GetDangerType))); |
| 260 } | 261 } |
| 261 | 262 |
| 262 bool DownloadQuery::AddFilter(DownloadQuery::FilterType type, | 263 bool DownloadQuery::AddFilter(DownloadQuery::FilterType type, |
| 263 const base::Value& value) { | 264 const base::Value& value) { |
| 264 switch (type) { | 265 switch (type) { |
| 265 case FILTER_BYTES_RECEIVED: | 266 case FILTER_BYTES_RECEIVED: |
| 266 return AddFilter(BuildFilter<int>(value, EQ, &GetReceivedBytes)); | 267 return AddFilter(BuildFilter<double>(value, EQ, &GetReceivedBytes)); |
| 267 case FILTER_DANGER_ACCEPTED: | 268 case FILTER_DANGER_ACCEPTED: |
| 268 return AddFilter(BuildFilter<bool>(value, EQ, &GetDangerAccepted)); | 269 return AddFilter(BuildFilter<bool>(value, EQ, &GetDangerAccepted)); |
| 269 case FILTER_EXISTS: | 270 case FILTER_EXISTS: |
| 270 return AddFilter(BuildFilter<bool>(value, EQ, &GetExists)); | 271 return AddFilter(BuildFilter<bool>(value, EQ, &GetExists)); |
| 271 case FILTER_FILENAME: | 272 case FILTER_FILENAME: |
| 272 return AddFilter(BuildFilter<base::string16>(value, EQ, &GetFilename)); | 273 return AddFilter(BuildFilter<base::string16>(value, EQ, &GetFilename)); |
| 273 case FILTER_FILENAME_REGEX: | 274 case FILTER_FILENAME_REGEX: |
| 274 return AddFilter(BuildRegexFilter(value, &GetFilenameUTF8)); | 275 return AddFilter(BuildRegexFilter(value, &GetFilenameUTF8)); |
| 275 case FILTER_MIME: | 276 case FILTER_MIME: |
| 276 return AddFilter(BuildFilter<std::string>(value, EQ, &GetMimeType)); | 277 return AddFilter(BuildFilter<std::string>(value, EQ, &GetMimeType)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 288 return AddFilter(BuildFilter<std::string>(value, LT, &GetEndTime)); | 289 return AddFilter(BuildFilter<std::string>(value, LT, &GetEndTime)); |
| 289 case FILTER_END_TIME: | 290 case FILTER_END_TIME: |
| 290 return AddFilter(BuildFilter<std::string>(value, EQ, &GetEndTime)); | 291 return AddFilter(BuildFilter<std::string>(value, EQ, &GetEndTime)); |
| 291 case FILTER_STARTED_AFTER: | 292 case FILTER_STARTED_AFTER: |
| 292 return AddFilter(BuildFilter<std::string>(value, GT, &GetStartTime)); | 293 return AddFilter(BuildFilter<std::string>(value, GT, &GetStartTime)); |
| 293 case FILTER_STARTED_BEFORE: | 294 case FILTER_STARTED_BEFORE: |
| 294 return AddFilter(BuildFilter<std::string>(value, LT, &GetStartTime)); | 295 return AddFilter(BuildFilter<std::string>(value, LT, &GetStartTime)); |
| 295 case FILTER_START_TIME: | 296 case FILTER_START_TIME: |
| 296 return AddFilter(BuildFilter<std::string>(value, EQ, &GetStartTime)); | 297 return AddFilter(BuildFilter<std::string>(value, EQ, &GetStartTime)); |
| 297 case FILTER_TOTAL_BYTES: | 298 case FILTER_TOTAL_BYTES: |
| 298 return AddFilter(BuildFilter<int>(value, EQ, &GetTotalBytes)); | 299 return AddFilter(BuildFilter<double>(value, EQ, &GetTotalBytes)); |
| 299 case FILTER_TOTAL_BYTES_GREATER: | 300 case FILTER_TOTAL_BYTES_GREATER: |
| 300 return AddFilter(BuildFilter<int>(value, GT, &GetTotalBytes)); | 301 return AddFilter(BuildFilter<double>(value, GT, &GetTotalBytes)); |
| 301 case FILTER_TOTAL_BYTES_LESS: | 302 case FILTER_TOTAL_BYTES_LESS: |
| 302 return AddFilter(BuildFilter<int>(value, LT, &GetTotalBytes)); | 303 return AddFilter(BuildFilter<double>(value, LT, &GetTotalBytes)); |
| 303 case FILTER_URL: | 304 case FILTER_URL: |
| 304 return AddFilter(BuildFilter<std::string>(value, EQ, &GetUrl)); | 305 return AddFilter(BuildFilter<std::string>(value, EQ, &GetUrl)); |
| 305 case FILTER_URL_REGEX: | 306 case FILTER_URL_REGEX: |
| 306 return AddFilter(BuildRegexFilter(value, &GetUrl)); | 307 return AddFilter(BuildRegexFilter(value, &GetUrl)); |
| 307 } | 308 } |
| 308 return false; | 309 return false; |
| 309 } | 310 } |
| 310 | 311 |
| 311 bool DownloadQuery::Matches(const DownloadItem& item) const { | 312 bool DownloadQuery::Matches(const DownloadItem& item) const { |
| 312 for (FilterCallbackVector::const_iterator filter = filters_.begin(); | 313 for (FilterCallbackVector::const_iterator filter = filters_.begin(); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 sorters_.push_back(Sorter::Build<DownloadItem::DownloadState>( | 412 sorters_.push_back(Sorter::Build<DownloadItem::DownloadState>( |
| 412 direction, &GetState)); | 413 direction, &GetState)); |
| 413 break; | 414 break; |
| 414 case SORT_PAUSED: | 415 case SORT_PAUSED: |
| 415 sorters_.push_back(Sorter::Build<bool>(direction, &IsPaused)); | 416 sorters_.push_back(Sorter::Build<bool>(direction, &IsPaused)); |
| 416 break; | 417 break; |
| 417 case SORT_MIME: | 418 case SORT_MIME: |
| 418 sorters_.push_back(Sorter::Build<std::string>(direction, &GetMimeType)); | 419 sorters_.push_back(Sorter::Build<std::string>(direction, &GetMimeType)); |
| 419 break; | 420 break; |
| 420 case SORT_BYTES_RECEIVED: | 421 case SORT_BYTES_RECEIVED: |
| 421 sorters_.push_back(Sorter::Build<int>(direction, &GetReceivedBytes)); | 422 sorters_.push_back(Sorter::Build<double>(direction, &GetReceivedBytes)); |
| 422 break; | 423 break; |
| 423 case SORT_TOTAL_BYTES: | 424 case SORT_TOTAL_BYTES: |
| 424 sorters_.push_back(Sorter::Build<int>(direction, &GetTotalBytes)); | 425 sorters_.push_back(Sorter::Build<double>(direction, &GetTotalBytes)); |
| 425 break; | 426 break; |
| 426 } | 427 } |
| 427 } | 428 } |
| 428 | 429 |
| 429 void DownloadQuery::FinishSearch(DownloadQuery::DownloadVector* results) const { | 430 void DownloadQuery::FinishSearch(DownloadQuery::DownloadVector* results) const { |
| 430 if (!sorters_.empty()) { | 431 if (!sorters_.empty()) { |
| 431 std::partial_sort(results->begin(), | 432 std::partial_sort(results->begin(), |
| 432 results->begin() + std::min(limit_, results->size()), | 433 results->begin() + std::min(limit_, results->size()), |
| 433 results->end(), | 434 results->end(), |
| 434 DownloadComparator(sorters_)); | 435 DownloadComparator(sorters_)); |
| 435 } | 436 } |
| 436 | 437 |
| 437 if (results->size() > limit_) | 438 if (results->size() > limit_) |
| 438 results->resize(limit_); | 439 results->resize(limit_); |
| 439 } | 440 } |
| OLD | NEW |