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