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 "google_apis/gaia/identity_provider.h"
6
7IdentityProvider::Observer::~Observer() {}
8
9IdentityProvider::~IdentityProvider() {}
10
11void IdentityProvider::AddActiveAccountRefreshTokenObserver(
12    OAuth2TokenService::Observer* observer) {
13  OAuth2TokenService* token_service = GetTokenService();
14  if (!token_service || token_service_observers_.HasObserver(observer))
15    return;
16
17  token_service_observers_.AddObserver(observer);
18  if (++token_service_observer_count_ == 1)
19    token_service->AddObserver(this);
20}
21
22void IdentityProvider::RemoveActiveAccountRefreshTokenObserver(
23    OAuth2TokenService::Observer* observer) {
24  OAuth2TokenService* token_service = GetTokenService();
25  if (!token_service || !token_service_observers_.HasObserver(observer))
26    return;
27
28  token_service_observers_.RemoveObserver(observer);
29  if (--token_service_observer_count_ == 0)
30    token_service->RemoveObserver(this);
31}
32
33void IdentityProvider::AddObserver(Observer* observer) {
34  observers_.AddObserver(observer);
35}
36
37void IdentityProvider::RemoveObserver(Observer* observer) {
38  observers_.RemoveObserver(observer);
39}
40
41void IdentityProvider::OnRefreshTokenAvailable(const std::string& account_id) {
42  if (account_id != GetActiveAccountId())
43    return;
44  FOR_EACH_OBSERVER(OAuth2TokenService::Observer,
45                    token_service_observers_,
46                    OnRefreshTokenAvailable(account_id));
47}
48
49void IdentityProvider::OnRefreshTokenRevoked(const std::string& account_id) {
50  if (account_id != GetActiveAccountId())
51    return;
52  FOR_EACH_OBSERVER(OAuth2TokenService::Observer,
53                    token_service_observers_,
54                    OnRefreshTokenRevoked(account_id));
55}
56
57void IdentityProvider::OnRefreshTokensLoaded() {
58  FOR_EACH_OBSERVER(OAuth2TokenService::Observer,
59                    token_service_observers_,
60                    OnRefreshTokensLoaded());
61}
62
63IdentityProvider::IdentityProvider() : token_service_observer_count_(0) {}
64
65void IdentityProvider::FireOnActiveAccountLogin() {
66  FOR_EACH_OBSERVER(Observer, observers_, OnActiveAccountLogin());
67}
68
69void IdentityProvider::FireOnActiveAccountLogout() {
70  FOR_EACH_OBSERVER(Observer, observers_, OnActiveAccountLogout());
71}
72