Chromium Code Reviews| Index: content/browser/download/download_query.h |
| diff --git a/content/browser/download/download_query.h b/content/browser/download/download_query.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2174ab4589ba39302e32f21aec0b7225c4d03f0c |
| --- /dev/null |
| +++ b/content/browser/download/download_query.h |
| @@ -0,0 +1,114 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_ |
| +#define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_ |
| +#pragma once |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/callback.h" |
| +#include "base/values.h" |
| +#include "content/browser/download/download_item.h" |
| + |
| +namespace download_util { |
| + |
| +// Filter and sort a vector of DownloadItem*s. See also the macros at the bottom |
| +// of this file. |
| +// |
| +// std::vector<DownloadItem*> results; |
| +// std::string error_msg; |
| +// scoped_ptr<base::ListValue> json_results(new base::ListValue()); |
| +// bool FilterOutOddDownloads(DownloadItem* item) { |
| +// CHECK(item); |
| +// return 0 == (item->id() % 2); |
| +// } |
| +// download_manager->Search(DownloadQuery(). |
| +// filter_func(base::Bind(&FilterOutOddDownloads)). |
| +// filenameRegex(".*foo.*"). |
| +// urlRegex(".*foo.*"), |
| +// &results, &error_msg, json_results.get()); |
| +class DownloadQuery { |
| + public: |
| + typedef base::Callback<bool(const DownloadItem&)> FilterType; |
| + |
| + DownloadQuery(); |
| + |
| + ~DownloadQuery() {} |
| + |
| + // Modify items in-place to sort, filter, and limit. |
| + // If results is non-NULL, convert the results to json in it. |
| + // Return true iff the query is well-formed. Return false and set error_msg if |
| + // query is malformed. |
| + 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.
|
| + std::string* error_msg = NULL, |
| + base::ListValue* results = NULL) const; |
| + |
| + // Chainable settors set a field to a value and return |this|. |
| + 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.
|
| + DownloadQuery& state_enum(DownloadItem::DownloadState dstate); |
| + DownloadQuery& danger_enum(DownloadItem::DangerType danger_type); |
| +#define Boolean bool |
| +#define Integer int |
| +#define String std::string |
| +#define FIELD(name, type) \ |
| + protected: bool has_ ## name ## _; \ |
| + protected: type name ## _; \ |
| + public: DownloadQuery& name(type value) { \ |
| + name ## _ = value; \ |
| + has_ ## name ## _ = true; \ |
| + return *this; \ |
| + } |
| +#include "content/browser/download/download_query_fields.h" |
| +#undef FIELD |
| +#undef Boolean |
| +#undef Integer |
| +#undef String |
| + |
| + protected: |
| + FilterType filter_func_; |
| + bool has_state_enum_; |
| + DownloadItem::DownloadState state_enum_; |
| + bool has_danger_enum_; |
| + DownloadItem::DangerType danger_enum_; |
| + // Allow copy and assign. |
| +}; |
| +} // namespace download_util |
| + |
| + |
| +// The following macros facilitate using anonymous expressions (lambdas) to |
| +// create a DownloadQuery and set its filter_func. The parameters passed to the |
| +// second and third forms MUST be variable names and cannot be expressions. |
| +// |
| +// dlman->Search(DOWNLOAD_QUERY_FILTER(item.is_temporary()). |
| +// orderBy("-endTime"), results); |
| +// download_manager->Search(DOWNLOAD_QUERY_FILTER1(otr, |
| +// item.is_otr() == otr).orderBy("-endTime"), results); |
| +// download_manager->Search(DOWNLOAD_QUERY_FILTER2(otr, temp, |
| +// ((item.is_otr() == otr) && (item.is_temporary() == temp))). |
| +// orderBy("-endTime"), results); |
| + |
| +#define SYMCAT(X, Y) X##Y |
| +#define DOWNLOAD_QUERY_FILTER_IMPL(NAME, expr) \ |
| + ({struct NAME {static bool Filter(const DownloadItem& item) {return expr;}}; \ |
| + download_util::DownloadQuery().filter_func(base::Bind(&NAME::Filter));}) |
| +#define DOWNLOAD_QUERY_FILTER(expr) DOWNLOAD_QUERY_FILTER_IMPL( \ |
| + SYMCAT(DownloadQuery, __LINE__), (expr)) |
| +#define DOWNLOAD_QUERY_FILTER1_IMPL(NAME, var0, expr) \ |
| + ({struct NAME {static bool Filter( \ |
| + typeof(var0) var0, const DownloadItem& item) {return expr;}}; \ |
| + download_util::DownloadQuery().filter_func( \ |
| + base::Bind(&NAME::Filter, var0));}) |
| +#define DOWNLOAD_QUERY_FILTER1(var0, expr) DOWNLOAD_QUERY_FILTER1_IMPL( \ |
| + SYMCAT(DownloadQuery, __LINE__), var0, (expr)) |
| +#define DOWNLOAD_QUERY_FILTER2_IMPL(NAME, var0, var1, expr) \ |
| + ({struct NAME {static bool Filter(typeof(var0) var0, typeof(var1) var1, \ |
| + const DownloadItem& item) {return expr;}}; \ |
| + download_util::DownloadQuery().filter_func( \ |
| + base::Bind(&NAME::Filter, var0, var1));}) |
| +#define DOWNLOAD_QUERY_FILTER2(var0, var1, expr) DOWNLOAD_QUERY_FILTER2_IMPL( \ |
| + SYMCAT(DownloadQuery, __LINE__), var0, var1, (expr)) |
| + |
| +#endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_ |