Commit ec40eb01 authored by anthonyvd's avatar anthonyvd Committed by Commit bot

Add service flags to the AccountTrackerService.

BUG=466799

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

Cr-Commit-Position: refs/heads/master@{#330095}
parent 80341c95
...@@ -39,7 +39,8 @@ void FakeAccountTrackerService::FakeUserInfoFetchSuccess( ...@@ -39,7 +39,8 @@ void FakeAccountTrackerService::FakeUserInfoFetchSuccess(
user_info.SetString("id", gaia); user_info.SetString("id", gaia);
user_info.SetString("email", email); user_info.SetString("email", email);
user_info.SetString("hd", hosted_domain); user_info.SetString("hd", hosted_domain);
SetAccountStateFromUserInfo(account_id, &user_info); std::vector<std::string> service_flags;
SetAccountStateFromUserInfo(account_id, &user_info, &service_flags);
} }
void FakeAccountTrackerService::SendRefreshTokenAnnotationRequest( void FakeAccountTrackerService::SendRefreshTokenAnnotationRequest(
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/metrics/field_trial.h" #include "base/metrics/field_trial.h"
#include "base/prefs/scoped_user_pref_update.h" #include "base/prefs/scoped_user_pref_update.h"
#include "base/profiler/scoped_tracker.h" #include "base/profiler/scoped_tracker.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/trace_event/trace_event.h" #include "base/trace_event/trace_event.h"
#include "components/signin/core/browser/refresh_token_annotation_request.h" #include "components/signin/core/browser/refresh_token_annotation_request.h"
...@@ -17,6 +18,8 @@ ...@@ -17,6 +18,8 @@
#include "components/signin/core/browser/signin_manager.h" #include "components/signin/core/browser/signin_manager.h"
#include "components/signin/core/common/signin_pref_names.h" #include "components/signin/core/common/signin_pref_names.h"
#include "components/signin/core/common/signin_switches.h" #include "components/signin/core/common/signin_switches.h"
#include "google_apis/gaia/gaia_auth_consumer.h"
#include "google_apis/gaia/gaia_auth_fetcher.h"
#include "google_apis/gaia/gaia_auth_util.h" #include "google_apis/gaia/gaia_auth_util.h"
#include "google_apis/gaia/gaia_constants.h" #include "google_apis/gaia/gaia_constants.h"
#include "google_apis/gaia/gaia_oauth_client.h" #include "google_apis/gaia/gaia_oauth_client.h"
...@@ -31,6 +34,7 @@ const char kAccountHostedDomainPath[] = "hd"; ...@@ -31,6 +34,7 @@ const char kAccountHostedDomainPath[] = "hd";
const char kAccountFullNamePath[] = "full_name"; const char kAccountFullNamePath[] = "full_name";
const char kAccountGivenNamePath[] = "given_name"; const char kAccountGivenNamePath[] = "given_name";
const char kAccountLocalePath[] = "locale"; const char kAccountLocalePath[] = "locale";
const char kAccountServiceFlagsPath[] = "service_flags";
const base::TimeDelta kRefreshFromTokenServiceDelay = const base::TimeDelta kRefreshFromTokenServiceDelay =
base::TimeDelta::FromHours(24); base::TimeDelta::FromHours(24);
...@@ -50,7 +54,8 @@ bool IsRefreshTokenDeviceIdExperimentEnabled() { ...@@ -50,7 +54,8 @@ bool IsRefreshTokenDeviceIdExperimentEnabled() {
const char AccountTrackerService::kNoHostedDomainFound[] = "NO_HOSTED_DOMAIN"; const char AccountTrackerService::kNoHostedDomainFound[] = "NO_HOSTED_DOMAIN";
class AccountInfoFetcher : public OAuth2TokenService::Consumer, class AccountInfoFetcher : public OAuth2TokenService::Consumer,
public gaia::GaiaOAuthClient::Delegate { public gaia::GaiaOAuthClient::Delegate,
public GaiaAuthConsumer {
public: public:
AccountInfoFetcher(OAuth2TokenService* token_service, AccountInfoFetcher(OAuth2TokenService* token_service,
net::URLRequestContextGetter* request_context_getter, net::URLRequestContextGetter* request_context_getter,
...@@ -61,6 +66,7 @@ class AccountInfoFetcher : public OAuth2TokenService::Consumer, ...@@ -61,6 +66,7 @@ class AccountInfoFetcher : public OAuth2TokenService::Consumer,
const std::string& account_id() { return account_id_; } const std::string& account_id() { return account_id_; }
void Start(); void Start();
void SendSuccessIfDoneFetching();
// OAuth2TokenService::Consumer implementation. // OAuth2TokenService::Consumer implementation.
void OnGetTokenSuccess(const OAuth2TokenService::Request* request, void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
...@@ -75,6 +81,12 @@ class AccountInfoFetcher : public OAuth2TokenService::Consumer, ...@@ -75,6 +81,12 @@ class AccountInfoFetcher : public OAuth2TokenService::Consumer,
void OnOAuthError() override; void OnOAuthError() override;
void OnNetworkError(int response_code) override; void OnNetworkError(int response_code) override;
// Overridden from GaiaAuthConsumer:
void OnClientLoginSuccess(const ClientLoginResult& result) override;
void OnClientLoginFailure(const GoogleServiceAuthError& error) override;
void OnGetUserInfoSuccess(const UserInfoMap& data) override;
void OnGetUserInfoFailure(const GoogleServiceAuthError& error) override;
private: private:
OAuth2TokenService* token_service_; OAuth2TokenService* token_service_;
net::URLRequestContextGetter* request_context_getter_; net::URLRequestContextGetter* request_context_getter_;
...@@ -83,6 +95,10 @@ class AccountInfoFetcher : public OAuth2TokenService::Consumer, ...@@ -83,6 +95,10 @@ class AccountInfoFetcher : public OAuth2TokenService::Consumer,
scoped_ptr<OAuth2TokenService::Request> login_token_request_; scoped_ptr<OAuth2TokenService::Request> login_token_request_;
scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_;
scoped_ptr<GaiaAuthFetcher> gaia_auth_fetcher_;
scoped_ptr<base::DictionaryValue> fetched_user_info_;
scoped_ptr<std::vector<std::string> > fetched_service_flags_;
}; };
AccountInfoFetcher::AccountInfoFetcher( AccountInfoFetcher::AccountInfoFetcher(
...@@ -108,10 +124,18 @@ void AccountInfoFetcher::Start() { ...@@ -108,10 +124,18 @@ void AccountInfoFetcher::Start() {
OAuth2TokenService::ScopeSet scopes; OAuth2TokenService::ScopeSet scopes;
scopes.insert(GaiaConstants::kGoogleUserInfoEmail); scopes.insert(GaiaConstants::kGoogleUserInfoEmail);
scopes.insert(GaiaConstants::kGoogleUserInfoProfile); scopes.insert(GaiaConstants::kGoogleUserInfoProfile);
scopes.insert(GaiaConstants::kOAuth1LoginScope);
login_token_request_ = token_service_->StartRequest( login_token_request_ = token_service_->StartRequest(
account_id_, scopes, this); account_id_, scopes, this);
} }
void AccountInfoFetcher::SendSuccessIfDoneFetching() {
if (fetched_user_info_ && fetched_service_flags_) {
service_->OnUserInfoFetchSuccess(
this, fetched_user_info_.get(), fetched_service_flags_.get());
}
}
void AccountInfoFetcher::OnGetTokenSuccess( void AccountInfoFetcher::OnGetTokenSuccess(
const OAuth2TokenService::Request* request, const OAuth2TokenService::Request* request,
const std::string& access_token, const std::string& access_token,
...@@ -121,9 +145,14 @@ void AccountInfoFetcher::OnGetTokenSuccess( ...@@ -121,9 +145,14 @@ void AccountInfoFetcher::OnGetTokenSuccess(
DCHECK_EQ(request, login_token_request_.get()); DCHECK_EQ(request, login_token_request_.get());
gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(request_context_getter_)); gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(request_context_getter_));
const int kMaxRetries = 3; const int kMaxRetries = 3;
gaia_oauth_client_->GetUserInfo(access_token, kMaxRetries, this); gaia_oauth_client_->GetUserInfo(access_token, kMaxRetries, this);
gaia_auth_fetcher_.reset(
new GaiaAuthFetcher(
this, GaiaConstants::kChromeSource, request_context_getter_));
gaia_auth_fetcher_->StartOAuthLogin(
access_token, GaiaConstants::kGaiaService);
} }
void AccountInfoFetcher::OnGetTokenFailure( void AccountInfoFetcher::OnGetTokenFailure(
...@@ -148,7 +177,36 @@ void AccountInfoFetcher::OnGetUserInfoResponse( ...@@ -148,7 +177,36 @@ void AccountInfoFetcher::OnGetUserInfoResponse(
"OnGetUserInfoResponse", "OnGetUserInfoResponse",
"account_id", "account_id",
account_id_); account_id_);
service_->OnUserInfoFetchSuccess(this, user_info.get()); fetched_user_info_ = user_info.Pass();
SendSuccessIfDoneFetching();
}
void AccountInfoFetcher::OnClientLoginSuccess(
const ClientLoginResult& result) {
gaia_auth_fetcher_->StartGetUserInfo(result.lsid);
}
void AccountInfoFetcher::OnClientLoginFailure(
const GoogleServiceAuthError& error) {
service_->OnUserInfoFetchFailure(this);
}
void AccountInfoFetcher::OnGetUserInfoSuccess(const UserInfoMap& data) {
fetched_service_flags_.reset(new std::vector<std::string>);
UserInfoMap::const_iterator services_iter = data.find("allServices");
if (services_iter != data.end()) {
base::SplitString(services_iter->second, ',', fetched_service_flags_.get());
SendSuccessIfDoneFetching();
} else {
DLOG(WARNING) << "AccountInfoFetcher::OnGetUserInfoSuccess: "
<< "GetUserInfo response didn't include allServices field.";
service_->OnUserInfoFetchFailure(this);
}
}
void AccountInfoFetcher::OnGetUserInfoFailure(
const GoogleServiceAuthError& error) {
service_->OnUserInfoFetchFailure(this);
} }
void AccountInfoFetcher::OnOAuthError() { void AccountInfoFetcher::OnOAuthError() {
...@@ -440,7 +498,8 @@ void AccountTrackerService::StartFetchingUserInfo( ...@@ -440,7 +498,8 @@ void AccountTrackerService::StartFetchingUserInfo(
void AccountTrackerService::SetAccountStateFromUserInfo( void AccountTrackerService::SetAccountStateFromUserInfo(
const std::string& account_id, const std::string& account_id,
const base::DictionaryValue* user_info) { const base::DictionaryValue* user_info,
const std::vector<std::string>* service_flags) {
AccountState& state = accounts_[account_id]; AccountState& state = accounts_[account_id];
std::string gaia_id; std::string gaia_id;
...@@ -461,6 +520,8 @@ void AccountTrackerService::SetAccountStateFromUserInfo( ...@@ -461,6 +520,8 @@ void AccountTrackerService::SetAccountStateFromUserInfo(
user_info->GetString("given_name", &state.info.given_name); user_info->GetString("given_name", &state.info.given_name);
user_info->GetString("locale", &state.info.locale); user_info->GetString("locale", &state.info.locale);
state.info.service_flags = *service_flags;
NotifyAccountUpdated(state); NotifyAccountUpdated(state);
SaveToPrefs(state); SaveToPrefs(state);
} }
...@@ -468,11 +529,12 @@ void AccountTrackerService::SetAccountStateFromUserInfo( ...@@ -468,11 +529,12 @@ void AccountTrackerService::SetAccountStateFromUserInfo(
void AccountTrackerService::OnUserInfoFetchSuccess( void AccountTrackerService::OnUserInfoFetchSuccess(
AccountInfoFetcher* fetcher, AccountInfoFetcher* fetcher,
const base::DictionaryValue* user_info) { const base::DictionaryValue* user_info,
const std::vector<std::string>* service_flags) {
const std::string& account_id = fetcher->account_id(); const std::string& account_id = fetcher->account_id();
DCHECK(ContainsKey(accounts_, account_id)); DCHECK(ContainsKey(accounts_, account_id));
SetAccountStateFromUserInfo(account_id, user_info); SetAccountStateFromUserInfo(account_id, user_info, service_flags);
DeleteFetcher(fetcher); DeleteFetcher(fetcher);
} }
...@@ -516,6 +578,18 @@ void AccountTrackerService::LoadFromPrefs() { ...@@ -516,6 +578,18 @@ void AccountTrackerService::LoadFromPrefs() {
state.info.given_name = base::UTF16ToUTF8(value); state.info.given_name = base::UTF16ToUTF8(value);
if (dict->GetString(kAccountLocalePath, &value)) if (dict->GetString(kAccountLocalePath, &value))
state.info.locale = base::UTF16ToUTF8(value); state.info.locale = base::UTF16ToUTF8(value);
const base::ListValue* service_flags_list;
if (dict->GetList(kAccountServiceFlagsPath, &service_flags_list)) {
std::string flag;
for(base::Value* flag: *service_flags_list) {
std::string flag_string;
if(flag->GetAsString(&flag_string)) {
state.info.service_flags.push_back(flag_string);
}
}
}
if (state.info.IsValid()) if (state.info.IsValid())
NotifyAccountUpdated(state); NotifyAccountUpdated(state);
} }
...@@ -552,6 +626,12 @@ void AccountTrackerService::SaveToPrefs(const AccountState& state) { ...@@ -552,6 +626,12 @@ void AccountTrackerService::SaveToPrefs(const AccountState& state) {
dict->SetString(kAccountFullNamePath, state.info.full_name); dict->SetString(kAccountFullNamePath, state.info.full_name);
dict->SetString(kAccountGivenNamePath, state.info.given_name); dict->SetString(kAccountGivenNamePath, state.info.given_name);
dict->SetString(kAccountLocalePath, state.info.locale); dict->SetString(kAccountLocalePath, state.info.locale);
scoped_ptr<base::ListValue> service_flags_list;
service_flags_list.reset(new base::ListValue);
service_flags_list->AppendStrings(state.info.service_flags);
dict->Set(kAccountServiceFlagsPath, service_flags_list.Pass());
} }
void AccountTrackerService::RemoveFromPrefs(const AccountState& state) { void AccountTrackerService::RemoveFromPrefs(const AccountState& state) {
......
...@@ -58,6 +58,7 @@ class AccountTrackerService : public KeyedService, ...@@ -58,6 +58,7 @@ class AccountTrackerService : public KeyedService,
std::string given_name; std::string given_name;
std::string hosted_domain; std::string hosted_domain;
std::string locale; std::string locale;
std::vector<std::string> service_flags;
// TODO(rogerta): eventually this structure will include other information // TODO(rogerta): eventually this structure will include other information
// about the account, like full name, profile picture URL, etc. // about the account, like full name, profile picture URL, etc.
...@@ -130,15 +131,18 @@ class AccountTrackerService : public KeyedService, ...@@ -130,15 +131,18 @@ class AccountTrackerService : public KeyedService,
protected: protected:
// Available to be called in tests. // Available to be called in tests.
void SetAccountStateFromUserInfo(const std::string& account_id, void SetAccountStateFromUserInfo(
const base::DictionaryValue* user_info); const std::string& account_id,
const base::DictionaryValue* user_info,
const std::vector<std::string>* service_flags);
private: private:
friend class AccountInfoFetcher; friend class AccountInfoFetcher;
// These methods are called by fetchers. // These methods are called by fetchers.
void OnUserInfoFetchSuccess(AccountInfoFetcher* fetcher, void OnUserInfoFetchSuccess(AccountInfoFetcher* fetcher,
const base::DictionaryValue* user_info); const base::DictionaryValue* user_info,
const std::vector<std::string>* service_flags);
void OnUserInfoFetchFailure(AccountInfoFetcher* fetcher); void OnUserInfoFetchFailure(AccountInfoFetcher* fetcher);
// Refreshes the AccountInfo associated with |account_id| if it's invalid or // Refreshes the AccountInfo associated with |account_id| if it's invalid or
......
...@@ -39,6 +39,10 @@ const std::string kTokenInfoIncompleteResponseFormat = ...@@ -39,6 +39,10 @@ const std::string kTokenInfoIncompleteResponseFormat =
\"hd\": \"\", \ \"hd\": \"\", \
}"; }";
const std::string kLSIDResponse = "{ lsid: \"Foo\" }";
const std::string kServiceFlags = "allServices=Service1,Service2";
enum TrackingEventType { enum TrackingEventType {
UPDATED, UPDATED,
REMOVED, REMOVED,
...@@ -74,6 +78,9 @@ void CheckAccountDetails(const std::string account_id, ...@@ -74,6 +78,9 @@ void CheckAccountDetails(const std::string account_id,
EXPECT_EQ(AccountIdToFullName(account_id), info.full_name); EXPECT_EQ(AccountIdToFullName(account_id), info.full_name);
EXPECT_EQ(AccountIdToGivenName(account_id), info.given_name); EXPECT_EQ(AccountIdToGivenName(account_id), info.given_name);
EXPECT_EQ(AccountIdToLocale(account_id), info.locale); EXPECT_EQ(AccountIdToLocale(account_id), info.locale);
EXPECT_EQ(2U, info.service_flags.size());
EXPECT_EQ("Service1", info.service_flags[0]);
EXPECT_EQ("Service2", info.service_flags[1]);
} }
class TrackingEvent { class TrackingEvent {
...@@ -340,6 +347,8 @@ void AccountTrackerServiceTest::ReturnOAuthUrlFetchSuccess( ...@@ -340,6 +347,8 @@ void AccountTrackerServiceTest::ReturnOAuthUrlFetchSuccess(
ReturnOAuthUrlFetchResults(gaia::GaiaOAuthClient::kUrlFetcherId, ReturnOAuthUrlFetchResults(gaia::GaiaOAuthClient::kUrlFetcherId,
net::HTTP_OK, net::HTTP_OK,
GenerateValidTokenInfoResponse(account_id)); GenerateValidTokenInfoResponse(account_id));
ReturnOAuthUrlFetchResults(0, net::HTTP_OK, kLSIDResponse);
ReturnOAuthUrlFetchResults(0, net::HTTP_OK, kServiceFlags);
} }
void AccountTrackerServiceTest::ReturnOAuthUrlFetchSuccessIncomplete( void AccountTrackerServiceTest::ReturnOAuthUrlFetchSuccessIncomplete(
......
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