| 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/people/people_provider.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/strings/utf_string_conversions.h" | |
| 14 #include "base/values.h" | |
| 15 #include "chrome/browser/browser_process.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/browser/search/search.h" | |
| 18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 19 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 20 #include "chrome/browser/ui/app_list/search/common/json_response_fetcher.h" | |
| 21 #include "chrome/browser/ui/app_list/search/people/people_result.h" | |
| 22 #include "chrome/browser/ui/app_list/search/people/person.h" | |
| 23 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 24 #include "components/signin/core/browser/signin_manager.h" | |
| 25 #include "google_apis/gaia/gaia_constants.h" | |
| 26 #include "net/base/url_util.h" | |
| 27 #include "url/gurl.h" | |
| 28 | |
| 29 namespace app_list { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 const char kKeyItems[] = "items"; | |
| 34 | |
| 35 const char kAccessTokenField[] = "access_token"; | |
| 36 const char kQueryField[] = "query"; | |
| 37 const char kPeopleSearchUrl[] = | |
| 38 "https://www.googleapis.com/plus/v2whitelisted/people/autocomplete"; | |
| 39 | |
| 40 // OAuth2 scope for access to the Google+ People Search API. | |
| 41 const char kPeopleSearchOAuth2Scope[] = | |
| 42 "https://www.googleapis.com/auth/plus.peopleapi.readwrite"; | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 PeopleProvider::PeopleProvider(Profile* profile, | |
| 47 AppListControllerDelegate* controller) | |
| 48 : WebserviceSearchProvider(profile), | |
| 49 OAuth2TokenService::Consumer("people_provider"), | |
| 50 controller_(controller), | |
| 51 people_search_url_(kPeopleSearchUrl), | |
| 52 skip_request_token_for_test_(false) { | |
| 53 oauth2_scope_.insert(kPeopleSearchOAuth2Scope); | |
| 54 } | |
| 55 | |
| 56 PeopleProvider::~PeopleProvider() {} | |
| 57 | |
| 58 void PeopleProvider::Start(bool /*is_voice_query*/, | |
| 59 const base::string16& query) { | |
| 60 ClearResults(); | |
| 61 if (!IsValidQuery(query)) { | |
| 62 query_.clear(); | |
| 63 return; | |
| 64 } | |
| 65 | |
| 66 query_ = base::UTF16ToUTF8(query); | |
| 67 | |
| 68 const CacheResult result = cache_->Get(WebserviceCache::PEOPLE, query_); | |
| 69 if (result.second) { | |
| 70 ProcessPeopleSearchResults(result.second); | |
| 71 if (!people_search_fetched_callback_.is_null()) | |
| 72 people_search_fetched_callback_.Run(); | |
| 73 if (result.first == FRESH) | |
| 74 return; | |
| 75 } | |
| 76 | |
| 77 | |
| 78 if (!people_search_) { | |
| 79 people_search_.reset(new JSONResponseFetcher( | |
| 80 base::Bind(&PeopleProvider::OnPeopleSearchFetched, | |
| 81 base::Unretained(this)), | |
| 82 profile_->GetRequestContext())); | |
| 83 } | |
| 84 | |
| 85 if (!skip_request_token_for_test_) { | |
| 86 // We start with reqesting the access token. Once the token is fetched, | |
| 87 // we'll create the full query URL and fetch it. | |
| 88 StartThrottledQuery(base::Bind(&PeopleProvider::RequestAccessToken, | |
| 89 base::Unretained(this))); | |
| 90 } else { | |
| 91 // Running in a test, skip requesting the access token, straight away | |
| 92 // start our query. | |
| 93 StartThrottledQuery(base::Bind(&PeopleProvider::StartQuery, | |
| 94 base::Unretained(this))); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 void PeopleProvider::Stop() { | |
| 99 if (people_search_) | |
| 100 people_search_->Stop(); | |
| 101 } | |
| 102 | |
| 103 void PeopleProvider::OnGetTokenSuccess( | |
| 104 const OAuth2TokenService::Request* request, | |
| 105 const std::string& access_token, | |
| 106 const base::Time& expiration_time) { | |
| 107 DCHECK_EQ(access_token_request_.get(), request); | |
| 108 access_token_request_.reset(); | |
| 109 access_token_ = access_token; | |
| 110 StartQuery(); | |
| 111 } | |
| 112 | |
| 113 void PeopleProvider::OnGetTokenFailure( | |
| 114 const OAuth2TokenService::Request* request, | |
| 115 const GoogleServiceAuthError& error) { | |
| 116 DCHECK_EQ(access_token_request_.get(), request); | |
| 117 access_token_request_.reset(); | |
| 118 } | |
| 119 | |
| 120 void PeopleProvider::RequestAccessToken() { | |
| 121 // Only one active request at a time. | |
| 122 if (access_token_request_ != NULL) | |
| 123 return; | |
| 124 | |
| 125 SigninManagerBase* signin_manager = | |
| 126 SigninManagerFactory::GetInstance()->GetForProfile(profile_); | |
| 127 if (signin_manager->IsAuthenticated()) { | |
| 128 ProfileOAuth2TokenService* token_service = | |
| 129 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 130 access_token_request_ = token_service->StartRequest( | |
| 131 signin_manager->GetAuthenticatedAccountId(), oauth2_scope_, this); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 GURL PeopleProvider::GetQueryUrl(const std::string& query) { | |
| 136 GURL people_search_url = people_search_url_; | |
| 137 people_search_url = net::AppendQueryParameter(people_search_url, | |
| 138 kAccessTokenField, | |
| 139 access_token_); | |
| 140 people_search_url = net::AppendQueryParameter(people_search_url, | |
| 141 kQueryField, | |
| 142 query); | |
| 143 | |
| 144 return people_search_url; | |
| 145 } | |
| 146 | |
| 147 void PeopleProvider::StartQuery() { | |
| 148 // |query_| can be NULL when the query is scheduled but then canceled. | |
| 149 if (!people_search_ || query_.empty()) | |
| 150 return; | |
| 151 | |
| 152 GURL url = GetQueryUrl(query_); | |
| 153 people_search_->Start(url); | |
| 154 } | |
| 155 | |
| 156 void PeopleProvider::OnPeopleSearchFetched( | |
| 157 std::unique_ptr<base::DictionaryValue> json) { | |
| 158 ProcessPeopleSearchResults(json.get()); | |
| 159 cache_->Put(WebserviceCache::PEOPLE, query_, std::move(json)); | |
| 160 | |
| 161 if (!people_search_fetched_callback_.is_null()) | |
| 162 people_search_fetched_callback_.Run(); | |
| 163 } | |
| 164 | |
| 165 void PeopleProvider::ProcessPeopleSearchResults( | |
| 166 const base::DictionaryValue* json) { | |
| 167 const base::ListValue* item_list = NULL; | |
| 168 if (!json || | |
| 169 !json->GetList(kKeyItems, &item_list) || | |
| 170 !item_list || | |
| 171 item_list->empty()) { | |
| 172 return; | |
| 173 } | |
| 174 | |
| 175 ClearResults(); | |
| 176 for (base::ListValue::const_iterator it = item_list->begin(); | |
| 177 it != item_list->end(); | |
| 178 ++it) { | |
| 179 const base::DictionaryValue* dict; | |
| 180 if (!(*it)->GetAsDictionary(&dict)) | |
| 181 continue; | |
| 182 | |
| 183 std::unique_ptr<SearchResult> result(CreateResult(*dict)); | |
| 184 if (!result) | |
| 185 continue; | |
| 186 | |
| 187 Add(std::move(result)); | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 std::unique_ptr<SearchResult> PeopleProvider::CreateResult( | |
| 192 const base::DictionaryValue& dict) { | |
| 193 std::unique_ptr<SearchResult> result; | |
| 194 | |
| 195 std::unique_ptr<Person> person = Person::Create(dict); | |
| 196 if (!person) | |
| 197 return result; | |
| 198 | |
| 199 result.reset(new PeopleResult(profile_, controller_, std::move(person))); | |
| 200 return result; | |
| 201 } | |
| 202 | |
| 203 void PeopleProvider::SetupForTest( | |
| 204 const base::Closure& people_search_fetched_callback, | |
| 205 const GURL& people_search_url) { | |
| 206 people_search_fetched_callback_ = people_search_fetched_callback; | |
| 207 people_search_url_ = people_search_url; | |
| 208 skip_request_token_for_test_ = true; | |
| 209 } | |
| 210 | |
| 211 } // namespace app_list | |
| OLD | NEW |