Chromium Code Reviews| Index: chrome/browser/ui/app_list/search/webstore_provider.cc |
| diff --git a/chrome/browser/ui/app_list/search/webstore_provider.cc b/chrome/browser/ui/app_list/search/webstore_provider.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..937816b25104d704e4cdb0c443aee4c161727b26 |
| --- /dev/null |
| +++ b/chrome/browser/ui/app_list/search/webstore_provider.cc |
| @@ -0,0 +1,117 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/app_list/search/webstore_provider.h" |
| + |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/app_list/search/search_webstore_result.h" |
| +#include "chrome/browser/ui/app_list/search/webstore_result.h" |
| +#include "chrome/browser/ui/app_list/search/webstore_search_fetcher.h" |
| +#include "chrome/common/extensions/extension_constants.h" |
| +#include "googleurl/src/gurl.h" |
| + |
| +namespace app_list { |
| + |
| +namespace { |
| + |
| +const char kKeyResults[] = "results"; |
| +const char kKeyId[] = "id"; |
| +const char kKeyLocalizedName[] = "localized_name"; |
| +const char kKeyIconUrl[] = "icon_url"; |
| + |
| +} // namespace |
| + |
| +WebstoreProvider::WebstoreProvider(Profile* profile) : profile_(profile) {} |
| + |
| +WebstoreProvider::~WebstoreProvider() {} |
| + |
| +void WebstoreProvider::Start(const base::string16& query) { |
| + if (!webstore_search_) { |
| + webstore_search_.reset(new WebstoreSearchFetcher( |
| + base::Bind(&WebstoreProvider::OnWebstoreSearchFetched, |
| + base::Unretained(this)), |
| + profile_->GetRequestContext())); |
| + } |
| + |
| + const std::string query8 = UTF16ToUTF8(query); |
|
James Cook
2013/05/20 14:39:34
nit: query8 -> query_utf8 or similar
At first I t
xiyuan
2013/05/20 16:56:31
Renamed query8 -> query_utf8. Too lazy to type. :p
|
| + webstore_search_->Start(query8, g_browser_process->GetApplicationLocale()); |
| + |
| + ClearResults(); |
| + Add(scoped_ptr<ChromeSearchResult>( |
|
koz (OOO until 15th September)
2013/05/20 01:14:16
I think this would be clearer with a comment:
"Ad
xiyuan
2013/05/20 16:56:31
Done.
|
| + new SearchWebstoreResult(profile_, query8)).Pass()); |
| +} |
| + |
| +void WebstoreProvider::Stop() { |
| + if (webstore_search_) |
| + webstore_search_->Stop(); |
| +} |
| + |
| +void WebstoreProvider::OnWebstoreSearchFetched( |
| + scoped_ptr<base::DictionaryValue> json) { |
| + ProcessWebstoreSearchResults(json.get()); |
| + |
| + if (!webstore_search_fetched_callback_.is_null()) |
| + webstore_search_fetched_callback_.Run(); |
| +} |
| + |
| +void WebstoreProvider::ProcessWebstoreSearchResults( |
| + base::DictionaryValue* json) { |
| + base::ListValue* result_list = NULL; |
| + if (!json || |
| + !json->GetList(kKeyResults, &result_list) || |
| + !result_list || |
| + result_list->empty()) { |
| + return; |
| + } |
| + |
| + bool first_result = true; |
| + for (ListValue::const_iterator it = result_list->begin(); |
| + it != result_list->end(); |
| + ++it) { |
| + base::DictionaryValue* dict; |
| + if (!(*it)->GetAsDictionary(&dict)) |
| + continue; |
| + |
| + scoped_ptr<ChromeSearchResult> result(CreateResult(*dict)); |
| + if (!result) |
| + continue; |
| + |
| + if (first_result) { |
| + // Clears "search in webstore" place holder results. |
| + ClearResults(); |
| + first_result = false; |
| + } |
| + |
| + Add(result.Pass()); |
| + } |
| +} |
| + |
| +scoped_ptr<ChromeSearchResult> WebstoreProvider::CreateResult( |
| + const base::DictionaryValue& dict) { |
| + scoped_ptr<ChromeSearchResult> result; |
| + |
| + std::string app_id; |
| + std::string localized_name; |
| + std::string icon_url_string; |
| + if (!dict.GetString(kKeyId, &app_id) || |
| + !dict.GetString(kKeyLocalizedName, &localized_name) || |
| + !dict.GetString(kKeyIconUrl, &icon_url_string)) { |
| + return result.Pass(); |
| + } |
| + |
| + GURL icon_url(icon_url_string); |
| + if (!icon_url.is_valid()) |
| + return result.Pass(); |
| + |
| + result.reset(new WebstoreResult(profile_, app_id, localized_name, icon_url)); |
| + return result.Pass(); |
| +} |
| + |
| +} // namespace app_list |