Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Unified Diff: chrome/browser/extensions/api/identity/identity_api.cc

Issue 10701041: implement sign in dialog (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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,23 +12,27 @@
#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 GetAuthTokenFunction::kInvalidClientId[] =
+ "Invalid OAuth2 Client ID.";
+const char GetAuthTokenFunction::kInvalidScopes[] = "Invalid OAuth2 scopes.";
+const char GetAuthTokenFunction::kAuthFailure[] = "OAuth2 request failed: ";
+const char GetAuthTokenFunction::kNoGrant[] = "OAuth2 not granted or revoked.";
+const char GetAuthTokenFunction::kUserRejected[] =
+ "The user did not approve access.";
+const char GetAuthTokenFunction::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
-
GetAuthTokenFunction::GetAuthTokenFunction() : interactive_(false) {}
GetAuthTokenFunction::~GetAuthTokenFunction() {}
@@ -37,13 +41,33 @@
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &arg));
arg->GetBoolean("interactive", &interactive_);
+ const Extension::OAuth2Info& oauth2_info = GetOAuth2Info();
+
+ // 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();
@@ -71,7 +95,7 @@
if (interactive_) {
install_ui_.reset(
chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser()));
- install_ui_->ConfirmIssueAdvice(this, GetExtension(), issue_advice);
+ ShowOAuthApprovalDialog(issue_advice);
} else {
error_ = kNoGrant;
SendResponse(false);
@@ -79,6 +103,14 @@
}
}
+void GetAuthTokenFunction::OnLoginUIClosed(LoginUIService::LoginUI* ui) {
+ StopObservingLoginService();
+ 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
@@ -94,33 +126,98 @@
}
bool GetAuthTokenFunction::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 GetAuthTokenFunction::StartLogin() {
+ if (!interactive_) {
+ error_ = kUserNotSignedIn;
return false;
}
+ ShowLoginPopup();
+ return true;
+}
+
+void GetAuthTokenFunction::StartObservingLoginService() {
+ LoginUIService* login_ui_service =
+ LoginUIServiceFactory::GetForProfile(profile());
+ login_ui_service->AddObserver(this);
+}
+
+void GetAuthTokenFunction::StopObservingLoginService() {
+ LoginUIService* login_ui_service =
+ LoginUIServiceFactory::GetForProfile(profile());
+ login_ui_service->RemoveObserver(this);
+}
+
+void GetAuthTokenFunction::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 {
+ chrome::NavigateParams params(NULL,
+ GURL(chrome::kChromeUISyncPromoURL),
+ content::PAGE_TRANSITION_START_PAGE);
+ params.profile = profile();
+ params.disposition = NEW_POPUP;
+ chrome::Navigate(&params);
+ }
+}
+
+void GetAuthTokenFunction::ShowOAuthApprovalDialog(
+ const IssueAdviceInfo& issue_advice) {
+ install_ui_->ConfirmIssueAdvice(this, GetExtension(), issue_advice);
+}
+
+const std::string& GetAuthTokenFunction::GetExtensionId() const {
+ return GetExtension()->id();
Mihai Parparita -not on Chrome 2012/07/17 01:01:00 You can override the Extension instance used in a
Munjal (Google) 2012/07/19 22:35:42 Done.
+}
+
+const Extension::OAuth2Info& GetAuthTokenFunction::GetOAuth2Info() const {
+ return GetExtension()->oauth2_info();
+}
+
+OAuth2MintTokenFlow* GetAuthTokenFunction::CreateMintTokenFlow(
+ OAuth2MintTokenFlow::Mode mode) {
+ const Extension::OAuth2Info& oauth2_info = GetOAuth2Info();
TokenService* token_service = TokenServiceFactory::GetForProfile(profile());
- flow_.reset(new OAuth2MintTokenFlow(
+ return new OAuth2MintTokenFlow(
profile()->GetRequestContext(),
this,
OAuth2MintTokenFlow::Parameters(
token_service->GetOAuth2LoginRefreshToken(),
- extension->id(),
+ GetExtensionId(),
oauth2_info.client_id,
oauth2_info.scopes,
- mode)));
- flow_->Start();
- return true;
+ mode));
}
+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 :
+ OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE;
+}
+
+const char LaunchWebAuthFlowFunction::kInvalidRedirect[] =
+ "Did not redirect to the right URL.";
+
LaunchWebAuthFlowFunction::LaunchWebAuthFlowFunction() {}
LaunchWebAuthFlowFunction::~LaunchWebAuthFlowFunction() {}

Powered by Google App Engine
This is Rietveld 408576698