Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: chrome/browser/profiles/profile_downloader.cc

Issue 8742008: ChromeOS: Use OAuth2 refresh token to download GAIA info (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 client login 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[] = "Authorization: Bearer %s";
44 "Authorization: Bearer %s";
45 39
46 // URL requesting Picasa API for user info. 40 // URL requesting Picasa API for user info.
47 const char kUserEntryURL[] = 41 const char kUserEntryURL[] =
48 "http://picasaweb.google.com/data/entry/api/user/default?alt=json"; 42 "http://picasaweb.google.com/data/entry/api/user/default?alt=json";
49 43
50 // OAuth scope for the Picasa API. 44 // OAuth scope for the Picasa API.
51 const char kPicasaScope[] = "http://picasaweb.google.com/data/"; 45 const char kPicasaScope[] = "http://picasaweb.google.com/data/";
52 46
53 // Path in JSON dictionary to user's photo thumbnail URL. 47 // Path in JSON dictionary to user's photo thumbnail URL.
54 const char kPhotoThumbnailURLPath[] = "entry.gphoto$thumbnail.$t"; 48 const char kPhotoThumbnailURLPath[] = "entry.gphoto$thumbnail.$t";
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
194 188
195 TokenService* service = delegate_->GetBrowserProfile()->GetTokenService(); 189 TokenService* service = delegate_->GetBrowserProfile()->GetTokenService();
196 if (!service) { 190 if (!service) {
197 // This can happen in some test paths. 191 // This can happen in some test paths.
198 LOG(WARNING) << "User has no token service"; 192 LOG(WARNING) << "User has no token service";
199 delegate_->OnDownloadComplete(this, false); 193 delegate_->OnDownloadComplete(this, false);
200 return; 194 return;
201 } 195 }
202 196
203 if (delegate_->GetShouldUseOAuthRefreshToken() && 197 if (service->HasOAuthLoginToken()) {
204 service->HasOAuthLoginToken()) {
205 std::vector<std::string> scopes; 198 std::vector<std::string> scopes;
206 scopes.push_back(kPicasaScope); 199 scopes.push_back(kPicasaScope);
207 oauth2_access_token_fetcher_.reset(new OAuth2AccessTokenFetcher( 200 oauth2_access_token_fetcher_.reset(new OAuth2AccessTokenFetcher(
208 this, delegate_->GetBrowserProfile()->GetRequestContext())); 201 this, delegate_->GetBrowserProfile()->GetRequestContext()));
209 oauth2_access_token_fetcher_->Start( 202 oauth2_access_token_fetcher_->Start(
210 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), 203 GaiaUrls::GetInstance()->oauth2_chrome_client_id(),
211 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), 204 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(),
212 service->GetOAuth2LoginRefreshToken(), 205 service->GetOAuth2LoginRefreshToken(),
213 scopes); 206 scopes);
214 } else if (!delegate_->GetShouldUseOAuthRefreshToken() &&
215 service->HasTokenForService(GaiaConstants::kPicasaService)) {
216 auth_token_ =
217 service->GetTokenForService(GaiaConstants::kPicasaService);
218 StartFetchingImage();
219 } else { 207 } else {
Munjal (Google) 2011/11/30 19:29:29 In else you don't want to just wait for token avai
sail 2011/12/01 20:01:49 Ahh, you're right. I'm not sure if this is a probl
Munjal (Google) 2011/12/01 23:25:58 Sure, you can fix it in a separate CL.
220 registrar_.Add(this, 208 registrar_.Add(this,
221 chrome::NOTIFICATION_TOKEN_AVAILABLE, 209 chrome::NOTIFICATION_TOKEN_AVAILABLE,
222 content::Source<TokenService>(service)); 210 content::Source<TokenService>(service));
223 registrar_.Add(this, 211 registrar_.Add(this,
224 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, 212 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED,
225 content::Source<TokenService>(service)); 213 content::Source<TokenService>(service));
226 } 214 }
227 } 215 }
228 216
229 string16 ProfileDownloader::GetProfileFullName() const { 217 string16 ProfileDownloader::GetProfileFullName() const {
230 return profile_full_name_; 218 return profile_full_name_;
231 } 219 }
232 220
233 SkBitmap ProfileDownloader::GetProfilePicture() const { 221 SkBitmap ProfileDownloader::GetProfilePicture() const {
234 return profile_picture_; 222 return profile_picture_;
235 } 223 }
236 224
237 void ProfileDownloader::StartFetchingImage() { 225 void ProfileDownloader::StartFetchingImage() {
238 VLOG(1) << "Fetching user entry with token: " << auth_token_; 226 VLOG(1) << "Fetching user entry with token: " << auth_token_;
239 user_entry_fetcher_.reset(content::URLFetcher::Create( 227 user_entry_fetcher_.reset(content::URLFetcher::Create(
240 GURL(kUserEntryURL), content::URLFetcher::GET, this)); 228 GURL(kUserEntryURL), content::URLFetcher::GET, this));
241 user_entry_fetcher_->SetRequestContext( 229 user_entry_fetcher_->SetRequestContext(
242 delegate_->GetBrowserProfile()->GetRequestContext()); 230 delegate_->GetBrowserProfile()->GetRequestContext());
243 if (!auth_token_.empty()) { 231 if (!auth_token_.empty()) {
244 user_entry_fetcher_->SetExtraRequestHeaders( 232 user_entry_fetcher_->SetExtraRequestHeaders(
245 base::StringPrintf(GetAuthorizationHeader(), auth_token_.c_str())); 233 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str()));
246 } 234 }
247 user_entry_fetcher_->Start(); 235 user_entry_fetcher_->Start();
248 } 236 }
249 237
250 const char* ProfileDownloader::GetAuthorizationHeader() const {
251 return delegate_->GetShouldUseOAuthRefreshToken() ?
252 kOAuthAccessAuthorizationHeader : kClientAccessAuthorizationHeader;
253 }
254
255 ProfileDownloader::~ProfileDownloader() {} 238 ProfileDownloader::~ProfileDownloader() {}
256 239
257 void ProfileDownloader::OnURLFetchComplete(const content::URLFetcher* source) { 240 void ProfileDownloader::OnURLFetchComplete(const content::URLFetcher* source) {
258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
259 std::string data; 242 std::string data;
260 source->GetResponseAsString(&data); 243 source->GetResponseAsString(&data);
261 if (source->GetResponseCode() != 200) { 244 if (source->GetResponseCode() != 200) {
262 LOG(ERROR) << "Response code is " << source->GetResponseCode(); 245 LOG(ERROR) << "Response code is " << source->GetResponseCode();
263 LOG(ERROR) << "Url is " << source->GetURL().spec(); 246 LOG(ERROR) << "Url is " << source->GetURL().spec();
264 LOG(ERROR) << "Data is " << data; 247 LOG(ERROR) << "Data is " << data;
(...skipping 11 matching lines...) Expand all
276 delegate_->OnDownloadComplete(this, true); 259 delegate_->OnDownloadComplete(this, true);
277 return; 260 return;
278 } 261 }
279 VLOG(1) << "Fetching profile image from " << image_url; 262 VLOG(1) << "Fetching profile image from " << image_url;
280 profile_image_fetcher_.reset(content::URLFetcher::Create( 263 profile_image_fetcher_.reset(content::URLFetcher::Create(
281 GURL(image_url), content::URLFetcher::GET, this)); 264 GURL(image_url), content::URLFetcher::GET, this));
282 profile_image_fetcher_->SetRequestContext( 265 profile_image_fetcher_->SetRequestContext(
283 delegate_->GetBrowserProfile()->GetRequestContext()); 266 delegate_->GetBrowserProfile()->GetRequestContext());
284 if (!auth_token_.empty()) { 267 if (!auth_token_.empty()) {
285 profile_image_fetcher_->SetExtraRequestHeaders( 268 profile_image_fetcher_->SetExtraRequestHeaders(
286 base::StringPrintf(GetAuthorizationHeader(), auth_token_.c_str())); 269 base::StringPrintf(kAuthorizationHeader, auth_token_.c_str()));
287 } 270 }
288 profile_image_fetcher_->Start(); 271 profile_image_fetcher_->Start();
289 } else if (source == profile_image_fetcher_.get()) { 272 } else if (source == profile_image_fetcher_.get()) {
290 VLOG(1) << "Decoding the image..."; 273 VLOG(1) << "Decoding the image...";
291 scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder( 274 scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder(
292 this, data); 275 this, data);
293 image_decoder->Start(); 276 image_decoder->Start();
294 } 277 }
295 } 278 }
296 279
(...skipping 16 matching lines...) Expand all
313 296
314 void ProfileDownloader::Observe( 297 void ProfileDownloader::Observe(
315 int type, 298 int type,
316 const content::NotificationSource& source, 299 const content::NotificationSource& source,
317 const content::NotificationDetails& details) { 300 const content::NotificationDetails& details) {
318 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE || 301 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE ||
319 type == chrome::NOTIFICATION_TOKEN_REQUEST_FAILED); 302 type == chrome::NOTIFICATION_TOKEN_REQUEST_FAILED);
320 303
321 TokenService::TokenAvailableDetails* token_details = 304 TokenService::TokenAvailableDetails* token_details =
322 content::Details<TokenService::TokenAvailableDetails>(details).ptr(); 305 content::Details<TokenService::TokenAvailableDetails>(details).ptr();
323 std::string service = delegate_->GetShouldUseOAuthRefreshToken() ?
324 GaiaConstants::kGaiaOAuth2LoginRefreshToken :
325 GaiaConstants::kPicasaService;
326 306
327 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { 307 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) {
328 if (token_details->service() == service) { 308 if (token_details->service() ==
309 GaiaConstants::kGaiaOAuth2LoginRefreshToken) {
329 registrar_.RemoveAll(); 310 registrar_.RemoveAll();
330 auth_token_ = token_details->token(); 311 auth_token_ = token_details->token();
331 StartFetchingImage(); 312 StartFetchingImage();
332 } 313 }
333 } else { 314 } else {
334 if (token_details->service() == service) { 315 if (token_details->service() ==
316 GaiaConstants::kGaiaOAuth2LoginRefreshToken) {
335 LOG(WARNING) << "ProfileDownloader: token request failed"; 317 LOG(WARNING) << "ProfileDownloader: token request failed";
336 delegate_->OnDownloadComplete(this, false); 318 delegate_->OnDownloadComplete(this, false);
337 } 319 }
338 } 320 }
339 } 321 }
340 322
341 void ProfileDownloader::OnGetTokenSuccess(const std::string& access_token) { 323 void ProfileDownloader::OnGetTokenSuccess(const std::string& access_token) {
342 auth_token_ = access_token; 324 auth_token_ = access_token;
343 StartFetchingImage(); 325 StartFetchingImage();
344 } 326 }
345 327
346 void ProfileDownloader::OnGetTokenFailure(const GoogleServiceAuthError& error) { 328 void ProfileDownloader::OnGetTokenFailure(const GoogleServiceAuthError& error) {
347 LOG(WARNING) << "ProfileDownloader: token request using refresh token failed"; 329 LOG(WARNING) << "ProfileDownloader: token request using refresh token failed";
348 delegate_->OnDownloadComplete(this, false); 330 delegate_->OnDownloadComplete(this, false);
349 } 331 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698