summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-26 22:02:14 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-26 22:02:14 +0000
commite30e4d816f7e32f2be39f32159e020d3b037de04 (patch)
tree7df7110e76599dc6c3a738567986c4a64f0f7a45
parent19f9aa382283378bb1b6c130511ae6bd873b5453 (diff)
downloadchromium_src-e30e4d816f7e32f2be39f32159e020d3b037de04.zip
chromium_src-e30e4d816f7e32f2be39f32159e020d3b037de04.tar.gz
chromium_src-e30e4d816f7e32f2be39f32159e020d3b037de04.tar.bz2
Classes that implement InfoBarDelegates should contain only what's needed to actually subclass the InfoBarDelegate base classes. If more logic is needed to decide when to show the relevant infobars, then instead of writing a more complex InfoBarDelegate object that is created early and may call AddInfoBar on itself later, split the logic into separate classes, one for the infobar and one for the code that decides when to display the infobar. This is sort of like converting inheritance to composition.
This also does some cleanup, especially on auto_login_prompter.cc, e.g. converting constructors to take classes' direct dependencies, reordering functions to be in the same order in header and .cc file, shorter code, etc. BUG=96876 TEST=none Review URL: http://codereview.chromium.org/8042001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@102814 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/alternate_nav_url_fetcher.cc127
-rw-r--r--chrome/browser/alternate_nav_url_fetcher.h26
-rw-r--r--chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc10
-rw-r--r--chrome/browser/ui/auto_login_prompter.cc (renamed from chrome/browser/ui/autologin_infobar_delegate.cc)290
-rw-r--r--chrome/browser/ui/auto_login_prompter.h57
-rw-r--r--chrome/browser/ui/autologin_infobar_delegate.h68
-rw-r--r--chrome/chrome_browser.gypi4
7 files changed, 319 insertions, 263 deletions
diff --git a/chrome/browser/alternate_nav_url_fetcher.cc b/chrome/browser/alternate_nav_url_fetcher.cc
index 4edac95..a9ede47 100644
--- a/chrome/browser/alternate_nav_url_fetcher.cc
+++ b/chrome/browser/alternate_nav_url_fetcher.cc
@@ -8,6 +8,7 @@
#include "chrome/browser/infobars/infobar_tab_helper.h"
#include "chrome/browser/intranet_redirect_detector.h"
#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/tab_contents/link_infobar_delegate.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "content/browser/tab_contents/navigation_controller.h"
#include "content/browser/tab_contents/navigation_entry.h"
@@ -19,19 +20,88 @@
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
+// AlternateNavInfoBarDelegate ------------------------------------------------
+
+class AlternateNavInfoBarDelegate : public LinkInfoBarDelegate {
+ public:
+ AlternateNavInfoBarDelegate(TabContents* tab_contents,
+ const GURL& alternate_nav_url);
+ virtual ~AlternateNavInfoBarDelegate();
+
+ private:
+ // LinkInfoBarDelegate
+ virtual gfx::Image* GetIcon() const OVERRIDE;
+ virtual Type GetInfoBarType() const OVERRIDE;
+ virtual string16 GetMessageTextWithOffset(size_t* link_offset) const OVERRIDE;
+ virtual string16 GetLinkText() const OVERRIDE;
+ virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE;
+
+ TabContents* tab_contents_;
+ GURL alternate_nav_url_;
+
+ DISALLOW_COPY_AND_ASSIGN(AlternateNavInfoBarDelegate);
+};
+
+AlternateNavInfoBarDelegate::AlternateNavInfoBarDelegate(
+ TabContents* tab_contents,
+ const GURL& alternate_nav_url)
+ : LinkInfoBarDelegate(tab_contents),
+ tab_contents_(tab_contents),
+ alternate_nav_url_(alternate_nav_url) {
+}
+
+AlternateNavInfoBarDelegate::~AlternateNavInfoBarDelegate() {
+}
+
+gfx::Image* AlternateNavInfoBarDelegate::GetIcon() const {
+ return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(
+ IDR_INFOBAR_ALT_NAV_URL);
+}
+
+InfoBarDelegate::Type AlternateNavInfoBarDelegate::GetInfoBarType() const {
+ return PAGE_ACTION_TYPE;
+}
+
+string16 AlternateNavInfoBarDelegate::GetMessageTextWithOffset(
+ size_t* link_offset) const {
+ const string16 label = l10n_util::GetStringFUTF16(
+ IDS_ALTERNATE_NAV_URL_VIEW_LABEL, string16(), link_offset);
+ return label;
+}
+
+string16 AlternateNavInfoBarDelegate::GetLinkText() const {
+ return UTF8ToUTF16(alternate_nav_url_.spec());
+}
+
+bool AlternateNavInfoBarDelegate::LinkClicked(
+ WindowOpenDisposition disposition) {
+ tab_contents_->OpenURL(
+ alternate_nav_url_, GURL(), disposition,
+ // Pretend the user typed this URL, so that navigating to
+ // it will be the default action when it's typed again in
+ // the future.
+ PageTransition::TYPED);
+
+ // We should always close, even if the navigation did not occur within this
+ // TabContents.
+ return true;
+}
+
+
+// AlternateNavURLFetcher -----------------------------------------------------
+
AlternateNavURLFetcher::AlternateNavURLFetcher(
const GURL& alternate_nav_url)
- : LinkInfoBarDelegate(NULL),
- alternate_nav_url_(alternate_nav_url),
+ : alternate_nav_url_(alternate_nav_url),
controller_(NULL),
state_(NOT_STARTED),
- navigated_to_entry_(false),
- infobar_contents_(NULL) {
+ navigated_to_entry_(false) {
registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING,
NotificationService::AllSources());
}
-AlternateNavURLFetcher::~AlternateNavURLFetcher() {}
+AlternateNavURLFetcher::~AlternateNavURLFetcher() {
+}
void AlternateNavURLFetcher::Observe(int type,
const NotificationSource& source,
@@ -42,8 +112,7 @@ void AlternateNavURLFetcher::Observe(int type,
// should delete ourselves as that indicates that the page is being
// re-loaded so this instance is now stale.
// http://crbug.com/43378
- if (!infobar_contents_ &&
- controller_ == Source<NavigationController>(source).ptr()) {
+ if (controller_ == Source<NavigationController>(source).ptr()) {
delete this;
} else if (!controller_) {
controller_ = Source<NavigationController>(source).ptr();
@@ -73,6 +142,7 @@ void AlternateNavURLFetcher::Observe(int type,
Source<NavigationController>(controller_));
navigated_to_entry_ = true;
ShowInfobarIfPossible();
+ // WARNING: |this| may be deleted!
break;
case content::NOTIFICATION_TAB_CLOSED:
@@ -97,39 +167,7 @@ void AlternateNavURLFetcher::OnURLFetchComplete(
DCHECK_EQ(fetcher_.get(), source);
SetStatusFromURLFetch(url, status, response_code);
ShowInfobarIfPossible();
-}
-
-gfx::Image* AlternateNavURLFetcher::GetIcon() const {
- return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(
- IDR_INFOBAR_ALT_NAV_URL);
-}
-
-InfoBarDelegate::Type AlternateNavURLFetcher::GetInfoBarType() const {
- return PAGE_ACTION_TYPE;
-}
-
-string16 AlternateNavURLFetcher::GetMessageTextWithOffset(
- size_t* link_offset) const {
- const string16 label = l10n_util::GetStringFUTF16(
- IDS_ALTERNATE_NAV_URL_VIEW_LABEL, string16(), link_offset);
- return label;
-}
-
-string16 AlternateNavURLFetcher::GetLinkText() const {
- return UTF8ToUTF16(alternate_nav_url_.spec());
-}
-
-bool AlternateNavURLFetcher::LinkClicked(WindowOpenDisposition disposition) {
- infobar_contents_->OpenURL(
- alternate_nav_url_, GURL(), disposition,
- // Pretend the user typed this URL, so that navigating to
- // it will be the default action when it's typed again in
- // the future.
- PageTransition::TYPED);
-
- // We should always close, even if the navigation did not occur within this
- // TabContents.
- return true;
+ // WARNING: |this| may be deleted!
}
void AlternateNavURLFetcher::SetStatusFromURLFetch(
@@ -164,8 +202,9 @@ void AlternateNavURLFetcher::ShowInfobarIfPossible() {
return;
}
- infobar_contents_ = controller_->tab_contents();
- StoreActiveEntryUniqueID(infobar_contents_);
- TabContentsWrapper::GetCurrentWrapperForContents(infobar_contents_)->
- infobar_tab_helper()->AddInfoBar(this);
+ TabContents* tab_contents = controller_->tab_contents();
+ TabContentsWrapper::GetCurrentWrapperForContents(tab_contents)->
+ infobar_tab_helper()->AddInfoBar(new AlternateNavInfoBarDelegate(
+ tab_contents, alternate_nav_url_));
+ delete this;
}
diff --git a/chrome/browser/alternate_nav_url_fetcher.h b/chrome/browser/alternate_nav_url_fetcher.h
index 36a9adb..0f4a295 100644
--- a/chrome/browser/alternate_nav_url_fetcher.h
+++ b/chrome/browser/alternate_nav_url_fetcher.h
@@ -9,7 +9,6 @@
#include <string>
#include "base/memory/scoped_ptr.h"
-#include "chrome/browser/tab_contents/link_infobar_delegate.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_registrar.h"
#include "content/common/net/url_fetcher.h"
@@ -27,11 +26,14 @@ class NavigationController;
// will create us and be responsible for us until we attach as an observer
// after a pending load starts (it will delete us if this doesn't happen).
// Once this pending load starts, we're responsible for deleting ourselves.
-// We'll do this when the load commits, or when the navigation controller
-// itself is deleted.
+// We'll do this in the following cases:
+// * The tab is navigated again once we start listening (so the fetch is no
+// longer useful)
+// * The tab is closed before we show an infobar
+// * The intranet fetch fails
+// * None of the above apply, so we successfully show an infobar
class AlternateNavURLFetcher : public NotificationObserver,
- public URLFetcher::Delegate,
- public LinkInfoBarDelegate {
+ public URLFetcher::Delegate {
public:
enum State {
NOT_STARTED,
@@ -59,13 +61,6 @@ class AlternateNavURLFetcher : public NotificationObserver,
const net::ResponseCookies& cookies,
const std::string& data) OVERRIDE;
- // LinkInfoBarDelegate
- virtual gfx::Image* GetIcon() const OVERRIDE;
- virtual Type GetInfoBarType() const OVERRIDE;
- virtual string16 GetMessageTextWithOffset(size_t* link_offset) const OVERRIDE;
- virtual string16 GetLinkText() const OVERRIDE;
- virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE;
-
// Sets |state_| to either SUCCEEDED or FAILED depending on the result of the
// fetch.
void SetStatusFromURLFetch(const GURL& url,
@@ -73,7 +68,9 @@ class AlternateNavURLFetcher : public NotificationObserver,
int response_code);
// Displays the infobar if all conditions are met (the page has loaded and
- // the fetch of the alternate URL succeeded).
+ // the fetch of the alternate URL succeeded). Unless we're still waiting on
+ // one of the above conditions to finish, this will also delete us, as whether
+ // or not we show an infobar, there is no reason to live further.
void ShowInfobarIfPossible();
GURL alternate_nav_url_;
@@ -82,9 +79,6 @@ class AlternateNavURLFetcher : public NotificationObserver,
State state_;
bool navigated_to_entry_;
- // The TabContents the InfoBarDelegate was added to.
- TabContents* infobar_contents_;
-
NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(AlternateNavURLFetcher);
diff --git a/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc b/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc
index c4b7276..7ef7a06 100644
--- a/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc
+++ b/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc
@@ -19,7 +19,7 @@
#include "chrome/browser/renderer_host/chrome_url_request_user_data.h"
#include "chrome/browser/renderer_host/safe_browsing_resource_handler.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
-#include "chrome/browser/ui/autologin_infobar_delegate.h"
+#include "chrome/browser/ui/auto_login_prompter.h"
#include "chrome/browser/ui/login/login_prompt.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/extensions/user_script.h"
@@ -48,8 +48,8 @@ namespace {
// instant. This empty ResourceDispatcherHostLoginDelegate implementation does
// that.
// TODO: see if we can handle this case more robustly.
-class InstantResourceDispatcherHostLoginDelegate :
- public ResourceDispatcherHostLoginDelegate {
+class InstantResourceDispatcherHostLoginDelegate
+ : public ResourceDispatcherHostLoginDelegate {
public:
InstantResourceDispatcherHostLoginDelegate() {}
@@ -329,8 +329,8 @@ void ChromeResourceDispatcherHostDelegate::OnResponseStarted(
// See if the response contains the X-Auto-Login header. If so, this was
// a request for a login page, and the server is allowing the browser to
// suggest auto-login, if available.
- AutoLoginInfoBarDelegate::ShowIfAutoLoginRequested(request, info->child_id(),
- info->route_id());
+ AutoLoginPrompter::ShowInfoBarIfPossible(request, info->child_id(),
+ info->route_id());
}
void ChromeResourceDispatcherHostDelegate::OnRequestRedirected(
diff --git a/chrome/browser/ui/autologin_infobar_delegate.cc b/chrome/browser/ui/auto_login_prompter.cc
index a50dbea..1725c62 100644
--- a/chrome/browser/ui/autologin_infobar_delegate.cc
+++ b/chrome/browser/ui/auto_login_prompter.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "chrome/browser/ui/autologin_infobar_delegate.h"
+#include "chrome/browser/ui/auto_login_prompter.h"
#include "base/command_line.h"
#include "base/logging.h"
@@ -14,6 +14,7 @@
#include "chrome/browser/net/gaia/token_service.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/tab_contents/confirm_infobar_delegate.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/signin_manager.h"
@@ -38,12 +39,16 @@
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
-// This class holds context information needed while re-issuing service tokens
-// using the TokenService, getting the browser cookies with the TokenAuth API,
-// and finally redirecting the user to the correct page.
+// AutoLoginRedirector --------------------------------------------------------
+
+// This class is created by the AutoLoginInfoBarDelegate when the user wishes to
+// auto-login. It holds context information needed while re-issuing service
+// tokens using the TokenService, gets the browser cookies with the TokenAuth
+// API, and finally redirects the user to the correct page.
class AutoLoginRedirector : public NotificationObserver {
public:
- AutoLoginRedirector(TabContentsWrapper* tab_contents_wrapper,
+ AutoLoginRedirector(TokenService* token_service,
+ NavigationController* navigation_controller,
const std::string& args);
virtual ~AutoLoginRedirector();
@@ -57,7 +62,7 @@ class AutoLoginRedirector : public NotificationObserver {
// to the desired page.
void RedirectToMergeSession(const std::string& token);
- TabContentsWrapper* tab_contents_wrapper_;
+ NavigationController* navigation_controller_;
const std::string args_;
NotificationRegistrar registrar_;
@@ -65,20 +70,22 @@ class AutoLoginRedirector : public NotificationObserver {
};
AutoLoginRedirector::AutoLoginRedirector(
- TabContentsWrapper* tab_contents_wrapper, const std::string& args)
- : tab_contents_wrapper_(tab_contents_wrapper), args_(args) {
+ TokenService* token_service,
+ NavigationController* navigation_controller,
+ const std::string& args)
+ : navigation_controller_(navigation_controller),
+ args_(args) {
// Register to receive notification for new tokens and then force the tokens
// to be re-issued. The token service guarantees to fire either
// TOKEN_AVAILABLE or TOKEN_REQUEST_FAILED, so we will get at least one or
// the other, allow AutoLoginRedirector to delete itself correctly.
- TokenService* service = tab_contents_wrapper_->profile()->GetTokenService();
registrar_.Add(this,
chrome::NOTIFICATION_TOKEN_AVAILABLE,
- Source<TokenService>(service));
+ Source<TokenService>(token_service));
registrar_.Add(this,
chrome::NOTIFICATION_TOKEN_REQUEST_FAILED,
- Source<TokenService>(service));
- service->StartFetchingTokens();
+ Source<TokenService>(token_service));
+ token_service->StartFetchingTokens();
}
AutoLoginRedirector::~AutoLoginRedirector() {
@@ -94,20 +101,16 @@ void AutoLoginRedirector::Observe(int type,
if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) {
TokenService::TokenAvailableDetails* tok_details =
Details<TokenService::TokenAvailableDetails>(details).ptr();
-
if (tok_details->service() == GaiaConstants::kGaiaService) {
RedirectToMergeSession(tok_details->token());
delete this;
- return;
}
} else {
TokenService::TokenRequestFailedDetails* tok_details =
Details<TokenService::TokenRequestFailedDetails>(details).ptr();
-
if (tok_details->service() == GaiaConstants::kGaiaService) {
LOG(WARNING) << "AutoLoginRedirector: token request failed";
delete this;
- return;
}
}
}
@@ -118,31 +121,117 @@ void AutoLoginRedirector::RedirectToMergeSession(const std::string& token) {
url_util::DecodeURLEscapeSequences(args_.c_str(), args_.length(), &output);
std::string unescaped_args;
UTF16ToUTF8(output.data(), output.length(), &unescaped_args);
-
- const char kUrlFormat[] = "%s?"
- "source=chrome&"
- "uberauth=%s&"
- "%s";
- const std::string url_string = GaiaUrls::GetInstance()->merge_session_url();
- std::string url = base::StringPrintf(kUrlFormat,
- url_string.c_str(),
- token.c_str(),
- unescaped_args.c_str());
-
// TODO(rogerta): what is the correct page transition?
- tab_contents_wrapper_->tab_contents()->controller().LoadURL(GURL(url),
+ navigation_controller_->LoadURL(
+ GURL(GaiaUrls::GetInstance()->merge_session_url() +
+ "?source=chrome&uberauth=" + token + "&" + unescaped_args),
GURL(), PageTransition::AUTO_BOOKMARK, std::string());
}
+
+// AutoLoginInfoBarDelegate ---------------------------------------------------
+
+// This is the actual infobar displayed to prompt the user to auto-login.
+class AutoLoginInfoBarDelegate : public ConfirmInfoBarDelegate {
+ public:
+ AutoLoginInfoBarDelegate(TabContents* tab_contents,
+ TokenService* token_service,
+ PrefService* pref_service,
+ const std::string& username,
+ const std::string& args);
+ virtual ~AutoLoginInfoBarDelegate();
+
+ private:
+ // ConfirmInfoBarDelegate overrides.
+ virtual gfx::Image* GetIcon() const OVERRIDE;
+ virtual Type GetInfoBarType() const OVERRIDE;
+ virtual string16 GetMessageText() const OVERRIDE;
+ virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE;
+ virtual bool Accept() OVERRIDE;
+ virtual bool Cancel() OVERRIDE;
+
+ NavigationController* navigation_controller_;
+ TokenService* token_service_;
+ PrefService* pref_service_;
+ std::string username_;
+ std::string args_;
+
+ DISALLOW_COPY_AND_ASSIGN(AutoLoginInfoBarDelegate);
+};
+
+AutoLoginInfoBarDelegate::AutoLoginInfoBarDelegate(TabContents* tab_contents,
+ TokenService* token_service,
+ PrefService* pref_service,
+ const std::string& username,
+ const std::string& args)
+ : ConfirmInfoBarDelegate(tab_contents),
+ navigation_controller_(&tab_contents->controller()),
+ token_service_(token_service),
+ pref_service_(pref_service),
+ username_(username),
+ args_(args) {
+}
+
+AutoLoginInfoBarDelegate::~AutoLoginInfoBarDelegate() {
+}
+
+gfx::Image* AutoLoginInfoBarDelegate::GetIcon() const {
+ return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(
+ IDR_INFOBAR_AUTOLOGIN);
+}
+
+InfoBarDelegate::Type AutoLoginInfoBarDelegate::GetInfoBarType() const {
+ return PAGE_ACTION_TYPE;
+}
+
+string16 AutoLoginInfoBarDelegate::GetMessageText() const {
+ return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE,
+ UTF8ToUTF16(username_));
+}
+
+string16 AutoLoginInfoBarDelegate::GetButtonLabel(
+ InfoBarButton button) const {
+ return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
+ IDS_AUTOLOGIN_INFOBAR_OK_BUTTON : IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON);
+}
+
+bool AutoLoginInfoBarDelegate::Accept() {
+ // AutoLoginRedirector deletes itself.
+ new AutoLoginRedirector(token_service_, navigation_controller_, args_);
+ return true;
+}
+
+bool AutoLoginInfoBarDelegate::Cancel() {
+ pref_service_->SetBoolean(prefs::kAutologinEnabled, false);
+ pref_service_->ScheduleSavePersistentPrefs();
+ return true;
+}
+
+
+// AutoLoginPrompter ----------------------------------------------------------
+
+AutoLoginPrompter::AutoLoginPrompter(
+ TabContents* tab_contents,
+ const std::string& username,
+ const std::string& args)
+ : tab_contents_(tab_contents),
+ username_(username),
+ args_(args) {
+ registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
+ Source<NavigationController>(&tab_contents_->controller()));
+ registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
+ Source<TabContents>(tab_contents_));
+}
+
+AutoLoginPrompter::~AutoLoginPrompter() {
+}
+
// static
-void AutoLoginInfoBarDelegate::ShowIfAutoLoginRequested(
- net::URLRequest* request,
- int child_id,
- int route_id) {
- if (!CommandLine::ForCurrentProcess()->HasSwitch(
- switches::kEnableAutologin)) {
+void AutoLoginPrompter::ShowInfoBarIfPossible(net::URLRequest* request,
+ int child_id,
+ int route_id) {
+ if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableAutologin))
return;
- }
// See if the response contains the X-Auto-Login header. If so, this was
// a request for a login page, and the server is allowing the browser to
@@ -171,129 +260,74 @@ void AutoLoginInfoBarDelegate::ShowIfAutoLoginRequested(
}
}
- // We only accept GAIA credentdials.
+ // Currently we only accept GAIA credentials.
if (realm != "com.google")
return;
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
- NewRunnableFunction(&AutoLoginInfoBarDelegate::ShowInfoBarIfNeeded,
- account, args, child_id, route_id));
+ NewRunnableFunction(&AutoLoginPrompter::ShowInfoBarUIThread, account,
+ args, child_id, route_id));
}
// static
-void AutoLoginInfoBarDelegate::ShowInfoBarIfNeeded(const std::string& account,
- const std::string& args,
- int child_id, int route_id) {
+void AutoLoginPrompter::ShowInfoBarUIThread(const std::string& account,
+ const std::string& args,
+ int child_id,
+ int route_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
TabContents* tab_contents = tab_util::GetTabContentsByID(child_id, route_id);
if (!tab_contents)
return;
- TabContentsWrapper* tab_contents_wrapper =
- TabContentsWrapper::GetCurrentWrapperForContents(tab_contents);
- // tab_contents_wrapper is NULL for TabContents hosted in HTMLDialog.
- if (!tab_contents_wrapper)
- return;
-
// If auto-login is turned off, then simply return.
- if (!tab_contents_wrapper->profile()->GetPrefs()->GetBoolean(
- prefs::kAutologinEnabled))
+ Profile* profile =
+ Profile::FromBrowserContext(tab_contents->browser_context());
+ if (!profile->GetPrefs()->GetBoolean(prefs::kAutologinEnabled))
return;
- // Make sure that the account specified matches the logged in user.
- // However, account is usually empty. In an incognito window, there may
- // not be a profile sync service and/or signin manager.
- if (!tab_contents_wrapper->profile()->HasProfileSyncService())
+ // In an incognito window, there may not be a profile sync service and/or
+ // signin manager.
+ if (!profile->HasProfileSyncService())
return;
-
- SigninManager* signin_manager =
- tab_contents_wrapper->profile()->GetProfileSyncService()->signin();
+ SigninManager* signin_manager = profile->GetProfileSyncService()->signin();
if (!signin_manager)
return;
+ // Make sure that |account|, if specified, matches the logged in user.
+ // However, |account| is usually empty.
const std::string& username = signin_manager->GetUsername();
if (!account.empty() && (username != account))
return;
// Make sure there are credentials in the token manager, otherwise there is
// no way to craft the TokenAuth URL.
- if (!tab_contents_wrapper->profile()->GetTokenService()->
- AreCredentialsValid()) {
+ if (!profile->GetTokenService()->AreCredentialsValid())
return;
- }
-
- // We can't add the infobar just yet, since we need to wait for the tab
- // to finish loading. If we don't, the info bar appears and then disappears
- // immediately. The delegate will eventually be deleted by the TabContents.
- new AutoLoginInfoBarDelegate(tab_contents_wrapper, username, args);
-}
-AutoLoginInfoBarDelegate::AutoLoginInfoBarDelegate(
- TabContentsWrapper* tab_contents_wrapper, const std::string& account,
- const std::string& args)
- : ConfirmInfoBarDelegate(tab_contents_wrapper->tab_contents()),
- tab_contents_wrapper_(tab_contents_wrapper),
- account_(account), args_(args) {
- registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
- Source<NavigationController>(
- &tab_contents_wrapper->tab_contents()->controller()));
- registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
- Source<TabContents>(tab_contents_wrapper->tab_contents()));
-}
-
-AutoLoginInfoBarDelegate::~AutoLoginInfoBarDelegate() {
+ // We can't add the infobar just yet, since we need to wait for the tab to
+ // finish loading. If we don't, the info bar appears and then disappears
+ // immediately. Create an AutoLoginPrompter instance to listen for the
+ // relevant notifications; it will delete itself.
+ new AutoLoginPrompter(tab_contents, username, args);
}
-void AutoLoginInfoBarDelegate::Observe(int type,
- const NotificationSource& source,
- const NotificationDetails& details) {
+void AutoLoginPrompter::Observe(int type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
if (type == content::NOTIFICATION_LOAD_STOP) {
- // The wrapper takes ownership of this delegate.
- tab_contents_wrapper_->infobar_tab_helper()->AddInfoBar(this);
- registrar_.RemoveAll();
- } else if (type == content::NOTIFICATION_TAB_CONTENTS_DESTROYED) {
- // The tab contents was destroyed before the naviagation completed, so
- // just cleanup this delegate.
- delete this;
- } else {
- NOTREACHED() << "Unexpected notification type";
+ TabContentsWrapper* wrapper =
+ TabContentsWrapper::GetCurrentWrapperForContents(tab_contents_);
+ // |wrapper| is NULL for TabContents hosted in HTMLDialog.
+ if (wrapper) {
+ Profile* profile = wrapper->profile();
+ wrapper->infobar_tab_helper()->AddInfoBar(new AutoLoginInfoBarDelegate(
+ tab_contents_, profile->GetTokenService(), profile->GetPrefs(),
+ username_, args_));
+ }
}
-}
-
-gfx::Image* AutoLoginInfoBarDelegate::GetIcon() const {
- return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(
- IDR_INFOBAR_AUTOLOGIN);
-}
-
-InfoBarDelegate::Type AutoLoginInfoBarDelegate::GetInfoBarType() const {
- return PAGE_ACTION_TYPE;
-}
-
-string16 AutoLoginInfoBarDelegate::GetMessageText() const {
- return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE,
- UTF8ToUTF16(account_));
-}
-
-int AutoLoginInfoBarDelegate::GetButtons() const {
- return BUTTON_OK | BUTTON_CANCEL;
-}
-
-string16 AutoLoginInfoBarDelegate::GetButtonLabel(
- InfoBarButton button) const {
- return l10n_util::GetStringUTF16(BUTTON_OK == button ?
- IDS_AUTOLOGIN_INFOBAR_OK_BUTTON : IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON);
-}
-
-bool AutoLoginInfoBarDelegate::Accept() {
- // AutoLoginRedirector auto deletes itself.
- new AutoLoginRedirector(tab_contents_wrapper_, args_);
- return true;
-}
-
-bool AutoLoginInfoBarDelegate::Cancel() {
- PrefService* user_prefs = tab_contents_wrapper_->profile()->GetPrefs();
- user_prefs->SetBoolean(prefs::kAutologinEnabled, false);
- user_prefs->ScheduleSavePersistentPrefs();
- return true;
+ // Either we couldn't add the infobar, we added the infobar, or the tab
+ // contents was destroyed before the navigation completed. In any case
+ // there's no reason to live further.
+ delete this;
}
diff --git a/chrome/browser/ui/auto_login_prompter.h b/chrome/browser/ui/auto_login_prompter.h
new file mode 100644
index 0000000..0cdb1b9
--- /dev/null
+++ b/chrome/browser/ui/auto_login_prompter.h
@@ -0,0 +1,57 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_UI_AUTO_LOGIN_PROMPTER_H_
+#define CHROME_BROWSER_UI_AUTO_LOGIN_PROMPTER_H_
+
+#include <string>
+#include "base/compiler_specific.h"
+#include "content/common/notification_observer.h"
+#include "content/common/notification_registrar.h"
+
+class TabContents;
+namespace net {
+class URLRequest;
+}
+
+// This class displays an infobar that allows the user to automatically login to
+// the currently loaded page with one click. This is used when the browser
+// detects that the user has navigated to a login page and that there are stored
+// tokens that would allow a one-click login.
+class AutoLoginPrompter : public NotificationObserver {
+ public:
+ AutoLoginPrompter(TabContents* tab_contents,
+ const std::string& username,
+ const std::string& args);
+
+ virtual ~AutoLoginPrompter();
+
+ // Looks for the X-Auto-Login response header in the request, and if found,
+ // tries to display an infobar in the tab contents identified by the
+ // child/route id.
+ static void ShowInfoBarIfPossible(net::URLRequest* request,
+ int child_id,
+ int route_id);
+
+ private:
+ // The portion of ShowInfoBarIfPossible() that needs to run on the UI thread.
+ static void ShowInfoBarUIThread(const std::string& account,
+ const std::string& args,
+ int child_id,
+ int route_id);
+
+ // NotificationObserver override.
+ virtual void Observe(int type,
+ const NotificationSource& source,
+ const NotificationDetails& details) OVERRIDE;
+
+ TabContents* tab_contents_;
+ const std::string username_;
+ const std::string args_;
+ NotificationRegistrar registrar_;
+
+ DISALLOW_COPY_AND_ASSIGN(AutoLoginPrompter);
+};
+
+#endif // CHROME_BROWSER_UI_AUTO_LOGIN_PROMPTER_H_
diff --git a/chrome/browser/ui/autologin_infobar_delegate.h b/chrome/browser/ui/autologin_infobar_delegate.h
deleted file mode 100644
index 3fa589f..0000000
--- a/chrome/browser/ui/autologin_infobar_delegate.h
+++ /dev/null
@@ -1,68 +0,0 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_UI_AUTOLOGIN_INFOBAR_DELEGATE_H_
-#define CHROME_BROWSER_UI_AUTOLOGIN_INFOBAR_DELEGATE_H_
-
-#include "chrome/browser/tab_contents/confirm_infobar_delegate.h"
-#include "content/common/notification_observer.h"
-#include "content/common/notification_registrar.h"
-
-class PrefService;
-
-namespace net {
-class URLRequest;
-}
-
-// This class configures an infobar that allows the user to automatically login
-// to the currently loaded page with one click. This is used when the browser
-// detects that the user has navigated to a login page and that there are
-// stored tokens that would allow a one-click login.
-class AutoLoginInfoBarDelegate : public ConfirmInfoBarDelegate,
- public NotificationObserver {
- public:
- AutoLoginInfoBarDelegate(TabContentsWrapper* tab_contents_wrapper,
- const std::string& account,
- const std::string& args);
-
- virtual ~AutoLoginInfoBarDelegate();
-
- // Looks for the X-Auto-Login response header in the request, and if found,
- // displays an infobar in the tab contents identified by the child/route id
- // if possible.
- static void ShowIfAutoLoginRequested(net::URLRequest* request,
- int child_id,
- int route_id);
-
- private:
- // ConfirmInfoBarDelegate overrides.
- virtual gfx::Image* GetIcon() const OVERRIDE;
- virtual Type GetInfoBarType() const OVERRIDE;
- virtual string16 GetMessageText() const OVERRIDE;
- virtual int GetButtons() const OVERRIDE;
- virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE;
- virtual bool Accept() OVERRIDE;
- virtual bool Cancel() OVERRIDE;
-
- // NotificationObserver override.
- virtual void Observe(int type,
- const NotificationSource& source,
- const NotificationDetails& details) OVERRIDE;
-
- // Will display an auto-login infobar in the tab contents that corresponds
- // to the child/route if possible.
- static void ShowInfoBarIfNeeded(const std::string& account,
- const std::string& args,
- int child_id,
- int route_id);
-
- TabContentsWrapper* tab_contents_wrapper_;
- const std::string account_;
- const std::string args_;
- NotificationRegistrar registrar_;
-
- DISALLOW_COPY_AND_ASSIGN(AutoLoginInfoBarDelegate);
-};
-
-#endif // CHROME_BROWSER_UI_AUTOLOGIN_INFOBAR_DELEGATE_H_
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi
index 5097c42..28086a6 100644
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -2290,8 +2290,8 @@
'browser/ui/app_modal_dialogs/message_box_handler.cc',
'browser/ui/app_modal_dialogs/message_box_handler.h',
'browser/ui/app_modal_dialogs/native_app_modal_dialog.h',
- 'browser/ui/autologin_infobar_delegate.cc',
- 'browser/ui/autologin_infobar_delegate.h',
+ 'browser/ui/auto_login_prompter.cc',
+ 'browser/ui/auto_login_prompter.h',
'browser/ui/blocked_content/blocked_content_container.cc',
'browser/ui/blocked_content/blocked_content_container.h',
'browser/ui/blocked_content/blocked_content_tab_helper.cc',