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
3 // found in the LICENSE file.
5 #include "chrome/browser/signin/fake_signin_manager.h"
7 #include "base/callback_helpers.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/signin/chrome_signin_client_factory.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
12 #include "chrome/browser/signin/signin_manager_factory.h"
13 #include "chrome/browser/ui/global_error/global_error_service.h"
14 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
15 #include "chrome/common/pref_names.h"
17 FakeSigninManagerBase::FakeSigninManagerBase(Profile
* profile
)
19 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile
)) {}
21 FakeSigninManagerBase::~FakeSigninManagerBase() {
25 KeyedService
* FakeSigninManagerBase::Build(content::BrowserContext
* context
) {
26 SigninManagerBase
* manager
;
27 Profile
* profile
= static_cast<Profile
*>(context
);
28 #if defined(OS_CHROMEOS)
29 manager
= new FakeSigninManagerBase(profile
);
31 manager
= new FakeSigninManager(profile
);
33 manager
->Initialize(NULL
);
34 SigninManagerFactory::GetInstance()
35 ->NotifyObserversOfSigninManagerCreationForTesting(manager
);
39 #if !defined (OS_CHROMEOS)
41 FakeSigninManager::FakeSigninManager(Profile
* profile
)
43 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile
),
44 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
)) {}
46 FakeSigninManager::~FakeSigninManager() {
49 void FakeSigninManager::StartSignInWithRefreshToken(
50 const std::string
& refresh_token
,
51 const std::string
& username
,
52 const std::string
& password
,
53 const OAuthTokenFetchedCallback
& oauth_fetched_callback
) {
54 set_auth_in_progress(username
);
55 set_password(password
);
56 if (!oauth_fetched_callback
.is_null())
57 oauth_fetched_callback
.Run(refresh_token
);
61 void FakeSigninManager::CompletePendingSignin() {
62 SetAuthenticatedUsername(GetUsernameForAuthInProgress());
63 set_auth_in_progress(std::string());
64 FOR_EACH_OBSERVER(Observer
,
66 GoogleSigninSucceeded(authenticated_username_
, password_
));
69 void FakeSigninManager::AddMergeSessionObserver(
70 MergeSessionHelper::Observer
* observer
) {
71 SigninManager::AddMergeSessionObserver(observer
);
72 merge_session_observer_list_
.AddObserver(observer
);
75 void FakeSigninManager::RemoveMergeSessionObserver(
76 MergeSessionHelper::Observer
* observer
) {
77 SigninManager::RemoveMergeSessionObserver(observer
);
78 merge_session_observer_list_
.RemoveObserver(observer
);
81 void FakeSigninManager::NotifyMergeSessionObservers(
82 const GoogleServiceAuthError
& error
) {
83 FOR_EACH_OBSERVER(MergeSessionHelper::Observer
, merge_session_observer_list_
,
84 MergeSessionCompleted(GetAuthenticatedUsername(), error
));
87 void FakeSigninManager::SignIn(const std::string
& username
,
88 const std::string
& password
) {
89 StartSignInWithRefreshToken(
90 std::string(), username
, password
, OAuthTokenFetchedCallback());
91 CompletePendingSignin();
94 void FakeSigninManager::FailSignin(const GoogleServiceAuthError
& error
) {
95 FOR_EACH_OBSERVER(Observer
, observer_list_
, GoogleSigninFailed(error
));
98 void FakeSigninManager::SignOut(
99 signin_metrics::ProfileSignout signout_source_metric
) {
100 if (IsSignoutProhibited())
102 set_auth_in_progress(std::string());
103 set_password(std::string());
104 const std::string username
= authenticated_username_
;
105 authenticated_username_
.clear();
107 FOR_EACH_OBSERVER(SigninManagerBase::Observer
, observer_list_
,
108 GoogleSignedOut(username
));
111 #endif // !defined (OS_CHROMEOS)