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 #include "content/browser/download/download_query.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/callback.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/stl_util.h" | |
| 17 #include "base/string16.h" | |
| 18 #include "base/string_split.h" | |
| 19 #include "base/time.h" | |
| 20 #include "base/utf_string_conversions.h" | |
| 21 #include "content/browser/download/download_item.h" | |
| 22 #include "googleurl/src/gurl.h" | |
| 23 #include "unicode/regex.h" | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // The next several functions are helpers for making Callbacks that access | |
| 28 // DownloadItem fields. | |
| 29 | |
| 30 static bool MatchesQuery(const string16& value, const DownloadItem& item) { | |
| 31 return item.MatchesQuery(value); | |
| 32 } | |
| 33 | |
| 34 static int GetStartTime(const DownloadItem& item) { | |
| 35 return (item.GetStartTime() - base::Time::UnixEpoch()).InMilliseconds(); | |
| 36 } | |
| 37 | |
| 38 static bool GetDangerAccepted(const DownloadItem& item) { | |
| 39 return (item.GetSafetyState() == DownloadItem::DANGEROUS_BUT_VALIDATED); | |
| 40 } | |
| 41 | |
| 42 static string16 GetFilename(const DownloadItem& item) { | |
| 43 // This filename will be compared with strings that could be passed in by the | |
| 44 // user, who only sees LossyDisplayNames. | |
| 45 return item.GetFullPath().LossyDisplayName(); | |
| 46 } | |
| 47 | |
| 48 static std::string GetFilenameUTF8(const DownloadItem& item) { | |
| 49 return UTF16ToUTF8(GetFilename(item)); | |
| 50 } | |
| 51 | |
| 52 static std::string GetUrl(const DownloadItem& item) { | |
| 53 return item.GetOriginalUrl().spec(); | |
| 54 } | |
| 55 | |
| 56 static DownloadItem::DownloadState GetState(const DownloadItem& item) { | |
| 57 return item.GetState(); | |
| 58 } | |
| 59 | |
| 60 static DownloadStateInfo::DangerType GetDangerType(const DownloadItem& item) { | |
| 61 return item.GetDangerType(); | |
| 62 } | |
| 63 | |
| 64 static int GetReceivedBytes(const DownloadItem& item) { | |
| 65 return item.GetReceivedBytes(); | |
| 66 } | |
| 67 | |
| 68 static int GetTotalBytes(const DownloadItem& item) { | |
| 69 return item.GetTotalBytes(); | |
| 70 } | |
| 71 | |
| 72 static std::string GetMimeType(const DownloadItem& item) { | |
| 73 return item.GetMimeType(); | |
| 74 } | |
| 75 | |
| 76 static bool IsPaused(const DownloadItem& item) { | |
| 77 return item.IsPaused(); | |
| 78 } | |
| 79 | |
| 80 // Wrap Callback to work around a bug in base::Bind/Callback where the inner | |
| 81 // callback is nullified when the outer callback is Run. | |
| 82 template<typename ValueType> | |
| 83 class InnerCallback { | |
| 84 public: | |
| 85 typedef base::Callback<ValueType(const DownloadItem&)> CallbackType; | |
| 86 | |
| 87 explicit InnerCallback(const CallbackType& inner) : inner_(inner) {} | |
| 88 ~InnerCallback() {} | |
| 89 | |
| 90 // Mimic Callback's interface to facilitate removing InnerCallback when the | |
| 91 // bug is fixed. | |
| 92 ValueType Run(const DownloadItem& item) const { return inner_.Run(item); } | |
| 93 | |
| 94 private: | |
| 95 CallbackType inner_; | |
| 96 }; | |
| 97 | |
| 98 enum ComparisonType {LT, EQ, GT}; | |
| 99 | |
| 100 // Returns true if |item| matches the filter specified by |value|, |cmptype|, | |
| 101 // and |accessor|. |accessor| is conceptually a function that takes a | |
| 102 // DownloadItem and returns one of its fields, which is then compared to | |
| 103 // |value|. | |
| 104 template<typename ValueType> | |
| 105 static bool FieldMatches( | |
| 106 const ValueType& value, | |
| 107 ComparisonType cmptype, | |
| 108 const InnerCallback<ValueType>& accessor, | |
| 109 const DownloadItem& item) { | |
| 110 switch (cmptype) { | |
| 111 case LT: return accessor.Run(item) < value; | |
| 112 case EQ: return accessor.Run(item) == value; | |
| 113 case GT: return accessor.Run(item) > value; | |
| 114 } | |
| 115 NOTREACHED(); | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 // Returns true if |accessor.Run(item)| matches |pattern|. | |
| 120 static bool FindRegex( | |
| 121 icu::RegexPattern* pattern, | |
| 122 const InnerCallback<std::string>& accessor, | |
| 123 const DownloadItem& item) { | |
| 124 icu::UnicodeString input(accessor.Run(item).c_str()); | |
| 125 UErrorCode status = U_ZERO_ERROR; | |
| 126 scoped_ptr<icu::RegexMatcher> matcher(pattern->matcher(input, status)); | |
| 127 return matcher->find(); | |
| 128 } | |
| 129 | |
| 130 // Returns a ComparisonType to indicate whether a field in |left| is less than, | |
| 131 // greater than or equal to the same field in |right|. | |
| 132 template<typename ValueType> | |
| 133 static ComparisonType Compare( | |
| 134 const InnerCallback<ValueType>& accessor, | |
| 135 const DownloadItem& left, const DownloadItem& right) { | |
| 136 ValueType left_value = accessor.Run(left); | |
| 137 ValueType right_value = accessor.Run(right); | |
| 138 if (left_value > right_value) return GT; | |
|
cbentzel
2011/12/05 21:56:26
Do you need to return GT/LT/EQ? Could you just ret
benjhayden
2011/12/09 19:29:15
Yes to the first, no to the second. :-)
Compare()
| |
| 139 if (left_value < right_value) return LT; | |
| 140 DCHECK_EQ(left_value, right_value); | |
| 141 return EQ; | |
| 142 } | |
| 143 | |
| 144 } // anonymous namespace | |
| 145 | |
| 146 DownloadQuery::DownloadQuery() | |
| 147 : limit_(kuint32max) { | |
| 148 } | |
| 149 | |
| 150 DownloadQuery::~DownloadQuery() { | |
| 151 } | |
| 152 | |
| 153 // Filter() pushes a new FilterType to filter_fields_. Most FilterTypes are | |
| 154 // Callbacks to FieldMatches<>(). Search() iterates over given DownloadItems, | |
| 155 // discarding items for which any filter returns false. A DownloadQuery may have | |
| 156 // zero or more FilterTypes. | |
| 157 | |
| 158 void DownloadQuery::Filter(const DownloadQuery::FilterType& value) { | |
| 159 filter_fields_.push_back(value); | |
| 160 } | |
| 161 | |
| 162 void DownloadQuery::Filter(DownloadItem::DownloadState state) { | |
| 163 filter_fields_.push_back(base::Bind( | |
| 164 &FieldMatches<DownloadItem::DownloadState>, | |
| 165 state, | |
| 166 EQ, | |
| 167 InnerCallback<DownloadItem::DownloadState>(base::Bind(&GetState)))); | |
| 168 } | |
| 169 | |
| 170 void DownloadQuery::Filter(DownloadStateInfo::DangerType danger) { | |
| 171 filter_fields_.push_back(base::Bind( | |
| 172 &FieldMatches<DownloadStateInfo::DangerType>, | |
| 173 danger, | |
| 174 EQ, | |
| 175 InnerCallback<DownloadStateInfo::DangerType>(base::Bind( | |
| 176 &GetDangerType)))); | |
| 177 } | |
| 178 | |
| 179 bool DownloadQuery::Filter(DownloadQuery::FilterFieldName name, int value) { | |
| 180 switch (static_cast<int>(name)) { | |
| 181 case FILTER_FIELD_START_TIME: | |
| 182 Filter(base::Bind(&FieldMatches<int>, value, EQ, | |
| 183 InnerCallback<int>(base::Bind(&GetStartTime)))); | |
| 184 return true; | |
| 185 case FILTER_FIELD_STARTED_BEFORE: | |
| 186 Filter(base::Bind(&FieldMatches<int>, value, LT, | |
| 187 InnerCallback<int>(base::Bind(&GetStartTime)))); | |
| 188 return true; | |
| 189 case FILTER_FIELD_STARTED_AFTER: | |
| 190 Filter(base::Bind(&FieldMatches<int>, value, GT, | |
| 191 InnerCallback<int>(base::Bind(&GetStartTime)))); | |
| 192 return true; | |
| 193 case FILTER_FIELD_BYTES_RECEIVED: | |
| 194 Filter(base::Bind(&FieldMatches<int>, value, EQ, | |
| 195 InnerCallback<int>(base::Bind(&GetReceivedBytes)))); | |
| 196 return true; | |
| 197 case FILTER_FIELD_TOTAL_BYTES: | |
| 198 Filter(base::Bind(&FieldMatches<int>, value, EQ, | |
| 199 InnerCallback<int>(base::Bind(&GetTotalBytes)))); | |
| 200 return true; | |
| 201 case FILTER_FIELD_TOTAL_BYTES_GREATER: | |
|
cbentzel
2011/12/05 21:56:26
Should bytes filter fields be size_t or check >= 0
benjhayden
2011/12/09 19:29:15
If we're allowing users to specify conflicting fil
| |
| 202 Filter(base::Bind(&FieldMatches<int>, value, GT, | |
| 203 InnerCallback<int>(base::Bind(&GetTotalBytes)))); | |
| 204 return true; | |
| 205 case FILTER_FIELD_TOTAL_BYTES_LESS: | |
| 206 Filter(base::Bind(&FieldMatches<int>, value, LT, | |
| 207 InnerCallback<int>(base::Bind(&GetTotalBytes)))); | |
| 208 return true; | |
| 209 } | |
| 210 return false; | |
| 211 } | |
| 212 | |
| 213 bool DownloadQuery::Filter(DownloadQuery::FilterFieldName name, bool value) { | |
| 214 switch (static_cast<int>(name)) { | |
| 215 case FILTER_FIELD_DANGER_ACCEPTED: | |
| 216 Filter(base::Bind(&FieldMatches<bool>, value, EQ, | |
| 217 InnerCallback<bool>(base::Bind(&GetDangerAccepted)))); | |
| 218 return true; | |
| 219 case FILTER_FIELD_PAUSED: | |
| 220 Filter(base::Bind(&FieldMatches<bool>, value, EQ, | |
| 221 InnerCallback<bool>(base::Bind(&IsPaused)))); | |
| 222 return true; | |
| 223 } | |
| 224 return false; | |
| 225 } | |
| 226 | |
| 227 bool DownloadQuery::FilterRegex( | |
| 228 const std::string& regex_str, | |
| 229 const InnerCallback<std::string>::CallbackType& accessor) { | |
| 230 UParseError re_err; | |
| 231 UErrorCode re_status = U_ZERO_ERROR; | |
| 232 scoped_ptr<icu::RegexPattern> pattern(icu::RegexPattern::compile( | |
| 233 icu::UnicodeString::fromUTF8(regex_str), re_err, re_status)); | |
| 234 if (!U_SUCCESS(re_status)) return false; | |
| 235 Filter(base::Bind(&FindRegex, base::Owned(pattern.release()), | |
| 236 InnerCallback<std::string>(accessor))); | |
| 237 return true; | |
| 238 } | |
| 239 | |
| 240 bool DownloadQuery::Filter( | |
| 241 DownloadQuery::FilterFieldName name, const std::string& value) { | |
| 242 switch (static_cast<int>(name)) { | |
| 243 case FILTER_FIELD_MIME: | |
| 244 Filter(base::Bind(&FieldMatches<std::string>, value, EQ, | |
| 245 InnerCallback<std::string>(base::Bind(&GetMimeType)))); | |
| 246 return true; | |
| 247 case FILTER_FIELD_URL: | |
| 248 Filter(base::Bind(&FieldMatches<std::string>, value, EQ, | |
| 249 InnerCallback<std::string>(base::Bind(&GetUrl)))); | |
| 250 return true; | |
| 251 case FILTER_FIELD_URL_REGEX: | |
| 252 return FilterRegex(value, base::Bind(&GetUrl)); | |
| 253 } | |
| 254 return false; | |
| 255 } | |
| 256 | |
| 257 bool DownloadQuery::Filter( | |
| 258 DownloadQuery::FilterFieldName name, const string16& value) { | |
| 259 switch (static_cast<int>(name)) { | |
| 260 case FILTER_FIELD_QUERY: | |
| 261 Filter(base::Bind(&MatchesQuery, value)); | |
| 262 return true; | |
| 263 case FILTER_FIELD_FILENAME: | |
| 264 Filter(base::Bind(&FieldMatches<string16>, value, EQ, | |
| 265 InnerCallback<string16>(base::Bind(&GetFilename)))); | |
| 266 return true; | |
| 267 case FILTER_FIELD_FILENAME_REGEX: | |
| 268 return FilterRegex(UTF16ToUTF8(value), base::Bind(&GetFilenameUTF8)); | |
| 269 } | |
| 270 return false; | |
| 271 } | |
| 272 | |
| 273 bool DownloadQuery::Matches(const DownloadItem& item) const { | |
| 274 for (FilterFieldVector::const_iterator filter = filter_fields_.begin(); | |
| 275 filter != filter_fields_.end(); ++filter) { | |
| 276 if (!filter->Run(item)) | |
| 277 return false; | |
| 278 } | |
| 279 return true; | |
| 280 } | |
| 281 | |
| 282 // Sort() creates a SortField and pushes it onto sort_fields_. A SortField is a | |
| 283 // direction and a Callback to Compare<>(). After filtering, Search() makes a | |
| 284 // DownloadComparator functor from the sort_fields_ and passes the | |
| 285 // DownloadComparator to std::partial_sort. std::partial_sort calls the | |
| 286 // DownloadComparator with different pairs of DownloadItems. DownloadComparator | |
| 287 // iterates over the sort fields until a callback returns ComparisonType LT or | |
| 288 // GT. DownloadComparator returns true or false depending on that | |
| 289 // ComparisonType and the sort field's direction in order to indicate to | |
| 290 // std::partial_sort whether the left item is after or before the right item. If | |
| 291 // all sort fields return EQ, then DownloadComparator compares GetId or | |
| 292 // GetDbHandle. A DownloadQuery may have zero or more SortFields, but there is | |
| 293 // one DownloadComparator per call to Search(). | |
| 294 | |
| 295 struct DownloadQuery::SortField { | |
| 296 typedef base::Callback<ComparisonType( | |
| 297 const DownloadItem&, const DownloadItem&)> SortType; | |
| 298 | |
| 299 SortField(DownloadQuery::SortFieldDirection adirection, | |
| 300 const SortType& asorter) | |
| 301 : direction(adirection), | |
| 302 sorter(asorter) { | |
| 303 } | |
| 304 ~SortField() {} | |
| 305 | |
| 306 DownloadQuery::SortFieldDirection direction; | |
| 307 SortType sorter; | |
| 308 }; | |
| 309 | |
| 310 class DownloadQuery::DownloadComparator { | |
| 311 public: | |
| 312 DownloadComparator(const DownloadQuery::SortFieldVector& terms) | |
| 313 : terms_(terms) { | |
| 314 } | |
| 315 | |
| 316 // Returns true if |left| sorts before |right|. | |
| 317 bool operator() (const DownloadItem* left, const DownloadItem* right); | |
| 318 | |
| 319 private: | |
| 320 const DownloadQuery::SortFieldVector& terms_; | |
| 321 | |
| 322 // std::sort requires this class to be copyable. | |
| 323 }; | |
| 324 | |
| 325 bool DownloadQuery::DownloadComparator::operator() ( | |
| 326 const DownloadItem* left, const DownloadItem* right) { | |
| 327 for (DownloadQuery::SortFieldVector::const_iterator term = terms_.begin(); | |
| 328 term != terms_.end(); ++term) { | |
| 329 switch (term->sorter.Run(*left, *right)) { | |
| 330 case LT: return term->direction == DownloadQuery::ASCENDING; | |
| 331 case GT: return term->direction == DownloadQuery::DESCENDING; | |
| 332 case EQ: break; // break the switch but not the loop | |
| 333 } | |
| 334 } | |
| 335 if (left->GetId() != right->GetId()) | |
| 336 return left->GetId() < right->GetId(); | |
| 337 CHECK_NE(left->GetDbHandle(), right->GetDbHandle()); | |
| 338 return left->GetDbHandle() < right->GetDbHandle(); | |
| 339 } | |
| 340 | |
| 341 void DownloadQuery::Sort(DownloadQuery::SortFieldName name, | |
| 342 DownloadQuery::SortFieldDirection direction) { | |
| 343 switch (name) { | |
| 344 case SORT_FIELD_START_TIME: | |
| 345 sort_fields_.push_back(SortField(direction, base::Bind( | |
| 346 &Compare<int>, InnerCallback<int>(base::Bind(&GetStartTime))))); | |
| 347 break; | |
| 348 case SORT_FIELD_URL: | |
| 349 sort_fields_.push_back(SortField(direction, base::Bind( | |
| 350 &Compare<std::string>, | |
| 351 InnerCallback<std::string>(base::Bind(&GetUrl))))); | |
| 352 break; | |
| 353 case SORT_FIELD_FILENAME: | |
| 354 sort_fields_.push_back(SortField(direction, base::Bind( | |
| 355 &Compare<string16>, | |
| 356 InnerCallback<string16>(base::Bind(&GetFilename))))); | |
| 357 break; | |
| 358 case SORT_FIELD_DANGER: | |
| 359 sort_fields_.push_back(SortField(direction, base::Bind( | |
| 360 &Compare<DownloadStateInfo::DangerType>, | |
| 361 InnerCallback<DownloadStateInfo::DangerType>(base::Bind( | |
| 362 &GetDangerType))))); | |
| 363 break; | |
| 364 case SORT_FIELD_DANGER_ACCEPTED: | |
| 365 sort_fields_.push_back(SortField(direction, base::Bind( | |
| 366 &Compare<bool>, | |
| 367 InnerCallback<bool>(base::Bind(&GetDangerAccepted))))); | |
| 368 break; | |
| 369 case SORT_FIELD_STATE: | |
| 370 sort_fields_.push_back(SortField(direction, base::Bind( | |
| 371 &Compare<DownloadItem::DownloadState>, | |
| 372 InnerCallback<DownloadItem::DownloadState>(base::Bind(&GetState))))); | |
| 373 break; | |
| 374 case SORT_FIELD_PAUSED: | |
| 375 sort_fields_.push_back(SortField(direction, base::Bind( | |
| 376 &Compare<bool>, InnerCallback<bool>(base::Bind(&IsPaused))))); | |
| 377 break; | |
| 378 case SORT_FIELD_MIME: | |
| 379 sort_fields_.push_back(SortField(direction, base::Bind( | |
| 380 &Compare<std::string>, | |
| 381 InnerCallback<std::string>(base::Bind(&GetMimeType))))); | |
| 382 break; | |
| 383 case SORT_FIELD_BYTES_RECEIVED: | |
| 384 sort_fields_.push_back(SortField(direction, base::Bind( | |
| 385 &Compare<int>, InnerCallback<int>(base::Bind(&GetReceivedBytes))))); | |
| 386 break; | |
| 387 case SORT_FIELD_TOTAL_BYTES: | |
| 388 sort_fields_.push_back(SortField(direction, base::Bind( | |
| 389 &Compare<int>, InnerCallback<int>(base::Bind(&GetTotalBytes))))); | |
| 390 break; | |
| 391 default: NOTREACHED(); | |
| 392 } | |
| 393 } | |
| 394 | |
| 395 template <typename InputIterator> | |
| 396 void DownloadQuery::Search(InputIterator iter, const InputIterator last, | |
| 397 DownloadQuery::DownloadVector* results) const { | |
| 398 results->clear(); | |
| 399 for (; iter != last; ++iter) { | |
| 400 if (Matches(**iter)) | |
| 401 results->push_back(*iter); | |
| 402 } | |
| 403 if (!sort_fields_.empty()) | |
| 404 std::partial_sort(results->begin(), | |
| 405 results->begin() + std::min(limit_, results->size()), | |
| 406 results->end(), | |
| 407 DownloadComparator(sort_fields_)); | |
| 408 if (results->size() > limit_) | |
| 409 results->resize(limit_); | |
| 410 } | |
| 411 | |
| 412 template void DownloadQuery::Search( | |
| 413 std::set<DownloadItem*>::const_iterator iter, | |
| 414 const std::set<DownloadItem*>::const_iterator last, | |
| 415 DownloadQuery::DownloadVector* results) const; | |
| 416 template void DownloadQuery::Search( | |
| 417 std::vector<DownloadItem*>::const_iterator iter, | |
| 418 const std::vector<DownloadItem*>::const_iterator last, | |
| 419 DownloadQuery::DownloadVector* results) const; | |
| 420 template void DownloadQuery::Search( | |
| 421 std::vector<DownloadItem*>::iterator iter, | |
| 422 const std::vector<DownloadItem*>::iterator last, | |
| 423 DownloadQuery::DownloadVector* results) const; | |
| 424 | |
| OLD | NEW |