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

Side by Side Diff: google_apis/gaia/account_tracker.cc

Issue 336253002: Add IdentityProvider-based AccountTracker to google_apis (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address code review comments Created 6 years, 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/extensions/api/identity/account_tracker.h" 5 #include "google_apis/gaia/account_tracker.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "chrome/browser/browser_process.h" 9 #include "net/url_request/url_request_context_getter.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13 #include "chrome/browser/signin/signin_manager_factory.h"
14 #include "components/signin/core/browser/profile_oauth2_token_service.h"
15 #include "components/signin/core/browser/signin_manager.h"
16 #include "content/public/browser/notification_details.h"
17 #include "extensions/browser/extension_system.h"
18 10
19 namespace extensions { 11 namespace gaia {
20 12
21 AccountTracker::AccountTracker(Profile* profile) : profile_(profile) { 13 AccountTracker::AccountTracker(
22 ProfileOAuth2TokenService* service = 14 IdentityProvider* identity_provider,
23 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); 15 net::URLRequestContextGetter* request_context_getter)
24 service->AddObserver(this); 16 : identity_provider_(identity_provider),
25 service->signin_error_controller()->AddProvider(this); 17 request_context_getter_(request_context_getter),
26 SigninManagerFactory::GetForProfile(profile_)->AddObserver(this); 18 shutdown_called_(false) {
19 identity_provider_->AddObserver(this);
20 identity_provider_->GetTokenService()->AddObserver(this);
27 } 21 }
28 22
29 AccountTracker::~AccountTracker() {} 23 AccountTracker::~AccountTracker() {
30 24 DCHECK(shutdown_called_);
31 void AccountTracker::ReportAuthError(const std::string& account_id,
32 const GoogleServiceAuthError& error) {
33 account_errors_.insert(make_pair(account_id, error));
34 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->
35 signin_error_controller()->AuthStatusChanged();
36 UpdateSignInState(account_id, false);
37 } 25 }
38 26
39 void AccountTracker::Shutdown() { 27 void AccountTracker::Shutdown() {
28 shutdown_called_ = true;
40 STLDeleteValues(&user_info_requests_); 29 STLDeleteValues(&user_info_requests_);
41 SigninManagerFactory::GetForProfile(profile_)->RemoveObserver(this); 30 identity_provider_->GetTokenService()->RemoveObserver(this);
42 ProfileOAuth2TokenService* service = 31 identity_provider_->RemoveObserver(this);
43 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
44 service->signin_error_controller()->RemoveProvider(this);
45 service->RemoveObserver(this);
46 } 32 }
47 33
48 void AccountTracker::AddObserver(Observer* observer) { 34 void AccountTracker::AddObserver(Observer* observer) {
49 observer_list_.AddObserver(observer); 35 observer_list_.AddObserver(observer);
50 } 36 }
51 37
52 void AccountTracker::RemoveObserver(Observer* observer) { 38 void AccountTracker::RemoveObserver(Observer* observer) {
53 observer_list_.RemoveObserver(observer); 39 observer_list_.RemoveObserver(observer);
54 } 40 }
55 41
56 std::vector<AccountIds> AccountTracker::GetAccounts() const { 42 std::vector<AccountIds> AccountTracker::GetAccounts() const {
57 const std::string primary_account_id = signin_manager_account_id(); 43 const std::string active_account_id =
44 identity_provider_->GetActiveAccountId();
58 std::vector<AccountIds> accounts; 45 std::vector<AccountIds> accounts;
59 46
60 for (std::map<std::string, AccountState>::const_iterator it = 47 for (std::map<std::string, AccountState>::const_iterator it =
61 accounts_.begin(); 48 accounts_.begin();
62 it != accounts_.end(); 49 it != accounts_.end();
63 ++it) { 50 ++it) {
64 const AccountState& state = it->second; 51 const AccountState& state = it->second;
65 bool is_visible = state.is_signed_in && !state.ids.gaia.empty(); 52 bool is_visible = state.is_signed_in && !state.ids.gaia.empty();
66 53
67 if (it->first == primary_account_id) { 54 if (it->first == active_account_id) {
68 if (is_visible) 55 if (is_visible)
69 accounts.insert(accounts.begin(), state.ids); 56 accounts.insert(accounts.begin(), state.ids);
70 else 57 else
71 return std::vector<AccountIds>(); 58 return std::vector<AccountIds>();
72 59
73 } else if (is_visible) { 60 } else if (is_visible) {
74 accounts.push_back(state.ids); 61 accounts.push_back(state.ids);
75 } 62 }
76 } 63 }
77 return accounts; 64 return accounts;
78 } 65 }
79 66
80 std::string AccountTracker::FindAccountKeyByGaiaId(const std::string& gaia_id) { 67 AccountIds AccountTracker::FindAccountIdsByGaiaId(const std::string& gaia_id) {
81 for (std::map<std::string, AccountState>::const_iterator it = 68 for (std::map<std::string, AccountState>::const_iterator it =
82 accounts_.begin(); 69 accounts_.begin();
83 it != accounts_.end(); 70 it != accounts_.end();
84 ++it) { 71 ++it) {
85 const AccountState& state = it->second; 72 const AccountState& state = it->second;
86 if (state.ids.gaia == gaia_id) { 73 if (state.ids.gaia == gaia_id) {
87 return state.ids.account_key; 74 return state.ids;
88 } 75 }
89 } 76 }
90 77
91 return std::string(); 78 return AccountIds();
92 } 79 }
93 80
94 void AccountTracker::OnRefreshTokenAvailable(const std::string& account_id) { 81 void AccountTracker::OnRefreshTokenAvailable(const std::string& account_id) {
95 // Ignore refresh tokens if there is no primary account ID at all. 82 // Ignore refresh tokens if there is no active account ID at all.
96 if (signin_manager_account_id().empty()) 83 if (identity_provider_->GetActiveAccountId().empty())
97 return; 84 return;
98 85
99 DVLOG(1) << "AVAILABLE " << account_id; 86 DVLOG(1) << "AVAILABLE " << account_id;
100 ClearAuthError(account_id);
101 UpdateSignInState(account_id, true); 87 UpdateSignInState(account_id, true);
102 } 88 }
103 89
104 void AccountTracker::OnRefreshTokenRevoked(const std::string& account_id) { 90 void AccountTracker::OnRefreshTokenRevoked(const std::string& account_id) {
105 DVLOG(1) << "REVOKED " << account_id; 91 DVLOG(1) << "REVOKED " << account_id;
106 UpdateSignInState(account_id, false); 92 UpdateSignInState(account_id, false);
107 } 93 }
108 94
109 void AccountTracker::GoogleSigninSucceeded(const std::string& username, 95 void AccountTracker::OnActiveAccountLogin() {
110 const std::string& password) {
111 std::vector<std::string> accounts = 96 std::vector<std::string> accounts =
112 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->GetAccounts(); 97 identity_provider_->GetTokenService()->GetAccounts();
98
99 DVLOG(1) << "LOGIN " << accounts.size() << " accounts available.";
113 100
114 for (std::vector<std::string>::const_iterator it = accounts.begin(); 101 for (std::vector<std::string>::const_iterator it = accounts.begin();
115 it != accounts.end(); 102 it != accounts.end();
116 ++it) { 103 ++it) {
117 OnRefreshTokenAvailable(*it); 104 OnRefreshTokenAvailable(*it);
118 } 105 }
119 } 106 }
120 107
121 void AccountTracker::GoogleSignedOut(const std::string& username) { 108 void AccountTracker::OnActiveAccountLogout() {
122 if (username == signin_manager_account_id() || 109 DVLOG(1) << "LOGOUT";
123 signin_manager_account_id().empty()) { 110 StopTrackingAllAccounts();
124 StopTrackingAllAccounts();
125 } else {
126 StopTrackingAccount(username);
127 }
128 } 111 }
129 112
130 void AccountTracker::SetAccountStateForTest(AccountIds ids, bool is_signed_in) { 113 void AccountTracker::SetAccountStateForTest(AccountIds ids, bool is_signed_in) {
131 accounts_[ids.account_key].ids = ids; 114 accounts_[ids.account_key].ids = ids;
132 accounts_[ids.account_key].is_signed_in = is_signed_in; 115 accounts_[ids.account_key].is_signed_in = is_signed_in;
133 116
134 DVLOG(1) << "SetAccountStateForTest " << ids.account_key << ":" 117 DVLOG(1) << "SetAccountStateForTest " << ids.account_key << ":"
135 << is_signed_in; 118 << is_signed_in;
136 119
137 if (VLOG_IS_ON(1)) { 120 if (VLOG_IS_ON(1)) {
138 for (std::map<std::string, AccountState>::const_iterator it = 121 for (std::map<std::string, AccountState>::const_iterator it =
139 accounts_.begin(); 122 accounts_.begin();
140 it != accounts_.end(); 123 it != accounts_.end();
141 ++it) { 124 ++it) {
142 DVLOG(1) << it->first << ":" << it->second.is_signed_in; 125 DVLOG(1) << it->first << ":" << it->second.is_signed_in;
143 } 126 }
144 } 127 }
145 } 128 }
146 129
147 const std::string AccountTracker::signin_manager_account_id() const {
148 return SigninManagerFactory::GetForProfile(profile_)
149 ->GetAuthenticatedAccountId();
150 }
151
152 void AccountTracker::NotifyAccountAdded(const AccountState& account) { 130 void AccountTracker::NotifyAccountAdded(const AccountState& account) {
153 DCHECK(!account.ids.gaia.empty()); 131 DCHECK(!account.ids.gaia.empty());
154 FOR_EACH_OBSERVER( 132 FOR_EACH_OBSERVER(
155 Observer, observer_list_, OnAccountAdded(account.ids)); 133 Observer, observer_list_, OnAccountAdded(account.ids));
156 } 134 }
157 135
158 void AccountTracker::NotifyAccountRemoved(const AccountState& account) { 136 void AccountTracker::NotifyAccountRemoved(const AccountState& account) {
159 DCHECK(!account.ids.gaia.empty()); 137 DCHECK(!account.ids.gaia.empty());
160 FOR_EACH_OBSERVER( 138 FOR_EACH_OBSERVER(
161 Observer, observer_list_, OnAccountRemoved(account.ids)); 139 Observer, observer_list_, OnAccountRemoved(account.ids));
162 } 140 }
163 141
164 void AccountTracker::NotifySignInChanged(const AccountState& account) { 142 void AccountTracker::NotifySignInChanged(const AccountState& account) {
165 DCHECK(!account.ids.gaia.empty()); 143 DCHECK(!account.ids.gaia.empty());
166 FOR_EACH_OBSERVER(Observer, 144 FOR_EACH_OBSERVER(Observer,
167 observer_list_, 145 observer_list_,
168 OnAccountSignInChanged(account.ids, account.is_signed_in)); 146 OnAccountSignInChanged(account.ids, account.is_signed_in));
169 } 147 }
170 148
171 void AccountTracker::ClearAuthError(const std::string& account_key) { 149 void AccountTracker::UpdateSignInState(const std::string account_key,
172 account_errors_.erase(account_key);
173 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->
174 signin_error_controller()->AuthStatusChanged();
175 }
176
177 void AccountTracker::UpdateSignInState(const std::string& account_key,
178 bool is_signed_in) { 150 bool is_signed_in) {
179 StartTrackingAccount(account_key); 151 StartTrackingAccount(account_key);
180 AccountState& account = accounts_[account_key]; 152 AccountState& account = accounts_[account_key];
181 bool needs_gaia_id = account.ids.gaia.empty(); 153 bool needs_gaia_id = account.ids.gaia.empty();
182 bool was_signed_in = account.is_signed_in; 154 bool was_signed_in = account.is_signed_in;
183 account.is_signed_in = is_signed_in; 155 account.is_signed_in = is_signed_in;
184 156
185 if (needs_gaia_id && is_signed_in) 157 if (needs_gaia_id && is_signed_in)
186 StartFetchingUserInfo(account_key); 158 StartFetchingUserInfo(account_key);
187 159
188 if (!needs_gaia_id && (was_signed_in != is_signed_in)) 160 if (!needs_gaia_id && (was_signed_in != is_signed_in))
189 NotifySignInChanged(account); 161 NotifySignInChanged(account);
190 } 162 }
191 163
192 void AccountTracker::StartTrackingAccount(const std::string& account_key) { 164 void AccountTracker::StartTrackingAccount(const std::string account_key) {
193 if (!ContainsKey(accounts_, account_key)) { 165 if (!ContainsKey(accounts_, account_key)) {
194 DVLOG(1) << "StartTracking " << account_key; 166 DVLOG(1) << "StartTracking " << account_key;
195 AccountState account_state; 167 AccountState account_state;
196 account_state.ids.account_key = account_key; 168 account_state.ids.account_key = account_key;
197 account_state.ids.email = account_key; 169 account_state.ids.email = account_key;
198 account_state.is_signed_in = false; 170 account_state.is_signed_in = false;
199 accounts_.insert(make_pair(account_key, account_state)); 171 accounts_.insert(make_pair(account_key, account_state));
200 } 172 }
201 } 173 }
202 174
203 void AccountTracker::StopTrackingAccount(const std::string& account_key) { 175 void AccountTracker::StopTrackingAccount(const std::string account_key) {
176 DVLOG(1) << "StopTracking " << account_key;
204 if (ContainsKey(accounts_, account_key)) { 177 if (ContainsKey(accounts_, account_key)) {
205 AccountState& account = accounts_[account_key]; 178 AccountState& account = accounts_[account_key];
206 if (!account.ids.gaia.empty()) { 179 if (!account.ids.gaia.empty()) {
207 UpdateSignInState(account_key, false); 180 UpdateSignInState(account_key, false);
208 NotifyAccountRemoved(account); 181 NotifyAccountRemoved(account);
209 } 182 }
210 accounts_.erase(account_key); 183 accounts_.erase(account_key);
211 } 184 }
212 185
213 ClearAuthError(account_key);
214
215 if (ContainsKey(user_info_requests_, account_key)) 186 if (ContainsKey(user_info_requests_, account_key))
216 DeleteFetcher(user_info_requests_[account_key]); 187 DeleteFetcher(user_info_requests_[account_key]);
217 } 188 }
218 189
219 void AccountTracker::StopTrackingAllAccounts() { 190 void AccountTracker::StopTrackingAllAccounts() {
220 while (!accounts_.empty()) 191 while (!accounts_.empty())
221 StopTrackingAccount(accounts_.begin()->first); 192 StopTrackingAccount(accounts_.begin()->first);
222 } 193 }
223 194
224 void AccountTracker::StartFetchingUserInfo(const std::string& account_key) { 195 void AccountTracker::StartFetchingUserInfo(const std::string account_key) {
225 if (ContainsKey(user_info_requests_, account_key)) 196 if (ContainsKey(user_info_requests_, account_key))
226 DeleteFetcher(user_info_requests_[account_key]); 197 DeleteFetcher(user_info_requests_[account_key]);
227 198
228 DVLOG(1) << "StartFetching " << account_key; 199 DVLOG(1) << "StartFetching " << account_key;
229 AccountIdFetcher* fetcher = 200 AccountIdFetcher* fetcher =
230 new AccountIdFetcher(profile_, this, account_key); 201 new AccountIdFetcher(identity_provider_->GetTokenService(),
202 request_context_getter_.get(),
203 this,
204 account_key);
231 user_info_requests_[account_key] = fetcher; 205 user_info_requests_[account_key] = fetcher;
232 fetcher->Start(); 206 fetcher->Start();
233 } 207 }
234 208
235 void AccountTracker::OnUserInfoFetchSuccess(AccountIdFetcher* fetcher, 209 void AccountTracker::OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
236 const std::string& gaia_id) { 210 const std::string& gaia_id) {
237 const std::string& account_key = fetcher->account_key(); 211 const std::string& account_key = fetcher->account_key();
238 DCHECK(ContainsKey(accounts_, account_key)); 212 DCHECK(ContainsKey(accounts_, account_key));
239 AccountState& account = accounts_[account_key]; 213 AccountState& account = accounts_[account_key];
240 214
241 account.ids.gaia = gaia_id; 215 account.ids.gaia = gaia_id;
242 NotifyAccountAdded(account); 216 NotifyAccountAdded(account);
243 217
244 if (account.is_signed_in) 218 if (account.is_signed_in)
245 NotifySignInChanged(account); 219 NotifySignInChanged(account);
246 220
247 DeleteFetcher(fetcher); 221 DeleteFetcher(fetcher);
248 } 222 }
249 223
250 void AccountTracker::OnUserInfoFetchFailure(AccountIdFetcher* fetcher) { 224 void AccountTracker::OnUserInfoFetchFailure(AccountIdFetcher* fetcher) {
251 LOG(WARNING) << "Failed to get UserInfo for " << fetcher->account_key(); 225 LOG(WARNING) << "Failed to get UserInfo for " << fetcher->account_key();
252 std::string key = fetcher->account_key(); 226 std::string key = fetcher->account_key();
253 DeleteFetcher(fetcher); 227 DeleteFetcher(fetcher);
254 StopTrackingAccount(key); 228 StopTrackingAccount(key);
255 } 229 }
256 230
257 std::string AccountTracker::GetAccountId() const {
258 if (account_errors_.size() == 0)
259 return std::string();
260 else
261 return account_errors_.begin()->first;
262 }
263
264 std::string AccountTracker::GetUsername() const {
265 std::string id = GetAccountId();
266 if (!id.empty()) {
267 std::map<std::string, AccountState>::const_iterator it =
268 accounts_.find(id);
269 if (it != accounts_.end())
270 return it->second.ids.email;
271 }
272 return std::string();
273 }
274
275 GoogleServiceAuthError AccountTracker::GetAuthStatus() const {
276 if (account_errors_.size() == 0)
277 return GoogleServiceAuthError::AuthErrorNone();
278 else
279 return account_errors_.begin()->second;
280 }
281
282 void AccountTracker::DeleteFetcher(AccountIdFetcher* fetcher) { 231 void AccountTracker::DeleteFetcher(AccountIdFetcher* fetcher) {
232 DVLOG(1) << "DeleteFetcher " << fetcher->account_key();
283 const std::string& account_key = fetcher->account_key(); 233 const std::string& account_key = fetcher->account_key();
284 DCHECK(ContainsKey(user_info_requests_, account_key)); 234 DCHECK(ContainsKey(user_info_requests_, account_key));
285 DCHECK_EQ(fetcher, user_info_requests_[account_key]); 235 DCHECK_EQ(fetcher, user_info_requests_[account_key]);
286 user_info_requests_.erase(account_key); 236 user_info_requests_.erase(account_key);
287 delete fetcher; 237 delete fetcher;
288 } 238 }
289 239
290 AccountIdFetcher::AccountIdFetcher(Profile* profile, 240 AccountIdFetcher::AccountIdFetcher(
291 AccountTracker* tracker, 241 OAuth2TokenService* token_service,
292 const std::string& account_key) 242 net::URLRequestContextGetter* request_context_getter,
293 : OAuth2TokenService::Consumer("extensions_account_tracker"), 243 AccountTracker* tracker,
294 profile_(profile), 244 const std::string& account_key)
245 : OAuth2TokenService::Consumer("gaia_account_tracker"),
246 token_service_(token_service),
247 request_context_getter_(request_context_getter),
295 tracker_(tracker), 248 tracker_(tracker),
296 account_key_(account_key) {} 249 account_key_(account_key) {
250 }
297 251
298 AccountIdFetcher::~AccountIdFetcher() {} 252 AccountIdFetcher::~AccountIdFetcher() {}
299 253
300 void AccountIdFetcher::Start() { 254 void AccountIdFetcher::Start() {
301 ProfileOAuth2TokenService* service = 255 login_token_request_ = token_service_->StartRequest(
302 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
303 login_token_request_ = service->StartRequest(
304 account_key_, OAuth2TokenService::ScopeSet(), this); 256 account_key_, OAuth2TokenService::ScopeSet(), this);
305 } 257 }
306 258
307 void AccountIdFetcher::OnGetTokenSuccess( 259 void AccountIdFetcher::OnGetTokenSuccess(
308 const OAuth2TokenService::Request* request, 260 const OAuth2TokenService::Request* request,
309 const std::string& access_token, 261 const std::string& access_token,
310 const base::Time& expiration_time) { 262 const base::Time& expiration_time) {
311 DCHECK_EQ(request, login_token_request_.get()); 263 DCHECK_EQ(request, login_token_request_.get());
312 264
313 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient( 265 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(request_context_getter_));
314 g_browser_process->system_request_context()));
315 266
316 const int kMaxGetUserIdRetries = 3; 267 const int kMaxGetUserIdRetries = 3;
317 gaia_oauth_client_->GetUserId(access_token, kMaxGetUserIdRetries, this); 268 gaia_oauth_client_->GetUserId(access_token, kMaxGetUserIdRetries, this);
318 } 269 }
319 270
320 void AccountIdFetcher::OnGetTokenFailure( 271 void AccountIdFetcher::OnGetTokenFailure(
321 const OAuth2TokenService::Request* request, 272 const OAuth2TokenService::Request* request,
322 const GoogleServiceAuthError& error) { 273 const GoogleServiceAuthError& error) {
323 LOG(ERROR) << "OnGetTokenFailure: " << error.ToString(); 274 LOG(ERROR) << "OnGetTokenFailure: " << error.ToString();
324 DCHECK_EQ(request, login_token_request_.get()); 275 DCHECK_EQ(request, login_token_request_.get());
325 tracker_->OnUserInfoFetchFailure(this); 276 tracker_->OnUserInfoFetchFailure(this);
326 } 277 }
327 278
328 void AccountIdFetcher::OnGetUserIdResponse(const std::string& gaia_id) { 279 void AccountIdFetcher::OnGetUserIdResponse(const std::string& gaia_id) {
329 tracker_->OnUserInfoFetchSuccess(this, gaia_id); 280 tracker_->OnUserInfoFetchSuccess(this, gaia_id);
330 } 281 }
331 282
332 void AccountIdFetcher::OnOAuthError() { 283 void AccountIdFetcher::OnOAuthError() {
333 LOG(ERROR) << "OnOAuthError"; 284 LOG(ERROR) << "OnOAuthError";
334 tracker_->OnUserInfoFetchFailure(this); 285 tracker_->OnUserInfoFetchFailure(this);
335 } 286 }
336 287
337 void AccountIdFetcher::OnNetworkError(int response_code) { 288 void AccountIdFetcher::OnNetworkError(int response_code) {
338 LOG(ERROR) << "OnNetworkError " << response_code; 289 LOG(ERROR) << "OnNetworkError " << response_code;
339 tracker_->OnUserInfoFetchFailure(this); 290 tracker_->OnUserInfoFetchFailure(this);
340 } 291 }
341 292
342 } // namespace extensions 293 } // namespace gaia
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698