Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/signin/signin_account_id_helper.h" | 5 #include "chrome/browser/signin/signin_account_id_helper.h" |
| 6 | 6 |
| 7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 8 #include "chrome/browser/profiles/profile.h" | |
| 9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 10 #include "chrome/common/pref_names.h" | |
| 11 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 8 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 9 #include "components/signin/core/browser/signin_client.h" | |
| 10 #include "components/signin/core/common/signin_pref_names.h" | |
| 12 #include "google_apis/gaia/gaia_oauth_client.h" | 11 #include "google_apis/gaia/gaia_oauth_client.h" |
| 13 | 12 |
| 14 // TODO(guohui): this class should be moved to a more generic place for reuse. | 13 // TODO(guohui): this class should be moved to a more generic place for reuse. |
| 15 class SigninAccountIdHelper::GaiaIdFetcher | 14 class SigninAccountIdHelper::GaiaIdFetcher |
| 16 : public OAuth2TokenService::Consumer, | 15 : public OAuth2TokenService::Consumer, |
| 17 public gaia::GaiaOAuthClient::Delegate { | 16 public gaia::GaiaOAuthClient::Delegate { |
| 18 public: | 17 public: |
| 19 GaiaIdFetcher(Profile* profile, | 18 GaiaIdFetcher(SigninClient* client, |
| 19 ProfileOAuth2TokenService* token_service, | |
| 20 SigninManagerBase* signin_manager, | 20 SigninManagerBase* signin_manager, |
| 21 SigninAccountIdHelper* signin_account_id_helper); | 21 SigninAccountIdHelper* signin_account_id_helper); |
| 22 virtual ~GaiaIdFetcher(); | 22 virtual ~GaiaIdFetcher(); |
| 23 | 23 |
| 24 // OAuth2TokenService::Consumer implementation. | 24 // OAuth2TokenService::Consumer implementation. |
| 25 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 25 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 26 const std::string& access_token, | 26 const std::string& access_token, |
| 27 const base::Time& expiration_time) OVERRIDE; | 27 const base::Time& expiration_time) OVERRIDE; |
| 28 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 28 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 29 const GoogleServiceAuthError& error) OVERRIDE; | 29 const GoogleServiceAuthError& error) OVERRIDE; |
| 30 | 30 |
| 31 // gaia::GaiaOAuthClient::Delegate implementation. | 31 // gaia::GaiaOAuthClient::Delegate implementation. |
| 32 virtual void OnGetUserIdResponse(const std::string& gaia_id) OVERRIDE; | 32 virtual void OnGetUserIdResponse(const std::string& gaia_id) OVERRIDE; |
| 33 virtual void OnOAuthError() OVERRIDE; | 33 virtual void OnOAuthError() OVERRIDE; |
| 34 virtual void OnNetworkError(int response_code) OVERRIDE; | 34 virtual void OnNetworkError(int response_code) OVERRIDE; |
| 35 | 35 |
| 36 private: | 36 private: |
| 37 void Start(); | 37 void Start(); |
| 38 | 38 |
| 39 Profile* profile_; | 39 SigninClient* client_; |
| 40 ProfileOAuth2TokenService* token_service_; | |
| 40 SigninManagerBase* signin_manager_; | 41 SigninManagerBase* signin_manager_; |
| 41 SigninAccountIdHelper* signin_account_id_helper_; | 42 SigninAccountIdHelper* signin_account_id_helper_; |
| 42 | 43 |
| 43 scoped_ptr<OAuth2TokenService::Request> login_token_request_; | 44 scoped_ptr<OAuth2TokenService::Request> login_token_request_; |
| 44 scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; | 45 scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; |
| 45 | 46 |
| 46 DISALLOW_COPY_AND_ASSIGN(GaiaIdFetcher); | 47 DISALLOW_COPY_AND_ASSIGN(GaiaIdFetcher); |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 SigninAccountIdHelper::GaiaIdFetcher::GaiaIdFetcher( | 50 SigninAccountIdHelper::GaiaIdFetcher::GaiaIdFetcher( |
| 50 Profile* profile, | 51 SigninClient* client, |
| 52 ProfileOAuth2TokenService* token_service, | |
| 51 SigninManagerBase* signin_manager, | 53 SigninManagerBase* signin_manager, |
| 52 SigninAccountIdHelper* signin_account_id_helper) | 54 SigninAccountIdHelper* signin_account_id_helper) |
| 53 : OAuth2TokenService::Consumer("gaia_id_fetcher"), | 55 : OAuth2TokenService::Consumer("gaia_id_fetcher"), |
| 54 profile_(profile), | 56 client_(client), |
| 57 token_service_(token_service), | |
| 55 signin_manager_(signin_manager), | 58 signin_manager_(signin_manager), |
| 56 signin_account_id_helper_(signin_account_id_helper) { | 59 signin_account_id_helper_(signin_account_id_helper) { |
| 57 Start(); | 60 Start(); |
| 58 } | 61 } |
| 59 | 62 |
| 60 SigninAccountIdHelper::GaiaIdFetcher::~GaiaIdFetcher() {} | 63 SigninAccountIdHelper::GaiaIdFetcher::~GaiaIdFetcher() {} |
| 61 | 64 |
| 62 void SigninAccountIdHelper::GaiaIdFetcher::Start() { | 65 void SigninAccountIdHelper::GaiaIdFetcher::Start() { |
| 63 ProfileOAuth2TokenService* service = | |
| 64 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 65 OAuth2TokenService::ScopeSet scopes; | 66 OAuth2TokenService::ScopeSet scopes; |
| 66 scopes.insert("https://www.googleapis.com/auth/userinfo.profile"); | 67 scopes.insert("https://www.googleapis.com/auth/userinfo.profile"); |
| 67 login_token_request_ = service->StartRequest( | 68 login_token_request_ = token_service_->StartRequest( |
| 68 signin_manager_->GetAuthenticatedAccountId(), scopes, this); | 69 signin_manager_->GetAuthenticatedAccountId(), scopes, this); |
| 69 } | 70 } |
| 70 | 71 |
| 71 void SigninAccountIdHelper::GaiaIdFetcher::OnGetTokenSuccess( | 72 void SigninAccountIdHelper::GaiaIdFetcher::OnGetTokenSuccess( |
| 72 const OAuth2TokenService::Request* request, | 73 const OAuth2TokenService::Request* request, |
| 73 const std::string& access_token, | 74 const std::string& access_token, |
| 74 const base::Time& expiration_time) { | 75 const base::Time& expiration_time) { |
| 75 DCHECK_EQ(request, login_token_request_.get()); | 76 DCHECK_EQ(request, login_token_request_.get()); |
| 76 | 77 |
| 77 gaia_oauth_client_.reset( | 78 gaia_oauth_client_.reset( |
| 78 new gaia::GaiaOAuthClient(profile_->GetRequestContext())); | 79 new gaia::GaiaOAuthClient(client_->GetURLRequestContext())); |
| 79 | 80 |
| 80 const int kMaxGetUserIdRetries = 3; | 81 const int kMaxGetUserIdRetries = 3; |
| 81 gaia_oauth_client_->GetUserId(access_token, kMaxGetUserIdRetries, this); | 82 gaia_oauth_client_->GetUserId(access_token, kMaxGetUserIdRetries, this); |
| 82 } | 83 } |
| 83 | 84 |
| 84 void SigninAccountIdHelper::GaiaIdFetcher::OnGetTokenFailure( | 85 void SigninAccountIdHelper::GaiaIdFetcher::OnGetTokenFailure( |
| 85 const OAuth2TokenService::Request* request, | 86 const OAuth2TokenService::Request* request, |
| 86 const GoogleServiceAuthError& error) { | 87 const GoogleServiceAuthError& error) { |
| 87 VLOG(1) << "OnGetTokenFailure: " << error.error_message(); | 88 VLOG(1) << "OnGetTokenFailure: " << error.error_message(); |
| 88 DCHECK_EQ(request, login_token_request_.get()); | 89 DCHECK_EQ(request, login_token_request_.get()); |
| 89 signin_account_id_helper_->OnPrimaryAccountIdFetched(""); | 90 signin_account_id_helper_->OnPrimaryAccountIdFetched(""); |
| 90 } | 91 } |
| 91 | 92 |
| 92 void SigninAccountIdHelper::GaiaIdFetcher::OnGetUserIdResponse( | 93 void SigninAccountIdHelper::GaiaIdFetcher::OnGetUserIdResponse( |
| 93 const std::string& gaia_id) { | 94 const std::string& gaia_id) { |
| 94 signin_account_id_helper_->OnPrimaryAccountIdFetched(gaia_id); | 95 signin_account_id_helper_->OnPrimaryAccountIdFetched(gaia_id); |
| 95 } | 96 } |
| 96 | 97 |
| 97 void SigninAccountIdHelper::GaiaIdFetcher::OnOAuthError() { | 98 void SigninAccountIdHelper::GaiaIdFetcher::OnOAuthError() { |
| 98 VLOG(1) << "OnOAuthError"; | 99 VLOG(1) << "OnOAuthError"; |
| 99 } | 100 } |
| 100 | 101 |
| 101 void SigninAccountIdHelper::GaiaIdFetcher::OnNetworkError( | 102 void SigninAccountIdHelper::GaiaIdFetcher::OnNetworkError( |
| 102 int response_code) { | 103 int response_code) { |
| 103 VLOG(1) << "OnNetworkError " << response_code; | 104 VLOG(1) << "OnNetworkError " << response_code; |
| 104 } | 105 } |
| 105 | 106 |
| 106 SigninAccountIdHelper::SigninAccountIdHelper(Profile* profile, | 107 SigninAccountIdHelper::SigninAccountIdHelper( |
| 107 SigninManagerBase* signin_manager) | 108 SigninClient* client, |
| 108 : profile_(profile), signin_manager_(signin_manager) { | 109 ProfileOAuth2TokenService* token_service, |
| 110 SigninManagerBase* signin_manager) | |
| 111 : client_(client), | |
| 112 token_service_(token_service), | |
| 113 signin_manager_(signin_manager) { | |
| 114 DCHECK(token_service_); | |
| 109 DCHECK(signin_manager_); | 115 DCHECK(signin_manager_); |
|
Roger Tawa OOO till Jul 10th
2014/03/27 15:53:29
DCHECK client_ is not null?
blundell
2014/03/27 17:43:56
Done.
| |
| 110 signin_manager_->AddObserver(this); | 116 signin_manager_->AddObserver(this); |
| 111 ProfileOAuth2TokenService* token_service = | |
| 112 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 113 std::string primary_email = signin_manager_->GetAuthenticatedAccountId(); | 117 std::string primary_email = signin_manager_->GetAuthenticatedAccountId(); |
| 114 if (!primary_email.empty() && | 118 if (!primary_email.empty() && |
| 115 token_service->RefreshTokenIsAvailable(primary_email) && | 119 token_service_->RefreshTokenIsAvailable(primary_email) && |
| 116 !disable_for_test_) { | 120 !disable_for_test_) { |
| 117 id_fetcher_.reset(new GaiaIdFetcher(profile_, signin_manager_, this)); | 121 id_fetcher_.reset( |
| 122 new GaiaIdFetcher(client_, token_service_, signin_manager_, this)); | |
| 118 } | 123 } |
| 119 token_service->AddObserver(this); | 124 token_service_->AddObserver(this); |
| 120 } | 125 } |
| 121 | 126 |
| 122 SigninAccountIdHelper::~SigninAccountIdHelper() { | 127 SigninAccountIdHelper::~SigninAccountIdHelper() { |
| 123 signin_manager_->RemoveObserver(this); | 128 signin_manager_->RemoveObserver(this); |
| 124 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_) | 129 token_service_->RemoveObserver(this); |
| 125 ->RemoveObserver(this); | |
| 126 } | 130 } |
| 127 | 131 |
| 128 void SigninAccountIdHelper::GoogleSignedOut(const std::string& username) { | 132 void SigninAccountIdHelper::GoogleSignedOut(const std::string& username) { |
| 129 profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUserAccountId); | 133 client_->GetPrefs()->ClearPref(prefs::kGoogleServicesUserAccountId); |
| 130 } | 134 } |
| 131 | 135 |
| 132 void SigninAccountIdHelper::OnRefreshTokenAvailable( | 136 void SigninAccountIdHelper::OnRefreshTokenAvailable( |
| 133 const std::string& account_id) { | 137 const std::string& account_id) { |
| 134 if (account_id == signin_manager_->GetAuthenticatedAccountId()) { | 138 if (account_id == signin_manager_->GetAuthenticatedAccountId()) { |
| 135 std::string current_gaia_id = | 139 std::string current_gaia_id = |
| 136 profile_->GetPrefs()->GetString(prefs::kGoogleServicesUserAccountId); | 140 client_->GetPrefs()->GetString(prefs::kGoogleServicesUserAccountId); |
| 137 if (current_gaia_id.empty() && !disable_for_test_) { | 141 if (current_gaia_id.empty() && !disable_for_test_) { |
| 138 id_fetcher_.reset(new GaiaIdFetcher(profile_, signin_manager_, this)); | 142 id_fetcher_.reset( |
| 143 new GaiaIdFetcher(client_, token_service_, signin_manager_, this)); | |
| 139 } | 144 } |
| 140 } | 145 } |
| 141 } | 146 } |
| 142 | 147 |
| 143 void SigninAccountIdHelper::OnPrimaryAccountIdFetched( | 148 void SigninAccountIdHelper::OnPrimaryAccountIdFetched( |
| 144 const std::string& gaia_id) { | 149 const std::string& gaia_id) { |
| 145 if (!gaia_id.empty()) { | 150 if (!gaia_id.empty()) { |
| 146 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUserAccountId, | 151 client_->GetPrefs()->SetString(prefs::kGoogleServicesUserAccountId, |
| 147 gaia_id); | 152 gaia_id); |
| 148 } | 153 } |
| 149 } | 154 } |
| 150 | 155 |
| 151 // static | 156 // static |
| 152 bool SigninAccountIdHelper::disable_for_test_ = false; | 157 bool SigninAccountIdHelper::disable_for_test_ = false; |
| 153 | 158 |
| 154 // static | 159 // static |
| 155 void SigninAccountIdHelper::SetDisableForTest(bool disable_for_test) { | 160 void SigninAccountIdHelper::SetDisableForTest(bool disable_for_test) { |
| 156 disable_for_test_ = disable_for_test; | 161 disable_for_test_ = disable_for_test; |
| 157 } | 162 } |
| 158 | 163 |
| OLD | NEW |