Chromium Code Reviews| 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 <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/values.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 // std::vector<DownloadItem*> results; | |
| 22 // std::string error_msg; | |
| 23 // scoped_ptr<base::ListValue> json_results(new base::ListValue()); | |
| 24 // bool FilterOutOddDownloads(DownloadItem* item) { | |
| 25 // CHECK(item); | |
| 26 // return 0 == (item->id() % 2); | |
| 27 // } | |
| 28 // download_manager->Search(DownloadQuery(). | |
| 29 // filter_func(base::Bind(&FilterOutOddDownloads)). | |
| 30 // filenameRegex(".*foo.*"). | |
| 31 // urlRegex(".*foo.*"), | |
| 32 // &results, &error_msg, json_results.get()); | |
| 33 class DownloadQuery { | |
| 34 public: | |
| 35 typedef base::Callback<bool(const DownloadItem&)> FilterType; | |
| 36 | |
| 37 DownloadQuery(); | |
| 38 | |
| 39 ~DownloadQuery() {} | |
| 40 | |
| 41 // Modify items in-place to sort, filter, and limit. | |
| 42 // If results is non-NULL, convert the results to json in it. | |
| 43 // Return true iff the query is well-formed. Return false and set error_msg if | |
| 44 // query is malformed. | |
| 45 bool Search(std::vector<DownloadItem*>* items, | |
|
cbentzel
2011/10/10 14:35:30
The way this is used - it seems like it would make
benjhayden
2011/10/13 14:07:34
Done.
| |
| 46 std::string* error_msg = NULL, | |
| 47 base::ListValue* results = NULL) const; | |
| 48 | |
| 49 // Chainable settors set a field to a value and return |this|. | |
| 50 DownloadQuery& filter_func(FilterType func); | |
|
cbentzel
2011/10/10 14:35:30
I haven't really seen this style in Chromium (the
benjhayden
2011/10/13 14:07:34
Done.
| |
| 51 DownloadQuery& state_enum(DownloadItem::DownloadState dstate); | |
| 52 DownloadQuery& danger_enum(DownloadItem::DangerType danger_type); | |
| 53 #define Boolean bool | |
| 54 #define Integer int | |
| 55 #define String std::string | |
| 56 #define FIELD(name, type) \ | |
| 57 protected: bool has_ ## name ## _; \ | |
| 58 protected: type name ## _; \ | |
| 59 public: DownloadQuery& name(type value) { \ | |
| 60 name ## _ = value; \ | |
| 61 has_ ## name ## _ = true; \ | |
| 62 return *this; \ | |
| 63 } | |
| 64 #include "content/browser/download/download_query_fields.h" | |
| 65 #undef FIELD | |
| 66 #undef Boolean | |
| 67 #undef Integer | |
| 68 #undef String | |
| 69 | |
| 70 protected: | |
| 71 FilterType filter_func_; | |
| 72 bool has_state_enum_; | |
| 73 DownloadItem::DownloadState state_enum_; | |
| 74 bool has_danger_enum_; | |
| 75 DownloadItem::DangerType danger_enum_; | |
| 76 // Allow copy and assign. | |
| 77 }; | |
| 78 } // namespace download_util | |
| 79 | |
| 80 | |
| 81 // The following macros facilitate using anonymous expressions (lambdas) to | |
| 82 // create a DownloadQuery and set its filter_func. The parameters passed to the | |
| 83 // second and third forms MUST be variable names and cannot be expressions. | |
| 84 // | |
| 85 // dlman->Search(DOWNLOAD_QUERY_FILTER(item.is_temporary()). | |
| 86 // orderBy("-endTime"), results); | |
| 87 // download_manager->Search(DOWNLOAD_QUERY_FILTER1(otr, | |
| 88 // item.is_otr() == otr).orderBy("-endTime"), results); | |
| 89 // download_manager->Search(DOWNLOAD_QUERY_FILTER2(otr, temp, | |
| 90 // ((item.is_otr() == otr) && (item.is_temporary() == temp))). | |
| 91 // orderBy("-endTime"), results); | |
| 92 | |
| 93 #define SYMCAT(X, Y) X##Y | |
| 94 #define DOWNLOAD_QUERY_FILTER_IMPL(NAME, expr) \ | |
| 95 ({struct NAME {static bool Filter(const DownloadItem& item) {return expr;}}; \ | |
| 96 download_util::DownloadQuery().filter_func(base::Bind(&NAME::Filter));}) | |
| 97 #define DOWNLOAD_QUERY_FILTER(expr) DOWNLOAD_QUERY_FILTER_IMPL( \ | |
| 98 SYMCAT(DownloadQuery, __LINE__), (expr)) | |
| 99 #define DOWNLOAD_QUERY_FILTER1_IMPL(NAME, var0, expr) \ | |
| 100 ({struct NAME {static bool Filter( \ | |
| 101 typeof(var0) var0, const DownloadItem& item) {return expr;}}; \ | |
| 102 download_util::DownloadQuery().filter_func( \ | |
| 103 base::Bind(&NAME::Filter, var0));}) | |
| 104 #define DOWNLOAD_QUERY_FILTER1(var0, expr) DOWNLOAD_QUERY_FILTER1_IMPL( \ | |
| 105 SYMCAT(DownloadQuery, __LINE__), var0, (expr)) | |
| 106 #define DOWNLOAD_QUERY_FILTER2_IMPL(NAME, var0, var1, expr) \ | |
| 107 ({struct NAME {static bool Filter(typeof(var0) var0, typeof(var1) var1, \ | |
| 108 const DownloadItem& item) {return expr;}}; \ | |
| 109 download_util::DownloadQuery().filter_func( \ | |
| 110 base::Bind(&NAME::Filter, var0, var1));}) | |
| 111 #define DOWNLOAD_QUERY_FILTER2(var0, var1, expr) DOWNLOAD_QUERY_FILTER2_IMPL( \ | |
| 112 SYMCAT(DownloadQuery, __LINE__), var0, var1, (expr)) | |
| 113 | |
| 114 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_ | |
| OLD | NEW |