diff options
author | rlp@chromium.org <rlp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-08 05:22:22 +0000 |
---|---|---|
committer | rlp@chromium.org <rlp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-08 05:22:22 +0000 |
commit | 859f6b2052bf8609ca7322dc64c089dbfdc5ff19 (patch) | |
tree | 8f73ab08c636ed75a63c7c044d07b29cacbe3779 /chrome/browser/signin | |
parent | 12fdc50d289ef601f000832381f45a62e56b0db6 (diff) | |
download | chromium_src-859f6b2052bf8609ca7322dc64c089dbfdc5ff19.zip chromium_src-859f6b2052bf8609ca7322dc64c089dbfdc5ff19.tar.gz chromium_src-859f6b2052bf8609ca7322dc64c089dbfdc5ff19.tar.bz2 |
Converting TokenService to a ProfileKeyedService.
BUG=112531
TEST=no new ones
Review URL: http://codereview.chromium.org/9387017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@125565 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/signin')
-rw-r--r-- | chrome/browser/signin/signin_manager.cc | 21 | ||||
-rw-r--r-- | chrome/browser/signin/signin_manager_factory.cc | 5 | ||||
-rw-r--r-- | chrome/browser/signin/signin_tracker.cc | 8 | ||||
-rw-r--r-- | chrome/browser/signin/signin_tracker_unittest.cc | 10 | ||||
-rw-r--r-- | chrome/browser/signin/token_service.h | 4 | ||||
-rw-r--r-- | chrome/browser/signin/token_service_factory.cc | 34 | ||||
-rw-r--r-- | chrome/browser/signin/token_service_factory.h | 41 | ||||
-rw-r--r-- | chrome/browser/signin/token_service_unittest.cc | 3 | ||||
-rw-r--r-- | chrome/browser/signin/ubertoken_fetcher.cc | 5 |
9 files changed, 111 insertions, 20 deletions
diff --git a/chrome/browser/signin/signin_manager.cc b/chrome/browser/signin/signin_manager.cc index 37958c9..dfb9c59 100644 --- a/chrome/browser/signin/signin_manager.cc +++ b/chrome/browser/signin/signin_manager.cc @@ -14,6 +14,7 @@ #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/signin/token_service.h" +#include "chrome/browser/signin/token_service_factory.h" #include "chrome/browser/sync/profile_sync_service.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/chrome_switches.h" @@ -46,7 +47,7 @@ void SigninManager::Initialize(Profile* profile) { if (!user.empty()) SetAuthenticatedUsername(user); // TokenService can be null for unit tests. - TokenService* token_service = profile_->GetTokenService(); + TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); if (token_service) { token_service->Initialize(GaiaConstants::kChromeSource, profile_); if (!authenticated_username_.empty()) { @@ -61,7 +62,8 @@ bool SigninManager::IsInitialized() const { void SigninManager::CleanupNotificationRegistration() { #if !defined(OS_CHROMEOS) - content::Source<TokenService> token_service(profile_->GetTokenService()); + content::Source<TokenService> token_service( + TokenServiceFactory::GetForProfile(profile_)); if (registrar_.IsRegistered(this, chrome::NOTIFICATION_TOKEN_AVAILABLE, token_service)) { @@ -130,9 +132,10 @@ void SigninManager::StartSignIn(const std::string& username, if (cookie_settings && cookie_settings->IsSettingCookieAllowed(GURL(kGoogleAccountsUrl), GURL(kGoogleAccountsUrl))) { + TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); registrar_.Add(this, chrome::NOTIFICATION_TOKEN_AVAILABLE, - content::Source<TokenService>(profile_->GetTokenService())); + content::Source<TokenService>(token_service)); } #endif } @@ -214,8 +217,9 @@ void SigninManager::SignOut() { authenticated_username_.clear(); profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); profile_->GetPrefs()->ClearPref(prefs::kIsGooglePlusUser); - profile_->GetTokenService()->ResetCredentialsInMemory(); - profile_->GetTokenService()->EraseTokensFromDB(); + TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); + token_service->ResetCredentialsInMemory(); + token_service->EraseTokensFromDB(); } bool SigninManager::AuthInProgress() const { @@ -312,9 +316,10 @@ void SigninManager::OnGetUserInfoSuccess(const UserInfoMap& data) { password_.clear(); // Don't need it anymore. - profile_->GetTokenService()->UpdateCredentials(last_result_); - DCHECK(profile_->GetTokenService()->AreCredentialsValid()); - profile_->GetTokenService()->StartFetchingTokens(); + TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); + token_service->UpdateCredentials(last_result_); + DCHECK(token_service->AreCredentialsValid()); + token_service->StartFetchingTokens(); } void SigninManager::OnGetUserInfoFailure(const GoogleServiceAuthError& error) { diff --git a/chrome/browser/signin/signin_manager_factory.cc b/chrome/browser/signin/signin_manager_factory.cc index 1406669..71c218c 100644 --- a/chrome/browser/signin/signin_manager_factory.cc +++ b/chrome/browser/signin/signin_manager_factory.cc @@ -7,14 +7,13 @@ #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile_dependency_manager.h" #include "chrome/browser/signin/signin_manager.h" +#include "chrome/browser/signin/token_service_factory.h" #include "chrome/common/pref_names.h" SigninManagerFactory::SigninManagerFactory() : ProfileKeyedServiceFactory("SigninManager", ProfileDependencyManager::GetInstance()) { - // TODO(atwilson): SigninManager depends on TokenService - when this is - // converted to the ProfileKeyedService framework, uncomment this dependency. - // DependsOn(TokenServiceFactory::GetInstance()); + DependsOn(TokenServiceFactory::GetInstance()); } SigninManagerFactory::~SigninManagerFactory() {} diff --git a/chrome/browser/signin/signin_tracker.cc b/chrome/browser/signin/signin_tracker.cc index 1d77c37..ec2e023 100644 --- a/chrome/browser/signin/signin_tracker.cc +++ b/chrome/browser/signin/signin_tracker.cc @@ -6,6 +6,7 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/signin/token_service.h" +#include "chrome/browser/signin/token_service_factory.h" #include "chrome/browser/sync/profile_sync_service.h" #include "chrome/browser/sync/profile_sync_service_factory.h" #include "chrome/common/chrome_notification_types.h" @@ -34,12 +35,13 @@ SigninTracker::SigninTracker(Profile* profile, Observer* observer) registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, content::Source<Profile>(profile_)); + TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); registrar_.Add(this, chrome::NOTIFICATION_TOKEN_AVAILABLE, - content::Source<TokenService>(profile_->GetTokenService())); + content::Source<TokenService>(token_service)); registrar_.Add(this, chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, - content::Source<TokenService>(profile_->GetTokenService())); + content::Source<TokenService>(token_service)); // Also listen for notifications from the various signed in services (only // sync for now). @@ -141,7 +143,7 @@ void SigninTracker::HandleServiceStateChange() { // static bool SigninTracker::AreServiceTokensLoaded(Profile* profile) { // See if we have all of the tokens required. - TokenService* token_service = profile->GetTokenService(); + TokenService* token_service = TokenServiceFactory::GetForProfile(profile); for (int i = 0; i < kNumSignedInServices; ++i) { if (!token_service->HasTokenForService(kSignedInServices[i])) { // Don't have a token for one of our signed-in services. diff --git a/chrome/browser/signin/signin_tracker_unittest.cc b/chrome/browser/signin/signin_tracker_unittest.cc index e279b91..9d70257 100644 --- a/chrome/browser/signin/signin_tracker_unittest.cc +++ b/chrome/browser/signin/signin_tracker_unittest.cc @@ -10,6 +10,7 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/signin/signin_manager.h" #include "chrome/browser/signin/token_service.h" +#include "chrome/browser/signin/token_service_factory.h" #include "chrome/browser/sync/profile_sync_service_factory.h" #include "chrome/browser/sync/profile_sync_service_mock.h" #include "chrome/common/chrome_notification_types.h" @@ -34,6 +35,10 @@ class MockTokenService : public TokenService { MOCK_CONST_METHOD1(HasTokenForService, bool(const char*)); }; +ProfileKeyedBase* BuildMockTokenService(Profile* profile) { + return new MockTokenService; +} + class MockObserver : public SigninTracker::Observer { public: MockObserver() {} @@ -49,8 +54,9 @@ class SigninTrackerTest : public testing::Test { SigninTrackerTest() {} virtual void SetUp() OVERRIDE { profile_.reset(ProfileSyncServiceMock::MakeSignedInTestingProfile()); - mock_token_service_ = new MockTokenService(); - profile_->SetTokenService(mock_token_service_); + mock_token_service_ = static_cast<MockTokenService*>( + TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( + profile_.get(), BuildMockTokenService)); mock_pss_ = static_cast<ProfileSyncServiceMock*>( ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( profile_.get(), diff --git a/chrome/browser/signin/token_service.h b/chrome/browser/signin/token_service.h index fc7a08b..af44d763 100644 --- a/chrome/browser/signin/token_service.h +++ b/chrome/browser/signin/token_service.h @@ -41,6 +41,7 @@ #include "base/gtest_prod_util.h" #include "base/memory/scoped_ptr.h" +#include "chrome/browser/profiles/profile_keyed_service.h" #include "chrome/browser/webdata/web_data_service.h" #include "chrome/common/net/gaia/gaia_auth_consumer.h" #include "chrome/common/net/gaia/gaia_auth_fetcher.h" @@ -58,6 +59,7 @@ class URLRequestContextGetter; // The TokenService is a Profile member, so all calls are expected // from the UI thread. class TokenService : public GaiaAuthConsumer, + public ProfileKeyedService, public WebDataServiceConsumer, public content::NotificationObserver { public: @@ -145,7 +147,7 @@ class TokenService : public GaiaAuthConsumer, // Typical use is to create an OAuth2 token for appropriate scope and then // use that token to call a Google API. virtual bool HasOAuthLoginToken() const; - const std::string& GetOAuth2LoginRefreshToken() const; + virtual const std::string& GetOAuth2LoginRefreshToken() const; const std::string& GetOAuth2LoginAccessToken() const; // For tests only. Doesn't save to the WebDB. diff --git a/chrome/browser/signin/token_service_factory.cc b/chrome/browser/signin/token_service_factory.cc new file mode 100644 index 0000000..54d6581 --- /dev/null +++ b/chrome/browser/signin/token_service_factory.cc @@ -0,0 +1,34 @@ +// Copyright (c) 2012 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/token_service_factory.h" + +#include "chrome/browser/profiles/profile_dependency_manager.h" +#include "chrome/browser/signin/token_service.h" + +TokenServiceFactory::TokenServiceFactory() + : ProfileKeyedServiceFactory("TokenService", + ProfileDependencyManager::GetInstance()) { + // TODO(rlp): TokenService depends on WebDataService - when this is + // converted to the ProfileKeyedService framework, uncomment this dependency. + // DependsOn(WebDataServiceFactory::GetInstance()); +} + +TokenServiceFactory::~TokenServiceFactory() {} + +// static +TokenService* TokenServiceFactory::GetForProfile(Profile* profile) { + return static_cast<TokenService*>( + GetInstance()->GetServiceForProfile(profile, true)); +} + +// static +TokenServiceFactory* TokenServiceFactory::GetInstance() { + return Singleton<TokenServiceFactory>::get(); +} + +ProfileKeyedService* TokenServiceFactory::BuildServiceInstanceFor( + Profile* profile) const { + return new TokenService(); +} diff --git a/chrome/browser/signin/token_service_factory.h b/chrome/browser/signin/token_service_factory.h new file mode 100644 index 0000000..5f98b0d --- /dev/null +++ b/chrome/browser/signin/token_service_factory.h @@ -0,0 +1,41 @@ +// Copyright (c) 2012 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_TOKEN_SERVICE_FACTORY_H_ +#define CHROME_BROWSER_SIGNIN_TOKEN_SERVICE_FACTORY_H_ +#pragma once + +#include "base/memory/singleton.h" +#include "chrome/browser/profiles/profile_keyed_service_factory.h" + +class TokenService; +class Profile; + +// Singleton that owns all TokenServices and associates them with Profiles. +// Listens for the Profile's destruction notification and cleans up the +// associated TokenService. +class TokenServiceFactory : public ProfileKeyedServiceFactory { + public: + // Returns the instance of TokenService associated with this profile + // (creating one if none exists). Returns NULL if this profile cannot have a + // TokenService (for example, if |profile| is incognito). + static TokenService* GetForProfile(Profile* profile); + + // Returns an instance of the TokenServiceFactory singleton. + static TokenServiceFactory* GetInstance(); + + private: + friend struct DefaultSingletonTraits<TokenServiceFactory>; + + TokenServiceFactory(); + virtual ~TokenServiceFactory(); + + // ProfileKeyedServiceFactory: + virtual ProfileKeyedService* BuildServiceInstanceFor( + Profile* profile) const OVERRIDE; + + DISALLOW_COPY_AND_ASSIGN(TokenServiceFactory); +}; + +#endif // CHROME_BROWSER_SIGNIN_TOKEN_SERVICE_FACTORY_H_ diff --git a/chrome/browser/signin/token_service_unittest.cc b/chrome/browser/signin/token_service_unittest.cc index e2f15dd..246895a 100644 --- a/chrome/browser/signin/token_service_unittest.cc +++ b/chrome/browser/signin/token_service_unittest.cc @@ -12,6 +12,7 @@ #include "base/message_loop.h" #include "base/synchronization/waitable_event.h" #include "chrome/browser/password_manager/encryptor.h" +#include "chrome/browser/signin/token_service_factory.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/net/gaia/mock_url_fetcher_factory.h" @@ -73,7 +74,7 @@ void TokenServiceTestHarness::SetUp() { profile_.reset(new TestingProfile()); profile_->CreateWebDataService(false); WaitForDBLoadCompletion(); - service_ = profile_->GetTokenService(); + service_ = TokenServiceFactory::GetForProfile(profile_.get()); success_tracker_.ListenFor(chrome::NOTIFICATION_TOKEN_AVAILABLE, content::Source<TokenService>(service_)); diff --git a/chrome/browser/signin/ubertoken_fetcher.cc b/chrome/browser/signin/ubertoken_fetcher.cc index 3e6be55..6f685df 100644 --- a/chrome/browser/signin/ubertoken_fetcher.cc +++ b/chrome/browser/signin/ubertoken_fetcher.cc @@ -6,6 +6,7 @@ #include "base/stringprintf.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/signin/token_service.h" +#include "chrome/browser/signin/token_service_factory.h" #include "chrome/browser/signin/ubertoken_fetcher.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/net/gaia/gaia_constants.h" @@ -24,7 +25,7 @@ UbertokenFetcher::~UbertokenFetcher() { } void UbertokenFetcher::StartFetchingToken() { - TokenService* token_service = profile_->GetTokenService(); + TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); if (token_service->HasOAuthLoginToken()) { StartFetchingUbertoken(); } else { @@ -39,7 +40,7 @@ void UbertokenFetcher::StartFetchingToken() { } void UbertokenFetcher::StartFetchingUbertoken() { - TokenService* token_service = profile_->GetTokenService(); + TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); DCHECK(token_service->HasOAuthLoginToken()); gaia::OAuthClientInfo client_info; GaiaUrls* urls = GaiaUrls::GetInstance(); |