Index: chrome/browser/profiles/profile_downloader.cc |
=================================================================== |
--- chrome/browser/profiles/profile_downloader.cc (revision 280060) |
+++ chrome/browser/profiles/profile_downloader.cc (working copy) |
@@ -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 kFullNamePath[] = "name"; |
-const char kGivenNamePath[] = "name.givenName"; |
+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( |
@@ -275,10 +307,27 @@ |
service->RemoveObserver(this); |
} |
-void ProfileDownloader::OnGetUserInfoResponse( |
- scoped_ptr<base::DictionaryValue> user_info) { |
+void ProfileDownloader::OnURLFetchComplete(const net::URLFetcher* source) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ std::string data; |
+ source->GetResponseAsString(&data); |
+ bool network_error = |
+ source->GetStatus().status() != net::URLRequestStatus::SUCCESS; |
+ if (network_error || source->GetResponseCode() != 200) { |
+ LOG(WARNING) << "Fetching profile data failed"; |
+ DVLOG(1) << " Status: " << source->GetStatus().status(); |
+ DVLOG(1) << " Error: " << source->GetStatus().error(); |
+ DVLOG(1) << " Response code: " << source->GetResponseCode(); |
+ DVLOG(1) << " Url: " << source->GetURL().spec(); |
+ delegate_->OnProfileDownloadFailure(this, network_error ? |
+ ProfileDownloaderDelegate::NETWORK_ERROR : |
+ ProfileDownloaderDelegate::SERVICE_ERROR); |
+ return; |
+ } |
+ |
+ if (source == user_entry_fetcher_.get()) { |
std::string image_url; |
- if (!ParseProfileJSON(user_info.get(), |
+ if (!ParseProfileJSON(data, |
&profile_full_name_, |
&profile_given_name_, |
&image_url, |
@@ -318,45 +367,14 @@ |
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) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- std::string data; |
- source->GetResponseAsString(&data); |
- bool network_error = |
- source->GetStatus().status() != net::URLRequestStatus::SUCCESS; |
- if (network_error || source->GetResponseCode() != 200) { |
- LOG(WARNING) << "Fetching profile data failed"; |
- DVLOG(1) << " Status: " << source->GetStatus().status(); |
- DVLOG(1) << " Error: " << source->GetStatus().error(); |
- DVLOG(1) << " Response code: " << source->GetResponseCode(); |
- DVLOG(1) << " Url: " << source->GetURL().spec(); |
- delegate_->OnProfileDownloadFailure(this, network_error ? |
- ProfileDownloaderDelegate::NETWORK_ERROR : |
- ProfileDownloaderDelegate::SERVICE_ERROR); |
- return; |
+ } 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); |
} |
- |
- 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, |