Index: chrome/browser/profiles/profile_downloader.cc |
diff --git a/chrome/browser/profiles/profile_downloader.cc b/chrome/browser/profiles/profile_downloader.cc |
index 5d08ffe0fdcbba5813a34c3b0dbe266752fe688c..15e8ceef496f45cb3a092c4c16b27a61c899b0d7 100644 |
--- a/chrome/browser/profiles/profile_downloader.cc |
+++ b/chrome/browser/profiles/profile_downloader.cc |
@@ -38,16 +38,24 @@ |
const char kAuthorizationHeader[] = |
"Authorization: Bearer %s"; |
+// URL requesting user info. |
+const char kUserEntryURL[] = |
+ "https://www.googleapis.com/oauth2/v1/userinfo?alt=json"; |
+ |
+// OAuth scope for the user info API. |
+// For more info, see https://developers.google.com/accounts/docs/OAuth2LoginV1. |
+const char kAPIScope[] = "https://www.googleapis.com/auth/userinfo.profile"; |
+ |
// Path in JSON dictionary to user's photo thumbnail URL. |
-const char kPhotoThumbnailURLPath[] = "image.url"; |
+const char kPhotoThumbnailURLPath[] = "picture"; |
// From the user info API, this field corresponds to the full name of the user. |
-const char kFullNamePath[] = "displayName"; |
- |
-const char kGivenNamePath[] = "name.givenName"; |
+const char kFullNamePath[] = "name"; |
+ |
+const char kGivenNamePath[] = "given_name"; |
// Path in JSON dictionary to user's preferred locale. |
-const char kLocalePath[] = "language"; |
+const char kLocalePath[] = "locale"; |
// Path format for specifying thumbnail's size. |
const char kThumbnailSizeFormat[] = "s%d-c"; |
@@ -125,7 +133,7 @@ |
// Parses the entry response and gets the name and profile image URL. |
// |data| should be the JSON formatted data return by the response. |
// Returns false to indicate a parsing error. |
-bool ProfileDownloader::ParseProfileJSON(base::DictionaryValue* root_dictionary, |
+bool ProfileDownloader::ParseProfileJSON(const std::string& data, |
base::string16* full_name, |
base::string16* given_name, |
std::string* url, |
@@ -141,6 +149,23 @@ |
*url = std::string(); |
*profile_locale = std::string(); |
+ int error_code = -1; |
+ std::string error_message; |
+ scoped_ptr<base::Value> root_value(base::JSONReader::ReadAndReturnError( |
+ data, base::JSON_PARSE_RFC, &error_code, &error_message)); |
+ if (!root_value) { |
+ LOG(ERROR) << "Error while parsing user entry response: " |
+ << error_message; |
+ return false; |
+ } |
+ if (!root_value->IsType(base::Value::TYPE_DICTIONARY)) { |
+ LOG(ERROR) << "JSON root is not a dictionary: " |
+ << root_value->GetType(); |
+ return false; |
+ } |
+ base::DictionaryValue* root_dictionary = |
+ static_cast<base::DictionaryValue*>(root_value.get()); |
+ |
root_dictionary->GetString(kFullNamePath, full_name); |
root_dictionary->GetString(kGivenNamePath, given_name); |
root_dictionary->GetString(kLocalePath, profile_locale); |
@@ -248,17 +273,24 @@ |
} |
void ProfileDownloader::StartFetchingImage() { |
- DCHECK(!auth_token_.empty()); |
VLOG(1) << "Fetching user entry with token: " << auth_token_; |
- gaia_client_.reset(new gaia::GaiaOAuthClient( |
- delegate_->GetBrowserProfile()->GetRequestContext())); |
- gaia_client_->GetUserInfo(auth_token_, 0, this); |
+ user_entry_fetcher_.reset(net::URLFetcher::Create( |
+ GURL(kUserEntryURL), net::URLFetcher::GET, this)); |
+ user_entry_fetcher_->SetRequestContext( |
+ delegate_->GetBrowserProfile()->GetRequestContext()); |
+ user_entry_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
+ net::LOAD_DO_NOT_SAVE_COOKIES); |
+ if (!auth_token_.empty()) { |
+ user_entry_fetcher_->SetExtraRequestHeaders( |
+ base::StringPrintf(kAuthorizationHeader, auth_token_.c_str())); |
+ } |
+ user_entry_fetcher_->Start(); |
} |
void ProfileDownloader::StartFetchingOAuth2AccessToken() { |
Profile* profile = delegate_->GetBrowserProfile(); |
OAuth2TokenService::ScopeSet scopes; |
- scopes.insert(GaiaConstants::kGoogleUserInfoProfile); |
+ scopes.insert(kAPIScope); |
ProfileOAuth2TokenService* token_service = |
ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
oauth2_access_token_request_ = token_service->StartRequest( |
@@ -273,64 +305,6 @@ |
delegate_->GetBrowserProfile()); |
if (service) |
service->RemoveObserver(this); |
-} |
- |
-void ProfileDownloader::OnGetUserInfoResponse( |
- scoped_ptr<base::DictionaryValue> user_info) { |
- std::string image_url; |
- if (!ParseProfileJSON(user_info.get(), |
- &profile_full_name_, |
- &profile_given_name_, |
- &image_url, |
- delegate_->GetDesiredImageSideLength(), |
- &profile_locale_)) { |
- delegate_->OnProfileDownloadFailure( |
- this, ProfileDownloaderDelegate::SERVICE_ERROR); |
- return; |
- } |
- if (!delegate_->NeedsProfilePicture()) { |
- VLOG(1) << "Skipping profile picture download"; |
- delegate_->OnProfileDownloadSuccess(this); |
- return; |
- } |
- if (IsDefaultProfileImageURL(image_url)) { |
- VLOG(1) << "User has default profile picture"; |
- picture_status_ = PICTURE_DEFAULT; |
- delegate_->OnProfileDownloadSuccess(this); |
- return; |
- } |
- if (!image_url.empty() && image_url == delegate_->GetCachedPictureURL()) { |
- VLOG(1) << "Picture URL matches cached picture URL"; |
- picture_status_ = PICTURE_CACHED; |
- delegate_->OnProfileDownloadSuccess(this); |
- return; |
- } |
- VLOG(1) << "Fetching profile image from " << image_url; |
- picture_url_ = image_url; |
- profile_image_fetcher_.reset(net::URLFetcher::Create( |
- GURL(image_url), net::URLFetcher::GET, this)); |
- profile_image_fetcher_->SetRequestContext( |
- delegate_->GetBrowserProfile()->GetRequestContext()); |
- profile_image_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
- net::LOAD_DO_NOT_SAVE_COOKIES); |
- if (!auth_token_.empty()) { |
- profile_image_fetcher_->SetExtraRequestHeaders( |
- base::StringPrintf(kAuthorizationHeader, auth_token_.c_str())); |
- } |
- profile_image_fetcher_->Start(); |
-} |
- |
-void ProfileDownloader::OnOAuthError() { |
- LOG(WARNING) << "OnOAuthError: Fetching profile data failed"; |
- delegate_->OnProfileDownloadFailure( |
- this, ProfileDownloaderDelegate::SERVICE_ERROR); |
-} |
- |
-void ProfileDownloader::OnNetworkError(int response_code) { |
- LOG(WARNING) << "OnNetworkError: Fetching profile data failed"; |
- DVLOG(1) << " Response code: " << response_code; |
- delegate_->OnProfileDownloadFailure( |
- this, ProfileDownloaderDelegate::NETWORK_ERROR); |
} |
void ProfileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { |
@@ -351,12 +325,56 @@ |
return; |
} |
- VLOG(1) << "Decoding the image..."; |
- scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder( |
- this, data, ImageDecoder::DEFAULT_CODEC); |
- scoped_refptr<base::MessageLoopProxy> task_runner = |
- BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
- image_decoder->Start(task_runner); |
+ if (source == user_entry_fetcher_.get()) { |
+ std::string image_url; |
+ if (!ParseProfileJSON(data, |
+ &profile_full_name_, |
+ &profile_given_name_, |
+ &image_url, |
+ delegate_->GetDesiredImageSideLength(), |
+ &profile_locale_)) { |
+ delegate_->OnProfileDownloadFailure( |
+ this, ProfileDownloaderDelegate::SERVICE_ERROR); |
+ return; |
+ } |
+ if (!delegate_->NeedsProfilePicture()) { |
+ VLOG(1) << "Skipping profile picture download"; |
+ delegate_->OnProfileDownloadSuccess(this); |
+ return; |
+ } |
+ if (IsDefaultProfileImageURL(image_url)) { |
+ VLOG(1) << "User has default profile picture"; |
+ picture_status_ = PICTURE_DEFAULT; |
+ delegate_->OnProfileDownloadSuccess(this); |
+ return; |
+ } |
+ if (!image_url.empty() && image_url == delegate_->GetCachedPictureURL()) { |
+ VLOG(1) << "Picture URL matches cached picture URL"; |
+ picture_status_ = PICTURE_CACHED; |
+ delegate_->OnProfileDownloadSuccess(this); |
+ return; |
+ } |
+ VLOG(1) << "Fetching profile image from " << image_url; |
+ picture_url_ = image_url; |
+ profile_image_fetcher_.reset(net::URLFetcher::Create( |
+ GURL(image_url), net::URLFetcher::GET, this)); |
+ profile_image_fetcher_->SetRequestContext( |
+ delegate_->GetBrowserProfile()->GetRequestContext()); |
+ profile_image_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
+ net::LOAD_DO_NOT_SAVE_COOKIES); |
+ if (!auth_token_.empty()) { |
+ profile_image_fetcher_->SetExtraRequestHeaders( |
+ base::StringPrintf(kAuthorizationHeader, auth_token_.c_str())); |
+ } |
+ profile_image_fetcher_->Start(); |
+ } else if (source == profile_image_fetcher_.get()) { |
+ VLOG(1) << "Decoding the image..."; |
+ scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder( |
+ this, data, ImageDecoder::DEFAULT_CODEC); |
+ scoped_refptr<base::MessageLoopProxy> task_runner = |
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
+ image_decoder->Start(task_runner); |
+ } |
} |
void ProfileDownloader::OnImageDecoded(const ImageDecoder* decoder, |