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

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: 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 scoped_ptr<IdentityProvider> identity_provider,
23 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); 15 net::URLRequestContextGetter* request_context_getter)
24 service->AddObserver(this); 16 : identity_provider_(identity_provider.Pass()),
25 service->signin_error_controller()->AddProvider(this); 17 request_context_getter_(request_context_getter) {
26 SigninManagerFactory::GetForProfile(profile_)->AddObserver(this); 18 identity_provider_->AddObserver(this);
19 identity_provider_->GetTokenService()->AddObserver(this);
27 } 20 }
28 21
29 AccountTracker::~AccountTracker() {} 22 AccountTracker::~AccountTracker() {}
30 23
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 }
38
39 void AccountTracker::Shutdown() { 24 void AccountTracker::Shutdown() {
40 STLDeleteValues(&user_info_requests_); 25 STLDeleteValues(&user_info_requests_);
fgorski 2014/06/17 04:14:00 Is there a chance Shutdown does not get call befor
Roger Tawa OOO till Jul 10th 2014/06/17 15:28:20 I'd eventually like this make this class a |KeyedS
Michael Courage 2014/06/18 23:13:15 Done.
41 SigninManagerFactory::GetForProfile(profile_)->RemoveObserver(this); 26 identity_provider_->GetTokenService()->RemoveObserver(this);
42 ProfileOAuth2TokenService* service = 27 identity_provider_->RemoveObserver(this);
43 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
44 service->signin_error_controller()->RemoveProvider(this);
45 service->RemoveObserver(this);
46 } 28 }
47 29
48 void AccountTracker::AddObserver(Observer* observer) { 30 void AccountTracker::AddObserver(Observer* observer) {
49 observer_list_.AddObserver(observer); 31 observer_list_.AddObserver(observer);
50 } 32 }
51 33
52 void AccountTracker::RemoveObserver(Observer* observer) { 34 void AccountTracker::RemoveObserver(Observer* observer) {
53 observer_list_.RemoveObserver(observer); 35 observer_list_.RemoveObserver(observer);
54 } 36 }
55 37
56 std::vector<AccountIds> AccountTracker::GetAccounts() const { 38 std::vector<AccountIds> AccountTracker::GetAccounts() const {
57 const std::string primary_account_id = signin_manager_account_id(); 39 const std::string primary_account_id = active_account_id();
Roger Tawa OOO till Jul 10th 2014/06/17 15:28:20 Rename local var to |active_account_id| ?
Michael Courage 2014/06/18 23:13:14 Done. (note: giving method and var the same name d
58 std::vector<AccountIds> accounts; 40 std::vector<AccountIds> accounts;
59 41
60 for (std::map<std::string, AccountState>::const_iterator it = 42 for (std::map<std::string, AccountState>::const_iterator it =
61 accounts_.begin(); 43 accounts_.begin();
62 it != accounts_.end(); 44 it != accounts_.end();
63 ++it) { 45 ++it) {
64 const AccountState& state = it->second; 46 const AccountState& state = it->second;
65 bool is_visible = state.is_signed_in && !state.ids.gaia.empty(); 47 bool is_visible = state.is_signed_in && !state.ids.gaia.empty();
66 48
67 if (it->first == primary_account_id) { 49 if (it->first == primary_account_id) {
(...skipping 17 matching lines...) Expand all
85 const AccountState& state = it->second; 67 const AccountState& state = it->second;
86 if (state.ids.gaia == gaia_id) { 68 if (state.ids.gaia == gaia_id) {
87 return state.ids.account_key; 69 return state.ids.account_key;
88 } 70 }
89 } 71 }
90 72
91 return std::string(); 73 return std::string();
92 } 74 }
93 75
94 void AccountTracker::OnRefreshTokenAvailable(const std::string& account_id) { 76 void AccountTracker::OnRefreshTokenAvailable(const std::string& account_id) {
95 // Ignore refresh tokens if there is no primary account ID at all. 77 // Ignore refresh tokens if there is no primary account ID at all.
Roger Tawa OOO till Jul 10th 2014/06/17 15:28:20 Maybe do a global replace of |primary| with |activ
Michael Courage 2014/06/18 23:13:15 Done.
96 if (signin_manager_account_id().empty()) 78 if (active_account_id().empty())
97 return; 79 return;
98 80
99 DVLOG(1) << "AVAILABLE " << account_id; 81 DVLOG(1) << "AVAILABLE " << account_id;
100 ClearAuthError(account_id);
101 UpdateSignInState(account_id, true); 82 UpdateSignInState(account_id, true);
102 } 83 }
103 84
104 void AccountTracker::OnRefreshTokenRevoked(const std::string& account_id) { 85 void AccountTracker::OnRefreshTokenRevoked(const std::string& account_id) {
105 DVLOG(1) << "REVOKED " << account_id; 86 DVLOG(1) << "REVOKED " << account_id;
106 UpdateSignInState(account_id, false); 87 UpdateSignInState(account_id, false);
107 } 88 }
108 89
109 void AccountTracker::GoogleSigninSucceeded(const std::string& username, 90 void AccountTracker::OnActiveAccountLogin() {
110 const std::string& password) {
111 std::vector<std::string> accounts = 91 std::vector<std::string> accounts =
112 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->GetAccounts(); 92 identity_provider_->GetTokenService()->GetAccounts();
93
94 DVLOG(1) << "LOGIN " << accounts.size() << " accounts available.";
113 95
114 for (std::vector<std::string>::const_iterator it = accounts.begin(); 96 for (std::vector<std::string>::const_iterator it = accounts.begin();
115 it != accounts.end(); 97 it != accounts.end();
116 ++it) { 98 ++it) {
117 OnRefreshTokenAvailable(*it); 99 OnRefreshTokenAvailable(*it);
118 } 100 }
119 } 101 }
120 102
121 void AccountTracker::GoogleSignedOut(const std::string& username) { 103 void AccountTracker::OnActiveAccountLogout() {
122 if (username == signin_manager_account_id() || 104 DVLOG(1) << "LOGOUT";
123 signin_manager_account_id().empty()) { 105 StopTrackingAllAccounts();
124 StopTrackingAllAccounts();
125 } else {
126 StopTrackingAccount(username);
127 }
128 } 106 }
129 107
130 void AccountTracker::SetAccountStateForTest(AccountIds ids, bool is_signed_in) { 108 void AccountTracker::SetAccountStateForTest(AccountIds ids, bool is_signed_in) {
131 accounts_[ids.account_key].ids = ids; 109 accounts_[ids.account_key].ids = ids;
132 accounts_[ids.account_key].is_signed_in = is_signed_in; 110 accounts_[ids.account_key].is_signed_in = is_signed_in;
133 111
134 DVLOG(1) << "SetAccountStateForTest " << ids.account_key << ":" 112 DVLOG(1) << "SetAccountStateForTest " << ids.account_key << ":"
135 << is_signed_in; 113 << is_signed_in;
136 114
137 if (VLOG_IS_ON(1)) { 115 if (VLOG_IS_ON(1)) {
138 for (std::map<std::string, AccountState>::const_iterator it = 116 for (std::map<std::string, AccountState>::const_iterator it =
139 accounts_.begin(); 117 accounts_.begin();
140 it != accounts_.end(); 118 it != accounts_.end();
141 ++it) { 119 ++it) {
142 DVLOG(1) << it->first << ":" << it->second.is_signed_in; 120 DVLOG(1) << it->first << ":" << it->second.is_signed_in;
143 } 121 }
144 } 122 }
145 } 123 }
146 124
147 const std::string AccountTracker::signin_manager_account_id() const { 125 const std::string AccountTracker::active_account_id() const {
148 return SigninManagerFactory::GetForProfile(profile_) 126 return identity_provider_->GetActiveAccountId();
149 ->GetAuthenticatedAccountId();
150 } 127 }
151 128
152 void AccountTracker::NotifyAccountAdded(const AccountState& account) { 129 void AccountTracker::NotifyAccountAdded(const AccountState& account) {
153 DCHECK(!account.ids.gaia.empty()); 130 DCHECK(!account.ids.gaia.empty());
154 FOR_EACH_OBSERVER( 131 FOR_EACH_OBSERVER(
155 Observer, observer_list_, OnAccountAdded(account.ids)); 132 Observer, observer_list_, OnAccountAdded(account.ids));
156 } 133 }
157 134
158 void AccountTracker::NotifyAccountRemoved(const AccountState& account) { 135 void AccountTracker::NotifyAccountRemoved(const AccountState& account) {
159 DCHECK(!account.ids.gaia.empty()); 136 DCHECK(!account.ids.gaia.empty());
160 FOR_EACH_OBSERVER( 137 FOR_EACH_OBSERVER(
161 Observer, observer_list_, OnAccountRemoved(account.ids)); 138 Observer, observer_list_, OnAccountRemoved(account.ids));
162 } 139 }
163 140
164 void AccountTracker::NotifySignInChanged(const AccountState& account) { 141 void AccountTracker::NotifySignInChanged(const AccountState& account) {
165 DCHECK(!account.ids.gaia.empty()); 142 DCHECK(!account.ids.gaia.empty());
166 FOR_EACH_OBSERVER(Observer, 143 FOR_EACH_OBSERVER(Observer,
167 observer_list_, 144 observer_list_,
168 OnAccountSignInChanged(account.ids, account.is_signed_in)); 145 OnAccountSignInChanged(account.ids, account.is_signed_in));
169 } 146 }
170 147
171 void AccountTracker::ClearAuthError(const std::string& account_key) { 148 void AccountTracker::UpdateSignInState(const std::string account_key,
Roger Tawa OOO till Jul 10th 2014/06/17 15:28:20 What is the reason to remove the & from here and b
Michael Courage 2014/06/18 23:13:14 StopTrackingAccount had a use-after-free before th
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) { 149 bool is_signed_in) {
179 StartTrackingAccount(account_key); 150 StartTrackingAccount(account_key);
180 AccountState& account = accounts_[account_key]; 151 AccountState& account = accounts_[account_key];
181 bool needs_gaia_id = account.ids.gaia.empty(); 152 bool needs_gaia_id = account.ids.gaia.empty();
182 bool was_signed_in = account.is_signed_in; 153 bool was_signed_in = account.is_signed_in;
183 account.is_signed_in = is_signed_in; 154 account.is_signed_in = is_signed_in;
184 155
185 if (needs_gaia_id && is_signed_in) 156 if (needs_gaia_id && is_signed_in)
186 StartFetchingUserInfo(account_key); 157 StartFetchingUserInfo(account_key);
187 158
188 if (!needs_gaia_id && (was_signed_in != is_signed_in)) 159 if (!needs_gaia_id && (was_signed_in != is_signed_in))
189 NotifySignInChanged(account); 160 NotifySignInChanged(account);
190 } 161 }
191 162
192 void AccountTracker::StartTrackingAccount(const std::string& account_key) { 163 void AccountTracker::StartTrackingAccount(const std::string account_key) {
193 if (!ContainsKey(accounts_, account_key)) { 164 if (!ContainsKey(accounts_, account_key)) {
194 DVLOG(1) << "StartTracking " << account_key; 165 DVLOG(1) << "StartTracking " << account_key;
195 AccountState account_state; 166 AccountState account_state;
196 account_state.ids.account_key = account_key; 167 account_state.ids.account_key = account_key;
197 account_state.ids.email = account_key; 168 account_state.ids.email = account_key;
198 account_state.is_signed_in = false; 169 account_state.is_signed_in = false;
199 accounts_.insert(make_pair(account_key, account_state)); 170 accounts_.insert(make_pair(account_key, account_state));
200 } 171 }
201 } 172 }
202 173
203 void AccountTracker::StopTrackingAccount(const std::string& account_key) { 174 void AccountTracker::StopTrackingAccount(const std::string account_key) {
175 DVLOG(1) << "StopTracking " << account_key;
204 if (ContainsKey(accounts_, account_key)) { 176 if (ContainsKey(accounts_, account_key)) {
205 AccountState& account = accounts_[account_key]; 177 AccountState& account = accounts_[account_key];
206 if (!account.ids.gaia.empty()) { 178 if (!account.ids.gaia.empty()) {
207 UpdateSignInState(account_key, false); 179 UpdateSignInState(account_key, false);
208 NotifyAccountRemoved(account); 180 NotifyAccountRemoved(account);
209 } 181 }
210 accounts_.erase(account_key); 182 accounts_.erase(account_key);
211 } 183 }
212 184
213 ClearAuthError(account_key);
214
215 if (ContainsKey(user_info_requests_, account_key)) 185 if (ContainsKey(user_info_requests_, account_key))
216 DeleteFetcher(user_info_requests_[account_key]); 186 DeleteFetcher(user_info_requests_[account_key]);
217 } 187 }
218 188
219 void AccountTracker::StopTrackingAllAccounts() { 189 void AccountTracker::StopTrackingAllAccounts() {
220 while (!accounts_.empty()) 190 while (!accounts_.empty())
221 StopTrackingAccount(accounts_.begin()->first); 191 StopTrackingAccount(accounts_.begin()->first);
222 } 192 }
223 193
224 void AccountTracker::StartFetchingUserInfo(const std::string& account_key) { 194 void AccountTracker::StartFetchingUserInfo(const std::string account_key) {
225 if (ContainsKey(user_info_requests_, account_key)) 195 if (ContainsKey(user_info_requests_, account_key))
226 DeleteFetcher(user_info_requests_[account_key]); 196 DeleteFetcher(user_info_requests_[account_key]);
227 197
228 DVLOG(1) << "StartFetching " << account_key; 198 DVLOG(1) << "StartFetching " << account_key;
229 AccountIdFetcher* fetcher = 199 AccountIdFetcher* fetcher =
230 new AccountIdFetcher(profile_, this, account_key); 200 new AccountIdFetcher(identity_provider_->GetTokenService(),
201 request_context_getter_.get(),
202 this,
203 account_key);
231 user_info_requests_[account_key] = fetcher; 204 user_info_requests_[account_key] = fetcher;
232 fetcher->Start(); 205 fetcher->Start();
233 } 206 }
234 207
235 void AccountTracker::OnUserInfoFetchSuccess(AccountIdFetcher* fetcher, 208 void AccountTracker::OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
236 const std::string& gaia_id) { 209 const std::string& gaia_id) {
237 const std::string& account_key = fetcher->account_key(); 210 const std::string& account_key = fetcher->account_key();
238 DCHECK(ContainsKey(accounts_, account_key)); 211 DCHECK(ContainsKey(accounts_, account_key));
239 AccountState& account = accounts_[account_key]; 212 AccountState& account = accounts_[account_key];
240 213
241 account.ids.gaia = gaia_id; 214 account.ids.gaia = gaia_id;
242 NotifyAccountAdded(account); 215 NotifyAccountAdded(account);
243 216
244 if (account.is_signed_in) 217 if (account.is_signed_in)
245 NotifySignInChanged(account); 218 NotifySignInChanged(account);
246 219
247 DeleteFetcher(fetcher); 220 DeleteFetcher(fetcher);
248 } 221 }
249 222
250 void AccountTracker::OnUserInfoFetchFailure(AccountIdFetcher* fetcher) { 223 void AccountTracker::OnUserInfoFetchFailure(AccountIdFetcher* fetcher) {
251 LOG(WARNING) << "Failed to get UserInfo for " << fetcher->account_key(); 224 LOG(WARNING) << "Failed to get UserInfo for " << fetcher->account_key();
252 std::string key = fetcher->account_key(); 225 std::string key = fetcher->account_key();
253 DeleteFetcher(fetcher); 226 DeleteFetcher(fetcher);
254 StopTrackingAccount(key); 227 StopTrackingAccount(key);
255 } 228 }
256 229
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) { 230 void AccountTracker::DeleteFetcher(AccountIdFetcher* fetcher) {
231 DVLOG(1) << "DeleteFetcher " << fetcher->account_key();
283 const std::string& account_key = fetcher->account_key(); 232 const std::string& account_key = fetcher->account_key();
284 DCHECK(ContainsKey(user_info_requests_, account_key)); 233 DCHECK(ContainsKey(user_info_requests_, account_key));
285 DCHECK_EQ(fetcher, user_info_requests_[account_key]); 234 DCHECK_EQ(fetcher, user_info_requests_[account_key]);
286 user_info_requests_.erase(account_key); 235 user_info_requests_.erase(account_key);
287 delete fetcher; 236 delete fetcher;
288 } 237 }
289 238
290 AccountIdFetcher::AccountIdFetcher(Profile* profile, 239 AccountIdFetcher::AccountIdFetcher(
291 AccountTracker* tracker, 240 OAuth2TokenService* token_service,
292 const std::string& account_key) 241 net::URLRequestContextGetter* request_context_getter,
293 : OAuth2TokenService::Consumer("extensions_account_tracker"), 242 AccountTracker* tracker,
294 profile_(profile), 243 const std::string& account_key)
244 : OAuth2TokenService::Consumer("gaia_account_tracker"),
245 token_service_(token_service),
246 request_context_getter_(request_context_getter),
295 tracker_(tracker), 247 tracker_(tracker),
296 account_key_(account_key) {} 248 account_key_(account_key) {
249 }
297 250
298 AccountIdFetcher::~AccountIdFetcher() {} 251 AccountIdFetcher::~AccountIdFetcher() {}
299 252
300 void AccountIdFetcher::Start() { 253 void AccountIdFetcher::Start() {
301 ProfileOAuth2TokenService* service = 254 login_token_request_ = token_service_->StartRequest(
302 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
303 login_token_request_ = service->StartRequest(
304 account_key_, OAuth2TokenService::ScopeSet(), this); 255 account_key_, OAuth2TokenService::ScopeSet(), this);
305 } 256 }
306 257
307 void AccountIdFetcher::OnGetTokenSuccess( 258 void AccountIdFetcher::OnGetTokenSuccess(
308 const OAuth2TokenService::Request* request, 259 const OAuth2TokenService::Request* request,
309 const std::string& access_token, 260 const std::string& access_token,
310 const base::Time& expiration_time) { 261 const base::Time& expiration_time) {
311 DCHECK_EQ(request, login_token_request_.get()); 262 DCHECK_EQ(request, login_token_request_.get());
312 263
313 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient( 264 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(request_context_getter_));
314 g_browser_process->system_request_context()));
315 265
316 const int kMaxGetUserIdRetries = 3; 266 const int kMaxGetUserIdRetries = 3;
317 gaia_oauth_client_->GetUserId(access_token, kMaxGetUserIdRetries, this); 267 gaia_oauth_client_->GetUserId(access_token, kMaxGetUserIdRetries, this);
318 } 268 }
319 269
320 void AccountIdFetcher::OnGetTokenFailure( 270 void AccountIdFetcher::OnGetTokenFailure(
321 const OAuth2TokenService::Request* request, 271 const OAuth2TokenService::Request* request,
322 const GoogleServiceAuthError& error) { 272 const GoogleServiceAuthError& error) {
323 LOG(ERROR) << "OnGetTokenFailure: " << error.ToString(); 273 LOG(ERROR) << "OnGetTokenFailure: " << error.ToString();
324 DCHECK_EQ(request, login_token_request_.get()); 274 DCHECK_EQ(request, login_token_request_.get());
325 tracker_->OnUserInfoFetchFailure(this); 275 tracker_->OnUserInfoFetchFailure(this);
326 } 276 }
327 277
328 void AccountIdFetcher::OnGetUserIdResponse(const std::string& gaia_id) { 278 void AccountIdFetcher::OnGetUserIdResponse(const std::string& gaia_id) {
329 tracker_->OnUserInfoFetchSuccess(this, gaia_id); 279 tracker_->OnUserInfoFetchSuccess(this, gaia_id);
330 } 280 }
331 281
332 void AccountIdFetcher::OnOAuthError() { 282 void AccountIdFetcher::OnOAuthError() {
333 LOG(ERROR) << "OnOAuthError"; 283 LOG(ERROR) << "OnOAuthError";
334 tracker_->OnUserInfoFetchFailure(this); 284 tracker_->OnUserInfoFetchFailure(this);
335 } 285 }
336 286
337 void AccountIdFetcher::OnNetworkError(int response_code) { 287 void AccountIdFetcher::OnNetworkError(int response_code) {
338 LOG(ERROR) << "OnNetworkError " << response_code; 288 LOG(ERROR) << "OnNetworkError " << response_code;
339 tracker_->OnUserInfoFetchFailure(this); 289 tracker_->OnUserInfoFetchFailure(this);
340 } 290 }
341 291
342 } // namespace extensions 292 } // namespace gaia
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698