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/utf_string_conversions.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/app_list/search/search_webstore_result.h" |
| 15 #include "chrome/browser/ui/app_list/search/webstore_result.h" |
| 16 #include "chrome/browser/ui/app_list/search/webstore_search_fetcher.h" |
| 17 #include "chrome/common/extensions/extension_constants.h" |
| 18 #include "googleurl/src/gurl.h" |
| 19 |
| 20 namespace app_list { |
| 21 |
| 22 namespace { |
| 23 |
| 24 const char kKeyResults[] = "results"; |
| 25 const char kKeyId[] = "id"; |
| 26 const char kKeyLocalizedName[] = "localized_name"; |
| 27 const char kKeyIconUrl[] = "icon_url"; |
| 28 |
| 29 } // namespace |
| 30 |
| 31 WebstoreProvider::WebstoreProvider(Profile* profile) : profile_(profile) {} |
| 32 |
| 33 WebstoreProvider::~WebstoreProvider() {} |
| 34 |
| 35 void WebstoreProvider::Start(const base::string16& query) { |
| 36 if (!webstore_search_) { |
| 37 webstore_search_.reset(new WebstoreSearchFetcher( |
| 38 base::Bind(&WebstoreProvider::OnWebstoreSearchFetched, |
| 39 base::Unretained(this)), |
| 40 profile_->GetRequestContext())); |
| 41 } |
| 42 |
| 43 const std::string query_utf8 = UTF16ToUTF8(query); |
| 44 webstore_search_->Start(query_utf8, |
| 45 g_browser_process->GetApplicationLocale()); |
| 46 |
| 47 // Add a placeholder result which when clicked will run the user's query in a |
| 48 // browser. This placeholder is removed when the search results arrive. |
| 49 ClearResults(); |
| 50 Add(scoped_ptr<ChromeSearchResult>( |
| 51 new SearchWebstoreResult(profile_, query_utf8)).Pass()); |
| 52 } |
| 53 |
| 54 void WebstoreProvider::Stop() { |
| 55 if (webstore_search_) |
| 56 webstore_search_->Stop(); |
| 57 } |
| 58 |
| 59 void WebstoreProvider::OnWebstoreSearchFetched( |
| 60 scoped_ptr<base::DictionaryValue> json) { |
| 61 ProcessWebstoreSearchResults(json.get()); |
| 62 |
| 63 if (!webstore_search_fetched_callback_.is_null()) |
| 64 webstore_search_fetched_callback_.Run(); |
| 65 } |
| 66 |
| 67 void WebstoreProvider::ProcessWebstoreSearchResults( |
| 68 base::DictionaryValue* json) { |
| 69 base::ListValue* result_list = NULL; |
| 70 if (!json || |
| 71 !json->GetList(kKeyResults, &result_list) || |
| 72 !result_list || |
| 73 result_list->empty()) { |
| 74 return; |
| 75 } |
| 76 |
| 77 bool first_result = true; |
| 78 for (ListValue::const_iterator it = result_list->begin(); |
| 79 it != result_list->end(); |
| 80 ++it) { |
| 81 base::DictionaryValue* dict; |
| 82 if (!(*it)->GetAsDictionary(&dict)) |
| 83 continue; |
| 84 |
| 85 scoped_ptr<ChromeSearchResult> result(CreateResult(*dict)); |
| 86 if (!result) |
| 87 continue; |
| 88 |
| 89 if (first_result) { |
| 90 // Clears "search in webstore" place holder results. |
| 91 ClearResults(); |
| 92 first_result = false; |
| 93 } |
| 94 |
| 95 Add(result.Pass()); |
| 96 } |
| 97 } |
| 98 |
| 99 scoped_ptr<ChromeSearchResult> WebstoreProvider::CreateResult( |
| 100 const base::DictionaryValue& dict) { |
| 101 scoped_ptr<ChromeSearchResult> result; |
| 102 |
| 103 std::string app_id; |
| 104 std::string localized_name; |
| 105 std::string icon_url_string; |
| 106 if (!dict.GetString(kKeyId, &app_id) || |
| 107 !dict.GetString(kKeyLocalizedName, &localized_name) || |
| 108 !dict.GetString(kKeyIconUrl, &icon_url_string)) { |
| 109 return result.Pass(); |
| 110 } |
| 111 |
| 112 GURL icon_url(icon_url_string); |
| 113 if (!icon_url.is_valid()) |
| 114 return result.Pass(); |
| 115 |
| 116 result.reset(new WebstoreResult(profile_, app_id, localized_name, icon_url)); |
| 117 return result.Pass(); |
| 118 } |
| 119 |
| 120 } // namespace app_list |
OLD | NEW |