| OLD | NEW |
| 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 20 matching lines...) Expand all Loading... |
| 31 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 32 | 32 |
| 33 using content::BrowserThread; | 33 using content::BrowserThread; |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 // Template for optional authorization header when using an OAuth access token. | 37 // Template for optional authorization header when using an OAuth access token. |
| 38 const char kAuthorizationHeader[] = | 38 const char kAuthorizationHeader[] = |
| 39 "Authorization: Bearer %s"; | 39 "Authorization: Bearer %s"; |
| 40 | 40 |
| 41 // URL requesting user info. | |
| 42 const char kUserEntryURL[] = | |
| 43 "https://www.googleapis.com/oauth2/v1/userinfo?alt=json"; | |
| 44 | |
| 45 // OAuth scope for the user info API. | |
| 46 // For more info, see https://developers.google.com/accounts/docs/OAuth2LoginV1. | |
| 47 const char kAPIScope[] = "https://www.googleapis.com/auth/userinfo.profile"; | |
| 48 | |
| 49 // Path in JSON dictionary to user's photo thumbnail URL. | 41 // Path in JSON dictionary to user's photo thumbnail URL. |
| 50 const char kPhotoThumbnailURLPath[] = "picture"; | 42 const char kPhotoThumbnailURLPath[] = "image.url"; |
| 51 | 43 |
| 52 // From the user info API, this field corresponds to the full name of the user. | 44 // From the user info API, this field corresponds to the full name of the user. |
| 53 const char kFullNamePath[] = "name"; | 45 const char kFullNamePath[] = "displayName"; |
| 54 | 46 |
| 55 const char kGivenNamePath[] = "given_name"; | 47 const char kGivenNamePath[] = "name.givenName"; |
| 56 | 48 |
| 57 // Path in JSON dictionary to user's preferred locale. | 49 // Path in JSON dictionary to user's preferred locale. |
| 58 const char kLocalePath[] = "locale"; | 50 const char kLocalePath[] = "language"; |
| 59 | 51 |
| 60 // Path format for specifying thumbnail's size. | 52 // Path format for specifying thumbnail's size. |
| 61 const char kThumbnailSizeFormat[] = "s%d-c"; | 53 const char kThumbnailSizeFormat[] = "s%d-c"; |
| 62 // Default thumbnail size. | 54 // Default thumbnail size. |
| 63 const int kDefaultThumbnailSize = 64; | 55 const int kDefaultThumbnailSize = 64; |
| 64 | 56 |
| 65 // Separator of URL path components. | 57 // Separator of URL path components. |
| 66 const char kURLPathSeparator = '/'; | 58 const char kURLPathSeparator = '/'; |
| 67 | 59 |
| 68 // Photo ID of the Picasa Web Albums profile picture (base64 of 0). | 60 // Photo ID of the Picasa Web Albums profile picture (base64 of 0). |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 // We can't set the image size, just use the default size. | 118 // We can't set the image size, just use the default size. |
| 127 *new_url = old_url; | 119 *new_url = old_url; |
| 128 return true; | 120 return true; |
| 129 } | 121 } |
| 130 | 122 |
| 131 } // namespace | 123 } // namespace |
| 132 | 124 |
| 133 // Parses the entry response and gets the name and profile image URL. | 125 // Parses the entry response and gets the name and profile image URL. |
| 134 // |data| should be the JSON formatted data return by the response. | 126 // |data| should be the JSON formatted data return by the response. |
| 135 // Returns false to indicate a parsing error. | 127 // Returns false to indicate a parsing error. |
| 136 bool ProfileDownloader::ParseProfileJSON(const std::string& data, | 128 bool ProfileDownloader::ParseProfileJSON(base::DictionaryValue* root_dictionary, |
| 137 base::string16* full_name, | 129 base::string16* full_name, |
| 138 base::string16* given_name, | 130 base::string16* given_name, |
| 139 std::string* url, | 131 std::string* url, |
| 140 int image_size, | 132 int image_size, |
| 141 std::string* profile_locale) { | 133 std::string* profile_locale) { |
| 142 DCHECK(full_name); | 134 DCHECK(full_name); |
| 143 DCHECK(given_name); | 135 DCHECK(given_name); |
| 144 DCHECK(url); | 136 DCHECK(url); |
| 145 DCHECK(profile_locale); | 137 DCHECK(profile_locale); |
| 146 | 138 |
| 147 *full_name = base::string16(); | 139 *full_name = base::string16(); |
| 148 *given_name = base::string16(); | 140 *given_name = base::string16(); |
| 149 *url = std::string(); | 141 *url = std::string(); |
| 150 *profile_locale = std::string(); | 142 *profile_locale = std::string(); |
| 151 | 143 |
| 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); | 144 root_dictionary->GetString(kFullNamePath, full_name); |
| 170 root_dictionary->GetString(kGivenNamePath, given_name); | 145 root_dictionary->GetString(kGivenNamePath, given_name); |
| 171 root_dictionary->GetString(kLocalePath, profile_locale); | 146 root_dictionary->GetString(kLocalePath, profile_locale); |
| 172 | 147 |
| 173 std::string url_string; | 148 std::string url_string; |
| 174 if (root_dictionary->GetString(kPhotoThumbnailURLPath, &url_string)) { | 149 if (root_dictionary->GetString(kPhotoThumbnailURLPath, &url_string)) { |
| 175 GURL new_url; | 150 GURL new_url; |
| 176 if (!GetImageURLWithSize(GURL(url_string), image_size, &new_url)) { | 151 if (!GetImageURLWithSize(GURL(url_string), image_size, &new_url)) { |
| 177 LOG(ERROR) << "GetImageURLWithSize failed for url: " << url_string; | 152 LOG(ERROR) << "GetImageURLWithSize failed for url: " << url_string; |
| 178 return false; | 153 return false; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 ProfileDownloader::PictureStatus ProfileDownloader::GetProfilePictureStatus() | 241 ProfileDownloader::PictureStatus ProfileDownloader::GetProfilePictureStatus() |
| 267 const { | 242 const { |
| 268 return picture_status_; | 243 return picture_status_; |
| 269 } | 244 } |
| 270 | 245 |
| 271 std::string ProfileDownloader::GetProfilePictureURL() const { | 246 std::string ProfileDownloader::GetProfilePictureURL() const { |
| 272 return picture_url_; | 247 return picture_url_; |
| 273 } | 248 } |
| 274 | 249 |
| 275 void ProfileDownloader::StartFetchingImage() { | 250 void ProfileDownloader::StartFetchingImage() { |
| 251 DCHECK(!auth_token_.empty()); |
| 276 VLOG(1) << "Fetching user entry with token: " << auth_token_; | 252 VLOG(1) << "Fetching user entry with token: " << auth_token_; |
| 277 user_entry_fetcher_.reset(net::URLFetcher::Create( | 253 gaia_client_.reset(new gaia::GaiaOAuthClient( |
| 278 GURL(kUserEntryURL), net::URLFetcher::GET, this)); | 254 delegate_->GetBrowserProfile()->GetRequestContext())); |
| 279 user_entry_fetcher_->SetRequestContext( | 255 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())); | |
| 286 } | |
| 287 user_entry_fetcher_->Start(); | |
| 288 } | 256 } |
| 289 | 257 |
| 290 void ProfileDownloader::StartFetchingOAuth2AccessToken() { | 258 void ProfileDownloader::StartFetchingOAuth2AccessToken() { |
| 291 Profile* profile = delegate_->GetBrowserProfile(); | 259 Profile* profile = delegate_->GetBrowserProfile(); |
| 292 OAuth2TokenService::ScopeSet scopes; | 260 OAuth2TokenService::ScopeSet scopes; |
| 293 scopes.insert(kAPIScope); | 261 scopes.insert(GaiaConstants::kGoogleUserInfoProfile); |
| 294 ProfileOAuth2TokenService* token_service = | 262 ProfileOAuth2TokenService* token_service = |
| 295 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 263 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 296 oauth2_access_token_request_ = token_service->StartRequest( | 264 oauth2_access_token_request_ = token_service->StartRequest( |
| 297 account_id_, scopes, this); | 265 account_id_, scopes, this); |
| 298 } | 266 } |
| 299 | 267 |
| 300 ProfileDownloader::~ProfileDownloader() { | 268 ProfileDownloader::~ProfileDownloader() { |
| 301 // Ensures PO2TS observation is cleared when ProfileDownloader is destructed | 269 // Ensures PO2TS observation is cleared when ProfileDownloader is destructed |
| 302 // before refresh token is available. | 270 // before refresh token is available. |
| 303 ProfileOAuth2TokenService* service = | 271 ProfileOAuth2TokenService* service = |
| 304 ProfileOAuth2TokenServiceFactory::GetForProfile( | 272 ProfileOAuth2TokenServiceFactory::GetForProfile( |
| 305 delegate_->GetBrowserProfile()); | 273 delegate_->GetBrowserProfile()); |
| 306 if (service) | 274 if (service) |
| 307 service->RemoveObserver(this); | 275 service->RemoveObserver(this); |
| 308 } | 276 } |
| 309 | 277 |
| 310 void ProfileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { | 278 void ProfileDownloader::OnGetUserInfoResponse( |
| 311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 279 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; | 280 std::string image_url; |
| 330 if (!ParseProfileJSON(data, | 281 if (!ParseProfileJSON(user_info.get(), |
| 331 &profile_full_name_, | 282 &profile_full_name_, |
| 332 &profile_given_name_, | 283 &profile_given_name_, |
| 333 &image_url, | 284 &image_url, |
| 334 delegate_->GetDesiredImageSideLength(), | 285 delegate_->GetDesiredImageSideLength(), |
| 335 &profile_locale_)) { | 286 &profile_locale_)) { |
| 336 delegate_->OnProfileDownloadFailure( | 287 delegate_->OnProfileDownloadFailure( |
| 337 this, ProfileDownloaderDelegate::SERVICE_ERROR); | 288 this, ProfileDownloaderDelegate::SERVICE_ERROR); |
| 338 return; | 289 return; |
| 339 } | 290 } |
| 340 if (!delegate_->NeedsProfilePicture()) { | 291 if (!delegate_->NeedsProfilePicture()) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 360 GURL(image_url), net::URLFetcher::GET, this)); | 311 GURL(image_url), net::URLFetcher::GET, this)); |
| 361 profile_image_fetcher_->SetRequestContext( | 312 profile_image_fetcher_->SetRequestContext( |
| 362 delegate_->GetBrowserProfile()->GetRequestContext()); | 313 delegate_->GetBrowserProfile()->GetRequestContext()); |
| 363 profile_image_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 314 profile_image_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 364 net::LOAD_DO_NOT_SAVE_COOKIES); | 315 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 365 if (!auth_token_.empty()) { | 316 if (!auth_token_.empty()) { |
| 366 profile_image_fetcher_->SetExtraRequestHeaders( | 317 profile_image_fetcher_->SetExtraRequestHeaders( |
| 367 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str())); | 318 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str())); |
| 368 } | 319 } |
| 369 profile_image_fetcher_->Start(); | 320 profile_image_fetcher_->Start(); |
| 370 } else if (source == profile_image_fetcher_.get()) { | 321 } |
| 371 VLOG(1) << "Decoding the image..."; | 322 |
| 372 scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder( | 323 void ProfileDownloader::OnOAuthError() { |
| 373 this, data, ImageDecoder::DEFAULT_CODEC); | 324 LOG(WARNING) << "OnOAuthError: Fetching profile data failed"; |
| 374 scoped_refptr<base::MessageLoopProxy> task_runner = | 325 delegate_->OnProfileDownloadFailure( |
| 375 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | 326 this, ProfileDownloaderDelegate::SERVICE_ERROR); |
| 376 image_decoder->Start(task_runner); | 327 } |
| 328 |
| 329 void ProfileDownloader::OnNetworkError(int response_code) { |
| 330 LOG(WARNING) << "OnNetworkError: Fetching profile data failed"; |
| 331 DVLOG(1) << " Response code: " << response_code; |
| 332 delegate_->OnProfileDownloadFailure( |
| 333 this, ProfileDownloaderDelegate::NETWORK_ERROR); |
| 334 } |
| 335 |
| 336 void ProfileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { |
| 337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 338 std::string data; |
| 339 source->GetResponseAsString(&data); |
| 340 bool network_error = |
| 341 source->GetStatus().status() != net::URLRequestStatus::SUCCESS; |
| 342 if (network_error || source->GetResponseCode() != 200) { |
| 343 LOG(WARNING) << "Fetching profile data failed"; |
| 344 DVLOG(1) << " Status: " << source->GetStatus().status(); |
| 345 DVLOG(1) << " Error: " << source->GetStatus().error(); |
| 346 DVLOG(1) << " Response code: " << source->GetResponseCode(); |
| 347 DVLOG(1) << " Url: " << source->GetURL().spec(); |
| 348 delegate_->OnProfileDownloadFailure(this, network_error ? |
| 349 ProfileDownloaderDelegate::NETWORK_ERROR : |
| 350 ProfileDownloaderDelegate::SERVICE_ERROR); |
| 351 return; |
| 377 } | 352 } |
| 353 |
| 354 VLOG(1) << "Decoding the image..."; |
| 355 scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder( |
| 356 this, data, ImageDecoder::DEFAULT_CODEC); |
| 357 scoped_refptr<base::MessageLoopProxy> task_runner = |
| 358 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
| 359 image_decoder->Start(task_runner); |
| 378 } | 360 } |
| 379 | 361 |
| 380 void ProfileDownloader::OnImageDecoded(const ImageDecoder* decoder, | 362 void ProfileDownloader::OnImageDecoded(const ImageDecoder* decoder, |
| 381 const SkBitmap& decoded_image) { | 363 const SkBitmap& decoded_image) { |
| 382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 364 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 383 int image_size = delegate_->GetDesiredImageSideLength(); | 365 int image_size = delegate_->GetDesiredImageSideLength(); |
| 384 profile_picture_ = skia::ImageOperations::Resize( | 366 profile_picture_ = skia::ImageOperations::Resize( |
| 385 decoded_image, | 367 decoded_image, |
| 386 skia::ImageOperations::RESIZE_BEST, | 368 skia::ImageOperations::RESIZE_BEST, |
| 387 image_size, | 369 image_size, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 void ProfileDownloader::OnGetTokenFailure( | 405 void ProfileDownloader::OnGetTokenFailure( |
| 424 const OAuth2TokenService::Request* request, | 406 const OAuth2TokenService::Request* request, |
| 425 const GoogleServiceAuthError& error) { | 407 const GoogleServiceAuthError& error) { |
| 426 DCHECK_EQ(request, oauth2_access_token_request_.get()); | 408 DCHECK_EQ(request, oauth2_access_token_request_.get()); |
| 427 oauth2_access_token_request_.reset(); | 409 oauth2_access_token_request_.reset(); |
| 428 LOG(WARNING) << "ProfileDownloader: token request using refresh token failed:" | 410 LOG(WARNING) << "ProfileDownloader: token request using refresh token failed:" |
| 429 << error.ToString(); | 411 << error.ToString(); |
| 430 delegate_->OnProfileDownloadFailure( | 412 delegate_->OnProfileDownloadFailure( |
| 431 this, ProfileDownloaderDelegate::TOKEN_ERROR); | 413 this, ProfileDownloaderDelegate::TOKEN_ERROR); |
| 432 } | 414 } |
| OLD | NEW |