| 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> |
| 11 | 11 |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "chrome/browser/app_mode/app_mode_utils.h" | 17 #include "chrome/browser/app_mode/app_mode_utils.h" |
| 18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
| 20 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 23 #include "chrome/browser/signin/signin_manager_factory.h" | 23 #include "chrome/browser/signin/signin_manager_factory.h" |
| 24 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" |
| 24 #include "chrome/common/extensions/api/identity.h" | 25 #include "chrome/common/extensions/api/identity.h" |
| 25 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" | 26 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" |
| 26 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
| 27 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
| 28 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 29 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 29 #include "components/signin/core/browser/signin_manager.h" | 30 #include "components/signin/core/browser/signin_manager.h" |
| 30 #include "components/signin/core/common/profile_management_switches.h" | 31 #include "components/signin/core/common/profile_management_switches.h" |
| 31 #include "extensions/browser/event_router.h" | 32 #include "extensions/browser/event_router.h" |
| 32 #include "extensions/browser/extension_function_dispatcher.h" | 33 #include "extensions/browser/extension_function_dispatcher.h" |
| 33 #include "extensions/common/extension.h" | 34 #include "extensions/common/extension.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 return status_ == CACHE_STATUS_NOTFOUND || | 124 return status_ == CACHE_STATUS_NOTFOUND || |
| 124 expiration_time_ < base::Time::Now(); | 125 expiration_time_ < base::Time::Now(); |
| 125 } | 126 } |
| 126 | 127 |
| 127 const base::Time& IdentityTokenCacheValue::expiration_time() const { | 128 const base::Time& IdentityTokenCacheValue::expiration_time() const { |
| 128 return expiration_time_; | 129 return expiration_time_; |
| 129 } | 130 } |
| 130 | 131 |
| 131 IdentityAPI::IdentityAPI(content::BrowserContext* context) | 132 IdentityAPI::IdentityAPI(content::BrowserContext* context) |
| 132 : browser_context_(context), | 133 : browser_context_(context), |
| 133 account_tracker_(Profile::FromBrowserContext(context)) { | 134 profile_identity_provider_( |
| 135 SigninManagerFactory::GetForProfile( |
| 136 Profile::FromBrowserContext(context)), |
| 137 ProfileOAuth2TokenServiceFactory::GetForProfile( |
| 138 Profile::FromBrowserContext(context)), |
| 139 LoginUIServiceFactory::GetForProfile( |
| 140 Profile::FromBrowserContext(context))), |
| 141 account_tracker_(&profile_identity_provider_, |
| 142 g_browser_process->system_request_context()) { |
| 134 account_tracker_.AddObserver(this); | 143 account_tracker_.AddObserver(this); |
| 135 } | 144 } |
| 136 | 145 |
| 137 IdentityAPI::~IdentityAPI() {} | 146 IdentityAPI::~IdentityAPI() {} |
| 138 | 147 |
| 139 IdentityMintRequestQueue* IdentityAPI::mint_queue() { return &mint_queue_; } | 148 IdentityMintRequestQueue* IdentityAPI::mint_queue() { return &mint_queue_; } |
| 140 | 149 |
| 141 void IdentityAPI::SetCachedToken(const ExtensionTokenKey& key, | 150 void IdentityAPI::SetCachedToken(const ExtensionTokenKey& key, |
| 142 const IdentityTokenCacheValue& token_data) { | 151 const IdentityTokenCacheValue& token_data) { |
| 143 CachedTokens::iterator it = token_cache_.find(key); | 152 CachedTokens::iterator it = token_cache_.find(key); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 166 const ExtensionTokenKey& key) { | 175 const ExtensionTokenKey& key) { |
| 167 return token_cache_[key]; | 176 return token_cache_[key]; |
| 168 } | 177 } |
| 169 | 178 |
| 170 const IdentityAPI::CachedTokens& IdentityAPI::GetAllCachedTokens() { | 179 const IdentityAPI::CachedTokens& IdentityAPI::GetAllCachedTokens() { |
| 171 return token_cache_; | 180 return token_cache_; |
| 172 } | 181 } |
| 173 | 182 |
| 174 std::vector<std::string> IdentityAPI::GetAccounts() const { | 183 std::vector<std::string> IdentityAPI::GetAccounts() const { |
| 175 const std::string primary_account_id = GetPrimaryAccountId(browser_context_); | 184 const std::string primary_account_id = GetPrimaryAccountId(browser_context_); |
| 176 const std::vector<AccountIds> ids = account_tracker_.GetAccounts(); | 185 const std::vector<gaia::AccountIds> ids = account_tracker_.GetAccounts(); |
| 177 std::vector<std::string> gaia_ids; | 186 std::vector<std::string> gaia_ids; |
| 178 | 187 |
| 179 if (switches::IsExtensionsMultiAccount()) { | 188 if (switches::IsExtensionsMultiAccount()) { |
| 180 for (std::vector<AccountIds>::const_iterator it = ids.begin(); | 189 for (std::vector<gaia::AccountIds>::const_iterator it = ids.begin(); |
| 181 it != ids.end(); | 190 it != ids.end(); |
| 182 ++it) { | 191 ++it) { |
| 183 gaia_ids.push_back(it->gaia); | 192 gaia_ids.push_back(it->gaia); |
| 184 } | 193 } |
| 185 } else if (ids.size() >= 1) { | 194 } else if (ids.size() >= 1) { |
| 186 gaia_ids.push_back(ids[0].gaia); | 195 gaia_ids.push_back(ids[0].gaia); |
| 187 } | 196 } |
| 188 | 197 |
| 189 return gaia_ids; | 198 return gaia_ids; |
| 190 } | 199 } |
| 191 | 200 |
| 192 std::string IdentityAPI::FindAccountKeyByGaiaId(const std::string& gaia_id) { | 201 std::string IdentityAPI::FindAccountKeyByGaiaId(const std::string& gaia_id) { |
| 193 return account_tracker_.FindAccountKeyByGaiaId(gaia_id); | 202 return account_tracker_.FindAccountIdsByGaiaId(gaia_id).account_key; |
| 194 } | |
| 195 | |
| 196 void IdentityAPI::ReportAuthError(const GoogleServiceAuthError& error) { | |
| 197 account_tracker_.ReportAuthError(GetPrimaryAccountId(browser_context_), | |
| 198 error); | |
| 199 } | |
| 200 | |
| 201 GoogleServiceAuthError IdentityAPI::GetAuthStatusForTest() const { | |
| 202 return account_tracker_.GetAuthStatus(); | |
| 203 } | 203 } |
| 204 | 204 |
| 205 void IdentityAPI::Shutdown() { | 205 void IdentityAPI::Shutdown() { |
| 206 FOR_EACH_OBSERVER(ShutdownObserver, shutdown_observer_list_, OnShutdown()); | 206 FOR_EACH_OBSERVER(ShutdownObserver, shutdown_observer_list_, OnShutdown()); |
| 207 account_tracker_.RemoveObserver(this); | 207 account_tracker_.RemoveObserver(this); |
| 208 account_tracker_.Shutdown(); | 208 account_tracker_.Shutdown(); |
| 209 } | 209 } |
| 210 | 210 |
| 211 static base::LazyInstance<BrowserContextKeyedAPIFactory<IdentityAPI> > | 211 static base::LazyInstance<BrowserContextKeyedAPIFactory<IdentityAPI> > |
| 212 g_factory = LAZY_INSTANCE_INITIALIZER; | 212 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 213 | 213 |
| 214 // static | 214 // static |
| 215 BrowserContextKeyedAPIFactory<IdentityAPI>* IdentityAPI::GetFactoryInstance() { | 215 BrowserContextKeyedAPIFactory<IdentityAPI>* IdentityAPI::GetFactoryInstance() { |
| 216 return g_factory.Pointer(); | 216 return g_factory.Pointer(); |
| 217 } | 217 } |
| 218 | 218 |
| 219 void IdentityAPI::OnAccountAdded(const AccountIds& ids) {} | 219 void IdentityAPI::OnAccountAdded(const gaia::AccountIds& ids) { |
| 220 } |
| 220 | 221 |
| 221 void IdentityAPI::OnAccountRemoved(const AccountIds& ids) {} | 222 void IdentityAPI::OnAccountRemoved(const gaia::AccountIds& ids) { |
| 223 } |
| 222 | 224 |
| 223 void IdentityAPI::OnAccountSignInChanged(const AccountIds& ids, | 225 void IdentityAPI::OnAccountSignInChanged(const gaia::AccountIds& ids, |
| 224 bool is_signed_in) { | 226 bool is_signed_in) { |
| 225 api::identity::AccountInfo account_info; | 227 api::identity::AccountInfo account_info; |
| 226 account_info.id = ids.gaia; | 228 account_info.id = ids.gaia; |
| 227 | 229 |
| 228 scoped_ptr<base::ListValue> args = | 230 scoped_ptr<base::ListValue> args = |
| 229 api::identity::OnSignInChanged::Create(account_info, is_signed_in); | 231 api::identity::OnSignInChanged::Create(account_info, is_signed_in); |
| 230 scoped_ptr<Event> event(new Event(api::identity::OnSignInChanged::kEventName, | 232 scoped_ptr<Event> event(new Event(api::identity::OnSignInChanged::kEventName, |
| 231 args.Pass(), | 233 args.Pass(), |
| 232 browser_context_)); | 234 browser_context_)); |
| 233 | 235 |
| 234 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); | 236 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); |
| 235 } | 237 } |
| 236 | 238 |
| 237 void IdentityAPI::AddShutdownObserver(ShutdownObserver* observer) { | 239 void IdentityAPI::AddShutdownObserver(ShutdownObserver* observer) { |
| 238 shutdown_observer_list_.AddObserver(observer); | 240 shutdown_observer_list_.AddObserver(observer); |
| 239 } | 241 } |
| 240 | 242 |
| 241 void IdentityAPI::RemoveShutdownObserver(ShutdownObserver* observer) { | 243 void IdentityAPI::RemoveShutdownObserver(ShutdownObserver* observer) { |
| 242 shutdown_observer_list_.RemoveObserver(observer); | 244 shutdown_observer_list_.RemoveObserver(observer); |
| 243 } | 245 } |
| 244 | 246 |
| 245 void IdentityAPI::SetAccountStateForTest(AccountIds ids, bool is_signed_in) { | 247 void IdentityAPI::SetAccountStateForTest(gaia::AccountIds ids, |
| 248 bool is_signed_in) { |
| 246 account_tracker_.SetAccountStateForTest(ids, is_signed_in); | 249 account_tracker_.SetAccountStateForTest(ids, is_signed_in); |
| 247 } | 250 } |
| 248 | 251 |
| 249 template <> | 252 template <> |
| 250 void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies() { | 253 void BrowserContextKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies() { |
| 251 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 254 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 252 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); | 255 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); |
| 253 } | 256 } |
| 254 | 257 |
| 255 IdentityGetAccountsFunction::IdentityGetAccountsFunction() { | 258 IdentityGetAccountsFunction::IdentityGetAccountsFunction() { |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 } | 529 } |
| 527 | 530 |
| 528 void IdentityGetAuthTokenFunction::OnMintTokenFailure( | 531 void IdentityGetAuthTokenFunction::OnMintTokenFailure( |
| 529 const GoogleServiceAuthError& error) { | 532 const GoogleServiceAuthError& error) { |
| 530 CompleteMintTokenFlow(); | 533 CompleteMintTokenFlow(); |
| 531 | 534 |
| 532 switch (error.state()) { | 535 switch (error.state()) { |
| 533 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: | 536 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: |
| 534 case GoogleServiceAuthError::ACCOUNT_DELETED: | 537 case GoogleServiceAuthError::ACCOUNT_DELETED: |
| 535 case GoogleServiceAuthError::ACCOUNT_DISABLED: | 538 case GoogleServiceAuthError::ACCOUNT_DISABLED: |
| 536 extensions::IdentityAPI::GetFactoryInstance() | 539 // TODO(courage): flush ticket and retry once |
| 537 ->Get(GetProfile()) | |
| 538 ->ReportAuthError(error); | |
| 539 if (should_prompt_for_signin_) { | 540 if (should_prompt_for_signin_) { |
| 540 // Display a login prompt and try again (once). | 541 // Display a login prompt and try again (once). |
| 541 StartSigninFlow(); | 542 StartSigninFlow(); |
| 542 return; | 543 return; |
| 543 } | 544 } |
| 544 break; | 545 break; |
| 545 default: | 546 default: |
| 546 // Return error to caller. | 547 // Return error to caller. |
| 547 break; | 548 break; |
| 548 } | 549 } |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 874 void IdentityLaunchWebAuthFlowFunction::OnAuthFlowURLChange( | 875 void IdentityLaunchWebAuthFlowFunction::OnAuthFlowURLChange( |
| 875 const GURL& redirect_url) { | 876 const GURL& redirect_url) { |
| 876 if (redirect_url.GetWithEmptyPath() == final_url_prefix_) { | 877 if (redirect_url.GetWithEmptyPath() == final_url_prefix_) { |
| 877 SetResult(new base::StringValue(redirect_url.spec())); | 878 SetResult(new base::StringValue(redirect_url.spec())); |
| 878 SendResponse(true); | 879 SendResponse(true); |
| 879 Release(); // Balanced in RunAsync. | 880 Release(); // Balanced in RunAsync. |
| 880 } | 881 } |
| 881 } | 882 } |
| 882 | 883 |
| 883 } // namespace extensions | 884 } // namespace extensions |
| OLD | NEW |