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

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: include 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
« no previous file with comments | « no previous file | content/browser/download/download_query.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_forward.h"
14 #include "content/browser/download/download_item.h"
15
16 namespace base {
17 class Value;
18 }
19
20 // Filter and sort a vector of DownloadItem*s.
21 //
22 // The following example copies from |all_items| to |results| those
23 // DownloadItem*s whose start time is 0 and whose id is odd, sorts primarily by
24 // bytes received ascending and secondarily by url descending, and limits the
25 // results to 20 items. Any number of filters or sorters is allowed. If all
26 // sorters compare two DownloadItems equivalently, then they are sorted by their
27 // id ascending.
28 //
29 // DownloadQuery query;
30 // scoped_ptr<base::Value> start_time(base::Balue::CreateIntegerValue(0));
31 // CHECK(query.AddFilter(FILTER_START_TIME, *start_time.get()));
32 // bool FilterOutOddDownloads(const DownloadItem& item) {
33 // return 0 == (item.GetId() % 2);
34 // }
35 // CHECK(query.AddFilter(base::Bind(&FilterOutOddDownloads)));
36 // query.AddSorter(SORT_BYTES_RECEIVED, ASCENDING);
37 // query.AddSorter(SORT_URL, DESCENDING);
38 // query.Limit(20);
39 // DownloadVector all_items, results;
40 // query.Search(all_items.begin(), all_items.end(), &results);
41 class CONTENT_EXPORT DownloadQuery {
42 public:
43 typedef std::vector<DownloadItem*> DownloadVector;
44
45 // FilterCallback is a Callback that takes a DownloadItem and returns true if
46 // the item matches the filter and false otherwise.
47 // query.AddFilter(base::Bind(&YourFilterFunction));
48 typedef base::Callback<bool(const DownloadItem&)> FilterCallback;
49
50 // All times are the number of milliseconds since the Unix epoch.
51 enum FilterType {
52 FILTER_BYTES_RECEIVED, // int
53 FILTER_DANGER_ACCEPTED, // bool
54 FILTER_FILENAME, // string
55 FILTER_FILENAME_REGEX, // string
56 FILTER_MIME, // string
57 FILTER_PAUSED, // bool
58 FILTER_QUERY, // string
59 FILTER_STARTED_AFTER, // int
60 FILTER_STARTED_BEFORE, // int
61 FILTER_START_TIME, // int
62 FILTER_TOTAL_BYTES, // int
63 FILTER_TOTAL_BYTES_GREATER, // int
64 FILTER_TOTAL_BYTES_LESS, // int
65 FILTER_URL, // string
66 FILTER_URL_REGEX, // string
67 };
68
69 enum SortType {
70 SORT_BYTES_RECEIVED,
71 SORT_DANGER,
72 SORT_DANGER_ACCEPTED,
73 SORT_FILENAME,
74 SORT_MIME,
75 SORT_PAUSED,
76 SORT_START_TIME,
77 SORT_STATE,
78 SORT_TOTAL_BYTES,
79 SORT_URL,
80 };
81
82 enum SortDirection {
83 ASCENDING,
84 DESCENDING,
85 };
86
87 DownloadQuery();
88 ~DownloadQuery();
89
90 // Adds a new filter of type |type| with value |value| and returns true if
91 // |type| is valid and |value| is the correct Value-type and well-formed.
92 // Returns false if |type| is invalid or |value| is the incorrect Value-type
93 // or malformed. Search() will filter out all DownloadItem*s that do not
94 // match all filters. Multiple instances of the same FilterType are allowed,
95 // so you can pass two regexes to AddFilter(URL_REGEX,...) in order to
96 // Search() for items whose url matches both regexes. You can also pass two
97 // different DownloadStates to AddFilter(), which will cause Search() to
98 // filter out all items.
99 bool AddFilter(const FilterCallback& filter);
100 bool AddFilter(FilterType type, const base::Value& value);
101 void AddFilter(DownloadStateInfo::DangerType danger);
102 void AddFilter(DownloadItem::DownloadState state);
103
104 // Adds a new sorter of type |type| with direction |direction|. After
105 // filtering DownloadItem*s, Search() will sort the results primarily by the
106 // sorter from the first call to Sort(), secondarily by the sorter from the
107 // second call to Sort(), and so on. For example, if the InputIterator passed
108 // to Search() yields four DownloadItems {id:0, error:0, start_time:0}, {id:1,
109 // error:0, start_time:1}, {id:2, error:1, start_time:0}, {id:3, error:1,
110 // start_time:1}, and Sort is called twice, once with (SORT_ERROR, ASCENDING)
111 // then with (SORT_START_TIME, DESCENDING), then Search() will return items
112 // ordered 1,0,3,2.
113 void AddSorter(SortType type, SortDirection direction);
114
115 // Limit the size of search results to |limit|.
116 void Limit(size_t limit) { limit_ = limit; }
117
118 // Filters DownloadItem*s from |iter| to |last| into |results|, sorts
119 // |results|, and limits the size of |results|. |results| must be non-NULL.
120 template <typename InputIterator>
121 void Search(InputIterator iter, const InputIterator last,
122 DownloadVector* results) const {
123 results->clear();
124 for (; iter != last; ++iter) {
125 if (Matches(**iter)) results->push_back(*iter);
126 }
127 FinishSearch(results);
128 }
129
130 private:
131 struct Sorter;
132 class DownloadComparator;
133 typedef std::vector<FilterCallback> FilterCallbackVector;
134 typedef std::vector<Sorter> SorterVector;
135
136 bool FilterRegex(const std::string& regex_str,
137 const base::Callback<std::string(const DownloadItem&)>& accessor);
138 bool Matches(const DownloadItem& item) const;
139 void FinishSearch(DownloadVector* results) const;
140
141 FilterCallbackVector filters_;
142 SorterVector sorters_;
143 size_t limit_;
144
145 DISALLOW_COPY_AND_ASSIGN(DownloadQuery);
146 };
147
148 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_
OLDNEW
« no previous file with comments | « no previous file | content/browser/download/download_query.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698