OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/ios/browser/profile_oauth2_token_service_ios.h" | 5 #include "components/signin/ios/browser/profile_oauth2_token_service_ios_delegat
e.h" |
6 | 6 |
7 #include <Foundation/Foundation.h> | 7 #include <Foundation/Foundation.h> |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 ios::ProfileOAuth2TokenServiceIOSProvider* provider, | 93 ios::ProfileOAuth2TokenServiceIOSProvider* provider, |
94 const std::string account_id) | 94 const std::string account_id) |
95 : OAuth2AccessTokenFetcher(consumer), | 95 : OAuth2AccessTokenFetcher(consumer), |
96 provider_(provider), | 96 provider_(provider), |
97 account_id_(account_id), | 97 account_id_(account_id), |
98 request_was_cancelled_(false), | 98 request_was_cancelled_(false), |
99 weak_factory_(this) { | 99 weak_factory_(this) { |
100 DCHECK(provider_); | 100 DCHECK(provider_); |
101 } | 101 } |
102 | 102 |
103 SSOAccessTokenFetcher::~SSOAccessTokenFetcher() {} | 103 SSOAccessTokenFetcher::~SSOAccessTokenFetcher() { |
| 104 } |
104 | 105 |
105 void SSOAccessTokenFetcher::Start(const std::string& client_id, | 106 void SSOAccessTokenFetcher::Start(const std::string& client_id, |
106 const std::string& client_secret, | 107 const std::string& client_secret, |
107 const std::vector<std::string>& scopes) { | 108 const std::vector<std::string>& scopes) { |
108 std::set<std::string> scopes_set(scopes.begin(), scopes.end()); | 109 std::set<std::string> scopes_set(scopes.begin(), scopes.end()); |
109 provider_->GetAccessToken( | 110 provider_->GetAccessToken( |
110 account_id_, client_id, client_secret, scopes_set, | 111 account_id_, client_id, client_secret, scopes_set, |
111 base::Bind(&SSOAccessTokenFetcher::OnAccessTokenResponse, | 112 base::Bind(&SSOAccessTokenFetcher::OnAccessTokenResponse, |
112 weak_factory_.GetWeakPtr())); | 113 weak_factory_.GetWeakPtr())); |
113 } | 114 } |
114 | 115 |
115 void SSOAccessTokenFetcher::CancelRequest() { request_was_cancelled_ = true; } | 116 void SSOAccessTokenFetcher::CancelRequest() { |
| 117 request_was_cancelled_ = true; |
| 118 } |
116 | 119 |
117 void SSOAccessTokenFetcher::OnAccessTokenResponse(NSString* token, | 120 void SSOAccessTokenFetcher::OnAccessTokenResponse(NSString* token, |
118 NSDate* expiration, | 121 NSDate* expiration, |
119 NSError* error) { | 122 NSError* error) { |
120 if (request_was_cancelled_) { | 123 if (request_was_cancelled_) { |
121 // Ignore the callback if the request was cancelled. | 124 // Ignore the callback if the request was cancelled. |
122 return; | 125 return; |
123 } | 126 } |
124 GoogleServiceAuthError auth_error = | 127 GoogleServiceAuthError auth_error = |
125 GetGoogleServiceAuthErrorFromNSError(provider_, error); | 128 GetGoogleServiceAuthErrorFromNSError(provider_, error); |
126 if (auth_error.state() == GoogleServiceAuthError::NONE) { | 129 if (auth_error.state() == GoogleServiceAuthError::NONE) { |
127 base::Time expiration_date = | 130 base::Time expiration_date = |
128 base::Time::FromDoubleT([expiration timeIntervalSince1970]); | 131 base::Time::FromDoubleT([expiration timeIntervalSince1970]); |
129 FireOnGetTokenSuccess(base::SysNSStringToUTF8(token), expiration_date); | 132 FireOnGetTokenSuccess(base::SysNSStringToUTF8(token), expiration_date); |
130 } else { | 133 } else { |
131 FireOnGetTokenFailure(auth_error); | 134 FireOnGetTokenFailure(auth_error); |
132 } | 135 } |
133 } | 136 } |
134 | 137 |
135 } // namespace | 138 } // namespace |
136 | 139 |
137 ProfileOAuth2TokenServiceIOS::AccountInfo::AccountInfo( | 140 ProfileOAuth2TokenServiceIOSDelegate::AccountInfo::AccountInfo( |
138 SigninErrorController* signin_error_controller, | 141 SigninErrorController* signin_error_controller, |
139 const std::string& account_id) | 142 const std::string& account_id) |
140 : signin_error_controller_(signin_error_controller), | 143 : signin_error_controller_(signin_error_controller), |
141 account_id_(account_id), | 144 account_id_(account_id), |
142 last_auth_error_(GoogleServiceAuthError::NONE), | 145 last_auth_error_(GoogleServiceAuthError::NONE), |
143 marked_for_removal_(false) { | 146 marked_for_removal_(false) { |
144 DCHECK(signin_error_controller_); | 147 DCHECK(signin_error_controller_); |
145 DCHECK(!account_id_.empty()); | 148 DCHECK(!account_id_.empty()); |
146 signin_error_controller_->AddProvider(this); | 149 signin_error_controller_->AddProvider(this); |
147 } | 150 } |
148 | 151 |
149 ProfileOAuth2TokenServiceIOS::AccountInfo::~AccountInfo() { | 152 ProfileOAuth2TokenServiceIOSDelegate::AccountInfo::~AccountInfo() { |
150 signin_error_controller_->RemoveProvider(this); | 153 signin_error_controller_->RemoveProvider(this); |
151 } | 154 } |
152 | 155 |
153 void ProfileOAuth2TokenServiceIOS::AccountInfo::SetLastAuthError( | 156 void ProfileOAuth2TokenServiceIOSDelegate::AccountInfo::SetLastAuthError( |
154 const GoogleServiceAuthError& error) { | 157 const GoogleServiceAuthError& error) { |
155 if (error.state() != last_auth_error_.state()) { | 158 if (error.state() != last_auth_error_.state()) { |
156 last_auth_error_ = error; | 159 last_auth_error_ = error; |
157 signin_error_controller_->AuthStatusChanged(); | 160 signin_error_controller_->AuthStatusChanged(); |
158 } | 161 } |
159 } | 162 } |
160 | 163 |
161 std::string ProfileOAuth2TokenServiceIOS::AccountInfo::GetAccountId() const { | 164 std::string ProfileOAuth2TokenServiceIOSDelegate::AccountInfo::GetAccountId() |
| 165 const { |
162 return account_id_; | 166 return account_id_; |
163 } | 167 } |
164 | 168 |
165 GoogleServiceAuthError | 169 GoogleServiceAuthError |
166 ProfileOAuth2TokenServiceIOS::AccountInfo::GetAuthStatus() const { | 170 ProfileOAuth2TokenServiceIOSDelegate::AccountInfo::GetAuthStatus() const { |
167 return last_auth_error_; | 171 return last_auth_error_; |
168 } | 172 } |
169 | 173 |
170 ProfileOAuth2TokenServiceIOS::ProfileOAuth2TokenServiceIOS() | 174 ProfileOAuth2TokenServiceIOSDelegate::ProfileOAuth2TokenServiceIOSDelegate( |
171 : ProfileOAuth2TokenService() { | 175 SigninClient* client, |
| 176 SigninErrorController* signin_error_controller) |
| 177 : client_(client), signin_error_controller_(signin_error_controller) { |
| 178 DCHECK(thread_checker_.CalledOnValidThread()); |
| 179 DCHECK(client); |
| 180 DCHECK(signin_error_controller); |
| 181 } |
| 182 |
| 183 ProfileOAuth2TokenServiceIOSDelegate::~ProfileOAuth2TokenServiceIOSDelegate() { |
172 DCHECK(thread_checker_.CalledOnValidThread()); | 184 DCHECK(thread_checker_.CalledOnValidThread()); |
173 } | 185 } |
174 | 186 |
175 ProfileOAuth2TokenServiceIOS::~ProfileOAuth2TokenServiceIOS() { | 187 void ProfileOAuth2TokenServiceIOSDelegate::Shutdown() { |
176 DCHECK(thread_checker_.CalledOnValidThread()); | 188 DCHECK(thread_checker_.CalledOnValidThread()); |
177 } | |
178 | |
179 void ProfileOAuth2TokenServiceIOS::Initialize( | |
180 SigninClient* client, SigninErrorController* signin_error_controller) { | |
181 DCHECK(thread_checker_.CalledOnValidThread()); | |
182 ProfileOAuth2TokenService::Initialize(client, signin_error_controller); | |
183 } | |
184 | |
185 void ProfileOAuth2TokenServiceIOS::Shutdown() { | |
186 DCHECK(thread_checker_.CalledOnValidThread()); | |
187 CancelAllRequests(); | |
188 accounts_.clear(); | 189 accounts_.clear(); |
189 ProfileOAuth2TokenService::Shutdown(); | |
190 } | 190 } |
191 | 191 |
192 ios::ProfileOAuth2TokenServiceIOSProvider* | 192 ios::ProfileOAuth2TokenServiceIOSProvider* |
193 ProfileOAuth2TokenServiceIOS::GetProvider() { | 193 ProfileOAuth2TokenServiceIOSDelegate::GetProvider() { |
194 ios::ProfileOAuth2TokenServiceIOSProvider* provider = | 194 ios::ProfileOAuth2TokenServiceIOSProvider* provider = |
195 client()->GetIOSProvider(); | 195 client_->GetIOSProvider(); |
196 DCHECK(provider); | 196 DCHECK(provider); |
197 return provider; | 197 return provider; |
198 } | 198 } |
199 | 199 |
200 void ProfileOAuth2TokenServiceIOS::LoadCredentials( | 200 void ProfileOAuth2TokenServiceIOSDelegate::LoadCredentials( |
201 const std::string& primary_account_id) { | 201 const std::string& primary_account_id) { |
202 DCHECK(thread_checker_.CalledOnValidThread()); | 202 DCHECK(thread_checker_.CalledOnValidThread()); |
203 | 203 |
204 // LoadCredentials() is called iff the user is signed in to Chrome, so the | 204 // LoadCredentials() is called iff the user is signed in to Chrome, so the |
205 // primary account id must not be empty. | 205 // primary account id must not be empty. |
206 DCHECK(!primary_account_id.empty()); | 206 DCHECK(!primary_account_id.empty()); |
207 | 207 |
208 GetProvider()->InitializeSharedAuthentication(); | 208 GetProvider()->InitializeSharedAuthentication(); |
209 ReloadCredentials(primary_account_id); | 209 ReloadCredentials(primary_account_id); |
210 FireRefreshTokensLoaded(); | 210 FireRefreshTokensLoaded(); |
211 } | 211 } |
212 | 212 |
213 void ProfileOAuth2TokenServiceIOS::ReloadCredentials( | 213 void ProfileOAuth2TokenServiceIOSDelegate::ReloadCredentials( |
214 const std::string& primary_account_id) { | 214 const std::string& primary_account_id) { |
215 DCHECK(!primary_account_id.empty()); | 215 DCHECK(!primary_account_id.empty()); |
216 DCHECK(primary_account_id_.empty() || | 216 DCHECK(primary_account_id_.empty() || |
217 primary_account_id_ == primary_account_id); | 217 primary_account_id_ == primary_account_id); |
218 primary_account_id_ = primary_account_id; | 218 primary_account_id_ = primary_account_id; |
219 ReloadCredentials(); | 219 ReloadCredentials(); |
220 } | 220 } |
221 | 221 |
222 void ProfileOAuth2TokenServiceIOS::ReloadCredentials() { | 222 void ProfileOAuth2TokenServiceIOSDelegate::ReloadCredentials() { |
223 DCHECK(thread_checker_.CalledOnValidThread()); | 223 DCHECK(thread_checker_.CalledOnValidThread()); |
224 if (primary_account_id_.empty()) { | 224 if (primary_account_id_.empty()) { |
225 // Avoid loading the credentials if there is no primary account id. | 225 // Avoid loading the credentials if there is no primary account id. |
226 return; | 226 return; |
227 } | 227 } |
228 | 228 |
229 std::vector<std::string> new_accounts(GetProvider()->GetAllAccountIds()); | 229 std::vector<std::string> new_accounts(GetProvider()->GetAllAccountIds()); |
230 if (GetExcludeAllSecondaryAccounts()) { | 230 if (GetExcludeAllSecondaryAccounts()) { |
231 // Only keep the |primary_account_id| in the list of new accounts. | 231 // Only keep the |primary_account_id| in the list of new accounts. |
232 if (std::find(new_accounts.begin(), | 232 if (std::find(new_accounts.begin(), new_accounts.end(), |
233 new_accounts.end(), | |
234 primary_account_id_) != new_accounts.end()) { | 233 primary_account_id_) != new_accounts.end()) { |
235 new_accounts.clear(); | 234 new_accounts.clear(); |
236 new_accounts.push_back(primary_account_id_); | 235 new_accounts.push_back(primary_account_id_); |
237 } | 236 } |
238 } else { | 237 } else { |
239 std::set<std::string> exclude_secondary_accounts = | 238 std::set<std::string> exclude_secondary_accounts = |
240 GetExcludedSecondaryAccounts(); | 239 GetExcludedSecondaryAccounts(); |
241 DCHECK(std::find(exclude_secondary_accounts.begin(), | 240 DCHECK(std::find(exclude_secondary_accounts.begin(), |
242 exclude_secondary_accounts.end(), | 241 exclude_secondary_accounts.end(), |
243 primary_account_id_) == exclude_secondary_accounts.end()); | 242 primary_account_id_) == exclude_secondary_accounts.end()); |
(...skipping 23 matching lines...) Expand all Loading... |
267 RemoveAccount(*i); | 266 RemoveAccount(*i); |
268 } | 267 } |
269 } | 268 } |
270 | 269 |
271 // Load all new_accounts. | 270 // Load all new_accounts. |
272 for (auto i = new_accounts.begin(); i != new_accounts.end(); ++i) { | 271 for (auto i = new_accounts.begin(); i != new_accounts.end(); ++i) { |
273 AddOrUpdateAccount(*i); | 272 AddOrUpdateAccount(*i); |
274 } | 273 } |
275 } | 274 } |
276 | 275 |
277 void ProfileOAuth2TokenServiceIOS::UpdateCredentials( | 276 void ProfileOAuth2TokenServiceIOSDelegate::UpdateCredentials( |
278 const std::string& account_id, | 277 const std::string& account_id, |
279 const std::string& refresh_token) { | 278 const std::string& refresh_token) { |
280 DCHECK(thread_checker_.CalledOnValidThread()); | 279 DCHECK(thread_checker_.CalledOnValidThread()); |
281 NOTREACHED() << "Unexpected call to UpdateCredentials when using shared " | 280 NOTREACHED() << "Unexpected call to UpdateCredentials when using shared " |
282 "authentication."; | 281 "authentication."; |
283 } | 282 } |
284 | 283 |
285 void ProfileOAuth2TokenServiceIOS::RevokeAllCredentials() { | 284 void ProfileOAuth2TokenServiceIOSDelegate::RevokeAllCredentials() { |
286 DCHECK(thread_checker_.CalledOnValidThread()); | 285 DCHECK(thread_checker_.CalledOnValidThread()); |
287 | 286 |
288 ScopedBatchChange batch(this); | 287 ScopedBatchChange batch(this); |
289 AccountInfoMap toRemove = accounts_; | 288 AccountInfoMap toRemove = accounts_; |
290 for (AccountInfoMap::iterator i = toRemove.begin(); i != toRemove.end(); ++i) | 289 for (AccountInfoMap::iterator i = toRemove.begin(); i != toRemove.end(); ++i) |
291 RemoveAccount(i->first); | 290 RemoveAccount(i->first); |
292 | 291 |
293 DCHECK_EQ(0u, accounts_.size()); | 292 DCHECK_EQ(0u, accounts_.size()); |
294 primary_account_id_.clear(); | 293 primary_account_id_.clear(); |
295 ClearExcludedSecondaryAccounts(); | 294 ClearExcludedSecondaryAccounts(); |
296 // |RemoveAccount| should have cancelled all the requests and cleared the | |
297 // cache, account-by-account. This extra-cleaning should do nothing unless | |
298 // something went wrong and some cache values and/or pending requests were not | |
299 // linked to any valid account. | |
300 CancelAllRequests(); | |
301 ClearCache(); | |
302 } | 295 } |
303 | 296 |
304 OAuth2AccessTokenFetcher* | 297 OAuth2AccessTokenFetcher* |
305 ProfileOAuth2TokenServiceIOS::CreateAccessTokenFetcher( | 298 ProfileOAuth2TokenServiceIOSDelegate::CreateAccessTokenFetcher( |
306 const std::string& account_id, | 299 const std::string& account_id, |
307 net::URLRequestContextGetter* getter, | 300 net::URLRequestContextGetter* getter, |
308 OAuth2AccessTokenConsumer* consumer) { | 301 OAuth2AccessTokenConsumer* consumer) { |
309 return new SSOAccessTokenFetcher(consumer, GetProvider(), account_id); | 302 return new SSOAccessTokenFetcher(consumer, GetProvider(), account_id); |
310 } | 303 } |
311 | 304 |
312 void ProfileOAuth2TokenServiceIOS::InvalidateOAuth2Token( | 305 std::vector<std::string> ProfileOAuth2TokenServiceIOSDelegate::GetAccounts() { |
313 const std::string& account_id, | |
314 const std::string& client_id, | |
315 const ScopeSet& scopes, | |
316 const std::string& access_token) { | |
317 DCHECK(thread_checker_.CalledOnValidThread()); | |
318 | |
319 // Call |ProfileOAuth2TokenService::InvalidateOAuth2Token| to clear the | |
320 // cached access token. | |
321 ProfileOAuth2TokenService::InvalidateOAuth2Token(account_id, | |
322 client_id, | |
323 scopes, | |
324 access_token); | |
325 | |
326 // There is no need to inform the authentication library that the access | |
327 // token is invalid as it never caches the token. | |
328 } | |
329 | |
330 std::vector<std::string> ProfileOAuth2TokenServiceIOS::GetAccounts() { | |
331 DCHECK(thread_checker_.CalledOnValidThread()); | 306 DCHECK(thread_checker_.CalledOnValidThread()); |
332 std::vector<std::string> account_ids; | 307 std::vector<std::string> account_ids; |
333 for (auto i = accounts_.begin(); i != accounts_.end(); ++i) | 308 for (auto i = accounts_.begin(); i != accounts_.end(); ++i) |
334 account_ids.push_back(i->first); | 309 account_ids.push_back(i->first); |
335 return account_ids; | 310 return account_ids; |
336 } | 311 } |
337 | 312 |
338 bool ProfileOAuth2TokenServiceIOS::RefreshTokenIsAvailable( | 313 bool ProfileOAuth2TokenServiceIOSDelegate::RefreshTokenIsAvailable( |
339 const std::string& account_id) const { | 314 const std::string& account_id) const { |
340 DCHECK(thread_checker_.CalledOnValidThread()); | 315 DCHECK(thread_checker_.CalledOnValidThread()); |
341 | 316 |
342 AccountInfoMap::const_iterator iter = accounts_.find(account_id); | 317 AccountInfoMap::const_iterator iter = accounts_.find(account_id); |
343 return iter != accounts_.end() && !iter->second->marked_for_removal(); | 318 return iter != accounts_.end() && !iter->second->marked_for_removal(); |
344 } | 319 } |
345 | 320 |
346 void ProfileOAuth2TokenServiceIOS::UpdateAuthError( | 321 void ProfileOAuth2TokenServiceIOSDelegate::UpdateAuthError( |
347 const std::string& account_id, | 322 const std::string& account_id, |
348 const GoogleServiceAuthError& error) { | 323 const GoogleServiceAuthError& error) { |
349 DCHECK(thread_checker_.CalledOnValidThread()); | 324 DCHECK(thread_checker_.CalledOnValidThread()); |
350 | 325 |
351 // Do not report connection errors as these are not actually auth errors. | 326 // Do not report connection errors as these are not actually auth errors. |
352 // We also want to avoid masking a "real" auth error just because we | 327 // We also want to avoid masking a "real" auth error just because we |
353 // subsequently get a transient network error. | 328 // subsequently get a transient network error. |
354 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED || | 329 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED || |
355 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE) { | 330 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE) { |
356 return; | 331 return; |
357 } | 332 } |
358 | 333 |
359 if (accounts_.count(account_id) == 0) { | 334 if (accounts_.count(account_id) == 0) { |
360 NOTREACHED(); | 335 NOTREACHED(); |
361 return; | 336 return; |
362 } | 337 } |
363 accounts_[account_id]->SetLastAuthError(error); | 338 accounts_[account_id]->SetLastAuthError(error); |
364 } | 339 } |
365 | 340 |
366 // Clear the authentication error state and notify all observers that a new | 341 // Clear the authentication error state and notify all observers that a new |
367 // refresh token is available so that they request new access tokens. | 342 // refresh token is available so that they request new access tokens. |
368 void ProfileOAuth2TokenServiceIOS::AddOrUpdateAccount( | 343 void ProfileOAuth2TokenServiceIOSDelegate::AddOrUpdateAccount( |
369 const std::string& account_id) { | 344 const std::string& account_id) { |
370 DCHECK(thread_checker_.CalledOnValidThread()); | 345 DCHECK(thread_checker_.CalledOnValidThread()); |
371 DCHECK(!account_id.empty()); | 346 DCHECK(!account_id.empty()); |
372 | 347 |
373 bool account_present = accounts_.count(account_id) > 0; | 348 bool account_present = accounts_.count(account_id) > 0; |
374 if (account_present && accounts_[account_id]->GetAuthStatus().state() == | 349 if (account_present && |
375 GoogleServiceAuthError::NONE) { | 350 accounts_[account_id]->GetAuthStatus().state() == |
| 351 GoogleServiceAuthError::NONE) { |
376 // No need to update the account if it is already a known account and if | 352 // No need to update the account if it is already a known account and if |
377 // there is no auth error. | 353 // there is no auth error. |
378 return; | 354 return; |
379 } | 355 } |
380 | 356 |
381 if (account_present) { | 357 if (!account_present) { |
382 CancelRequestsForAccount(account_id); | |
383 ClearCacheForAccount(account_id); | |
384 } else { | |
385 accounts_[account_id].reset( | 358 accounts_[account_id].reset( |
386 new AccountInfo(signin_error_controller(), account_id)); | 359 new AccountInfo(signin_error_controller_, account_id)); |
387 } | 360 } |
| 361 |
388 UpdateAuthError(account_id, GoogleServiceAuthError::AuthErrorNone()); | 362 UpdateAuthError(account_id, GoogleServiceAuthError::AuthErrorNone()); |
389 FireRefreshTokenAvailable(account_id); | 363 FireRefreshTokenAvailable(account_id); |
390 } | 364 } |
391 | 365 |
392 void ProfileOAuth2TokenServiceIOS::RemoveAccount( | 366 void ProfileOAuth2TokenServiceIOSDelegate::RemoveAccount( |
393 const std::string& account_id) { | 367 const std::string& account_id) { |
394 DCHECK(thread_checker_.CalledOnValidThread()); | 368 DCHECK(thread_checker_.CalledOnValidThread()); |
395 DCHECK(!account_id.empty()); | 369 DCHECK(!account_id.empty()); |
396 | 370 |
397 if (accounts_.count(account_id) > 0) { | 371 if (accounts_.count(account_id) > 0) { |
398 // This is needed to ensure that refresh token for |acccount_id| is not | 372 // This is needed to ensure that refresh token for |acccount_id| is not |
399 // available while the account is removed. Thus all access token requests | 373 // available while the account is removed. Thus all access token requests |
400 // for |account_id| triggered while an account is being removed will get a | 374 // for |account_id| triggered while an account is being removed will get a |
401 // user not signed up error response. | 375 // user not signed up error response. |
402 accounts_[account_id]->set_marked_for_removal(true); | 376 accounts_[account_id]->set_marked_for_removal(true); |
403 CancelRequestsForAccount(account_id); | |
404 ClearCacheForAccount(account_id); | |
405 accounts_.erase(account_id); | 377 accounts_.erase(account_id); |
406 FireRefreshTokenRevoked(account_id); | 378 FireRefreshTokenRevoked(account_id); |
407 } | 379 } |
408 } | 380 } |
409 | 381 |
410 std::set<std::string> | 382 std::set<std::string> |
411 ProfileOAuth2TokenServiceIOS::GetExcludedSecondaryAccounts() { | 383 ProfileOAuth2TokenServiceIOSDelegate::GetExcludedSecondaryAccounts() { |
412 const base::ListValue* excluded_secondary_accounts_pref = | 384 const base::ListValue* excluded_secondary_accounts_pref = |
413 client()->GetPrefs()->GetList( | 385 client_->GetPrefs()->GetList( |
414 prefs::kTokenServiceExcludedSecondaryAccounts); | 386 prefs::kTokenServiceExcludedSecondaryAccounts); |
415 std::set<std::string> excluded_secondary_accounts; | 387 std::set<std::string> excluded_secondary_accounts; |
416 for (base::Value* pref_value : *excluded_secondary_accounts_pref) { | 388 for (base::Value* pref_value : *excluded_secondary_accounts_pref) { |
417 std::string value; | 389 std::string value; |
418 if (pref_value->GetAsString(&value)) | 390 if (pref_value->GetAsString(&value)) |
419 excluded_secondary_accounts.insert(value); | 391 excluded_secondary_accounts.insert(value); |
420 } | 392 } |
421 return excluded_secondary_accounts; | 393 return excluded_secondary_accounts; |
422 } | 394 } |
423 | 395 |
424 void ProfileOAuth2TokenServiceIOS::ExcludeSecondaryAccounts( | 396 void ProfileOAuth2TokenServiceIOSDelegate::ExcludeSecondaryAccounts( |
425 const std::vector<std::string>& account_ids) { | 397 const std::vector<std::string>& account_ids) { |
426 for (const auto& account_id : account_ids) | 398 for (const auto& account_id : account_ids) |
427 ExcludeSecondaryAccount(account_id); | 399 ExcludeSecondaryAccount(account_id); |
428 } | 400 } |
429 | 401 |
430 void ProfileOAuth2TokenServiceIOS::ExcludeSecondaryAccount( | 402 void ProfileOAuth2TokenServiceIOSDelegate::ExcludeSecondaryAccount( |
431 const std::string& account_id) { | 403 const std::string& account_id) { |
432 if (GetExcludeAllSecondaryAccounts()) { | 404 if (GetExcludeAllSecondaryAccounts()) { |
433 // Avoid excluding individual secondary accounts when all secondary | 405 // Avoid excluding individual secondary accounts when all secondary |
434 // accounts are excluded. | 406 // accounts are excluded. |
435 return; | 407 return; |
436 } | 408 } |
437 | 409 |
438 DCHECK(!account_id.empty()); | 410 DCHECK(!account_id.empty()); |
439 ListPrefUpdate update(client()->GetPrefs(), | 411 ListPrefUpdate update(client_->GetPrefs(), |
440 prefs::kTokenServiceExcludedSecondaryAccounts); | 412 prefs::kTokenServiceExcludedSecondaryAccounts); |
441 base::ListValue* excluded_secondary_accounts = update.Get(); | 413 base::ListValue* excluded_secondary_accounts = update.Get(); |
442 for (base::Value* pref_value : *excluded_secondary_accounts) { | 414 for (base::Value* pref_value : *excluded_secondary_accounts) { |
443 std::string value_at_it; | 415 std::string value_at_it; |
444 if (pref_value->GetAsString(&value_at_it) && (value_at_it == account_id)) { | 416 if (pref_value->GetAsString(&value_at_it) && (value_at_it == account_id)) { |
445 // |account_id| is already excluded. | 417 // |account_id| is already excluded. |
446 return; | 418 return; |
447 } | 419 } |
448 } | 420 } |
449 excluded_secondary_accounts->AppendString(account_id); | 421 excluded_secondary_accounts->AppendString(account_id); |
450 } | 422 } |
451 | 423 |
452 void ProfileOAuth2TokenServiceIOS::IncludeSecondaryAccount( | 424 void ProfileOAuth2TokenServiceIOSDelegate::IncludeSecondaryAccount( |
453 const std::string& account_id) { | 425 const std::string& account_id) { |
454 if (GetExcludeAllSecondaryAccounts()) { | 426 if (GetExcludeAllSecondaryAccounts()) { |
455 // Avoid including individual secondary accounts when all secondary | 427 // Avoid including individual secondary accounts when all secondary |
456 // accounts are excluded. | 428 // accounts are excluded. |
457 return; | 429 return; |
458 } | 430 } |
459 | 431 |
460 DCHECK_NE(account_id, primary_account_id_); | 432 DCHECK_NE(account_id, primary_account_id_); |
461 DCHECK(!primary_account_id_.empty()); | 433 DCHECK(!primary_account_id_.empty()); |
462 | 434 |
463 // Excluded secondary account ids is a logical set (not a list) of accounts. | 435 // Excluded secondary account ids is a logical set (not a list) of accounts. |
464 // As the value stored in the excluded account ids preference is a list, | 436 // As the value stored in the excluded account ids preference is a list, |
465 // the code below removes all occurences of |account_id| from this list. This | 437 // the code below removes all occurences of |account_id| from this list. This |
466 // ensures that |account_id| is actually included even in cases when the | 438 // ensures that |account_id| is actually included even in cases when the |
467 // preference value was corrupted (see bug http://crbug.com/453470 as | 439 // preference value was corrupted (see bug http://crbug.com/453470 as |
468 // example). | 440 // example). |
469 ListPrefUpdate update(client()->GetPrefs(), | 441 ListPrefUpdate update(client_->GetPrefs(), |
470 prefs::kTokenServiceExcludedSecondaryAccounts); | 442 prefs::kTokenServiceExcludedSecondaryAccounts); |
471 base::ListValue* excluded_secondary_accounts = update.Get(); | 443 base::ListValue* excluded_secondary_accounts = update.Get(); |
472 base::ListValue::iterator it = excluded_secondary_accounts->begin(); | 444 base::ListValue::iterator it = excluded_secondary_accounts->begin(); |
473 while (it != excluded_secondary_accounts->end()) { | 445 while (it != excluded_secondary_accounts->end()) { |
474 base::Value* pref_value = *it; | 446 base::Value* pref_value = *it; |
475 std::string value_at_it; | 447 std::string value_at_it; |
476 if (pref_value->GetAsString(&value_at_it) && (value_at_it == account_id)) { | 448 if (pref_value->GetAsString(&value_at_it) && (value_at_it == account_id)) { |
477 it = excluded_secondary_accounts->Erase(it, nullptr); | 449 it = excluded_secondary_accounts->Erase(it, nullptr); |
478 continue; | 450 continue; |
479 } | 451 } |
480 ++it; | 452 ++it; |
481 } | 453 } |
482 } | 454 } |
483 | 455 |
484 bool ProfileOAuth2TokenServiceIOS::GetExcludeAllSecondaryAccounts() { | 456 bool ProfileOAuth2TokenServiceIOSDelegate::GetExcludeAllSecondaryAccounts() { |
485 return client()->GetPrefs()->GetBoolean( | 457 return client_->GetPrefs()->GetBoolean( |
486 prefs::kTokenServiceExcludeAllSecondaryAccounts); | 458 prefs::kTokenServiceExcludeAllSecondaryAccounts); |
487 } | 459 } |
488 | 460 |
489 void ProfileOAuth2TokenServiceIOS::ExcludeAllSecondaryAccounts() { | 461 void ProfileOAuth2TokenServiceIOSDelegate::ExcludeAllSecondaryAccounts() { |
490 client()->GetPrefs()->SetBoolean( | 462 client_->GetPrefs()->SetBoolean( |
491 prefs::kTokenServiceExcludeAllSecondaryAccounts, true); | 463 prefs::kTokenServiceExcludeAllSecondaryAccounts, true); |
492 } | 464 } |
493 | 465 |
494 void ProfileOAuth2TokenServiceIOS::ClearExcludedSecondaryAccounts() { | 466 void ProfileOAuth2TokenServiceIOSDelegate::ClearExcludedSecondaryAccounts() { |
495 client()->GetPrefs()->ClearPref( | 467 client_->GetPrefs()->ClearPref( |
496 prefs::kTokenServiceExcludeAllSecondaryAccounts); | 468 prefs::kTokenServiceExcludeAllSecondaryAccounts); |
497 client()->GetPrefs()->ClearPref( | 469 client_->GetPrefs()->ClearPref(prefs::kTokenServiceExcludedSecondaryAccounts); |
498 prefs::kTokenServiceExcludedSecondaryAccounts); | |
499 } | 470 } |
OLD | NEW |