Commit f37c1c99 authored by treib@chromium.org's avatar treib@chromium.org

Create AccountServiceFlagFetcher which downloads an account's Gaia service...

Create AccountServiceFlagFetcher which downloads an account's Gaia service flags, using https://accounts.google.com/GetUserInfo.

BUG=372381

Review URL: https://codereview.chromium.org/284763004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274541 0039d316-1c4b-4281-b951-d872f2087c98
parent 9f35bd24
This diff is collapsed.
...@@ -67,6 +67,10 @@ void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser( ...@@ -67,6 +67,10 @@ void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser(
} }
} }
void FakeProfileOAuth2TokenService::IssueAllRefreshTokensLoaded() {
FireRefreshTokensLoaded();
}
void FakeProfileOAuth2TokenService::IssueAllTokensForAccount( void FakeProfileOAuth2TokenService::IssueAllTokensForAccount(
const std::string& account_id, const std::string& account_id,
const std::string& access_token, const std::string& access_token,
...@@ -79,6 +83,17 @@ void FakeProfileOAuth2TokenService::IssueAllTokensForAccount( ...@@ -79,6 +83,17 @@ void FakeProfileOAuth2TokenService::IssueAllTokensForAccount(
expiration); expiration);
} }
void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequestsForAccount(
const std::string& account_id,
const GoogleServiceAuthError& error) {
CompleteRequests(account_id,
true,
ScopeSet(),
error,
std::string(),
base::Time());
}
void FakeProfileOAuth2TokenService::IssueTokenForScope( void FakeProfileOAuth2TokenService::IssueTokenForScope(
const ScopeSet& scope, const ScopeSet& scope,
const std::string& access_token, const std::string& access_token,
......
...@@ -82,6 +82,9 @@ class FakeProfileOAuth2TokenService ...@@ -82,6 +82,9 @@ class FakeProfileOAuth2TokenService
void IssueRefreshTokenForUser(const std::string& account_id, void IssueRefreshTokenForUser(const std::string& account_id,
const std::string& token); const std::string& token);
// Fire OnRefreshTokensLoaded on all observers.
void IssueAllRefreshTokensLoaded();
// Gets a list of active requests (can be used by tests to validate that the // Gets a list of active requests (can be used by tests to validate that the
// correct request has been issued). // correct request has been issued).
std::vector<PendingRequest> GetPendingRequests(); std::vector<PendingRequest> GetPendingRequests();
...@@ -91,6 +94,10 @@ class FakeProfileOAuth2TokenService ...@@ -91,6 +94,10 @@ class FakeProfileOAuth2TokenService
const std::string& access_token, const std::string& access_token,
const base::Time& expiration); const base::Time& expiration);
void IssueErrorForAllPendingRequestsForAccount(
const std::string& account_id,
const GoogleServiceAuthError& error);
void IssueTokenForScope(const ScopeSet& scopes, void IssueTokenForScope(const ScopeSet& scopes,
const std::string& access_token, const std::string& access_token,
const base::Time& expiration); const base::Time& expiration);
......
...@@ -1295,6 +1295,7 @@ ...@@ -1295,6 +1295,7 @@
'browser/shell_integration_linux_unittest.cc', 'browser/shell_integration_linux_unittest.cc',
'browser/shell_integration_win_unittest.cc', 'browser/shell_integration_win_unittest.cc',
'browser/signin/account_reconcilor_unittest.cc', 'browser/signin/account_reconcilor_unittest.cc',
'browser/signin/account_service_flag_fetcher_unittest.cc',
'browser/signin/local_auth_unittest.cc', 'browser/signin/local_auth_unittest.cc',
'browser/signin/signin_error_notifier_ash_unittest.cc', 'browser/signin/signin_error_notifier_ash_unittest.cc',
'browser/signin/signin_global_error_unittest.cc', 'browser/signin/signin_global_error_unittest.cc',
......
...@@ -43,6 +43,8 @@ ...@@ -43,6 +43,8 @@
'signin/core/browser/about_signin_internals.h', 'signin/core/browser/about_signin_internals.h',
'signin/core/browser/account_reconcilor.cc', 'signin/core/browser/account_reconcilor.cc',
'signin/core/browser/account_reconcilor.h', 'signin/core/browser/account_reconcilor.h',
'signin/core/browser/account_service_flag_fetcher.cc',
'signin/core/browser/account_service_flag_fetcher.h',
'signin/core/browser/mutable_profile_oauth2_token_service.cc', 'signin/core/browser/mutable_profile_oauth2_token_service.cc',
'signin/core/browser/mutable_profile_oauth2_token_service.h', 'signin/core/browser/mutable_profile_oauth2_token_service.h',
'signin/core/browser/profile_oauth2_token_service.cc', 'signin/core/browser/profile_oauth2_token_service.cc',
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/signin/core/browser/account_service_flag_fetcher.h"
#include "base/strings/string_split.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "google_apis/gaia/gaia_constants.h"
AccountServiceFlagFetcher::AccountServiceFlagFetcher(
const std::string& account_id,
ProfileOAuth2TokenService* token_service,
net::URLRequestContextGetter* request_context,
const ResultCallback& callback)
: OAuth2TokenService::Consumer("account_service_flag_fetcher"),
account_id_(account_id),
token_service_(token_service),
gaia_auth_fetcher_(this, GaiaConstants::kChromeSource, request_context),
callback_(callback) {
Start();
}
AccountServiceFlagFetcher::~AccountServiceFlagFetcher() {
// Ensures PO2TS observation is cleared when AccountServiceFlagFetcher is
// destructed before refresh token is available.
token_service_->RemoveObserver(this);
gaia_auth_fetcher_.CancelRequest();
}
void AccountServiceFlagFetcher::Start() {
if (token_service_->RefreshTokenIsAvailable(account_id_)) {
StartFetchingOAuth2AccessToken();
} else {
// Wait until we get a refresh token.
token_service_->AddObserver(this);
}
}
void AccountServiceFlagFetcher::OnRefreshTokenAvailable(
const std::string& account_id) {
// Wait until we get a refresh token for the requested account.
if (account_id != account_id_)
return;
token_service_->RemoveObserver(this);
StartFetchingOAuth2AccessToken();
}
void AccountServiceFlagFetcher::OnRefreshTokensLoaded() {
token_service_->RemoveObserver(this);
// The PO2TS has loaded all tokens, but we didn't get one for the account we
// want. We probably won't get one any time soon, so report an error.
DLOG(WARNING) << "AccountServiceFlagFetcher::OnRefreshTokensLoaded: "
<< "Did not get a refresh token for account " << account_id_;
callback_.Run(TOKEN_ERROR, std::vector<std::string>());
}
void AccountServiceFlagFetcher::StartFetchingOAuth2AccessToken() {
OAuth2TokenService::ScopeSet scopes;
scopes.insert(GaiaConstants::kOAuth1LoginScope);
oauth2_access_token_request_ = token_service_->StartRequest(
account_id_, scopes, this);
}
void AccountServiceFlagFetcher::OnGetTokenSuccess(
const OAuth2TokenService::Request* request,
const std::string& access_token,
const base::Time& expiration_time) {
DCHECK_EQ(oauth2_access_token_request_.get(), request);
oauth2_access_token_request_.reset();
gaia_auth_fetcher_.StartOAuthLogin(access_token, GaiaConstants::kGaiaService);
}
void AccountServiceFlagFetcher::OnGetTokenFailure(
const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) {
DCHECK_EQ(oauth2_access_token_request_.get(), request);
oauth2_access_token_request_.reset();
DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetTokenFailure: "
<< error.ToString();
callback_.Run(TOKEN_ERROR, std::vector<std::string>());
}
void AccountServiceFlagFetcher::OnClientLoginSuccess(
const ClientLoginResult& result) {
gaia_auth_fetcher_.StartGetUserInfo(result.lsid);
}
void AccountServiceFlagFetcher::OnClientLoginFailure(
const GoogleServiceAuthError& error) {
DLOG(WARNING) << "AccountServiceFlagFetcher::OnClientLoginFailure: "
<< error.ToString();
callback_.Run(SERVICE_ERROR, std::vector<std::string>());
}
void AccountServiceFlagFetcher::OnGetUserInfoSuccess(const UserInfoMap& data) {
ResultCode result = SERVICE_ERROR;
std::vector<std::string> services;
UserInfoMap::const_iterator services_iter = data.find("allServices");
if (services_iter != data.end()) {
result = SUCCESS;
base::SplitString(services_iter->second, ',', &services);
} else {
DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetUserInfoSuccess: "
<< "GetUserInfo response didn't include allServices field.";
}
callback_.Run(result, services);
}
void AccountServiceFlagFetcher::OnGetUserInfoFailure(
const GoogleServiceAuthError& error) {
DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetUserInfoFailure: "
<< error.ToString();
callback_.Run(SERVICE_ERROR, std::vector<std::string>());
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_SERVICE_FLAG_FETCHER_H_
#define COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_SERVICE_FLAG_FETCHER_H_
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "google_apis/gaia/gaia_auth_consumer.h"
#include "google_apis/gaia/gaia_auth_fetcher.h"
#include "google_apis/gaia/oauth2_token_service.h"
class ProfileOAuth2TokenService;
namespace net {
class URLRequestContextGetter;
}
// Downloads an account's list of Gaia service flags.
// On construction, the download starts immediately and calls the given callback
// when either the download is successful or an error is detected. It is valid
// to destruct the object before the callback is called; this will cancel the
// pending request.
class AccountServiceFlagFetcher : public GaiaAuthConsumer,
public OAuth2TokenService::Observer,
public OAuth2TokenService::Consumer {
public:
enum ResultCode {
SUCCESS,
TOKEN_ERROR, // Failed to get OAuth2 token.
SERVICE_ERROR, // Service returned an error or malformed reply.
};
// If the flag download is successful, this will return the list of service
// flags that are set for the given account.
typedef base::Callback<void(ResultCode /* result */,
const std::vector<std::string>& /* flags */)>
ResultCallback;
// Immediately starts fetching the flags.
AccountServiceFlagFetcher(const std::string& account_id,
ProfileOAuth2TokenService* token_service,
net::URLRequestContextGetter* request_context,
const ResultCallback& callback);
// Destructing the object before the callback is called cancels the request.
virtual ~AccountServiceFlagFetcher();
private:
void Start();
void StartFetchingOAuth2AccessToken();
// Overridden from OAuth2TokenService::Observer:
virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
virtual void OnRefreshTokensLoaded() OVERRIDE;
// Overridden from OAuth2TokenService::Consumer:
virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
const std::string& access_token,
const base::Time& expiration_time) OVERRIDE;
virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) OVERRIDE;
// Overridden from GaiaAuthConsumer:
virtual void OnClientLoginSuccess(const ClientLoginResult& result) OVERRIDE;
virtual void OnClientLoginFailure(const GoogleServiceAuthError& error)
OVERRIDE;
virtual void OnGetUserInfoSuccess(const UserInfoMap& data) OVERRIDE;
virtual void OnGetUserInfoFailure(const GoogleServiceAuthError& error)
OVERRIDE;
const std::string account_id_;
ProfileOAuth2TokenService* token_service_;
GaiaAuthFetcher gaia_auth_fetcher_;
ResultCallback callback_;
scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_;
DISALLOW_COPY_AND_ASSIGN(AccountServiceFlagFetcher);
};
#endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_SERVICE_FLAG_FETCHER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment