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

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: tests 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;
cbentzel 2011/12/05 21:56:26 Nice use of sample code to indicate how this works
benjhayden 2011/12/09 19:29:15 Done.
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 {
cbentzel 2011/12/05 21:56:26 Does this need to be in content/public?
benjhayden 2011/12/09 19:29:15 What's the rule for what goes in content/public? T
Randy Smith (Not in Mondays) 2011/12/12 18:17:54 This is a good question that I don't know the answ
benjhayden 2011/12/12 19:52:10 Done.
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;
cbentzel 2011/12/05 21:56:26 Nit: typedefs and enums come before methods - incl
benjhayden 2011/12/09 19:29:15 Done.
45
46 enum FilterFieldName {
47 FILTER_FIELD_BYTES_RECEIVED, // int
48 FILTER_FIELD_DANGER_ACCEPTED, // bool
49 FILTER_FIELD_FILENAME, // string16
50 FILTER_FIELD_FILENAME_REGEX, // string16
51 FILTER_FIELD_MIME, // std::string
52 FILTER_FIELD_PAUSED, // bool
53 FILTER_FIELD_QUERY, // string16
54 FILTER_FIELD_STARTED_AFTER, // int
55 FILTER_FIELD_STARTED_BEFORE, // int
56 FILTER_FIELD_START_TIME, // int
Randy Smith (Not in Mondays) 2011/12/05 20:25:42 nit: Mention somewhere that times are in ms?
benjhayden 2011/12/09 19:29:15 Done.
57 FILTER_FIELD_TOTAL_BYTES, // int
58 FILTER_FIELD_TOTAL_BYTES_GREATER, // int
59 FILTER_FIELD_TOTAL_BYTES_LESS, // int
60 FILTER_FIELD_URL, // std::string
61 FILTER_FIELD_URL_REGEX, // std::string
62 };
63
64 // Returns false if |name| is unrecognized or if |value| is the wrong type or
cbentzel 2011/12/05 21:56:26 I'd change this comment to indicate that it Adds a
benjhayden 2011/12/09 19:29:15 Done.
65 // malformed. Otherwise, adds a new filter of type |name| and with value
66 // |value|, and returns true. Search() will filter out all DownloadItem*s that
67 // do not match all filters. Multiple instances of the same FilterFieldName
68 // are allowed, so you can pass two regexes to Filter(URL_REGEX,...) in order
69 // to Search() for items whose url matches both regexes. You can also pass two
70 // different DownloadStates to Filter(), which will cause Search() to filter
71 // out all items.
cbentzel 2011/12/05 21:56:26 Thanks for spelling out that this won't check that
benjhayden 2011/12/09 19:29:15 Done.
72 bool Filter(FilterFieldName name, int value);
cbentzel 2011/12/05 21:56:26 Perhaps instead of all the overloading, do bool F
benjhayden 2011/12/09 19:29:15 Done.
Randy Smith (Not in Mondays) 2011/12/12 18:17:54 Does it work to make this a reference rather than
cbentzel 2011/12/12 19:08:29 Reference would work. I think that most of the con
Randy Smith (Not in Mondays) 2011/12/12 19:19:29 I'm a bit reluctant to target DownloadQuery direct
benjhayden 2011/12/12 19:52:10 base::Values don't support the C++ copy constructo
benjhayden 2011/12/12 19:52:10 How would you feel about adding helper methods lik
Randy Smith (Not in Mondays) 2011/12/12 21:01:38 That's ok by me. But let's please add them if we
Randy Smith (Not in Mondays) 2011/12/12 21:01:38 My inclination would be not to do this, based on C
benjhayden 2011/12/12 21:28:06 There are more than half as many const Value& as c
Randy Smith (Not in Mondays) 2011/12/12 21:31:57 Ok, I'm good with passing by reference. I just wa
73 bool Filter(FilterFieldName name, bool value);
cbentzel 2011/12/05 21:56:26 Should these be called AddFilter, to indicate that
benjhayden 2011/12/09 19:29:15 Done.
74 bool Filter(FilterFieldName name, const std::string& value);
75 bool Filter(FilterFieldName name, const string16& value);
76 void Filter(DownloadStateInfo::DangerType danger);
77 void Filter(DownloadItem::DownloadState state);
78 void Filter(const FilterType& filter);
79
80 enum SortFieldName {
81 SORT_FIELD_BYTES_RECEIVED,
82 SORT_FIELD_DANGER,
83 SORT_FIELD_DANGER_ACCEPTED,
84 SORT_FIELD_FILENAME,
85 SORT_FIELD_MIME,
86 SORT_FIELD_PAUSED,
87 SORT_FIELD_START_TIME,
88 SORT_FIELD_STATE,
89 SORT_FIELD_TOTAL_BYTES,
90 SORT_FIELD_URL,
91 };
92
93 enum SortFieldDirection {
94 ASCENDING,
95 DESCENDING,
96 };
97
98 // Adds a new sort field named |name| and sets its direction to |direction|.
99 // After filtering DownloadItem*s, Search() will sort the results primarily by
100 // the sort field from the first call to Sort(), secondarily by the sort field
101 // from the second call to Sort(), and so on. For example, if the
102 // InputIterator passed to Search() yields four DownloadItems {id:0, error:0,
103 // start_time:0}, {id:1, error:0, start_time:1}, {id:2, error:1,
104 // start_time:0}, {id:3, error:1, start_time:1}, and Sort is called twice,
105 // once with (SORT_FIELD_ERROR, ASCENDING) then with (SORT_FIELD_START_TIME,
106 // DESCENDING), then Search() will return items ordered 1,0,3,2.
107 void Sort(SortFieldName name, SortFieldDirection direction);
108
109 // Limit the size of search results to |limit|.
110 void Limit(size_t limit) { limit_ = limit; }
111
112 typedef std::vector<DownloadItem*> DownloadVector;
cbentzel 2011/12/05 21:56:26 typedef's etc move to the top.
benjhayden 2011/12/09 19:29:15 Done.
113
114 // Filters DownloadItem*s from |iter| to |last| into |results|, sorts
115 // |results|, and limits the size of |results|. |results| must be non-NULL.
116 template <typename InputIterator>
117 void Search(InputIterator iter, const InputIterator last,
118 DownloadVector* results) const;
cbentzel 2011/12/05 21:56:26 This could use an OutputIterator such as an insert
benjhayden 2011/12/09 19:29:15 So, it cannot use an OutputIterator, right?
cbentzel 2011/12/09 19:38:58 Correct. I should have removed that comment.
benjhayden 2011/12/12 19:52:10 Done.
119
120 // Returns true if |item| matches all set filter fields; returns false if any
121 // filter fields have been set that the item does not match.
122 bool Matches(const DownloadItem& item) const;
cbentzel 2011/12/05 21:56:26 Does this need to be public? Seems like a private
benjhayden 2011/12/09 19:29:15 Done.
123
124 private:
125 struct SortField;
126 class DownloadComparator;
127 typedef std::vector<FilterType> FilterFieldVector;
128 typedef std::vector<SortField> SortFieldVector;
129
130 bool FilterRegex(const std::string& regex_str,
131 const base::Callback<std::string(const DownloadItem&)>& accessor);
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_
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