| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/app_list/search/webstore_provider.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/metrics/field_trial.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/browser_process.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #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/search_webstore_result.h" | |
| 18 #include "chrome/browser/ui/app_list/search/webstore_result.h" | |
| 19 #include "chrome/common/extensions/extension_constants.h" | |
| 20 #include "url/gurl.h" | |
| 21 | |
| 22 namespace app_list { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const char kKeyResults[] = "results"; | |
| 27 const char kKeyId[] = "id"; | |
| 28 const char kKeyLocalizedName[] = "localized_name"; | |
| 29 const char kKeyIconUrl[] = "icon_url"; | |
| 30 const size_t kMinimumQueryLength = 3u; | |
| 31 | |
| 32 // Returns true if the launcher should send queries to the web store server. | |
| 33 bool UseWebstoreSearch() { | |
| 34 const char kFieldTrialName[] = "LauncherUseWebstoreSearch"; | |
| 35 const char kEnable[] = "Enable"; | |
| 36 return base::FieldTrialList::FindFullName(kFieldTrialName) == kEnable; | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 WebstoreProvider::WebstoreProvider(Profile* profile, | |
| 42 AppListControllerDelegate* controller) | |
| 43 : profile_(profile), | |
| 44 controller_(controller) {} | |
| 45 | |
| 46 WebstoreProvider::~WebstoreProvider() {} | |
| 47 | |
| 48 void WebstoreProvider::Start(const base::string16& query) { | |
| 49 ClearResults(); | |
| 50 | |
| 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(); | |
| 55 return; | |
| 56 } | |
| 57 | |
| 58 const std::string query_utf8 = 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_); | |
| 67 if (cached_result) { | |
| 68 ProcessWebstoreSearchResults(cached_result); | |
| 69 if (!webstore_search_fetched_callback_.is_null()) | |
| 70 webstore_search_fetched_callback_.Run(); | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 if (UseWebstoreSearch() && chrome::IsSuggestPrefEnabled(profile_)) { | |
| 75 if (!webstore_search_) { | |
| 76 webstore_search_.reset(new JSONResponseFetcher( | |
| 77 base::Bind(&WebstoreProvider::OnWebstoreSearchFetched, | |
| 78 base::Unretained(this)), | |
| 79 profile_->GetRequestContext())); | |
| 80 } | |
| 81 | |
| 82 StartThrottledQuery(base::Bind(&WebstoreProvider::StartQuery, | |
| 83 base::Unretained(this))); | |
| 84 } | |
| 85 | |
| 86 // 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. | |
| 88 Add(scoped_ptr<ChromeSearchResult>( | |
| 89 new SearchWebstoreResult(profile_, query_utf8)).Pass()); | |
| 90 } | |
| 91 | |
| 92 void WebstoreProvider::Stop() { | |
| 93 if (webstore_search_) | |
| 94 webstore_search_->Stop(); | |
| 95 } | |
| 96 | |
| 97 void WebstoreProvider::StartQuery() { | |
| 98 // |query_| can be NULL when the query is scheduled but then canceled. | |
| 99 if (!webstore_search_ || query_.empty()) | |
| 100 return; | |
| 101 | |
| 102 webstore_search_->Start(extension_urls::GetWebstoreJsonSearchUrl( | |
| 103 query_, g_browser_process->GetApplicationLocale())); | |
| 104 } | |
| 105 | |
| 106 void WebstoreProvider::OnWebstoreSearchFetched( | |
| 107 scoped_ptr<base::DictionaryValue> json) { | |
| 108 ProcessWebstoreSearchResults(json.get()); | |
| 109 cache_.Put(query_, json.Pass()); | |
| 110 | |
| 111 if (!webstore_search_fetched_callback_.is_null()) | |
| 112 webstore_search_fetched_callback_.Run(); | |
| 113 } | |
| 114 | |
| 115 void WebstoreProvider::ProcessWebstoreSearchResults( | |
| 116 const base::DictionaryValue* json) { | |
| 117 const base::ListValue* result_list = NULL; | |
| 118 if (!json || | |
| 119 !json->GetList(kKeyResults, &result_list) || | |
| 120 !result_list || | |
| 121 result_list->empty()) { | |
| 122 return; | |
| 123 } | |
| 124 | |
| 125 bool first_result = true; | |
| 126 for (ListValue::const_iterator it = result_list->begin(); | |
| 127 it != result_list->end(); | |
| 128 ++it) { | |
| 129 const base::DictionaryValue* dict; | |
| 130 if (!(*it)->GetAsDictionary(&dict)) | |
| 131 continue; | |
| 132 | |
| 133 scoped_ptr<ChromeSearchResult> result(CreateResult(*dict)); | |
| 134 if (!result) | |
| 135 continue; | |
| 136 | |
| 137 if (first_result) { | |
| 138 // Clears "search in webstore" place holder results. | |
| 139 ClearResults(); | |
| 140 first_result = false; | |
| 141 } | |
| 142 | |
| 143 Add(result.Pass()); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 scoped_ptr<ChromeSearchResult> WebstoreProvider::CreateResult( | |
| 148 const base::DictionaryValue& dict) { | |
| 149 scoped_ptr<ChromeSearchResult> result; | |
| 150 | |
| 151 std::string app_id; | |
| 152 std::string localized_name; | |
| 153 std::string icon_url_string; | |
| 154 if (!dict.GetString(kKeyId, &app_id) || | |
| 155 !dict.GetString(kKeyLocalizedName, &localized_name) || | |
| 156 !dict.GetString(kKeyIconUrl, &icon_url_string)) { | |
| 157 return result.Pass(); | |
| 158 } | |
| 159 | |
| 160 GURL icon_url(icon_url_string); | |
| 161 if (!icon_url.is_valid()) | |
| 162 return result.Pass(); | |
| 163 | |
| 164 result.reset(new WebstoreResult( | |
| 165 profile_, app_id, localized_name, icon_url, controller_)); | |
| 166 return result.Pass(); | |
| 167 } | |
| 168 | |
| 169 } // namespace app_list | |
| OLD | NEW |