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.
4
5 #include "chrome/browser/signin/signin_tracker.h"
6
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/signin/profile_oauth2_token_service.h"
10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
11 #include "chrome/browser/signin/signin_manager.h"
12 #include "chrome/browser/signin/signin_manager_factory.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"
16
SigninTracker(Profile * profile,Observer * observer)17 SigninTracker::SigninTracker(Profile* profile, Observer* observer)
18 : profile_(profile), observer_(observer) {
19 DCHECK(profile_);
20 Initialize();
21 }
22
~SigninTracker()23 SigninTracker::~SigninTracker() {
24 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->
25 RemoveObserver(this);
26 }
27
Initialize()28 void SigninTracker::Initialize() {
29 DCHECK(observer_);
30
31 // Register for notifications from the SigninManager.
32 registrar_.Add(this,
33 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED,
34 content::Source<Profile>(profile_));
35
36 OAuth2TokenService* token_service =
37 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
38 token_service->AddObserver(this);
39 }
40
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)41 void SigninTracker::Observe(int type,
42 const content::NotificationSource& source,
43 const content::NotificationDetails& details) {
44 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, type);
45
46 // We should not get more than one of these notifications.
47 const GoogleServiceAuthError& error =
48 *(content::Details<const GoogleServiceAuthError>(details).ptr());
49 observer_->SigninFailed(error);
50 }
51
OnRefreshTokenAvailable(const std::string & account_id)52 void SigninTracker::OnRefreshTokenAvailable(const std::string& account_id) {
53 // TODO: when OAuth2TokenService handles multi-login, this should check
54 // that |account_id| is the primary account before signalling success.
55 observer_->SigninSuccess();
56 }
57
OnRefreshTokenRevoked(const std::string & account_id)58 void SigninTracker::OnRefreshTokenRevoked(const std::string& account_id) {
59 NOTREACHED();
60 }
61