summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/signin/chrome_signin_status_metrics_provider_delegate.cc
blob: 63fe0ae0a6bb9492830e10643ad565e9bf8c58a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/signin/chrome_signin_status_metrics_provider_delegate.h"

#include <string>
#include <vector>

#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "components/signin/core/browser/signin_status_metrics_provider.h"
#include "components/signin/public/identity_manager/identity_manager.h"

#if !defined(OS_ANDROID)
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#endif

ChromeSigninStatusMetricsProviderDelegate::
    ChromeSigninStatusMetricsProviderDelegate() {}

ChromeSigninStatusMetricsProviderDelegate::
    ~ChromeSigninStatusMetricsProviderDelegate() {
#if !defined(OS_ANDROID)
  BrowserList::RemoveObserver(this);
#endif

  auto* factory = IdentityManagerFactory::GetInstance();
  if (factory)
    factory->RemoveObserver(this);
}

void ChromeSigninStatusMetricsProviderDelegate::Initialize() {
#if !defined(OS_ANDROID)
  // On Android, there is always only one profile in any situation, opening new
  // windows (which is possible with only some Android devices) will not change
  // the opened profiles signin status.
  BrowserList::AddObserver(this);
#endif

  auto* factory = IdentityManagerFactory::GetInstance();
  if (factory)
    factory->AddObserver(this);
}

AccountsStatus
ChromeSigninStatusMetricsProviderDelegate::GetStatusOfAllAccounts() {
  ProfileManager* profile_manager = g_browser_process->profile_manager();
  std::vector<Profile*> profile_list = profile_manager->GetLoadedProfiles();

  AccountsStatus accounts_status;
  accounts_status.num_accounts = profile_list.size();
  for (Profile* profile : profile_list) {
#if !defined(OS_ANDROID)
    if (chrome::GetBrowserCount(profile) == 0) {
      // The profile is loaded, but there's no opened browser for this profile.
      continue;
    }
#endif
    accounts_status.num_opened_accounts++;

    signin::IdentityManager* identity_manager =
        IdentityManagerFactory::GetForProfile(profile->GetOriginalProfile());
    if (identity_manager &&
        identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSync)) {
      accounts_status.num_signed_in_accounts++;
    }
  }

  return accounts_status;
}

std::vector<signin::IdentityManager*>
ChromeSigninStatusMetricsProviderDelegate::GetIdentityManagersForAllAccounts() {
  ProfileManager* profile_manager = g_browser_process->profile_manager();
  std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles();

  std::vector<signin::IdentityManager*> managers;
  for (Profile* profile : profiles) {
    auto* identity_manager =
        IdentityManagerFactory::GetForProfileIfExists(profile);
    if (identity_manager)
      managers.push_back(identity_manager);
  }

  return managers;
}

#if !defined(OS_ANDROID)
void ChromeSigninStatusMetricsProviderDelegate::OnBrowserAdded(
    Browser* browser) {
  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(browser->profile());

  // Nothing will change if the opened browser is in incognito mode.
  if (!identity_manager)
    return;

  const bool signed_in =
      identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSync);
  UpdateStatusWhenBrowserAdded(signed_in);
}
#endif

void ChromeSigninStatusMetricsProviderDelegate::IdentityManagerCreated(
    signin::IdentityManager* identity_manager) {
  owner()->OnIdentityManagerCreated(identity_manager);
}

void ChromeSigninStatusMetricsProviderDelegate::UpdateStatusWhenBrowserAdded(
    bool signed_in) {
#if !defined(OS_ANDROID)
  SigninStatusMetricsProviderBase::SigninStatus status =
      owner()->signin_status();

  // NOTE: If |status| is MIXED_SIGNIN_STATUS, this method
  // intentionally does not update it.
  if ((status == SigninStatusMetricsProviderBase::ALL_PROFILES_NOT_SIGNED_IN &&
       signed_in) ||
      (status == SigninStatusMetricsProviderBase::ALL_PROFILES_SIGNED_IN &&
       !signed_in)) {
    owner()->UpdateSigninStatus(
        SigninStatusMetricsProviderBase::MIXED_SIGNIN_STATUS);
  } else if (status == SigninStatusMetricsProviderBase::UNKNOWN_SIGNIN_STATUS) {
    // If when function ProvideCurrentSessionData() is called, Chrome is
    // running in the background with no browser window opened, |signin_status_|
    // will be reset to |UNKNOWN_SIGNIN_STATUS|. Then this newly added browser
    // is the only opened browser/profile and its signin status represents
    // the whole status.
    owner()->UpdateSigninStatus(
        signed_in
            ? SigninStatusMetricsProviderBase::ALL_PROFILES_SIGNED_IN
            : SigninStatusMetricsProviderBase::ALL_PROFILES_NOT_SIGNED_IN);
  }
#endif
}