| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/identity_api.h" | 5 #include "chrome/browser/extensions/api/identity/identity_api.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 void IdentityGetAuthTokenFunction::CompleteFunctionWithError( | 155 void IdentityGetAuthTokenFunction::CompleteFunctionWithError( |
| 156 const std::string& error) { | 156 const std::string& error) { |
| 157 error_ = error; | 157 error_ = error; |
| 158 SendResponse(false); | 158 SendResponse(false); |
| 159 Release(); // Balanced in RunImpl. | 159 Release(); // Balanced in RunImpl. |
| 160 } | 160 } |
| 161 | 161 |
| 162 void IdentityGetAuthTokenFunction::StartSigninFlow() { | 162 void IdentityGetAuthTokenFunction::StartSigninFlow() { |
| 163 // All cached tokens are invalid because the user is not signed in. | 163 // All cached tokens are invalid because the user is not signed in. |
| 164 IdentityAPI* id_api = | 164 IdentityAPI* id_api = |
| 165 extensions::IdentityAPI::GetFactoryInstance()->GetForProfile( | 165 extensions::IdentityAPI::GetFactoryInstance()->Get(GetProfile()); |
| 166 GetProfile()); | |
| 167 id_api->EraseAllCachedTokens(); | 166 id_api->EraseAllCachedTokens(); |
| 168 // Display a login prompt. If the subsequent mint fails, don't display the | 167 // Display a login prompt. If the subsequent mint fails, don't display the |
| 169 // login prompt again. | 168 // login prompt again. |
| 170 should_prompt_for_signin_ = false; | 169 should_prompt_for_signin_ = false; |
| 171 ShowLoginPopup(); | 170 ShowLoginPopup(); |
| 172 } | 171 } |
| 173 | 172 |
| 174 void IdentityGetAuthTokenFunction::StartMintTokenFlow( | 173 void IdentityGetAuthTokenFunction::StartMintTokenFlow( |
| 175 IdentityMintRequestQueue::MintType type) { | 174 IdentityMintRequestQueue::MintType type) { |
| 176 mint_token_flow_type_ = type; | 175 mint_token_flow_type_ = type; |
| 177 | 176 |
| 178 // Flows are serialized to prevent excessive traffic to GAIA, and | 177 // Flows are serialized to prevent excessive traffic to GAIA, and |
| 179 // to consolidate UI pop-ups. | 178 // to consolidate UI pop-ups. |
| 180 IdentityAPI* id_api = | 179 IdentityAPI* id_api = |
| 181 extensions::IdentityAPI::GetFactoryInstance()->GetForProfile( | 180 extensions::IdentityAPI::GetFactoryInstance()->Get(GetProfile()); |
| 182 GetProfile()); | |
| 183 | 181 |
| 184 if (!should_prompt_for_scopes_) { | 182 if (!should_prompt_for_scopes_) { |
| 185 // Caller requested no interaction. | 183 // Caller requested no interaction. |
| 186 | 184 |
| 187 if (type == IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE) { | 185 if (type == IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE) { |
| 188 // GAIA told us to do a consent UI. | 186 // GAIA told us to do a consent UI. |
| 189 CompleteFunctionWithError(identity_constants::kNoGrant); | 187 CompleteFunctionWithError(identity_constants::kNoGrant); |
| 190 return; | 188 return; |
| 191 } | 189 } |
| 192 if (!id_api->mint_queue()->empty( | 190 if (!id_api->mint_queue()->empty( |
| 193 IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE, *token_key_)) { | 191 IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE, *token_key_)) { |
| 194 // Another call is going through a consent UI. | 192 // Another call is going through a consent UI. |
| 195 CompleteFunctionWithError(identity_constants::kNoGrant); | 193 CompleteFunctionWithError(identity_constants::kNoGrant); |
| 196 return; | 194 return; |
| 197 } | 195 } |
| 198 } | 196 } |
| 199 id_api->mint_queue()->RequestStart(type, *token_key_, this); | 197 id_api->mint_queue()->RequestStart(type, *token_key_, this); |
| 200 } | 198 } |
| 201 | 199 |
| 202 void IdentityGetAuthTokenFunction::CompleteMintTokenFlow() { | 200 void IdentityGetAuthTokenFunction::CompleteMintTokenFlow() { |
| 203 IdentityMintRequestQueue::MintType type = mint_token_flow_type_; | 201 IdentityMintRequestQueue::MintType type = mint_token_flow_type_; |
| 204 | 202 |
| 205 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension()); | 203 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension()); |
| 206 std::set<std::string> scopes(oauth2_info.scopes.begin(), | 204 std::set<std::string> scopes(oauth2_info.scopes.begin(), |
| 207 oauth2_info.scopes.end()); | 205 oauth2_info.scopes.end()); |
| 208 | 206 |
| 209 extensions::IdentityAPI::GetFactoryInstance() | 207 extensions::IdentityAPI::GetFactoryInstance() |
| 210 ->GetForProfile(GetProfile()) | 208 ->Get(GetProfile()) |
| 211 ->mint_queue() | 209 ->mint_queue() |
| 212 ->RequestComplete(type, *token_key_, this); | 210 ->RequestComplete(type, *token_key_, this); |
| 213 } | 211 } |
| 214 | 212 |
| 215 void IdentityGetAuthTokenFunction::StartMintToken( | 213 void IdentityGetAuthTokenFunction::StartMintToken( |
| 216 IdentityMintRequestQueue::MintType type) { | 214 IdentityMintRequestQueue::MintType type) { |
| 217 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension()); | 215 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension()); |
| 218 IdentityAPI* id_api = | 216 IdentityAPI* id_api = IdentityAPI::GetFactoryInstance()->Get(GetProfile()); |
| 219 IdentityAPI::GetFactoryInstance()->GetForProfile(GetProfile()); | |
| 220 IdentityTokenCacheValue cache_entry = id_api->GetCachedToken(*token_key_); | 217 IdentityTokenCacheValue cache_entry = id_api->GetCachedToken(*token_key_); |
| 221 IdentityTokenCacheValue::CacheValueStatus cache_status = | 218 IdentityTokenCacheValue::CacheValueStatus cache_status = |
| 222 cache_entry.status(); | 219 cache_entry.status(); |
| 223 | 220 |
| 224 if (type == IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE) { | 221 if (type == IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE) { |
| 225 switch (cache_status) { | 222 switch (cache_status) { |
| 226 case IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND: | 223 case IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND: |
| 227 #if defined(OS_CHROMEOS) | 224 #if defined(OS_CHROMEOS) |
| 228 // Always force minting token for ChromeOS kiosk app. | 225 // Always force minting token for ChromeOS kiosk app. |
| 229 if (chromeos::UserManager::Get()->IsLoggedInAsKioskApp()) { | 226 if (chromeos::UserManager::Get()->IsLoggedInAsKioskApp()) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 } else { | 268 } else { |
| 272 ShowOAuthApprovalDialog(issue_advice_); | 269 ShowOAuthApprovalDialog(issue_advice_); |
| 273 } | 270 } |
| 274 } | 271 } |
| 275 } | 272 } |
| 276 | 273 |
| 277 void IdentityGetAuthTokenFunction::OnMintTokenSuccess( | 274 void IdentityGetAuthTokenFunction::OnMintTokenSuccess( |
| 278 const std::string& access_token, int time_to_live) { | 275 const std::string& access_token, int time_to_live) { |
| 279 IdentityTokenCacheValue token(access_token, | 276 IdentityTokenCacheValue token(access_token, |
| 280 base::TimeDelta::FromSeconds(time_to_live)); | 277 base::TimeDelta::FromSeconds(time_to_live)); |
| 281 IdentityAPI::GetFactoryInstance() | 278 IdentityAPI::GetFactoryInstance()->Get(GetProfile())->SetCachedToken( |
| 282 ->GetForProfile(GetProfile()) | 279 *token_key_, token); |
| 283 ->SetCachedToken(*token_key_, token); | |
| 284 | 280 |
| 285 CompleteMintTokenFlow(); | 281 CompleteMintTokenFlow(); |
| 286 CompleteFunctionWithResult(access_token); | 282 CompleteFunctionWithResult(access_token); |
| 287 } | 283 } |
| 288 | 284 |
| 289 void IdentityGetAuthTokenFunction::OnMintTokenFailure( | 285 void IdentityGetAuthTokenFunction::OnMintTokenFailure( |
| 290 const GoogleServiceAuthError& error) { | 286 const GoogleServiceAuthError& error) { |
| 291 CompleteMintTokenFlow(); | 287 CompleteMintTokenFlow(); |
| 292 | 288 |
| 293 switch (error.state()) { | 289 switch (error.state()) { |
| 294 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: | 290 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: |
| 295 case GoogleServiceAuthError::ACCOUNT_DELETED: | 291 case GoogleServiceAuthError::ACCOUNT_DELETED: |
| 296 case GoogleServiceAuthError::ACCOUNT_DISABLED: | 292 case GoogleServiceAuthError::ACCOUNT_DISABLED: |
| 297 extensions::IdentityAPI::GetFactoryInstance() | 293 extensions::IdentityAPI::GetFactoryInstance() |
| 298 ->GetForProfile(GetProfile()) | 294 ->Get(GetProfile()) |
| 299 ->ReportAuthError(error); | 295 ->ReportAuthError(error); |
| 300 if (should_prompt_for_signin_) { | 296 if (should_prompt_for_signin_) { |
| 301 // Display a login prompt and try again (once). | 297 // Display a login prompt and try again (once). |
| 302 StartSigninFlow(); | 298 StartSigninFlow(); |
| 303 return; | 299 return; |
| 304 } | 300 } |
| 305 break; | 301 break; |
| 306 default: | 302 default: |
| 307 // Return error to caller. | 303 // Return error to caller. |
| 308 break; | 304 break; |
| 309 } | 305 } |
| 310 | 306 |
| 311 CompleteFunctionWithError( | 307 CompleteFunctionWithError( |
| 312 std::string(identity_constants::kAuthFailure) + error.ToString()); | 308 std::string(identity_constants::kAuthFailure) + error.ToString()); |
| 313 } | 309 } |
| 314 | 310 |
| 315 void IdentityGetAuthTokenFunction::OnIssueAdviceSuccess( | 311 void IdentityGetAuthTokenFunction::OnIssueAdviceSuccess( |
| 316 const IssueAdviceInfo& issue_advice) { | 312 const IssueAdviceInfo& issue_advice) { |
| 317 IdentityAPI::GetFactoryInstance() | 313 IdentityAPI::GetFactoryInstance()->Get(GetProfile())->SetCachedToken( |
| 318 ->GetForProfile(GetProfile()) | 314 *token_key_, IdentityTokenCacheValue(issue_advice)); |
| 319 ->SetCachedToken(*token_key_, | |
| 320 IdentityTokenCacheValue(issue_advice)); | |
| 321 CompleteMintTokenFlow(); | 315 CompleteMintTokenFlow(); |
| 322 | 316 |
| 323 should_prompt_for_signin_ = false; | 317 should_prompt_for_signin_ = false; |
| 324 // Existing grant was revoked and we used NO_FORCE, so we got info back | 318 // Existing grant was revoked and we used NO_FORCE, so we got info back |
| 325 // instead. Start a consent UI if we can. | 319 // instead. Start a consent UI if we can. |
| 326 issue_advice_ = issue_advice; | 320 issue_advice_ = issue_advice; |
| 327 StartMintTokenFlow(IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE); | 321 StartMintTokenFlow(IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE); |
| 328 } | 322 } |
| 329 | 323 |
| 330 void IdentityGetAuthTokenFunction::SigninSuccess() { | 324 void IdentityGetAuthTokenFunction::SigninSuccess() { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 } | 368 } |
| 375 | 369 |
| 376 void IdentityGetAuthTokenFunction::OnGaiaFlowCompleted( | 370 void IdentityGetAuthTokenFunction::OnGaiaFlowCompleted( |
| 377 const std::string& access_token, | 371 const std::string& access_token, |
| 378 const std::string& expiration) { | 372 const std::string& expiration) { |
| 379 | 373 |
| 380 int time_to_live; | 374 int time_to_live; |
| 381 if (!expiration.empty() && base::StringToInt(expiration, &time_to_live)) { | 375 if (!expiration.empty() && base::StringToInt(expiration, &time_to_live)) { |
| 382 IdentityTokenCacheValue token_value( | 376 IdentityTokenCacheValue token_value( |
| 383 access_token, base::TimeDelta::FromSeconds(time_to_live)); | 377 access_token, base::TimeDelta::FromSeconds(time_to_live)); |
| 384 IdentityAPI::GetFactoryInstance() | 378 IdentityAPI::GetFactoryInstance()->Get(GetProfile())->SetCachedToken( |
| 385 ->GetForProfile(GetProfile()) | 379 *token_key_, token_value); |
| 386 ->SetCachedToken(*token_key_, token_value); | |
| 387 } | 380 } |
| 388 | 381 |
| 389 CompleteMintTokenFlow(); | 382 CompleteMintTokenFlow(); |
| 390 CompleteFunctionWithResult(access_token); | 383 CompleteFunctionWithResult(access_token); |
| 391 } | 384 } |
| 392 | 385 |
| 393 void IdentityGetAuthTokenFunction::OnGetTokenSuccess( | 386 void IdentityGetAuthTokenFunction::OnGetTokenSuccess( |
| 394 const OAuth2TokenService::Request* request, | 387 const OAuth2TokenService::Request* request, |
| 395 const std::string& access_token, | 388 const std::string& access_token, |
| 396 const base::Time& expiration_time) { | 389 const base::Time& expiration_time) { |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 | 517 |
| 525 bool IdentityRemoveCachedAuthTokenFunction::RunImpl() { | 518 bool IdentityRemoveCachedAuthTokenFunction::RunImpl() { |
| 526 if (GetProfile()->IsOffTheRecord()) { | 519 if (GetProfile()->IsOffTheRecord()) { |
| 527 error_ = identity_constants::kOffTheRecord; | 520 error_ = identity_constants::kOffTheRecord; |
| 528 return false; | 521 return false; |
| 529 } | 522 } |
| 530 | 523 |
| 531 scoped_ptr<identity::RemoveCachedAuthToken::Params> params( | 524 scoped_ptr<identity::RemoveCachedAuthToken::Params> params( |
| 532 identity::RemoveCachedAuthToken::Params::Create(*args_)); | 525 identity::RemoveCachedAuthToken::Params::Create(*args_)); |
| 533 EXTENSION_FUNCTION_VALIDATE(params.get()); | 526 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 534 IdentityAPI::GetFactoryInstance() | 527 IdentityAPI::GetFactoryInstance()->Get(GetProfile())->EraseCachedToken( |
| 535 ->GetForProfile(GetProfile()) | 528 GetExtension()->id(), params->details.token); |
| 536 ->EraseCachedToken(GetExtension()->id(), params->details.token); | |
| 537 return true; | 529 return true; |
| 538 } | 530 } |
| 539 | 531 |
| 540 IdentityLaunchWebAuthFlowFunction::IdentityLaunchWebAuthFlowFunction() {} | 532 IdentityLaunchWebAuthFlowFunction::IdentityLaunchWebAuthFlowFunction() {} |
| 541 | 533 |
| 542 IdentityLaunchWebAuthFlowFunction::~IdentityLaunchWebAuthFlowFunction() { | 534 IdentityLaunchWebAuthFlowFunction::~IdentityLaunchWebAuthFlowFunction() { |
| 543 if (auth_flow_) | 535 if (auth_flow_) |
| 544 auth_flow_.release()->DetachDelegateAndDelete(); | 536 auth_flow_.release()->DetachDelegateAndDelete(); |
| 545 } | 537 } |
| 546 | 538 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 | 713 |
| 722 GoogleServiceAuthError IdentityAPI::GetAuthStatusForTest() const { | 714 GoogleServiceAuthError IdentityAPI::GetAuthStatusForTest() const { |
| 723 return account_tracker_.GetAuthStatus(); | 715 return account_tracker_.GetAuthStatus(); |
| 724 } | 716 } |
| 725 | 717 |
| 726 void IdentityAPI::Shutdown() { | 718 void IdentityAPI::Shutdown() { |
| 727 account_tracker_.RemoveObserver(this); | 719 account_tracker_.RemoveObserver(this); |
| 728 account_tracker_.Shutdown(); | 720 account_tracker_.Shutdown(); |
| 729 } | 721 } |
| 730 | 722 |
| 731 static base::LazyInstance<ProfileKeyedAPIFactory<IdentityAPI> > | 723 static base::LazyInstance<BrowserContextKeyedAPIFactory<IdentityAPI> > |
| 732 g_factory = LAZY_INSTANCE_INITIALIZER; | 724 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 733 | 725 |
| 734 // static | 726 // static |
| 735 ProfileKeyedAPIFactory<IdentityAPI>* IdentityAPI::GetFactoryInstance() { | 727 BrowserContextKeyedAPIFactory<IdentityAPI>* IdentityAPI::GetFactoryInstance() { |
| 736 return g_factory.Pointer(); | 728 return g_factory.Pointer(); |
| 737 } | 729 } |
| 738 | 730 |
| 739 void IdentityAPI::OnAccountAdded(const AccountIds& ids) {} | 731 void IdentityAPI::OnAccountAdded(const AccountIds& ids) {} |
| 740 | 732 |
| 741 void IdentityAPI::OnAccountRemoved(const AccountIds& ids) {} | 733 void IdentityAPI::OnAccountRemoved(const AccountIds& ids) {} |
| 742 | 734 |
| 743 void IdentityAPI::OnAccountSignInChanged(const AccountIds& ids, | 735 void IdentityAPI::OnAccountSignInChanged(const AccountIds& ids, |
| 744 bool is_signed_in) { | 736 bool is_signed_in) { |
| 745 api::identity::AccountInfo account_info; | 737 api::identity::AccountInfo account_info; |
| 746 account_info.id = ids.gaia; | 738 account_info.id = ids.gaia; |
| 747 | 739 |
| 748 scoped_ptr<base::ListValue> args = | 740 scoped_ptr<base::ListValue> args = |
| 749 api::identity::OnSignInChanged::Create(account_info, is_signed_in); | 741 api::identity::OnSignInChanged::Create(account_info, is_signed_in); |
| 750 scoped_ptr<Event> event(new Event(api::identity::OnSignInChanged::kEventName, | 742 scoped_ptr<Event> event(new Event(api::identity::OnSignInChanged::kEventName, |
| 751 args.Pass(), | 743 args.Pass(), |
| 752 browser_context_)); | 744 browser_context_)); |
| 753 | 745 |
| 754 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( | 746 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( |
| 755 event.Pass()); | 747 event.Pass()); |
| 756 } | 748 } |
| 757 | 749 |
| 758 template <> | 750 template <> |
| 759 void ProfileKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies() { | 751 void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies() { |
| 760 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 752 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 761 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); | 753 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); |
| 762 } | 754 } |
| 763 | 755 |
| 764 } // namespace extensions | 756 } // namespace extensions |
| OLD | NEW |