Chromium Code Reviews| 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 { | |
|
Randy Smith (Not in Mondays)
2011/11/23 21:07:34
Given that we've got a pretty descriptive top leve
benjhayden
2011/11/30 16:21:47
Done.
cbentzel
2011/12/05 21:56:26
Have there been any thoughts about moving content/
Randy Smith (Not in Mondays)
2011/12/12 18:17:54
We've thought about it, but it's never seemed need
| |
| 17 | |
| 18 // Filter and sort a vector of DownloadItem*s. | |
| 19 // | |
| 20 // The following example copies from |all_items| to |results| those | |
| 21 // DownloadItem*s whose start time is not 0 or whose id is odd, sorts primarily | |
| 22 // by bytes received ascending and secondarily by url descending, and limits the | |
| 23 // results to 20 items. Any number of filter fields or sort rules is | |
| 24 // permissible. After all explicit sort rules are applied, two implicit sort | |
| 25 // rules are applied: id ascending then db_handle ascending. | |
|
Randy Smith (Not in Mondays)
2011/11/23 21:07:34
nit: I find this comment sentence a little confusi
benjhayden
2011/11/30 16:21:47
Done.
| |
| 26 // | |
| 27 // DownloadQuery query; | |
| 28 // CHECK(query.Filter(FILTER_FIELD_START_TIME, 0)); | |
| 29 // bool FilterOutOddDownloads(const DownloadItem& item) { | |
| 30 // return 0 == (item.GetId() % 2); | |
| 31 // } | |
| 32 // CHECK(query.Filter(FILTER_FIELD_FILTER, base::Bind(&FilterOutOddDownloads))); | |
| 33 // query.Sort(SORT_FIELD_BYTES_RECEIVED, ASCENDING); | |
| 34 // query.Sort(SORT_FIELD_URL, DESCENDING); | |
| 35 // query.Limit(20); | |
| 36 // DownloadVector all_items, results; | |
| 37 // query.Search(all_items.begin(), all_items.end(), &results); | |
| 38 class CONTENT_EXPORT DownloadQuery { | |
| 39 public: | |
| 40 typedef std::vector<DownloadItem*> DownloadVector; | |
| 41 | |
| 42 enum FilterFieldName { | |
| 43 FILTER_FIELD_BYTES_RECEIVED, | |
| 44 FILTER_FIELD_DANGER, | |
| 45 FILTER_FIELD_DANGER_ACCEPTED, | |
| 46 FILTER_FIELD_ENDED_AFTER, | |
| 47 FILTER_FIELD_ENDED_BEFORE, | |
| 48 FILTER_FIELD_END_TIME, | |
| 49 FILTER_FIELD_ERROR, | |
| 50 FILTER_FIELD_FILENAME, | |
| 51 FILTER_FIELD_FILENAME_REGEX, | |
| 52 FILTER_FIELD_FILE_SIZE, | |
| 53 FILTER_FIELD_FILE_SIZE_GREATER, | |
| 54 FILTER_FIELD_FILE_SIZE_LESS, | |
| 55 FILTER_FIELD_FILTER, | |
| 56 FILTER_FIELD_MIME, | |
| 57 FILTER_FIELD_PAUSED, | |
| 58 FILTER_FIELD_QUERY, | |
| 59 FILTER_FIELD_STARTED_AFTER, | |
| 60 FILTER_FIELD_STARTED_BEFORE, | |
| 61 FILTER_FIELD_START_TIME, | |
| 62 FILTER_FIELD_STATE, | |
| 63 FILTER_FIELD_TOTAL_BYTES, | |
| 64 FILTER_FIELD_TOTAL_BYTES_GREATER, | |
| 65 FILTER_FIELD_TOTAL_BYTES_LESS, | |
| 66 FILTER_FIELD_URL, | |
| 67 FILTER_FIELD_URL_REGEX, | |
| 68 }; | |
| 69 | |
| 70 enum SortFieldName { | |
| 71 SORT_FIELD_BYTES_RECEIVED, | |
| 72 SORT_FIELD_DANGER, | |
| 73 SORT_FIELD_DANGER_ACCEPTED, | |
| 74 SORT_FIELD_END_TIME, | |
| 75 SORT_FIELD_ERROR, | |
| 76 SORT_FIELD_FILENAME, | |
| 77 SORT_FIELD_FILE_SIZE, | |
| 78 SORT_FIELD_MIME, | |
| 79 SORT_FIELD_PAUSED, | |
| 80 SORT_FIELD_START_TIME, | |
| 81 SORT_FIELD_STATE, | |
| 82 SORT_FIELD_TOTAL_BYTES, | |
| 83 SORT_FIELD_URL, | |
| 84 }; | |
| 85 | |
| 86 enum SortFieldDirection { | |
| 87 ASCENDING, | |
| 88 DESCENDING, | |
| 89 }; | |
| 90 | |
| 91 DownloadQuery(); | |
| 92 ~DownloadQuery(); | |
| 93 | |
| 94 // Returns false if |name| is unrecognized or if |value| is the wrong type or | |
| 95 // malformed. Otherwise, makes a new filter named |name|, sets its value to | |
| 96 // |value|, and returns true. Search() will filter out all DownloadItem*s that | |
| 97 // do not match all filters. See the implementation for the type of each | |
| 98 // |name|. (DRY.) | |
|
Randy Smith (Not in Mondays)
2011/11/23 21:07:34
nit: What doe the "DRY" add to the comment? (I'm
benjhayden
2011/11/30 16:21:47
Done.
| |
| 99 template <typename ValueType> | |
| 100 bool Filter(FilterFieldName name, const ValueType& value); | |
| 101 | |
| 102 // Makes a new sort field named |name| and sets its direction to |direction|. | |
| 103 // After filtering DownloadItem*s, Search() will sort the results primarily by | |
| 104 // the first sort field, secondarily by the second sort field, and so on. For | |
|
Randy Smith (Not in Mondays)
2011/11/23 21:07:34
nit: I'd say "field in first call to sort"; "first
benjhayden
2011/11/30 16:21:47
Done.
| |
| 105 // example, if the InputIterator passed to Search() yields four DownloadItems | |
| 106 // {id:0, error:0, start_time:0}, {id:1, error:0, start_time:1}, {id:2, | |
| 107 // error:1, start_time:0}, {id:3, error:1, start_time:1}, and Sort is called | |
| 108 // twice, once with (SORT_FIELD_ERROR, ASCENDING) then with | |
| 109 // (SORT_FIELD_START_TIME, DESCENDING), then Search() will return items | |
| 110 // ordered 1,0,3,2. | |
| 111 void Sort(SortFieldName name, SortFieldDirection direction); | |
| 112 | |
| 113 // Limit the size of search results to |limit|. | |
| 114 void Limit(size_t limit) { limit_ = limit; } | |
| 115 | |
| 116 // Filters DownloadItem*s from |iter| to |last| into |results|, sorts | |
| 117 // |results|, and limits the size of |results|. |results| must be non-NULL. | |
| 118 template <typename InputIterator> | |
| 119 void Search(InputIterator iter, const InputIterator last, | |
| 120 DownloadVector* results) const; | |
| 121 | |
| 122 // Returns true if |item| matches all set filter fields; returns false if any | |
| 123 // filter fields have been set that the item does not match. | |
| 124 bool Matches(const DownloadItem& item) const; | |
|
Randy Smith (Not in Mondays)
2011/11/23 21:07:34
Why is this a public member function?
benjhayden
2011/11/30 16:21:47
If you pass an id to search(), then DownloadSearch
| |
| 125 | |
| 126 private: | |
| 127 class FilterFieldInterface; | |
| 128 struct OrderTerm { | |
| 129 OrderTerm(SortFieldName _name, SortFieldDirection _direction) | |
| 130 : name(_name), | |
| 131 direction(_direction) { | |
| 132 } | |
| 133 SortFieldName name; | |
| 134 SortFieldDirection direction; | |
| 135 }; | |
| 136 typedef std::vector<FilterFieldInterface*> FilterFieldVector; | |
| 137 typedef std::vector<OrderTerm> OrderTermVector; | |
| 138 | |
| 139 bool AddFilter(FilterFieldInterface* field); | |
| 140 | |
| 141 FilterFieldVector filter_fields_; | |
| 142 OrderTermVector order_by_fields_; | |
| 143 size_t limit_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(DownloadQuery); | |
| 146 }; | |
| 147 } // namespace download_util | |
| 148 | |
| 149 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_ | |
| OLD | NEW |