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/signin_tracker.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/signin/account_reconcilor_factory.h"
10 #include "chrome/browser/signin/profile_oauth2_token_service.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
12 #include "chrome/common/profile_management_switches.h"
13 #include "content/public/browser/notification_details.h"
14 #include "content/public/browser/notification_source.h"
15 #include "google_apis/gaia/gaia_constants.h"
17 #if !defined(OS_CHROMEOS)
18 #include "chrome/browser/signin/signin_manager.h"
19 #include "chrome/browser/signin/signin_manager_factory.h"
22 SigninTracker::SigninTracker(Profile
* profile
, Observer
* observer
)
23 : profile_(profile
), observer_(observer
) {
28 SigninTracker::~SigninTracker() {
29 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
)->
32 if (!switches::IsEnableWebBasedSignin()) {
33 if (switches::IsNewProfileManagement()) {
34 AccountReconcilorFactory::GetForProfile(profile_
)->
35 RemoveMergeSessionObserver(this);
36 #if !defined(OS_CHROMEOS)
38 SigninManagerFactory::GetForProfile(profile_
)->
39 RemoveMergeSessionObserver(this);
45 void SigninTracker::Initialize() {
48 // Register for notifications from the SigninManager.
50 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED
,
51 content::Source
<Profile
>(profile_
));
53 OAuth2TokenService
* token_service
=
54 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
);
55 token_service
->AddObserver(this);
58 void SigninTracker::Observe(int type
,
59 const content::NotificationSource
& source
,
60 const content::NotificationDetails
& details
) {
61 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED
, type
);
63 // We should not get more than one of these notifications.
64 const GoogleServiceAuthError
& error
=
65 *(content::Details
<const GoogleServiceAuthError
>(details
).ptr());
66 observer_
->SigninFailed(error
);
69 void SigninTracker::OnRefreshTokenAvailable(const std::string
& account_id
) {
70 // TODO: when OAuth2TokenService handles multi-login, this should check
71 // that |account_id| is the primary account before signalling success.
72 if (!switches::IsEnableWebBasedSignin()) {
73 if (switches::IsNewProfileManagement()) {
74 AccountReconcilorFactory::GetForProfile(profile_
)->
75 AddMergeSessionObserver(this);
76 #if !defined(OS_CHROMEOS)
78 SigninManagerFactory::GetForProfile(profile_
)->
79 AddMergeSessionObserver(this);
84 observer_
->SigninSuccess();
87 void SigninTracker::OnRefreshTokenRevoked(const std::string
& account_id
) {
91 void SigninTracker::MergeSessionCompleted(
92 const std::string
& account_id
,
93 const GoogleServiceAuthError
& error
) {
94 observer_
->MergeSessionComplete(error
);