| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_ |
| 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <map> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/callback.h" |
| 14 #include "content/browser/download/download_item.h" |
| 15 |
| 16 namespace download_util { |
| 17 |
| 18 // Filter and sort a vector of DownloadItem*s. See also the macros at the bottom |
| 19 // of this file. |
| 20 // |
| 21 // DownloadQuery query; |
| 22 // CHECK(query.Set("startTime", 0)); |
| 23 // bool FilterOutOddDownloads(const DownloadItem& item) { |
| 24 // return 0 == (item.id() % 2); |
| 25 // } |
| 26 // CHECK(query.Set("filter", base::Bind(&FilterOutOddDownloads))); |
| 27 // OrderTermVector order_by; |
| 28 // order_by.push_back(OrderTerm(SORT_FIELD_BYTES_RECEIVED, ASCENDING)); |
| 29 // order_by.push_back(OrderTerm(SORT_FIELD_URL, DESCENDING)); |
| 30 // CHECK(query.OrderBy(order_by)); |
| 31 // query.Limit(20); |
| 32 // DownloadVector all_items, results; |
| 33 // query.Search(all_items.begin(), all_items.end(), &results); |
| 34 class DownloadQuery { |
| 35 public: |
| 36 enum FilterFieldName { |
| 37 FILTER_FIELD_BYTES_RECEIVED, |
| 38 FILTER_FIELD_DANGER, |
| 39 FILTER_FIELD_DANGER_ACCEPTED, |
| 40 FILTER_FIELD_ENDED_AFTER, |
| 41 FILTER_FIELD_ENDED_BEFORE, |
| 42 FILTER_FIELD_END_TIME, |
| 43 FILTER_FIELD_ERROR, |
| 44 FILTER_FIELD_FILENAME, |
| 45 FILTER_FIELD_FILENAME_REGEX, |
| 46 FILTER_FIELD_FILE_SIZE, |
| 47 FILTER_FIELD_FILE_SIZE_GREATER, |
| 48 FILTER_FIELD_FILE_SIZE_LESS, |
| 49 FILTER_FIELD_FILTER, |
| 50 FILTER_FIELD_MIME, |
| 51 FILTER_FIELD_PAUSED, |
| 52 FILTER_FIELD_QUERY, |
| 53 FILTER_FIELD_STARTED_AFTER, |
| 54 FILTER_FIELD_STARTED_BEFORE, |
| 55 FILTER_FIELD_START_TIME, |
| 56 FILTER_FIELD_STATE, |
| 57 FILTER_FIELD_TOTAL_BYTES, |
| 58 FILTER_FIELD_TOTAL_BYTES_GREATER, |
| 59 FILTER_FIELD_TOTAL_BYTES_LESS, |
| 60 FILTER_FIELD_URL, |
| 61 FILTER_FIELD_URL_REGEX, |
| 62 }; |
| 63 |
| 64 enum SortFieldName { |
| 65 SORT_FIELD_BYTES_RECEIVED, |
| 66 SORT_FIELD_DANGER, |
| 67 SORT_FIELD_DANGER_ACCEPTED, |
| 68 SORT_FIELD_END_TIME, |
| 69 SORT_FIELD_ERROR, |
| 70 SORT_FIELD_FILENAME, |
| 71 SORT_FIELD_FILE_SIZE, |
| 72 SORT_FIELD_MIME, |
| 73 SORT_FIELD_PAUSED, |
| 74 SORT_FIELD_START_TIME, |
| 75 SORT_FIELD_STATE, |
| 76 SORT_FIELD_TOTAL_BYTES, |
| 77 SORT_FIELD_URL, |
| 78 }; |
| 79 |
| 80 enum SortFieldDirection { |
| 81 ASCENDING, |
| 82 DESCENDING, |
| 83 }; |
| 84 |
| 85 DownloadQuery(); |
| 86 ~DownloadQuery(); |
| 87 |
| 88 // Returns false if |name| is unrecognized or if |value| is the wrong type or |
| 89 // malformed. Returns true and sets the filter field named |name| to |value| |
| 90 // otherwise. The filter field will be used to filter out DownloadItem*s when |
| 91 // you call Search(). See the implementation for the type of each |name|. |
| 92 // (DRY.) |
| 93 template <typename ValueType> |
| 94 bool Filter(FilterFieldName name, const ValueType& value); |
| 95 |
| 96 // XXX comment |
| 97 void Sort(SortFieldName name, SortFieldDirection direction); |
| 98 |
| 99 // Remember to limit the size of search results to |limit|. |
| 100 void Limit(size_t limit) { limit_ = limit; } |
| 101 |
| 102 typedef std::vector<DownloadItem*> DownloadVector; |
| 103 |
| 104 // Filters DownloadItem*s from |iter| to |last| into |results|, sorts |
| 105 // |results|, and limits the size of |results|. |results| must be non-NULL. |
| 106 template <typename InputIterator> |
| 107 void Search(InputIterator iter, const InputIterator last, |
| 108 DownloadVector* results) const; |
| 109 |
| 110 // Returns true if |item| matches all set filter fields; returns false if any |
| 111 // filter fields have been set that the item does not match. |
| 112 bool Matches(const DownloadItem& item) const; |
| 113 |
| 114 private: |
| 115 class FilterFieldInterface; |
| 116 struct OrderTerm { |
| 117 OrderTerm(SortFieldName _name, SortFieldDirection _direction) |
| 118 : name(_name), |
| 119 direction(_direction) { |
| 120 } |
| 121 SortFieldName name; |
| 122 SortFieldDirection direction; |
| 123 }; |
| 124 typedef std::vector<FilterFieldInterface*> FilterFieldVector; |
| 125 typedef std::vector<OrderTerm> OrderTermVector; |
| 126 |
| 127 bool AddFilter(FilterFieldInterface* field); |
| 128 |
| 129 FilterFieldVector filter_fields_; |
| 130 OrderTermVector order_by_fields_; |
| 131 size_t limit_; |
| 132 |
| 133 DISALLOW_COPY_AND_ASSIGN(DownloadQuery); |
| 134 }; |
| 135 } // namespace download_util |
| 136 |
| 137 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_ |
| OLD | NEW |