Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(520)

Side by Side Diff: chrome/browser/profiles/profile_downloader.cc

Issue 257773002: Use new people.get api instead of oauth2/v1/userinfo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix PD tests Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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/profiles/profile_downloader.h" 5 #include "chrome/browser/profiles/profile_downloader.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 29 matching lines...) Expand all
40 40
41 // URL requesting user info. 41 // URL requesting user info.
42 const char kUserEntryURL[] = 42 const char kUserEntryURL[] =
43 "https://www.googleapis.com/oauth2/v1/userinfo?alt=json"; 43 "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
44 44
45 // OAuth scope for the user info API. 45 // OAuth scope for the user info API.
46 // For more info, see https://developers.google.com/accounts/docs/OAuth2LoginV1. 46 // For more info, see https://developers.google.com/accounts/docs/OAuth2LoginV1.
47 const char kAPIScope[] = "https://www.googleapis.com/auth/userinfo.profile"; 47 const char kAPIScope[] = "https://www.googleapis.com/auth/userinfo.profile";
48 48
49 // Path in JSON dictionary to user's photo thumbnail URL. 49 // Path in JSON dictionary to user's photo thumbnail URL.
50 const char kPhotoThumbnailURLPath[] = "picture"; 50 const char kPhotoThumbnailURLPath[] = "image.url";
51 51
52 // From the user info API, this field corresponds to the full name of the user. 52 // From the user info API, this field corresponds to the full name of the user.
53 const char kFullNamePath[] = "name"; 53 const char kFullNamePath[] = "displayName";
54 54
55 const char kGivenNamePath[] = "given_name"; 55 const char kGivenNamePath[] = "name.givenName";
56 56
57 // Path in JSON dictionary to user's preferred locale. 57 // Path in JSON dictionary to user's preferred locale.
58 const char kLocalePath[] = "locale"; 58 const char kLocalePath[] = "language";
59 59
60 // Path format for specifying thumbnail's size. 60 // Path format for specifying thumbnail's size.
61 const char kThumbnailSizeFormat[] = "s%d-c"; 61 const char kThumbnailSizeFormat[] = "s%d-c";
62 // Default thumbnail size. 62 // Default thumbnail size.
63 const int kDefaultThumbnailSize = 64; 63 const int kDefaultThumbnailSize = 64;
64 64
65 // Separator of URL path components. 65 // Separator of URL path components.
66 const char kURLPathSeparator = '/'; 66 const char kURLPathSeparator = '/';
67 67
68 // Photo ID of the Picasa Web Albums profile picture (base64 of 0). 68 // Photo ID of the Picasa Web Albums profile picture (base64 of 0).
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 // We can't set the image size, just use the default size. 126 // We can't set the image size, just use the default size.
127 *new_url = old_url; 127 *new_url = old_url;
128 return true; 128 return true;
129 } 129 }
130 130
131 } // namespace 131 } // namespace
132 132
133 // Parses the entry response and gets the name and profile image URL. 133 // Parses the entry response and gets the name and profile image URL.
134 // |data| should be the JSON formatted data return by the response. 134 // |data| should be the JSON formatted data return by the response.
135 // Returns false to indicate a parsing error. 135 // Returns false to indicate a parsing error.
136 bool ProfileDownloader::ParseProfileJSON(const std::string& data, 136 bool ProfileDownloader::ParseProfileJSON(base::DictionaryValue* root_dictionary,
137 base::string16* full_name, 137 base::string16* full_name,
138 base::string16* given_name, 138 base::string16* given_name,
139 std::string* url, 139 std::string* url,
140 int image_size, 140 int image_size,
141 std::string* profile_locale) { 141 std::string* profile_locale) {
142 DCHECK(full_name); 142 DCHECK(full_name);
143 DCHECK(given_name); 143 DCHECK(given_name);
144 DCHECK(url); 144 DCHECK(url);
145 DCHECK(profile_locale); 145 DCHECK(profile_locale);
146 146
147 *full_name = base::string16(); 147 *full_name = base::string16();
148 *given_name = base::string16(); 148 *given_name = base::string16();
149 *url = std::string(); 149 *url = std::string();
150 *profile_locale = std::string(); 150 *profile_locale = std::string();
151 151
152 int error_code = -1;
153 std::string error_message;
154 scoped_ptr<base::Value> root_value(base::JSONReader::ReadAndReturnError(
155 data, base::JSON_PARSE_RFC, &error_code, &error_message));
156 if (!root_value) {
157 LOG(ERROR) << "Error while parsing user entry response: "
158 << error_message;
159 return false;
160 }
161 if (!root_value->IsType(base::Value::TYPE_DICTIONARY)) {
162 LOG(ERROR) << "JSON root is not a dictionary: "
163 << root_value->GetType();
164 return false;
165 }
166 base::DictionaryValue* root_dictionary =
167 static_cast<base::DictionaryValue*>(root_value.get());
168
169 root_dictionary->GetString(kFullNamePath, full_name); 152 root_dictionary->GetString(kFullNamePath, full_name);
170 root_dictionary->GetString(kGivenNamePath, given_name); 153 root_dictionary->GetString(kGivenNamePath, given_name);
171 root_dictionary->GetString(kLocalePath, profile_locale); 154 root_dictionary->GetString(kLocalePath, profile_locale);
172 155
173 std::string url_string; 156 std::string url_string;
174 if (root_dictionary->GetString(kPhotoThumbnailURLPath, &url_string)) { 157 if (root_dictionary->GetString(kPhotoThumbnailURLPath, &url_string)) {
175 GURL new_url; 158 GURL new_url;
176 if (!GetImageURLWithSize(GURL(url_string), image_size, &new_url)) { 159 if (!GetImageURLWithSize(GURL(url_string), image_size, &new_url)) {
177 LOG(ERROR) << "GetImageURLWithSize failed for url: " << url_string; 160 LOG(ERROR) << "GetImageURLWithSize failed for url: " << url_string;
178 return false; 161 return false;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 const { 250 const {
268 return picture_status_; 251 return picture_status_;
269 } 252 }
270 253
271 std::string ProfileDownloader::GetProfilePictureURL() const { 254 std::string ProfileDownloader::GetProfilePictureURL() const {
272 return picture_url_; 255 return picture_url_;
273 } 256 }
274 257
275 void ProfileDownloader::StartFetchingImage() { 258 void ProfileDownloader::StartFetchingImage() {
276 VLOG(1) << "Fetching user entry with token: " << auth_token_; 259 VLOG(1) << "Fetching user entry with token: " << auth_token_;
277 user_entry_fetcher_.reset(net::URLFetcher::Create( 260 gaia_client_.reset(new gaia::GaiaOAuthClient(
278 GURL(kUserEntryURL), net::URLFetcher::GET, this)); 261 delegate_->GetBrowserProfile()->GetRequestContext()));
279 user_entry_fetcher_->SetRequestContext( 262 gaia_client_->GetUserInfo(auth_token_, 0, this);
280 delegate_->GetBrowserProfile()->GetRequestContext());
281 user_entry_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
282 net::LOAD_DO_NOT_SAVE_COOKIES);
283 if (!auth_token_.empty()) {
284 user_entry_fetcher_->SetExtraRequestHeaders(
285 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str()));
noms (inactive) 2014/04/25 17:07:40 So here we check whether the token is empty, and a
Roger Tawa OOO till Jul 10th 2014/04/28 20:44:12 Yup. In fact, this can't work with an empty token
286 }
287 user_entry_fetcher_->Start();
288 } 263 }
289 264
290 void ProfileDownloader::StartFetchingOAuth2AccessToken() { 265 void ProfileDownloader::StartFetchingOAuth2AccessToken() {
291 Profile* profile = delegate_->GetBrowserProfile(); 266 Profile* profile = delegate_->GetBrowserProfile();
292 OAuth2TokenService::ScopeSet scopes; 267 OAuth2TokenService::ScopeSet scopes;
293 scopes.insert(kAPIScope); 268 scopes.insert(kAPIScope);
294 ProfileOAuth2TokenService* token_service = 269 ProfileOAuth2TokenService* token_service =
295 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); 270 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
296 oauth2_access_token_request_ = token_service->StartRequest( 271 oauth2_access_token_request_ = token_service->StartRequest(
297 account_id_, scopes, this); 272 account_id_, scopes, this);
298 } 273 }
299 274
300 ProfileDownloader::~ProfileDownloader() { 275 ProfileDownloader::~ProfileDownloader() {
301 // Ensures PO2TS observation is cleared when ProfileDownloader is destructed 276 // Ensures PO2TS observation is cleared when ProfileDownloader is destructed
302 // before refresh token is available. 277 // before refresh token is available.
303 ProfileOAuth2TokenService* service = 278 ProfileOAuth2TokenService* service =
304 ProfileOAuth2TokenServiceFactory::GetForProfile( 279 ProfileOAuth2TokenServiceFactory::GetForProfile(
305 delegate_->GetBrowserProfile()); 280 delegate_->GetBrowserProfile());
306 if (service) 281 if (service)
307 service->RemoveObserver(this); 282 service->RemoveObserver(this);
308 } 283 }
309 284
310 void ProfileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { 285 void ProfileDownloader::OnGetUserInfoResponse(
311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 286 scoped_ptr<base::DictionaryValue> user_info) {
312 std::string data;
313 source->GetResponseAsString(&data);
314 bool network_error =
315 source->GetStatus().status() != net::URLRequestStatus::SUCCESS;
316 if (network_error || source->GetResponseCode() != 200) {
317 LOG(WARNING) << "Fetching profile data failed";
318 DVLOG(1) << " Status: " << source->GetStatus().status();
319 DVLOG(1) << " Error: " << source->GetStatus().error();
320 DVLOG(1) << " Response code: " << source->GetResponseCode();
321 DVLOG(1) << " Url: " << source->GetURL().spec();
322 delegate_->OnProfileDownloadFailure(this, network_error ?
323 ProfileDownloaderDelegate::NETWORK_ERROR :
324 ProfileDownloaderDelegate::SERVICE_ERROR);
325 return;
326 }
327
328 if (source == user_entry_fetcher_.get()) {
329 std::string image_url; 287 std::string image_url;
330 if (!ParseProfileJSON(data, 288 if (!ParseProfileJSON(user_info.get(),
331 &profile_full_name_, 289 &profile_full_name_,
332 &profile_given_name_, 290 &profile_given_name_,
333 &image_url, 291 &image_url,
334 delegate_->GetDesiredImageSideLength(), 292 delegate_->GetDesiredImageSideLength(),
335 &profile_locale_)) { 293 &profile_locale_)) {
336 delegate_->OnProfileDownloadFailure( 294 delegate_->OnProfileDownloadFailure(
337 this, ProfileDownloaderDelegate::SERVICE_ERROR); 295 this, ProfileDownloaderDelegate::SERVICE_ERROR);
338 return; 296 return;
339 } 297 }
340 if (!delegate_->NeedsProfilePicture()) { 298 if (!delegate_->NeedsProfilePicture()) {
(...skipping 19 matching lines...) Expand all
360 GURL(image_url), net::URLFetcher::GET, this)); 318 GURL(image_url), net::URLFetcher::GET, this));
361 profile_image_fetcher_->SetRequestContext( 319 profile_image_fetcher_->SetRequestContext(
362 delegate_->GetBrowserProfile()->GetRequestContext()); 320 delegate_->GetBrowserProfile()->GetRequestContext());
363 profile_image_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 321 profile_image_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
364 net::LOAD_DO_NOT_SAVE_COOKIES); 322 net::LOAD_DO_NOT_SAVE_COOKIES);
365 if (!auth_token_.empty()) { 323 if (!auth_token_.empty()) {
366 profile_image_fetcher_->SetExtraRequestHeaders( 324 profile_image_fetcher_->SetExtraRequestHeaders(
367 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str())); 325 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str()));
368 } 326 }
369 profile_image_fetcher_->Start(); 327 profile_image_fetcher_->Start();
370 } else if (source == profile_image_fetcher_.get()) { 328 }
371 VLOG(1) << "Decoding the image..."; 329
372 scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder( 330 void ProfileDownloader::OnOAuthError() {
373 this, data, ImageDecoder::DEFAULT_CODEC); 331 LOG(WARNING) << "OnOAuthError: Fetching profile data failed";
374 scoped_refptr<base::MessageLoopProxy> task_runner = 332 delegate_->OnProfileDownloadFailure(
375 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); 333 this, ProfileDownloaderDelegate::SERVICE_ERROR);
376 image_decoder->Start(task_runner); 334 }
335
336 void ProfileDownloader::OnNetworkError(int response_code) {
337 LOG(WARNING) << "OnNetworkError: Fetching profile data failed";
338 DVLOG(1) << " Response code: " << response_code;
339 delegate_->OnProfileDownloadFailure(
340 this, ProfileDownloaderDelegate::NETWORK_ERROR);
341 }
342
343 void ProfileDownloader::OnURLFetchComplete(const net::URLFetcher* source) {
344 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
345 std::string data;
346 source->GetResponseAsString(&data);
347 bool network_error =
348 source->GetStatus().status() != net::URLRequestStatus::SUCCESS;
349 if (network_error || source->GetResponseCode() != 200) {
350 LOG(WARNING) << "Fetching profile data failed";
351 DVLOG(1) << " Status: " << source->GetStatus().status();
352 DVLOG(1) << " Error: " << source->GetStatus().error();
353 DVLOG(1) << " Response code: " << source->GetResponseCode();
354 DVLOG(1) << " Url: " << source->GetURL().spec();
355 delegate_->OnProfileDownloadFailure(this, network_error ?
356 ProfileDownloaderDelegate::NETWORK_ERROR :
357 ProfileDownloaderDelegate::SERVICE_ERROR);
358 return;
377 } 359 }
360
361 VLOG(1) << "Decoding the image...";
362 scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder(
363 this, data, ImageDecoder::DEFAULT_CODEC);
364 scoped_refptr<base::MessageLoopProxy> task_runner =
365 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
366 image_decoder->Start(task_runner);
378 } 367 }
379 368
380 void ProfileDownloader::OnImageDecoded(const ImageDecoder* decoder, 369 void ProfileDownloader::OnImageDecoded(const ImageDecoder* decoder,
381 const SkBitmap& decoded_image) { 370 const SkBitmap& decoded_image) {
382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 371 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
383 int image_size = delegate_->GetDesiredImageSideLength(); 372 int image_size = delegate_->GetDesiredImageSideLength();
384 profile_picture_ = skia::ImageOperations::Resize( 373 profile_picture_ = skia::ImageOperations::Resize(
385 decoded_image, 374 decoded_image,
386 skia::ImageOperations::RESIZE_BEST, 375 skia::ImageOperations::RESIZE_BEST,
387 image_size, 376 image_size,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 void ProfileDownloader::OnGetTokenFailure( 412 void ProfileDownloader::OnGetTokenFailure(
424 const OAuth2TokenService::Request* request, 413 const OAuth2TokenService::Request* request,
425 const GoogleServiceAuthError& error) { 414 const GoogleServiceAuthError& error) {
426 DCHECK_EQ(request, oauth2_access_token_request_.get()); 415 DCHECK_EQ(request, oauth2_access_token_request_.get());
427 oauth2_access_token_request_.reset(); 416 oauth2_access_token_request_.reset();
428 LOG(WARNING) << "ProfileDownloader: token request using refresh token failed:" 417 LOG(WARNING) << "ProfileDownloader: token request using refresh token failed:"
429 << error.ToString(); 418 << error.ToString();
430 delegate_->OnProfileDownloadFailure( 419 delegate_->OnProfileDownloadFailure(
431 this, ProfileDownloaderDelegate::TOKEN_ERROR); 420 this, ProfileDownloaderDelegate::TOKEN_ERROR);
432 } 421 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698