OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/ui/app_list/search/webstore_provider.h" | 5 #include "chrome/browser/ui/app_list/search/webstore/webstore_provider.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
10 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
11 #include "base/strings/string_util.h" | |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/search/search.h" | 16 #include "chrome/browser/search/search.h" |
| 17 #include "chrome/browser/ui/app_list/search/common/json_response_fetcher.h" |
17 #include "chrome/browser/ui/app_list/search/search_webstore_result.h" | 18 #include "chrome/browser/ui/app_list/search/search_webstore_result.h" |
18 #include "chrome/browser/ui/app_list/search/webstore_result.h" | 19 #include "chrome/browser/ui/app_list/search/webstore/webstore_result.h" |
19 #include "chrome/browser/ui/app_list/search/webstore_search_fetcher.h" | |
20 #include "chrome/common/extensions/extension_constants.h" | 20 #include "chrome/common/extensions/extension_constants.h" |
21 #include "chrome/common/url_constants.h" | |
22 #include "url/gurl.h" | 21 #include "url/gurl.h" |
23 | 22 |
24 namespace app_list { | 23 namespace app_list { |
25 | 24 |
26 namespace { | 25 namespace { |
27 | 26 |
28 const char kKeyResults[] = "results"; | 27 const char kKeyResults[] = "results"; |
29 const char kKeyId[] = "id"; | 28 const char kKeyId[] = "id"; |
30 const char kKeyLocalizedName[] = "localized_name"; | 29 const char kKeyLocalizedName[] = "localized_name"; |
31 const char kKeyIconUrl[] = "icon_url"; | 30 const char kKeyIconUrl[] = "icon_url"; |
32 const size_t kMinimumQueryLength = 3u; | 31 const size_t kMinimumQueryLength = 3u; |
33 const int kWebstoreQueryThrottleIntrevalInMs = 100; | |
34 | 32 |
35 // Returns true if the launcher should send queries to the web store server. | 33 // Returns true if the launcher should send queries to the web store server. |
36 bool UseWebstoreSearch() { | 34 bool UseWebstoreSearch() { |
37 const char kFieldTrialName[] = "LauncherUseWebstoreSearch"; | 35 const char kFieldTrialName[] = "LauncherUseWebstoreSearch"; |
38 const char kEnable[] = "Enable"; | 36 const char kEnable[] = "Enable"; |
39 return base::FieldTrialList::FindFullName(kFieldTrialName) == kEnable; | 37 return base::FieldTrialList::FindFullName(kFieldTrialName) == kEnable; |
40 } | 38 } |
41 | 39 |
42 // Returns whether or not the user's input string, |query|, might contain any | |
43 // sensitive information, based purely on its value and not where it came from. | |
44 bool IsSensitiveInput(const string16& query) { | |
45 const GURL query_as_url(query); | |
46 if (!query_as_url.is_valid()) | |
47 return false; | |
48 | |
49 // The input can be interpreted as a URL. Check to see if it is potentially | |
50 // sensitive. (Code shamelessly copied from search_provider.cc's | |
51 // IsQuerySuitableForSuggest function.) | |
52 | |
53 // First we check the scheme: if this looks like a URL with a scheme that is | |
54 // file, we shouldn't send it. Sending such things is a waste of time and a | |
55 // disclosure of potentially private, local data. If the scheme is OK, we | |
56 // still need to check other cases below. | |
57 if (LowerCaseEqualsASCII(query_as_url.scheme(), chrome::kFileScheme)) | |
58 return true; | |
59 | |
60 // Don't send URLs with usernames, queries or refs. Some of these are | |
61 // private, and the Suggest server is unlikely to have any useful results | |
62 // for any of them. Also don't send URLs with ports, as we may initially | |
63 // think that a username + password is a host + port (and we don't want to | |
64 // send usernames/passwords), and even if the port really is a port, the | |
65 // server is once again unlikely to have and useful results. | |
66 if (!query_as_url.username().empty() || | |
67 !query_as_url.port().empty() || | |
68 !query_as_url.query().empty() || | |
69 !query_as_url.ref().empty()) { | |
70 return true; | |
71 } | |
72 | |
73 // Don't send anything for https except the hostname. Hostnames are OK | |
74 // because they are visible when the TCP connection is established, but the | |
75 // specific path may reveal private information. | |
76 if (LowerCaseEqualsASCII(query_as_url.scheme(), content::kHttpsScheme) && | |
77 !query_as_url.path().empty() && query_as_url.path() != "/") { | |
78 return true; | |
79 } | |
80 | |
81 return false; | |
82 } | |
83 | |
84 } // namespace | 40 } // namespace |
85 | 41 |
86 WebstoreProvider::WebstoreProvider(Profile* profile, | 42 WebstoreProvider::WebstoreProvider(Profile* profile, |
87 AppListControllerDelegate* controller) | 43 AppListControllerDelegate* controller) |
88 : profile_(profile), | 44 : WebserviceSearchProvider(profile), |
89 controller_(controller), | 45 controller_(controller){} |
90 use_throttling_(true) {} | |
91 | 46 |
92 WebstoreProvider::~WebstoreProvider() {} | 47 WebstoreProvider::~WebstoreProvider() {} |
93 | 48 |
94 void WebstoreProvider::Start(const base::string16& query) { | 49 void WebstoreProvider::Start(const base::string16& query) { |
95 ClearResults(); | 50 ClearResults(); |
96 | 51 if (!IsValidQuery(query)) { |
97 // If |query| contains sensitive data, bail out and do not create the place | |
98 // holder "search-web-store" result. | |
99 if (IsSensitiveInput(query)) { | |
100 query_.clear(); | 52 query_.clear(); |
101 return; | 53 return; |
102 } | 54 } |
103 | 55 |
104 const std::string query_utf8 = UTF16ToUTF8(query); | 56 query_ = UTF16ToUTF8(query); |
105 | |
106 if (query_utf8.size() < kMinimumQueryLength) { | |
107 query_.clear(); | |
108 return; | |
109 } | |
110 | |
111 query_ = query_utf8; | |
112 const base::DictionaryValue* cached_result = cache_.Get(query_); | 57 const base::DictionaryValue* cached_result = cache_.Get(query_); |
113 if (cached_result) { | 58 if (cached_result) { |
114 ProcessWebstoreSearchResults(cached_result); | 59 ProcessWebstoreSearchResults(cached_result); |
115 if (!webstore_search_fetched_callback_.is_null()) | 60 if (!webstore_search_fetched_callback_.is_null()) |
116 webstore_search_fetched_callback_.Run(); | 61 webstore_search_fetched_callback_.Run(); |
117 return; | 62 return; |
118 } | 63 } |
119 | 64 |
120 if (UseWebstoreSearch() && chrome::IsSuggestPrefEnabled(profile_)) { | 65 if (UseWebstoreSearch()) { |
121 if (!webstore_search_) { | 66 if (!webstore_search_) { |
122 webstore_search_.reset(new WebstoreSearchFetcher( | 67 webstore_search_.reset(new JSONResponseFetcher( |
123 base::Bind(&WebstoreProvider::OnWebstoreSearchFetched, | 68 base::Bind(&WebstoreProvider::OnWebstoreSearchFetched, |
124 base::Unretained(this)), | 69 base::Unretained(this)), |
125 profile_->GetRequestContext())); | 70 profile_->GetRequestContext())); |
126 } | 71 } |
127 | 72 |
128 base::TimeDelta interval = | 73 StartThrottledQuery(base::Bind(&WebstoreProvider::StartQuery, |
129 base::TimeDelta::FromMilliseconds(kWebstoreQueryThrottleIntrevalInMs); | 74 base::Unretained(this))); |
130 if (!use_throttling_ || base::Time::Now() - last_keytyped_ > interval) { | |
131 query_throttler_.Stop(); | |
132 StartQuery(); | |
133 } else { | |
134 query_throttler_.Start( | |
135 FROM_HERE, | |
136 interval, | |
137 base::Bind(&WebstoreProvider::StartQuery, base::Unretained(this))); | |
138 } | |
139 last_keytyped_ = base::Time::Now(); | |
140 } | 75 } |
141 | 76 |
142 // Add a placeholder result which when clicked will run the user's query in a | 77 // Add a placeholder result which when clicked will run the user's query in a |
143 // browser. This placeholder is removed when the search results arrive. | 78 // browser. This placeholder is removed when the search results arrive. |
144 Add(scoped_ptr<ChromeSearchResult>( | 79 Add(scoped_ptr<ChromeSearchResult>( |
145 new SearchWebstoreResult(profile_, query_utf8)).Pass()); | 80 new SearchWebstoreResult(profile_, query_)).Pass()); |
146 } | 81 } |
147 | 82 |
148 void WebstoreProvider::Stop() { | 83 void WebstoreProvider::Stop() { |
149 if (webstore_search_) | 84 if (webstore_search_) |
150 webstore_search_->Stop(); | 85 webstore_search_->Stop(); |
151 } | 86 } |
152 | 87 |
153 void WebstoreProvider::StartQuery() { | 88 void WebstoreProvider::StartQuery() { |
154 // |query_| can be NULL when the query is scheduled but then canceled. | 89 // |query_| can be NULL when the query is scheduled but then canceled. |
155 if (!webstore_search_ || query_.empty()) | 90 if (!webstore_search_ || query_.empty()) |
156 return; | 91 return; |
157 | 92 |
158 webstore_search_->Start(query_, g_browser_process->GetApplicationLocale()); | 93 webstore_search_->Start(extension_urls::GetWebstoreJsonSearchUrl( |
| 94 query_, g_browser_process->GetApplicationLocale())); |
159 } | 95 } |
160 | 96 |
161 void WebstoreProvider::OnWebstoreSearchFetched( | 97 void WebstoreProvider::OnWebstoreSearchFetched( |
162 scoped_ptr<base::DictionaryValue> json) { | 98 scoped_ptr<base::DictionaryValue> json) { |
163 ProcessWebstoreSearchResults(json.get()); | 99 ProcessWebstoreSearchResults(json.get()); |
164 cache_.Put(query_, json.Pass()); | 100 cache_.Put(query_, json.Pass()); |
165 | 101 |
166 if (!webstore_search_fetched_callback_.is_null()) | 102 if (!webstore_search_fetched_callback_.is_null()) |
167 webstore_search_fetched_callback_.Run(); | 103 webstore_search_fetched_callback_.Run(); |
168 } | 104 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 GURL icon_url(icon_url_string); | 151 GURL icon_url(icon_url_string); |
216 if (!icon_url.is_valid()) | 152 if (!icon_url.is_valid()) |
217 return result.Pass(); | 153 return result.Pass(); |
218 | 154 |
219 result.reset(new WebstoreResult( | 155 result.reset(new WebstoreResult( |
220 profile_, app_id, localized_name, icon_url, controller_)); | 156 profile_, app_id, localized_name, icon_url, controller_)); |
221 return result.Pass(); | 157 return result.Pass(); |
222 } | 158 } |
223 | 159 |
224 } // namespace app_list | 160 } // namespace app_list |
OLD | NEW |