Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 "components/signin/core/browser/account_service_flag_fetcher.h" | |
| 6 | |
| 7 #include "base/strings/string_split.h" | |
| 8 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 9 #include "google_apis/gaia/gaia_constants.h" | |
| 10 | |
| 11 AccountServiceFlagFetcher::AccountServiceFlagFetcher( | |
| 12 const std::string& account_id, | |
| 13 ProfileOAuth2TokenService* token_service, | |
| 14 net::URLRequestContextGetter* request_context, | |
| 15 const Callback& callback) | |
| 16 : OAuth2TokenService::Consumer("account_service_flag_fetcher"), | |
|
Bernhard Bauer
2014/05/20 12:35:27
This should be indented four spaces, then the next
Marc Treib
2014/05/20 13:24:28
Done.
| |
| 17 account_id_(account_id), | |
| 18 token_service_(token_service), | |
| 19 gaia_auth_fetcher_(this, GaiaConstants::kChromeSource, request_context), | |
| 20 callback_(callback) { | |
| 21 Start(); | |
| 22 } | |
| 23 | |
| 24 AccountServiceFlagFetcher::~AccountServiceFlagFetcher() { | |
| 25 // Ensures PO2TS observation is cleared when AccountServiceFlagFetcher is | |
| 26 // destructed before refresh token is available. | |
| 27 token_service_->RemoveObserver(this); | |
| 28 | |
| 29 gaia_auth_fetcher_.CancelRequest(); | |
| 30 } | |
| 31 | |
| 32 void AccountServiceFlagFetcher::Start() { | |
| 33 if (token_service_->RefreshTokenIsAvailable(account_id_)) { | |
| 34 StartFetchingOAuth2AccessToken(); | |
| 35 } else { | |
| 36 // Wait until we get a refresh token. | |
| 37 token_service_->AddObserver(this); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 void AccountServiceFlagFetcher::OnRefreshTokenAvailable( | |
| 42 const std::string& account_id) { | |
| 43 // Wait until we get a refresh token for the requested account. | |
| 44 if (account_id != account_id_) | |
| 45 return; | |
| 46 | |
| 47 token_service_->RemoveObserver(this); | |
| 48 | |
| 49 StartFetchingOAuth2AccessToken(); | |
| 50 } | |
| 51 | |
| 52 void AccountServiceFlagFetcher::StartFetchingOAuth2AccessToken() { | |
| 53 OAuth2TokenService::ScopeSet scopes; | |
| 54 scopes.insert(GaiaConstants::kOAuth1LoginScope); | |
| 55 oauth2_access_token_request_ = token_service_->StartRequest( | |
| 56 account_id_, scopes, this); | |
| 57 } | |
| 58 | |
| 59 void AccountServiceFlagFetcher::OnGetTokenSuccess( | |
| 60 const OAuth2TokenService::Request* request, | |
| 61 const std::string& access_token, | |
| 62 const base::Time& expiration_time) { | |
| 63 DCHECK_EQ(oauth2_access_token_request_.get(), request); | |
| 64 oauth2_access_token_request_.reset(); | |
| 65 | |
| 66 gaia_auth_fetcher_.StartOAuthLogin(access_token, GaiaConstants::kGaiaService); | |
| 67 } | |
| 68 | |
| 69 void AccountServiceFlagFetcher::OnGetTokenFailure( | |
| 70 const OAuth2TokenService::Request* request, | |
| 71 const GoogleServiceAuthError& error) { | |
| 72 DCHECK_EQ(oauth2_access_token_request_.get(), request); | |
| 73 oauth2_access_token_request_.reset(); | |
| 74 | |
| 75 DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetTokenFailure: " | |
| 76 << error.ToString(); | |
| 77 callback_.Run(TOKEN_ERROR, std::vector<std::string>()); | |
| 78 } | |
| 79 | |
| 80 void AccountServiceFlagFetcher::OnClientLoginSuccess( | |
| 81 const ClientLoginResult& result) { | |
| 82 gaia_auth_fetcher_.StartGetUserInfo(result.lsid); | |
| 83 } | |
| 84 | |
| 85 void AccountServiceFlagFetcher::OnClientLoginFailure( | |
| 86 const GoogleServiceAuthError& error) { | |
| 87 DLOG(WARNING) << "AccountServiceFlagFetcher::OnClientLoginFailure: " | |
| 88 << error.ToString(); | |
| 89 callback_.Run(SERVICE_ERROR, std::vector<std::string>()); | |
|
Bernhard Bauer
2014/05/20 12:35:27
Hm, if we now support returning errors and have a
Marc Treib
2014/05/20 13:24:28
True, we could distinguish between more types of e
Bernhard Bauer
2014/05/20 13:50:10
Well, at a minimum, a client could log the errors
| |
| 90 } | |
| 91 | |
| 92 void AccountServiceFlagFetcher::OnGetUserInfoSuccess(const UserInfoMap& data) { | |
| 93 ResultCode result = SERVICE_ERROR; | |
| 94 std::vector<std::string> services; | |
| 95 UserInfoMap::const_iterator services_iter = data.find("allServices"); | |
| 96 if (services_iter != data.end()) { | |
| 97 result = SUCCESS; | |
| 98 base::SplitString(services_iter->second, ',', &services); | |
| 99 } else { | |
| 100 DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetUserInfoSuccess: " | |
| 101 << "GetUserInfo response didn't include allServices field."; | |
| 102 } | |
| 103 callback_.Run(result, services); | |
| 104 } | |
| 105 | |
| 106 void AccountServiceFlagFetcher::OnGetUserInfoFailure( | |
| 107 const GoogleServiceAuthError& error) { | |
| 108 DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetUserInfoFailure: " | |
| 109 << error.ToString(); | |
| 110 callback_.Run(SERVICE_ERROR, std::vector<std::string>()); | |
| 111 } | |
| OLD | NEW |