| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 16 matching lines...) Expand all Loading... |
| 27 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
| 28 #include "content/public/browser/notification_types.h" | 28 #include "content/public/browser/notification_types.h" |
| 29 #include "content/public/common/url_fetcher.h" | 29 #include "content/public/common/url_fetcher.h" |
| 30 #include "googleurl/src/gurl.h" | 30 #include "googleurl/src/gurl.h" |
| 31 #include "skia/ext/image_operations.h" | 31 #include "skia/ext/image_operations.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 the ClientLogin access | |
| 38 // token. | |
| 39 const char kClientAccessAuthorizationHeader[] = | |
| 40 "Authorization: GoogleLogin auth=%s"; | |
| 41 | |
| 42 // Template for optional authorization header when using an OAuth access token. | 37 // Template for optional authorization header when using an OAuth access token. |
| 43 const char kOAuthAccessAuthorizationHeader[] = | 38 const char kAuthorizationHeader[] = |
| 44 "Authorization: Bearer %s"; | 39 "Authorization: Bearer %s"; |
| 45 | 40 |
| 46 // URL requesting Picasa API for user info. | 41 // URL requesting Picasa API for user info. |
| 47 const char kUserEntryURL[] = | 42 const char kUserEntryURL[] = |
| 48 "http://picasaweb.google.com/data/entry/api/user/default?alt=json"; | 43 "http://picasaweb.google.com/data/entry/api/user/default?alt=json"; |
| 49 | 44 |
| 50 // OAuth scope for the Picasa API. | 45 // OAuth scope for the Picasa API. |
| 51 const char kPicasaScope[] = "http://picasaweb.google.com/data/"; | 46 const char kPicasaScope[] = "http://picasaweb.google.com/data/"; |
| 52 | 47 |
| 53 // Path in JSON dictionary to user's photo thumbnail URL. | 48 // Path in JSON dictionary to user's photo thumbnail URL. |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 195 | 190 |
| 196 TokenService* service = delegate_->GetBrowserProfile()->GetTokenService(); | 191 TokenService* service = delegate_->GetBrowserProfile()->GetTokenService(); |
| 197 if (!service) { | 192 if (!service) { |
| 198 // This can happen in some test paths. | 193 // This can happen in some test paths. |
| 199 LOG(WARNING) << "User has no token service"; | 194 LOG(WARNING) << "User has no token service"; |
| 200 delegate_->OnDownloadComplete(this, false); | 195 delegate_->OnDownloadComplete(this, false); |
| 201 return; | 196 return; |
| 202 } | 197 } |
| 203 | 198 |
| 204 if (delegate_->ShouldUseOAuthRefreshToken() && | 199 if (service->HasOAuthLoginToken()) { |
| 205 service->HasOAuthLoginToken()) { | |
| 206 StartFetchingOAuth2AccessToken(); | 200 StartFetchingOAuth2AccessToken(); |
| 207 } else if (!delegate_->ShouldUseOAuthRefreshToken() && | |
| 208 service->HasTokenForService(GaiaConstants::kPicasaService)) { | |
| 209 auth_token_ = | |
| 210 service->GetTokenForService(GaiaConstants::kPicasaService); | |
| 211 StartFetchingImage(); | |
| 212 } else { | 201 } else { |
| 213 registrar_.Add(this, | 202 registrar_.Add(this, |
| 214 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 203 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 215 content::Source<TokenService>(service)); | 204 content::Source<TokenService>(service)); |
| 216 registrar_.Add(this, | 205 registrar_.Add(this, |
| 217 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, | 206 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, |
| 218 content::Source<TokenService>(service)); | 207 content::Source<TokenService>(service)); |
| 219 } | 208 } |
| 220 } | 209 } |
| 221 | 210 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 237 } | 226 } |
| 238 | 227 |
| 239 void ProfileDownloader::StartFetchingImage() { | 228 void ProfileDownloader::StartFetchingImage() { |
| 240 VLOG(1) << "Fetching user entry with token: " << auth_token_; | 229 VLOG(1) << "Fetching user entry with token: " << auth_token_; |
| 241 user_entry_fetcher_.reset(content::URLFetcher::Create( | 230 user_entry_fetcher_.reset(content::URLFetcher::Create( |
| 242 GURL(kUserEntryURL), content::URLFetcher::GET, this)); | 231 GURL(kUserEntryURL), content::URLFetcher::GET, this)); |
| 243 user_entry_fetcher_->SetRequestContext( | 232 user_entry_fetcher_->SetRequestContext( |
| 244 delegate_->GetBrowserProfile()->GetRequestContext()); | 233 delegate_->GetBrowserProfile()->GetRequestContext()); |
| 245 if (!auth_token_.empty()) { | 234 if (!auth_token_.empty()) { |
| 246 user_entry_fetcher_->SetExtraRequestHeaders( | 235 user_entry_fetcher_->SetExtraRequestHeaders( |
| 247 base::StringPrintf(GetAuthorizationHeader(), auth_token_.c_str())); | 236 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str())); |
| 248 } | 237 } |
| 249 user_entry_fetcher_->Start(); | 238 user_entry_fetcher_->Start(); |
| 250 } | 239 } |
| 251 | 240 |
| 252 const char* ProfileDownloader::GetAuthorizationHeader() const { | |
| 253 return delegate_->ShouldUseOAuthRefreshToken() ? | |
| 254 kOAuthAccessAuthorizationHeader : kClientAccessAuthorizationHeader; | |
| 255 } | |
| 256 | |
| 257 void ProfileDownloader::StartFetchingOAuth2AccessToken() { | 241 void ProfileDownloader::StartFetchingOAuth2AccessToken() { |
| 258 TokenService* service = delegate_->GetBrowserProfile()->GetTokenService(); | 242 TokenService* service = delegate_->GetBrowserProfile()->GetTokenService(); |
| 259 DCHECK(!service->GetOAuth2LoginRefreshToken().empty()); | 243 DCHECK(!service->GetOAuth2LoginRefreshToken().empty()); |
| 260 | 244 |
| 261 std::vector<std::string> scopes; | 245 std::vector<std::string> scopes; |
| 262 scopes.push_back(kPicasaScope); | 246 scopes.push_back(kPicasaScope); |
| 263 oauth2_access_token_fetcher_.reset(new OAuth2AccessTokenFetcher( | 247 oauth2_access_token_fetcher_.reset(new OAuth2AccessTokenFetcher( |
| 264 this, delegate_->GetBrowserProfile()->GetRequestContext())); | 248 this, delegate_->GetBrowserProfile()->GetRequestContext())); |
| 265 oauth2_access_token_fetcher_->Start( | 249 oauth2_access_token_fetcher_->Start( |
| 266 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), | 250 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 return; | 286 return; |
| 303 } | 287 } |
| 304 VLOG(1) << "Fetching profile image from " << image_url; | 288 VLOG(1) << "Fetching profile image from " << image_url; |
| 305 picture_url_ = image_url; | 289 picture_url_ = image_url; |
| 306 profile_image_fetcher_.reset(content::URLFetcher::Create( | 290 profile_image_fetcher_.reset(content::URLFetcher::Create( |
| 307 GURL(image_url), content::URLFetcher::GET, this)); | 291 GURL(image_url), content::URLFetcher::GET, this)); |
| 308 profile_image_fetcher_->SetRequestContext( | 292 profile_image_fetcher_->SetRequestContext( |
| 309 delegate_->GetBrowserProfile()->GetRequestContext()); | 293 delegate_->GetBrowserProfile()->GetRequestContext()); |
| 310 if (!auth_token_.empty()) { | 294 if (!auth_token_.empty()) { |
| 311 profile_image_fetcher_->SetExtraRequestHeaders( | 295 profile_image_fetcher_->SetExtraRequestHeaders( |
| 312 base::StringPrintf(GetAuthorizationHeader(), auth_token_.c_str())); | 296 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str())); |
| 313 } | 297 } |
| 314 profile_image_fetcher_->Start(); | 298 profile_image_fetcher_->Start(); |
| 315 } else if (source == profile_image_fetcher_.get()) { | 299 } else if (source == profile_image_fetcher_.get()) { |
| 316 VLOG(1) << "Decoding the image..."; | 300 VLOG(1) << "Decoding the image..."; |
| 317 scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder( | 301 scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder( |
| 318 this, data); | 302 this, data); |
| 319 image_decoder->Start(); | 303 image_decoder->Start(); |
| 320 } | 304 } |
| 321 } | 305 } |
| 322 | 306 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 340 | 324 |
| 341 void ProfileDownloader::Observe( | 325 void ProfileDownloader::Observe( |
| 342 int type, | 326 int type, |
| 343 const content::NotificationSource& source, | 327 const content::NotificationSource& source, |
| 344 const content::NotificationDetails& details) { | 328 const content::NotificationDetails& details) { |
| 345 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE || | 329 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE || |
| 346 type == chrome::NOTIFICATION_TOKEN_REQUEST_FAILED); | 330 type == chrome::NOTIFICATION_TOKEN_REQUEST_FAILED); |
| 347 | 331 |
| 348 TokenService::TokenAvailableDetails* token_details = | 332 TokenService::TokenAvailableDetails* token_details = |
| 349 content::Details<TokenService::TokenAvailableDetails>(details).ptr(); | 333 content::Details<TokenService::TokenAvailableDetails>(details).ptr(); |
| 350 std::string service = delegate_->ShouldUseOAuthRefreshToken() ? | |
| 351 GaiaConstants::kGaiaOAuth2LoginRefreshToken : | |
| 352 GaiaConstants::kPicasaService; | |
| 353 | 334 |
| 354 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { | 335 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { |
| 355 if (token_details->service() == service) { | 336 if (token_details->service() == |
| 337 GaiaConstants::kGaiaOAuth2LoginRefreshToken) { |
| 356 registrar_.RemoveAll(); | 338 registrar_.RemoveAll(); |
| 357 if (delegate_->ShouldUseOAuthRefreshToken()) { | 339 StartFetchingOAuth2AccessToken(); |
| 358 StartFetchingOAuth2AccessToken(); | |
| 359 } else { | |
| 360 auth_token_ = token_details->token(); | |
| 361 StartFetchingImage(); | |
| 362 } | |
| 363 } | 340 } |
| 364 } else { | 341 } else { |
| 365 if (token_details->service() == service) { | 342 if (token_details->service() == |
| 343 GaiaConstants::kGaiaOAuth2LoginRefreshToken) { |
| 366 LOG(WARNING) << "ProfileDownloader: token request failed"; | 344 LOG(WARNING) << "ProfileDownloader: token request failed"; |
| 367 delegate_->OnDownloadComplete(this, false); | 345 delegate_->OnDownloadComplete(this, false); |
| 368 } | 346 } |
| 369 } | 347 } |
| 370 } | 348 } |
| 371 | 349 |
| 372 // Callback for OAuth2AccessTokenFetcher on success. |access_token| is the token | 350 // Callback for OAuth2AccessTokenFetcher on success. |access_token| is the token |
| 373 // used to start fetching user data. | 351 // used to start fetching user data. |
| 374 void ProfileDownloader::OnGetTokenSuccess(const std::string& access_token) { | 352 void ProfileDownloader::OnGetTokenSuccess(const std::string& access_token) { |
| 375 auth_token_ = access_token; | 353 auth_token_ = access_token; |
| 376 StartFetchingImage(); | 354 StartFetchingImage(); |
| 377 } | 355 } |
| 378 | 356 |
| 379 // Callback for OAuth2AccessTokenFetcher on failure. | 357 // Callback for OAuth2AccessTokenFetcher on failure. |
| 380 void ProfileDownloader::OnGetTokenFailure(const GoogleServiceAuthError& error) { | 358 void ProfileDownloader::OnGetTokenFailure(const GoogleServiceAuthError& error) { |
| 381 LOG(WARNING) << "ProfileDownloader: token request using refresh token failed"; | 359 LOG(WARNING) << "ProfileDownloader: token request using refresh token failed"; |
| 382 delegate_->OnDownloadComplete(this, false); | 360 delegate_->OnDownloadComplete(this, false); |
| 383 } | 361 } |
| OLD | NEW |