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/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/search/search.h" | 16 #include "chrome/browser/search/search.h" |
16 #include "chrome/browser/ui/app_list/search/common/json_response_fetcher.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/common/extensions/extension_constants.h" | 20 #include "chrome/common/extensions/extension_constants.h" |
20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
21 | 22 |
22 namespace app_list { | 23 namespace app_list { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 const char kKeyResults[] = "results"; | 27 const char kKeyResults[] = "results"; |
27 const char kKeyId[] = "id"; | 28 const char kKeyId[] = "id"; |
28 const char kKeyLocalizedName[] = "localized_name"; | 29 const char kKeyLocalizedName[] = "localized_name"; |
29 const char kKeyIconUrl[] = "icon_url"; | 30 const char kKeyIconUrl[] = "icon_url"; |
30 const size_t kMinimumQueryLength = 3u; | 31 const size_t kMinimumQueryLength = 3u; |
31 | 32 |
32 // 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. |
33 bool UseWebstoreSearch() { | 34 bool UseWebstoreSearch() { |
34 const char kFieldTrialName[] = "LauncherUseWebstoreSearch"; | 35 const char kFieldTrialName[] = "LauncherUseWebstoreSearch"; |
35 const char kEnable[] = "Enable"; | 36 const char kEnable[] = "Enable"; |
36 return base::FieldTrialList::FindFullName(kFieldTrialName) == kEnable; | 37 return base::FieldTrialList::FindFullName(kFieldTrialName) == kEnable; |
37 } | 38 } |
38 | 39 |
39 } // namespace | 40 } // namespace |
40 | 41 |
41 WebstoreProvider::WebstoreProvider(Profile* profile, | 42 WebstoreProvider::WebstoreProvider(Profile* profile, |
42 AppListControllerDelegate* controller) | 43 AppListControllerDelegate* controller) |
43 : profile_(profile), | 44 : WebserviceSearchProvider(profile), |
44 controller_(controller) {} | 45 controller_(controller){} |
45 | 46 |
46 WebstoreProvider::~WebstoreProvider() {} | 47 WebstoreProvider::~WebstoreProvider() {} |
47 | 48 |
48 void WebstoreProvider::Start(const base::string16& query) { | 49 void WebstoreProvider::Start(const base::string16& query) { |
49 ClearResults(); | 50 ClearResults(); |
50 | 51 if (!IsValidQuery(query)) { |
51 // If |query| contains sensitive data, bail out and do not create the place | |
52 // holder "search-web-store" result. | |
53 if (IsSensitiveInput(query)) { | |
54 query_.clear(); | 52 query_.clear(); |
55 return; | 53 return; |
56 } | 54 } |
57 | 55 |
58 const std::string query_utf8 = UTF16ToUTF8(query); | 56 query_ = UTF16ToUTF8(query); |
59 | |
60 if (query_utf8.size() < kMinimumQueryLength) { | |
61 query_.clear(); | |
62 return; | |
63 } | |
64 | |
65 query_ = query_utf8; | |
66 const base::DictionaryValue* cached_result = cache_.Get(query_); | 57 const base::DictionaryValue* cached_result = cache_.Get(query_); |
67 if (cached_result) { | 58 if (cached_result) { |
68 ProcessWebstoreSearchResults(cached_result); | 59 ProcessWebstoreSearchResults(cached_result); |
69 if (!webstore_search_fetched_callback_.is_null()) | 60 if (!webstore_search_fetched_callback_.is_null()) |
70 webstore_search_fetched_callback_.Run(); | 61 webstore_search_fetched_callback_.Run(); |
71 return; | 62 return; |
72 } | 63 } |
73 | 64 |
74 if (UseWebstoreSearch() && chrome::IsSuggestPrefEnabled(profile_)) { | 65 if (UseWebstoreSearch()) { |
75 if (!webstore_search_) { | 66 if (!webstore_search_) { |
76 webstore_search_.reset(new JSONResponseFetcher( | 67 webstore_search_.reset(new JSONResponseFetcher( |
77 base::Bind(&WebstoreProvider::OnWebstoreSearchFetched, | 68 base::Bind(&WebstoreProvider::OnWebstoreSearchFetched, |
78 base::Unretained(this)), | 69 base::Unretained(this)), |
79 profile_->GetRequestContext())); | 70 profile_->GetRequestContext())); |
80 } | 71 } |
81 | 72 |
82 StartThrottledQuery(base::Bind(&WebstoreProvider::StartQuery, | 73 StartThrottledQuery(base::Bind(&WebstoreProvider::StartQuery, |
83 base::Unretained(this))); | 74 base::Unretained(this))); |
84 } | 75 } |
85 | 76 |
86 // 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 |
87 // browser. This placeholder is removed when the search results arrive. | 78 // browser. This placeholder is removed when the search results arrive. |
88 Add(scoped_ptr<ChromeSearchResult>( | 79 Add(scoped_ptr<ChromeSearchResult>( |
89 new SearchWebstoreResult(profile_, query_utf8)).Pass()); | 80 new SearchWebstoreResult(profile_, query_)).Pass()); |
90 } | 81 } |
91 | 82 |
92 void WebstoreProvider::Stop() { | 83 void WebstoreProvider::Stop() { |
93 if (webstore_search_) | 84 if (webstore_search_) |
94 webstore_search_->Stop(); | 85 webstore_search_->Stop(); |
95 } | 86 } |
96 | 87 |
97 void WebstoreProvider::StartQuery() { | 88 void WebstoreProvider::StartQuery() { |
98 // |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. |
99 if (!webstore_search_ || query_.empty()) | 90 if (!webstore_search_ || query_.empty()) |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 GURL icon_url(icon_url_string); | 151 GURL icon_url(icon_url_string); |
161 if (!icon_url.is_valid()) | 152 if (!icon_url.is_valid()) |
162 return result.Pass(); | 153 return result.Pass(); |
163 | 154 |
164 result.reset(new WebstoreResult( | 155 result.reset(new WebstoreResult( |
165 profile_, app_id, localized_name, icon_url, controller_)); | 156 profile_, app_id, localized_name, icon_url, controller_)); |
166 return result.Pass(); | 157 return result.Pass(); |
167 } | 158 } |
168 | 159 |
169 } // namespace app_list | 160 } // namespace app_list |
OLD | NEW |