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 // Filter and sort a vector of DownloadItem*s. | |
17 // | |
18 // The following example copies from |all_items| to |results| those | |
19 // DownloadItem*s whose start time is not 0 or whose id is odd, sorts primarily | |
20 // by bytes received ascending and secondarily by url descending, and limits the | |
21 // results to 20 items. Any number of filter fields or sort rules is | |
22 // permissible. If all sort fields compare two DownloadItems equivalently, then | |
23 // they are sorted by their id ascending or db_handle ascending. | |
24 // | |
25 // DownloadQuery query; | |
26 // CHECK(query.Filter(FILTER_FIELD_START_TIME, 0)); | |
27 // bool FilterOutOddDownloads(const DownloadItem& item) { | |
28 // return 0 == (item.GetId() % 2); | |
29 // } | |
30 // CHECK(query.Filter(FILTER_FIELD_FILTER, base::Bind(&FilterOutOddDownloads))); | |
31 // query.Sort(SORT_FIELD_BYTES_RECEIVED, ASCENDING); | |
32 // query.Sort(SORT_FIELD_URL, DESCENDING); | |
33 // query.Limit(20); | |
34 // DownloadVector all_items, results; | |
35 // query.Search(all_items.begin(), all_items.end(), &results); | |
36 class CONTENT_EXPORT DownloadQuery { | |
37 public: | |
38 typedef std::vector<DownloadItem*> DownloadVector; | |
39 typedef base::Callback<bool(const DownloadItem&)> FilterType; | |
Randy Smith (Not in Mondays)
2011/11/30 19:26:17
A comment here or on the Filter() interface that u
benjhayden
2011/11/30 22:13:49
Done.
| |
40 | |
41 enum FilterFieldName { | |
42 FILTER_FIELD_BYTES_RECEIVED, | |
43 FILTER_FIELD_DANGER_ACCEPTED, | |
44 FILTER_FIELD_ENDED_AFTER, | |
45 FILTER_FIELD_ENDED_BEFORE, | |
46 FILTER_FIELD_END_TIME, | |
47 FILTER_FIELD_ERROR, | |
48 FILTER_FIELD_FILENAME, | |
49 FILTER_FIELD_FILENAME_REGEX, | |
50 FILTER_FIELD_FILE_SIZE, | |
51 FILTER_FIELD_FILE_SIZE_GREATER, | |
52 FILTER_FIELD_FILE_SIZE_LESS, | |
53 FILTER_FIELD_MIME, | |
54 FILTER_FIELD_PAUSED, | |
55 FILTER_FIELD_QUERY, | |
56 FILTER_FIELD_STARTED_AFTER, | |
57 FILTER_FIELD_STARTED_BEFORE, | |
58 FILTER_FIELD_START_TIME, | |
59 FILTER_FIELD_TOTAL_BYTES, | |
60 FILTER_FIELD_TOTAL_BYTES_GREATER, | |
61 FILTER_FIELD_TOTAL_BYTES_LESS, | |
62 FILTER_FIELD_URL, | |
63 FILTER_FIELD_URL_REGEX, | |
64 }; | |
65 | |
66 enum SortFieldName { | |
67 SORT_FIELD_BYTES_RECEIVED, | |
68 SORT_FIELD_DANGER, | |
69 SORT_FIELD_DANGER_ACCEPTED, | |
70 SORT_FIELD_END_TIME, | |
71 SORT_FIELD_ERROR, | |
72 SORT_FIELD_FILENAME, | |
73 SORT_FIELD_FILE_SIZE, | |
74 SORT_FIELD_MIME, | |
75 SORT_FIELD_PAUSED, | |
76 SORT_FIELD_START_TIME, | |
77 SORT_FIELD_STATE, | |
78 SORT_FIELD_TOTAL_BYTES, | |
79 SORT_FIELD_URL, | |
80 }; | |
81 | |
82 enum SortFieldDirection { | |
83 ASCENDING, | |
84 DESCENDING, | |
85 }; | |
86 | |
87 DownloadQuery(); | |
88 ~DownloadQuery(); | |
89 | |
90 // Returns false if |name| is unrecognized or if |value| is the wrong type or | |
91 // malformed. Otherwise, makes a new filter named |name|, sets its value to | |
Randy Smith (Not in Mondays)
2011/11/30 19:26:17
nit, suggestion: "makes" -> "adds"?
Randy Smith (Not in Mondays)
2011/11/30 19:26:17
nit: "filter named |name|" implies that the name i
benjhayden
2011/11/30 22:13:49
Done.
benjhayden
2011/11/30 22:13:49
Done.
| |
92 // |value|, and returns true. Search() will filter out all DownloadItem*s that | |
93 // do not match all filters. See the implementation for the type of each | |
Randy Smith (Not in Mondays)
2011/11/30 19:26:17
nit: I dislike pointing people at the implementati
benjhayden
2011/11/30 22:13:49
Done.
| |
94 // |name|. | |
95 bool Filter(FilterFieldName name, int value); | |
96 bool Filter(FilterFieldName name, bool value); | |
97 bool Filter(FilterFieldName name, const std::string& value); | |
98 bool Filter(FilterFieldName name, const string16& value); | |
99 void Filter(DownloadStateInfo::DangerType danger); | |
100 void Filter(DownloadItem::DownloadState state); | |
101 void Filter(const FilterType& value); | |
102 | |
103 // Makes a new sort field named |name| and sets its direction to |direction|. | |
Randy Smith (Not in Mondays)
2011/11/30 19:26:17
nit, suggestion: "Makes" -> "Adds"?
benjhayden
2011/11/30 22:13:49
Done.
| |
104 // After filtering DownloadItem*s, Search() will sort the results primarily by | |
105 // the sort field from the first call to Sort(), secondarily by the sort field | |
106 // from the second call to Sort(), and so on. For example, if the | |
107 // InputIterator passed to Search() yields four DownloadItems {id:0, error:0, | |
108 // start_time:0}, {id:1, error:0, start_time:1}, {id:2, error:1, | |
109 // start_time:0}, {id:3, error:1, start_time:1}, and Sort is called twice, | |
110 // once with (SORT_FIELD_ERROR, ASCENDING) then with (SORT_FIELD_START_TIME, | |
111 // DESCENDING), then Search() will return items ordered 1,0,3,2. | |
112 void Sort(SortFieldName name, SortFieldDirection direction); | |
113 | |
114 // Limit the size of search results to |limit|. | |
115 void Limit(size_t limit) { limit_ = limit; } | |
116 | |
117 // Filters DownloadItem*s from |iter| to |last| into |results|, sorts | |
118 // |results|, and limits the size of |results|. |results| must be non-NULL. | |
119 template <typename InputIterator> | |
120 void Search(InputIterator iter, const InputIterator last, | |
121 DownloadVector* results) const; | |
122 | |
123 // Returns true if |item| matches all set filter fields; returns false if any | |
124 // filter fields have been set that the item does not match. | |
125 bool Matches(const DownloadItem& item) const; | |
126 | |
127 private: | |
128 struct SortField; | |
129 class DownloadComparator; | |
130 typedef std::vector<FilterType> FilterFieldVector; | |
131 typedef std::vector<SortField> SortFieldVector; | |
132 | |
133 FilterFieldVector filter_fields_; | |
134 SortFieldVector sort_fields_; | |
135 size_t limit_; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(DownloadQuery); | |
138 }; | |
139 | |
140 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_ | |
OLD | NEW |