| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/signin/fake_signin_manager.h" | 5 #include "chrome/browser/signin/fake_signin_manager_builder.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | |
| 8 #include "base/prefs/pref_service.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/signin/account_tracker_service_factory.h" | 8 #include "chrome/browser/signin/account_tracker_service_factory.h" |
| 11 #include "chrome/browser/signin/chrome_signin_client_factory.h" | 9 #include "chrome/browser/signin/chrome_signin_client_factory.h" |
| 12 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h" | 10 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h" |
| 13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 14 #include "chrome/browser/signin/signin_manager_factory.h" | 12 #include "chrome/browser/signin/signin_manager_factory.h" |
| 15 #include "chrome/browser/ui/global_error/global_error_service.h" | |
| 16 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | |
| 17 #include "chrome/common/pref_names.h" | |
| 18 #include "components/signin/core/browser/account_tracker_service.h" | |
| 19 | 13 |
| 20 FakeSigninManagerBase::FakeSigninManagerBase(Profile* profile) | 14 scoped_ptr<KeyedService> BuildFakeSigninManagerBase( |
| 21 : SigninManagerBase( | |
| 22 ChromeSigninClientFactory::GetForProfile(profile), | |
| 23 AccountTrackerServiceFactory::GetForProfile(profile)) {} | |
| 24 | |
| 25 FakeSigninManagerBase::~FakeSigninManagerBase() { | |
| 26 } | |
| 27 | |
| 28 // static | |
| 29 scoped_ptr<KeyedService> FakeSigninManagerBase::Build( | |
| 30 content::BrowserContext* context) { | 15 content::BrowserContext* context) { |
| 31 scoped_ptr<SigninManagerBase> manager; | 16 scoped_ptr<SigninManagerBase> manager; |
| 32 Profile* profile = static_cast<Profile*>(context); | 17 Profile* profile = static_cast<Profile*>(context); |
| 33 #if defined(OS_CHROMEOS) | 18 manager.reset(new FakeSigninManagerForTesting(profile)); |
| 34 manager.reset(new FakeSigninManagerBase(profile)); | 19 manager->Initialize(nullptr); |
| 35 #else | |
| 36 manager.reset(new FakeSigninManager(profile)); | |
| 37 #endif | |
| 38 manager->Initialize(NULL); | |
| 39 SigninManagerFactory::GetInstance() | 20 SigninManagerFactory::GetInstance() |
| 40 ->NotifyObserversOfSigninManagerCreationForTesting(manager.get()); | 21 ->NotifyObserversOfSigninManagerCreationForTesting(manager.get()); |
| 41 return manager.Pass(); | 22 return manager.Pass(); |
| 42 } | 23 } |
| 43 | 24 |
| 44 #if !defined (OS_CHROMEOS) | 25 #if defined(OS_CHROMEOS) |
| 45 | 26 FakeSigninManagerForTesting::FakeSigninManagerForTesting(Profile* profile) |
| 46 FakeSigninManager::FakeSigninManager(Profile* profile) | 27 : FakeSigninManagerBase( |
| 47 : SigninManager( | 28 ChromeSigninClientFactory::GetForProfile(profile), |
| 29 AccountTrackerServiceFactory::GetForProfile(profile)) {} |
| 30 #else |
| 31 FakeSigninManagerForTesting::FakeSigninManagerForTesting(Profile* profile) |
| 32 : FakeSigninManager( |
| 48 ChromeSigninClientFactory::GetForProfile(profile), | 33 ChromeSigninClientFactory::GetForProfile(profile), |
| 49 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), | 34 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), |
| 50 AccountTrackerServiceFactory::GetForProfile(profile), | 35 AccountTrackerServiceFactory::GetForProfile(profile), |
| 51 GaiaCookieManagerServiceFactory::GetForProfile(profile)) {} | 36 GaiaCookieManagerServiceFactory::GetForProfile(profile)) {} |
| 52 | 37 #endif |
| 53 FakeSigninManager::~FakeSigninManager() { | |
| 54 } | |
| 55 | |
| 56 void FakeSigninManager::StartSignInWithRefreshToken( | |
| 57 const std::string& refresh_token, | |
| 58 const std::string& gaia_id, | |
| 59 const std::string& username, | |
| 60 const std::string& password, | |
| 61 const OAuthTokenFetchedCallback& oauth_fetched_callback) { | |
| 62 set_auth_in_progress( | |
| 63 account_tracker_service()->SeedAccountInfo(gaia_id, username)); | |
| 64 set_password(password); | |
| 65 username_ = username; | |
| 66 | |
| 67 if (!oauth_fetched_callback.is_null()) | |
| 68 oauth_fetched_callback.Run(refresh_token); | |
| 69 } | |
| 70 | |
| 71 | |
| 72 void FakeSigninManager::CompletePendingSignin() { | |
| 73 SetAuthenticatedAccountId(GetAccountIdForAuthInProgress()); | |
| 74 set_auth_in_progress(std::string()); | |
| 75 FOR_EACH_OBSERVER(SigninManagerBase::Observer, | |
| 76 observer_list_, | |
| 77 GoogleSigninSucceeded(authenticated_account_id_, | |
| 78 username_, | |
| 79 password_)); | |
| 80 } | |
| 81 | |
| 82 void FakeSigninManager::SignIn(const std::string& gaia_id, | |
| 83 const std::string& username, | |
| 84 const std::string& password) { | |
| 85 StartSignInWithRefreshToken( | |
| 86 std::string(), gaia_id, username, password, | |
| 87 OAuthTokenFetchedCallback()); | |
| 88 CompletePendingSignin(); | |
| 89 } | |
| 90 | |
| 91 void FakeSigninManager::FailSignin(const GoogleServiceAuthError& error) { | |
| 92 FOR_EACH_OBSERVER(SigninManagerBase::Observer, | |
| 93 observer_list_, | |
| 94 GoogleSigninFailed(error)); | |
| 95 } | |
| 96 | |
| 97 void FakeSigninManager::SignOut( | |
| 98 signin_metrics::ProfileSignout signout_source_metric) { | |
| 99 if (IsSignoutProhibited()) | |
| 100 return; | |
| 101 set_auth_in_progress(std::string()); | |
| 102 set_password(std::string()); | |
| 103 const std::string account_id = GetAuthenticatedAccountId(); | |
| 104 const std::string username = GetAuthenticatedUsername(); | |
| 105 authenticated_account_id_.clear(); | |
| 106 | |
| 107 FOR_EACH_OBSERVER(SigninManagerBase::Observer, observer_list_, | |
| 108 GoogleSignedOut(account_id, username)); | |
| 109 } | |
| 110 | |
| 111 #endif // !defined (OS_CHROMEOS) | |
| OLD | NEW |