[Cleanup] Used scoped pointers in KeyedServiceFactory's SetTestingFactory functions.
[chromium-blink-merge.git] / chrome / browser / signin / fake_signin_manager.cc
blobf81b4b48bf467e8cecb3493bab59776ccdd4685f
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/account_tracker_service_factory.h"
11 #include "chrome/browser/signin/chrome_signin_client_factory.h"
12 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14 #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"
20 FakeSigninManagerBase::FakeSigninManagerBase(Profile* profile)
21 : SigninManagerBase(
22 ChromeSigninClientFactory::GetForProfile(profile),
23 AccountTrackerServiceFactory::GetForProfile(profile)) {}
25 FakeSigninManagerBase::~FakeSigninManagerBase() {
28 // static
29 scoped_ptr<KeyedService> FakeSigninManagerBase::Build(
30 content::BrowserContext* context) {
31 scoped_ptr<SigninManagerBase> manager;
32 Profile* profile = static_cast<Profile*>(context);
33 #if defined(OS_CHROMEOS)
34 manager.reset(new FakeSigninManagerBase(profile));
35 #else
36 manager.reset(new FakeSigninManager(profile));
37 #endif
38 manager->Initialize(NULL);
39 SigninManagerFactory::GetInstance()
40 ->NotifyObserversOfSigninManagerCreationForTesting(manager.get());
41 return manager.Pass();
44 #if !defined (OS_CHROMEOS)
46 FakeSigninManager::FakeSigninManager(Profile* profile)
47 : SigninManager(
48 ChromeSigninClientFactory::GetForProfile(profile),
49 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
50 AccountTrackerServiceFactory::GetForProfile(profile),
51 GaiaCookieManagerServiceFactory::GetForProfile(profile)) {}
53 FakeSigninManager::~FakeSigninManager() {
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;
67 if (!oauth_fetched_callback.is_null())
68 oauth_fetched_callback.Run(refresh_token);
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_));
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();
91 void FakeSigninManager::FailSignin(const GoogleServiceAuthError& error) {
92 FOR_EACH_OBSERVER(SigninManagerBase::Observer,
93 observer_list_,
94 GoogleSigninFailed(error));
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();
107 FOR_EACH_OBSERVER(SigninManagerBase::Observer, observer_list_,
108 GoogleSignedOut(account_id, username));
111 #endif // !defined (OS_CHROMEOS)