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