From b554dd57b292f84edaf296184d22f2fff5c5f7d6 Mon Sep 17 00:00:00 2001 From: "thestig@chromium.org" Date: Fri, 22 Aug 2014 05:38:00 +0000 Subject: [PATCH] Revert of Create a new AccountTrackerService (patchset #8 of https://codereview.chromium.org/425823002/) Reason for revert: Likely causing chrome.browser.signin.OAuth2TokenServiceIntegrationTest failures on Android. TBR=rogerta@chromium.org NOTREECHECKS=true NOTRY=true Original issue's description: > Create a new AccountTrackerService PKS based on the existing AccountTracker. > > This CL is one part of the effort to change chrome to use gaiaid instead of > email address as the unique id for an account. > > BUG=341408 > > Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=291263 Review URL: https://codereview.chromium.org/490403002 Cr-Commit-Position: refs/heads/master@{#291335} git-svn-id: svn://svn.chromium.org/chrome/trunk/src@291335 0039d316-1c4b-4281-b951-d872f2087c98 --- .../chrome_browser_main_extra_parts_profiles.cc | 2 - chrome/browser/profiles/profile_manager.cc | 2 - .../signin/account_tracker_service_factory.cc | 53 --- .../signin/account_tracker_service_factory.h | 44 -- chrome/chrome_browser.gypi | 2 - components/components_tests.gyp | 1 - components/signin.gypi | 2 - .../signin/core/browser/account_tracker_service.cc | 431 ----------------- .../signin/core/browser/account_tracker_service.h | 117 ----- .../browser/account_tracker_service_unittest.cc | 509 --------------------- 10 files changed, 1163 deletions(-) delete mode 100644 chrome/browser/signin/account_tracker_service_factory.cc delete mode 100644 chrome/browser/signin/account_tracker_service_factory.h delete mode 100644 components/signin/core/browser/account_tracker_service.cc delete mode 100644 components/signin/core/browser/account_tracker_service.h delete mode 100644 components/signin/core/browser/account_tracker_service_unittest.cc diff --git a/chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.cc b/chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.cc index ea05a19d12d7..20a7f9448487 100644 --- a/chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.cc +++ b/chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.cc @@ -37,7 +37,6 @@ #include "chrome/browser/sessions/session_service_factory.h" #include "chrome/browser/sessions/tab_restore_service_factory.h" #include "chrome/browser/signin/about_signin_internals_factory.h" -#include "chrome/browser/signin/account_tracker_service_factory.h" #include "chrome/browser/signin/signin_manager_factory.h" #include "chrome/browser/sync/profile_sync_service_factory.h" #include "chrome/browser/themes/theme_service_factory.h" @@ -150,7 +149,6 @@ EnsureBrowserContextKeyedServiceFactoriesBuilt() { #endif AboutSigninInternalsFactory::GetInstance(); - AccountTrackerServiceFactory::GetInstance(); autofill::PersonalDataManagerFactory::GetInstance(); #if defined(ENABLE_BACKGROUND) BackgroundContentsServiceFactory::GetInstance(); diff --git a/chrome/browser/profiles/profile_manager.cc b/chrome/browser/profiles/profile_manager.cc index 4047bef6ecf6..9d410c58537b 100644 --- a/chrome/browser/profiles/profile_manager.cc +++ b/chrome/browser/profiles/profile_manager.cc @@ -36,7 +36,6 @@ #include "chrome/browser/profiles/startup_task_runner_service.h" #include "chrome/browser/profiles/startup_task_runner_service_factory.h" #include "chrome/browser/signin/account_reconcilor_factory.h" -#include "chrome/browser/signin/account_tracker_service_factory.h" #include "chrome/browser/sync/profile_sync_service.h" #include "chrome/browser/sync/profile_sync_service_factory.h" #include "chrome/browser/ui/browser.h" @@ -1014,7 +1013,6 @@ void ProfileManager::DoFinalInitForServices(Profile* profile, StartupTaskRunnerServiceFactory::GetForProfile(profile)-> StartDeferredTaskRunners(); - AccountTrackerServiceFactory::GetForProfile(profile); AccountReconcilorFactory::GetForProfile(profile); } diff --git a/chrome/browser/signin/account_tracker_service_factory.cc b/chrome/browser/signin/account_tracker_service_factory.cc deleted file mode 100644 index 0ad58278a6c5..000000000000 --- a/chrome/browser/signin/account_tracker_service_factory.cc +++ /dev/null @@ -1,53 +0,0 @@ -// 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 "chrome/browser/signin/account_tracker_service_factory.h" - -#include "base/memory/scoped_ptr.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" -#include "components/keyed_service/content/browser_context_dependency_manager.h" -#include "components/pref_registry/pref_registry_syncable.h" -#include "components/signin/core/browser/account_tracker_service.h" -#include "components/signin/core/browser/profile_oauth2_token_service.h" - -AccountTrackerServiceFactory::AccountTrackerServiceFactory() - : BrowserContextKeyedServiceFactory( - "AccountTrackerServiceFactory", - BrowserContextDependencyManager::GetInstance()) { - DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); -} - -AccountTrackerServiceFactory::~AccountTrackerServiceFactory() { -} - -// static -AccountTrackerService* -AccountTrackerServiceFactory::GetForProfile(Profile* profile) { - return static_cast( - GetInstance()->GetServiceForBrowserContext(profile, true)); -} - -// static -AccountTrackerServiceFactory* AccountTrackerServiceFactory::GetInstance() { - return Singleton::get(); -} - -void AccountTrackerServiceFactory::RegisterProfilePrefs( - user_prefs::PrefRegistrySyncable* registry) { - registry->RegisterListPref( - AccountTrackerService::kAccountInfoPref, - user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); -} - -KeyedService* AccountTrackerServiceFactory::BuildServiceInstanceFor( - content::BrowserContext* context) const { - Profile* profile = static_cast(context); - AccountTrackerService* service = new AccountTrackerService(); - service->Initialize( - ProfileOAuth2TokenServiceFactory::GetForProfile(profile), - profile->GetPrefs(), - profile->GetRequestContext()); - return service; -} diff --git a/chrome/browser/signin/account_tracker_service_factory.h b/chrome/browser/signin/account_tracker_service_factory.h deleted file mode 100644 index a38d1f93ffc8..000000000000 --- a/chrome/browser/signin/account_tracker_service_factory.h +++ /dev/null @@ -1,44 +0,0 @@ -// 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 CHROME_BROWSER_SIGNIN_ACCOUNT_TRACKER_SERVICE_FACTORY_H_ -#define CHROME_BROWSER_SIGNIN_ACCOUNT_TRACKER_SERVICE_FACTORY_H_ - -#include "base/memory/singleton.h" -#include "components/keyed_service/content/browser_context_keyed_service_factory.h" - -class AccountTrackerService; -class Profile; - -// Singleton that owns all AccountTrackerServices and associates them with -// Profiles. Listens for the Profile's destruction notification and cleans up -// the associated AccountTrackerService. -class AccountTrackerServiceFactory - : public BrowserContextKeyedServiceFactory { - public: - // Returns the instance of AccountTrackerService associated with this - // profile (creating one if none exists). Returns NULL if this profile - // cannot have a AccountTrackerService (for example, if |profile| is - // incognito). - static AccountTrackerService* GetForProfile(Profile* profile); - - // Returns an instance of the AccountTrackerServiceFactory singleton. - static AccountTrackerServiceFactory* GetInstance(); - - private: - friend struct DefaultSingletonTraits; - - AccountTrackerServiceFactory(); - virtual ~AccountTrackerServiceFactory(); - - // BrowserContextKeyedServiceFactory implementation. - virtual void RegisterProfilePrefs( - user_prefs::PrefRegistrySyncable* registry) OVERRIDE; - virtual KeyedService* BuildServiceInstanceFor( - content::BrowserContext* context) const OVERRIDE; - - DISALLOW_COPY_AND_ASSIGN(AccountTrackerServiceFactory); -}; - -#endif // CHROME_BROWSER_SIGNIN_ACCOUNT_TRACKER_SERVICE_FACTORY_H_ diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 26d0a5ea4df2..f4d423804806 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1180,8 +1180,6 @@ 'browser/signin/about_signin_internals_factory.h', 'browser/signin/account_reconcilor_factory.cc', 'browser/signin/account_reconcilor_factory.h', - 'browser/signin/account_tracker_service_factory.cc', - 'browser/signin/account_tracker_service_factory.h', 'browser/signin/chrome_signin_client.cc', 'browser/signin/chrome_signin_client.h', 'browser/signin/chrome_signin_client_factory.cc', diff --git a/components/components_tests.gyp b/components/components_tests.gyp index c2f376472ff7..e58c5598e971 100644 --- a/components/components_tests.gyp +++ b/components/components_tests.gyp @@ -179,7 +179,6 @@ 'search_provider_logos/logo_cache_unittest.cc', 'search_provider_logos/logo_tracker_unittest.cc', 'sessions/serialized_navigation_entry_unittest.cc', - 'signin/core/browser/account_tracker_service_unittest.cc', 'signin/core/browser/mutable_profile_oauth2_token_service_unittest.cc', 'signin/core/browser/signin_error_controller_unittest.cc', 'signin/core/browser/webdata/token_service_table_unittest.cc', diff --git a/components/signin.gypi b/components/signin.gypi index 5f9993ef9a85..4270ba711417 100644 --- a/components/signin.gypi +++ b/components/signin.gypi @@ -48,8 +48,6 @@ '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/account_tracker_service.cc', - 'signin/core/browser/account_tracker_service.h', 'signin/core/browser/mutable_profile_oauth2_token_service.cc', 'signin/core/browser/mutable_profile_oauth2_token_service.h', 'signin/core/browser/profile_oauth2_token_service.cc', diff --git a/components/signin/core/browser/account_tracker_service.cc b/components/signin/core/browser/account_tracker_service.cc deleted file mode 100644 index 4f205bf7018a..000000000000 --- a/components/signin/core/browser/account_tracker_service.cc +++ /dev/null @@ -1,431 +0,0 @@ -// 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_tracker_service.h" - -#include "base/debug/trace_event.h" -#include "base/prefs/pref_service.h" -#include "base/prefs/scoped_user_pref_update.h" -#include "base/strings/utf_string_conversions.h" -#include "components/signin/core/browser/signin_manager.h" -#include "google_apis/gaia/gaia_auth_util.h" -#include "google_apis/gaia/gaia_constants.h" -#include "google_apis/gaia/gaia_oauth_client.h" -#include "google_apis/gaia/oauth2_token_service.h" -#include "net/url_request/url_request_context_getter.h" - -namespace { - -const char kAccountKeyPath[] = "account_id"; -const char kAccountEmailPath[] = "email"; -const char kAccountGaiaPath[] = "gaia"; - -} - -class AccountInfoFetcher : public OAuth2TokenService::Consumer, - public gaia::GaiaOAuthClient::Delegate { - public: - AccountInfoFetcher(OAuth2TokenService* token_service, - net::URLRequestContextGetter* request_context_getter, - AccountTrackerService* service, - const std::string& account_id); - virtual ~AccountInfoFetcher(); - - const std::string& account_id() { return account_id_; } - - void Start(); - - // OAuth2TokenService::Consumer implementation. - 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; - - // gaia::GaiaOAuthClient::Delegate implementation. - virtual void OnGetUserInfoResponse( - scoped_ptr user_info) OVERRIDE; - virtual void OnOAuthError() OVERRIDE; - virtual void OnNetworkError(int response_code) OVERRIDE; - - private: - OAuth2TokenService* token_service_; - net::URLRequestContextGetter* request_context_getter_; - AccountTrackerService* service_; - const std::string account_id_; - - scoped_ptr login_token_request_; - scoped_ptr gaia_oauth_client_; -}; - -AccountInfoFetcher::AccountInfoFetcher( - OAuth2TokenService* token_service, - net::URLRequestContextGetter* request_context_getter, - AccountTrackerService* service, - const std::string& account_id) - : OAuth2TokenService::Consumer("gaia_account_tracker"), - token_service_(token_service), - request_context_getter_(request_context_getter), - service_(service), - account_id_(account_id) { - TRACE_EVENT_ASYNC_BEGIN1( - "AccountTrackerService", "AccountIdFetcher", this, - "account_id", account_id); -} - -AccountInfoFetcher::~AccountInfoFetcher() { - TRACE_EVENT_ASYNC_END0("AccountTrackerService", "AccountIdFetcher", this); -} - -void AccountInfoFetcher::Start() { - OAuth2TokenService::ScopeSet scopes; - scopes.insert(GaiaConstants::kGoogleUserInfoEmail); - scopes.insert(GaiaConstants::kGoogleUserInfoProfile); - login_token_request_ = token_service_->StartRequest( - account_id_, scopes, this); -} - -void AccountInfoFetcher::OnGetTokenSuccess( - const OAuth2TokenService::Request* request, - const std::string& access_token, - const base::Time& expiration_time) { - TRACE_EVENT_ASYNC_STEP_PAST0( - "AccountTrackerService", "AccountIdFetcher", this, "OnGetTokenSuccess"); - DCHECK_EQ(request, login_token_request_.get()); - - gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(request_context_getter_)); - - const int kMaxRetries = 3; - gaia_oauth_client_->GetUserInfo(access_token, kMaxRetries, this); -} - -void AccountInfoFetcher::OnGetTokenFailure( - const OAuth2TokenService::Request* request, - const GoogleServiceAuthError& error) { - TRACE_EVENT_ASYNC_STEP_PAST1("AccountTrackerService", - "AccountIdFetcher", - this, - "OnGetTokenFailure", - "google_service_auth_error", - error.ToString()); - LOG(ERROR) << "OnGetTokenFailure: " << error.ToString(); - DCHECK_EQ(request, login_token_request_.get()); - service_->OnUserInfoFetchFailure(this); -} - -void AccountInfoFetcher::OnGetUserInfoResponse( - scoped_ptr user_info) { - TRACE_EVENT_ASYNC_STEP_PAST1("AccountTrackerService", - "AccountIdFetcher", - this, - "OnGetUserInfoResponse", - "account_id", - account_id_); - service_->OnUserInfoFetchSuccess(this, user_info.get()); -} - -void AccountInfoFetcher::OnOAuthError() { - TRACE_EVENT_ASYNC_STEP_PAST0( - "AccountTrackerService", "AccountIdFetcher", this, "OnOAuthError"); - LOG(ERROR) << "OnOAuthError"; - service_->OnUserInfoFetchFailure(this); -} - -void AccountInfoFetcher::OnNetworkError(int response_code) { - TRACE_EVENT_ASYNC_STEP_PAST1("AccountTrackerService", - "AccountIdFetcher", - this, - "OnNetworkError", - "response_code", - response_code); - LOG(ERROR) << "OnNetworkError " << response_code; - service_->OnUserInfoFetchFailure(this); -} - - -const char AccountTrackerService::kAccountInfoPref[] = "account_info"; - -AccountTrackerService::AccountTrackerService() - : token_service_(NULL), - pref_service_(NULL), - shutdown_called_(false) { -} - -AccountTrackerService::~AccountTrackerService() { - DCHECK(shutdown_called_); -} - -void AccountTrackerService::Initialize( - OAuth2TokenService* token_service, - PrefService* pref_service, - net::URLRequestContextGetter* request_context_getter) { - DCHECK(token_service); - DCHECK(!token_service_); - DCHECK(pref_service); - DCHECK(!pref_service_); - token_service_ = token_service; - pref_service_ = pref_service; - request_context_getter_ = request_context_getter; - token_service_->AddObserver(this); - LoadFromPrefs(); - LoadFromTokenService(); -} - -void AccountTrackerService::Shutdown() { - shutdown_called_ = true; - STLDeleteValues(&user_info_requests_); - token_service_->RemoveObserver(this); -} - -void AccountTrackerService::AddObserver(Observer* observer) { - observer_list_.AddObserver(observer); -} - -void AccountTrackerService::RemoveObserver(Observer* observer) { - observer_list_.RemoveObserver(observer); -} - -bool AccountTrackerService::IsAllUserInfoFetched() const { - return user_info_requests_.empty(); -} - -std::vector -AccountTrackerService::GetAccounts() const { - std::vector accounts; - - for (std::map::const_iterator it = - accounts_.begin(); - it != accounts_.end(); - ++it) { - const AccountState& state = it->second; - accounts.push_back(state.info); - } - return accounts; -} - -AccountTrackerService::AccountInfo AccountTrackerService::GetAccountInfo( - const std::string& account_id) { - if (ContainsKey(accounts_, account_id)) - return accounts_[account_id].info; - - return AccountInfo(); -} - -AccountTrackerService::AccountInfo -AccountTrackerService::FindAccountInfoByGaiaId( - const std::string& gaia_id) { - for (std::map::const_iterator it = - accounts_.begin(); - it != accounts_.end(); - ++it) { - const AccountState& state = it->second; - if (state.info.gaia == gaia_id) - return state.info; - } - - return AccountInfo(); -} - -AccountTrackerService::AccountInfo -AccountTrackerService::FindAccountInfoByEmail( - const std::string& email) { - for (std::map::const_iterator it = - accounts_.begin(); - it != accounts_.end(); - ++it) { - const AccountState& state = it->second; - if (gaia::AreEmailsSame(state.info.email, email)) - return state.info; - } - - return AccountInfo(); -} - -void AccountTrackerService::OnRefreshTokenAvailable( - const std::string& account_id) { - TRACE_EVENT1("AccountTrackerService", - "AccountTracker::OnRefreshTokenAvailable", - "account_id", - account_id); - DVLOG(1) << "AVAILABLE " << account_id; - - StartTrackingAccount(account_id); - AccountState& state = accounts_[account_id]; - - if (state.info.gaia.empty()) - StartFetchingUserInfo(account_id); -} - -void AccountTrackerService::OnRefreshTokenRevoked( - const std::string& account_id) { - TRACE_EVENT1("AccountTrackerService", - "AccountTracker::OnRefreshTokenRevoked", - "account_id", - account_id); - - DVLOG(1) << "REVOKED " << account_id; - StopTrackingAccount(account_id); -} - -void AccountTrackerService::NotifyAccountUpdated(const AccountState& state) { - DCHECK(!state.info.gaia.empty()); - FOR_EACH_OBSERVER( - Observer, observer_list_, OnAccountUpdated(state.info)); -} - -void AccountTrackerService::NotifyAccountRemoved(const AccountState& state) { - DCHECK(!state.info.gaia.empty()); - FOR_EACH_OBSERVER( - Observer, observer_list_, OnAccountRemoved(state.info)); -} - -void AccountTrackerService::StartTrackingAccount( - const std::string& account_id) { - if (!ContainsKey(accounts_, account_id)) { - DVLOG(1) << "StartTracking " << account_id; - AccountState state; - state.info.account_id = account_id; - accounts_.insert(make_pair(account_id, state)); - } -} - -void AccountTrackerService::StopTrackingAccount(const std::string& account_id) { - DVLOG(1) << "StopTracking " << account_id; - if (ContainsKey(accounts_, account_id)) { - AccountState& state = accounts_[account_id]; - RemoveFromPrefs(state); - if (!state.info.gaia.empty()) - NotifyAccountRemoved(state); - - accounts_.erase(account_id); - } - - if (ContainsKey(user_info_requests_, account_id)) - DeleteFetcher(user_info_requests_[account_id]); -} - -void AccountTrackerService::StartFetchingUserInfo( - const std::string& account_id) { - if (ContainsKey(user_info_requests_, account_id)) - DeleteFetcher(user_info_requests_[account_id]); - - DVLOG(1) << "StartFetching " << account_id; - AccountInfoFetcher* fetcher = - new AccountInfoFetcher(token_service_, - request_context_getter_.get(), - this, - account_id); - user_info_requests_[account_id] = fetcher; - fetcher->Start(); -} - -void AccountTrackerService::OnUserInfoFetchSuccess( - AccountInfoFetcher* fetcher, - const base::DictionaryValue* user_info) { - const std::string& account_id = fetcher->account_id(); - DCHECK(ContainsKey(accounts_, account_id)); - AccountState& state = accounts_[account_id]; - - std::string gaia_id; - std::string email; - if (user_info->GetString("id", &gaia_id) && - user_info->GetString("email", &email)) { - state.info.gaia = gaia_id; - state.info.email = email; - - NotifyAccountUpdated(state); - SaveToPrefs(state); - } - DeleteFetcher(fetcher); -} - -void AccountTrackerService::OnUserInfoFetchFailure( - AccountInfoFetcher* fetcher) { - LOG(WARNING) << "Failed to get UserInfo for " << fetcher->account_id(); - DeleteFetcher(fetcher); - // TODO(rogerta): figure out when to retry. -} - -void AccountTrackerService::DeleteFetcher(AccountInfoFetcher* fetcher) { - DVLOG(1) << "DeleteFetcher " << fetcher->account_id(); - const std::string& account_id = fetcher->account_id(); - DCHECK(ContainsKey(user_info_requests_, account_id)); - DCHECK_EQ(fetcher, user_info_requests_[account_id]); - user_info_requests_.erase(account_id); - delete fetcher; -} - -void AccountTrackerService::LoadFromPrefs() { - const base::ListValue* list = pref_service_->GetList(kAccountInfoPref); - for (size_t i = 0; i < list->GetSize(); ++i) { - const base::DictionaryValue* dict; - if (list->GetDictionary(i, &dict)) { - base::string16 value; - if (dict->GetString(kAccountKeyPath, &value)) { - std::string account_id = base::UTF16ToUTF8(value); - StartTrackingAccount(account_id); - AccountState& state = accounts_[account_id]; - - if (dict->GetString(kAccountGaiaPath, &value)) - state.info.gaia = base::UTF16ToUTF8(value); - if (dict->GetString(kAccountEmailPath, &value)) - state.info.email = base::UTF16ToUTF8(value); - - if (!state.info.gaia.empty()) - NotifyAccountUpdated(state); - } - } - } -} - -void AccountTrackerService::SaveToPrefs(const AccountState& state) { - if (!pref_service_) - return; - - base::DictionaryValue* dict = NULL; - base::string16 account_id_16 = base::UTF8ToUTF16(state.info.account_id); - ListPrefUpdate update(pref_service_, kAccountInfoPref); - for(size_t i = 0; i < update->GetSize(); ++i, dict = NULL) { - if (update->GetDictionary(i, &dict)) { - base::string16 value; - if (dict->GetString(kAccountKeyPath, &value) && value == account_id_16) - break; - } - } - - if (!dict) { - dict = new base::DictionaryValue(); - update->Append(dict); // |update| takes ownership. - dict->SetString(kAccountKeyPath, account_id_16); - } - - dict->SetString(kAccountEmailPath, state.info.email); - dict->SetString(kAccountGaiaPath, state.info.gaia); -} - -void AccountTrackerService::RemoveFromPrefs(const AccountState& state) { - if (!pref_service_) - return; - - base::string16 account_id_16 = base::UTF8ToUTF16(state.info.account_id); - ListPrefUpdate update(pref_service_, kAccountInfoPref); - for(size_t i = 0; i < update->GetSize(); ++i) { - base::DictionaryValue* dict = NULL; - if (update->GetDictionary(i, &dict)) { - base::string16 value; - if (dict->GetString(kAccountKeyPath, &value) && value == account_id_16) { - update->Remove(i, NULL); - break; - } - } - } -} - -void AccountTrackerService::LoadFromTokenService() { - std::vector accounts = token_service_->GetAccounts(); - for (std::vector::const_iterator it = accounts.begin(); - it != accounts.end(); ++it) { - OnRefreshTokenAvailable(*it); - } -} diff --git a/components/signin/core/browser/account_tracker_service.h b/components/signin/core/browser/account_tracker_service.h deleted file mode 100644 index 36c8b8578dfc..000000000000 --- a/components/signin/core/browser/account_tracker_service.h +++ /dev/null @@ -1,117 +0,0 @@ -// 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_TRACKER_SERVICE_H_ -#define COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_ - -#include -#include -#include - -#include "base/memory/ref_counted.h" -#include "components/keyed_service/core/keyed_service.h" -#include "google_apis/gaia/oauth2_token_service.h" - -class AccountInfoFetcher; -class OAuth2TokenService; -class PrefService; - -namespace base { -class DictionaryValue; -} - -// AccountTrackerService is a KeyedService that retrieves and caches GAIA -// information about Google Accounts. -class AccountTrackerService : public KeyedService, - public OAuth2TokenService::Observer { - public: - // Name of the preference property that persists the account information - // tracked by this service. - static const char kAccountInfoPref[]; - - // Information about a specific account. - struct AccountInfo { - std::string account_id; // The account ID used by OAuth2TokenService. - std::string gaia; - std::string email; - // TODO(rogerta): eventually this structure will include other information - // about the account, like full name, profile picture URL, etc. - }; - - // Clients of AccountTrackerService can implement this interface and register - // with AddObserver() to learn about account information changes. - class Observer { - public: - virtual ~Observer() {} - virtual void OnAccountUpdated(const AccountInfo& info) = 0; - virtual void OnAccountRemoved(const AccountInfo& info) = 0; - }; - - AccountTrackerService(); - virtual ~AccountTrackerService(); - - // KeyedService implementation. - virtual void Shutdown() OVERRIDE; - - void AddObserver(Observer* observer); - void RemoveObserver(Observer* observer); - - void Initialize(OAuth2TokenService* token_service, - PrefService* pref_service, - net::URLRequestContextGetter* request_context_getter); - - // Returns the list of known accounts and for which gaia IDs - // have been fetched. - std::vector GetAccounts() const; - AccountInfo GetAccountInfo(const std::string& account_id); - AccountInfo FindAccountInfoByGaiaId(const std::string& gaia_id); - AccountInfo FindAccountInfoByEmail(const std::string& email); - - // Indicates if all user information has been fetched. If the result is false, - // there are still unfininshed fetchers. - virtual bool IsAllUserInfoFetched() const; - - private: - friend class AccountInfoFetcher; - - // These methods are called by fetchers. - void OnUserInfoFetchSuccess(AccountInfoFetcher* fetcher, - const base::DictionaryValue* user_info); - void OnUserInfoFetchFailure(AccountInfoFetcher* fetcher); - - // OAuth2TokenService::Observer implementation. - virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; - virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE; - - struct AccountState { - AccountInfo info; - }; - - void NotifyAccountUpdated(const AccountState& state); - void NotifyAccountRemoved(const AccountState& state); - - void StartTrackingAccount(const std::string& account_id); - void StopTrackingAccount(const std::string& account_id); - void StartFetchingUserInfo(const std::string& account_id); - void DeleteFetcher(AccountInfoFetcher* fetcher); - - // Load the current state of the account info from the preferences file. - void LoadFromPrefs(); - void SaveToPrefs(const AccountState& account); - void RemoveFromPrefs(const AccountState& account); - - void LoadFromTokenService(); - - OAuth2TokenService* token_service_; // Not owned. - PrefService* pref_service_; // Not owned. - scoped_refptr request_context_getter_; - std::map user_info_requests_; - std::map accounts_; - ObserverList observer_list_; - bool shutdown_called_; - - DISALLOW_COPY_AND_ASSIGN(AccountTrackerService); -}; - -#endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_TRACKER_SERVICE_H_ diff --git a/components/signin/core/browser/account_tracker_service_unittest.cc b/components/signin/core/browser/account_tracker_service_unittest.cc deleted file mode 100644 index f376d964781c..000000000000 --- a/components/signin/core/browser/account_tracker_service_unittest.cc +++ /dev/null @@ -1,509 +0,0 @@ -// 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 -#include - -#include "base/message_loop/message_loop.h" -#include "base/prefs/pref_registry_simple.h" -#include "base/prefs/testing_pref_service.h" -#include "base/strings/stringprintf.h" -#include "components/signin/core/browser/account_tracker_service.h" -#include "google_apis/gaia/fake_oauth2_token_service.h" -#include "google_apis/gaia/gaia_oauth_client.h" -#include "net/http/http_status_code.h" -#include "net/url_request/test_url_fetcher_factory.h" -#include "net/url_request/url_fetcher_delegate.h" -#include "net/url_request/url_request_test_util.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace { - -enum TrackingEventType { - UPDATED, - REMOVED, -}; - -std::string AccountIdToEmail(const std::string account_id) { - return account_id + "@gmail.com"; -} - -std::string AccountIdToGaiaId(const std::string account_id) { - return "gaia-" + account_id; -} - -class TrackingEvent { - public: - TrackingEvent(TrackingEventType type, - const std::string& account_id, - const std::string& gaia_id) - : type_(type), - account_id_(account_id), - gaia_id_(gaia_id) {} - - TrackingEvent(TrackingEventType type, - const std::string& account_id) - : type_(type), - account_id_(account_id), - gaia_id_(AccountIdToGaiaId(account_id)) {} - - bool operator==(const TrackingEvent& event) const { - return type_ == event.type_ && account_id_ == event.account_id_ && - gaia_id_ == event.gaia_id_; - } - - std::string ToString() const { - const char * typestr = "INVALID"; - switch (type_) { - case UPDATED: - typestr = "UPD"; - break; - case REMOVED: - typestr = "REM"; - break; - } - return base::StringPrintf("{ type: %s, account_id: %s, gaia: %s }", - typestr, - account_id_.c_str(), - gaia_id_.c_str()); - } - - private: - friend bool CompareByUser(TrackingEvent a, TrackingEvent b); - - TrackingEventType type_; - std::string account_id_; - std::string gaia_id_; -}; - -bool CompareByUser(TrackingEvent a, TrackingEvent b) { - return a.account_id_ < b.account_id_; -} - -std::string Str(const std::vector& events) { - std::string str = "["; - bool needs_comma = false; - for (std::vector::const_iterator it = - events.begin(); it != events.end(); ++it) { - if (needs_comma) - str += ",\n "; - needs_comma = true; - str += it->ToString(); - } - str += "]"; - return str; -} - -class AccountTrackerObserver : public AccountTrackerService::Observer { - public: - AccountTrackerObserver() {} - virtual ~AccountTrackerObserver() {} - - void Clear(); - void SortEventsByUser(); - - testing::AssertionResult CheckEvents(); - testing::AssertionResult CheckEvents(const TrackingEvent& e1); - testing::AssertionResult CheckEvents(const TrackingEvent& e1, - const TrackingEvent& e2); - testing::AssertionResult CheckEvents(const TrackingEvent& e1, - const TrackingEvent& e2, - const TrackingEvent& e3); - - private: - // AccountTrackerService::Observer implementation - virtual void OnAccountUpdated( - const AccountTrackerService::AccountInfo& ids) OVERRIDE; - virtual void OnAccountRemoved( - const AccountTrackerService::AccountInfo& ids) OVERRIDE; - - testing::AssertionResult CheckEvents( - const std::vector& events); - - std::vector events_; -}; - -void AccountTrackerObserver::OnAccountUpdated( - const AccountTrackerService::AccountInfo& ids) { - events_.push_back(TrackingEvent(UPDATED, ids.account_id, ids.gaia)); -} - -void AccountTrackerObserver::OnAccountRemoved( - const AccountTrackerService::AccountInfo& ids) { - events_.push_back(TrackingEvent(REMOVED, ids.account_id, ids.gaia)); -} - -void AccountTrackerObserver::Clear() { - events_.clear(); -} - -void AccountTrackerObserver::SortEventsByUser() { - std::stable_sort(events_.begin(), events_.end(), CompareByUser); -} - -testing::AssertionResult AccountTrackerObserver::CheckEvents() { - std::vector events; - return CheckEvents(events); -} - -testing::AssertionResult AccountTrackerObserver::CheckEvents( - const TrackingEvent& e1) { - std::vector events; - events.push_back(e1); - return CheckEvents(events); -} - -testing::AssertionResult AccountTrackerObserver::CheckEvents( - const TrackingEvent& e1, - const TrackingEvent& e2) { - std::vector events; - events.push_back(e1); - events.push_back(e2); - return CheckEvents(events); -} - -testing::AssertionResult AccountTrackerObserver::CheckEvents( - const TrackingEvent& e1, - const TrackingEvent& e2, - const TrackingEvent& e3) { - std::vector events; - events.push_back(e1); - events.push_back(e2); - events.push_back(e3); - return CheckEvents(events); -} - -testing::AssertionResult AccountTrackerObserver::CheckEvents( - const std::vector& events) { - std::string maybe_newline = (events.size() + events_.size()) > 2 ? "\n" : ""; - testing::AssertionResult result( - (events_ == events) - ? testing::AssertionSuccess() - : (testing::AssertionFailure() - << "Expected " << maybe_newline << Str(events) << ", " - << maybe_newline << "Got " << maybe_newline << Str(events_))); - events_.clear(); - return result; -} - -} // namespace - -class AccountTrackerServiceTest : public testing::Test { - public: - AccountTrackerServiceTest() {} - - virtual ~AccountTrackerServiceTest() {} - - virtual void SetUp() OVERRIDE { - fake_oauth2_token_service_.reset(new FakeOAuth2TokenService()); - - pref_service_.registry()->RegisterListPref( - AccountTrackerService::kAccountInfoPref); - - account_tracker_.reset(new AccountTrackerService()); - account_tracker_->Initialize(fake_oauth2_token_service_.get(), - &pref_service_, - new net::TestURLRequestContextGetter( - message_loop_.message_loop_proxy())); - account_tracker_->AddObserver(&observer_); - } - - virtual void TearDown() OVERRIDE { - account_tracker_->RemoveObserver(&observer_); - account_tracker_->Shutdown(); - } - - void SimulateTokenAvailable(const std::string& account_id) { - fake_oauth2_token_service_->AddAccount(account_id); - } - - void SimulateTokenRevoked(const std::string& account_id) { - fake_oauth2_token_service_->RemoveAccount(account_id); - } - - // Helpers to fake access token and user info fetching - void IssueAccessToken(const std::string& account_id) { - fake_oauth2_token_service_->IssueAllTokensForAccount( - account_id, "access_token-" + account_id, base::Time::Max()); - } - - std::string GenerateValidTokenInfoResponse(const std::string& account_id) { - return base::StringPrintf("{\"id\": \"%s\", \"email\": \"%s\"}", - AccountIdToGaiaId(account_id).c_str(), - AccountIdToEmail(account_id).c_str()); - } - - void ReturnOAuthUrlFetchSuccess(const std::string& account_id); - void ReturnOAuthUrlFetchFailure(const std::string& account_id); - - base::MessageLoopForIO* message_loop() { return &message_loop_; } - AccountTrackerService* account_tracker() { return account_tracker_.get(); } - AccountTrackerObserver* observer() { return &observer_; } - OAuth2TokenService* token_service() { - return fake_oauth2_token_service_.get(); - } - TestingPrefServiceSimple* pref_service() { return &pref_service_; } - - private: - void ReturnOAuthUrlFetchResults(int fetcher_id, - net::HttpStatusCode response_code, - const std::string& response_string); - - base::MessageLoopForIO message_loop_; - net::TestURLFetcherFactory test_fetcher_factory_; - scoped_ptr fake_oauth2_token_service_; - TestingPrefServiceSimple pref_service_; - scoped_ptr account_tracker_; - AccountTrackerObserver observer_; -}; - -void AccountTrackerServiceTest::ReturnOAuthUrlFetchResults( - int fetcher_id, - net::HttpStatusCode response_code, - const std::string& response_string) { - net::TestURLFetcher* fetcher = - test_fetcher_factory_.GetFetcherByID(fetcher_id); - ASSERT_TRUE(fetcher); - fetcher->set_response_code(response_code); - fetcher->SetResponseString(response_string); - fetcher->delegate()->OnURLFetchComplete(fetcher); -} - -void AccountTrackerServiceTest::ReturnOAuthUrlFetchSuccess( - const std::string& account_id) { - IssueAccessToken(account_id); - ReturnOAuthUrlFetchResults(gaia::GaiaOAuthClient::kUrlFetcherId, - net::HTTP_OK, - GenerateValidTokenInfoResponse(account_id)); -} - -void AccountTrackerServiceTest::ReturnOAuthUrlFetchFailure( - const std::string& account_id) { - IssueAccessToken(account_id); - ReturnOAuthUrlFetchResults( - gaia::GaiaOAuthClient::kUrlFetcherId, net::HTTP_BAD_REQUEST, ""); -} - -TEST_F(AccountTrackerServiceTest, Basic) { -} - -TEST_F(AccountTrackerServiceTest, TokenAvailable) { - SimulateTokenAvailable("alpha"); - ASSERT_FALSE(account_tracker()->IsAllUserInfoFetched()); - ASSERT_TRUE(observer()->CheckEvents()); -} - -TEST_F(AccountTrackerServiceTest, TokenAvailable_Revoked) { - SimulateTokenAvailable("alpha"); - SimulateTokenRevoked("alpha"); - ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched()); - ASSERT_TRUE(observer()->CheckEvents()); -} - -TEST_F(AccountTrackerServiceTest, TokenAvailable_UserInfo) { - SimulateTokenAvailable("alpha"); - ReturnOAuthUrlFetchSuccess("alpha"); - ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched()); - ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED, "alpha"))); -} - -TEST_F(AccountTrackerServiceTest, TokenAvailable_UserInfo_Revoked) { - SimulateTokenAvailable("alpha"); - ReturnOAuthUrlFetchSuccess("alpha"); - ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched()); - ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED, "alpha"))); - SimulateTokenRevoked("alpha"); - ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(REMOVED, "alpha"))); -} - -TEST_F(AccountTrackerServiceTest, TokenAvailable_UserInfoFailed) { - SimulateTokenAvailable("alpha"); - ReturnOAuthUrlFetchFailure("alpha"); - ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched()); - ASSERT_TRUE(observer()->CheckEvents()); -} - -TEST_F(AccountTrackerServiceTest, TokenAlreadyExists) { - SimulateTokenAvailable("alpha"); - AccountTrackerService tracker; - AccountTrackerObserver observer; - tracker.AddObserver(&observer); - tracker.Initialize(token_service(), - pref_service(), - new net::TestURLRequestContextGetter( - message_loop()->message_loop_proxy())); - ASSERT_FALSE(tracker.IsAllUserInfoFetched()); - ASSERT_TRUE(observer.CheckEvents()); - tracker.RemoveObserver(&observer); - tracker.Shutdown(); -} - -TEST_F(AccountTrackerServiceTest, TwoTokenAvailable_TwoUserInfo) { - SimulateTokenAvailable("alpha"); - SimulateTokenAvailable("beta"); - ReturnOAuthUrlFetchSuccess("alpha"); - ReturnOAuthUrlFetchSuccess("beta"); - ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched()); - ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED, "alpha"), - TrackingEvent(UPDATED, "beta"))); -} - -TEST_F(AccountTrackerServiceTest, TwoTokenAvailable_OneUserInfo) { - SimulateTokenAvailable("alpha"); - SimulateTokenAvailable("beta"); - ReturnOAuthUrlFetchSuccess("beta"); - ASSERT_FALSE(account_tracker()->IsAllUserInfoFetched()); - ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED, "beta"))); - ReturnOAuthUrlFetchSuccess("alpha"); - ASSERT_TRUE(account_tracker()->IsAllUserInfoFetched()); - ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED, "alpha"))); -} - -TEST_F(AccountTrackerServiceTest, GetAccounts) { - SimulateTokenAvailable("alpha"); - SimulateTokenAvailable("beta"); - SimulateTokenAvailable("gamma"); - ReturnOAuthUrlFetchSuccess("alpha"); - ReturnOAuthUrlFetchSuccess("beta"); - ReturnOAuthUrlFetchSuccess("gamma"); - - std::vector infos = - account_tracker()->GetAccounts(); - - EXPECT_EQ(3u, infos.size()); - EXPECT_EQ("alpha", infos[0].account_id); - EXPECT_EQ(AccountIdToGaiaId("alpha"), infos[0].gaia); - EXPECT_EQ(AccountIdToEmail("alpha"), infos[0].email); - EXPECT_EQ("beta", infos[1].account_id); - EXPECT_EQ(AccountIdToGaiaId("beta"), infos[1].gaia); - EXPECT_EQ(AccountIdToEmail("beta"), infos[1].email); - EXPECT_EQ("gamma", infos[2].account_id); - EXPECT_EQ(AccountIdToGaiaId("gamma"), infos[2].gaia); - EXPECT_EQ(AccountIdToEmail("gamma"), infos[2].email); -} - -TEST_F(AccountTrackerServiceTest, GetAccountInfo_Empty) { - AccountTrackerService::AccountInfo info = - account_tracker()->GetAccountInfo("alpha"); - ASSERT_EQ("", info.account_id); -} - -TEST_F(AccountTrackerServiceTest, GetAccountInfo_TokenAvailable) { - SimulateTokenAvailable("alpha"); - AccountTrackerService::AccountInfo info = - account_tracker()->GetAccountInfo("alpha"); - ASSERT_EQ("alpha", info.account_id); - ASSERT_EQ("", info.gaia); - ASSERT_EQ("", info.email); -} - -TEST_F(AccountTrackerServiceTest, GetAccountInfo_TokenAvailable_UserInfo) { - SimulateTokenAvailable("alpha"); - ReturnOAuthUrlFetchSuccess("alpha"); - AccountTrackerService::AccountInfo info = - account_tracker()->GetAccountInfo("alpha"); - ASSERT_EQ("alpha", info.account_id); - ASSERT_EQ(AccountIdToGaiaId("alpha"), info.gaia); - ASSERT_EQ(AccountIdToEmail("alpha"), info.email); -} - -TEST_F(AccountTrackerServiceTest, FindAccountInfoByGaiaId) { - SimulateTokenAvailable("alpha"); - ReturnOAuthUrlFetchSuccess("alpha"); - - std::string gaia_id = AccountIdToGaiaId("alpha"); - AccountTrackerService::AccountInfo info = - account_tracker()->FindAccountInfoByGaiaId(gaia_id); - ASSERT_EQ("alpha", info.account_id); - ASSERT_EQ(gaia_id, info.gaia); - - gaia_id = AccountIdToGaiaId("beta"); - info = account_tracker()->FindAccountInfoByGaiaId(gaia_id); - ASSERT_EQ("", info.account_id); -} - -TEST_F(AccountTrackerServiceTest, FindAccountInfoByEmail) { - SimulateTokenAvailable("alpha"); - ReturnOAuthUrlFetchSuccess("alpha"); - - std::string email = AccountIdToEmail("alpha"); - AccountTrackerService::AccountInfo info = - account_tracker()->FindAccountInfoByEmail(email); - ASSERT_EQ("alpha", info.account_id); - ASSERT_EQ(email, info.email); - - // Should also work with "canonically-equal" email addresses. - info = account_tracker()->FindAccountInfoByEmail("Alpha@Gmail.COM"); - ASSERT_EQ("alpha", info.account_id); - ASSERT_EQ(email, info.email); - info = account_tracker()->FindAccountInfoByEmail("al.pha@gmail.com"); - ASSERT_EQ("alpha", info.account_id); - ASSERT_EQ(email, info.email); - - email = AccountIdToEmail("beta"); - info = account_tracker()->FindAccountInfoByEmail(email); - ASSERT_EQ("", info.account_id); -} - -TEST_F(AccountTrackerServiceTest, Persistence) { - // Create a tracker and add two accounts. This should cause the accounts - // to be saved to persistence. - { - AccountTrackerService tracker; - tracker.Initialize(token_service(), - pref_service(), - new net::TestURLRequestContextGetter( - message_loop()->message_loop_proxy())); - SimulateTokenAvailable("alpha"); - ReturnOAuthUrlFetchSuccess("alpha"); - SimulateTokenAvailable("beta"); - ReturnOAuthUrlFetchSuccess("beta"); - tracker.Shutdown(); - } - - // Create a new tracker and make sure it loads the accounts corectly from - // persistence. - { - AccountTrackerService tracker; - tracker.AddObserver(observer()); - tracker.Initialize(token_service(), - pref_service(), - new net::TestURLRequestContextGetter( - message_loop()->message_loop_proxy())); - ASSERT_TRUE(observer()->CheckEvents(TrackingEvent(UPDATED, "alpha"), - TrackingEvent(UPDATED, "beta"))); - - std::vector infos = - tracker.GetAccounts(); - ASSERT_EQ(2u, infos.size()); - EXPECT_EQ(AccountIdToGaiaId("alpha"), infos[0].gaia); - EXPECT_EQ(AccountIdToEmail("alpha"), infos[0].email); - EXPECT_EQ("beta", infos[1].account_id); - EXPECT_EQ(AccountIdToGaiaId("beta"), infos[1].gaia); - EXPECT_EQ(AccountIdToEmail("beta"), infos[1].email); - - // Remove account. - SimulateTokenRevoked("alpha"); - tracker.RemoveObserver(observer()); - tracker.Shutdown(); - } - - // Create a new tracker and make sure it loads the single account from - // persistence. - { - AccountTrackerService tracker; - tracker.Initialize(token_service(), - pref_service(), - new net::TestURLRequestContextGetter( - message_loop()->message_loop_proxy())); - - std::vector infos = - tracker.GetAccounts(); - ASSERT_EQ(1u, infos.size()); - EXPECT_EQ("beta", infos[0].account_id); - EXPECT_EQ(AccountIdToGaiaId("beta"), infos[0].gaia); - EXPECT_EQ(AccountIdToEmail("beta"), infos[0].email); - tracker.Shutdown(); - } -} -- 2.11.4.GIT