| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/metrics/signin_status_metrics_provider.h" | 5 #include "chrome/browser/metrics/signin_status_metrics_provider.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #if !defined(OS_ANDROID) | 21 #if !defined(OS_ANDROID) |
| 22 #include "chrome/browser/ui/browser_finder.h" | 22 #include "chrome/browser/ui/browser_finder.h" |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 // The event of calling function ComputeCurrentSigninStatus and the errors | 27 // The event of calling function ComputeCurrentSigninStatus and the errors |
| 28 // occurred during the function execution. | 28 // occurred during the function execution. |
| 29 enum ComputeSigninStatus { | 29 enum ComputeSigninStatus { |
| 30 ENTERED_COMPUTE_SIGNIN_STATUS, | 30 ENTERED_COMPUTE_SIGNIN_STATUS, |
| 31 ERROR_COMPUTE_SIGNIN_STATUS, | 31 ERROR_NO_PROFILE_FOUND, |
| 32 NO_BROWSER_OPENED, |
| 33 USER_SIGNIN_WHEN_STATUS_UNKNOWN, |
| 34 USER_SIGNOUT_WHEN_STATUS_UNKNOWN, |
| 32 COMPUTE_SIGNIN_STATUS_MAX, | 35 COMPUTE_SIGNIN_STATUS_MAX, |
| 33 }; | 36 }; |
| 34 | 37 |
| 35 void RecordComputeSigninStatusHistogram(ComputeSigninStatus status) { | 38 void RecordComputeSigninStatusHistogram(ComputeSigninStatus status) { |
| 36 UMA_HISTOGRAM_ENUMERATION("UMA.ComputeCurrentSigninStatus", status, | 39 UMA_HISTOGRAM_ENUMERATION("UMA.ComputeCurrentSigninStatus", status, |
| 37 COMPUTE_SIGNIN_STATUS_MAX); | 40 COMPUTE_SIGNIN_STATUS_MAX); |
| 38 } | 41 } |
| 39 | 42 |
| 40 } // namespace | 43 } // namespace |
| 41 | 44 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 void SigninStatusMetricsProvider::SigninManagerShutdown( | 119 void SigninStatusMetricsProvider::SigninManagerShutdown( |
| 117 SigninManagerBase* manager) { | 120 SigninManagerBase* manager) { |
| 118 if (scoped_observer_.IsObserving(manager)) | 121 if (scoped_observer_.IsObserving(manager)) |
| 119 scoped_observer_.Remove(manager); | 122 scoped_observer_.Remove(manager); |
| 120 } | 123 } |
| 121 | 124 |
| 122 void SigninStatusMetricsProvider::GoogleSigninSucceeded( | 125 void SigninStatusMetricsProvider::GoogleSigninSucceeded( |
| 123 const std::string& account_id, | 126 const std::string& account_id, |
| 124 const std::string& username, | 127 const std::string& username, |
| 125 const std::string& password) { | 128 const std::string& password) { |
| 126 if (signin_status_ == ALL_PROFILES_NOT_SIGNED_IN) | 129 if (signin_status_ == ALL_PROFILES_NOT_SIGNED_IN) { |
| 127 signin_status_ = MIXED_SIGNIN_STATUS; | 130 signin_status_ = MIXED_SIGNIN_STATUS; |
| 131 } else if (signin_status_ == UNKNOWN_SIGNIN_STATUS) { |
| 132 // There should have at least one browser opened if the user can sign in, so |
| 133 // signin_status_ value should not be unknown. |
| 134 signin_status_ = ERROR_GETTING_SIGNIN_STATUS; |
| 135 RecordComputeSigninStatusHistogram(USER_SIGNIN_WHEN_STATUS_UNKNOWN); |
| 136 } |
| 128 } | 137 } |
| 129 | 138 |
| 130 void SigninStatusMetricsProvider::GoogleSignedOut(const std::string& account_id, | 139 void SigninStatusMetricsProvider::GoogleSignedOut(const std::string& account_id, |
| 131 const std::string& username) { | 140 const std::string& username) { |
| 132 if (signin_status_ == ALL_PROFILES_SIGNED_IN) | 141 if (signin_status_ == ALL_PROFILES_SIGNED_IN) { |
| 133 signin_status_ = MIXED_SIGNIN_STATUS; | 142 signin_status_ = MIXED_SIGNIN_STATUS; |
| 143 } else if (signin_status_ == UNKNOWN_SIGNIN_STATUS) { |
| 144 // There should have at least one browser opened if the user can sign out, |
| 145 // so signin_status_ value should not be unknown. |
| 146 signin_status_ = ERROR_GETTING_SIGNIN_STATUS; |
| 147 RecordComputeSigninStatusHistogram(USER_SIGNOUT_WHEN_STATUS_UNKNOWN); |
| 148 } |
| 134 } | 149 } |
| 135 | 150 |
| 136 void SigninStatusMetricsProvider::Initialize() { | 151 void SigninStatusMetricsProvider::Initialize() { |
| 137 // Add observers. | 152 // Add observers. |
| 138 #if !defined(OS_ANDROID) | 153 #if !defined(OS_ANDROID) |
| 139 // On Android, there is always only one profile in any situation, opening new | 154 // On Android, there is always only one profile in any situation, opening new |
| 140 // windows (which is possible with only some Android devices) will not change | 155 // windows (which is possible with only some Android devices) will not change |
| 141 // the opened profiles signin status. | 156 // the opened profiles signin status. |
| 142 BrowserList::AddObserver(this); | 157 BrowserList::AddObserver(this); |
| 143 #endif | 158 #endif |
| (...skipping 17 matching lines...) Expand all Loading... |
| 161 if (profiles.empty()) { | 176 if (profiles.empty()) { |
| 162 signin_status_ = UNKNOWN_SIGNIN_STATUS; | 177 signin_status_ = UNKNOWN_SIGNIN_STATUS; |
| 163 } else { | 178 } else { |
| 164 ComputeCurrentSigninStatus(); | 179 ComputeCurrentSigninStatus(); |
| 165 } | 180 } |
| 166 } | 181 } |
| 167 | 182 |
| 168 void SigninStatusMetricsProvider::UpdateInitialSigninStatus( | 183 void SigninStatusMetricsProvider::UpdateInitialSigninStatus( |
| 169 size_t total_count, | 184 size_t total_count, |
| 170 size_t signed_in_profiles_count) { | 185 size_t signed_in_profiles_count) { |
| 171 RecordComputeSigninStatusHistogram(ENTERED_COMPUTE_SIGNIN_STATUS); | 186 // total_count is known to be bigger than 0. |
| 172 | 187 if (signed_in_profiles_count == 0) { |
| 173 if (total_count == 0) { | |
| 174 // This should never happen. If it does, record it in histogram. | |
| 175 RecordComputeSigninStatusHistogram(ERROR_COMPUTE_SIGNIN_STATUS); | |
| 176 signin_status_ = UNKNOWN_SIGNIN_STATUS; | |
| 177 } else if (signed_in_profiles_count == 0) { | |
| 178 signin_status_ = ALL_PROFILES_NOT_SIGNED_IN; | 188 signin_status_ = ALL_PROFILES_NOT_SIGNED_IN; |
| 179 } else if (total_count == signed_in_profiles_count) { | 189 } else if (total_count == signed_in_profiles_count) { |
| 180 signin_status_ = ALL_PROFILES_SIGNED_IN; | 190 signin_status_ = ALL_PROFILES_SIGNED_IN; |
| 181 } else { | 191 } else { |
| 182 signin_status_ = MIXED_SIGNIN_STATUS; | 192 signin_status_ = MIXED_SIGNIN_STATUS; |
| 183 } | 193 } |
| 184 } | 194 } |
| 185 | 195 |
| 186 void SigninStatusMetricsProvider::UpdateStatusWhenBrowserAdded(bool signed_in) { | 196 void SigninStatusMetricsProvider::UpdateStatusWhenBrowserAdded(bool signed_in) { |
| 187 #if !defined(OS_ANDROID) | 197 #if !defined(OS_ANDROID) |
| 188 if ((signin_status_ == ALL_PROFILES_NOT_SIGNED_IN && signed_in) || | 198 if ((signin_status_ == ALL_PROFILES_NOT_SIGNED_IN && signed_in) || |
| 189 (signin_status_ == ALL_PROFILES_SIGNED_IN && !signed_in)) { | 199 (signin_status_ == ALL_PROFILES_SIGNED_IN && !signed_in)) { |
| 190 signin_status_ = MIXED_SIGNIN_STATUS; | 200 signin_status_ = MIXED_SIGNIN_STATUS; |
| 201 } else if (signin_status_ == UNKNOWN_SIGNIN_STATUS) { |
| 202 // If when function RecordSigninStatusHistogram() is called, Chrome is |
| 203 // running in the background with no browser window opened, |signin_status_| |
| 204 // will be reset to |UNKNOWN_SIGNIN_STATUS|. Then this newly added browser |
| 205 // is the only opened browser/profile and its signin status represents |
| 206 // the whole status. |
| 207 signin_status_ = signed_in ? ALL_PROFILES_SIGNED_IN : |
| 208 ALL_PROFILES_NOT_SIGNED_IN; |
| 191 } | 209 } |
| 192 #endif | 210 #endif |
| 193 } | 211 } |
| 194 | 212 |
| 195 void SigninStatusMetricsProvider::ComputeCurrentSigninStatus() { | 213 void SigninStatusMetricsProvider::ComputeCurrentSigninStatus() { |
| 196 // Get the sign-in status of all currently open profiles. Sign-in status is | |
| 197 // indicated by its username. When username is not empty, the profile is | |
| 198 // signed-in. | |
| 199 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 214 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 200 std::vector<Profile*> profile_list = profile_manager->GetLoadedProfiles(); | 215 std::vector<Profile*> profile_list = profile_manager->GetLoadedProfiles(); |
| 201 | 216 |
| 202 size_t opened_profiles_count = 0; | 217 size_t opened_profiles_count = 0; |
| 203 size_t signed_in_profiles_count = 0; | 218 size_t signed_in_profiles_count = 0; |
| 204 | 219 |
| 205 for (size_t i = 0; i < profile_list.size(); ++i) { | 220 for (size_t i = 0; i < profile_list.size(); ++i) { |
| 206 #if !defined(OS_ANDROID) | 221 #if !defined(OS_ANDROID) |
| 207 if (chrome::GetTotalBrowserCountForProfile(profile_list[i]) == 0) { | 222 if (chrome::GetTotalBrowserCountForProfile(profile_list[i]) == 0) { |
| 208 // The profile is loaded, but there's no opened browser for this profile. | 223 // The profile is loaded, but there's no opened browser for this profile. |
| 209 continue; | 224 continue; |
| 210 } | 225 } |
| 211 #endif | 226 #endif |
| 212 opened_profiles_count++; | 227 opened_profiles_count++; |
| 213 SigninManager* manager = SigninManagerFactory::GetForProfile( | 228 SigninManager* manager = SigninManagerFactory::GetForProfile( |
| 214 profile_list[i]->GetOriginalProfile()); | 229 profile_list[i]->GetOriginalProfile()); |
| 215 if (manager && manager->IsAuthenticated()) | 230 if (manager && manager->IsAuthenticated()) |
| 216 signed_in_profiles_count++; | 231 signed_in_profiles_count++; |
| 217 } | 232 } |
| 218 UpdateInitialSigninStatus(opened_profiles_count, signed_in_profiles_count); | 233 |
| 234 RecordComputeSigninStatusHistogram(ENTERED_COMPUTE_SIGNIN_STATUS); |
| 235 if (profile_list.empty()) { |
| 236 // This should not happen. If it does, record it in histogram. |
| 237 RecordComputeSigninStatusHistogram(ERROR_NO_PROFILE_FOUND); |
| 238 signin_status_ = ERROR_GETTING_SIGNIN_STATUS; |
| 239 } else if (opened_profiles_count == 0) { |
| 240 // The code indicates that Chrome is running in the background but no |
| 241 // browser window is opened. |
| 242 RecordComputeSigninStatusHistogram(NO_BROWSER_OPENED); |
| 243 signin_status_ = UNKNOWN_SIGNIN_STATUS; |
| 244 } else { |
| 245 UpdateInitialSigninStatus(opened_profiles_count, signed_in_profiles_count); |
| 246 } |
| 219 } | 247 } |
| 220 | 248 |
| 221 SigninStatusMetricsProvider::ProfilesSigninStatus | 249 SigninStatusMetricsProvider::ProfilesSigninStatus |
| 222 SigninStatusMetricsProvider::GetSigninStatusForTesting() { | 250 SigninStatusMetricsProvider::GetSigninStatusForTesting() { |
| 223 return signin_status_; | 251 return signin_status_; |
| 224 } | 252 } |
| OLD | NEW |