Index: chrome/browser/extensions/api/identity/identity_api.cc |
=================================================================== |
--- chrome/browser/extensions/api/identity/identity_api.cc (revision 147566) |
+++ chrome/browser/extensions/api/identity/identity_api.cc (working copy) |
@@ -13,23 +13,30 @@ |
#include "chrome/browser/signin/token_service.h" |
#include "chrome/browser/signin/token_service_factory.h" |
#include "chrome/browser/ui/browser.h" |
-#include "chrome/browser/ui/tab_contents/tab_contents.h" |
+#include "chrome/browser/ui/browser_navigator.h" |
+#include "chrome/browser/ui/webui/signin/login_ui_service.h" |
+#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" |
#include "chrome/common/extensions/extension.h" |
+#include "chrome/common/url_constants.h" |
+#include "content/public/common/page_transition_types.h" |
#include "googleurl/src/gurl.h" |
+#include "webkit/glue/window_open_disposition.h" |
namespace extensions { |
-namespace { |
+const char IdentityGetAuthTokenFunction::kInvalidClientId[] = |
+ "Invalid OAuth2 Client ID."; |
+const char IdentityGetAuthTokenFunction::kInvalidScopes[] = |
+ "Invalid OAuth2 scopes."; |
+const char IdentityGetAuthTokenFunction::kAuthFailure[] = |
+ "OAuth2 request failed: "; |
+const char IdentityGetAuthTokenFunction::kNoGrant[] = |
+ "OAuth2 not granted or revoked."; |
+const char IdentityGetAuthTokenFunction::kUserRejected[] = |
+ "The user did not approve access."; |
+const char IdentityGetAuthTokenFunction::kUserNotSignedIn[] = |
+ "The user is not signed in."; |
-const char kInvalidClientId[] = "Invalid OAuth2 Client ID."; |
-const char kInvalidScopes[] = "Invalid OAuth2 scopes."; |
-const char kInvalidRedirect[] = "Did not redirect to the right URL."; |
-const char kAuthFailure[] = "OAuth2 request failed: "; |
-const char kNoGrant[] = "OAuth2 not granted or revoked."; |
-const char kUserRejected[] = "The user did not approve access."; |
- |
-} // namespace |
- |
namespace GetAuthToken = extensions::api::experimental_identity::GetAuthToken; |
namespace LaunchWebAuthFlow = |
extensions::api::experimental_identity::LaunchWebAuthFlow; |
@@ -44,13 +51,33 @@ |
if (params->details.get() && params->details->interactive.get()) |
interactive_ = *params->details->interactive; |
+ const Extension::OAuth2Info& oauth2_info = GetExtension()->oauth2_info(); |
+ |
+ // Check that the necessary information is present in the manfist. |
+ if (oauth2_info.client_id.empty()) { |
+ error_ = kInvalidClientId; |
+ return false; |
+ } |
+ |
+ if (oauth2_info.scopes.size() == 0) { |
+ error_ = kInvalidScopes; |
+ return false; |
+ } |
+ |
// Balanced in OnIssueAdviceSuccess|OnMintTokenSuccess|OnMintTokenFailure| |
- // InstallUIAbort. |
+ // InstallUIAbort|OnLoginUIClosed. |
AddRef(); |
- if (StartFlow(ExtensionInstallPrompt::ShouldAutomaticallyApproveScopes() ? |
- OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE : |
- OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE)) { |
+ if (!HasLoginToken()) { |
+ if (StartLogin()) { |
+ return true; |
+ } else { |
+ Release(); |
+ return false; |
+ } |
+ } |
+ |
+ if (StartFlow(GetTokenFlowMode())) { |
return true; |
} else { |
Release(); |
@@ -79,7 +106,7 @@ |
if (interactive_) { |
install_ui_.reset( |
chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser())); |
- install_ui_->ConfirmIssueAdvice(this, GetExtension(), issue_advice); |
+ ShowOAuthApprovalDialog(issue_advice); |
} else { |
error_ = kNoGrant; |
SendResponse(false); |
@@ -87,6 +114,15 @@ |
} |
} |
+void IdentityGetAuthTokenFunction::OnLoginUIClosed( |
+ LoginUIService::LoginUI* ui) { |
+ StopObservingLoginService(); |
+ if (!StartFlow(GetTokenFlowMode())) { |
+ SendResponse(false); |
+ Release(); |
+ } |
+} |
+ |
void IdentityGetAuthTokenFunction::InstallUIProceed() { |
DCHECK(install_ui_->record_oauth2_grant()); |
// The user has accepted the scopes, so we may now force (recording a grant |
@@ -102,33 +138,92 @@ |
} |
bool IdentityGetAuthTokenFunction::StartFlow(OAuth2MintTokenFlow::Mode mode) { |
- const Extension* extension = GetExtension(); |
- Extension::OAuth2Info oauth2_info = extension->oauth2_info(); |
- |
- if (oauth2_info.client_id.empty()) { |
- error_ = kInvalidClientId; |
+ if (!HasLoginToken()) { |
+ error_ = kUserNotSignedIn; |
return false; |
} |
- if (oauth2_info.scopes.size() == 0) { |
- error_ = kInvalidScopes; |
+ flow_.reset(CreateMintTokenFlow(mode)); |
+ flow_->Start(); |
+ return true; |
+} |
+ |
+bool IdentityGetAuthTokenFunction::StartLogin() { |
+ if (!interactive_) { |
+ error_ = kUserNotSignedIn; |
return false; |
} |
+ ShowLoginPopup(); |
+ return true; |
+} |
+ |
+void IdentityGetAuthTokenFunction::StartObservingLoginService() { |
+ LoginUIService* login_ui_service = |
+ LoginUIServiceFactory::GetForProfile(profile()); |
+ login_ui_service->AddObserver(this); |
+} |
+ |
+void IdentityGetAuthTokenFunction::StopObservingLoginService() { |
+ LoginUIService* login_ui_service = |
+ LoginUIServiceFactory::GetForProfile(profile()); |
+ login_ui_service->RemoveObserver(this); |
+} |
+ |
+void IdentityGetAuthTokenFunction::ShowLoginPopup() { |
+ StartObservingLoginService(); |
+ |
+ LoginUIService* login_ui_service = |
+ LoginUIServiceFactory::GetForProfile(profile()); |
+ LoginUIService::LoginUI* login_ui = login_ui_service->current_login_ui(); |
+ if (login_ui) { |
+ login_ui->FocusUI(); |
+ } else { |
+ Browser* browser = Browser::CreateWithParams(Browser::CreateParams( |
+ Browser::TYPE_POPUP, profile())); |
+ chrome::NavigateParams params(browser, |
+ GURL(chrome::kChromeUISyncPromoURL), |
+ content::PAGE_TRANSITION_START_PAGE); |
+ params.disposition = CURRENT_TAB; |
+ chrome::Navigate(¶ms); |
+ } |
+} |
+ |
+void IdentityGetAuthTokenFunction::ShowOAuthApprovalDialog( |
+ const IssueAdviceInfo& issue_advice) { |
+ install_ui_->ConfirmIssueAdvice(this, GetExtension(), issue_advice); |
+} |
+ |
+OAuth2MintTokenFlow* IdentityGetAuthTokenFunction::CreateMintTokenFlow( |
+ OAuth2MintTokenFlow::Mode mode) { |
+ const Extension::OAuth2Info& oauth2_info = GetExtension()->oauth2_info(); |
TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); |
- flow_.reset(new OAuth2MintTokenFlow( |
+ return new OAuth2MintTokenFlow( |
profile()->GetRequestContext(), |
this, |
OAuth2MintTokenFlow::Parameters( |
token_service->GetOAuth2LoginRefreshToken(), |
- extension->id(), |
+ GetExtension()->id(), |
oauth2_info.client_id, |
oauth2_info.scopes, |
- mode))); |
- flow_->Start(); |
- return true; |
+ mode)); |
} |
+bool IdentityGetAuthTokenFunction::HasLoginToken() const { |
+ TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); |
+ return token_service->HasOAuthLoginToken(); |
+} |
+ |
+OAuth2MintTokenFlow::Mode IdentityGetAuthTokenFunction::GetTokenFlowMode() |
+ const { |
+ return ExtensionInstallPrompt::ShouldAutomaticallyApproveScopes() ? |
+ OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE : |
+ OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE; |
+} |
+ |
+const char IdentityLaunchWebAuthFlowFunction::kInvalidRedirect[] = |
+ "Did not redirect to the right URL."; |
+ |
IdentityLaunchWebAuthFlowFunction::IdentityLaunchWebAuthFlowFunction() {} |
IdentityLaunchWebAuthFlowFunction::~IdentityLaunchWebAuthFlowFunction() {} |