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

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

Issue 7825035: Implement chrome.experimental.downloads.search() (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: comments Created 9 years, 2 months 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 <string>
10 #include <vector>
11
12 #include "base/callback.h"
13 #include "base/stl_util.h"
14 #include "content/browser/download/download_item.h"
15
16 namespace download_util {
17
18 // Filter and sort a vector of DownloadItem*s. See also the macros at the bottom
19 // of this file.
20 //
21 // DownloadQuery query;
22 // CHECK(query.Set("startTime", 0));
Randy Smith (Not in Mondays) 2011/10/18 23:58:46 Is this the ideal name for it? "Set" is a generic
23 // bool FilterOutOddDownloads(DownloadItem* item) {
24 // return 0 == (item->id() % 2);
25 // }
26 // CHECK(query.Set("filter", base::Bind(&FilterOutOddDownloads)));
Randy Smith (Not in Mondays) 2011/10/18 23:58:46 I'd be inclined to make filter a separate method;
27 // CHECK(query.OrderBy("-startTime id error"));
28 // query.Limit(20);
29 // DownloadItems all_items, results;
30 // query.Search(all_items, &results);
31 class DownloadQuery {
32 public:
33 typedef std::vector<DownloadItem*> DownloadItems;
34
35 DownloadQuery();
36 ~DownloadQuery();
37
38 // Returns false if |value| is the wrong type or if |name| is unrecognized or
39 // if |value| is malformed. Returns true and sets the filter field named
40 // |name| to |value| otherwise. See the implementation for valid |name|s and
41 // their types. (DRY.)
42 template <typename ValueType>
43 bool Set(const char* name, const ValueType& value);
44
45 // Returns false if |order_by| is not a space-separated sequence of sort field
46 // names, each optionally prefixed by a hyphen. See the implementation for
47 // valid sort field names.
48 bool OrderBy(const std::string& order_by);
Randy Smith (Not in Mondays) 2011/10/18 23:58:46 So one of my thoughts about suggesting that we go
49
50 // Remember to limit the size of search results to |limit|.
51 void Limit(uint32 limit) { limit_ = limit; }
52
53 // Sorts, filters, and limits |all_items| into |results|, which must be
54 // non-NULL.
55 void Search(const DownloadItems& all_items, DownloadItems* results) const;
56
57 // Returns true if |item| matches |this| settings, false otherwise.
58 bool Matches(const DownloadItem& item) const;
59
60 // Ignore these mostly-private interfaces.
61 class SortFieldInterface;
62 class FilterFieldInterface;
63 typedef base::hash_map<std::string, SortFieldInterface*> SortFields;
64 typedef base::hash_map<std::string, FilterFieldInterface*> FilterFields;
65 typedef std::vector<std::string> Strings;
66
67 private:
68 void SetSortField(const char* name, SortFieldInterface* field);
69 void SetFilterField(const char* name, FilterFieldInterface* field);
70
71 SortFields sort_fields_;
72 FilterFields filter_fields_;
73 Strings order_by_fields_;
74 uint32 limit_;
75 STLValueDeleter<SortFields> delete_sort_fields_;
76 STLValueDeleter<FilterFields> delete_filter_fields_;
77
78 DISALLOW_COPY_AND_ASSIGN(DownloadQuery);
79 };
80 } // namespace download_util
81
82
83 // The following macros facilitate using anonymous expressions (lambdas) to
84 // implement arbitrary filters for DownloadQuery. The parameters passed to the
85 // second and third forms MUST be variable names and cannot be expressions or
86 // even lvalues.
Randy Smith (Not in Mondays) 2011/10/13 23:23:11 What's the reason for this restriction? Is there
benjhayden 2011/10/14 19:31:02 The reason is that the macro implementation uses t
Randy Smith (Not in Mondays) 2011/10/18 23:58:46 Hmmm. All reasonable options, but I find myself w
87 //
88 // query.Set("filter", DOWNLOAD_QUERY_FILTER2(otr, foo,
89 // item.otr() == otr && item.foo() == foo));
90
91 #define SYMCAT(X, Y) X##Y
92 #define DOWNLOAD_QUERY_FILTER_IMPL(NAME, expr) \
93 ({struct NAME {static bool Filter(const DownloadItem& item) {return expr;}}; \
94 base::Bind(&NAME::Filter);})
95 #define DOWNLOAD_QUERY_FILTER1_IMPL(NAME, var0, expr) \
96 ({template<typename Var0Type>struct NAME {static bool Filter( \
97 Var0Type var0, const DownloadItem& item) {return expr;}}; \
98 base::Bind(&NAME::Filter, var0);})
99 #define DOWNLOAD_QUERY_FILTER2_IMPL(NAME, var0, var1, expr) \
100 ({template<typename Var0Type, Var1Type>struct NAME {static bool Filter( \
101 Var0Type var0, Var1Type var1, const DownloadItem& item) {return expr;}}; \
102 base::Bind(&NAME::Filter, var0, var1);})
103 #define DOWNLOAD_QUERY_FILTER(expr) DOWNLOAD_QUERY_FILTER_IMPL( \
104 SYMCAT(DownloadQueryFilter, __LINE__), (expr))
105 #define DOWNLOAD_QUERY_FILTER1(var0, expr) DOWNLOAD_QUERY_FILTER1_IMPL( \
106 SYMCAT(DownloadQueryFilter, __LINE__), var0, (expr))
107 #define DOWNLOAD_QUERY_FILTER2(var0, var1, expr) DOWNLOAD_QUERY_FILTER2_IMPL( \
108 SYMCAT(DownloadQueryFilter, __LINE__), var0, var1, (expr))
109
110 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698