| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 IdentityAPI::IdentityAPI(content::BrowserContext* context) | 136 IdentityAPI::IdentityAPI(content::BrowserContext* context) |
| 137 : browser_context_(context), | 137 : browser_context_(context), |
| 138 profile_identity_provider_( | 138 profile_identity_provider_( |
| 139 SigninManagerFactory::GetForProfile( | 139 SigninManagerFactory::GetForProfile( |
| 140 Profile::FromBrowserContext(context)), | 140 Profile::FromBrowserContext(context)), |
| 141 ProfileOAuth2TokenServiceFactory::GetForProfile( | 141 ProfileOAuth2TokenServiceFactory::GetForProfile( |
| 142 Profile::FromBrowserContext(context)), | 142 Profile::FromBrowserContext(context)), |
| 143 LoginUIServiceFactory::GetShowLoginPopupCallbackForProfile( | 143 LoginUIServiceFactory::GetShowLoginPopupCallbackForProfile( |
| 144 Profile::FromBrowserContext(context))), | 144 Profile::FromBrowserContext(context))), |
| 145 account_tracker_(&profile_identity_provider_, | 145 account_tracker_(&profile_identity_provider_, |
| 146 g_browser_process->system_request_context()) { | 146 g_browser_process->system_request_context()), |
| 147 get_auth_token_function_(nullptr) { |
| 147 account_tracker_.AddObserver(this); | 148 account_tracker_.AddObserver(this); |
| 148 } | 149 } |
| 149 | 150 |
| 150 IdentityAPI::~IdentityAPI() {} | 151 IdentityAPI::~IdentityAPI() {} |
| 151 | 152 |
| 152 IdentityMintRequestQueue* IdentityAPI::mint_queue() { return &mint_queue_; } | 153 IdentityMintRequestQueue* IdentityAPI::mint_queue() { return &mint_queue_; } |
| 153 | 154 |
| 154 void IdentityAPI::SetCachedToken(const ExtensionTokenKey& key, | 155 void IdentityAPI::SetCachedToken(const ExtensionTokenKey& key, |
| 155 const IdentityTokenCacheValue& token_data) { | 156 const IdentityTokenCacheValue& token_data) { |
| 156 CachedTokens::iterator it = token_cache_.find(key); | 157 CachedTokens::iterator it = token_cache_.find(key); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 } | 201 } |
| 201 | 202 |
| 202 return gaia_ids; | 203 return gaia_ids; |
| 203 } | 204 } |
| 204 | 205 |
| 205 std::string IdentityAPI::FindAccountKeyByGaiaId(const std::string& gaia_id) { | 206 std::string IdentityAPI::FindAccountKeyByGaiaId(const std::string& gaia_id) { |
| 206 return account_tracker_.FindAccountIdsByGaiaId(gaia_id).account_key; | 207 return account_tracker_.FindAccountIdsByGaiaId(gaia_id).account_key; |
| 207 } | 208 } |
| 208 | 209 |
| 209 void IdentityAPI::Shutdown() { | 210 void IdentityAPI::Shutdown() { |
| 210 for (auto& observer : shutdown_observer_list_) | 211 if (get_auth_token_function_) |
| 211 observer.OnShutdown(); | 212 get_auth_token_function_->Shutdown(); |
| 212 account_tracker_.RemoveObserver(this); | 213 account_tracker_.RemoveObserver(this); |
| 213 account_tracker_.Shutdown(); | 214 account_tracker_.Shutdown(); |
| 214 } | 215 } |
| 215 | 216 |
| 216 static base::LazyInstance<BrowserContextKeyedAPIFactory<IdentityAPI> > | 217 static base::LazyInstance<BrowserContextKeyedAPIFactory<IdentityAPI> > |
| 217 g_factory = LAZY_INSTANCE_INITIALIZER; | 218 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 218 | 219 |
| 219 // static | 220 // static |
| 220 BrowserContextKeyedAPIFactory<IdentityAPI>* IdentityAPI::GetFactoryInstance() { | 221 BrowserContextKeyedAPIFactory<IdentityAPI>* IdentityAPI::GetFactoryInstance() { |
| 221 return g_factory.Pointer(); | 222 return g_factory.Pointer(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 235 std::unique_ptr<base::ListValue> args = | 236 std::unique_ptr<base::ListValue> args = |
| 236 api::identity::OnSignInChanged::Create(account_info, is_signed_in); | 237 api::identity::OnSignInChanged::Create(account_info, is_signed_in); |
| 237 std::unique_ptr<Event> event( | 238 std::unique_ptr<Event> event( |
| 238 new Event(events::IDENTITY_ON_SIGN_IN_CHANGED, | 239 new Event(events::IDENTITY_ON_SIGN_IN_CHANGED, |
| 239 api::identity::OnSignInChanged::kEventName, std::move(args), | 240 api::identity::OnSignInChanged::kEventName, std::move(args), |
| 240 browser_context_)); | 241 browser_context_)); |
| 241 | 242 |
| 242 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event)); | 243 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event)); |
| 243 } | 244 } |
| 244 | 245 |
| 245 void IdentityAPI::AddShutdownObserver(ShutdownObserver* observer) { | |
| 246 shutdown_observer_list_.AddObserver(observer); | |
| 247 } | |
| 248 | |
| 249 void IdentityAPI::RemoveShutdownObserver(ShutdownObserver* observer) { | |
| 250 shutdown_observer_list_.RemoveObserver(observer); | |
| 251 } | |
| 252 | |
| 253 void IdentityAPI::SetAccountStateForTest(gaia::AccountIds ids, | 246 void IdentityAPI::SetAccountStateForTest(gaia::AccountIds ids, |
| 254 bool is_signed_in) { | 247 bool is_signed_in) { |
| 255 account_tracker_.SetAccountStateForTest(ids, is_signed_in); | 248 account_tracker_.SetAccountStateForTest(ids, is_signed_in); |
| 256 } | 249 } |
| 257 | 250 |
| 258 template <> | 251 template <> |
| 259 void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies() { | 252 void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies() { |
| 260 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 253 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 261 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); | 254 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); |
| 262 } | 255 } |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 } | 394 } |
| 402 | 395 |
| 403 return true; | 396 return true; |
| 404 } | 397 } |
| 405 | 398 |
| 406 void IdentityGetAuthTokenFunction::StartAsyncRun() { | 399 void IdentityGetAuthTokenFunction::StartAsyncRun() { |
| 407 // Balanced in CompleteAsyncRun | 400 // Balanced in CompleteAsyncRun |
| 408 AddRef(); | 401 AddRef(); |
| 409 extensions::IdentityAPI::GetFactoryInstance() | 402 extensions::IdentityAPI::GetFactoryInstance() |
| 410 ->Get(GetProfile()) | 403 ->Get(GetProfile()) |
| 411 ->AddShutdownObserver(this); | 404 ->set_get_auth_token_function(this); |
| 412 } | 405 } |
| 413 | 406 |
| 414 void IdentityGetAuthTokenFunction::CompleteAsyncRun(bool success) { | 407 void IdentityGetAuthTokenFunction::CompleteAsyncRun(bool success) { |
| 415 extensions::IdentityAPI::GetFactoryInstance() | 408 extensions::IdentityAPI::GetFactoryInstance() |
| 416 ->Get(GetProfile()) | 409 ->Get(GetProfile()) |
| 417 ->RemoveShutdownObserver(this); | 410 ->set_get_auth_token_function(nullptr); |
| 418 | 411 |
| 419 SendResponse(success); | 412 SendResponse(success); |
| 420 Release(); // Balanced in StartAsyncRun | 413 Release(); // Balanced in StartAsyncRun |
| 421 } | 414 } |
| 422 | 415 |
| 423 void IdentityGetAuthTokenFunction::CompleteFunctionWithResult( | 416 void IdentityGetAuthTokenFunction::CompleteFunctionWithResult( |
| 424 const std::string& access_token) { | 417 const std::string& access_token) { |
| 425 SetResult(base::MakeUnique<base::StringValue>(access_token)); | 418 SetResult(base::MakeUnique<base::StringValue>(access_token)); |
| 426 CompleteAsyncRun(true); | 419 CompleteAsyncRun(true); |
| 427 } | 420 } |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 739 TRACE_EVENT_ASYNC_STEP_PAST1("identity", | 732 TRACE_EVENT_ASYNC_STEP_PAST1("identity", |
| 740 "IdentityGetAuthTokenFunction", | 733 "IdentityGetAuthTokenFunction", |
| 741 this, | 734 this, |
| 742 "OnGetTokenFailure", | 735 "OnGetTokenFailure", |
| 743 "error", | 736 "error", |
| 744 error.ToString()); | 737 error.ToString()); |
| 745 login_token_request_.reset(); | 738 login_token_request_.reset(); |
| 746 OnGaiaFlowFailure(GaiaWebAuthFlow::SERVICE_AUTH_ERROR, error, std::string()); | 739 OnGaiaFlowFailure(GaiaWebAuthFlow::SERVICE_AUTH_ERROR, error, std::string()); |
| 747 } | 740 } |
| 748 | 741 |
| 749 void IdentityGetAuthTokenFunction::OnShutdown() { | 742 void IdentityGetAuthTokenFunction::Shutdown() { |
| 750 gaia_web_auth_flow_.reset(); | 743 gaia_web_auth_flow_.reset(); |
| 751 signin_flow_.reset(); | 744 signin_flow_.reset(); |
| 752 login_token_request_.reset(); | 745 login_token_request_.reset(); |
| 753 extensions::IdentityAPI::GetFactoryInstance() | 746 extensions::IdentityAPI::GetFactoryInstance() |
| 754 ->Get(GetProfile()) | 747 ->Get(GetProfile()) |
| 755 ->mint_queue() | 748 ->mint_queue() |
| 756 ->RequestCancel(*token_key_, this); | 749 ->RequestCancel(*token_key_, this); |
| 757 CompleteFunctionWithError(identity_constants::kCanceled); | 750 CompleteFunctionWithError(identity_constants::kCanceled); |
| 758 } | 751 } |
| 759 | 752 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 874 // Component apps using auto_approve may use Chrome's client ID by | 867 // Component apps using auto_approve may use Chrome's client ID by |
| 875 // omitting the field. | 868 // omitting the field. |
| 876 if (client_id.empty() && extension()->location() == Manifest::COMPONENT && | 869 if (client_id.empty() && extension()->location() == Manifest::COMPONENT && |
| 877 oauth2_info.auto_approve) { | 870 oauth2_info.auto_approve) { |
| 878 client_id = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); | 871 client_id = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); |
| 879 } | 872 } |
| 880 return client_id; | 873 return client_id; |
| 881 } | 874 } |
| 882 | 875 |
| 883 } // namespace extensions | 876 } // namespace extensions |
| OLD | NEW |