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 // TODO(phajdan.jr): write a test case for the following code. | |
66 // A good test case would be: | |
67 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", | |
68 // L"/\x4f60\x597d\x4f60\x597d", | |
69 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" | |
70 string16 url_formatted = url_raw; | |
71 if (item.GetBrowserContext()) | |
asanka
2012/09/12 18:43:49
Nit: Braces
benjhayden
2012/09/12 19:31:57
Done.
| |
72 url_formatted = net::FormatUrl( | |
73 item.GetOriginalUrl(), | |
74 content::GetContentClient()->browser()->GetAcceptLangs( | |
75 item.GetBrowserContext())); | |
76 if (base::i18n::StringSearchIgnoringCaseAndAccents( | |
77 query, url_formatted, NULL, NULL)) { | |
78 return true; | |
79 } | |
80 | |
81 string16 path(item.GetTargetFilePath().LossyDisplayName()); | |
82 return base::i18n::StringSearchIgnoringCaseAndAccents( | |
83 query, path, NULL, NULL); | |
51 } | 84 } |
52 | 85 |
53 static int GetStartTime(const DownloadItem& item) { | 86 static int GetStartTime(const DownloadItem& item) { |
54 return (item.GetStartTime() - base::Time::UnixEpoch()).InMilliseconds(); | 87 return (item.GetStartTime() - base::Time::UnixEpoch()).InMilliseconds(); |
55 } | 88 } |
56 | 89 |
57 static bool GetDangerAccepted(const DownloadItem& item) { | 90 static bool GetDangerAccepted(const DownloadItem& item) { |
58 return (item.GetSafetyState() == DownloadItem::DANGEROUS_BUT_VALIDATED); | 91 return (item.GetSafetyState() == DownloadItem::DANGEROUS_BUT_VALIDATED); |
59 } | 92 } |
60 | 93 |
61 static string16 GetFilename(const DownloadItem& item) { | 94 static string16 GetFilename(const DownloadItem& item) { |
62 // This filename will be compared with strings that could be passed in by the | 95 // This filename will be compared with strings that could be passed in by the |
63 // user, who only sees LossyDisplayNames. | 96 // user, who only sees LossyDisplayNames. |
64 return item.GetFullPath().LossyDisplayName(); | 97 return item.GetTargetFilePath().LossyDisplayName(); |
65 } | 98 } |
66 | 99 |
67 static std::string GetFilenameUTF8(const DownloadItem& item) { | 100 static std::string GetFilenameUTF8(const DownloadItem& item) { |
68 return UTF16ToUTF8(GetFilename(item)); | 101 return UTF16ToUTF8(GetFilename(item)); |
69 } | 102 } |
70 | 103 |
71 static std::string GetUrl(const DownloadItem& item) { | 104 static std::string GetUrl(const DownloadItem& item) { |
72 return item.GetOriginalUrl().spec(); | 105 return item.GetOriginalUrl().spec(); |
73 } | 106 } |
74 | 107 |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
349 | 382 |
350 void DownloadQuery::FinishSearch(DownloadQuery::DownloadVector* results) const { | 383 void DownloadQuery::FinishSearch(DownloadQuery::DownloadVector* results) const { |
351 if (!sorters_.empty()) | 384 if (!sorters_.empty()) |
352 std::partial_sort(results->begin(), | 385 std::partial_sort(results->begin(), |
353 results->begin() + std::min(limit_, results->size()), | 386 results->begin() + std::min(limit_, results->size()), |
354 results->end(), | 387 results->end(), |
355 DownloadComparator(sorters_)); | 388 DownloadComparator(sorters_)); |
356 if (results->size() > limit_) | 389 if (results->size() > limit_) |
357 results->resize(limit_); | 390 results->resize(limit_); |
358 } | 391 } |
OLD | NEW |