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

Side by Side Diff: content/browser/download/download_query.h

Issue 8601012: DownloadQuery filters and sorts DownloadItems. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: comments Created 9 years 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
(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(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 DownloadQuery();
39 ~DownloadQuery();
40
41 // FilterType is a Callback that takes a DownloadItem and returns true if the
42 // item matches the filter and false otherwise.
43 // query.Filter(base::Bind(&YourFilterFunction));
44 typedef base::Callback<bool(const DownloadItem&)> FilterType;
45
46 enum FilterFieldName {
47 FILTER_FIELD_BYTES_RECEIVED, // int
Randy Smith (Not in Mondays) 2011/12/01 19:24:30 nit, suggestion: I find this type of annotation ea
benjhayden 2011/12/02 22:31:53 Done.
48 FILTER_FIELD_DANGER_ACCEPTED, // bool
49 FILTER_FIELD_ENDED_AFTER, // int
50 FILTER_FIELD_ENDED_BEFORE, // int
51 FILTER_FIELD_END_TIME, // int
52 FILTER_FIELD_ERROR, // int
53 FILTER_FIELD_FILENAME, // string16
54 FILTER_FIELD_FILENAME_REGEX, // string16
55 FILTER_FIELD_FILE_SIZE, // int
56 FILTER_FIELD_FILE_SIZE_GREATER, // int
57 FILTER_FIELD_FILE_SIZE_LESS, // int
58 FILTER_FIELD_MIME, // std::string
59 FILTER_FIELD_PAUSED, // bool
60 FILTER_FIELD_QUERY, // string16
61 FILTER_FIELD_STARTED_AFTER, // int
62 FILTER_FIELD_STARTED_BEFORE, // int
63 FILTER_FIELD_START_TIME, // int
64 FILTER_FIELD_TOTAL_BYTES, // int
65 FILTER_FIELD_TOTAL_BYTES_GREATER, // int
66 FILTER_FIELD_TOTAL_BYTES_LESS, // int
67 FILTER_FIELD_URL, // std::string
68 FILTER_FIELD_URL_REGEX, // std::string
69 };
70
71 // Returns false if |name| is unrecognized or if |value| is the wrong type or
72 // malformed. Otherwise, adds a new filter of type |name| and with value
73 // |value|, and returns true. Search() will filter out all DownloadItem*s that
74 // do not match all filters. Multiple instances of the same FilterFieldName
75 // are allowed, so you can pass two regexes to Filter(URL_REGEX,...) in order
76 // to Search() for items whose url matches both regexes. You can also pass two
77 // different DownloadStates to Filter(), which will cause Search() to filter
78 // out all items.
79 bool Filter(FilterFieldName name, int value);
80 bool Filter(FilterFieldName name, bool value);
81 bool Filter(FilterFieldName name, const std::string& value);
82 bool Filter(FilterFieldName name, const string16& value);
83 void Filter(DownloadStateInfo::DangerType danger);
84 void Filter(DownloadItem::DownloadState state);
85 void Filter(const FilterType& filter);
86
87 enum SortFieldName {
88 SORT_FIELD_BYTES_RECEIVED,
89 SORT_FIELD_DANGER,
90 SORT_FIELD_DANGER_ACCEPTED,
91 SORT_FIELD_END_TIME,
92 SORT_FIELD_ERROR,
93 SORT_FIELD_FILENAME,
94 SORT_FIELD_FILE_SIZE,
95 SORT_FIELD_MIME,
96 SORT_FIELD_PAUSED,
97 SORT_FIELD_START_TIME,
98 SORT_FIELD_STATE,
99 SORT_FIELD_TOTAL_BYTES,
100 SORT_FIELD_URL,
101 };
102
103 enum SortFieldDirection {
104 ASCENDING,
105 DESCENDING,
106 };
107
108 // Adds a new sort field named |name| and sets its direction to |direction|.
Randy Smith (Not in Mondays) 2011/12/01 19:24:30 nit: I'm still tripping over the connotations in t
benjhayden 2011/12/02 22:31:53 I'm open to suggestions. There's a Callback typede
109 // After filtering DownloadItem*s, Search() will sort the results primarily by
110 // the sort field from the first call to Sort(), secondarily by the sort field
111 // from the second call to Sort(), and so on. For example, if the
112 // InputIterator passed to Search() yields four DownloadItems {id:0, error:0,
113 // start_time:0}, {id:1, error:0, start_time:1}, {id:2, error:1,
114 // start_time:0}, {id:3, error:1, start_time:1}, and Sort is called twice,
115 // once with (SORT_FIELD_ERROR, ASCENDING) then with (SORT_FIELD_START_TIME,
116 // DESCENDING), then Search() will return items ordered 1,0,3,2.
117 void Sort(SortFieldName name, SortFieldDirection direction);
118
119 // Limit the size of search results to |limit|.
120 void Limit(size_t limit) { limit_ = limit; }
121
122 typedef std::vector<DownloadItem*> DownloadVector;
123
124 // Filters DownloadItem*s from |iter| to |last| into |results|, sorts
125 // |results|, and limits the size of |results|. |results| must be non-NULL.
126 template <typename InputIterator>
127 void Search(InputIterator iter, const InputIterator last,
128 DownloadVector* results) const;
129
130 // Returns true if |item| matches all set filter fields; returns false if any
131 // filter fields have been set that the item does not match.
132 bool Matches(const DownloadItem& item) const;
133
134 private:
135 struct SortField;
136 class DownloadComparator;
137 typedef std::vector<FilterType> FilterFieldVector;
138 typedef std::vector<SortField> SortFieldVector;
139
140 bool FilterRegex(const std::string& regex_str,
141 const base::Callback<std::string(const DownloadItem&)>& accessor);
142
143 FilterFieldVector filter_fields_;
144 SortFieldVector sort_fields_;
145 size_t limit_;
146
147 DISALLOW_COPY_AND_ASSIGN(DownloadQuery);
148 };
149
150 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_
OLDNEW
« no previous file with comments | « no previous file | content/browser/download/download_query.cc » ('j') | content/browser/download/download_query.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698