| 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/webstore_provider.h" | 5 #include "chrome/browser/ui/app_list/search/webstore/webstore_provider.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 base::Unretained(this)), | 88 base::Unretained(this)), |
| 89 profile_->GetRequestContext())); | 89 profile_->GetRequestContext())); |
| 90 } | 90 } |
| 91 | 91 |
| 92 query_pending_ = true; | 92 query_pending_ = true; |
| 93 StartThrottledQuery(base::Bind(&WebstoreProvider::StartQuery, | 93 StartThrottledQuery(base::Bind(&WebstoreProvider::StartQuery, |
| 94 base::Unretained(this))); | 94 base::Unretained(this))); |
| 95 | 95 |
| 96 // Add a placeholder result which when clicked will run the user's query in a | 96 // Add a placeholder result which when clicked will run the user's query in a |
| 97 // browser. This placeholder is removed when the search results arrive. | 97 // browser. This placeholder is removed when the search results arrive. |
| 98 Add(scoped_ptr<SearchResult>( | 98 Add(std::unique_ptr<SearchResult>( |
| 99 new SearchWebstoreResult(profile_, controller_, query_))); | 99 new SearchWebstoreResult(profile_, controller_, query_))); |
| 100 } | 100 } |
| 101 | 101 |
| 102 void WebstoreProvider::Stop() { | 102 void WebstoreProvider::Stop() { |
| 103 if (webstore_search_) | 103 if (webstore_search_) |
| 104 webstore_search_->Stop(); | 104 webstore_search_->Stop(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 void WebstoreProvider::StartQuery() { | 107 void WebstoreProvider::StartQuery() { |
| 108 // |query_| can be NULL when the query is scheduled but then canceled. | 108 // |query_| can be NULL when the query is scheduled but then canceled. |
| 109 if (!webstore_search_ || query_.empty()) | 109 if (!webstore_search_ || query_.empty()) |
| 110 return; | 110 return; |
| 111 | 111 |
| 112 webstore_search_->Start(extension_urls::GetWebstoreJsonSearchUrl( | 112 webstore_search_->Start(extension_urls::GetWebstoreJsonSearchUrl( |
| 113 query_, g_browser_process->GetApplicationLocale())); | 113 query_, g_browser_process->GetApplicationLocale())); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void WebstoreProvider::OnWebstoreSearchFetched( | 116 void WebstoreProvider::OnWebstoreSearchFetched( |
| 117 scoped_ptr<base::DictionaryValue> json) { | 117 std::unique_ptr<base::DictionaryValue> json) { |
| 118 ProcessWebstoreSearchResults(json.get()); | 118 ProcessWebstoreSearchResults(json.get()); |
| 119 cache_->Put(WebserviceCache::WEBSTORE, query_, std::move(json)); | 119 cache_->Put(WebserviceCache::WEBSTORE, query_, std::move(json)); |
| 120 query_pending_ = false; | 120 query_pending_ = false; |
| 121 | 121 |
| 122 if (!webstore_search_fetched_callback_.is_null()) | 122 if (!webstore_search_fetched_callback_.is_null()) |
| 123 webstore_search_fetched_callback_.Run(); | 123 webstore_search_fetched_callback_.Run(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 void WebstoreProvider::ProcessWebstoreSearchResults( | 126 void WebstoreProvider::ProcessWebstoreSearchResults( |
| 127 const base::DictionaryValue* json) { | 127 const base::DictionaryValue* json) { |
| 128 const base::ListValue* result_list = NULL; | 128 const base::ListValue* result_list = NULL; |
| 129 if (!json || | 129 if (!json || |
| 130 !json->GetList(kKeyResults, &result_list) || | 130 !json->GetList(kKeyResults, &result_list) || |
| 131 !result_list || | 131 !result_list || |
| 132 result_list->empty()) { | 132 result_list->empty()) { |
| 133 return; | 133 return; |
| 134 } | 134 } |
| 135 | 135 |
| 136 bool first_result = true; | 136 bool first_result = true; |
| 137 TokenizedString query(base::UTF8ToUTF16(query_)); | 137 TokenizedString query(base::UTF8ToUTF16(query_)); |
| 138 for (base::ListValue::const_iterator it = result_list->begin(); | 138 for (base::ListValue::const_iterator it = result_list->begin(); |
| 139 it != result_list->end(); | 139 it != result_list->end(); |
| 140 ++it) { | 140 ++it) { |
| 141 const base::DictionaryValue* dict; | 141 const base::DictionaryValue* dict; |
| 142 if (!(*it)->GetAsDictionary(&dict)) | 142 if (!(*it)->GetAsDictionary(&dict)) |
| 143 continue; | 143 continue; |
| 144 | 144 |
| 145 scoped_ptr<SearchResult> result(CreateResult(query, *dict)); | 145 std::unique_ptr<SearchResult> result(CreateResult(query, *dict)); |
| 146 if (!result) | 146 if (!result) |
| 147 continue; | 147 continue; |
| 148 | 148 |
| 149 if (first_result) { | 149 if (first_result) { |
| 150 // Clears "search in webstore" place holder results. | 150 // Clears "search in webstore" place holder results. |
| 151 ClearResults(); | 151 ClearResults(); |
| 152 first_result = false; | 152 first_result = false; |
| 153 } | 153 } |
| 154 | 154 |
| 155 Add(std::move(result)); | 155 Add(std::move(result)); |
| 156 } | 156 } |
| 157 } | 157 } |
| 158 | 158 |
| 159 scoped_ptr<SearchResult> WebstoreProvider::CreateResult( | 159 std::unique_ptr<SearchResult> WebstoreProvider::CreateResult( |
| 160 const TokenizedString& query, | 160 const TokenizedString& query, |
| 161 const base::DictionaryValue& dict) { | 161 const base::DictionaryValue& dict) { |
| 162 std::string app_id; | 162 std::string app_id; |
| 163 std::string localized_name; | 163 std::string localized_name; |
| 164 std::string icon_url_string; | 164 std::string icon_url_string; |
| 165 bool is_paid = false; | 165 bool is_paid = false; |
| 166 if (!dict.GetString(kKeyId, &app_id) || | 166 if (!dict.GetString(kKeyId, &app_id) || |
| 167 !dict.GetString(kKeyLocalizedName, &localized_name) || | 167 !dict.GetString(kKeyLocalizedName, &localized_name) || |
| 168 !dict.GetString(kKeyIconUrl, &icon_url_string) || | 168 !dict.GetString(kKeyIconUrl, &icon_url_string) || |
| 169 !dict.GetBoolean(kKeyIsPaid, &is_paid)) { | 169 !dict.GetBoolean(kKeyIsPaid, &is_paid)) { |
| 170 return scoped_ptr<SearchResult>(); | 170 return std::unique_ptr<SearchResult>(); |
| 171 } | 171 } |
| 172 | 172 |
| 173 GURL icon_url(icon_url_string); | 173 GURL icon_url(icon_url_string); |
| 174 if (!icon_url.is_valid()) | 174 if (!icon_url.is_valid()) |
| 175 return scoped_ptr<SearchResult>(); | 175 return std::unique_ptr<SearchResult>(); |
| 176 | 176 |
| 177 std::string item_type_string; | 177 std::string item_type_string; |
| 178 dict.GetString(kKeyItemType, &item_type_string); | 178 dict.GetString(kKeyItemType, &item_type_string); |
| 179 extensions::Manifest::Type item_type = ParseItemType(item_type_string); | 179 extensions::Manifest::Type item_type = ParseItemType(item_type_string); |
| 180 | 180 |
| 181 // Calculate the relevance score by matching the query with the title. Results | 181 // Calculate the relevance score by matching the query with the title. Results |
| 182 // with a match score of 0 are discarded. This will also be used to set the | 182 // with a match score of 0 are discarded. This will also be used to set the |
| 183 // title tags (highlighting which parts of the title matched the search | 183 // title tags (highlighting which parts of the title matched the search |
| 184 // query). | 184 // query). |
| 185 TokenizedString title(base::UTF8ToUTF16(localized_name)); | 185 TokenizedString title(base::UTF8ToUTF16(localized_name)); |
| 186 TokenizedStringMatch match; | 186 TokenizedStringMatch match; |
| 187 if (!match.Calculate(query, title)) | 187 if (!match.Calculate(query, title)) |
| 188 return scoped_ptr<SearchResult>(); | 188 return std::unique_ptr<SearchResult>(); |
| 189 | 189 |
| 190 scoped_ptr<SearchResult> result(new WebstoreResult( | 190 std::unique_ptr<SearchResult> result(new WebstoreResult( |
| 191 profile_, app_id, icon_url, is_paid, item_type, controller_)); | 191 profile_, app_id, icon_url, is_paid, item_type, controller_)); |
| 192 result->UpdateFromMatch(title, match); | 192 result->UpdateFromMatch(title, match); |
| 193 return result; | 193 return result; |
| 194 } | 194 } |
| 195 | 195 |
| 196 } // namespace app_list | 196 } // namespace app_list |
| OLD | NEW |