Index: chrome/browser/download/download_query.h |
diff --git a/chrome/browser/download/download_query.h b/chrome/browser/download/download_query.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bf19d2e0dbd1ad24aae7ed4318f4ca0e94ab8502 |
--- /dev/null |
+++ b/chrome/browser/download/download_query.h |
@@ -0,0 +1,116 @@ |
+// 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 CHROME_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_ |
+#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_ |
+#pragma once |
+ |
+#include <string> |
+#include <vector> |
+ |
+#include "base/callback.h" |
+#include "chrome/browser/download/download_item.h" |
+ |
+class DictionaryValue; |
+class ListValue; |
+ |
+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<ListValue> json_results(new 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(); |
+ |
+ explicit DownloadQuery(const DictionaryValue& json); |
+ |
+ ~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, |
+ std::string* error_msg = NULL, |
+ ListValue* results = NULL) const; |
+ |
+ // Chainable settors set a field to a value and return |this|. |
+ DownloadQuery& filter_func(FilterType func); |
+ 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 "chrome/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 // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_QUERY_H_ |