1// Copyright 2014 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 "components/signin/core/browser/signin_tracker.h"
6
7#include "components/signin/core/browser/account_reconcilor.h"
8#include "components/signin/core/browser/profile_oauth2_token_service.h"
9#include "components/signin/core/browser/signin_client.h"
10#include "google_apis/gaia/gaia_constants.h"
11
12SigninTracker::SigninTracker(ProfileOAuth2TokenService* token_service,
13                             SigninManagerBase* signin_manager,
14                             AccountReconcilor* account_reconcilor,
15                             SigninClient* client,
16                             Observer* observer)
17    : token_service_(token_service),
18      signin_manager_(signin_manager),
19      account_reconcilor_(account_reconcilor),
20      client_(client),
21      observer_(observer) {
22  Initialize();
23}
24
25SigninTracker::~SigninTracker() {
26  signin_manager_->RemoveObserver(this);
27  token_service_->RemoveObserver(this);
28
29  if (account_reconcilor_) {
30    account_reconcilor_->RemoveMergeSessionObserver(this);
31#if !defined(OS_CHROMEOS)
32  } else if (client_->ShouldMergeSigninCredentialsIntoCookieJar()) {
33    SigninManager* manager = static_cast<SigninManager*>(signin_manager_);
34    manager->RemoveMergeSessionObserver(this);
35#endif
36  }
37}
38
39void SigninTracker::Initialize() {
40  DCHECK(observer_);
41  signin_manager_->AddObserver(this);
42  token_service_->AddObserver(this);
43}
44
45void SigninTracker::GoogleSigninFailed(const GoogleServiceAuthError& error) {
46  observer_->SigninFailed(error);
47}
48
49void SigninTracker::OnRefreshTokenAvailable(const std::string& account_id) {
50  if (account_id != signin_manager_->GetAuthenticatedAccountId())
51    return;
52
53  if (account_reconcilor_) {
54    account_reconcilor_->AddMergeSessionObserver(this);
55#if !defined(OS_CHROMEOS)
56  } else if (client_->ShouldMergeSigninCredentialsIntoCookieJar()) {
57    SigninManager* manager = static_cast<SigninManager*>(signin_manager_);
58    manager->AddMergeSessionObserver(this);
59#endif
60  }
61
62  observer_->SigninSuccess();
63}
64
65void SigninTracker::OnRefreshTokenRevoked(const std::string& account_id) {
66  NOTREACHED();
67}
68
69void SigninTracker::MergeSessionCompleted(
70    const std::string& account_id,
71    const GoogleServiceAuthError& error) {
72  observer_->MergeSessionComplete(error);
73}
74