Update V8 to version 4.6.22.
[chromium-blink-merge.git] / chrome / browser / metrics / signin_status_metrics_provider.cc
blob29adda48c2cf2b5f19475462a7b25e29a2ab3127
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.
5 #include "chrome/browser/metrics/signin_status_metrics_provider.h"
7 #include <string>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/location.h"
12 #include "base/metrics/histogram.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_info_cache.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_list.h"
21 #include "components/signin/core/browser/signin_manager.h"
23 #if !defined(OS_ANDROID)
24 #include "chrome/browser/ui/browser_finder.h"
25 #endif
27 namespace {
29 // The event of calling function ComputeCurrentSigninStatus and the errors
30 // occurred during the function execution.
31 enum ComputeSigninStatus {
32 ENTERED_COMPUTE_SIGNIN_STATUS,
33 ERROR_NO_PROFILE_FOUND,
34 NO_BROWSER_OPENED,
35 USER_SIGNIN_WHEN_STATUS_UNKNOWN,
36 USER_SIGNOUT_WHEN_STATUS_UNKNOWN,
37 TRY_TO_OVERRIDE_ERROR_STATUS,
38 COMPUTE_SIGNIN_STATUS_MAX,
41 void RecordComputeSigninStatusHistogram(ComputeSigninStatus status) {
42 UMA_HISTOGRAM_ENUMERATION("UMA.ComputeCurrentSigninStatus", status,
43 COMPUTE_SIGNIN_STATUS_MAX);
46 } // namespace
48 SigninStatusMetricsProvider::SigninStatusMetricsProvider(bool is_test)
49 : scoped_observer_(this),
50 is_test_(is_test),
51 weak_ptr_factory_(this) {
52 if (is_test_)
53 return;
55 // Postpone the initialization until all threads are created.
56 base::ThreadTaskRunnerHandle::Get()->PostTask(
57 FROM_HERE, base::Bind(&SigninStatusMetricsProvider::Initialize,
58 weak_ptr_factory_.GetWeakPtr()));
61 SigninStatusMetricsProvider::~SigninStatusMetricsProvider() {
62 if (is_test_)
63 return;
65 #if !defined(OS_ANDROID)
66 BrowserList::RemoveObserver(this);
67 #endif
69 SigninManagerFactory* factory = SigninManagerFactory::GetInstance();
70 if (factory)
71 factory->RemoveObserver(this);
74 void SigninStatusMetricsProvider::ProvideGeneralMetrics(
75 metrics::ChromeUserMetricsExtension* uma_proto) {
76 RecordSigninStatusHistogram(signin_status());
77 // After a histogram value is recorded, a new UMA session will be started, so
78 // we need to re-check the current sign-in status regardless of the previous
79 // recorded |signin_status_| value.
80 ResetSigninStatus();
81 ComputeCurrentSigninStatus();
84 // static
85 SigninStatusMetricsProvider* SigninStatusMetricsProvider::CreateInstance() {
86 return new SigninStatusMetricsProvider(false);
89 void SigninStatusMetricsProvider::OnBrowserAdded(Browser* browser) {
90 if (signin_status() == MIXED_SIGNIN_STATUS)
91 return;
93 SigninManager* manager = SigninManagerFactory::GetForProfile(
94 browser->profile());
96 // Nothing will change if the opened browser is in incognito mode.
97 if (!manager)
98 return;
100 const bool signed_in = manager->IsAuthenticated();
101 UpdateStatusWhenBrowserAdded(signed_in);
104 void SigninStatusMetricsProvider::SigninManagerCreated(
105 SigninManagerBase* manager) {
106 // Whenever a new profile is created, a new SigninManagerBase will be created
107 // for it. This ensures that all sign-in or sign-out actions of all opened
108 // profiles are being monitored.
109 scoped_observer_.Add(manager);
111 // If the status is unknown, it means this is the first created
112 // SigninManagerBase and the corresponding profile should be the only opened
113 // profile.
114 if (signin_status() == UNKNOWN_SIGNIN_STATUS) {
115 size_t signed_in_count =
116 manager->IsAuthenticated() ? 1 : 0;
117 UpdateInitialSigninStatus(1, signed_in_count);
121 void SigninStatusMetricsProvider::SigninManagerShutdown(
122 SigninManagerBase* manager) {
123 if (scoped_observer_.IsObserving(manager))
124 scoped_observer_.Remove(manager);
127 void SigninStatusMetricsProvider::GoogleSigninSucceeded(
128 const std::string& account_id,
129 const std::string& username,
130 const std::string& password) {
131 SigninStatus recorded_signin_status = signin_status();
132 if (recorded_signin_status == ALL_PROFILES_NOT_SIGNED_IN) {
133 SetSigninStatus(MIXED_SIGNIN_STATUS);
134 } else if (recorded_signin_status == UNKNOWN_SIGNIN_STATUS) {
135 // There should have at least one browser opened if the user can sign in, so
136 // signin_status_ value should not be unknown.
137 SetSigninStatus(ERROR_GETTING_SIGNIN_STATUS);
138 RecordComputeSigninStatusHistogram(USER_SIGNIN_WHEN_STATUS_UNKNOWN);
142 void SigninStatusMetricsProvider::GoogleSignedOut(const std::string& account_id,
143 const std::string& username) {
144 SigninStatus recorded_signin_status = signin_status();
145 if (recorded_signin_status == ALL_PROFILES_SIGNED_IN) {
146 SetSigninStatus(MIXED_SIGNIN_STATUS);
147 } else if (recorded_signin_status == UNKNOWN_SIGNIN_STATUS) {
148 // There should have at least one browser opened if the user can sign out,
149 // so signin_status_ value should not be unknown.
150 SetSigninStatus(ERROR_GETTING_SIGNIN_STATUS);
151 RecordComputeSigninStatusHistogram(USER_SIGNOUT_WHEN_STATUS_UNKNOWN);
155 void SigninStatusMetricsProvider::Initialize() {
156 // Add observers.
157 #if !defined(OS_ANDROID)
158 // On Android, there is always only one profile in any situation, opening new
159 // windows (which is possible with only some Android devices) will not change
160 // the opened profiles signin status.
161 BrowserList::AddObserver(this);
162 #endif
163 SigninManagerFactory::GetInstance()->AddObserver(this);
165 // Start observing all already-created SigninManagers.
166 ProfileManager* profile_manager = g_browser_process->profile_manager();
167 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles();
168 for (size_t i = 0; i < profiles.size(); ++i) {
169 SigninManager* manager = SigninManagerFactory::GetForProfileIfExists(
170 profiles[i]);
171 if (manager) {
172 DCHECK(!scoped_observer_.IsObserving(manager));
173 scoped_observer_.Add(manager);
177 // It is possible that when this object is created, no SigninManager is
178 // created yet, for example, when Chrome is opened for the first time after
179 // installation on desktop, or when Chrome on Android is loaded into memory.
180 if (profiles.empty()) {
181 SetSigninStatus(UNKNOWN_SIGNIN_STATUS);
182 } else {
183 ComputeCurrentSigninStatus();
187 void SigninStatusMetricsProvider::UpdateInitialSigninStatus(
188 size_t total_count,
189 size_t signed_in_profiles_count) {
190 // total_count is known to be bigger than 0.
191 if (signed_in_profiles_count == 0) {
192 SetSigninStatus(ALL_PROFILES_NOT_SIGNED_IN);
193 } else if (total_count == signed_in_profiles_count) {
194 SetSigninStatus(ALL_PROFILES_SIGNED_IN);
195 } else {
196 SetSigninStatus(MIXED_SIGNIN_STATUS);
200 void SigninStatusMetricsProvider::UpdateStatusWhenBrowserAdded(bool signed_in) {
201 #if !defined(OS_ANDROID)
202 SigninStatus recorded_signin_status = signin_status();
203 if ((recorded_signin_status == ALL_PROFILES_NOT_SIGNED_IN && signed_in) ||
204 (recorded_signin_status == ALL_PROFILES_SIGNED_IN && !signed_in)) {
205 SetSigninStatus(MIXED_SIGNIN_STATUS);
206 } else if (recorded_signin_status == UNKNOWN_SIGNIN_STATUS) {
207 // If when function ProvideGeneralMetrics() is called, Chrome is
208 // running in the background with no browser window opened, |signin_status_|
209 // will be reset to |UNKNOWN_SIGNIN_STATUS|. Then this newly added browser
210 // is the only opened browser/profile and its signin status represents
211 // the whole status.
212 SetSigninStatus(signed_in ? ALL_PROFILES_SIGNED_IN :
213 ALL_PROFILES_NOT_SIGNED_IN);
215 #endif
218 void SigninStatusMetricsProvider::ComputeCurrentSigninStatus() {
219 ProfileManager* profile_manager = g_browser_process->profile_manager();
220 std::vector<Profile*> profile_list = profile_manager->GetLoadedProfiles();
222 size_t opened_profiles_count = 0;
223 size_t signed_in_profiles_count = 0;
225 for (size_t i = 0; i < profile_list.size(); ++i) {
226 #if !defined(OS_ANDROID)
227 if (chrome::GetTotalBrowserCountForProfile(profile_list[i]) == 0) {
228 // The profile is loaded, but there's no opened browser for this profile.
229 continue;
231 #endif
232 opened_profiles_count++;
233 SigninManager* manager = SigninManagerFactory::GetForProfile(
234 profile_list[i]->GetOriginalProfile());
235 if (manager && manager->IsAuthenticated())
236 signed_in_profiles_count++;
239 RecordComputeSigninStatusHistogram(ENTERED_COMPUTE_SIGNIN_STATUS);
240 if (profile_list.empty()) {
241 // This should not happen. If it does, record it in histogram.
242 RecordComputeSigninStatusHistogram(ERROR_NO_PROFILE_FOUND);
243 SetSigninStatus(ERROR_GETTING_SIGNIN_STATUS);
244 } else if (opened_profiles_count == 0) {
245 // The code indicates that Chrome is running in the background but no
246 // browser window is opened.
247 RecordComputeSigninStatusHistogram(NO_BROWSER_OPENED);
248 SetSigninStatus(UNKNOWN_SIGNIN_STATUS);
249 } else {
250 UpdateInitialSigninStatus(opened_profiles_count, signed_in_profiles_count);
254 SigninStatusMetricsProvider::SigninStatus
255 SigninStatusMetricsProvider::GetSigninStatusForTesting() {
256 return signin_status();