#include <utility>
#include <vector>
+#include "base/debug/trace_event.h"
#include "base/lazy_instance.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
+#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
#include "chrome/common/extensions/api/identity.h"
#include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h"
#include "chrome/common/pref_names.h"
#include "url/gurl.h"
#if defined(OS_CHROMEOS)
-#include "chrome/browser/chromeos/login/users/user_manager.h"
+#include "chrome/browser/chromeos/login/session/user_session_manager.h"
#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
#include "chrome/browser/chromeos/settings/device_oauth2_token_service.h"
#include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h"
+#include "components/user_manager/user_manager.h"
#include "google_apis/gaia/gaia_constants.h"
#endif
IdentityAPI::IdentityAPI(content::BrowserContext* context)
: browser_context_(context),
- account_tracker_(Profile::FromBrowserContext(context)) {
+ profile_identity_provider_(
+ SigninManagerFactory::GetForProfile(
+ Profile::FromBrowserContext(context)),
+ ProfileOAuth2TokenServiceFactory::GetForProfile(
+ Profile::FromBrowserContext(context)),
+ LoginUIServiceFactory::GetForProfile(
+ Profile::FromBrowserContext(context))),
+ account_tracker_(&profile_identity_provider_,
+ g_browser_process->system_request_context()) {
account_tracker_.AddObserver(this);
}
std::vector<std::string> IdentityAPI::GetAccounts() const {
const std::string primary_account_id = GetPrimaryAccountId(browser_context_);
- const std::vector<AccountIds> ids = account_tracker_.GetAccounts();
+ const std::vector<gaia::AccountIds> ids = account_tracker_.GetAccounts();
std::vector<std::string> gaia_ids;
if (switches::IsExtensionsMultiAccount()) {
- for (std::vector<AccountIds>::const_iterator it = ids.begin();
+ for (std::vector<gaia::AccountIds>::const_iterator it = ids.begin();
it != ids.end();
++it) {
gaia_ids.push_back(it->gaia);
}
std::string IdentityAPI::FindAccountKeyByGaiaId(const std::string& gaia_id) {
- return account_tracker_.FindAccountKeyByGaiaId(gaia_id);
-}
-
-void IdentityAPI::ReportAuthError(const GoogleServiceAuthError& error) {
- account_tracker_.ReportAuthError(GetPrimaryAccountId(browser_context_),
- error);
-}
-
-GoogleServiceAuthError IdentityAPI::GetAuthStatusForTest() const {
- return account_tracker_.GetAuthStatus();
+ return account_tracker_.FindAccountIdsByGaiaId(gaia_id).account_key;
}
void IdentityAPI::Shutdown() {
return g_factory.Pointer();
}
-void IdentityAPI::OnAccountAdded(const AccountIds& ids) {}
+void IdentityAPI::OnAccountAdded(const gaia::AccountIds& ids) {
+}
-void IdentityAPI::OnAccountRemoved(const AccountIds& ids) {}
+void IdentityAPI::OnAccountRemoved(const gaia::AccountIds& ids) {
+}
-void IdentityAPI::OnAccountSignInChanged(const AccountIds& ids,
+void IdentityAPI::OnAccountSignInChanged(const gaia::AccountIds& ids,
bool is_signed_in) {
api::identity::AccountInfo account_info;
account_info.id = ids.gaia;
shutdown_observer_list_.RemoveObserver(observer);
}
-void IdentityAPI::SetAccountStateForTest(AccountIds ids, bool is_signed_in) {
+void IdentityAPI::SetAccountStateForTest(gaia::AccountIds ids,
+ bool is_signed_in) {
account_tracker_.SetAccountStateForTest(ids, is_signed_in);
}
IdentityGetAuthTokenFunction::IdentityGetAuthTokenFunction()
: OAuth2TokenService::Consumer("extensions_identity_api"),
should_prompt_for_scopes_(false),
- should_prompt_for_signin_(false) {}
+ should_prompt_for_signin_(false) {
+ TRACE_EVENT_ASYNC_BEGIN1("identity",
+ "IdentityGetAuthTokenFunction",
+ this,
+ "extension",
+ extension()->id());
+}
-IdentityGetAuthTokenFunction::~IdentityGetAuthTokenFunction() {}
+IdentityGetAuthTokenFunction::~IdentityGetAuthTokenFunction() {
+ TRACE_EVENT_ASYNC_END0("identity", "IdentityGetAuthTokenFunction", this);
+}
bool IdentityGetAuthTokenFunction::RunAsync() {
if (GetProfile()->IsOffTheRecord()) {
should_prompt_for_scopes_ = interactive;
should_prompt_for_signin_ = interactive;
- const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension());
+ const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension());
// Check that the necessary information is present in the manifest.
oauth2_client_id_ = GetOAuth2ClientId();
}
token_key_.reset(
- new ExtensionTokenKey(GetExtension()->id(), account_key, scopes));
+ new ExtensionTokenKey(extension()->id(), account_key, scopes));
// From here on out, results must be returned asynchronously.
StartAsyncRun();
#if defined(OS_CHROMEOS)
policy::BrowserPolicyConnectorChromeOS* connector =
g_browser_process->platform_part()->browser_policy_connector_chromeos();
- if (chromeos::UserManager::Get()->IsLoggedInAsKioskApp() &&
+ if (user_manager::UserManager::Get()->IsLoggedInAsKioskApp() &&
connector->IsEnterpriseManaged()) {
StartMintTokenFlow(IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE);
return true;
void IdentityGetAuthTokenFunction::CompleteFunctionWithError(
const std::string& error) {
+ TRACE_EVENT_ASYNC_STEP_PAST1("identity",
+ "IdentityGetAuthTokenFunction",
+ this,
+ "CompleteFunctionWithError",
+ "error",
+ error);
error_ = error;
CompleteAsyncRun(false);
}
void IdentityGetAuthTokenFunction::StartMintToken(
IdentityMintRequestQueue::MintType type) {
- const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension());
+ TRACE_EVENT_ASYNC_STEP_PAST1("identity",
+ "IdentityGetAuthTokenFunction",
+ this,
+ "StartMintToken",
+ "type",
+ type);
+
+ const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension());
IdentityAPI* id_api = IdentityAPI::GetFactoryInstance()->Get(GetProfile());
IdentityTokenCacheValue cache_entry = id_api->GetCachedToken(*token_key_);
IdentityTokenCacheValue::CacheValueStatus cache_status =
case IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND:
#if defined(OS_CHROMEOS)
// Always force minting token for ChromeOS kiosk app.
- if (chromeos::UserManager::Get()->IsLoggedInAsKioskApp()) {
+ if (user_manager::UserManager::Get()->IsLoggedInAsKioskApp()) {
gaia_mint_token_mode_ = OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE;
policy::BrowserPolicyConnectorChromeOS* connector =
g_browser_process->platform_part()
void IdentityGetAuthTokenFunction::OnMintTokenSuccess(
const std::string& access_token, int time_to_live) {
+ TRACE_EVENT_ASYNC_STEP_PAST0("identity",
+ "IdentityGetAuthTokenFunction",
+ this,
+ "OnMintTokenSuccess");
+
IdentityTokenCacheValue token(access_token,
base::TimeDelta::FromSeconds(time_to_live));
IdentityAPI::GetFactoryInstance()->Get(GetProfile())->SetCachedToken(
void IdentityGetAuthTokenFunction::OnMintTokenFailure(
const GoogleServiceAuthError& error) {
+ TRACE_EVENT_ASYNC_STEP_PAST1("identity",
+ "IdentityGetAuthTokenFunction",
+ this,
+ "OnMintTokenFailure",
+ "error",
+ error.ToString());
CompleteMintTokenFlow();
switch (error.state()) {
case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
case GoogleServiceAuthError::ACCOUNT_DELETED:
case GoogleServiceAuthError::ACCOUNT_DISABLED:
- extensions::IdentityAPI::GetFactoryInstance()
- ->Get(GetProfile())
- ->ReportAuthError(error);
+ // TODO(courage): flush ticket and retry once
if (should_prompt_for_signin_) {
// Display a login prompt and try again (once).
StartSigninFlow();
void IdentityGetAuthTokenFunction::OnIssueAdviceSuccess(
const IssueAdviceInfo& issue_advice) {
+ TRACE_EVENT_ASYNC_STEP_PAST0("identity",
+ "IdentityGetAuthTokenFunction",
+ this,
+ "OnIssueAdviceSuccess");
+
IdentityAPI::GetFactoryInstance()->Get(GetProfile())->SetCachedToken(
*token_key_, IdentityTokenCacheValue(issue_advice));
CompleteMintTokenFlow();
}
void IdentityGetAuthTokenFunction::SigninSuccess() {
+ TRACE_EVENT_ASYNC_STEP_PAST0("identity",
+ "IdentityGetAuthTokenFunction",
+ this,
+ "SigninSuccess");
+
+ // If there was no account associated this profile before the
+ // sign-in, we may not have an account_id in the token_key yet.
+ if (token_key_->account_id.empty()) {
+ token_key_->account_id = GetPrimaryAccountId(GetProfile());
+ }
+
StartMintTokenFlow(IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE);
}
void IdentityGetAuthTokenFunction::SigninFailed() {
+ TRACE_EVENT_ASYNC_STEP_PAST0("identity",
+ "IdentityGetAuthTokenFunction",
+ this,
+ "SigninFailed");
CompleteFunctionWithError(identity_constants::kUserNotSignedIn);
}
void IdentityGetAuthTokenFunction::OnGaiaFlowCompleted(
const std::string& access_token,
const std::string& expiration) {
-
+ TRACE_EVENT_ASYNC_STEP_PAST0("identity",
+ "IdentityGetAuthTokenFunction",
+ this,
+ "OnGaiaFlowCompleted");
int time_to_live;
if (!expiration.empty() && base::StringToInt(expiration, &time_to_live)) {
IdentityTokenCacheValue token_value(
const OAuth2TokenService::Request* request,
const std::string& access_token,
const base::Time& expiration_time) {
+ TRACE_EVENT_ASYNC_STEP_PAST1("identity",
+ "IdentityGetAuthTokenFunction",
+ this,
+ "OnGetTokenSuccess",
+ "account",
+ request->GetAccountId());
login_token_request_.reset();
StartGaiaRequest(access_token);
}
void IdentityGetAuthTokenFunction::OnGetTokenFailure(
const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) {
+ TRACE_EVENT_ASYNC_STEP_PAST1("identity",
+ "IdentityGetAuthTokenFunction",
+ this,
+ "OnGetTokenFailure",
+ "error",
+ error.ToString());
login_token_request_.reset();
OnGaiaFlowFailure(GaiaWebAuthFlow::SERVICE_AUTH_ERROR, error, std::string());
}
if (chrome::IsRunningInForcedAppMode()) {
std::string app_client_id;
std::string app_client_secret;
- if (chromeos::UserManager::Get()->GetAppModeChromeClientOAuthInfo(
- &app_client_id, &app_client_secret)) {
+ if (chromeos::UserSessionManager::GetInstance()->
+ GetAppModeChromeClientOAuthInfo(&app_client_id,
+ &app_client_secret)) {
login_token_request_ =
service->StartRequestForClient(token_key_->account_id,
app_client_id,
this,
OAuth2MintTokenFlow::Parameters(
login_access_token,
- GetExtension()->id(),
+ extension()->id(),
oauth2_client_id_,
std::vector<std::string>(token_key_->scopes.begin(),
token_key_->scopes.end()),
}
std::string IdentityGetAuthTokenFunction::GetOAuth2ClientId() const {
- const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(GetExtension());
+ const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension());
std::string client_id = oauth2_info.client_id;
// Component apps using auto_approve may use Chrome's client ID by
// omitting the field.
- if (client_id.empty() && GetExtension()->location() == Manifest::COMPONENT &&
+ if (client_id.empty() && extension()->location() == Manifest::COMPONENT &&
oauth2_info.auto_approve) {
client_id = GaiaUrls::GetInstance()->oauth2_chrome_client_id();
}
}
api::identity::ProfileUserInfo profile_user_info;
- if (GetExtension()->permissions_data()->HasAPIPermission(
+ if (extension()->permissions_data()->HasAPIPermission(
APIPermission::kIdentityEmail)) {
profile_user_info.email =
GetProfile()->GetPrefs()->GetString(prefs::kGoogleServicesUsername);
identity::RemoveCachedAuthToken::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
IdentityAPI::GetFactoryInstance()->Get(GetProfile())->EraseCachedToken(
- GetExtension()->id(), params->details.token);
+ extension()->id(), params->details.token);
return true;
}
// Set up acceptable target URLs. (Does not include chrome-extension
// scheme for this version of the API.)
- InitFinalRedirectURLPrefix(GetExtension()->id());
+ InitFinalRedirectURLPrefix(extension()->id());
AddRef(); // Balanced in OnAuthFlowSuccess/Failure.