1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/signin/ubertoken_fetcher.h"
6
7 #include <vector>
8
9 #include "base/logging.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13 #include "google_apis/gaia/gaia_auth_fetcher.h"
14 #include "google_apis/gaia/gaia_constants.h"
15 #include "google_apis/gaia/gaia_urls.h"
16 #include "google_apis/gaia/google_service_auth_error.h"
17
UbertokenFetcher(Profile * profile,UbertokenConsumer * consumer)18 UbertokenFetcher::UbertokenFetcher(Profile* profile,
19 UbertokenConsumer* consumer)
20 : profile_(profile), consumer_(consumer) {
21 DCHECK(profile);
22 DCHECK(consumer);
23 }
24
~UbertokenFetcher()25 UbertokenFetcher::~UbertokenFetcher() {
26 }
27
StartFetchingToken()28 void UbertokenFetcher::StartFetchingToken() {
29 ProfileOAuth2TokenService* token_service =
30 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
31 StartFetchingToken(token_service->GetPrimaryAccountId());
32 }
33
StartFetchingToken(const std::string & account_id)34 void UbertokenFetcher::StartFetchingToken(const std::string& account_id) {
35 OAuth2TokenService::ScopeSet scopes;
36 scopes.insert(GaiaUrls::GetInstance()->oauth1_login_scope());
37 ProfileOAuth2TokenService* token_service =
38 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
39 access_token_request_ = token_service->StartRequest(account_id, scopes, this);
40 }
41
OnUberAuthTokenSuccess(const std::string & token)42 void UbertokenFetcher::OnUberAuthTokenSuccess(const std::string& token) {
43 consumer_->OnUbertokenSuccess(token);
44 }
45
OnUberAuthTokenFailure(const GoogleServiceAuthError & error)46 void UbertokenFetcher::OnUberAuthTokenFailure(
47 const GoogleServiceAuthError& error) {
48 consumer_->OnUbertokenFailure(error);
49 }
50
OnGetTokenSuccess(const OAuth2TokenService::Request * request,const std::string & access_token,const base::Time & expiration_time)51 void UbertokenFetcher::OnGetTokenSuccess(
52 const OAuth2TokenService::Request* request,
53 const std::string& access_token,
54 const base::Time& expiration_time) {
55 access_token_request_.reset();
56 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this,
57 GaiaConstants::kChromeSource,
58 profile_->GetRequestContext()));
59 gaia_auth_fetcher_->StartTokenFetchForUberAuthExchange(access_token);
60 }
61
OnGetTokenFailure(const OAuth2TokenService::Request * request,const GoogleServiceAuthError & error)62 void UbertokenFetcher::OnGetTokenFailure(
63 const OAuth2TokenService::Request* request,
64 const GoogleServiceAuthError& error) {
65 access_token_request_.reset();
66 consumer_->OnUbertokenFailure(error);
67 }
68