Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(882)

Side by Side Diff: components/signin/core/browser/signin_manager_base.cc

Issue 964563002: Replace SetAuthenticatedUsername with SetAuthenticatedAccountId. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@priv
Patch Set: rebased Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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);
Mike Lerman 2015/04/08 14:45:27 Also clear prefs::kGoogleServicesUserAccountId?
Roger Tawa OOO till Jul 10th 2015/04/08 20:24:21 Already done. Did you mean some other pref?
Mike Lerman 2015/04/08 20:49:28 I did, I meeant kGoogleServicesLastAccountId.
Roger Tawa OOO till Jul 10th 2015/04/09 15:59:47 Good question. The *Last* prefs are for rememberi
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
Mike Lerman 2015/04/08 14:45:27 comment nit: There's no "profile" within component
Roger Tawa OOO till Jul 10th 2015/04/08 20:24:21 I think I'll it as is. I'm not sure "user" or "br
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());
Mike Lerman 2015/04/08 14:45:27 Is this supposed to read !pref_account_username.em
Roger Tawa OOO till Jul 10th 2015/04/08 20:24:21 I'm trying to DCHECK that if I have a username, th
68 if (!pref_account_username.empty() && !pref_gaia_id.empty()) {
69 account_tracker_service_->SeedAccountInfo(
Mike Lerman 2015/04/08 14:45:27 Should lines 69-72 instead be: account_id = SetAut
Roger Tawa OOO till Jul 10th 2015/04/08 20:24:21 I did it this way because this is the "upgrade" ca
70 pref_gaia_id, pref_account_username);
71
72 account_id = pref_account_username;
73 client_->GetPrefs()->SetString(prefs::kGoogleServicesLastAccountId,
74 account_id);
75
76 // Now remove obsolete preferences.
77 client_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername);
78
79 // TODO(rogerta): once migration to gaia id is complete, remove
80 // kGoogleServicesUserAccountId and change all uses of that pref to
81 // kGoogleServicesAccountId.
82 }
83 }
84
85 if (!account_id.empty())
86 SetAuthenticatedAccountId(account_id);
47 } 87 }
48 88
49 bool SigninManagerBase::IsInitialized() const { return initialized_; } 89 bool SigninManagerBase::IsInitialized() const { return initialized_; }
50 90
51 bool SigninManagerBase::IsSigninAllowed() const { 91 bool SigninManagerBase::IsSigninAllowed() const {
52 return client_->GetPrefs()->GetBoolean(prefs::kSigninAllowed); 92 return client_->GetPrefs()->GetBoolean(prefs::kSigninAllowed);
53 } 93 }
54 94
55 const std::string& SigninManagerBase::GetAuthenticatedUsername() const { 95 std::string SigninManagerBase::GetAuthenticatedUsername() const {
56 return authenticated_username_; 96 return account_tracker_service_->GetAccountInfo(
Mike Lerman 2015/04/08 14:45:27 Hmm. Should this method be deleted, and users of t
Roger Tawa OOO till Jul 10th 2015/04/08 20:24:21 It could, but there is just so much code that assu
97 GetAuthenticatedAccountId()).email;
57 } 98 }
58 99
59 const std::string& SigninManagerBase::GetAuthenticatedAccountId() const { 100 const std::string& SigninManagerBase::GetAuthenticatedAccountId() const {
60 return authenticated_account_id_; 101 return authenticated_account_id_;
61 } 102 }
62 103
63 void SigninManagerBase::SetAuthenticatedUsername(const std::string& username) { 104 void SigninManagerBase::SetAuthenticatedAccountInfo(const std::string& gaia_id,
64 DCHECK(!username.empty()); 105 const std::string& email) {
65 if (!authenticated_username_.empty()) { 106 std::string account_id =
Mike Lerman 2015/04/08 14:45:27 Can this be a const std::string&?
Roger Tawa OOO till Jul 10th 2015/04/08 20:24:21 SeedAccountInfo returns an AccountInfo by value.
66 DLOG_IF(ERROR, !gaia::AreEmailsSame(username, authenticated_username_)) 107 account_tracker_service_->SeedAccountInfo(gaia_id, email);
67 << "Tried to change the authenticated username to something different: " 108 SetAuthenticatedAccountId(account_id);
68 << "Current: " << authenticated_username_ << ", New: " << username; 109 }
69 110
70 #if defined(OS_IOS) 111 void SigninManagerBase::SetAuthenticatedAccountId(
71 // Prior to M26, chrome on iOS did not normalize the email before setting 112 const std::string& account_id) {
72 // it in SigninManager. If the emails are the same as given by 113 DCHECK(!account_id.empty());
73 // gaia::AreEmailsSame() but not the same as given by std::string::op==(), 114 if (!authenticated_account_id_.empty()) {
74 // make sure to set the authenticated name below. 115 DLOG_IF(ERROR, account_id != authenticated_account_id_)
75 if (!gaia::AreEmailsSame(username, authenticated_username_) || 116 << "Tried to change the authenticated id to something different: "
76 username == authenticated_username_) { 117 << "Current: " << authenticated_account_id_ << ", New: " << account_id;
77 return;
78 }
79 #else
80 return; 118 return;
81 #endif
82 } 119 }
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 120
89 // TODO(rogerta): remove this DCHECK when migration work is started. 121 std::string pref_account_id =
90 DCHECK_EQ(AccountTrackerService::MIGRATION_NOT_STARTED, 122 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 123
98 // Go ahead and update the last signed in username here as well. Once a 124 DCHECK(pref_account_id.empty() || pref_account_id == account_id)
125 << "account_id=" << account_id
126 << " pref_account_id=" << pref_account_id;
127 authenticated_account_id_ = account_id;
128 client_->GetPrefs()->SetString(prefs::kGoogleServicesAccountId, account_id);
129
130 // This preference is set so that code on I/O thread has access to the
131 // Gaia id of the signed in user.
132 AccountTrackerService::AccountInfo info =
133 account_tracker_service_->GetAccountInfo(account_id);
134 DCHECK(!info.gaia.empty());
135 client_->GetPrefs()->SetString(prefs::kGoogleServicesUserAccountId,
136 info.gaia);
137
138 // 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 139 // 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. 140 // opposed to on signin allows us to catch the upgrade scenario.
101 client_->GetPrefs()->SetString(prefs::kGoogleServicesLastUsername, username); 141 client_->GetPrefs()->SetString(prefs::kGoogleServicesLastAccountId,
Mike Lerman 2015/04/08 14:45:27 Umm -you never seem to read the kGoogleServicesLas
Roger Tawa OOO till Jul 10th 2015/04/08 20:24:21 kGoogleServicesLastUsername was used to know the i
Mike Lerman 2015/04/08 20:49:28 Since we'll be keeping kGoogleServicesLastUsername
Roger Tawa OOO till Jul 10th 2015/04/09 15:59:47 Done.
102 } 142 account_id);
103 143 client_->GetPrefs()->SetString(prefs::kGoogleServicesLastUsername,
Mike Lerman 2015/04/08 14:45:27 You never seem to read the kGoogleServicesLastUser
Roger Tawa OOO till Jul 10th 2015/04/08 20:24:21 Used in inline_login_handler_impl.cc:405
104 void SigninManagerBase::ClearAuthenticatedUsername() { 144 info.email);
105 authenticated_username_.clear();
106 authenticated_account_id_.clear();
107 } 145 }
108 146
109 bool SigninManagerBase::IsAuthenticated() const { 147 bool SigninManagerBase::IsAuthenticated() const {
110 return !authenticated_account_id_.empty(); 148 return !authenticated_account_id_.empty();
111 } 149 }
112 150
151 void SigninManagerBase::clear_authenticated_user() {
152 authenticated_account_id_.clear();
Mike Lerman 2015/04/08 14:45:27 Should this also clear the kGoogleServicesAccountI
Roger Tawa OOO till Jul 10th 2015/04/08 20:24:21 I don't think so.
Mike Lerman 2015/04/08 20:49:28 Then should the implementation of the hacker-style
Roger Tawa OOO till Jul 10th 2015/04/09 15:59:47 Done.
153 }
154
113 bool SigninManagerBase::AuthInProgress() const { 155 bool SigninManagerBase::AuthInProgress() const {
114 // SigninManagerBase never kicks off auth processes itself. 156 // SigninManagerBase never kicks off auth processes itself.
115 return false; 157 return false;
116 } 158 }
117 159
118 void SigninManagerBase::Shutdown() {} 160 void SigninManagerBase::Shutdown() {}
119 161
120 void SigninManagerBase::AddObserver(Observer* observer) { 162 void SigninManagerBase::AddObserver(Observer* observer) {
121 observer_list_.AddObserver(observer); 163 observer_list_.AddObserver(observer);
122 } 164 }
(...skipping 12 matching lines...) Expand all
135 signin_diagnostics_observers_.RemoveObserver(observer); 177 signin_diagnostics_observers_.RemoveObserver(observer);
136 } 178 }
137 179
138 void SigninManagerBase::NotifyDiagnosticsObservers( 180 void SigninManagerBase::NotifyDiagnosticsObservers(
139 const TimedSigninStatusField& field, 181 const TimedSigninStatusField& field,
140 const std::string& value) { 182 const std::string& value) {
141 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, 183 FOR_EACH_OBSERVER(SigninDiagnosticsObserver,
142 signin_diagnostics_observers_, 184 signin_diagnostics_observers_,
143 NotifySigninValueChanged(field, value)); 185 NotifySigninValueChanged(field, value));
144 } 186 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698