1 // Copyright 2015 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_investigator.h"
6 
7 #include "base/logging.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "components/signin/public/base/signin_metrics.h"
10 #include "components/signin/public/base/signin_pref_names.h"
11 #include "google_apis/gaia/gaia_auth_util.h"
12 
13 using signin_metrics::AccountEquality;
14 using signin_metrics::LogAccountEquality;
15 
SigninInvestigator(const std::string & current_email,const std::string & current_id,DependencyProvider * provider)16 SigninInvestigator::SigninInvestigator(const std::string& current_email,
17                                        const std::string& current_id,
18                                        DependencyProvider* provider)
19     : current_email_(current_email),
20       current_id_(current_id),
21       provider_(provider) {
22   DCHECK(!current_email_.empty());
23   DCHECK(provider);
24 }
25 
~SigninInvestigator()26 SigninInvestigator::~SigninInvestigator() {}
27 
AreAccountsEqualWithFallback()28 bool SigninInvestigator::AreAccountsEqualWithFallback() {
29   const std::string last_id =
30       provider_->GetPrefs()->GetString(prefs::kGoogleServicesLastAccountId);
31   bool same_email = gaia::AreEmailsSame(
32       current_email_,
33       provider_->GetPrefs()->GetString(prefs::kGoogleServicesLastUsername));
34   if (!current_id_.empty() && !last_id.empty()) {
35     bool same_id = current_id_ == last_id;
36     if (same_email && same_id) {
37       LogAccountEquality(AccountEquality::BOTH_EQUAL);
38     } else if (same_email) {
39       LogAccountEquality(AccountEquality::ONLY_SAME_EMAIL);
40     } else if (same_id) {
41       LogAccountEquality(AccountEquality::ONLY_SAME_ID);
42     } else {
43       LogAccountEquality(AccountEquality::BOTH_DIFFERENT);
44     }
45     return same_id;
46   } else {
47     LogAccountEquality(AccountEquality::EMAIL_FALLBACK);
48     return same_email;
49   }
50 }
51 
Investigate()52 InvestigatedScenario SigninInvestigator::Investigate() {
53   InvestigatedScenario scenario;
54   if (provider_->GetPrefs()
55           ->GetString(prefs::kGoogleServicesLastUsername)
56           .empty()) {
57     scenario = InvestigatedScenario::kFirstSignIn;
58   } else if (AreAccountsEqualWithFallback()) {
59     scenario = InvestigatedScenario::kSameAccount;
60   } else {
61     scenario = InvestigatedScenario::kDifferentAccount;
62   }
63 
64   UMA_HISTOGRAM_ENUMERATION("Signin.InvestigatedScenario", scenario);
65   return scenario;
66 }
67