Index: chrome/browser/extensions/api/identity/identity_api.cc |
=================================================================== |
--- chrome/browser/extensions/api/identity/identity_api.cc (revision 146190) |
+++ chrome/browser/extensions/api/identity/identity_api.cc (working copy) |
@@ -12,9 +12,14 @@ |
#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 { |
@@ -26,6 +31,7 @@ |
const char kAuthFailure[] = "OAuth2 request failed: "; |
const char kNoGrant[] = "OAuth2 not granted or revoked."; |
const char kUserRejected[] = "The user did not approve access."; |
+const char kUserNotSignedIn[] = "The user is not signed in."; |
} // namespace |
@@ -37,13 +43,34 @@ |
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &arg)); |
arg->GetBoolean("interactive", &interactive_); |
+ const Extension* extension = GetExtension(); |
+ Extension::OAuth2Info oauth2_info = extension->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,6 +106,20 @@ |
} |
} |
+void GetAuthTokenFunction::OnLoginUIShown(LoginUIService::LoginUI* ui) { |
+ // Do nothing when login ui is shown. |
+} |
+ |
+void GetAuthTokenFunction::OnLoginUIClosed(LoginUIService::LoginUI* ui) { |
+ LoginUIService* login_ui_service = |
+ LoginUIServiceFactory::GetForProfile(profile()); |
+ login_ui_service->RemoveObserver(this); |
+ if (!StartFlow(GetTokenFlowMode())) { |
+ SendResponse(false); |
+ Release(); |
+ } |
+} |
+ |
void GetAuthTokenFunction::InstallUIProceed() { |
DCHECK(install_ui_->record_oauth2_grant()); |
// The user has accepted the scopes, so we may now force (recording a grant |
@@ -97,16 +138,11 @@ |
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; |
- return false; |
- } |
- |
TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); |
flow_.reset(new OAuth2MintTokenFlow( |
profile()->GetRequestContext(), |
@@ -121,6 +157,42 @@ |
return true; |
} |
+bool GetAuthTokenFunction::StartLogin() { |
+ if (!interactive_) { |
+ error_ = kUserNotSignedIn; |
+ return false; |
+ } |
+ |
+ LoginUIService* login_ui_service = |
+ LoginUIServiceFactory::GetForProfile(profile()); |
+ login_ui_service->AddObserver(this); |
+ |
+ LoginUIService::LoginUI* login_ui = login_ui_service->current_login_ui(); |
+ if (login_ui) { |
+ login_ui->FocusUI(); |
+ } else { |
+ chrome::NavigateParams params(NULL, |
+ GURL(chrome::kChromeUISyncPromoURL), |
+ content::PAGE_TRANSITION_START_PAGE); |
+ params.profile = profile(); |
+ params.disposition = NEW_POPUP; |
+ chrome::Navigate(¶ms); |
+ } |
+ |
+ return true; |
+} |
+ |
+bool GetAuthTokenFunction::HasLoginToken() const { |
+ TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); |
+ return token_service->HasOAuthLoginToken(); |
+} |
+ |
+OAuth2MintTokenFlow::Mode GetAuthTokenFunction::GetTokenFlowMode() const { |
+ return ExtensionInstallPrompt::ShouldAutomaticallyApproveScopes() ? |
+ OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE : |
Evan Stade
2012/07/13 08:57:33
this indent should be 4 spaces relative to 'return
Munjal (Google)
2012/07/16 20:13:30
Done.
|
+ OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE; |
+} |
+ |
LaunchWebAuthFlowFunction::LaunchWebAuthFlowFunction() {} |
LaunchWebAuthFlowFunction::~LaunchWebAuthFlowFunction() {} |