OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/download/download_query.h" | 5 #include "chrome/browser/download/download_query.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 14 #include "base/i18n/case_conversion.h" |
| 15 #include "base/i18n/string_search.h" |
14 #include "base/logging.h" | 16 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
16 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
17 #include "base/string16.h" | 19 #include "base/string16.h" |
18 #include "base/string_split.h" | 20 #include "base/string_split.h" |
19 #include "base/time.h" | 21 #include "base/time.h" |
20 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
21 #include "base/values.h" | 23 #include "base/values.h" |
| 24 #include "content/public/browser/content_browser_client.h" |
22 #include "content/public/browser/download_item.h" | 25 #include "content/public/browser/download_item.h" |
23 #include "googleurl/src/gurl.h" | 26 #include "googleurl/src/gurl.h" |
| 27 #include "net/base/net_util.h" |
24 #include "unicode/regex.h" | 28 #include "unicode/regex.h" |
25 | 29 |
26 using content::DownloadDangerType; | 30 using content::DownloadDangerType; |
27 using content::DownloadItem; | 31 using content::DownloadItem; |
28 | 32 |
29 namespace { | 33 namespace { |
30 | 34 |
31 // Templatized base::Value::GetAs*(). | 35 // Templatized base::Value::GetAs*(). |
32 template <typename T> bool GetAs(const base::Value& in, T* out); | 36 template <typename T> bool GetAs(const base::Value& in, T* out); |
33 template<> bool GetAs(const base::Value& in, bool* out) { | 37 template<> bool GetAs(const base::Value& in, bool* out) { |
34 return in.GetAsBoolean(out); | 38 return in.GetAsBoolean(out); |
35 } | 39 } |
36 template<> bool GetAs(const base::Value& in, int* out) { | 40 template<> bool GetAs(const base::Value& in, int* out) { |
37 return in.GetAsInteger(out); | 41 return in.GetAsInteger(out); |
38 } | 42 } |
39 template<> bool GetAs(const base::Value& in, std::string* out) { | 43 template<> bool GetAs(const base::Value& in, std::string* out) { |
40 return in.GetAsString(out); | 44 return in.GetAsString(out); |
41 } | 45 } |
42 template<> bool GetAs(const base::Value& in, string16* out) { | 46 template<> bool GetAs(const base::Value& in, string16* out) { |
43 return in.GetAsString(out); | 47 return in.GetAsString(out); |
44 } | 48 } |
45 | 49 |
46 // The next several functions are helpers for making Callbacks that access | 50 // The next several functions are helpers for making Callbacks that access |
47 // DownloadItem fields. | 51 // DownloadItem fields. |
48 | 52 |
49 static bool MatchesQuery(const string16& value, const DownloadItem& item) { | 53 static bool MatchesQuery(const string16& query, const DownloadItem& item) { |
50 return item.MatchesQuery(value); | 54 if (query.empty()) |
| 55 return true; |
| 56 |
| 57 DCHECK_EQ(query, base::i18n::ToLower(query)); |
| 58 |
| 59 string16 url_raw(UTF8ToUTF16(item.GetOriginalUrl().spec())); |
| 60 if (base::i18n::StringSearchIgnoringCaseAndAccents( |
| 61 query, url_raw, NULL, NULL)) { |
| 62 return true; |
| 63 } |
| 64 |
| 65 string16 url_formatted = url_raw; |
| 66 if (item.GetBrowserContext()) { |
| 67 url_formatted = net::FormatUrl( |
| 68 item.GetOriginalUrl(), |
| 69 content::GetContentClient()->browser()->GetAcceptLangs( |
| 70 item.GetBrowserContext())); |
| 71 } |
| 72 if (base::i18n::StringSearchIgnoringCaseAndAccents( |
| 73 query, url_formatted, NULL, NULL)) { |
| 74 return true; |
| 75 } |
| 76 |
| 77 string16 path(item.GetTargetFilePath().LossyDisplayName()); |
| 78 return base::i18n::StringSearchIgnoringCaseAndAccents( |
| 79 query, path, NULL, NULL); |
51 } | 80 } |
52 | 81 |
53 static int GetStartTime(const DownloadItem& item) { | 82 static int GetStartTime(const DownloadItem& item) { |
54 return (item.GetStartTime() - base::Time::UnixEpoch()).InMilliseconds(); | 83 return (item.GetStartTime() - base::Time::UnixEpoch()).InMilliseconds(); |
55 } | 84 } |
56 | 85 |
57 static bool GetDangerAccepted(const DownloadItem& item) { | 86 static bool GetDangerAccepted(const DownloadItem& item) { |
58 return (item.GetSafetyState() == DownloadItem::DANGEROUS_BUT_VALIDATED); | 87 return (item.GetSafetyState() == DownloadItem::DANGEROUS_BUT_VALIDATED); |
59 } | 88 } |
60 | 89 |
61 static string16 GetFilename(const DownloadItem& item) { | 90 static string16 GetFilename(const DownloadItem& item) { |
62 // This filename will be compared with strings that could be passed in by the | 91 // This filename will be compared with strings that could be passed in by the |
63 // user, who only sees LossyDisplayNames. | 92 // user, who only sees LossyDisplayNames. |
64 return item.GetFullPath().LossyDisplayName(); | 93 return item.GetTargetFilePath().LossyDisplayName(); |
65 } | 94 } |
66 | 95 |
67 static std::string GetFilenameUTF8(const DownloadItem& item) { | 96 static std::string GetFilenameUTF8(const DownloadItem& item) { |
68 return UTF16ToUTF8(GetFilename(item)); | 97 return UTF16ToUTF8(GetFilename(item)); |
69 } | 98 } |
70 | 99 |
71 static std::string GetUrl(const DownloadItem& item) { | 100 static std::string GetUrl(const DownloadItem& item) { |
72 return item.GetOriginalUrl().spec(); | 101 return item.GetOriginalUrl().spec(); |
73 } | 102 } |
74 | 103 |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 | 378 |
350 void DownloadQuery::FinishSearch(DownloadQuery::DownloadVector* results) const { | 379 void DownloadQuery::FinishSearch(DownloadQuery::DownloadVector* results) const { |
351 if (!sorters_.empty()) | 380 if (!sorters_.empty()) |
352 std::partial_sort(results->begin(), | 381 std::partial_sort(results->begin(), |
353 results->begin() + std::min(limit_, results->size()), | 382 results->begin() + std::min(limit_, results->size()), |
354 results->end(), | 383 results->end(), |
355 DownloadComparator(sorters_)); | 384 DownloadComparator(sorters_)); |
356 if (results->size() > limit_) | 385 if (results->size() > limit_) |
357 results->resize(limit_); | 386 results->resize(limit_); |
358 } | 387 } |
OLD | NEW |