| 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 "components/signin/core/browser/signin_manager_base.h" | 5 #include "components/signin/core/browser/signin_manager_base.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "components/signin/core/browser/account_tracker_service.h" | 16 #include "components/signin/core/browser/account_tracker_service.h" |
| 17 #include "components/signin/core/browser/signin_client.h" | 17 #include "components/signin/core/browser/signin_client.h" |
| 18 #include "components/signin/core/common/signin_pref_names.h" | 18 #include "components/signin/core/common/signin_pref_names.h" |
| 19 #include "components/signin/core/common/signin_switches.h" | 19 #include "components/signin/core/common/signin_switches.h" |
| 20 #include "google_apis/gaia/gaia_auth_util.h" | 20 #include "google_apis/gaia/gaia_auth_util.h" |
| 21 #include "google_apis/gaia/gaia_constants.h" | 21 #include "google_apis/gaia/gaia_constants.h" |
| 22 #include "google_apis/gaia/gaia_urls.h" | 22 #include "google_apis/gaia/gaia_urls.h" |
| 23 | 23 |
| 24 using namespace signin_internals_util; | 24 using namespace signin_internals_util; |
| 25 | 25 |
| 26 SigninManagerBase::SigninManagerBase(SigninClient* client) | 26 SigninManagerBase::SigninManagerBase( |
| 27 : client_(client), initialized_(false), weak_pointer_factory_(this) {} | 27 SigninClient* client, |
| 28 AccountTrackerService* account_tracker_service) |
| 29 : client_(client), |
| 30 account_tracker_service_(account_tracker_service), |
| 31 initialized_(false), |
| 32 weak_pointer_factory_(this) { |
| 33 DCHECK(client_); |
| 34 DCHECK(account_tracker_service_); |
| 35 } |
| 28 | 36 |
| 29 SigninManagerBase::~SigninManagerBase() {} | 37 SigninManagerBase::~SigninManagerBase() {} |
| 30 | 38 |
| 31 void SigninManagerBase::Initialize(PrefService* local_state) { | 39 void SigninManagerBase::Initialize(PrefService* local_state) { |
| 32 // Should never call Initialize() twice. | 40 // Should never call Initialize() twice. |
| 33 DCHECK(!IsInitialized()); | 41 DCHECK(!IsInitialized()); |
| 34 initialized_ = true; | 42 initialized_ = true; |
| 35 | 43 |
| 36 // If the user is clearing the token service from the command line, then | 44 // If the user is clearing the token service from the command line, then |
| 37 // clear their login info also (not valid to be logged in without any | 45 // clear their login info also (not valid to be logged in without any |
| 38 // tokens). | 46 // tokens). |
| 39 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 47 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 40 if (cmd_line->HasSwitch(switches::kClearTokenService)) | 48 if (cmd_line->HasSwitch(switches::kClearTokenService)) { |
| 49 client_->GetPrefs()->ClearPref(prefs::kGoogleServicesAccountId); |
| 41 client_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); | 50 client_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); |
| 51 client_->GetPrefs()->ClearPref(prefs::kGoogleServicesUserAccountId); |
| 52 } |
| 42 | 53 |
| 43 std::string user = | 54 std::string account_id = |
| 44 client_->GetPrefs()->GetString(prefs::kGoogleServicesUsername); | 55 client_->GetPrefs()->GetString(prefs::kGoogleServicesAccountId); |
| 45 if (!user.empty()) | 56 |
| 46 SetAuthenticatedUsername(user); | 57 // Handle backward compatibility: if kGoogleServicesAccountId is empty, but |
| 58 // kGoogleServicesUsername is not, then this is an old profile that needs to |
| 59 // be updated. kGoogleServicesUserAccountId should not be empty, and contains |
| 60 // the gaia_id. Use both properties to prime the account tracker before |
| 61 // proceeding. |
| 62 if (account_id.empty()) { |
| 63 std::string pref_account_username = |
| 64 client_->GetPrefs()->GetString(prefs::kGoogleServicesUsername); |
| 65 std::string pref_gaia_id = |
| 66 client_->GetPrefs()->GetString(prefs::kGoogleServicesUserAccountId); |
| 67 DCHECK(pref_account_username.empty() || !pref_gaia_id.empty()); |
| 68 if (!pref_account_username.empty() && !pref_gaia_id.empty()) { |
| 69 account_id = account_tracker_service_->SeedAccountInfo( |
| 70 pref_gaia_id, pref_account_username); |
| 71 |
| 72 // Now remove obsolete preferences. |
| 73 client_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); |
| 74 |
| 75 // TODO(rogerta): once migration to gaia id is complete, remove |
| 76 // kGoogleServicesUserAccountId and change all uses of that pref to |
| 77 // kGoogleServicesAccountId. |
| 78 } |
| 79 } |
| 80 |
| 81 if (!account_id.empty()) |
| 82 SetAuthenticatedAccountId(account_id); |
| 47 } | 83 } |
| 48 | 84 |
| 49 bool SigninManagerBase::IsInitialized() const { return initialized_; } | 85 bool SigninManagerBase::IsInitialized() const { return initialized_; } |
| 50 | 86 |
| 51 bool SigninManagerBase::IsSigninAllowed() const { | 87 bool SigninManagerBase::IsSigninAllowed() const { |
| 52 return client_->GetPrefs()->GetBoolean(prefs::kSigninAllowed); | 88 return client_->GetPrefs()->GetBoolean(prefs::kSigninAllowed); |
| 53 } | 89 } |
| 54 | 90 |
| 55 const std::string& SigninManagerBase::GetAuthenticatedUsername() const { | 91 std::string SigninManagerBase::GetAuthenticatedUsername() const { |
| 56 return authenticated_username_; | 92 return account_tracker_service_->GetAccountInfo( |
| 93 GetAuthenticatedAccountId()).email; |
| 57 } | 94 } |
| 58 | 95 |
| 59 const std::string& SigninManagerBase::GetAuthenticatedAccountId() const { | 96 const std::string& SigninManagerBase::GetAuthenticatedAccountId() const { |
| 60 return authenticated_account_id_; | 97 return authenticated_account_id_; |
| 61 } | 98 } |
| 62 | 99 |
| 63 void SigninManagerBase::SetAuthenticatedUsername(const std::string& username) { | 100 void SigninManagerBase::SetAuthenticatedAccountInfo(const std::string& gaia_id, |
| 64 DCHECK(!username.empty()); | 101 const std::string& email) { |
| 65 if (!authenticated_username_.empty()) { | 102 std::string account_id = |
| 66 DLOG_IF(ERROR, !gaia::AreEmailsSame(username, authenticated_username_)) | 103 account_tracker_service_->SeedAccountInfo(gaia_id, email); |
| 67 << "Tried to change the authenticated username to something different: " | 104 SetAuthenticatedAccountId(account_id); |
| 68 << "Current: " << authenticated_username_ << ", New: " << username; | 105 } |
| 69 | 106 |
| 70 #if defined(OS_IOS) | 107 void SigninManagerBase::SetAuthenticatedAccountId( |
| 71 // Prior to M26, chrome on iOS did not normalize the email before setting | 108 const std::string& account_id) { |
| 72 // it in SigninManager. If the emails are the same as given by | 109 DCHECK(!account_id.empty()); |
| 73 // gaia::AreEmailsSame() but not the same as given by std::string::op==(), | 110 if (!authenticated_account_id_.empty()) { |
| 74 // make sure to set the authenticated name below. | 111 DLOG_IF(ERROR, account_id != authenticated_account_id_) |
| 75 if (!gaia::AreEmailsSame(username, authenticated_username_) || | 112 << "Tried to change the authenticated id to something different: " |
| 76 username == authenticated_username_) { | 113 << "Current: " << authenticated_account_id_ << ", New: " << account_id; |
| 77 return; | |
| 78 } | |
| 79 #else | |
| 80 return; | 114 return; |
| 81 #endif | |
| 82 } | 115 } |
| 83 std::string pref_username = | |
| 84 client_->GetPrefs()->GetString(prefs::kGoogleServicesUsername); | |
| 85 DCHECK(pref_username.empty() || gaia::AreEmailsSame(username, pref_username)) | |
| 86 << "username: " << username << "; pref_username: " << pref_username; | |
| 87 authenticated_username_ = username; | |
| 88 | 116 |
| 89 // TODO(rogerta): remove this DCHECK when migration work is started. | 117 std::string pref_account_id = |
| 90 DCHECK_EQ(AccountTrackerService::MIGRATION_NOT_STARTED, | 118 client_->GetPrefs()->GetString(prefs::kGoogleServicesAccountId); |
| 91 AccountTrackerService::GetMigrationState(client_->GetPrefs())); | |
| 92 authenticated_account_id_ = | |
| 93 AccountTrackerService::PickAccountIdForAccount(client_->GetPrefs(), | |
| 94 username, | |
| 95 username); | |
| 96 client_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, username); | |
| 97 | 119 |
| 98 // Go ahead and update the last signed in username here as well. Once a | 120 DCHECK(pref_account_id.empty() || pref_account_id == account_id) |
| 121 << "account_id=" << account_id |
| 122 << " pref_account_id=" << pref_account_id; |
| 123 authenticated_account_id_ = account_id; |
| 124 client_->GetPrefs()->SetString(prefs::kGoogleServicesAccountId, account_id); |
| 125 |
| 126 // This preference is set so that code on I/O thread has access to the |
| 127 // Gaia id of the signed in user. |
| 128 AccountTrackerService::AccountInfo info = |
| 129 account_tracker_service_->GetAccountInfo(account_id); |
| 130 DCHECK(!info.gaia.empty()); |
| 131 client_->GetPrefs()->SetString(prefs::kGoogleServicesUserAccountId, |
| 132 info.gaia); |
| 133 |
| 134 // Go ahead and update the last signed in account info here as well. Once a |
| 99 // user is signed in the two preferences should match. Doing it here as | 135 // user is signed in the two preferences should match. Doing it here as |
| 100 // opposed to on signin allows us to catch the upgrade scenario. | 136 // opposed to on signin allows us to catch the upgrade scenario. |
| 101 client_->GetPrefs()->SetString(prefs::kGoogleServicesLastUsername, username); | 137 client_->GetPrefs()->SetString(prefs::kGoogleServicesLastUsername, |
| 102 } | 138 info.email); |
| 103 | |
| 104 void SigninManagerBase::ClearAuthenticatedUsername() { | |
| 105 authenticated_username_.clear(); | |
| 106 authenticated_account_id_.clear(); | |
| 107 } | 139 } |
| 108 | 140 |
| 109 bool SigninManagerBase::IsAuthenticated() const { | 141 bool SigninManagerBase::IsAuthenticated() const { |
| 110 return !authenticated_account_id_.empty(); | 142 return !authenticated_account_id_.empty(); |
| 111 } | 143 } |
| 112 | 144 |
| 113 bool SigninManagerBase::AuthInProgress() const { | 145 bool SigninManagerBase::AuthInProgress() const { |
| 114 // SigninManagerBase never kicks off auth processes itself. | 146 // SigninManagerBase never kicks off auth processes itself. |
| 115 return false; | 147 return false; |
| 116 } | 148 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 135 signin_diagnostics_observers_.RemoveObserver(observer); | 167 signin_diagnostics_observers_.RemoveObserver(observer); |
| 136 } | 168 } |
| 137 | 169 |
| 138 void SigninManagerBase::NotifyDiagnosticsObservers( | 170 void SigninManagerBase::NotifyDiagnosticsObservers( |
| 139 const TimedSigninStatusField& field, | 171 const TimedSigninStatusField& field, |
| 140 const std::string& value) { | 172 const std::string& value) { |
| 141 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, | 173 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, |
| 142 signin_diagnostics_observers_, | 174 signin_diagnostics_observers_, |
| 143 NotifySigninValueChanged(field, value)); | 175 NotifySigninValueChanged(field, value)); |
| 144 } | 176 } |
| OLD | NEW |