diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-08 02:03:50 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-08 02:03:50 +0000 |
commit | 0be0993cca7083adc764540ed17ba7611c23aa5a (patch) | |
tree | 2c7337c84594d00957b6d961afa5ee62a12ff6c5 /chrome/browser | |
parent | d52f52f478d5aa0830cd2b621e464178d2fbaa1f (diff) | |
download | chromium_src-0be0993cca7083adc764540ed17ba7611c23aa5a.zip chromium_src-0be0993cca7083adc764540ed17ba7611c23aa5a.tar.gz chromium_src-0be0993cca7083adc764540ed17ba7611c23aa5a.tar.bz2 |
Change infobar creation to use a public static Create() method on the infobar delegate classes. Make constructors as private as possible.
This has several purposes:
* By preventing direct instantiation, it prevents callers from leaking if they create an infobar and don't add it to an InfoBarService.
* By moving decision-making about when to show infobars into these Create() functions, there's a pattern for where such code should go, and caller code becomes simpler and easier to read.
* The two bullets above together mean that for infobars which should only be displayed in certain circumstances, code can't accidentally bypass the decision logic.
* It enables us to eliminate a common InfoBarService* temp on the caller side since the caller no longer needs to both pass the pointer to the infobar _and_ call AddInfoBar() on the pointer. This was also a somewhat redundant-looking pattern.
* It makes it easier to change the ownership model for infobars in the future by limiting the affected callsites to only the Create() functions.
Note that right now, this still feels pretty redundant since we pass all the same args to Create() functions as constructors most times. In the new ownership model constructors will no longer need to take InfoBarService*s, which will make this better.
Additionally, this makes AddInfoBar()/ReplaceInfoBar() take scoped_ptr<>s to indicate they're receiving ownership. This sort of change is easy to make since we only need change the create functions.
This change also has a functional effect: it eliminates some cases where we tried to only show infobars when no other infobars were already showing (discussed and approved by Glen).
BUG=none
TEST=none
Review URL: https://codereview.chromium.org/11644059
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@175467 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
105 files changed, 1493 insertions, 1379 deletions
diff --git a/chrome/browser/accessibility/accessibility_extension_apitest.cc b/chrome/browser/accessibility/accessibility_extension_apitest.cc index 2c224ba..3bcd98f 100644 --- a/chrome/browser/accessibility/accessibility_extension_apitest.cc +++ b/chrome/browser/accessibility/accessibility_extension_apitest.cc @@ -24,11 +24,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_GetAlertsForTab) { ASSERT_TRUE(infobar_service); const char kAlertMessage[] = "Simple Alert Infobar."; - infobar_service->AddInfoBar( - new SimpleAlertInfoBarDelegate(infobar_service, + SimpleAlertInfoBarDelegate::Create(infobar_service, NULL, ASCIIToUTF16(kAlertMessage), - false)); + false); CommandLine::ForCurrentProcess()->AppendSwitch( switches::kEnableExperimentalExtensionApis); ASSERT_TRUE(RunExtensionTest("accessibility/get_alerts_for_tab")) << message_; diff --git a/chrome/browser/alternate_nav_url_fetcher.cc b/chrome/browser/alternate_nav_url_fetcher.cc index d52a4d7..4915f9e 100644 --- a/chrome/browser/alternate_nav_url_fetcher.cc +++ b/chrome/browser/alternate_nav_url_fetcher.cc @@ -142,10 +142,9 @@ void AlternateNavURLFetcher::SetStatusFromURLFetch( void AlternateNavURLFetcher::ShowInfobarIfPossible() { if (navigated_to_entry_ && (state_ == SUCCEEDED)) { - InfoBarService* infobar_service = - InfoBarService::FromWebContents(controller_->GetWebContents()); - infobar_service->AddInfoBar( - new AlternateNavInfoBarDelegate(infobar_service, alternate_nav_url_)); + AlternateNavInfoBarDelegate::Create( + InfoBarService::FromWebContents(controller_->GetWebContents()), + alternate_nav_url_); } else if (state_ != FAILED) { return; } diff --git a/chrome/browser/api/infobars/confirm_infobar_delegate.cc b/chrome/browser/api/infobars/confirm_infobar_delegate.cc index fa1f6b3..620ce80 100644 --- a/chrome/browser/api/infobars/confirm_infobar_delegate.cc +++ b/chrome/browser/api/infobars/confirm_infobar_delegate.cc @@ -8,6 +8,9 @@ #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" +ConfirmInfoBarDelegate::~ConfirmInfoBarDelegate() { +} + InfoBarDelegate::InfoBarAutomationType ConfirmInfoBarDelegate::GetInfoBarAutomationType() const { return CONFIRM_INFOBAR; @@ -46,9 +49,6 @@ ConfirmInfoBarDelegate::ConfirmInfoBarDelegate( : InfoBarDelegate(infobar_service) { } -ConfirmInfoBarDelegate::~ConfirmInfoBarDelegate() { -} - bool ConfirmInfoBarDelegate::EqualsDelegate(InfoBarDelegate* delegate) const { ConfirmInfoBarDelegate* confirm_delegate = delegate->AsConfirmInfoBarDelegate(); diff --git a/chrome/browser/api/infobars/confirm_infobar_delegate.h b/chrome/browser/api/infobars/confirm_infobar_delegate.h index ad9553d..845b5cc 100644 --- a/chrome/browser/api/infobars/confirm_infobar_delegate.h +++ b/chrome/browser/api/infobars/confirm_infobar_delegate.h @@ -20,6 +20,8 @@ class ConfirmInfoBarDelegate : public InfoBarDelegate { BUTTON_CANCEL = 1 << 1, }; + virtual ~ConfirmInfoBarDelegate(); + // Returns the InfoBar type to be displayed for the InfoBar. virtual InfoBarAutomationType GetInfoBarAutomationType() const OVERRIDE; @@ -59,7 +61,6 @@ class ConfirmInfoBarDelegate : public InfoBarDelegate { protected: explicit ConfirmInfoBarDelegate(InfoBarService* infobar_service); - virtual ~ConfirmInfoBarDelegate(); virtual bool ShouldExpireInternal( const content::LoadCommittedDetails& details) const OVERRIDE; diff --git a/chrome/browser/api/infobars/infobar_delegate.cc b/chrome/browser/api/infobars/infobar_delegate.cc index 198a820..16a63f7 100644 --- a/chrome/browser/api/infobars/infobar_delegate.cc +++ b/chrome/browser/api/infobars/infobar_delegate.cc @@ -39,10 +39,6 @@ bool InfoBarDelegate::ShouldExpire( void InfoBarDelegate::InfoBarDismissed() { } -void InfoBarDelegate::InfoBarClosed() { - delete this; -} - gfx::Image* InfoBarDelegate::GetIcon() const { return NULL; } diff --git a/chrome/browser/api/infobars/infobar_delegate.h b/chrome/browser/api/infobars/infobar_delegate.h index c600301..f0d540b 100644 --- a/chrome/browser/api/infobars/infobar_delegate.h +++ b/chrome/browser/api/infobars/infobar_delegate.h @@ -84,10 +84,6 @@ class InfoBarDelegate { // Called when the user clicks on the close button to dismiss the infobar. virtual void InfoBarDismissed(); - // Called after the InfoBar is closed. Deletes |this|. - // TODO(pkasting): Get rid of this and delete delegates directly. - void InfoBarClosed(); - // Return the icon to be shown for this InfoBar. If the returned Image is // NULL, no icon is shown. virtual gfx::Image* GetIcon() const; diff --git a/chrome/browser/api/infobars/infobar_service.h b/chrome/browser/api/infobars/infobar_service.h index c58353c..e817a56 100644 --- a/chrome/browser/api/infobars/infobar_service.h +++ b/chrome/browser/api/infobars/infobar_service.h @@ -5,7 +5,7 @@ #ifndef CHROME_BROWSER_API_INFOBARS_INFOBAR_SERVICE_H_ #define CHROME_BROWSER_API_INFOBARS_INFOBAR_SERVICE_H_ -#include <cstddef> +#include "base/memory/scoped_ptr.h" namespace content { class WebContents; @@ -35,8 +35,8 @@ class InfoBarService { // which returns true for InfoBarDelegate::EqualsDelegate(delegate), // |delegate| is closed immediately without being added. // - // Returns whether |delegate| was successfully added. - virtual bool AddInfoBar(InfoBarDelegate* delegate) = 0; + // Returns the delegate if it was successfully added. + virtual InfoBarDelegate* AddInfoBar(scoped_ptr<InfoBarDelegate> delegate) = 0; // Removes the InfoBar for the specified |delegate|. // @@ -50,16 +50,18 @@ class InfoBarService { // If infobars are disabled for this tab, |new_delegate| is closed immediately // without being added, and nothing else happens. // - // Returns whether |new_delegate| was successfully added. + // Returns the new delegate if it was successfully added. // // NOTE: This does not perform any EqualsDelegate() checks like AddInfoBar(). - virtual bool ReplaceInfoBar(InfoBarDelegate* old_delegate, - InfoBarDelegate* new_delegate) = 0; + virtual InfoBarDelegate* ReplaceInfoBar( + InfoBarDelegate* old_delegate, + scoped_ptr<InfoBarDelegate> new_delegate) = 0; // Returns the number of infobars for this tab. virtual size_t GetInfoBarCount() const = 0; - // Returns the infobar at the given |index|. + // Returns the infobar delegate at the given |index|. The InfoBarService + // retains ownership. // // Warning: Does not sanity check |index|. virtual InfoBarDelegate* GetInfoBarDelegateAt(size_t index) = 0; diff --git a/chrome/browser/api/infobars/simple_alert_infobar_delegate.cc b/chrome/browser/api/infobars/simple_alert_infobar_delegate.cc index e36a065..c099944 100644 --- a/chrome/browser/api/infobars/simple_alert_infobar_delegate.cc +++ b/chrome/browser/api/infobars/simple_alert_infobar_delegate.cc @@ -4,8 +4,19 @@ #include "chrome/browser/api/infobars/simple_alert_infobar_delegate.h" +#include "chrome/browser/api/infobars/infobar_service.h" #include "third_party/skia/include/core/SkBitmap.h" +// static +void SimpleAlertInfoBarDelegate::Create(InfoBarService* infobar_service, + gfx::Image* icon, + const string16& message, + bool auto_expire) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new SimpleAlertInfoBarDelegate(infobar_service, icon, message, + auto_expire))); +} + SimpleAlertInfoBarDelegate::SimpleAlertInfoBarDelegate( InfoBarService* infobar_service, gfx::Image* icon, diff --git a/chrome/browser/api/infobars/simple_alert_infobar_delegate.h b/chrome/browser/api/infobars/simple_alert_infobar_delegate.h index 6928ac8..ed5b656 100644 --- a/chrome/browser/api/infobars/simple_alert_infobar_delegate.h +++ b/chrome/browser/api/infobars/simple_alert_infobar_delegate.h @@ -12,12 +12,17 @@ class SimpleAlertInfoBarDelegate : public ConfirmInfoBarDelegate { public: + // Creates a simple alert delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + gfx::Image* icon, // May be NULL. + const string16& message, + bool auto_expire); + + private: SimpleAlertInfoBarDelegate(InfoBarService* infobar_service, - gfx::Image* icon, // May be NULL. + gfx::Image* icon, const string16& message, bool auto_expire); - - private: virtual ~SimpleAlertInfoBarDelegate(); // ConfirmInfoBarDelegate: diff --git a/chrome/browser/autofill/autofill_cc_infobar_delegate.cc b/chrome/browser/autofill/autofill_cc_infobar_delegate.cc index 342ca10..8ecc497 100644 --- a/chrome/browser/autofill/autofill_cc_infobar_delegate.cc +++ b/chrome/browser/autofill/autofill_cc_infobar_delegate.cc @@ -17,6 +17,17 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" +// static +void AutofillCCInfoBarDelegate::Create( + InfoBarService* infobar_service, + const CreditCard* credit_card, + PersonalDataManager* personal_data, + const AutofillMetrics* metric_logger) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new AutofillCCInfoBarDelegate(infobar_service, credit_card, personal_data, + metric_logger))); +} + AutofillCCInfoBarDelegate::AutofillCCInfoBarDelegate( InfoBarService* infobar_service, const CreditCard* credit_card, diff --git a/chrome/browser/autofill/autofill_cc_infobar_delegate.h b/chrome/browser/autofill/autofill_cc_infobar_delegate.h index ffd18e8..bf15d39 100644 --- a/chrome/browser/autofill/autofill_cc_infobar_delegate.h +++ b/chrome/browser/autofill/autofill_cc_infobar_delegate.h @@ -24,12 +24,27 @@ struct LoadCommittedDetails; // card information gathered from a form submission. class AutofillCCInfoBarDelegate : public ConfirmInfoBarDelegate { public: + // Creates an autofill credit card delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + const CreditCard* credit_card, + PersonalDataManager* personal_data, + const AutofillMetrics* metric_logger); + +#if defined(UNIT_TEST) + static scoped_ptr<ConfirmInfoBarDelegate> Create( + const CreditCard* credit_card, + PersonalDataManager* personal_data, + const AutofillMetrics* metric_logger) { + return scoped_ptr<ConfirmInfoBarDelegate>(new AutofillCCInfoBarDelegate( + NULL, credit_card, personal_data, metric_logger)); + } +#endif + + private: AutofillCCInfoBarDelegate(InfoBarService* infobar_service, const CreditCard* credit_card, PersonalDataManager* personal_data, const AutofillMetrics* metric_logger); - - private: virtual ~AutofillCCInfoBarDelegate(); void LogUserAction(AutofillMetrics::InfoBarMetric user_action); diff --git a/chrome/browser/autofill/autofill_manager.cc b/chrome/browser/autofill/autofill_manager.cc index c4b2f13..4bba52b 100644 --- a/chrome/browser/autofill/autofill_manager.cc +++ b/chrome/browser/autofill/autofill_manager.cc @@ -869,12 +869,8 @@ void AutofillManager::ImportFormData(const FormStructure& submitted_form) { // it. scoped_ptr<const CreditCard> scoped_credit_card(imported_credit_card); if (imported_credit_card && web_contents()) { - InfoBarService* infobar_service = manager_delegate_->GetInfoBarService(); - infobar_service->AddInfoBar( - new AutofillCCInfoBarDelegate(infobar_service, - scoped_credit_card.release(), - personal_data_, - metric_logger_.get())); + AutofillCCInfoBarDelegate::Create(manager_delegate_->GetInfoBarService(), + scoped_credit_card.release(), personal_data_, metric_logger_.get()); } } diff --git a/chrome/browser/autofill/autofill_metrics_unittest.cc b/chrome/browser/autofill/autofill_metrics_unittest.cc index 7b6c2e0..40fb33f 100644 --- a/chrome/browser/autofill/autofill_metrics_unittest.cc +++ b/chrome/browser/autofill/autofill_metrics_unittest.cc @@ -8,7 +8,6 @@ #include "base/string16.h" #include "base/time.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/api/infobars/infobar_service.h" #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" #include "chrome/browser/autofill/autofill_common_test.h" #include "chrome/browser/autofill/autofill_manager.h" @@ -269,8 +268,9 @@ class AutofillMetricsTest : public ChromeRenderViewHostTestHarness { virtual void TearDown() OVERRIDE; protected: - AutofillCCInfoBarDelegate* CreateDelegate(MockAutofillMetrics* metric_logger, - CreditCard** created_card); + scoped_ptr<ConfirmInfoBarDelegate> CreateDelegate( + MockAutofillMetrics* metric_logger, + CreditCard** created_card); content::TestBrowserThread ui_thread_; content::TestBrowserThread file_thread_; @@ -334,7 +334,7 @@ void AutofillMetricsTest::TearDown() { ChromeRenderViewHostTestHarness::TearDown(); } -AutofillCCInfoBarDelegate* AutofillMetricsTest::CreateDelegate( +scoped_ptr<ConfirmInfoBarDelegate> AutofillMetricsTest::CreateDelegate( MockAutofillMetrics* metric_logger, CreditCard** created_card) { EXPECT_CALL(*metric_logger, @@ -343,11 +343,8 @@ AutofillCCInfoBarDelegate* AutofillMetricsTest::CreateDelegate( CreditCard* credit_card = new CreditCard(); if (created_card) *created_card = credit_card; - return new AutofillCCInfoBarDelegate( - InfoBarService::FromWebContents(web_contents()), - credit_card, - &personal_data_, - metric_logger); + return AutofillCCInfoBarDelegate::Create(credit_card, &personal_data_, + metric_logger); } // Test that we log quality metrics appropriately. @@ -1138,37 +1135,40 @@ TEST_F(AutofillMetricsTest, AutofillIsEnabledAtPageLoad) { // Test that credit card infobar metrics are logged correctly. TEST_F(AutofillMetricsTest, CreditCardInfoBar) { - InfoBarService::CreateForWebContents(web_contents()); - MockAutofillMetrics metric_logger; ::testing::InSequence dummy; // Accept the infobar. { CreditCard* credit_card; - scoped_ptr<InfoBarDelegate> infobar(CreateDelegate(&metric_logger, - &credit_card)); + scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger, + &credit_card)); + ASSERT_TRUE(infobar); EXPECT_CALL(personal_data_, SaveImportedCreditCard(*credit_card)); EXPECT_CALL(metric_logger, LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_ACCEPTED)).Times(1); EXPECT_CALL(metric_logger, LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)).Times(0); - EXPECT_TRUE(static_cast<ConfirmInfoBarDelegate*>(infobar.get())->Accept()); + EXPECT_TRUE(infobar->Accept()); } // Cancel the infobar. { - scoped_ptr<InfoBarDelegate> infobar(CreateDelegate(&metric_logger, NULL)); + scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger, + NULL)); + ASSERT_TRUE(infobar); EXPECT_CALL(metric_logger, LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED)).Times(1); EXPECT_CALL(metric_logger, LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)).Times(0); - EXPECT_TRUE(static_cast<ConfirmInfoBarDelegate*>(infobar.get())->Cancel()); + EXPECT_TRUE(infobar->Cancel()); } // Dismiss the infobar. { - scoped_ptr<InfoBarDelegate> infobar(CreateDelegate(&metric_logger, NULL)); + scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger, + NULL)); + ASSERT_TRUE(infobar); EXPECT_CALL(metric_logger, LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED)).Times(1); EXPECT_CALL(metric_logger, @@ -1178,7 +1178,9 @@ TEST_F(AutofillMetricsTest, CreditCardInfoBar) { // Ignore the infobar. { - scoped_ptr<InfoBarDelegate> infobar(CreateDelegate(&metric_logger, NULL)); + scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger, + NULL)); + ASSERT_TRUE(infobar); EXPECT_CALL(metric_logger, LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)).Times(1); } diff --git a/chrome/browser/chrome_quota_permission_context.cc b/chrome/browser/chrome_quota_permission_context.cc index 8048389..84b3c0c 100644 --- a/chrome/browser/chrome_quota_permission_context.cc +++ b/chrome/browser/chrome_quota_permission_context.cc @@ -38,6 +38,16 @@ class RequestQuotaInfoBarDelegate : public ConfirmInfoBarDelegate { public: typedef QuotaPermissionContext::PermissionCallback PermissionCallback; + // Creates a request quota infobar delegate and adds it to |infobar_service|. + static void Create( + InfoBarService* infobar_service, + ChromeQuotaPermissionContext* context, + const GURL& origin_url, + int64 requested_quota, + const std::string& display_languages, + const PermissionCallback& callback); + + private: RequestQuotaInfoBarDelegate( InfoBarService* infobar_service, ChromeQuotaPermissionContext* context, @@ -52,7 +62,6 @@ class RequestQuotaInfoBarDelegate : public ConfirmInfoBarDelegate { requested_quota_(requested_quota), callback_(callback) {} - private: virtual ~RequestQuotaInfoBarDelegate() { if (!callback_.is_null()) context_->DispatchCallbackOnIOThread( @@ -78,6 +87,20 @@ class RequestQuotaInfoBarDelegate : public ConfirmInfoBarDelegate { DISALLOW_COPY_AND_ASSIGN(RequestQuotaInfoBarDelegate); }; +// static +void RequestQuotaInfoBarDelegate::Create( + InfoBarService* infobar_service, + ChromeQuotaPermissionContext* context, + const GURL& origin_url, + int64 requested_quota, + const std::string& display_languages, + const QuotaPermissionContext::PermissionCallback& callback) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new RequestQuotaInfoBarDelegate(infobar_service, context, origin_url, + requested_quota, display_languages, + callback))); +} + string16 RequestQuotaInfoBarDelegate::GetMessageText() const { return l10n_util::GetStringFUTF16( (requested_quota_ > kRequestLargeQuotaThreshold ? @@ -148,10 +171,10 @@ void ChromeQuotaPermissionContext::RequestQuotaPermission( } Profile* profile = Profile::FromBrowserContext(web_contents->GetBrowserContext()); - infobar_service->AddInfoBar(new RequestQuotaInfoBarDelegate( + RequestQuotaInfoBarDelegate::Create( infobar_service, this, origin_url, requested_quota, profile->GetPrefs()->GetString(prefs::kAcceptLanguages), - callback)); + callback); } void ChromeQuotaPermissionContext::DispatchCallbackOnIOThread( diff --git a/chrome/browser/chromeos/login/webui_login_view.cc b/chrome/browser/chromeos/login/webui_login_view.cc index a02e4e9..d70b344 100644 --- a/chrome/browser/chromeos/login/webui_login_view.cc +++ b/chrome/browser/chromeos/login/webui_login_view.cc @@ -17,11 +17,11 @@ #include "chrome/browser/chromeos/login/base_login_display_host.h" #include "chrome/browser/chromeos/login/proxy_settings_dialog.h" #include "chrome/browser/chromeos/login/webui_login_display.h" -#include "chrome/browser/media/media_stream_devices_controller.h" #include "chrome/browser/password_manager/password_manager.h" #include "chrome/browser/password_manager/password_manager_delegate_impl.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/renderer_preferences_util.h" +#include "chrome/browser/ui/media_stream_infobar_delegate.h" #include "chrome/browser/ui/web_contents_modal_dialog_manager.h" #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h" #include "chrome/common/chrome_notification_types.h" @@ -350,12 +350,7 @@ void WebUILoginView::RequestMediaAccessPermission( WebContents* web_contents, const content::MediaStreamRequest& request, const content::MediaResponseCallback& callback) { - Profile* profile = - Profile::FromBrowserContext(web_contents->GetBrowserContext()); - - scoped_ptr<MediaStreamDevicesController> controller( - new MediaStreamDevicesController(profile, request, callback)); - if (!controller->DismissInfoBarAndTakeActionOnSettings()) + if (MediaStreamInfoBarDelegate::Create(web_contents, request, callback)) NOTREACHED() << "Media stream not allowed for WebUI"; } diff --git a/chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.cc b/chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.cc index 0f62ce0..52c2a94 100644 --- a/chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.cc +++ b/chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.cc @@ -17,13 +17,30 @@ using content::OpenURLParams; using content::Referrer; using content::UserMetricsAction; -RegisterProtocolHandlerInfoBarDelegate::RegisterProtocolHandlerInfoBarDelegate( +// static +void RegisterProtocolHandlerInfoBarDelegate::Create( InfoBarService* infobar_service, ProtocolHandlerRegistry* registry, - const ProtocolHandler& handler) - : ConfirmInfoBarDelegate(infobar_service), - registry_(registry), - handler_(handler) { + const ProtocolHandler& handler) { + content::RecordAction( + content::UserMetricsAction("RegisterProtocolHandler.InfoBar_Shown")); + + scoped_ptr<InfoBarDelegate> infobar( + new RegisterProtocolHandlerInfoBarDelegate(infobar_service, registry, + handler)); + + for (size_t i = 0; i < infobar_service->GetInfoBarCount(); ++i) { + RegisterProtocolHandlerInfoBarDelegate* existing_delegate = + infobar_service->GetInfoBarDelegateAt(i)-> + AsRegisterProtocolHandlerInfoBarDelegate(); + if ((existing_delegate != NULL) && + existing_delegate->handler_.IsEquivalent(handler)) { + infobar_service->ReplaceInfoBar(existing_delegate, infobar.Pass()); + return; + } + } + + infobar_service->AddInfoBar(infobar.Pass()); } InfoBarDelegate::InfoBarAutomationType @@ -47,6 +64,15 @@ string16 RegisterProtocolHandlerInfoBarDelegate::GetMessageText() const { GetProtocolName(handler_)); } +RegisterProtocolHandlerInfoBarDelegate::RegisterProtocolHandlerInfoBarDelegate( + InfoBarService* infobar_service, + ProtocolHandlerRegistry* registry, + const ProtocolHandler& handler) + : ConfirmInfoBarDelegate(infobar_service), + registry_(registry), + handler_(handler) { +} + string16 RegisterProtocolHandlerInfoBarDelegate::GetProtocolName( const ProtocolHandler& handler) const { if (handler.protocol() == "mailto") @@ -101,11 +127,6 @@ bool RegisterProtocolHandlerInfoBarDelegate::LinkClicked( return false; } -bool RegisterProtocolHandlerInfoBarDelegate::IsReplacedBy( - RegisterProtocolHandlerInfoBarDelegate* delegate) { - return handler_.IsEquivalent(delegate->handler_); -} - RegisterProtocolHandlerInfoBarDelegate* RegisterProtocolHandlerInfoBarDelegate:: AsRegisterProtocolHandlerInfoBarDelegate() { diff --git a/chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.h b/chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.h index d319d3c..7ea1d4e 100644 --- a/chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.h +++ b/chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.h @@ -16,9 +16,12 @@ class ProtocolHandlerRegistry; // card information gathered from a form submission. class RegisterProtocolHandlerInfoBarDelegate : public ConfirmInfoBarDelegate { public: - RegisterProtocolHandlerInfoBarDelegate(InfoBarService* infobar_service, - ProtocolHandlerRegistry* registry, - const ProtocolHandler& handler); + // Creates a new RPH delegate. Searches |infobar_service| for an existing + // delegate for the same |handler|; replaces it with the new delegate if + // found, otherwise adds the new infobar to |infobar_service|. + static void Create(InfoBarService* infobar_service, + ProtocolHandlerRegistry* registry, + const ProtocolHandler& handler); // ConfirmInfoBarDelegate: virtual Type GetInfoBarType() const OVERRIDE; @@ -35,9 +38,11 @@ class RegisterProtocolHandlerInfoBarDelegate : public ConfirmInfoBarDelegate { virtual InfoBarAutomationType GetInfoBarAutomationType() const OVERRIDE; - bool IsReplacedBy(RegisterProtocolHandlerInfoBarDelegate* delegate); - private: + RegisterProtocolHandlerInfoBarDelegate(InfoBarService* infobar_service, + ProtocolHandlerRegistry* registry, + const ProtocolHandler& handler); + // Returns a user-friendly name for the protocol of this protocol handler. string16 GetProtocolName(const ProtocolHandler& handler) const; ProtocolHandlerRegistry* registry_; diff --git a/chrome/browser/download/download_request_infobar_delegate.cc b/chrome/browser/download/download_request_infobar_delegate.cc index 18184e4..b27ebb8 100644 --- a/chrome/browser/download/download_request_infobar_delegate.cc +++ b/chrome/browser/download/download_request_infobar_delegate.cc @@ -10,6 +10,19 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" +DownloadRequestInfoBarDelegate::~DownloadRequestInfoBarDelegate() { + if (host_) + host_->Cancel(); +} + +// static +void DownloadRequestInfoBarDelegate::Create( + InfoBarService* infobar_service, + DownloadRequestLimiter::TabDownloadState* host) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new DownloadRequestInfoBarDelegate(infobar_service, host))); +} + DownloadRequestInfoBarDelegate::DownloadRequestInfoBarDelegate( InfoBarService* infobar_service, DownloadRequestLimiter::TabDownloadState* host) @@ -17,11 +30,6 @@ DownloadRequestInfoBarDelegate::DownloadRequestInfoBarDelegate( host_(host) { } -DownloadRequestInfoBarDelegate::~DownloadRequestInfoBarDelegate() { - if (host_) - host_->Cancel(); -} - gfx::Image* DownloadRequestInfoBarDelegate::GetIcon() const { return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( IDR_INFOBAR_MULTIPLE_DOWNLOADS); diff --git a/chrome/browser/download/download_request_infobar_delegate.h b/chrome/browser/download/download_request_infobar_delegate.h index 1beaea2..e4a9ab0 100644 --- a/chrome/browser/download/download_request_infobar_delegate.h +++ b/chrome/browser/download/download_request_infobar_delegate.h @@ -17,16 +17,28 @@ class InfoBarService; // on an unsuspecting user. class DownloadRequestInfoBarDelegate : public ConfirmInfoBarDelegate { public: - DownloadRequestInfoBarDelegate( - InfoBarService* infobar_service, - DownloadRequestLimiter::TabDownloadState* host); + virtual ~DownloadRequestInfoBarDelegate(); + + // Creates a download request delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + DownloadRequestLimiter::TabDownloadState* host); + +#if defined(UNIT_TEST) + static scoped_ptr<DownloadRequestInfoBarDelegate> Create( + DownloadRequestLimiter::TabDownloadState* host) { + return scoped_ptr<DownloadRequestInfoBarDelegate>( + new DownloadRequestInfoBarDelegate(NULL, host)); + } +#endif void set_host(DownloadRequestLimiter::TabDownloadState* host) { host_ = host; } private: - virtual ~DownloadRequestInfoBarDelegate(); + DownloadRequestInfoBarDelegate( + InfoBarService* infobar_service, + DownloadRequestLimiter::TabDownloadState* host); // ConfirmInfoBarDelegate: virtual gfx::Image* GetIcon() const OVERRIDE; diff --git a/chrome/browser/download/download_request_infobar_delegate_unittest.cc b/chrome/browser/download/download_request_infobar_delegate_unittest.cc index fdaab1a..d8e070a 100644 --- a/chrome/browser/download/download_request_infobar_delegate_unittest.cc +++ b/chrome/browser/download/download_request_infobar_delegate_unittest.cc @@ -18,23 +18,14 @@ class MockTabDownloadState : public DownloadRequestLimiter::TabDownloadState { virtual void Cancel(); virtual void Accept(); - ConfirmInfoBarDelegate* infobar() { - return infobar_->AsConfirmInfoBarDelegate(); - } - void close_infobar() { - // TODO(pkasting): Right now InfoBarDelegates delete themselves via - // InfoBarClosed(); once InfoBars own their delegates, this can become a - // simple reset() call and ~MockTabDownloadState() will no longer need to - // call it. - if (infobar_ != NULL) - infobar_.release()->InfoBarClosed(); - } + ConfirmInfoBarDelegate* infobar() { return infobar_.get(); } + void delete_infobar_delegate() { infobar_.reset(); } bool responded() const { return responded_; } bool accepted() const { return accepted_; } private: // The actual infobar delegate we're listening to. - scoped_ptr<InfoBarDelegate> infobar_; + scoped_ptr<DownloadRequestInfoBarDelegate> infobar_; // True if we have gotten some sort of response. bool responded_; @@ -45,12 +36,12 @@ class MockTabDownloadState : public DownloadRequestLimiter::TabDownloadState { }; MockTabDownloadState::MockTabDownloadState() - : responded_(false), accepted_(false) { - infobar_.reset(new DownloadRequestInfoBarDelegate(NULL, this)); + : infobar_(DownloadRequestInfoBarDelegate::Create(this)), + responded_(false), + accepted_(false) { } MockTabDownloadState::~MockTabDownloadState() { - close_infobar(); EXPECT_TRUE(responded_); } @@ -64,7 +55,7 @@ void MockTabDownloadState::Accept() { EXPECT_FALSE(responded_); responded_ = true; accepted_ = true; - static_cast<DownloadRequestInfoBarDelegate*>(infobar_.get())->set_host(NULL); + infobar_->set_host(NULL); } @@ -73,19 +64,19 @@ void MockTabDownloadState::Accept() { TEST(DownloadRequestInfobarDelegate, AcceptTest) { MockTabDownloadState state; if (state.infobar()->Accept()) - state.close_infobar(); + state.delete_infobar_delegate(); EXPECT_TRUE(state.accepted()); } TEST(DownloadRequestInfobarDelegate, CancelTest) { MockTabDownloadState state; if (state.infobar()->Cancel()) - state.close_infobar(); + state.delete_infobar_delegate(); EXPECT_FALSE(state.accepted()); } TEST(DownloadRequestInfobarDelegate, CloseTest) { MockTabDownloadState state; - state.close_infobar(); + state.delete_infobar_delegate(); EXPECT_FALSE(state.accepted()); } diff --git a/chrome/browser/download/download_request_limiter.cc b/chrome/browser/download/download_request_limiter.cc index 2e93af8..1b7d08b 100644 --- a/chrome/browser/download/download_request_limiter.cc +++ b/chrome/browser/download/download_request_limiter.cc @@ -106,8 +106,7 @@ void DownloadRequestLimiter::TabDownloadState::PromptUserForDownload( Cancel(); return; } - infobar_ = new DownloadRequestInfoBarDelegate(infobar_service, this); - infobar_service->AddInfoBar(infobar_); + DownloadRequestInfoBarDelegate::Create(infobar_service, this); } void DownloadRequestLimiter::TabDownloadState::Cancel() { diff --git a/chrome/browser/extensions/api/debugger/debugger_api.cc b/chrome/browser/extensions/api/debugger/debugger_api.cc index e3940d3..79b28b7 100644 --- a/chrome/browser/extensions/api/debugger/debugger_api.cc +++ b/chrome/browser/extensions/api/debugger/debugger_api.cc @@ -59,17 +59,24 @@ class ExtensionDevToolsClientHost; class ExtensionDevToolsInfoBarDelegate : public ConfirmInfoBarDelegate { public: - ExtensionDevToolsInfoBarDelegate( + // Creates an extension dev tools delegate and adds it to |infobar_service|. + // Returns a pointer to the delegate if it was successfully added. + static ExtensionDevToolsInfoBarDelegate* Create( InfoBarService* infobar_service, const std::string& client_name, ExtensionDevToolsClientHost* client_host); - virtual ~ExtensionDevToolsInfoBarDelegate(); // Notifies infobar delegate that associated DevToolsClientHost will be // destroyed. void DiscardClientHost(); private: + ExtensionDevToolsInfoBarDelegate( + InfoBarService* infobar_service, + const std::string& client_name, + ExtensionDevToolsClientHost* client_host); + virtual ~ExtensionDevToolsInfoBarDelegate(); + // ConfirmInfoBarDelegate: virtual int GetButtons() const OVERRIDE; virtual Type GetInfoBarType() const OVERRIDE; @@ -196,14 +203,12 @@ ExtensionDevToolsClientHost::ExtensionDevToolsClientHost( InfoBarService* infobar_service = InfoBarService::FromWebContents(web_contents_); - infobar_delegate_ = new ExtensionDevToolsInfoBarDelegate(infobar_service, - extension_name, - this); - if (infobar_service->AddInfoBar(infobar_delegate_)) { + infobar_delegate_ = ExtensionDevToolsInfoBarDelegate::Create(infobar_service, + extension_name, + this); + if (infobar_delegate_) { registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, content::Source<InfoBarService>(infobar_service)); - } else { - infobar_delegate_ = NULL; } } @@ -347,6 +352,21 @@ void ExtensionDevToolsClientHost::DispatchOnInspectorFrontend( } } +// static +ExtensionDevToolsInfoBarDelegate* ExtensionDevToolsInfoBarDelegate::Create( + InfoBarService* infobar_service, + const std::string& client_name, + ExtensionDevToolsClientHost* client_host) { + return static_cast<ExtensionDevToolsInfoBarDelegate*>( + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new ExtensionDevToolsInfoBarDelegate(infobar_service, client_name, + client_host)))); +} + +void ExtensionDevToolsInfoBarDelegate::DiscardClientHost() { + client_host_ = NULL; +} + ExtensionDevToolsInfoBarDelegate::ExtensionDevToolsInfoBarDelegate( InfoBarService* infobar_service, const std::string& client_name, @@ -359,10 +379,6 @@ ExtensionDevToolsInfoBarDelegate::ExtensionDevToolsInfoBarDelegate( ExtensionDevToolsInfoBarDelegate::~ExtensionDevToolsInfoBarDelegate() { } -void ExtensionDevToolsInfoBarDelegate::DiscardClientHost() { - client_host_ = NULL; -} - int ExtensionDevToolsInfoBarDelegate::GetButtons() const { return BUTTON_CANCEL; } diff --git a/chrome/browser/extensions/app_notify_channel_ui_impl.cc b/chrome/browser/extensions/app_notify_channel_ui_impl.cc index 35762b23..b01ec68 100644 --- a/chrome/browser/extensions/app_notify_channel_ui_impl.cc +++ b/chrome/browser/extensions/app_notify_channel_ui_impl.cc @@ -33,10 +33,10 @@ namespace { class AppNotifyChannelUIInfoBarDelegate : public ConfirmInfoBarDelegate { public: - AppNotifyChannelUIInfoBarDelegate(AppNotifyChannelUIImpl* creator, - InfoBarService* infobar_service, - const std::string& app_name); - virtual ~AppNotifyChannelUIInfoBarDelegate(); + // Creates an app notify channel UI delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + AppNotifyChannelUIImpl* creator, + const std::string& app_name); // ConfirmInfoBarDelegate. virtual string16 GetMessageText() const OVERRIDE; @@ -46,22 +46,24 @@ class AppNotifyChannelUIInfoBarDelegate : public ConfirmInfoBarDelegate { virtual void InfoBarDismissed() OVERRIDE; private: + AppNotifyChannelUIInfoBarDelegate(AppNotifyChannelUIImpl* creator, + InfoBarService* infobar_service, + const std::string& app_name); + virtual ~AppNotifyChannelUIInfoBarDelegate(); + AppNotifyChannelUIImpl* creator_; std::string app_name_; DISALLOW_COPY_AND_ASSIGN(AppNotifyChannelUIInfoBarDelegate); }; -AppNotifyChannelUIInfoBarDelegate::AppNotifyChannelUIInfoBarDelegate( - AppNotifyChannelUIImpl* creator, - InfoBarService* infobar_service, - const std::string& app_name) - : ConfirmInfoBarDelegate(infobar_service), - creator_(creator), - app_name_(app_name) { -} - -AppNotifyChannelUIInfoBarDelegate::~AppNotifyChannelUIInfoBarDelegate() { +// static +void AppNotifyChannelUIInfoBarDelegate::Create(InfoBarService* infobar_service, + AppNotifyChannelUIImpl* creator, + const std::string& app_name) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new AppNotifyChannelUIInfoBarDelegate(creator, infobar_service, + app_name))); } string16 AppNotifyChannelUIInfoBarDelegate::GetMessageText() const { @@ -95,6 +97,18 @@ void AppNotifyChannelUIInfoBarDelegate::InfoBarDismissed() { Cancel(); } +AppNotifyChannelUIInfoBarDelegate::AppNotifyChannelUIInfoBarDelegate( + AppNotifyChannelUIImpl* creator, + InfoBarService* infobar_service, + const std::string& app_name) + : ConfirmInfoBarDelegate(infobar_service), + creator_(creator), + app_name_(app_name) { +} + +AppNotifyChannelUIInfoBarDelegate::~AppNotifyChannelUIInfoBarDelegate() { +} + } // namespace @@ -142,10 +156,8 @@ void AppNotifyChannelUIImpl::PromptSyncSetup( return; } - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents_); - infobar_service->AddInfoBar(new AppNotifyChannelUIInfoBarDelegate( - this, infobar_service, app_name_)); + AppNotifyChannelUIInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents_), this, app_name_); } void AppNotifyChannelUIImpl::OnInfoBarResult(bool accepted) { diff --git a/chrome/browser/extensions/extension_infobar_delegate.cc b/chrome/browser/extensions/extension_infobar_delegate.cc index f4be60a..ff1bcb4 100644 --- a/chrome/browser/extensions/extension_infobar_delegate.cc +++ b/chrome/browser/extensions/extension_infobar_delegate.cc @@ -16,6 +16,23 @@ #include "content/public/browser/notification_details.h" #include "content/public/browser/notification_source.h" + +ExtensionInfoBarDelegate::~ExtensionInfoBarDelegate() { + if (observer_) + observer_->OnDelegateDeleted(); +} + +// static +void ExtensionInfoBarDelegate::Create(InfoBarService* infobar_service, + Browser* browser, + const extensions::Extension* extension, + const GURL& url, + int height) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new ExtensionInfoBarDelegate(browser, infobar_service, extension, url, + height))); +} + ExtensionInfoBarDelegate::ExtensionInfoBarDelegate( Browser* browser, InfoBarService* infobar_service, @@ -54,11 +71,6 @@ ExtensionInfoBarDelegate::ExtensionInfoBarDelegate( height_ = default_height; } -ExtensionInfoBarDelegate::~ExtensionInfoBarDelegate() { - if (observer_) - observer_->OnDelegateDeleted(); -} - bool ExtensionInfoBarDelegate::EqualsDelegate(InfoBarDelegate* delegate) const { ExtensionInfoBarDelegate* extension_delegate = delegate->AsExtensionInfoBarDelegate(); diff --git a/chrome/browser/extensions/extension_infobar_delegate.h b/chrome/browser/extensions/extension_infobar_delegate.h index acbe138..579171c 100644 --- a/chrome/browser/extensions/extension_infobar_delegate.h +++ b/chrome/browser/extensions/extension_infobar_delegate.h @@ -33,11 +33,14 @@ class ExtensionInfoBarDelegate : public InfoBarDelegate, virtual ~DelegateObserver() {} }; - ExtensionInfoBarDelegate(Browser* browser, - InfoBarService* infobar_service, - const extensions::Extension* extension, - const GURL& url, - int height); + virtual ~ExtensionInfoBarDelegate(); + + // Creates an extension delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + Browser* browser, + const extensions::Extension* extension, + const GURL& url, + int height); const extensions::Extension* extension() { return extension_; } extensions::ExtensionHost* extension_host() { return extension_host_.get(); } @@ -48,7 +51,11 @@ class ExtensionInfoBarDelegate : public InfoBarDelegate, bool closing() const { return closing_; } private: - virtual ~ExtensionInfoBarDelegate(); + ExtensionInfoBarDelegate(Browser* browser, + InfoBarService* infobar_service, + const extensions::Extension* extension, + const GURL& url, + int height); // InfoBarDelegate: virtual InfoBar* CreateInfoBar(InfoBarService* owner) OVERRIDE; diff --git a/chrome/browser/extensions/extension_install_ui_default.cc b/chrome/browser/extensions/extension_install_ui_default.cc index af6834f..bc9c901 100644 --- a/chrome/browser/extensions/extension_install_ui_default.cc +++ b/chrome/browser/extensions/extension_install_ui_default.cc @@ -52,6 +52,12 @@ bool disable_failure_ui_for_tests = false; // Helper class to put up an infobar when installation fails. class ErrorInfobarDelegate : public ConfirmInfoBarDelegate { public: + // Creates an error delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + Browser* browser, + const extensions::CrxInstallerError& error); + + private: ErrorInfobarDelegate(InfoBarService* infobar_service, Browser* browser, const extensions::CrxInstallerError& error) @@ -60,7 +66,6 @@ class ErrorInfobarDelegate : public ConfirmInfoBarDelegate { error_(error) { } - private: virtual string16 GetMessageText() const OVERRIDE { return error_.message(); } @@ -88,6 +93,14 @@ class ErrorInfobarDelegate : public ConfirmInfoBarDelegate { extensions::CrxInstallerError error_; }; +// static +void ErrorInfobarDelegate::Create(InfoBarService* infobar_service, + Browser* browser, + const extensions::CrxInstallerError& error) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new ErrorInfobarDelegate(infobar_service, browser, error))); +} + } // namespace ExtensionInstallUIDefault::ExtensionInstallUIDefault(Profile* profile) @@ -124,8 +137,8 @@ void ExtensionInstallUIDefault::OnInstallSuccess(const Extension* extension, } if (extension->is_theme()) { - ShowThemeInfoBar(previous_theme_id_, previous_using_native_theme_, - extension, profile_); + ThemeInstalledInfoBarDelegate::Create( + extension, profile_, previous_theme_id_, previous_using_native_theme_); return; } @@ -166,10 +179,8 @@ void ExtensionInstallUIDefault::OnInstallFailure( WebContents* web_contents = chrome::GetActiveWebContents(browser); if (!web_contents) return; - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - infobar_service->AddInfoBar( - new ErrorInfobarDelegate(infobar_service, browser, error)); + ErrorInfobarDelegate::Create(InfoBarService::FromWebContents(web_contents), + browser, error); } void ExtensionInstallUIDefault::SetSkipPostInstallUI(bool skip_ui) { @@ -181,69 +192,6 @@ void ExtensionInstallUIDefault::SetUseAppInstalledBubble(bool use_bubble) { } // static -void ExtensionInstallUIDefault::ShowThemeInfoBar( - const std::string& previous_theme_id, bool previous_using_native_theme, - const Extension* new_theme, Profile* profile) { - if (!new_theme->is_theme()) - return; - - // Get last active tabbed browser of profile. - Browser* browser = browser::FindTabbedBrowser(profile, - true, - chrome::GetActiveDesktop()); - if (!browser) - return; - - WebContents* web_contents = chrome::GetActiveWebContents(browser); - if (!web_contents) - return; - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - - // First find any previous theme preview infobars. - InfoBarDelegate* old_delegate = NULL; - for (size_t i = 0; i < infobar_service->GetInfoBarCount(); ++i) { - InfoBarDelegate* delegate = infobar_service->GetInfoBarDelegateAt(i); - ThemeInstalledInfoBarDelegate* theme_infobar = - delegate->AsThemePreviewInfobarDelegate(); - if (theme_infobar) { - // If the user installed the same theme twice, ignore the second install - // and keep the first install info bar, so that they can easily undo to - // get back the previous theme. - if (theme_infobar->MatchesTheme(new_theme)) - return; - old_delegate = delegate; - break; - } - } - - // Then either replace that old one or add a new one. - InfoBarDelegate* new_delegate = GetNewThemeInstalledInfoBarDelegate( - web_contents, new_theme, previous_theme_id, previous_using_native_theme); - - if (old_delegate) - infobar_service->ReplaceInfoBar(old_delegate, new_delegate); - else - infobar_service->AddInfoBar(new_delegate); -} - -InfoBarDelegate* ExtensionInstallUIDefault::GetNewThemeInstalledInfoBarDelegate( - WebContents* web_contents, - const Extension* new_theme, - const std::string& previous_theme_id, - bool previous_using_native_theme) { - Profile* profile = - Profile::FromBrowserContext(web_contents->GetBrowserContext()); - return new ThemeInstalledInfoBarDelegate( - InfoBarService::FromWebContents(web_contents), - profile->GetExtensionService(), - ThemeServiceFactory::GetForProfile(profile), - new_theme, - previous_theme_id, - previous_using_native_theme); -} - -// static ExtensionInstallUI* ExtensionInstallUI::Create(Profile* profile) { return new ExtensionInstallUIDefault(profile); } diff --git a/chrome/browser/extensions/extension_install_ui_default.h b/chrome/browser/extensions/extension_install_ui_default.h index 34069bc..5c719b9 100644 --- a/chrome/browser/extensions/extension_install_ui_default.h +++ b/chrome/browser/extensions/extension_install_ui_default.h @@ -28,21 +28,6 @@ class ExtensionInstallUIDefault : public ExtensionInstallUI { virtual void SetUseAppInstalledBubble(bool use_bubble) OVERRIDE; private: - // Shows an infobar for a newly-installed theme. previous_theme_id should be - // empty if the previous theme was the system/default theme. - static void ShowThemeInfoBar(const std::string& previous_theme_id, - bool previous_using_native_theme, - const extensions::Extension* new_theme, - Profile* profile); - - // Returns the delegate to control the browser's info bar. This is - // within its own function due to its platform-specific nature. - static InfoBarDelegate* GetNewThemeInstalledInfoBarDelegate( - content::WebContents* web_contents, - const extensions::Extension* new_theme, - const std::string& previous_theme_id, - bool previous_using_native_theme); - // Whether or not to show the default UI after completing the installation. bool skip_post_install_ui_; diff --git a/chrome/browser/extensions/theme_installed_infobar_delegate.cc b/chrome/browser/extensions/theme_installed_infobar_delegate.cc index b8d34fa..b91f6ba 100644 --- a/chrome/browser/extensions/theme_installed_infobar_delegate.cc +++ b/chrome/browser/extensions/theme_installed_infobar_delegate.cc @@ -12,6 +12,8 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/themes/theme_service.h" #include "chrome/browser/themes/theme_service_factory.h" +#include "chrome/browser/ui/browser_finder.h" +#include "chrome/browser/ui/browser_tabstrip.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/extensions/extension.h" #include "content/public/browser/notification_source.h" @@ -20,6 +22,79 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" + +// static +void ThemeInstalledInfoBarDelegate::Create( + const extensions::Extension* new_theme, + Profile* profile, + const std::string& previous_theme_id, + bool previous_using_native_theme) { + if (!new_theme->is_theme()) + return; + + // Get last active tabbed browser of profile. + Browser* browser = browser::FindTabbedBrowser(profile, + true, + chrome::GetActiveDesktop()); + if (!browser) + return; + + content::WebContents* web_contents = chrome::GetActiveWebContents(browser); + if (!web_contents) + return; + InfoBarService* infobar_service = + InfoBarService::FromWebContents(web_contents); + + // First find any previous theme preview infobars. + InfoBarDelegate* old_delegate = NULL; + for (size_t i = 0; i < infobar_service->GetInfoBarCount(); ++i) { + InfoBarDelegate* delegate = infobar_service->GetInfoBarDelegateAt(i); + ThemeInstalledInfoBarDelegate* theme_infobar = + delegate->AsThemePreviewInfobarDelegate(); + if (theme_infobar) { + // If the user installed the same theme twice, ignore the second install + // and keep the first install info bar, so that they can easily undo to + // get back the previous theme. + if (theme_infobar->theme_id_ == new_theme->id()) + return; + old_delegate = delegate; + break; + } + } + + // Then either replace that old one or add a new one. + scoped_ptr<InfoBarDelegate> new_delegate( + new ThemeInstalledInfoBarDelegate( + infobar_service, + profile->GetExtensionService(), + ThemeServiceFactory::GetForProfile(profile), + new_theme, + previous_theme_id, + previous_using_native_theme)); + + if (old_delegate) + infobar_service->ReplaceInfoBar(old_delegate, new_delegate.Pass()); + else + infobar_service->AddInfoBar(new_delegate.Pass()); +} + +bool ThemeInstalledInfoBarDelegate::Cancel() { + if (!previous_theme_id_.empty()) { + const extensions::Extension* previous_theme = + extension_service_->GetExtensionById(previous_theme_id_, true); + if (previous_theme) { + theme_service_->SetTheme(previous_theme); + return false; // The theme change will close us. + } + } + + if (previous_using_native_theme_) + theme_service_->SetNativeTheme(); + else + theme_service_->UseDefaultTheme(); + return false; // The theme change will close us. +} + ThemeInstalledInfoBarDelegate::ThemeInstalledInfoBarDelegate( InfoBarService* infobar_service, ExtensionService* extension_service, @@ -39,11 +114,6 @@ ThemeInstalledInfoBarDelegate::ThemeInstalledInfoBarDelegate( content::Source<ThemeService>(theme_service_)); } -bool ThemeInstalledInfoBarDelegate::MatchesTheme( - const extensions::Extension* theme) const { - return theme && (theme->id() == theme_id_); -} - ThemeInstalledInfoBarDelegate::~ThemeInstalledInfoBarDelegate() { // We don't want any notifications while we're running our destructor. registrar_.RemoveAll(); @@ -51,23 +121,6 @@ ThemeInstalledInfoBarDelegate::~ThemeInstalledInfoBarDelegate() { theme_service_->OnInfobarDestroyed(); } -bool ThemeInstalledInfoBarDelegate::Cancel() { - if (!previous_theme_id_.empty()) { - const extensions::Extension* previous_theme = - extension_service_->GetExtensionById(previous_theme_id_, true); - if (previous_theme) { - theme_service_->SetTheme(previous_theme); - return false; // The theme change will close us. - } - } - - if (previous_using_native_theme_) - theme_service_->SetNativeTheme(); - else - theme_service_->UseDefaultTheme(); - return false; // The theme change will close us. -} - gfx::Image* ThemeInstalledInfoBarDelegate::GetIcon() const { // TODO(aa): Reply with the theme's icon, but this requires reading it // asynchronously from disk. diff --git a/chrome/browser/extensions/theme_installed_infobar_delegate.h b/chrome/browser/extensions/theme_installed_infobar_delegate.h index 9cee571..72cffd1 100644 --- a/chrome/browser/extensions/theme_installed_infobar_delegate.h +++ b/chrome/browser/extensions/theme_installed_infobar_delegate.h @@ -14,6 +14,7 @@ class ExtensionService; class InfoBarService; +class Profile; class ThemeService; namespace extensions { @@ -25,26 +26,28 @@ class Extension; class ThemeInstalledInfoBarDelegate : public ConfirmInfoBarDelegate, public content::NotificationObserver { public: - ThemeInstalledInfoBarDelegate(InfoBarService* infobar_service, - ExtensionService* extension_service, - ThemeService* theme_service, - const extensions::Extension* new_theme, - const std::string& previous_theme_id, - bool previous_using_native_theme); - - // Returns true if the given theme is the same as the one associated with this - // info bar. - bool MatchesTheme(const extensions::Extension* theme) const; + // Creates a theme installed delegate and adds it to the last active tab on + // |profile|. + static void Create(const extensions::Extension* new_theme, + Profile* profile, + const std::string& previous_theme_id, + bool previous_using_native_theme); protected: - virtual ~ThemeInstalledInfoBarDelegate(); - ThemeService* theme_service() { return theme_service_; } // ConfirmInfoBarDelegate: virtual bool Cancel() OVERRIDE; private: + ThemeInstalledInfoBarDelegate(InfoBarService* infobar_service, + ExtensionService* extension_service, + ThemeService* theme_service, + const extensions::Extension* new_theme, + const std::string& previous_theme_id, + bool previous_using_native_theme); + virtual ~ThemeInstalledInfoBarDelegate(); + // ConfirmInfoBarDelegate: virtual gfx::Image* GetIcon() const OVERRIDE; virtual Type GetInfoBarType() const OVERRIDE; diff --git a/chrome/browser/geolocation/chrome_geolocation_permission_context_unittest.cc b/chrome/browser/geolocation/chrome_geolocation_permission_context_unittest.cc index 0155641..0fe111c 100644 --- a/chrome/browser/geolocation/chrome_geolocation_permission_context_unittest.cc +++ b/chrome/browser/geolocation/chrome_geolocation_permission_context_unittest.cc @@ -294,7 +294,7 @@ TEST_F(GeolocationPermissionContextTests, SinglePermission) { infobar_service()->RemoveInfoBar(infobar_0); EXPECT_EQ(1U, closed_delegate_tracker_.size()); EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_0)); - infobar_0->InfoBarClosed(); + delete infobar_0; } #if defined(OS_ANDROID) @@ -399,7 +399,7 @@ TEST_F(GeolocationPermissionContextTests, QueuedPermission) { EXPECT_EQ(1U, closed_delegate_tracker_.size()); EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_0)); closed_delegate_tracker_.Clear(); - infobar_0->InfoBarClosed(); + delete infobar_0; // Now we should have a new infobar for the second frame. ASSERT_EQ(1U, infobar_service()->GetInfoBarCount()); @@ -416,7 +416,7 @@ TEST_F(GeolocationPermissionContextTests, QueuedPermission) { infobar_service()->RemoveInfoBar(infobar_1); EXPECT_EQ(1U, closed_delegate_tracker_.size()); EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_1)); - infobar_1->InfoBarClosed(); + delete infobar_1; EXPECT_EQ(0U, infobar_service()->GetInfoBarCount()); // Ensure the persisted permissions are ok. EXPECT_EQ(CONTENT_SETTING_ALLOW, @@ -470,7 +470,7 @@ TEST_F(GeolocationPermissionContextTests, CancelGeolocationPermissionRequest) { EXPECT_EQ(1U, closed_delegate_tracker_.size()); EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_0)); closed_delegate_tracker_.Clear(); - infobar_0->InfoBarClosed(); + delete infobar_0; ASSERT_EQ(1U, infobar_service()->GetInfoBarCount()); ConfirmInfoBarDelegate* infobar_1 = @@ -486,7 +486,7 @@ TEST_F(GeolocationPermissionContextTests, CancelGeolocationPermissionRequest) { infobar_service()->RemoveInfoBar(infobar_1); EXPECT_EQ(1U, closed_delegate_tracker_.size()); EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_1)); - infobar_1->InfoBarClosed(); + delete infobar_1; EXPECT_EQ(0U, infobar_service()->GetInfoBarCount()); // Ensure the persisted permissions are ok. EXPECT_EQ(CONTENT_SETTING_ASK, @@ -543,14 +543,14 @@ TEST_F(GeolocationPermissionContextTests, SameOriginMultipleTabs) { infobar_service()->RemoveInfoBar(infobar_0); EXPECT_EQ(2U, closed_delegate_tracker_.size()); EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_0)); - infobar_0->InfoBarClosed(); + delete infobar_0; // Now the infobar for the tab with the same origin should have gone. EXPECT_EQ(0U, infobar_service_for_tab(1)->GetInfoBarCount()); CheckPermissionMessageSentForTab(1, 0, true); EXPECT_TRUE(closed_delegate_tracker_.Contains(removed_infobar)); closed_delegate_tracker_.Clear(); // Destroy the infobar that has just been removed. - removed_infobar->InfoBarClosed(); + delete removed_infobar; // But the other tab should still have the info bar... ASSERT_EQ(1U, infobar_service_for_tab(0)->GetInfoBarCount()); @@ -560,7 +560,7 @@ TEST_F(GeolocationPermissionContextTests, SameOriginMultipleTabs) { infobar_service_for_tab(0)->RemoveInfoBar(infobar_1); EXPECT_EQ(1U, closed_delegate_tracker_.size()); EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_1)); - infobar_1->InfoBarClosed(); + delete infobar_1; } TEST_F(GeolocationPermissionContextTests, QueuedOriginMultipleTabs) { @@ -591,14 +591,14 @@ TEST_F(GeolocationPermissionContextTests, QueuedOriginMultipleTabs) { infobar_service_for_tab(0)->RemoveInfoBar(infobar_0); EXPECT_EQ(2U, closed_delegate_tracker_.size()); EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_0)); - infobar_0->InfoBarClosed(); + delete infobar_0; // Now the infobar for the tab with the same origin should have gone. EXPECT_EQ(0U, infobar_service()->GetInfoBarCount()); CheckPermissionMessageSent(0, true); EXPECT_TRUE(closed_delegate_tracker_.Contains(removed_infobar)); closed_delegate_tracker_.Clear(); // Destroy the infobar that has just been removed. - removed_infobar->InfoBarClosed(); + delete removed_infobar; // And we should have the queued infobar displayed now. ASSERT_EQ(1U, infobar_service_for_tab(0)->GetInfoBarCount()); @@ -612,7 +612,7 @@ TEST_F(GeolocationPermissionContextTests, QueuedOriginMultipleTabs) { infobar_service_for_tab(0)->RemoveInfoBar(infobar_1); EXPECT_EQ(1U, closed_delegate_tracker_.size()); EXPECT_TRUE(closed_delegate_tracker_.Contains(infobar_1)); - infobar_1->InfoBarClosed(); + delete infobar_1; } TEST_F(GeolocationPermissionContextTests, TabDestroyed) { @@ -647,7 +647,7 @@ TEST_F(GeolocationPermissionContextTests, TabDestroyed) { // Delete the tab contents. DeleteContents(); - infobar_0->InfoBarClosed(); + delete infobar_0; // During contents destruction, the infobar will have been closed, and the // pending request should have been cleared without an infobar being created. @@ -680,5 +680,5 @@ TEST_F(GeolocationPermissionContextTests, InfoBarUsesCommittedEntry) { // Delete the tab contents. DeleteContents(); - infobar_0->InfoBarClosed(); + delete infobar_0; } diff --git a/chrome/browser/geolocation/geolocation_confirm_infobar_delegate.cc b/chrome/browser/geolocation/geolocation_confirm_infobar_delegate.cc index d768a50..377dd68 100644 --- a/chrome/browser/geolocation/geolocation_confirm_infobar_delegate.cc +++ b/chrome/browser/geolocation/geolocation_confirm_infobar_delegate.cc @@ -17,6 +17,25 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" +#if defined(OS_ANDROID) +#include "chrome/browser/geolocation/geolocation_confirm_infobar_delegate_android.h" +typedef GeolocationConfirmInfoBarDelegateAndroid DelegateType; +#else +typedef GeolocationConfirmInfoBarDelegate DelegateType; +#endif + + +// static +InfoBarDelegate* GeolocationConfirmInfoBarDelegate::Create( + InfoBarService* infobar_service, + GeolocationInfoBarQueueController* controller, + const GeolocationPermissionRequestID& id, + const GURL& requesting_frame, + const std::string& display_languages) { + return infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new DelegateType(infobar_service, controller, id, requesting_frame, + display_languages))); +} GeolocationConfirmInfoBarDelegate::GeolocationConfirmInfoBarDelegate( InfoBarService* infobar_service, diff --git a/chrome/browser/geolocation/geolocation_confirm_infobar_delegate.h b/chrome/browser/geolocation/geolocation_confirm_infobar_delegate.h index ab9c1e1..28a35cb 100644 --- a/chrome/browser/geolocation/geolocation_confirm_infobar_delegate.h +++ b/chrome/browser/geolocation/geolocation_confirm_infobar_delegate.h @@ -19,6 +19,15 @@ class InfoBarService; // and handling of geolocation permission infobars to the user. class GeolocationConfirmInfoBarDelegate : public ConfirmInfoBarDelegate { public: + // Creates a geolocation delegate and adds it to |infobar_service|. Returns + // the delegate if it was successfully added. + static InfoBarDelegate* Create(InfoBarService* infobar_service, + GeolocationInfoBarQueueController* controller, + const GeolocationPermissionRequestID& id, + const GURL& requesting_frame, + const std::string& display_languages); + + protected: GeolocationConfirmInfoBarDelegate( InfoBarService* infobar_service, GeolocationInfoBarQueueController* controller, @@ -28,7 +37,6 @@ class GeolocationConfirmInfoBarDelegate : public ConfirmInfoBarDelegate { const GeolocationPermissionRequestID& id() const { return id_; } - protected: // ConfirmInfoBarDelegate: virtual gfx::Image* GetIcon() const OVERRIDE; virtual Type GetInfoBarType() const OVERRIDE; diff --git a/chrome/browser/geolocation/geolocation_confirm_infobar_delegate_factory.cc b/chrome/browser/geolocation/geolocation_confirm_infobar_delegate_factory.cc deleted file mode 100644 index 14daafc..0000000 --- a/chrome/browser/geolocation/geolocation_confirm_infobar_delegate_factory.cc +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2012 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. - -#include "chrome/browser/geolocation/geolocation_confirm_infobar_delegate_factory.h" - -#if defined(OS_ANDROID) -#include "chrome/browser/geolocation/geolocation_confirm_infobar_delegate_android.h" -#else -#include "chrome/browser/geolocation/geolocation_confirm_infobar_delegate.h" -#endif - -GeolocationConfirmInfoBarDelegate* - GeolocationConfirmInfoBarDelegateFactory::Create( - InfoBarService* infobar_service, - GeolocationInfoBarQueueController* controller, - const GeolocationPermissionRequestID& id, - const GURL& requesting_frame_url, - const std::string& display_languages) { -#if defined(OS_ANDROID) - return new GeolocationConfirmInfoBarDelegateAndroid( -#else - return new GeolocationConfirmInfoBarDelegate( -#endif - infobar_service, controller, id, requesting_frame_url, display_languages); -} diff --git a/chrome/browser/geolocation/geolocation_confirm_infobar_delegate_factory.h b/chrome/browser/geolocation/geolocation_confirm_infobar_delegate_factory.h deleted file mode 100644 index c33045b..0000000 --- a/chrome/browser/geolocation/geolocation_confirm_infobar_delegate_factory.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2012 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_GEOLOCATION_GEOLOCATION_CONFIRM_INFOBAR_DELEGATE_FACTORY_H_ -#define CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONFIRM_INFOBAR_DELEGATE_FACTORY_H_ - -#include "base/values.h" - -class GeolocationConfirmInfoBarDelegate; -class GeolocationInfoBarQueueController; -class GeolocationPermissionRequestID; -class GURL; -class InfoBarService; - -class GeolocationConfirmInfoBarDelegateFactory { - public: - GeolocationConfirmInfoBarDelegateFactory() {} - ~GeolocationConfirmInfoBarDelegateFactory() {} - static GeolocationConfirmInfoBarDelegate* Create( - InfoBarService* infobar_service, - GeolocationInfoBarQueueController* controller, - const GeolocationPermissionRequestID& id, - const GURL& requesting_frame_url, - const std::string& display_languages); - - private: - - DISALLOW_COPY_AND_ASSIGN(GeolocationConfirmInfoBarDelegateFactory); -}; - -#endif // CHROME_BROWSER_GEOLOCATION_GEOLOCATION_CONFIRM_INFOBAR_DELEGATE_FACTORY_H_ diff --git a/chrome/browser/geolocation/geolocation_infobar_queue_controller.cc b/chrome/browser/geolocation/geolocation_infobar_queue_controller.cc index 57dc9d5..40ebde8 100644 --- a/chrome/browser/geolocation/geolocation_infobar_queue_controller.cc +++ b/chrome/browser/geolocation/geolocation_infobar_queue_controller.cc @@ -7,7 +7,6 @@ #include "chrome/browser/api/infobars/infobar_service.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/geolocation/geolocation_confirm_infobar_delegate.h" -#include "chrome/browser/geolocation/geolocation_confirm_infobar_delegate_factory.h" #include "chrome/browser/infobars/infobar.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile.h" @@ -51,9 +50,7 @@ class GeolocationInfoBarQueueController::PendingInfoBarRequest { const GeolocationPermissionRequestID& id() const { return id_; } const GURL& requesting_frame() const { return requesting_frame_; } bool has_infobar_delegate() const { return !!infobar_delegate_; } - GeolocationConfirmInfoBarDelegate* infobar_delegate() { - return infobar_delegate_; - } + InfoBarDelegate* infobar_delegate() { return infobar_delegate_; } void RunCallback(bool allowed); void CreateInfoBarDelegate(GeolocationInfoBarQueueController* controller, @@ -64,7 +61,7 @@ class GeolocationInfoBarQueueController::PendingInfoBarRequest { GURL requesting_frame_; GURL embedder_; PermissionDecidedCallback callback_; - GeolocationConfirmInfoBarDelegate* infobar_delegate_; + InfoBarDelegate* infobar_delegate_; // Purposefully do not disable copying, as this is stored in STL containers. }; @@ -99,7 +96,7 @@ void GeolocationInfoBarQueueController::PendingInfoBarRequest::RunCallback( void GeolocationInfoBarQueueController::PendingInfoBarRequest:: CreateInfoBarDelegate(GeolocationInfoBarQueueController* controller, const std::string& display_languages) { - infobar_delegate_ = GeolocationConfirmInfoBarDelegateFactory::Create( + infobar_delegate_ = GeolocationConfirmInfoBarDelegate::Create( GetInfoBarService(id_), controller, id_, requesting_frame_, display_languages); @@ -227,7 +224,7 @@ void GeolocationInfoBarQueueController::Observe( content::Details<InfoBarRemovedDetails>(details)->first; for (PendingInfoBarRequests::iterator i = pending_infobar_requests_.begin(); i != pending_infobar_requests_.end(); ++i) { - GeolocationConfirmInfoBarDelegate* confirm_delegate = i->infobar_delegate(); + InfoBarDelegate* confirm_delegate = i->infobar_delegate(); if (confirm_delegate == delegate) { GeolocationPermissionRequestID id(i->id()); pending_infobar_requests_.erase(i); @@ -270,7 +267,6 @@ void GeolocationInfoBarQueueController::ShowQueuedInfoBarForTab( RegisterForInfoBarNotifications(infobar_service); i->CreateInfoBarDelegate( this, profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); - infobar_service->AddInfoBar(i->infobar_delegate()); return; } } diff --git a/chrome/browser/google/google_url_tracker.cc b/chrome/browser/google/google_url_tracker.cc index 7f0b3ba1..b935e2b 100644 --- a/chrome/browser/google/google_url_tracker.cc +++ b/chrome/browser/google/google_url_tracker.cc @@ -26,24 +26,6 @@ #include "net/url_request/url_request_status.h" -namespace { - -GoogleURLTrackerInfoBarDelegate* CreateInfoBar( - InfoBarService* infobar_service, - GoogleURLTracker* google_url_tracker, - const GURL& search_url) { - GoogleURLTrackerInfoBarDelegate* infobar = - new GoogleURLTrackerInfoBarDelegate(infobar_service, google_url_tracker, - search_url); - // AddInfoBar() takes ownership; it will delete |infobar| if it fails. - return infobar_service->AddInfoBar(infobar) ? infobar : NULL; -} - -} // namespace - - -// GoogleURLTracker ----------------------------------------------------------- - const char GoogleURLTracker::kDefaultGoogleHomepage[] = "http://www.google.com/"; const char GoogleURLTracker::kSearchDomainCheckURL[] = @@ -51,7 +33,7 @@ const char GoogleURLTracker::kSearchDomainCheckURL[] = GoogleURLTracker::GoogleURLTracker(Profile* profile, Mode mode) : profile_(profile), - infobar_creator_(base::Bind(&CreateInfoBar)), + infobar_creator_(base::Bind(&GoogleURLTrackerInfoBarDelegate::Create)), google_url_(mode == UNIT_TEST_MODE ? kDefaultGoogleHomepage : profile->GetPrefs()->GetString(prefs::kLastKnownGoogleURL)), ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), diff --git a/chrome/browser/google/google_url_tracker_infobar_delegate.cc b/chrome/browser/google/google_url_tracker_infobar_delegate.cc index a3d2a5d..5426098 100644 --- a/chrome/browser/google/google_url_tracker_infobar_delegate.cc +++ b/chrome/browser/google/google_url_tracker_infobar_delegate.cc @@ -16,14 +16,15 @@ #include "ui/base/l10n/l10n_util.h" -GoogleURLTrackerInfoBarDelegate::GoogleURLTrackerInfoBarDelegate( +// static +GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerInfoBarDelegate::Create( InfoBarService* infobar_service, GoogleURLTracker* google_url_tracker, - const GURL& search_url) - : ConfirmInfoBarDelegate(infobar_service), - google_url_tracker_(google_url_tracker), - search_url_(search_url), - pending_id_(0) { + const GURL& search_url) { + return static_cast<GoogleURLTrackerInfoBarDelegate*>( + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new GoogleURLTrackerInfoBarDelegate( + infobar_service, google_url_tracker, search_url)))); } bool GoogleURLTrackerInfoBarDelegate::Accept() { @@ -81,6 +82,16 @@ void GoogleURLTrackerInfoBarDelegate::Close(bool redo_search) { owner()->RemoveInfoBar(this); } +GoogleURLTrackerInfoBarDelegate::GoogleURLTrackerInfoBarDelegate( + InfoBarService* infobar_service, + GoogleURLTracker* google_url_tracker, + const GURL& search_url) + : ConfirmInfoBarDelegate(infobar_service), + google_url_tracker_(google_url_tracker), + search_url_(search_url), + pending_id_(0) { +} + GoogleURLTrackerInfoBarDelegate::~GoogleURLTrackerInfoBarDelegate() { } diff --git a/chrome/browser/google/google_url_tracker_infobar_delegate.h b/chrome/browser/google/google_url_tracker_infobar_delegate.h index c055361..3b21b57 100644 --- a/chrome/browser/google/google_url_tracker_infobar_delegate.h +++ b/chrome/browser/google/google_url_tracker_infobar_delegate.h @@ -14,9 +14,12 @@ class GoogleURLTracker; // changed. class GoogleURLTrackerInfoBarDelegate : public ConfirmInfoBarDelegate { public: - GoogleURLTrackerInfoBarDelegate(InfoBarService* infobar_service, - GoogleURLTracker* google_url_tracker, - const GURL& search_url); + // Creates a Google URL tracker delegate and adds it to |infobar_service|. + // Returns the delegate if it was successfully added. + static GoogleURLTrackerInfoBarDelegate* Create( + InfoBarService* infobar_service, + GoogleURLTracker* google_url_tracker, + const GURL& search_url); // ConfirmInfoBarDelegate: virtual bool Accept() OVERRIDE; @@ -37,6 +40,9 @@ class GoogleURLTrackerInfoBarDelegate : public ConfirmInfoBarDelegate { virtual void Close(bool redo_search); protected: + GoogleURLTrackerInfoBarDelegate(InfoBarService* infobar_service, + GoogleURLTracker* google_url_tracker, + const GURL& search_url); virtual ~GoogleURLTrackerInfoBarDelegate(); private: diff --git a/chrome/browser/google/google_url_tracker_unittest.cc b/chrome/browser/google/google_url_tracker_unittest.cc index 8ad21b7..15bba46 100644 --- a/chrome/browser/google/google_url_tracker_unittest.cc +++ b/chrome/browser/google/google_url_tracker_unittest.cc @@ -31,13 +31,25 @@ namespace { class TestInfoBarDelegate : public GoogleURLTrackerInfoBarDelegate { public: + // Creates a test delegate and returns it. Unlike the parent class, this does + // not create add the infobar to |infobar_service|, since that "pointer" is + // really just a magic number. Thus there is no InfoBarService ownership of + // the returned object; and since the caller doesn't own the returned object, + // we rely on |test_harness| cleaning this up eventually in + // GoogleURLTrackerTest::OnInfoBarClosed() to avoid leaks. + static GoogleURLTrackerInfoBarDelegate* Create( + GoogleURLTrackerTest* test_harness, + InfoBarService* infobar_service, + GoogleURLTracker* google_url_tracker, + const GURL& search_url); + + private: TestInfoBarDelegate(GoogleURLTrackerTest* test_harness, InfoBarService* infobar_service, GoogleURLTracker* google_url_tracker, const GURL& search_url); virtual ~TestInfoBarDelegate(); - private: // GoogleURLTrackerInfoBarDelegate: virtual void Update(const GURL& search_url) OVERRIDE; virtual void Close(bool redo_search) OVERRIDE; @@ -106,7 +118,7 @@ void TestNotificationObserver::Observe( class GoogleURLTrackerTest : public testing::Test { public: // Called by TestInfoBarDelegate::Close(). - void OnInfoBarClosed(GoogleURLTrackerInfoBarDelegate* infobar, + void OnInfoBarClosed(InfoBarDelegate* infobar, InfoBarService* infobar_service); protected: @@ -170,9 +182,8 @@ class GoogleURLTrackerTest : public testing::Test { std::set<int> unique_ids_seen_; }; -void GoogleURLTrackerTest::OnInfoBarClosed( - GoogleURLTrackerInfoBarDelegate* infobar, - InfoBarService* infobar_service) { +void GoogleURLTrackerTest::OnInfoBarClosed(InfoBarDelegate* infobar, + InfoBarService* infobar_service) { // First, simulate the InfoBarService firing INFOBAR_REMOVED. InfoBarRemovedDetails removed_details(infobar, false); GoogleURLTracker::EntryMap::const_iterator i = @@ -185,7 +196,7 @@ void GoogleURLTrackerTest::OnInfoBarClosed( content::Details<InfoBarRemovedDetails>(&removed_details)); // Second, simulate the infobar container closing the infobar in response. - infobar->InfoBarClosed(); + delete infobar; } GoogleURLTrackerTest::GoogleURLTrackerTest() @@ -386,8 +397,8 @@ GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::CreateTestInfoBar( InfoBarService* infobar_service, GoogleURLTracker* google_url_tracker, const GURL& search_url) { - return new TestInfoBarDelegate(this, infobar_service, google_url_tracker, - search_url); + return TestInfoBarDelegate::Create(this, infobar_service, google_url_tracker, + search_url); } @@ -395,6 +406,16 @@ GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::CreateTestInfoBar( namespace { +// static +GoogleURLTrackerInfoBarDelegate* TestInfoBarDelegate::Create( + GoogleURLTrackerTest* test_harness, + InfoBarService* infobar_service, + GoogleURLTracker* google_url_tracker, + const GURL& search_url) { + return new TestInfoBarDelegate(test_harness, infobar_service, + google_url_tracker, search_url); +} + TestInfoBarDelegate::TestInfoBarDelegate(GoogleURLTrackerTest* test_harness, InfoBarService* infobar_service, GoogleURLTracker* google_url_tracker, diff --git a/chrome/browser/infobars/alternate_nav_infobar_delegate.cc b/chrome/browser/infobars/alternate_nav_infobar_delegate.cc index 442aeb1..cc1cb08 100644 --- a/chrome/browser/infobars/alternate_nav_infobar_delegate.cc +++ b/chrome/browser/infobars/alternate_nav_infobar_delegate.cc @@ -12,6 +12,13 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" +// static +void AlternateNavInfoBarDelegate::Create(InfoBarService* infobar_service, + const GURL& alternate_nav_url) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new AlternateNavInfoBarDelegate(infobar_service, alternate_nav_url))); +} + AlternateNavInfoBarDelegate::AlternateNavInfoBarDelegate( InfoBarService* owner, const GURL& alternate_nav_url) diff --git a/chrome/browser/infobars/alternate_nav_infobar_delegate.h b/chrome/browser/infobars/alternate_nav_infobar_delegate.h index 48abd86..b85f0e23 100644 --- a/chrome/browser/infobars/alternate_nav_infobar_delegate.h +++ b/chrome/browser/infobars/alternate_nav_infobar_delegate.h @@ -12,15 +12,19 @@ class AlternateNavInfoBarDelegate : public InfoBarDelegate { public: - AlternateNavInfoBarDelegate(InfoBarService* owner, - const GURL& alternate_nav_url); - virtual ~AlternateNavInfoBarDelegate(); + // Creates an alternate nav delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + const GURL& alternate_nav_url); string16 GetMessageTextWithOffset(size_t* link_offset) const; string16 GetLinkText() const; bool LinkClicked(WindowOpenDisposition disposition); private: + AlternateNavInfoBarDelegate(InfoBarService* owner, + const GURL& alternate_nav_url); + virtual ~AlternateNavInfoBarDelegate(); + // InfoBarDelegate: virtual InfoBar* CreateInfoBar(InfoBarService* owner) OVERRIDE; virtual gfx::Image* GetIcon() const OVERRIDE; diff --git a/chrome/browser/infobars/infobar.cc b/chrome/browser/infobars/infobar.cc index 92844e0..c4121a0 100644 --- a/chrome/browser/infobars/infobar.cc +++ b/chrome/browser/infobars/infobar.cc @@ -183,7 +183,7 @@ void InfoBar::MaybeDelete() { if (!owner_ && delegate_ && (animation_.GetCurrentValue() == 0.0)) { if (container_) container_->RemoveInfoBar(this); - delegate_->InfoBarClosed(); + delete delegate_; delegate_ = NULL; } } diff --git a/chrome/browser/infobars/infobar_extension_api.cc b/chrome/browser/infobars/infobar_extension_api.cc index dd5afae..c1a0208 100644 --- a/chrome/browser/infobars/infobar_extension_api.cc +++ b/chrome/browser/infobars/infobar_extension_api.cc @@ -64,11 +64,9 @@ bool ShowInfoBarFunction::RunImpl() { return false; } - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - infobar_service->AddInfoBar( - new ExtensionInfoBarDelegate(browser, infobar_service, - GetExtension(), url, height)); + ExtensionInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents), browser, GetExtension(), + url, height); // TODO(finnur): Return the actual DOMWindow object. Bug 26463. DCHECK(browser->extension_window_controller()); diff --git a/chrome/browser/infobars/infobar_tab_helper.cc b/chrome/browser/infobars/infobar_tab_helper.cc index 0a3ec06..c02ce73 100644 --- a/chrome/browser/infobars/infobar_tab_helper.cc +++ b/chrome/browser/infobars/infobar_tab_helper.cc @@ -60,24 +60,23 @@ void InfoBarTabHelper::SetInfoBarsEnabled(bool enabled) { infobars_enabled_ = enabled; } -bool InfoBarTabHelper::AddInfoBar(InfoBarDelegate* delegate) { - if (!infobars_enabled_) { - delegate->InfoBarClosed(); - return false; - } +InfoBarDelegate* InfoBarTabHelper::AddInfoBar( + scoped_ptr<InfoBarDelegate> delegate) { + if (!infobars_enabled_) + return NULL; for (InfoBars::const_iterator i(infobars_.begin()); i != infobars_.end(); ++i) { - if ((*i)->EqualsDelegate(delegate)) { - DCHECK_NE(*i, delegate); - delegate->InfoBarClosed(); - return false; + if ((*i)->EqualsDelegate(delegate.get())) { + DCHECK_NE(*i, delegate.get()); + return NULL; } } // TODO(pkasting): Consider removing InfoBarTabHelper arg from delegate // constructors and instead using a setter from here. - infobars_.push_back(delegate); + InfoBarDelegate* delegate_ptr = delegate.release(); + infobars_.push_back(delegate_ptr); // Add ourselves as an observer for navigations the first time a delegate is // added. We use this notification to expire InfoBars that need to expire on // page transitions. @@ -91,35 +90,37 @@ bool InfoBarTabHelper::AddInfoBar(InfoBarDelegate* delegate) { content::NotificationService::current()->Notify( chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, content::Source<InfoBarService>(this), - content::Details<InfoBarAddedDetails>(delegate)); - return true; + content::Details<InfoBarAddedDetails>(delegate_ptr)); + return delegate_ptr; } void InfoBarTabHelper::RemoveInfoBar(InfoBarDelegate* delegate) { RemoveInfoBarInternal(delegate, true); } -bool InfoBarTabHelper::ReplaceInfoBar(InfoBarDelegate* old_delegate, - InfoBarDelegate* new_delegate) { +InfoBarDelegate* InfoBarTabHelper::ReplaceInfoBar( + InfoBarDelegate* old_delegate, + scoped_ptr<InfoBarDelegate> new_delegate) { if (!infobars_enabled_) - return AddInfoBar(new_delegate); // Deletes the delegate. + return AddInfoBar(new_delegate.Pass()); // Deletes the delegate. InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), old_delegate)); DCHECK(i != infobars_.end()); - i = infobars_.insert(i, new_delegate) + 1; + InfoBarDelegate* new_delegate_ptr = new_delegate.release(); + i = infobars_.insert(i, new_delegate_ptr); + InfoBarReplacedDetails replaced_details(old_delegate, new_delegate_ptr); // Remove the old delegate before notifying, so that if any observers call // back to AddInfoBar() or similar, we don't dupe-check against this delegate. - infobars_.erase(i); + infobars_.erase(++i); old_delegate->clear_owner(); - InfoBarReplacedDetails replaced_details(old_delegate, new_delegate); content::NotificationService::current()->Notify( chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, content::Source<InfoBarService>(this), content::Details<InfoBarReplacedDetails>(&replaced_details)); - return true; + return new_delegate_ptr; } size_t InfoBarTabHelper::GetInfoBarCount() const { @@ -169,30 +170,13 @@ void InfoBarTabHelper::RemoveAllInfoBars(bool animate) { } void InfoBarTabHelper::OnDidBlockDisplayingInsecureContent() { - // At most one infobar and do not supersede the stronger running content bar. - for (size_t i = 0; i < infobars_.size(); ++i) { - if (GetInfoBarDelegateAt(i)->AsInsecureContentInfoBarDelegate()) - return; - } - AddInfoBar(new InsecureContentInfoBarDelegate(this, - InsecureContentInfoBarDelegate::DISPLAY)); + InsecureContentInfoBarDelegate::Create( + this, InsecureContentInfoBarDelegate::DISPLAY); } void InfoBarTabHelper::OnDidBlockRunningInsecureContent() { - // At most one infobar superseding any weaker displaying content bar. - for (size_t i = 0; i < infobars_.size(); ++i) { - InsecureContentInfoBarDelegate* delegate = - GetInfoBarDelegateAt(i)->AsInsecureContentInfoBarDelegate(); - if (delegate) { - if (delegate->type() != InsecureContentInfoBarDelegate::RUN) { - ReplaceInfoBar(delegate, new InsecureContentInfoBarDelegate( - this, InsecureContentInfoBarDelegate::RUN)); - } - return; - } - } - AddInfoBar(new InsecureContentInfoBarDelegate(this, - InsecureContentInfoBarDelegate::RUN)); + InsecureContentInfoBarDelegate::Create(this, + InsecureContentInfoBarDelegate::RUN); } void InfoBarTabHelper::RenderViewGone(base::TerminationStatus status) { diff --git a/chrome/browser/infobars/infobar_tab_helper.h b/chrome/browser/infobars/infobar_tab_helper.h index cde1862..3eb9ac4 100644 --- a/chrome/browser/infobars/infobar_tab_helper.h +++ b/chrome/browser/infobars/infobar_tab_helper.h @@ -31,10 +31,12 @@ class InfoBarTabHelper : public InfoBarService, // InfoBarService: virtual void SetInfoBarsEnabled(bool enabled) OVERRIDE; - virtual bool AddInfoBar(InfoBarDelegate* delegate) OVERRIDE; + virtual InfoBarDelegate* AddInfoBar( + scoped_ptr<InfoBarDelegate> delegate) OVERRIDE; virtual void RemoveInfoBar(InfoBarDelegate* delegate) OVERRIDE; - virtual bool ReplaceInfoBar(InfoBarDelegate* old_delegate, - InfoBarDelegate* new_delegate) OVERRIDE; + virtual InfoBarDelegate* ReplaceInfoBar( + InfoBarDelegate* old_delegate, + scoped_ptr<InfoBarDelegate> new_delegate) OVERRIDE; virtual size_t GetInfoBarCount() const OVERRIDE; virtual InfoBarDelegate* GetInfoBarDelegateAt(size_t index) OVERRIDE; virtual content::WebContents* GetWebContents() OVERRIDE; diff --git a/chrome/browser/infobars/insecure_content_infobar_delegate.cc b/chrome/browser/infobars/insecure_content_infobar_delegate.cc index 3a3cf32..bebb45a 100644 --- a/chrome/browser/infobars/insecure_content_infobar_delegate.cc +++ b/chrome/browser/infobars/insecure_content_infobar_delegate.cc @@ -16,14 +16,37 @@ using content::OpenURLParams; +// static +void InsecureContentInfoBarDelegate::Create(InfoBarService* infobar_service, + InfoBarType type) { + scoped_ptr<InfoBarDelegate> new_infobar( + new InsecureContentInfoBarDelegate(infobar_service, type)); + + // Only supsersede an existing insecure content infobar if we are upgrading + // from DISPLAY to RUN. + for (size_t i = 0; i < infobar_service->GetInfoBarCount(); ++i) { + InsecureContentInfoBarDelegate* delegate = infobar_service-> + GetInfoBarDelegateAt(i)->AsInsecureContentInfoBarDelegate(); + if (delegate != NULL) { + if ((type == RUN) && (delegate->type_ == DISPLAY)) + return; + infobar_service->ReplaceInfoBar(delegate, new_infobar.Pass()); + break; + } + } + if (new_infobar.get()) + infobar_service->AddInfoBar(new_infobar.Pass()); + + UMA_HISTOGRAM_ENUMERATION("InsecureContentInfoBarDelegateV2", + (type == DISPLAY) ? DISPLAY_INFOBAR_SHOWN : RUN_INFOBAR_SHOWN, + NUM_EVENTS); +} + InsecureContentInfoBarDelegate::InsecureContentInfoBarDelegate( InfoBarService* infobar_service, InfoBarType type) : ConfirmInfoBarDelegate(infobar_service), type_(type) { - UMA_HISTOGRAM_ENUMERATION("InsecureContentInfoBarDelegateV2", - (type_ == DISPLAY) ? DISPLAY_INFOBAR_SHOWN : RUN_INFOBAR_SHOWN, - NUM_EVENTS); } InsecureContentInfoBarDelegate::~InsecureContentInfoBarDelegate() { diff --git a/chrome/browser/infobars/insecure_content_infobar_delegate.h b/chrome/browser/infobars/insecure_content_infobar_delegate.h index 144806b..7e7f4c0 100644 --- a/chrome/browser/infobars/insecure_content_infobar_delegate.h +++ b/chrome/browser/infobars/insecure_content_infobar_delegate.h @@ -16,11 +16,11 @@ class InsecureContentInfoBarDelegate : public ConfirmInfoBarDelegate { RUN, // Shown when "active" content (e.g. script) has been blocked. }; - InsecureContentInfoBarDelegate(InfoBarService* infobar_service, - InfoBarType type); - virtual ~InsecureContentInfoBarDelegate(); - - InfoBarType type() const { return type_; } + // Depending on the |type| requested and whether an insecure content infobar + // is already present in |infobar_service|, may do nothing; otherwise, creates + // an insecure content delegate and either adds it to |infobar_service| or + // replaces the existing infobar delegate. + static void Create(InfoBarService* infobar_service, InfoBarType type); private: enum HistogramEvents { @@ -35,6 +35,10 @@ class InsecureContentInfoBarDelegate : public ConfirmInfoBarDelegate { NUM_EVENTS }; + InsecureContentInfoBarDelegate(InfoBarService* infobar_service, + InfoBarType type); + virtual ~InsecureContentInfoBarDelegate(); + // ConfirmInfoBarDelegate: virtual void InfoBarDismissed() OVERRIDE; virtual InsecureContentInfoBarDelegate* diff --git a/chrome/browser/intents/register_intent_handler_helper.cc b/chrome/browser/intents/register_intent_handler_helper.cc deleted file mode 100644 index 2cca3c0..0000000 --- a/chrome/browser/intents/register_intent_handler_helper.cc +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2012 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. - -#include <string> - -#include "chrome/browser/api/infobars/infobar_service.h" -#include "chrome/browser/favicon/favicon_service.h" -#include "chrome/browser/favicon/favicon_service_factory.h" -#include "chrome/browser/intents/register_intent_handler_infobar_delegate.h" -#include "chrome/browser/intents/web_intents_registry_factory.h" -#include "chrome/browser/intents/web_intents_util.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/ui/browser.h" -#include "content/public/browser/web_contents.h" -#include "webkit/glue/web_intent_service_data.h" - -using content::WebContents; - -// static -void Browser::RegisterIntentHandlerHelper( - WebContents* web_contents, - const webkit_glue::WebIntentServiceData& data, - bool user_gesture) { - Profile* profile = - Profile::FromBrowserContext(web_contents->GetBrowserContext()); - if (profile->IsOffTheRecord()) - return; - - if (!web_intents::IsWebIntentsEnabledForProfile(profile)) - return; - - FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( - profile, Profile::EXPLICIT_ACCESS); - - RegisterIntentHandlerInfoBarDelegate::MaybeShowIntentInfoBar( - InfoBarService::FromWebContents(web_contents), - WebIntentsRegistryFactory::GetForProfile(profile), - data, - favicon_service, - web_contents->GetURL()); -} diff --git a/chrome/browser/intents/register_intent_handler_infobar_delegate.cc b/chrome/browser/intents/register_intent_handler_infobar_delegate.cc index d9df3ef..5892d7f 100644 --- a/chrome/browser/intents/register_intent_handler_infobar_delegate.cc +++ b/chrome/browser/intents/register_intent_handler_infobar_delegate.cc @@ -10,23 +10,41 @@ #include "base/utf_string_conversions.h" #include "chrome/browser/api/infobars/infobar_service.h" #include "chrome/browser/favicon/favicon_service.h" +#include "chrome/browser/favicon/favicon_service_factory.h" #include "chrome/browser/intents/web_intents_registry.h" #include "chrome/browser/intents/web_intents_registry_factory.h" +#include "chrome/browser/intents/web_intents_util.h" #include "chrome/browser/profiles/profile.h" +#include "content/public/browser/web_contents.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" -RegisterIntentHandlerInfoBarDelegate::RegisterIntentHandlerInfoBarDelegate( - InfoBarService* infobar_service, - WebIntentsRegistry* registry, - const webkit_glue::WebIntentServiceData& service, - FaviconService* favicon_service, - const GURL& origin_url) - : ConfirmInfoBarDelegate(infobar_service), - registry_(registry), - service_(service), - favicon_service_(favicon_service), - origin_url_(origin_url) { + +// static +void RegisterIntentHandlerInfoBarDelegate::Create( + content::WebContents* web_contents, + const webkit_glue::WebIntentServiceData& data) { + Profile* profile = + Profile::FromBrowserContext(web_contents->GetBrowserContext()); + if (profile->IsOffTheRecord()) + return; + + if (!web_intents::IsWebIntentsEnabledForProfile(profile)) + return; + + FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( + profile, Profile::EXPLICIT_ACCESS); + + WebIntentsRegistry* registry = + WebIntentsRegistryFactory::GetForProfile(profile); + registry->IntentServiceExists( + data, base::Bind( + &CreateContinuation, + base::Unretained(InfoBarService::FromWebContents(web_contents)), + registry, + data, + favicon_service, + web_contents->GetURL())); } InfoBarDelegate::Type @@ -74,37 +92,30 @@ bool RegisterIntentHandlerInfoBarDelegate::LinkClicked( return false; } -namespace { - -// Helper continuation for MaybeShowIntentInfoBar. -void CheckProvider(InfoBarService* infobar_service, - WebIntentsRegistry* registry, - const webkit_glue::WebIntentServiceData& service, - FaviconService* favicon_service, - const GURL& origin_url, - bool provider_exists) { - if (!provider_exists) { - infobar_service->AddInfoBar(new RegisterIntentHandlerInfoBarDelegate( - infobar_service, registry, service, favicon_service, origin_url)); - } +RegisterIntentHandlerInfoBarDelegate::RegisterIntentHandlerInfoBarDelegate( + InfoBarService* infobar_service, + WebIntentsRegistry* registry, + const webkit_glue::WebIntentServiceData& service, + FaviconService* favicon_service, + const GURL& origin_url) + : ConfirmInfoBarDelegate(infobar_service), + registry_(registry), + service_(service), + favicon_service_(favicon_service), + origin_url_(origin_url) { } -} // namespace - // static -void RegisterIntentHandlerInfoBarDelegate::MaybeShowIntentInfoBar( +void RegisterIntentHandlerInfoBarDelegate::CreateContinuation( InfoBarService* infobar_service, WebIntentsRegistry* registry, const webkit_glue::WebIntentServiceData& service, FaviconService* favicon_service, - const GURL& origin_url) { - DCHECK(infobar_service); - DCHECK(registry); - registry->IntentServiceExists(service, - base::Bind(&CheckProvider, - base::Unretained(infobar_service), - registry, - service, - favicon_service, - origin_url)); + const GURL& origin_url, + bool provider_exists) { + if (!provider_exists) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new RegisterIntentHandlerInfoBarDelegate( + infobar_service, registry, service, favicon_service, origin_url))); + } } diff --git a/chrome/browser/intents/register_intent_handler_infobar_delegate.h b/chrome/browser/intents/register_intent_handler_infobar_delegate.h index db15538..9fcb197 100644 --- a/chrome/browser/intents/register_intent_handler_infobar_delegate.h +++ b/chrome/browser/intents/register_intent_handler_infobar_delegate.h @@ -10,20 +10,37 @@ #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" #include "webkit/glue/web_intent_service_data.h" +#if defined(UNIT_TEST) +#include "base/memory/scoped_ptr.h" +#endif + class WebIntentsRegistry; class FaviconService; class GURL; +namespace content { +class WebContents; +} // The InfoBar used to request permission for a site to be registered as an // Intent handler. class RegisterIntentHandlerInfoBarDelegate : public ConfirmInfoBarDelegate { public: - RegisterIntentHandlerInfoBarDelegate( - InfoBarService* infobar_service, + // Checks whether the intent service specified by |data| exists. If not, and + // |web_contents| is in a non-incognito, web-intents-enabled profile, creates + // a register intent handler delegate and adds it to the InfoBarService for + // |web_contents|. + static void Create(content::WebContents* web_contents, + const webkit_glue::WebIntentServiceData& data); + +#if defined(UNIT_TEST) + static scoped_ptr<RegisterIntentHandlerInfoBarDelegate> Create( WebIntentsRegistry* registry, - const webkit_glue::WebIntentServiceData& service, - FaviconService* favicon_service, - const GURL& origin_url); + const webkit_glue::WebIntentServiceData& data) { + return scoped_ptr<RegisterIntentHandlerInfoBarDelegate>( + new RegisterIntentHandlerInfoBarDelegate(NULL, registry, data, NULL, + GURL())); + } +#endif // ConfirmInfoBarDelegate implementation. virtual Type GetInfoBarType() const OVERRIDE; @@ -33,22 +50,25 @@ class RegisterIntentHandlerInfoBarDelegate : public ConfirmInfoBarDelegate { virtual string16 GetLinkText() const OVERRIDE; virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE; - // Shows the intent registration infobar if |service| has not already been - // registered. - // |infobar_service| is the infobar controller for the tab in which the - // infobar may be shown. Must not be NULL. - // |registry| is the data source for web intents. Must not be NULL. - // |service| is the candidate service to show the infobar for. - // |favicon_service| is the favicon service to use. Must not be NULL. - // |origin_url| is the URL that the intent is registered from. - static void MaybeShowIntentInfoBar( + private: + RegisterIntentHandlerInfoBarDelegate( InfoBarService* infobar_service, WebIntentsRegistry* registry, const webkit_glue::WebIntentServiceData& service, FaviconService* favicon_service, const GURL& origin_url); - private: + // Finishes the work of Create(). This is called back from the + // WebIntentsRegistry once it determines whether the requested intent service + // exists. + static void CreateContinuation( + InfoBarService* infobar_service, + WebIntentsRegistry* registry, + const webkit_glue::WebIntentServiceData& service, + FaviconService* favicon_service, + const GURL& origin_url, + bool provider_exists); + // The web intents registry to use. Weak pointer. WebIntentsRegistry* registry_; diff --git a/chrome/browser/intents/register_intent_handler_infobar_delegate_unittest.cc b/chrome/browser/intents/register_intent_handler_infobar_delegate_unittest.cc index 5b09920..3b3d5f3 100644 --- a/chrome/browser/intents/register_intent_handler_infobar_delegate_unittest.cc +++ b/chrome/browser/intents/register_intent_handler_infobar_delegate_unittest.cc @@ -4,7 +4,6 @@ #include "base/synchronization/waitable_event.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/api/infobars/infobar_service.h" #include "chrome/browser/intents/register_intent_handler_infobar_delegate.h" #include "chrome/browser/intents/web_intents_registry.h" #include "chrome/browser/intents/web_intents_registry_factory.h" @@ -44,7 +43,6 @@ class RegisterIntentHandlerInfoBarDelegateTest virtual void SetUp() { ChromeRenderViewHostTestHarness::SetUp(); - InfoBarService::CreateForWebContents(web_contents()); profile()->CreateWebDataService(); web_intents_registry_ = BuildForProfile(profile()); @@ -70,13 +68,12 @@ TEST_F(RegisterIntentHandlerInfoBarDelegateTest, Accept) { service.service_url = GURL("google.com"); service.action = ASCIIToUTF16("http://webintents.org/share"); service.type = ASCIIToUTF16("text/url"); - RegisterIntentHandlerInfoBarDelegate delegate( - InfoBarService::FromWebContents(web_contents()), - WebIntentsRegistryFactory::GetForProfile(profile()), - service, NULL, GURL()); + scoped_ptr<RegisterIntentHandlerInfoBarDelegate> infobar_delegate( + RegisterIntentHandlerInfoBarDelegate::Create(web_intents_registry_, + service)); EXPECT_CALL(*web_intents_registry_, RegisterIntentService(service)); - delegate.Accept(); + infobar_delegate->Accept(); } } // namespace diff --git a/chrome/browser/managed_mode/managed_mode_navigation_observer.cc b/chrome/browser/managed_mode/managed_mode_navigation_observer.cc index 94928e4..d344d1e 100644 --- a/chrome/browser/managed_mode/managed_mode_navigation_observer.cc +++ b/chrome/browser/managed_mode/managed_mode_navigation_observer.cc @@ -22,9 +22,12 @@ namespace { class ManagedModeWarningInfobarDelegate : public ConfirmInfoBarDelegate { public: - explicit ManagedModeWarningInfobarDelegate(InfoBarService* infobar_service); + // Creates a managed mode warning delegate and adds it to |infobar_service|. + // Returns the delegate if it was successfully added. + static InfoBarDelegate* Create(InfoBarService* infobar_service); private: + explicit ManagedModeWarningInfobarDelegate(InfoBarService* infobar_service); virtual ~ManagedModeWarningInfobarDelegate(); // ConfirmInfoBarDelegate overrides: @@ -64,6 +67,13 @@ void GoBackToSafety(content::WebContents* web_contents) { web_contents->GetDelegate()->CloseContents(web_contents); } +// static +InfoBarDelegate* ManagedModeWarningInfobarDelegate::Create( + InfoBarService* infobar_service) { + return infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new ManagedModeWarningInfobarDelegate(infobar_service))); +} + ManagedModeWarningInfobarDelegate::ManagedModeWarningInfobarDelegate( InfoBarService* infobar_service) : ConfirmInfoBarDelegate(infobar_service) {} @@ -138,11 +148,8 @@ void ManagedModeNavigationObserver::DidCommitProvisionalLoadForFrame( if (behavior == ManagedModeURLFilter::WARN) { if (!warn_infobar_delegate_) { - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents()); - warn_infobar_delegate_ = - new ManagedModeWarningInfobarDelegate(infobar_service); - infobar_service->AddInfoBar(warn_infobar_delegate_); + warn_infobar_delegate_ = ManagedModeWarningInfobarDelegate::Create( + InfoBarService::FromWebContents(web_contents())); } } else { if (warn_infobar_delegate_) { diff --git a/chrome/browser/nacl_host/nacl_infobar.cc b/chrome/browser/nacl_host/nacl_infobar.cc index 764ded0..eb6fe35 100644 --- a/chrome/browser/nacl_host/nacl_infobar.cc +++ b/chrome/browser/nacl_host/nacl_infobar.cc @@ -30,8 +30,8 @@ void ShowInfobar(int render_process_id, int render_view_id, render_view_id); WebContents* wc = WebContents::FromRenderViewHost(rvh); InfoBarService* ibs = InfoBarService::FromWebContents(wc); - ibs->AddInfoBar(new SimpleAlertInfoBarDelegate(ibs, NULL, - l10n_util::GetStringUTF16(IDS_NACL_APP_MISSING_ARCH_MESSAGE), true)); + SimpleAlertInfoBarDelegate::Create(ibs, NULL, + l10n_util::GetStringUTF16(IDS_NACL_APP_MISSING_ARCH_MESSAGE), true); } } // namespace diff --git a/chrome/browser/notifications/desktop_notification_service.cc b/chrome/browser/notifications/desktop_notification_service.cc index ef0ed71..6b4ee0f 100644 --- a/chrome/browser/notifications/desktop_notification_service.cc +++ b/chrome/browser/notifications/desktop_notification_service.cc @@ -59,6 +59,17 @@ const ContentSetting kDefaultSetting = CONTENT_SETTING_ASK; // permissions. class NotificationPermissionInfoBarDelegate : public ConfirmInfoBarDelegate { public: + // Creates a notification permission delegate and adds it to + // |infobar_service|. + static void Create(InfoBarService* infobar_service, + DesktopNotificationService* notification_service, + const GURL& origin, + const string16& display_name, + int process_id, + int route_id, + int callback_context); + + private: NotificationPermissionInfoBarDelegate( InfoBarService* infobar_service, DesktopNotificationService* notification_service, @@ -67,8 +78,6 @@ class NotificationPermissionInfoBarDelegate : public ConfirmInfoBarDelegate { int process_id, int route_id, int callback_context); - - private: virtual ~NotificationPermissionInfoBarDelegate(); // ConfirmInfoBarDelegate: @@ -101,6 +110,21 @@ class NotificationPermissionInfoBarDelegate : public ConfirmInfoBarDelegate { DISALLOW_COPY_AND_ASSIGN(NotificationPermissionInfoBarDelegate); }; +// static +void NotificationPermissionInfoBarDelegate::Create( + InfoBarService* infobar_service, + DesktopNotificationService* notification_service, + const GURL& origin, + const string16& display_name, + int process_id, + int route_id, + int callback_context) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new NotificationPermissionInfoBarDelegate( + infobar_service, notification_service, origin, display_name, + process_id, route_id, callback_context))); +} + NotificationPermissionInfoBarDelegate::NotificationPermissionInfoBarDelegate( InfoBarService* infobar_service, DesktopNotificationService* notification_service, @@ -416,7 +440,7 @@ void DesktopNotificationService::RequestPermission( // browser action popup, extension background page, or any HTML that runs // outside of a tab. if (infobar_service) { - infobar_service->AddInfoBar(new NotificationPermissionInfoBarDelegate( + NotificationPermissionInfoBarDelegate::Create( infobar_service, DesktopNotificationServiceFactory::GetForProfile( Profile::FromBrowserContext(contents->GetBrowserContext())), @@ -424,7 +448,7 @@ void DesktopNotificationService::RequestPermission( DisplayNameForOrigin(origin), process_id, route_id, - callback_context)); + callback_context); return; } } diff --git a/chrome/browser/omnibox_search_hint.cc b/chrome/browser/omnibox_search_hint.cc index 892bdd3..5e63825 100644 --- a/chrome/browser/omnibox_search_hint.cc +++ b/chrome/browser/omnibox_search_hint.cc @@ -43,25 +43,21 @@ using content::NavigationEntry; DEFINE_WEB_CONTENTS_USER_DATA_KEY(OmniboxSearchHint); -// The URLs of search engines for which we want to trigger the infobar. -const char* const kSearchEngineURLs[] = { - "http://www.google.com/", - "http://www.yahoo.com/", - "http://www.bing.com/", - "http://www.altavista.com/", - "http://www.ask.com/", - "http://www.wolframalpha.com/", -}; - // HintInfoBar ---------------------------------------------------------------- class HintInfoBar : public ConfirmInfoBarDelegate { public: - HintInfoBar(OmniboxSearchHint* omnibox_hint, - InfoBarService* infobar_service); + // If the active entry for |web_contents| is a navigation to the user's + // default search engine, and the engine is on a small whitelist, creates a + // "you can search from the omnibox" hint delegate and adds it to the + // InfoBarService for |web_contents|. + static void Create(content::WebContents* web_contents, + OmniboxSearchHint* omnibox_hint); private: + HintInfoBar(OmniboxSearchHint* omnibox_hint, + InfoBarService* infobar_service); virtual ~HintInfoBar(); void AllowExpiry() { should_expire_ = true; } @@ -92,6 +88,48 @@ class HintInfoBar : public ConfirmInfoBarDelegate { DISALLOW_COPY_AND_ASSIGN(HintInfoBar); }; +// static +void HintInfoBar::Create(content::WebContents* web_contents, + OmniboxSearchHint* omnibox_hint) { + // The URLs of search engines for which we want to trigger the infobar. + const char* const kSearchEngineURLs[] = { + "http://www.google.com/", + "http://www.yahoo.com/", + "http://www.bing.com/", + "http://www.altavista.com/", + "http://www.ask.com/", + "http://www.wolframalpha.com/", + }; + CR_DEFINE_STATIC_LOCAL(std::set<std::string>, search_engine_urls, ()); + if (search_engine_urls.empty()) { + for (size_t i = 0; i < arraysize(kSearchEngineURLs); ++i) + search_engine_urls.insert(kSearchEngineURLs[i]); + } + + content::NavigationEntry* entry = + web_contents->GetController().GetActiveEntry(); + if (search_engine_urls.find(entry->GetURL().spec()) == + search_engine_urls.end()) { + // The search engine is not in our white-list, bail. + return; + } + + Profile* profile = + Profile::FromBrowserContext(web_contents->GetBrowserContext()); + const TemplateURL* const default_provider = + TemplateURLServiceFactory::GetForProfile(profile)-> + GetDefaultSearchProvider(); + if (!default_provider) + return; + + if (default_provider->url_ref().GetHost() == entry->GetURL().host()) { + InfoBarService* infobar_service = + InfoBarService::FromWebContents(web_contents); + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new HintInfoBar(omnibox_hint, infobar_service))); + } +} + HintInfoBar::HintInfoBar(OmniboxSearchHint* omnibox_hint, InfoBarService* infobar_service) : ConfirmInfoBarDelegate(infobar_service), @@ -165,9 +203,6 @@ OmniboxSearchHint::OmniboxSearchHint(content::WebContents* web_contents) this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, content::Source<NavigationController>(controller)); - // Fill the search_engine_urls_ map, used for faster look-up (overkill?). - for (size_t i = 0; i < arraysize(kSearchEngineURLs); ++i) - search_engine_urls_[kSearchEngineURLs[i]] = 1; Profile* profile = Profile::FromBrowserContext(web_contents->GetBrowserContext()); @@ -184,23 +219,7 @@ void OmniboxSearchHint::Observe(int type, const content::NotificationSource& source, const content::NotificationDetails& details) { if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { - content::NavigationEntry* entry = - web_contents_->GetController().GetActiveEntry(); - if (search_engine_urls_.find(entry->GetURL().spec()) == - search_engine_urls_.end()) { - // The search engine is not in our white-list, bail. - return; - } - Profile* profile = - Profile::FromBrowserContext(web_contents_->GetBrowserContext()); - const TemplateURL* const default_provider = - TemplateURLServiceFactory::GetForProfile(profile)-> - GetDefaultSearchProvider(); - if (!default_provider) - return; - - if (default_provider->url_ref().GetHost() == entry->GetURL().host()) - ShowInfoBar(); + HintInfoBar::Create(web_contents_, this); } else if (type == chrome::NOTIFICATION_OMNIBOX_OPENED_URL) { AutocompleteLog* log = content::Details<AutocompleteLog>(details).ptr(); AutocompleteMatch::Type type = @@ -213,12 +232,6 @@ void OmniboxSearchHint::Observe(int type, } } -void OmniboxSearchHint::ShowInfoBar() { - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents_); - infobar_service->AddInfoBar(new HintInfoBar(this, infobar_service)); -} - void OmniboxSearchHint::ShowEnteringQuery() { LocationBar* location_bar = chrome::FindBrowserWithWebContents( web_contents_)->window()->GetLocationBar(); diff --git a/chrome/browser/omnibox_search_hint.h b/chrome/browser/omnibox_search_hint.h index efb2826..82d8a21 100644 --- a/chrome/browser/omnibox_search_hint.h +++ b/chrome/browser/omnibox_search_hint.h @@ -5,9 +5,6 @@ #ifndef CHROME_BROWSER_OMNIBOX_SEARCH_HINT_H_ #define CHROME_BROWSER_OMNIBOX_SEARCH_HINT_H_ -#include <map> -#include <string> - #include "base/compiler_specific.h" #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_registrar.h" @@ -50,17 +47,11 @@ class OmniboxSearchHint explicit OmniboxSearchHint(content::WebContents* web_contents); friend class content::WebContentsUserData<OmniboxSearchHint>; - void ShowInfoBar(); - content::NotificationRegistrar notification_registrar_; // The contents we are associated with. content::WebContents* web_contents_; - // A map containing the URLs of the search engine for which we want to - // trigger the hint. - std::map<std::string, int> search_engine_urls_; - DISALLOW_COPY_AND_ASSIGN(OmniboxSearchHint); }; diff --git a/chrome/browser/password_manager/password_manager_delegate_impl.cc b/chrome/browser/password_manager/password_manager_delegate_impl.cc index 029879d..fc9c55e 100644 --- a/chrome/browser/password_manager/password_manager_delegate_impl.cc +++ b/chrome/browser/password_manager/password_manager_delegate_impl.cc @@ -38,8 +38,10 @@ DEFINE_WEB_CONTENTS_USER_DATA_KEY(PasswordManagerDelegateImpl); // forms never end up in an infobar. class SavePasswordInfoBarDelegate : public ConfirmInfoBarDelegate { public: - SavePasswordInfoBarDelegate(InfoBarService* infobar_service, - PasswordFormManager* form_to_save); + // If we won't be showing the one-click signin infobar, creates a save + // password delegate and adds it to the InfoBarService for |web_contents|. + static void Create(content::WebContents* web_contents, + PasswordFormManager* form_to_save); private: enum ResponseType { @@ -49,6 +51,8 @@ class SavePasswordInfoBarDelegate : public ConfirmInfoBarDelegate { NUM_RESPONSE_TYPES, }; + SavePasswordInfoBarDelegate(InfoBarService* infobar_service, + PasswordFormManager* form_to_save); virtual ~SavePasswordInfoBarDelegate(); // ConfirmInfoBarDelegate @@ -71,6 +75,32 @@ class SavePasswordInfoBarDelegate : public ConfirmInfoBarDelegate { DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegate); }; +// static +void SavePasswordInfoBarDelegate::Create(content::WebContents* web_contents, + PasswordFormManager* form_to_save) { +#if defined(ENABLE_ONE_CLICK_SIGNIN) + // Don't show the password manager infobar if this form is for a google + // account and we are going to show the one-click singin infobar. + // For now, one-click signin is fully implemented only on windows. + GURL realm(form_to_save->realm()); + // TODO(mathp): Checking only against associated_username() causes a bug + // referenced here: crbug.com/133275 + if ((realm == GURL(GaiaUrls::GetInstance()->gaia_login_form_realm()) || + realm == GURL("https://www.google.com/")) && + OneClickSigninHelper::CanOffer( + web_contents, + OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY, + UTF16ToUTF8(form_to_save->associated_username()), NULL)) { + return; + } +#endif + + InfoBarService* infobar_service = + InfoBarService::FromWebContents(web_contents); + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new SavePasswordInfoBarDelegate(infobar_service, form_to_save))); +} + SavePasswordInfoBarDelegate::SavePasswordInfoBarDelegate( InfoBarService* infobar_service, PasswordFormManager* form_to_save) @@ -147,26 +177,7 @@ void PasswordManagerDelegateImpl::FillPasswordForm( void PasswordManagerDelegateImpl::AddSavePasswordInfoBarIfPermitted( PasswordFormManager* form_to_save) { - // Don't show the password manager infobar if this form is for a google - // account and we are going to show the one-click singin infobar. - // For now, one-click signin is fully implemented only on windows. -#if defined(ENABLE_ONE_CLICK_SIGNIN) - GURL realm(form_to_save->realm()); - // TODO(mathp): Checking only against associated_username() causes a bug - // referenced here: crbug.com/133275 - if ((realm == GURL(GaiaUrls::GetInstance()->gaia_login_form_realm()) || - realm == GURL("https://www.google.com/")) && - OneClickSigninHelper::CanOffer(web_contents_, - OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY, - UTF16ToUTF8(form_to_save->associated_username()), NULL)) { - return; - } -#endif - - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents_); - infobar_service->AddInfoBar( - new SavePasswordInfoBarDelegate(infobar_service, form_to_save)); + SavePasswordInfoBarDelegate::Create(web_contents_, form_to_save); } Profile* PasswordManagerDelegateImpl::GetProfile() { diff --git a/chrome/browser/pepper_broker_infobar_delegate.cc b/chrome/browser/pepper_broker_infobar_delegate.cc index 91e9b9d..8a22734 100644 --- a/chrome/browser/pepper_broker_infobar_delegate.cc +++ b/chrome/browser/pepper_broker_infobar_delegate.cc @@ -32,7 +32,7 @@ using content::Referrer; using content::WebContents; // static -void PepperBrokerInfoBarDelegate::Show( +void PepperBrokerInfoBarDelegate::Create( WebContents* web_contents, const GURL& url, const FilePath& plugin_path, @@ -72,10 +72,10 @@ void PepperBrokerInfoBarDelegate::Show( InfoBarService::FromWebContents(web_contents); std::string languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); - infobar_service->AddInfoBar( + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( new PepperBrokerInfoBarDelegate( infobar_service, url, plugin_path, languages, content_settings, - callback)); + callback))); break; } default: @@ -83,26 +83,6 @@ void PepperBrokerInfoBarDelegate::Show( } } -PepperBrokerInfoBarDelegate::PepperBrokerInfoBarDelegate( - InfoBarService* infobar_service, - const GURL& url, - const FilePath& plugin_path, - const std::string& languages, - HostContentSettingsMap* content_settings, - const base::Callback<void(bool)>& callback) - : ConfirmInfoBarDelegate(infobar_service), - url_(url), - plugin_path_(plugin_path), - languages_(languages), - content_settings_(content_settings), - callback_(callback) { -} - -PepperBrokerInfoBarDelegate::~PepperBrokerInfoBarDelegate() { - if (!callback_.is_null()) - callback_.Run(false); -} - string16 PepperBrokerInfoBarDelegate::GetMessageText() const { content::PluginService* plugin_service = content::PluginService::GetInstance(); @@ -164,6 +144,26 @@ gfx::Image* PepperBrokerInfoBarDelegate::GetIcon() const { IDR_INFOBAR_PLUGIN_INSTALL); } +PepperBrokerInfoBarDelegate::PepperBrokerInfoBarDelegate( + InfoBarService* infobar_service, + const GURL& url, + const FilePath& plugin_path, + const std::string& languages, + HostContentSettingsMap* content_settings, + const base::Callback<void(bool)>& callback) + : ConfirmInfoBarDelegate(infobar_service), + url_(url), + plugin_path_(plugin_path), + languages_(languages), + content_settings_(content_settings), + callback_(callback) { +} + +PepperBrokerInfoBarDelegate::~PepperBrokerInfoBarDelegate() { + if (!callback_.is_null()) + callback_.Run(false); +} + void PepperBrokerInfoBarDelegate::DispatchCallback(bool result) { content::RecordAction(result ? content::UserMetricsAction("PPAPI.BrokerInfobarClickedAllow") : diff --git a/chrome/browser/pepper_broker_infobar_delegate.h b/chrome/browser/pepper_broker_infobar_delegate.h index 361e565..d2aeee6 100644 --- a/chrome/browser/pepper_broker_infobar_delegate.h +++ b/chrome/browser/pepper_broker_infobar_delegate.h @@ -22,13 +22,14 @@ class WebContents; // by storing a content setting for the site. class PepperBrokerInfoBarDelegate : public ConfirmInfoBarDelegate { public: - virtual ~PepperBrokerInfoBarDelegate(); - - static void Show( - content::WebContents* web_contents, - const GURL& url, - const FilePath& plugin_path, - const base::Callback<void(bool)>& callback); + // Determines whether the broker setting is allow, deny, or ask. In the first + // two cases, runs the callback directly. In the third, creates a pepper + // broker delegate and adds it to the InfoBarService associated with + // |web_contents|. + static void Create(content::WebContents* web_contents, + const GURL& url, + const FilePath& plugin_path, + const base::Callback<void(bool)>& callback); // ConfirmInfoBarDelegate: virtual string16 GetMessageText() const OVERRIDE; @@ -48,6 +49,7 @@ class PepperBrokerInfoBarDelegate : public ConfirmInfoBarDelegate { const std::string& languages, HostContentSettingsMap* content_settings, const base::Callback<void(bool)>& callback); + virtual ~PepperBrokerInfoBarDelegate(); void DispatchCallback(bool result); diff --git a/chrome/browser/plugins/plugin_infobar_delegates.cc b/chrome/browser/plugins/plugin_infobar_delegates.cc index cc83f34..6749140 100644 --- a/chrome/browser/plugins/plugin_infobar_delegates.cc +++ b/chrome/browser/plugins/plugin_infobar_delegates.cc @@ -28,6 +28,9 @@ #endif #if defined(ENABLE_PLUGIN_INSTALLATION) +#if defined(OS_WIN) +#include "base/win/metro.h" +#endif #include "chrome/browser/plugins/plugin_installer.h" #endif // defined(ENABLE_PLUGIN_INSTALLATION) @@ -75,15 +78,18 @@ string16 PluginInfoBarDelegate::GetLinkText() const { // UnauthorizedPluginInfoBarDelegate ------------------------------------------ -UnauthorizedPluginInfoBarDelegate::UnauthorizedPluginInfoBarDelegate( +// static +void UnauthorizedPluginInfoBarDelegate::Create( InfoBarService* infobar_service, HostContentSettingsMap* content_settings, const string16& utf16_name, - const std::string& identifier) - : PluginInfoBarDelegate(infobar_service, utf16_name, identifier), - content_settings_(content_settings) { + const std::string& identifier) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new UnauthorizedPluginInfoBarDelegate(infobar_service, content_settings, + utf16_name, identifier))); + content::RecordAction(UserMetricsAction("BlockedPluginInfobar.Shown")); - std::string name = UTF16ToUTF8(utf16_name); + std::string name(UTF16ToUTF8(utf16_name)); if (name == PluginMetadata::kJavaGroupName) content::RecordAction( UserMetricsAction("BlockedPluginInfobar.Shown.Java")); @@ -101,6 +107,15 @@ UnauthorizedPluginInfoBarDelegate::UnauthorizedPluginInfoBarDelegate( UserMetricsAction("BlockedPluginInfobar.Shown.WindowsMediaPlayer")); } +UnauthorizedPluginInfoBarDelegate::UnauthorizedPluginInfoBarDelegate( + InfoBarService* infobar_service, + HostContentSettingsMap* content_settings, + const string16& utf16_name, + const std::string& identifier) + : PluginInfoBarDelegate(infobar_service, utf16_name, identifier), + content_settings_(content_settings) { +} + UnauthorizedPluginInfoBarDelegate::~UnauthorizedPluginInfoBarDelegate() { content::RecordAction(UserMetricsAction("BlockedPluginInfobar.Closed")); } @@ -153,8 +168,8 @@ bool UnauthorizedPluginInfoBarDelegate::LinkClicked( #if defined(ENABLE_PLUGIN_INSTALLATION) // OutdatedPluginInfoBarDelegate ---------------------------------------------- -InfoBarDelegate* OutdatedPluginInfoBarDelegate::Create( - content::WebContents* web_contents, +void OutdatedPluginInfoBarDelegate::Create( + InfoBarService* infobar_service, PluginInstaller* installer, scoped_ptr<PluginMetadata> plugin_metadata) { string16 message; @@ -168,17 +183,18 @@ InfoBarDelegate* OutdatedPluginInfoBarDelegate::Create( plugin_metadata->name()); break; } - return new OutdatedPluginInfoBarDelegate( - web_contents, installer, plugin_metadata.Pass(), message); + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new OutdatedPluginInfoBarDelegate( + infobar_service, installer, plugin_metadata.Pass(), message))); } OutdatedPluginInfoBarDelegate::OutdatedPluginInfoBarDelegate( - content::WebContents* web_contents, + InfoBarService* infobar_service, PluginInstaller* installer, scoped_ptr<PluginMetadata> plugin_metadata, const string16& message) : PluginInfoBarDelegate( - InfoBarService::FromWebContents(web_contents), + infobar_service, plugin_metadata->name(), plugin_metadata->identifier()), WeakPluginInstallerObserver(installer), @@ -299,14 +315,55 @@ void OutdatedPluginInfoBarDelegate::ReplaceWithInfoBar( return; if (!owner()) return; - InfoBarDelegate* delegate = new PluginInstallerInfoBarDelegate( - owner(), installer(), plugin_metadata_->Clone(), - PluginInstallerInfoBarDelegate::InstallCallback(), false, message); - owner()->ReplaceInfoBar(this, delegate); + PluginInstallerInfoBarDelegate::Replace( + this, installer(), plugin_metadata_->Clone(), false, message); } // PluginInstallerInfoBarDelegate --------------------------------------------- +void PluginInstallerInfoBarDelegate::Create( + InfoBarService* infobar_service, + PluginInstaller* installer, + scoped_ptr<PluginMetadata> plugin_metadata, + const InstallCallback& callback) { + string16 name(plugin_metadata->name()); +#if defined(OS_WIN) + if (base::win::IsMetroProcess()) { + PluginMetroModeInfoBarDelegate::Create( + infobar_service, PluginMetroModeInfoBarDelegate::MISSING_PLUGIN, name); + return; + } +#endif + string16 message; + switch (installer->state()) { + case PluginInstaller::INSTALLER_STATE_IDLE: + message = l10n_util::GetStringFUTF16( + IDS_PLUGININSTALLER_INSTALLPLUGIN_PROMPT, name); + break; + case PluginInstaller::INSTALLER_STATE_DOWNLOADING: + message = l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING, name); + break; + } + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new PluginInstallerInfoBarDelegate( + infobar_service, installer, plugin_metadata.Pass(), callback, true, + message))); +} + +void PluginInstallerInfoBarDelegate::Replace( + InfoBarDelegate* infobar, + PluginInstaller* installer, + scoped_ptr<PluginMetadata> plugin_metadata, + bool new_install, + const string16& message) { + DCHECK(infobar->owner()); + infobar->owner()->ReplaceInfoBar(infobar, scoped_ptr<InfoBarDelegate>( + new PluginInstallerInfoBarDelegate( + infobar->owner(), installer, plugin_metadata.Pass(), + PluginInstallerInfoBarDelegate::InstallCallback(), new_install, + message))); +} + PluginInstallerInfoBarDelegate::PluginInstallerInfoBarDelegate( InfoBarService* infobar_service, PluginInstaller* installer, @@ -325,27 +382,6 @@ PluginInstallerInfoBarDelegate::PluginInstallerInfoBarDelegate( PluginInstallerInfoBarDelegate::~PluginInstallerInfoBarDelegate() { } -InfoBarDelegate* PluginInstallerInfoBarDelegate::Create( - InfoBarService* infobar_service, - PluginInstaller* installer, - scoped_ptr<PluginMetadata> plugin_metadata, - const InstallCallback& callback) { - string16 message; - switch (installer->state()) { - case PluginInstaller::INSTALLER_STATE_IDLE: - message = l10n_util::GetStringFUTF16( - IDS_PLUGININSTALLER_INSTALLPLUGIN_PROMPT, plugin_metadata->name()); - break; - case PluginInstaller::INSTALLER_STATE_DOWNLOADING: - message = l10n_util::GetStringFUTF16(IDS_PLUGIN_DOWNLOADING, - plugin_metadata->name()); - break; - } - return new PluginInstallerInfoBarDelegate( - infobar_service, installer, plugin_metadata.Pass(), - callback, true, message); -} - gfx::Image* PluginInstallerInfoBarDelegate::GetIcon() const { return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( IDR_INFOBAR_PLUGIN_INSTALL); @@ -428,14 +464,20 @@ void PluginInstallerInfoBarDelegate::ReplaceWithInfoBar( return; if (!owner()) return; - InfoBarDelegate* delegate = new PluginInstallerInfoBarDelegate( - owner(), installer(), plugin_metadata_->Clone(), - InstallCallback(), new_install_, message); - owner()->ReplaceInfoBar(this, delegate); + Replace(this, installer(), plugin_metadata_->Clone(), new_install_, message); } // PluginMetroModeInfoBarDelegate --------------------------------------------- #if defined(OS_WIN) +// static +void PluginMetroModeInfoBarDelegate::Create( + InfoBarService* infobar_service, + PluginMetroModeInfoBarDelegate::Mode mode, + const string16& name) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new PluginMetroModeInfoBarDelegate(infobar_service, mode, name))); +} + PluginMetroModeInfoBarDelegate::PluginMetroModeInfoBarDelegate( InfoBarService* infobar_service, PluginMetroModeInfoBarDelegate::Mode mode, diff --git a/chrome/browser/plugins/plugin_infobar_delegates.h b/chrome/browser/plugins/plugin_infobar_delegates.h index 62e0d84..d7df525 100644 --- a/chrome/browser/plugins/plugin_infobar_delegates.h +++ b/chrome/browser/plugins/plugin_infobar_delegates.h @@ -53,12 +53,17 @@ class PluginInfoBarDelegate : public ConfirmInfoBarDelegate { // Infobar that's shown when a plug-in requires user authorization to run. class UnauthorizedPluginInfoBarDelegate : public PluginInfoBarDelegate { public: + // Creates an unauthorized plugin delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + HostContentSettingsMap* content_settings, + const string16& name, + const std::string& identifier); + + private: UnauthorizedPluginInfoBarDelegate(InfoBarService* infobar_service, HostContentSettingsMap* content_settings, const string16& name, const std::string& identifier); - - private: virtual ~UnauthorizedPluginInfoBarDelegate(); // PluginInfoBarDelegate: @@ -80,12 +85,13 @@ class UnauthorizedPluginInfoBarDelegate : public PluginInfoBarDelegate { class OutdatedPluginInfoBarDelegate : public PluginInfoBarDelegate, public WeakPluginInstallerObserver { public: - static InfoBarDelegate* Create(content::WebContents* web_contents, - PluginInstaller* installer, - scoped_ptr<PluginMetadata> metadata); + // Creates an outdated plugin delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + PluginInstaller* installer, + scoped_ptr<PluginMetadata> metadata); private: - OutdatedPluginInfoBarDelegate(content::WebContents* web_contents, + OutdatedPluginInfoBarDelegate(InfoBarService* infobar_service, PluginInstaller* installer, scoped_ptr<PluginMetadata> metadata, const string16& message); @@ -131,14 +137,20 @@ class PluginInstallerInfoBarDelegate : public ConfirmInfoBarDelegate, // |installer|. When the user accepts, |callback| is called. // During installation of the plug-in, the infobar will change to reflect the // installation state. - static InfoBarDelegate* Create(InfoBarService* infobar_service, - PluginInstaller* installer, - scoped_ptr<PluginMetadata> plugin_metadata, - const InstallCallback& callback); + static void Create(InfoBarService* infobar_service, + PluginInstaller* installer, + scoped_ptr<PluginMetadata> plugin_metadata, + const InstallCallback& callback); + + // Replaces |infobar|, which must currently be owned, with an infobar asking + // the user to install or update a particular plugin. + static void Replace(InfoBarDelegate* infobar, + PluginInstaller* installer, + scoped_ptr<PluginMetadata> metadata, + bool new_install, + const string16& message); private: - friend class OutdatedPluginInfoBarDelegate; - PluginInstallerInfoBarDelegate(InfoBarService* infobar_service, PluginInstaller* installer, scoped_ptr<PluginMetadata> plugin_metadata, @@ -193,11 +205,16 @@ class PluginMetroModeInfoBarDelegate : public ConfirmInfoBarDelegate { DESKTOP_MODE_REQUIRED, }; + // Creates a metro mode infobar and delegate and adds the infobar to + // |infobar_service|. + static void Create(InfoBarService* infobar_service, + Mode mode, + const string16& name); + + private: PluginMetroModeInfoBarDelegate(InfoBarService* infobar_service, Mode mode, const string16& name); - - private: virtual ~PluginMetroModeInfoBarDelegate(); // ConfirmInfoBarDelegate: diff --git a/chrome/browser/plugins/plugin_observer.cc b/chrome/browser/plugins/plugin_observer.cc index 5205a5d..750812a 100644 --- a/chrome/browser/plugins/plugin_observer.cc +++ b/chrome/browser/plugins/plugin_observer.cc @@ -32,15 +32,14 @@ #include "webkit/plugins/webplugininfo.h" #if defined(ENABLE_PLUGIN_INSTALLATION) +#if defined(OS_WIN) +#include "base/win/metro.h" +#endif #include "chrome/browser/plugins/plugin_installer.h" #include "chrome/browser/plugins/plugin_installer_observer.h" #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" #endif // defined(ENABLE_PLUGIN_INSTALLATION) -#if defined(OS_WIN) -#include "base/win/metro.h" -#endif - using content::OpenURLParams; using content::PluginService; using content::Referrer; @@ -189,14 +188,9 @@ void PluginObserver::PluginCrashed(const FilePath& plugin_path) { PluginService::GetInstance()->GetPluginDisplayNameByPath(plugin_path); gfx::Image* icon = &ResourceBundle::GetSharedInstance().GetNativeImageNamed( IDR_INFOBAR_PLUGIN_CRASHED); - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents()); - infobar_service->AddInfoBar( - new SimpleAlertInfoBarDelegate( - infobar_service, - icon, - l10n_util::GetStringFUTF16(IDS_PLUGIN_CRASHED_PROMPT, plugin_name), - true)); + SimpleAlertInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents()), icon, + l10n_util::GetStringFUTF16(IDS_PLUGIN_CRASHED_PROMPT, plugin_name), true); } bool PluginObserver::OnMessageReceived(const IPC::Message& message) { @@ -227,14 +221,11 @@ bool PluginObserver::OnMessageReceived(const IPC::Message& message) { void PluginObserver::OnBlockedUnauthorizedPlugin( const string16& name, const std::string& identifier) { - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents()); - infobar_service->AddInfoBar( - new UnauthorizedPluginInfoBarDelegate( - infobar_service, - Profile::FromBrowserContext(web_contents()->GetBrowserContext())-> - GetHostContentSettingsMap(), - name, identifier)); + UnauthorizedPluginInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents()), + Profile::FromBrowserContext(web_contents()->GetBrowserContext())-> + GetHostContentSettingsMap(), + name, identifier); } void PluginObserver::OnBlockedOutdatedPlugin(int placeholder_id, @@ -252,11 +243,9 @@ void PluginObserver::OnBlockedOutdatedPlugin(int placeholder_id, plugin_placeholders_[placeholder_id] = new PluginPlaceholderHost(this, placeholder_id, plugin->name(), installer); - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents()); - infobar_service->AddInfoBar( - OutdatedPluginInfoBarDelegate::Create(web_contents(), - installer, plugin.Pass())); + OutdatedPluginInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents()), installer, + plugin.Pass()); #else // If we don't support third-party plug-in installation, we shouldn't have // outdated plug-ins. @@ -283,25 +272,11 @@ void PluginObserver::OnFindMissingPlugin(int placeholder_id, new PluginPlaceholderHost(this, placeholder_id, plugin_metadata->name(), installer); - PluginInstallerInfoBarDelegate::InstallCallback callback = + PluginInstallerInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents()), installer, + plugin_metadata.Pass(), base::Bind(&PluginObserver::InstallMissingPlugin, - weak_ptr_factory_.GetWeakPtr(), installer); - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents()); - InfoBarDelegate* delegate; -#if !defined(OS_WIN) - delegate = PluginInstallerInfoBarDelegate::Create( - infobar_service, installer, plugin_metadata.Pass(), callback); -#else - delegate = base::win::IsMetroProcess() ? - new PluginMetroModeInfoBarDelegate( - infobar_service, - PluginMetroModeInfoBarDelegate::MISSING_PLUGIN, - plugin_metadata->name()) : - PluginInstallerInfoBarDelegate::Create( - infobar_service, installer, plugin_metadata.Pass(), callback); -#endif - infobar_service->AddInfoBar(delegate); + weak_ptr_factory_.GetWeakPtr(), installer)); } void PluginObserver::InstallMissingPlugin( @@ -341,15 +316,13 @@ void PluginObserver::OnCouldNotLoadPlugin(const FilePath& plugin_path) { g_browser_process->metrics_service()->LogPluginLoadingError(plugin_path); string16 plugin_name = PluginService::GetInstance()->GetPluginDisplayNameByPath(plugin_path); - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents()); - infobar_service->AddInfoBar(new SimpleAlertInfoBarDelegate( - infobar_service, + SimpleAlertInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents()), &ResourceBundle::GetSharedInstance().GetNativeImageNamed( IDR_INFOBAR_PLUGIN_CRASHED), l10n_util::GetStringFUTF16(IDS_PLUGIN_INITIALIZATION_ERROR_PROMPT, plugin_name), - true /* auto_expire */)); + true /* auto_expire */); } void PluginObserver::OnNPAPINotSupported(const std::string& identifier) { @@ -375,13 +348,9 @@ void PluginObserver::OnNPAPINotSupported(const std::string& identifier) { return; } - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents()); - infobar_service->AddInfoBar( - new PluginMetroModeInfoBarDelegate( - infobar_service, - PluginMetroModeInfoBarDelegate::DESKTOP_MODE_REQUIRED, - plugin->name())); + PluginMetroModeInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents()), + PluginMetroModeInfoBarDelegate::DESKTOP_MODE_REQUIRED, plugin->name()); #else NOTREACHED(); #endif diff --git a/chrome/browser/ssl/ssl_tab_helper.cc b/chrome/browser/ssl/ssl_tab_helper.cc index 69ff23c..ca76dec 100644 --- a/chrome/browser/ssl/ssl_tab_helper.cc +++ b/chrome/browser/ssl/ssl_tab_helper.cc @@ -14,7 +14,6 @@ #include "base/values.h" #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" #include "chrome/browser/api/infobars/infobar_service.h" -#include "chrome/browser/api/infobars/simple_alert_infobar_delegate.h" #include "chrome/browser/certificate_viewer.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/infobars/infobar.h" @@ -36,23 +35,27 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -namespace { -gfx::Image* GetCertIcon() { - // TODO(davidben): use a more appropriate icon. - return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( - IDR_INFOBAR_SAVE_PASSWORD); -} +// SSLCertResultInfoBarDelegate ----------------------------------------------- -// SSLCertAddedInfoBarDelegate ------------------------------------------------ +namespace { -class SSLCertAddedInfoBarDelegate : public ConfirmInfoBarDelegate { +class SSLCertResultInfoBarDelegate : public ConfirmInfoBarDelegate { public: - SSLCertAddedInfoBarDelegate(InfoBarService* infobar_service, - net::X509Certificate* cert); + // Creates an SSL cert result delegate. If |previous_infobar| is + // NULL, adds the infobar to |infobar_service|; otherwise, replaces + // |previous_infobar|. Returns the new delegate if it was successfully added. + // |cert| is valid iff cert addition was successful. + static InfoBarDelegate* Create(InfoBarService* infobar_service, + InfoBarDelegate* previous_infobar, + const string16& message, + net::X509Certificate* cert); private: - virtual ~SSLCertAddedInfoBarDelegate(); + SSLCertResultInfoBarDelegate(InfoBarService* infobar_service, + const string16& message, + net::X509Certificate* cert); + virtual ~SSLCertResultInfoBarDelegate(); // ConfirmInfoBarDelegate: virtual gfx::Image* GetIcon() const OVERRIDE; @@ -62,44 +65,60 @@ class SSLCertAddedInfoBarDelegate : public ConfirmInfoBarDelegate { virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; virtual bool Accept() OVERRIDE; - scoped_refptr<net::X509Certificate> cert_; // The cert we added. + string16 message_; + scoped_refptr<net::X509Certificate> cert_; // The cert we added, if any. }; -SSLCertAddedInfoBarDelegate::SSLCertAddedInfoBarDelegate( +// static +InfoBarDelegate* SSLCertResultInfoBarDelegate::Create( + InfoBarService* infobar_service, + InfoBarDelegate* previous_infobar, + const string16& message, + net::X509Certificate* cert) { + scoped_ptr<InfoBarDelegate> infobar( + new SSLCertResultInfoBarDelegate(infobar_service, message, cert)); + return previous_infobar ? + infobar_service->ReplaceInfoBar(previous_infobar, infobar.Pass()) : + infobar_service->AddInfoBar(infobar.Pass()); +} + +SSLCertResultInfoBarDelegate::SSLCertResultInfoBarDelegate( InfoBarService* infobar_service, + const string16& message, net::X509Certificate* cert) : ConfirmInfoBarDelegate(infobar_service), + message_(message), cert_(cert) { } -SSLCertAddedInfoBarDelegate::~SSLCertAddedInfoBarDelegate() { +SSLCertResultInfoBarDelegate::~SSLCertResultInfoBarDelegate() { } -gfx::Image* SSLCertAddedInfoBarDelegate::GetIcon() const { - return GetCertIcon(); +gfx::Image* SSLCertResultInfoBarDelegate::GetIcon() const { + // TODO(davidben): use a more appropriate icon. + return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( + IDR_INFOBAR_SAVE_PASSWORD); } -InfoBarDelegate::Type SSLCertAddedInfoBarDelegate::GetInfoBarType() const { - return PAGE_ACTION_TYPE; +InfoBarDelegate::Type SSLCertResultInfoBarDelegate::GetInfoBarType() const { + return cert_.get() ? PAGE_ACTION_TYPE : WARNING_TYPE; } -string16 SSLCertAddedInfoBarDelegate::GetMessageText() const { - // TODO(evanm): GetDisplayName should return UTF-16. - return l10n_util::GetStringFUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_LABEL, - UTF8ToUTF16(cert_->issuer().GetDisplayName())); +string16 SSLCertResultInfoBarDelegate::GetMessageText() const { + return message_; } -int SSLCertAddedInfoBarDelegate::GetButtons() const { - return BUTTON_OK; +int SSLCertResultInfoBarDelegate::GetButtons() const { + return cert_.get() ? BUTTON_OK : BUTTON_NONE; } -string16 SSLCertAddedInfoBarDelegate::GetButtonLabel( +string16 SSLCertResultInfoBarDelegate::GetButtonLabel( InfoBarButton button) const { DCHECK_EQ(BUTTON_OK, button); return l10n_util::GetStringUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_BUTTON); } -bool SSLCertAddedInfoBarDelegate::Accept() { +bool SSLCertResultInfoBarDelegate::Accept() { ShowCertificateViewer( owner()->GetWebContents(), owner()->GetWebContents()->GetView()->GetTopLevelNativeWindow(), @@ -118,13 +137,8 @@ class SSLTabHelper::SSLAddCertData explicit SSLAddCertData(content::WebContents* contents); virtual ~SSLAddCertData(); - // Displays |delegate| as an infobar in |tab_|, replacing our current one if - // still active. - void ShowInfoBar(InfoBarDelegate* delegate); - - // Same as above, for the common case of wanting to show a simple alert - // message. - void ShowErrorInfoBar(const string16& message); + // Displays an infobar, replacing |infobar_delegate_| if it exists. + void ShowInfoBar(const string16& message, net::X509Certificate* cert); private: // content::NotificationObserver: @@ -152,17 +166,10 @@ SSLTabHelper::SSLAddCertData::SSLAddCertData(content::WebContents* contents) SSLTabHelper::SSLAddCertData::~SSLAddCertData() { } -void SSLTabHelper::SSLAddCertData::ShowInfoBar(InfoBarDelegate* delegate) { - if (infobar_delegate_) - infobar_service_->ReplaceInfoBar(infobar_delegate_, delegate); - else - infobar_service_->AddInfoBar(delegate); - infobar_delegate_ = delegate; -} - -void SSLTabHelper::SSLAddCertData::ShowErrorInfoBar(const string16& message) { - ShowInfoBar(new SimpleAlertInfoBarDelegate( - infobar_service_, GetCertIcon(), message, true)); +void SSLTabHelper::SSLAddCertData::ShowInfoBar(const string16& message, + net::X509Certificate* cert) { + infobar_delegate_ = SSLCertResultInfoBarDelegate::Create( + infobar_service_, infobar_delegate_, message, cert); } void SSLTabHelper::SSLAddCertData::Observe( @@ -203,10 +210,11 @@ void SSLTabHelper::OnVerifyClientCertificateError( SSLAddCertData* add_cert_data = GetAddCertData(handler); // Display an infobar with the error message. // TODO(davidben): Display a more user-friendly error string. - add_cert_data->ShowErrorInfoBar( + add_cert_data->ShowInfoBar( l10n_util::GetStringFUTF16(IDS_ADD_CERT_ERR_INVALID_CERT, base::IntToString16(-error_code), - ASCIIToUTF16(net::ErrorToString(error_code)))); + ASCIIToUTF16(net::ErrorToString(error_code))), + NULL); } void SSLTabHelper::AskToAddClientCertificate( @@ -218,10 +226,12 @@ void SSLTabHelper::OnAddClientCertificateSuccess( scoped_refptr<SSLAddCertHandler> handler) { SSLAddCertData* add_cert_data = GetAddCertData(handler); // Display an infobar to inform the user. - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents_); - add_cert_data->ShowInfoBar(new SSLCertAddedInfoBarDelegate( - infobar_service, handler->cert())); + net::X509Certificate* cert = handler->cert(); + // TODO(evanm): GetDisplayName should return UTF-16. + add_cert_data->ShowInfoBar( + l10n_util::GetStringFUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_LABEL, + UTF8ToUTF16(cert->issuer().GetDisplayName())), + cert); } void SSLTabHelper::OnAddClientCertificateError( @@ -229,10 +239,11 @@ void SSLTabHelper::OnAddClientCertificateError( SSLAddCertData* add_cert_data = GetAddCertData(handler); // Display an infobar with the error message. // TODO(davidben): Display a more user-friendly error string. - add_cert_data->ShowErrorInfoBar( + add_cert_data->ShowInfoBar( l10n_util::GetStringFUTF16(IDS_ADD_CERT_ERR_FAILED, base::IntToString16(-error_code), - ASCIIToUTF16(net::ErrorToString(error_code)))); + ASCIIToUTF16(net::ErrorToString(error_code))), + NULL); } void SSLTabHelper::OnAddClientCertificateFinished( diff --git a/chrome/browser/three_d_api_observer.cc b/chrome/browser/three_d_api_observer.cc index b1190c7..9699e6f 100644 --- a/chrome/browser/three_d_api_observer.cc +++ b/chrome/browser/three_d_api_observer.cc @@ -29,12 +29,16 @@ enum ThreeDInfobarDismissalHistogram { class ThreeDAPIInfoBarDelegate : public ConfirmInfoBarDelegate { public: + // Creates a 3D API delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + const GURL& url, + content::ThreeDAPIType requester); + + private: ThreeDAPIInfoBarDelegate( InfoBarService* owner, const GURL& url, content::ThreeDAPIType requester); - - private: virtual ~ThreeDAPIInfoBarDelegate(); // ConfirmInfoBarDelegate: @@ -57,6 +61,14 @@ class ThreeDAPIInfoBarDelegate : public ConfirmInfoBarDelegate { DISALLOW_COPY_AND_ASSIGN(ThreeDAPIInfoBarDelegate); }; +// static +void ThreeDAPIInfoBarDelegate::Create(InfoBarService* infobar_service, + const GURL& url, + content::ThreeDAPIType requester) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new ThreeDAPIInfoBarDelegate(infobar_service, url, requester))); +} + ThreeDAPIInfoBarDelegate::ThreeDAPIInfoBarDelegate( InfoBarService* owner, const GURL& url, @@ -157,6 +169,6 @@ ThreeDAPIObserver::~ThreeDAPIObserver() {} void ThreeDAPIObserver::DidBlock3DAPIs(const GURL& url, content::ThreeDAPIType requester) { - InfoBarService* service = InfoBarService::FromWebContents(web_contents()); - service->AddInfoBar(new ThreeDAPIInfoBarDelegate(service, url, requester)); + ThreeDAPIInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents()), url, requester); } diff --git a/chrome/browser/translate/translate_infobar_delegate.cc b/chrome/browser/translate/translate_infobar_delegate.cc index 8c205a4..003d468 100644 --- a/chrome/browser/translate/translate_infobar_delegate.cc +++ b/chrome/browser/translate/translate_infobar_delegate.cc @@ -25,48 +25,51 @@ using content::NavigationEntry; // static const size_t TranslateInfoBarDelegate::kNoIndex = static_cast<size_t>(-1); -// static -TranslateInfoBarDelegate* TranslateInfoBarDelegate::CreateDelegate( - Type infobar_type, - InfoBarService* infobar_service, - PrefService* prefs, - const std::string& original_language, - const std::string& target_language) { - DCHECK_NE(TRANSLATION_ERROR, infobar_type); - // These must be validated by our callers. - DCHECK(TranslateManager::IsSupportedLanguage(target_language)); - // The original language can only be "unknown" for the "translating" - // infobar, which is the case when the user started a translation from the - // context menu. - DCHECK(TranslateManager::IsSupportedLanguage(original_language) || - ((infobar_type == TRANSLATING) && - (original_language == chrome::kUnknownLanguageCode))); - TranslateInfoBarDelegate* delegate = - new TranslateInfoBarDelegate(infobar_type, - TranslateErrors::NONE, - infobar_service, - prefs, - original_language, - target_language); - DCHECK_NE(kNoIndex, delegate->target_language_index()); - return delegate; +TranslateInfoBarDelegate::~TranslateInfoBarDelegate() { } -TranslateInfoBarDelegate* TranslateInfoBarDelegate::CreateErrorDelegate( - TranslateErrors::Type error_type, - InfoBarService* infobar_service, - PrefService* prefs, - const std::string& original_language, - const std::string& target_language) { - return new TranslateInfoBarDelegate(TRANSLATION_ERROR, - error_type, - infobar_service, - prefs, - original_language, - target_language); -} +// static +void TranslateInfoBarDelegate::Create(InfoBarService* infobar_service, + bool replace_existing_infobar, + Type infobar_type, + TranslateErrors::Type error_type, + PrefService* prefs, + const std::string& original_language, + const std::string& target_language) { + // Check preconditions. + if (infobar_type != TRANSLATION_ERROR) { + DCHECK(TranslateManager::IsSupportedLanguage(target_language)); + if (!TranslateManager::IsSupportedLanguage(original_language)) { + // The original language can only be "unknown" for the "translating" + // infobar, which is the case when the user started a translation from the + // context menu. + DCHECK_EQ(TRANSLATING, infobar_type); + DCHECK_EQ(chrome::kUnknownLanguageCode, original_language); + } + } -TranslateInfoBarDelegate::~TranslateInfoBarDelegate() { + // Find any existing translate infobar delegate. + TranslateInfoBarDelegate* old_delegate = NULL; + for (size_t i = 0; i < infobar_service->GetInfoBarCount(); ++i) { + old_delegate = + infobar_service->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); + if (old_delegate) + break; + } + + // Create the new delegate. + scoped_ptr<TranslateInfoBarDelegate> infobar( + new TranslateInfoBarDelegate(infobar_type, error_type, infobar_service, + prefs, original_language, target_language)); + infobar->UpdateBackgroundAnimation(old_delegate); + + // Add the new delegate if necessary. + if (!old_delegate) { + infobar_service->AddInfoBar(infobar.PassAs<InfoBarDelegate>()); + } else if (replace_existing_infobar) { + infobar_service->ReplaceInfoBar(old_delegate, + infobar.PassAs<InfoBarDelegate>()); + } } void TranslateInfoBarDelegate::Translate() { diff --git a/chrome/browser/translate/translate_infobar_delegate.h b/chrome/browser/translate/translate_infobar_delegate.h index d931c10..b051db9 100644 --- a/chrome/browser/translate/translate_infobar_delegate.h +++ b/chrome/browser/translate/translate_infobar_delegate.h @@ -37,29 +37,29 @@ class TranslateInfoBarDelegate : public InfoBarDelegate { static const size_t kNoIndex; - // Factory method to create a non-error translate infobar. |original_language| - // and |target_language| must be ASCII language codes (e.g. "en", "fr", etc.) - // for languages the TranslateManager supports translating. The lone exception - // is when the user initiates translation from the context menu, in which case - // it's legal to call this with |type| == TRANSLATING and - // |originalLanguage| == kUnknownLanguageCode. - static TranslateInfoBarDelegate* CreateDelegate( - Type infobar_type, - InfoBarService* infobar_service, - PrefService* prefs, - const std::string& original_language, - const std::string& target_language); - - // Factory method to create an error translate infobar. - static TranslateInfoBarDelegate* CreateErrorDelegate( - TranslateErrors::Type error_type, - InfoBarService* infobar_service, - PrefService* prefs, - const std::string& original_language, - const std::string& target_language); - virtual ~TranslateInfoBarDelegate(); + // Factory method to create a translate infobar. |error_type| must be + // specified iff |infobar_type| == TRANSLATION_ERROR. For other infobar + // types, |original_language| and |target_language| must be ASCII language + // codes (e.g. "en", "fr", etc.) for languages the TranslateManager supports + // translating. The lone exception is when the user initiates translation + // from the context menu, in which case it's legal to call this with + // |infobar_type| == TRANSLATING and + // |original_language| == kUnknownLanguageCode. + // + // If |replace_existing_infobar| is true, the infobar is created and added to + // |infobar_service|, replacing any other translate infobar already present + // there. Otherwise, the infobar will only be added if there is no other + // translate infobar already present. + static void Create(InfoBarService* infobar_service, + bool replace_existing_infobar, + Type infobar_type, + TranslateErrors::Type error_type, + PrefService* prefs, + const std::string& original_language, + const std::string& target_language); + // Returns the number of languages supported. size_t num_languages() const { return languages_.size(); } diff --git a/chrome/browser/translate/translate_manager.cc b/chrome/browser/translate/translate_manager.cc index 794507a..7ba8f36 100644 --- a/chrome/browser/translate/translate_manager.cc +++ b/chrome/browser/translate/translate_manager.cc @@ -465,16 +465,14 @@ void TranslateManager::OnURLFetchComplete(const net::URLFetcher* source) { if (error) { Profile* profile = Profile::FromBrowserContext(web_contents->GetBrowserContext()); - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - ShowInfoBar( - web_contents, - TranslateInfoBarDelegate::CreateErrorDelegate( - TranslateErrors::NETWORK, - infobar_service, - profile->GetPrefs(), - request.source_lang, - request.target_lang)); + TranslateInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents), + true, + TranslateInfoBarDelegate::TRANSLATION_ERROR, + TranslateErrors::NETWORK, + profile->GetPrefs(), + request.source_lang, + request.target_lang); } else { // Translate the page. DoTranslatePage(web_contents, translate_script_, @@ -495,11 +493,6 @@ void TranslateManager::OnURLFetchComplete(const net::URLFetcher* source) { } } -// static -bool TranslateManager::IsShowingTranslateInfobar(WebContents* web_contents) { - return GetTranslateInfoBarDelegate(web_contents) != NULL; -} - TranslateManager::TranslateManager() : ALLOW_THIS_IN_INITIALIZER_LIST(weak_method_factory_(this)), translate_script_expiration_delay_( @@ -539,10 +532,6 @@ void TranslateManager::InitiateTranslation(WebContents* web_contents, return; } - // If there is already a translate infobar showing, don't show another one. - if (GetTranslateInfoBarDelegate(web_contents)) - return; - std::string target_lang = GetTargetLanguage(prefs); std::string language_code = GetLanguageCode(page_lang); // Nothing to do if either the language Chrome is in or the language of the @@ -592,13 +581,11 @@ void TranslateManager::InitiateTranslation(WebContents* web_contents, return; } - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); // Prompts the user if he/she wants the page translated. - infobar_service->AddInfoBar( - TranslateInfoBarDelegate::CreateDelegate( - TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar_service, - profile->GetPrefs(), language_code, target_lang)); + TranslateInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents), false, + TranslateInfoBarDelegate::BEFORE_TRANSLATE, TranslateErrors::NONE, + profile->GetPrefs(), language_code, target_lang); } void TranslateManager::InitiateTranslationPosted( @@ -628,11 +615,10 @@ void TranslateManager::TranslatePage(WebContents* web_contents, Profile* profile = Profile::FromBrowserContext(web_contents->GetBrowserContext()); - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - ShowInfoBar(web_contents, TranslateInfoBarDelegate::CreateDelegate( - TranslateInfoBarDelegate::TRANSLATING, infobar_service, - profile->GetPrefs(), source_lang, target_lang)); + TranslateInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents), true, + TranslateInfoBarDelegate::TRANSLATING, TranslateErrors::NONE, + profile->GetPrefs(), source_lang, target_lang); if (!translate_script_.empty()) { DoTranslatePage(web_contents, translate_script_, source_lang, target_lang); @@ -721,35 +707,24 @@ void TranslateManager::DoTranslatePage(WebContents* web_contents, void TranslateManager::PageTranslated(WebContents* web_contents, PageTranslatedDetails* details) { - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - Profile* profile = - Profile::FromBrowserContext(web_contents->GetBrowserContext()); - PrefService* prefs = profile->GetPrefs(); - - // Create the new infobar to display. - TranslateInfoBarDelegate* infobar; - if (details->error_type != TranslateErrors::NONE) { - infobar = TranslateInfoBarDelegate::CreateErrorDelegate( - details->error_type, - infobar_service, - prefs, - details->source_language, - details->target_language); - } else if (!IsSupportedLanguage(details->source_language)) { + if ((details->error_type == TranslateErrors::NONE) && + !IsSupportedLanguage(details->source_language)) { // TODO(jcivelli): http://crbug.com/9390 We should change the "after // translate" infobar to support unknown as the original // language. UMA_HISTOGRAM_COUNTS("Translate.ServerReportedUnsupportedLanguage", 1); - infobar = TranslateInfoBarDelegate::CreateErrorDelegate( - TranslateErrors::UNSUPPORTED_LANGUAGE, infobar_service, - prefs, details->source_language, details->target_language); - } else { - infobar = TranslateInfoBarDelegate::CreateDelegate( - TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar_service, - prefs, details->source_language, details->target_language); + details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; } - ShowInfoBar(web_contents, infobar); + Profile* profile = + Profile::FromBrowserContext(web_contents->GetBrowserContext()); + PrefService* prefs = profile->GetPrefs(); + TranslateInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents), true, + (details->error_type == TranslateErrors::NONE) ? + TranslateInfoBarDelegate::AFTER_TRANSLATE : + TranslateInfoBarDelegate::TRANSLATION_ERROR, + details->error_type, prefs, details->source_language, + details->target_language); } bool TranslateManager::IsAcceptLanguage(WebContents* web_contents, @@ -868,24 +843,6 @@ void TranslateManager::RequestTranslateScript() { translate_script_request_pending_->Start(); } -void TranslateManager::ShowInfoBar(content::WebContents* web_contents, - TranslateInfoBarDelegate* infobar) { - DCHECK(infobar != NULL); - TranslateInfoBarDelegate* old_infobar = - GetTranslateInfoBarDelegate(web_contents); - infobar->UpdateBackgroundAnimation(old_infobar); - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - if (!infobar_service) - return; - if (old_infobar) { - // There already is a translate infobar, simply replace it. - infobar_service->ReplaceInfoBar(old_infobar, infobar); - } else { - infobar_service->AddInfoBar(infobar); - } -} - // static std::string TranslateManager::GetTargetLanguage(PrefService* prefs) { std::string ui_lang = @@ -910,21 +867,3 @@ std::string TranslateManager::GetTargetLanguage(PrefService* prefs) { } return std::string(); } - -// static -TranslateInfoBarDelegate* TranslateManager::GetTranslateInfoBarDelegate( - WebContents* web_contents) { - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - if (!infobar_service) - return NULL; - - for (size_t i = 0; i < infobar_service->GetInfoBarCount(); ++i) { - TranslateInfoBarDelegate* delegate = - infobar_service->GetInfoBarDelegateAt(i)-> - AsTranslateInfoBarDelegate(); - if (delegate) - return delegate; - } - return NULL; -} diff --git a/chrome/browser/translate/translate_manager.h b/chrome/browser/translate/translate_manager.h index 2308267..3e67ae0 100644 --- a/chrome/browser/translate/translate_manager.h +++ b/chrome/browser/translate/translate_manager.h @@ -93,9 +93,6 @@ class TranslateManager : public content::NotificationObserver, base::TimeDelta::FromMilliseconds(delay_ms); } - // Convenience method to know if a tab is showing a translate infobar. - static bool IsShowingTranslateInfobar(content::WebContents* web_contents); - // Returns true if the URL can be translated. static bool IsTranslatableURL(const GURL& url); @@ -174,11 +171,6 @@ class TranslateManager : public content::NotificationObserver, // to translate it). void RequestTranslateScript(); - // Shows the specified translate |infobar| in the given |tab|. If a current - // translate infobar is showing, it just replaces it with the new one. - void ShowInfoBar(content::WebContents* web_contents, - TranslateInfoBarDelegate* infobar); - // Returns the language to translate to. The language returned is the // first language found in the following list that is supported by the // translation service: @@ -187,10 +179,6 @@ class TranslateManager : public content::NotificationObserver, // If no language is found then an empty string is returned. static std::string GetTargetLanguage(PrefService* prefs); - // Returns the translate info bar showing in |tab| or NULL if none is showing. - static TranslateInfoBarDelegate* GetTranslateInfoBarDelegate( - content::WebContents* web_contents); - content::NotificationRegistrar notification_registrar_; // Each PrefChangeRegistrar only tracks a single PrefService, so a map from diff --git a/chrome/browser/ui/auto_login_info_bar_delegate.cc b/chrome/browser/ui/auto_login_info_bar_delegate.cc index c875001..b2b9d131 100644 --- a/chrome/browser/ui/auto_login_info_bar_delegate.cc +++ b/chrome/browser/ui/auto_login_info_bar_delegate.cc @@ -165,22 +165,11 @@ void AutoLoginRedirector::RedirectToMergeSession(const std::string& token) { AutoLoginInfoBarDelegate::Params::Params() {} AutoLoginInfoBarDelegate::Params::~Params() {} -AutoLoginInfoBarDelegate::AutoLoginInfoBarDelegate( - InfoBarService* owner, - const Params& params) - : ConfirmInfoBarDelegate(owner), - params_(params), - button_pressed_(false) { - RecordHistogramAction(HISTOGRAM_SHOWN); - registrar_.Add(this, - chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, - content::Source<Profile>(Profile::FromBrowserContext( - owner->GetWebContents()->GetBrowserContext()))); -} - -AutoLoginInfoBarDelegate::~AutoLoginInfoBarDelegate() { - if (!button_pressed_) - RecordHistogramAction(HISTOGRAM_IGNORED); +// static +void AutoLoginInfoBarDelegate::Create(InfoBarService* infobar_service, + const Params& params) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new AutoLoginInfoBarDelegate(infobar_service, params))); } AutoLoginInfoBarDelegate* @@ -237,6 +226,24 @@ string16 AutoLoginInfoBarDelegate::GetMessageText( UTF8ToUTF16(username)); } +AutoLoginInfoBarDelegate::AutoLoginInfoBarDelegate( + InfoBarService* owner, + const Params& params) + : ConfirmInfoBarDelegate(owner), + params_(params), + button_pressed_(false) { + RecordHistogramAction(HISTOGRAM_SHOWN); + registrar_.Add(this, + chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, + content::Source<Profile>(Profile::FromBrowserContext( + owner->GetWebContents()->GetBrowserContext()))); +} + +AutoLoginInfoBarDelegate::~AutoLoginInfoBarDelegate() { + if (!button_pressed_) + RecordHistogramAction(HISTOGRAM_IGNORED); +} + void AutoLoginInfoBarDelegate::RecordHistogramAction(int action) { UMA_HISTOGRAM_ENUMERATION("AutoLogin.Regular", action, HISTOGRAM_MAX); } diff --git a/chrome/browser/ui/auto_login_info_bar_delegate.h b/chrome/browser/ui/auto_login_info_bar_delegate.h index 1cb3f2d..d209b63 100644 --- a/chrome/browser/ui/auto_login_info_bar_delegate.h +++ b/chrome/browser/ui/auto_login_info_bar_delegate.h @@ -40,8 +40,8 @@ class AutoLoginInfoBarDelegate : public ConfirmInfoBarDelegate, std::string username; }; - AutoLoginInfoBarDelegate(InfoBarService* owner, const Params& params); - virtual ~AutoLoginInfoBarDelegate(); + // Creates an autologin delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, const Params& params); // ConfirmInfoBarDelegate: virtual void InfoBarDismissed() OVERRIDE; @@ -67,6 +67,9 @@ class AutoLoginInfoBarDelegate : public ConfirmInfoBarDelegate, const std::string& args() const { return params_.args; } private: + AutoLoginInfoBarDelegate(InfoBarService* owner, const Params& params); + virtual ~AutoLoginInfoBarDelegate(); + void RecordHistogramAction(int action); const Params params_; diff --git a/chrome/browser/ui/auto_login_prompter.cc b/chrome/browser/ui/auto_login_prompter.cc index ac107d2..0bc2b24 100644 --- a/chrome/browser/ui/auto_login_prompter.cc +++ b/chrome/browser/ui/auto_login_prompter.cc @@ -178,10 +178,8 @@ void AutoLoginPrompter::Observe(int type, InfoBarService* infobar_service = InfoBarService::FromWebContents(web_contents_); // |infobar_service| is NULL for WebContents hosted in WebDialog. - if (infobar_service) { - infobar_service->AddInfoBar( - new AutoLoginInfoBarDelegate(infobar_service, params_)); - } + if (infobar_service) + AutoLoginInfoBarDelegate::Create(infobar_service, params_); } // Either we couldn't add the infobar, we added the infobar, or the tab // contents was destroyed before the navigation completed. In any case diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc index 9debd23..455d3dc 100644 --- a/chrome/browser/ui/browser.cc +++ b/chrome/browser/ui/browser.cc @@ -885,11 +885,9 @@ void Browser::JSOutOfMemoryHelper(WebContents* web_contents) { if (!infobar_service) return; - infobar_service->AddInfoBar(new SimpleAlertInfoBarDelegate( - infobar_service, - NULL, - l10n_util::GetStringUTF16(IDS_JS_OUT_OF_MEMORY_PROMPT), - true)); + SimpleAlertInfoBarDelegate::Create( + infobar_service, NULL, + l10n_util::GetStringUTF16(IDS_JS_OUT_OF_MEMORY_PROMPT), true); } // static @@ -929,29 +927,8 @@ void Browser::RegisterProtocolHandlerHelper(WebContents* web_contents, window->GetLocationBar()->UpdateContentSettingsIcons(); } - content::RecordAction( - UserMetricsAction("RegisterProtocolHandler.InfoBar_Shown")); - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - - RegisterProtocolHandlerInfoBarDelegate* rph_delegate = - new RegisterProtocolHandlerInfoBarDelegate(infobar_service, - registry, - handler); - - for (size_t i = 0; i < infobar_service->GetInfoBarCount(); i++) { - InfoBarDelegate* delegate = infobar_service->GetInfoBarDelegateAt(i); - RegisterProtocolHandlerInfoBarDelegate* cast_delegate = - delegate->AsRegisterProtocolHandlerInfoBarDelegate(); - if (cast_delegate != NULL && cast_delegate->IsReplacedBy(rph_delegate)) { - infobar_service->ReplaceInfoBar(cast_delegate, rph_delegate); - rph_delegate = NULL; - break; - } - } - - if (rph_delegate != NULL) - infobar_service->AddInfoBar(rph_delegate); + RegisterProtocolHandlerInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents), registry, handler); } // static @@ -972,38 +949,6 @@ void Browser::FindReplyHelper(WebContents* web_contents, final_update); } -// static -void Browser::RequestMediaAccessPermissionHelper( - content::WebContents* web_contents, - const content::MediaStreamRequest& request, - const content::MediaResponseCallback& callback) { - Profile* profile = - Profile::FromBrowserContext(web_contents->GetBrowserContext()); - - scoped_ptr<MediaStreamDevicesController> - controller(new MediaStreamDevicesController(profile, - request, - callback)); - if (!controller->DismissInfoBarAndTakeActionOnSettings()) { - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - InfoBarDelegate* old_infobar = NULL; - for (size_t i = 0; i < infobar_service->GetInfoBarCount(); ++i) { - old_infobar = infobar_service->GetInfoBarDelegateAt(i)-> - AsMediaStreamInfoBarDelegate(); - if (old_infobar) - break; - } - - InfoBarDelegate* infobar = - new MediaStreamInfoBarDelegate(infobar_service, controller.release()); - if (old_infobar) - infobar_service->ReplaceInfoBar(old_infobar, infobar); - else - infobar_service->AddInfoBar(infobar); - } -} - void Browser::UpdateUIForNavigationInTab(WebContents* contents, content::PageTransition transition, bool user_initiated) { @@ -1606,13 +1551,9 @@ void Browser::RendererResponsive(WebContents* source) { } void Browser::WorkerCrashed(WebContents* source) { - InfoBarService* infobar_service = - InfoBarService::FromWebContents(source); - infobar_service->AddInfoBar(new SimpleAlertInfoBarDelegate( - infobar_service, - NULL, - l10n_util::GetStringUTF16(IDS_WEBWORKER_CRASHED_PROMPT), - true)); + SimpleAlertInfoBarDelegate::Create( + InfoBarService::FromWebContents(source), NULL, + l10n_util::GetStringUTF16(IDS_WEBWORKER_CRASHED_PROMPT), true); } void Browser::DidNavigateMainFramePostCommit(WebContents* web_contents) { @@ -1692,7 +1633,7 @@ void Browser::RegisterIntentHandler( WebContents* web_contents, const webkit_glue::WebIntentServiceData& data, bool user_gesture) { - RegisterIntentHandlerHelper(web_contents, data, user_gesture); + RegisterIntentHandlerInfoBarDelegate::Create(web_contents, data); } void Browser::WebIntentDispatch( @@ -1788,7 +1729,7 @@ void Browser::RequestMediaAccessPermission( content::WebContents* web_contents, const content::MediaStreamRequest& request, const content::MediaResponseCallback& callback) { - RequestMediaAccessPermissionHelper(web_contents, request, callback); + MediaStreamInfoBarDelegate::Create(web_contents, request, callback); } bool Browser::RequestPpapiBrokerPermission( @@ -1796,7 +1737,7 @@ bool Browser::RequestPpapiBrokerPermission( const GURL& url, const FilePath& plugin_path, const base::Callback<void(bool)>& callback) { - PepperBrokerInfoBarDelegate::Show(web_contents, url, plugin_path, callback); + PepperBrokerInfoBarDelegate::Create(web_contents, url, plugin_path, callback); return true; } diff --git a/chrome/browser/ui/browser.h b/chrome/browser/ui/browser.h index 781447a..834e8e1 100644 --- a/chrome/browser/ui/browser.h +++ b/chrome/browser/ui/browser.h @@ -392,14 +392,6 @@ class Browser : public TabStripModelObserver, bool user_gesture, BrowserWindow* window); - // Helper function to register an intent handler. - // |data| is the registered handler data. |user_gesture| is true if the call - // was made in the context of a user gesture. - static void RegisterIntentHandlerHelper( - content::WebContents* web_contents, - const webkit_glue::WebIntentServiceData& data, - bool user_gesture); - // Helper function to handle find results. static void FindReplyHelper(content::WebContents* web_contents, int request_id, @@ -408,12 +400,6 @@ class Browser : public TabStripModelObserver, int active_match_ordinal, bool final_update); - // Helper function to handle media access requests. - static void RequestMediaAccessPermissionHelper( - content::WebContents* web_contents, - const content::MediaStreamRequest& request, - const content::MediaResponseCallback& callback); - // Called by chrome::Navigate() when a navigation has occurred in a tab in // this Browser. Updates the UI for the start of this navigation. void UpdateUIForNavigationInTab(content::WebContents* contents, diff --git a/chrome/browser/ui/chrome_select_file_policy.cc b/chrome/browser/ui/chrome_select_file_policy.cc index 4fe9337..a50cc74 100644 --- a/chrome/browser/ui/chrome_select_file_policy.cc +++ b/chrome/browser/ui/chrome_select_file_policy.cc @@ -29,14 +29,9 @@ bool ChromeSelectFilePolicy::CanOpenSelectFileDialog() { void ChromeSelectFilePolicy::SelectFileDenied() { // Show the InfoBar saying that file-selection dialogs are disabled. if (source_contents_) { - InfoBarService* infobar_service = - InfoBarService::FromWebContents(source_contents_); - DCHECK(infobar_service); - infobar_service->AddInfoBar(new SimpleAlertInfoBarDelegate( - infobar_service, - NULL, - l10n_util::GetStringUTF16(IDS_FILE_SELECTION_DIALOG_INFOBAR), - true)); + SimpleAlertInfoBarDelegate::Create( + InfoBarService::FromWebContents(source_contents_), NULL, + l10n_util::GetStringUTF16(IDS_FILE_SELECTION_DIALOG_INFOBAR), true); } else { LOG(WARNING) << "File-selection dialogs are disabled but no WebContents " << "is given to display the InfoBar."; diff --git a/chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.mm b/chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.mm index 1e0a2dd..7f8e8a5 100644 --- a/chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.mm +++ b/chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.mm @@ -214,10 +214,8 @@ void CollectedCookiesMac::OnConstrainedWindowClosed( - (void)windowWillClose:(NSNotification*)notif { if (contentSettingsChanged_) { - InfoBarService* infobarService = - InfoBarService::FromWebContents(webContents_); - infobarService->AddInfoBar( - new CollectedCookiesInfoBarDelegate(infobarService)); + CollectedCookiesInfoBarDelegate::Create( + InfoBarService::FromWebContents(webContents_)); } [allowedOutlineView_ setDelegate:nil]; [blockedOutlineView_ setDelegate:nil]; diff --git a/chrome/browser/ui/cocoa/infobars/infobar_container_controller.h b/chrome/browser/ui/cocoa/infobars/infobar_container_controller.h index 45639da..77badeb 100644 --- a/chrome/browser/ui/cocoa/infobars/infobar_container_controller.h +++ b/chrome/browser/ui/cocoa/infobars/infobar_container_controller.h @@ -127,10 +127,10 @@ const CGFloat kTipHeight = 12.0; @interface InfoBarContainerController (JustForTesting) // Removes all infobar views. Infobars which were already closing will be -// completely closed (i.e. InfobarDelegate::InfoBarClosed() will be called and -// we'll get a callback to removeController). Other infobars will simply stop -// animating and disappear. Callers must call positionInfoBarsAndRedraw() -// after calling this method. +// completely closed (i.e. the InfoBarDelegate will be deleted and we'll get a +// callback to removeController). Other infobars will simply stop animating and +// disappear. Callers must call positionInfoBarsAndRedraw() after calling this +// method. - (void)removeAllInfoBars; @end diff --git a/chrome/browser/ui/cocoa/infobars/infobar_controller.mm b/chrome/browser/ui/cocoa/infobars/infobar_controller.mm index be70ccd..5fa4bbf 100644 --- a/chrome/browser/ui/cocoa/infobars/infobar_controller.mm +++ b/chrome/browser/ui/cocoa/infobars/infobar_controller.mm @@ -248,10 +248,8 @@ const float kAnimateCloseDuration = 0.12; if (!infoBarClosing_) return; - // Notify the delegate that the infobar was closed. The delegate will delete - // itself as a result of InfoBarClosed(), so we null out its pointer. if (delegate_) { - delegate_->InfoBarClosed(); + delete delegate_; delegate_ = NULL; } diff --git a/chrome/browser/ui/cocoa/keystone_infobar_delegate.mm b/chrome/browser/ui/cocoa/keystone_infobar_delegate.mm index aa37c57..35e50c4 100644 --- a/chrome/browser/ui/cocoa/keystone_infobar_delegate.mm +++ b/chrome/browser/ui/cocoa/keystone_infobar_delegate.mm @@ -38,10 +38,13 @@ namespace { class KeystonePromotionInfoBarDelegate : public ConfirmInfoBarDelegate { public: - KeystonePromotionInfoBarDelegate(InfoBarService* infobar_service, - PrefService* prefs); + // If there's an active tab, creates a keystone promotion delegate and adds it + // to the InfoBarService associated with that tab. + static void Create(); private: + KeystonePromotionInfoBarDelegate(InfoBarService* infobar_service, + PrefService* prefs); virtual ~KeystonePromotionInfoBarDelegate(); // Sets this info bar to be able to expire. Called a predetermined amount @@ -69,6 +72,24 @@ class KeystonePromotionInfoBarDelegate : public ConfirmInfoBarDelegate { DISALLOW_COPY_AND_ASSIGN(KeystonePromotionInfoBarDelegate); }; +// static +void KeystonePromotionInfoBarDelegate::Create() { + Browser* browser = chrome::GetLastActiveBrowser(); + if (browser) { + content::WebContents* webContents = chrome::GetActiveWebContents(browser); + + if (webContents) { + InfoBarService* infobar_service = + InfoBarService::FromWebContents(webContents); + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new KeystonePromotionInfoBarDelegate( + infobar_service, + Profile::FromBrowserContext( + webContents->GetBrowserContext())->GetPrefs()))); + } + } +} + KeystonePromotionInfoBarDelegate::KeystonePromotionInfoBarDelegate( InfoBarService* infobar_service, PrefService* prefs) @@ -189,24 +210,7 @@ bool KeystonePromotionInfoBarDelegate::ShouldExpireInternal( if (status != kAutoupdateRegisterFailed && [[KeystoneGlue defaultKeystoneGlue] needsPromotion]) { - Browser* browser = chrome::GetLastActiveBrowser(); - if (browser) { - content::WebContents* webContents = chrome::GetActiveWebContents(browser); - - // Only show if no other info bars are showing, because that's how the - // default browser info bar works. - if (webContents) { - InfoBarService* infobarService = - InfoBarService::FromWebContents(webContents); - if (infobarService->GetInfoBarCount() == 0) { - infobarService->AddInfoBar( - new KeystonePromotionInfoBarDelegate( - infobarService, - Profile::FromBrowserContext( - webContents->GetBrowserContext())->GetPrefs())); - } - } - } + KeystonePromotionInfoBarDelegate::Create(); } [self release]; diff --git a/chrome/browser/ui/collected_cookies_infobar_delegate.cc b/chrome/browser/ui/collected_cookies_infobar_delegate.cc index 53ac137..2cd6615 100644 --- a/chrome/browser/ui/collected_cookies_infobar_delegate.cc +++ b/chrome/browser/ui/collected_cookies_infobar_delegate.cc @@ -12,6 +12,12 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" +// static +void CollectedCookiesInfoBarDelegate::Create(InfoBarService* infobar_service) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new CollectedCookiesInfoBarDelegate(infobar_service))); +} + CollectedCookiesInfoBarDelegate::CollectedCookiesInfoBarDelegate( InfoBarService* infobar_service) : ConfirmInfoBarDelegate(infobar_service) { diff --git a/chrome/browser/ui/collected_cookies_infobar_delegate.h b/chrome/browser/ui/collected_cookies_infobar_delegate.h index fd97d75..1ad0a54 100644 --- a/chrome/browser/ui/collected_cookies_infobar_delegate.h +++ b/chrome/browser/ui/collected_cookies_infobar_delegate.h @@ -16,9 +16,12 @@ class InfoBarService; // the reload right from the infobar. class CollectedCookiesInfoBarDelegate : public ConfirmInfoBarDelegate { public: - explicit CollectedCookiesInfoBarDelegate(InfoBarService* infobar_service); + // Creates a collected cookies delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service); private: + explicit CollectedCookiesInfoBarDelegate(InfoBarService* infobar_service); + // ConfirmInfoBarDelegate overrides. virtual gfx::Image* GetIcon() const OVERRIDE; virtual Type GetInfoBarType() const OVERRIDE; diff --git a/chrome/browser/ui/content_settings/content_setting_bubble_model.cc b/chrome/browser/ui/content_settings/content_setting_bubble_model.cc index 075ca46..c6fb16f 100644 --- a/chrome/browser/ui/content_settings/content_setting_bubble_model.cc +++ b/chrome/browser/ui/content_settings/content_setting_bubble_model.cc @@ -379,10 +379,8 @@ ContentSettingCookiesBubbleModel::ContentSettingCookiesBubbleModel( ContentSettingCookiesBubbleModel::~ContentSettingCookiesBubbleModel() { if (settings_changed()) { - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents()); - infobar_service->AddInfoBar( - new CollectedCookiesInfoBarDelegate(infobar_service)); + CollectedCookiesInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents())); } } diff --git a/chrome/browser/ui/gtk/collected_cookies_gtk.cc b/chrome/browser/ui/gtk/collected_cookies_gtk.cc index 1d2fbe8..c622ffa 100644 --- a/chrome/browser/ui/gtk/collected_cookies_gtk.cc +++ b/chrome/browser/ui/gtk/collected_cookies_gtk.cc @@ -460,10 +460,8 @@ void CollectedCookiesGtk::Observe(int type, void CollectedCookiesGtk::OnClose(GtkWidget* close_button) { if (status_changed_) { - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents_); - infobar_service->AddInfoBar( - new CollectedCookiesInfoBarDelegate(infobar_service)); + CollectedCookiesInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents_)); } window_->CloseWebContentsModalDialog(); } diff --git a/chrome/browser/ui/hung_plugin_tab_helper.cc b/chrome/browser/ui/hung_plugin_tab_helper.cc index c6fa5e3..2463be4 100644 --- a/chrome/browser/ui/hung_plugin_tab_helper.cc +++ b/chrome/browser/ui/hung_plugin_tab_helper.cc @@ -124,11 +124,12 @@ void KillPluginOnIOThread(int child_id) { class HungPluginInfoBarDelegate : public ConfirmInfoBarDelegate { public: - HungPluginInfoBarDelegate(HungPluginTabHelper* helper, - InfoBarService* infobar_service, - int plugin_child_id, - const string16& plugin_name); - virtual ~HungPluginInfoBarDelegate(); + // Creates a hung plugin delegate and adds it to |infobar_service|. Returns + // the delegate if it was successfully added. + static HungPluginInfoBarDelegate* Create(InfoBarService* infobar_service, + HungPluginTabHelper* helper, + int plugin_child_id, + const string16& plugin_name); // ConfirmInfoBarDelegate: virtual gfx::Image* GetIcon() const OVERRIDE; @@ -138,6 +139,12 @@ class HungPluginInfoBarDelegate : public ConfirmInfoBarDelegate { virtual bool Accept() OVERRIDE; private: + HungPluginInfoBarDelegate(HungPluginTabHelper* helper, + InfoBarService* infobar_service, + int plugin_child_id, + const string16& plugin_name); + virtual ~HungPluginInfoBarDelegate(); + HungPluginTabHelper* helper_; int plugin_child_id_; @@ -146,6 +153,17 @@ class HungPluginInfoBarDelegate : public ConfirmInfoBarDelegate { gfx::Image* icon_; }; +// static +HungPluginInfoBarDelegate* HungPluginInfoBarDelegate::Create( + InfoBarService* infobar_service, + HungPluginTabHelper* helper, + int plugin_child_id, + const string16& plugin_name) { + return static_cast<HungPluginInfoBarDelegate*>(infobar_service->AddInfoBar( + scoped_ptr<InfoBarDelegate>(new HungPluginInfoBarDelegate( + helper, infobar_service, plugin_child_id, plugin_name)))); +} + HungPluginInfoBarDelegate::HungPluginInfoBarDelegate( HungPluginTabHelper* helper, InfoBarService* infobar_service, @@ -362,9 +380,9 @@ void HungPluginTabHelper::ShowBar(int child_id, PluginState* state) { return; DCHECK(!state->info_bar); - state->info_bar = new HungPluginInfoBarDelegate(this, infobar_service, - child_id, state->name); - infobar_service->AddInfoBar(state->info_bar); + state->info_bar = + HungPluginInfoBarDelegate::Create(infobar_service, this, child_id, + state->name); } void HungPluginTabHelper::CloseBar(PluginState* state) { diff --git a/chrome/browser/ui/media_stream_infobar_delegate.cc b/chrome/browser/ui/media_stream_infobar_delegate.cc index 24452bc..e7f9171 100644 --- a/chrome/browser/ui/media_stream_infobar_delegate.cc +++ b/chrome/browser/ui/media_stream_infobar_delegate.cc @@ -8,6 +8,7 @@ #include "base/utf_string_conversions.h" #include "chrome/browser/api/infobars/infobar_service.h" #include "chrome/browser/google/google_util.h" +#include "chrome/browser/profiles/profile.h" #include "chrome/common/url_constants.h" #include "content/public/browser/web_contents.h" #include "googleurl/src/gurl.h" @@ -16,17 +17,40 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -MediaStreamInfoBarDelegate::MediaStreamInfoBarDelegate( - InfoBarService* infobar_service, - MediaStreamDevicesController* controller) - : ConfirmInfoBarDelegate(infobar_service), - controller_(controller) { - DCHECK(controller_.get()); - DCHECK(controller_->has_audio() || controller_->has_video()); -} - MediaStreamInfoBarDelegate::~MediaStreamInfoBarDelegate() {} +// static +bool MediaStreamInfoBarDelegate::Create( + content::WebContents* web_contents, + const content::MediaStreamRequest& request, + const content::MediaResponseCallback& callback) { + Profile* profile = + Profile::FromBrowserContext(web_contents->GetBrowserContext()); + + scoped_ptr<MediaStreamDevicesController> controller( + new MediaStreamDevicesController(profile, request, callback)); + if (!controller->DismissInfoBarAndTakeActionOnSettings()) { + InfoBarService* infobar_service = + InfoBarService::FromWebContents(web_contents); + InfoBarDelegate* old_infobar = NULL; + for (size_t i = 0; i < infobar_service->GetInfoBarCount(); ++i) { + old_infobar = infobar_service->GetInfoBarDelegateAt(i)-> + AsMediaStreamInfoBarDelegate(); + if (old_infobar) + break; + } + + scoped_ptr<InfoBarDelegate> infobar( + new MediaStreamInfoBarDelegate(infobar_service, controller.release())); + if (old_infobar) + infobar_service->ReplaceInfoBar(old_infobar, infobar.Pass()); + else + infobar_service->AddInfoBar(infobar.Pass()); + return true; + } + return false; +} + void MediaStreamInfoBarDelegate::InfoBarDismissed() { // Deny the request if the infobar was closed with the 'x' button, since // we don't want WebRTC to be waiting for an answer that will never come. @@ -91,3 +115,12 @@ bool MediaStreamInfoBarDelegate::LinkClicked( return false; // Do not dismiss the info bar. } + +MediaStreamInfoBarDelegate::MediaStreamInfoBarDelegate( + InfoBarService* infobar_service, + MediaStreamDevicesController* controller) + : ConfirmInfoBarDelegate(infobar_service), + controller_(controller) { + DCHECK(controller_.get()); + DCHECK(controller_->has_audio() || controller_->has_video()); +} diff --git a/chrome/browser/ui/media_stream_infobar_delegate.h b/chrome/browser/ui/media_stream_infobar_delegate.h index 14f2b2c..3420e97 100644 --- a/chrome/browser/ui/media_stream_infobar_delegate.h +++ b/chrome/browser/ui/media_stream_infobar_delegate.h @@ -19,13 +19,17 @@ // to them. class MediaStreamInfoBarDelegate : public ConfirmInfoBarDelegate { public: - // MediaStreamInfoBarDelegate takes the ownership of the |controller|. - MediaStreamInfoBarDelegate( - InfoBarService* infobar_service, - MediaStreamDevicesController* controller); - virtual ~MediaStreamInfoBarDelegate(); + // Handles a permission request (in |request|) for |web_contents|. If this + // involves prompting the user, creates a media stream delegate, then checks + // for an existing infobar for |web_contents| and replaces it if found, or + // just adds the new infobar otherwise. Returns whether an infobar was + // created. + static bool Create(content::WebContents* web_contents, + const content::MediaStreamRequest& request, + const content::MediaResponseCallback& callback); + // ConfirmInfoBarDelegate: virtual void InfoBarDismissed() OVERRIDE; virtual gfx::Image* GetIcon() const OVERRIDE; @@ -39,6 +43,11 @@ class MediaStreamInfoBarDelegate : public ConfirmInfoBarDelegate { virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE; private: + // MediaStreamInfoBarDelegate takes the ownership of the |controller|. + MediaStreamInfoBarDelegate( + InfoBarService* infobar_service, + MediaStreamDevicesController* controller); + scoped_ptr<MediaStreamDevicesController> controller_; DISALLOW_COPY_AND_ASSIGN(MediaStreamInfoBarDelegate); diff --git a/chrome/browser/ui/startup/autolaunch_prompt_win.cc b/chrome/browser/ui/startup/autolaunch_prompt_win.cc index d8179ac..d64e8a4 100644 --- a/chrome/browser/ui/startup/autolaunch_prompt_win.cc +++ b/chrome/browser/ui/startup/autolaunch_prompt_win.cc @@ -36,12 +36,17 @@ const int kMaxInfobarShown = 5; // The delegate for the infobar shown when Chrome was auto-launched. class AutolaunchInfoBarDelegate : public ConfirmInfoBarDelegate { public: + // Creates an autolaunch delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + PrefService* prefs, + Profile* profile); + + private: AutolaunchInfoBarDelegate(InfoBarService* infobar_service, PrefService* prefs, Profile* profile); virtual ~AutolaunchInfoBarDelegate(); - private: void AllowExpiry() { should_expire_ = true; } // ConfirmInfoBarDelegate: @@ -71,6 +76,14 @@ class AutolaunchInfoBarDelegate : public ConfirmInfoBarDelegate { DISALLOW_COPY_AND_ASSIGN(AutolaunchInfoBarDelegate); }; +// static +void AutolaunchInfoBarDelegate::Create(InfoBarService* infobar_service, + PrefService* prefs, + Profile* profile) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new AutolaunchInfoBarDelegate(infobar_service, prefs, profile))); +} + AutolaunchInfoBarDelegate::AutolaunchInfoBarDelegate( InfoBarService* infobar_service, PrefService* prefs, @@ -175,17 +188,10 @@ bool ShowAutolaunchPrompt(Browser* browser) { } content::WebContents* web_contents = chrome::GetActiveWebContents(browser); - - // Don't show the info-bar if there are already info-bars showing. - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - if (infobar_service->GetInfoBarCount() > 0) - return false; - profile = Profile::FromBrowserContext(web_contents->GetBrowserContext()); - infobar_service->AddInfoBar(new AutolaunchInfoBarDelegate( - infobar_service, profile->GetPrefs(), profile)); - + AutolaunchInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents), profile->GetPrefs(), + profile); return true; } diff --git a/chrome/browser/ui/startup/bad_flags_prompt.cc b/chrome/browser/ui/startup/bad_flags_prompt.cc index 64b758d..230956e 100644 --- a/chrome/browser/ui/startup/bad_flags_prompt.cc +++ b/chrome/browser/ui/startup/bad_flags_prompt.cc @@ -43,15 +43,11 @@ void ShowBadFlagsPrompt(Browser* browser) { content::WebContents* web_contents = chrome::GetActiveWebContents(browser); if (!web_contents) return; - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - infobar_service->AddInfoBar( - new SimpleAlertInfoBarDelegate( - infobar_service, NULL, - l10n_util::GetStringFUTF16( - IDS_BAD_FLAGS_WARNING_MESSAGE, - UTF8ToUTF16(std::string("--") + bad_flag)), - false)); + SimpleAlertInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents), NULL, + l10n_util::GetStringFUTF16(IDS_BAD_FLAGS_WARNING_MESSAGE, + UTF8ToUTF16(std::string("--") + bad_flag)), + false); } } diff --git a/chrome/browser/ui/startup/default_browser_prompt.cc b/chrome/browser/ui/startup/default_browser_prompt.cc index 64dcfee..d2169ee 100644 --- a/chrome/browser/ui/startup/default_browser_prompt.cc +++ b/chrome/browser/ui/startup/default_browser_prompt.cc @@ -55,11 +55,15 @@ void SetChromeAsDefaultBrowser(bool interactive_flow, PrefService* prefs) { // The delegate for the infobar shown when Chrome is not the default browser. class DefaultBrowserInfoBarDelegate : public ConfirmInfoBarDelegate { public: + // Creates a default browser delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + PrefService* prefs, + bool interactive_flow_required); + + private: DefaultBrowserInfoBarDelegate(InfoBarService* infobar_service, PrefService* prefs, bool interactive_flow_required); - - private: virtual ~DefaultBrowserInfoBarDelegate(); void AllowExpiry() { should_expire_ = true; } @@ -93,6 +97,15 @@ class DefaultBrowserInfoBarDelegate : public ConfirmInfoBarDelegate { DISALLOW_COPY_AND_ASSIGN(DefaultBrowserInfoBarDelegate); }; +// static +void DefaultBrowserInfoBarDelegate::Create(InfoBarService* infobar_service, + PrefService* prefs, + bool interactive_flow_required) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new DefaultBrowserInfoBarDelegate(infobar_service, prefs, + interactive_flow_required))); +} + DefaultBrowserInfoBarDelegate::DefaultBrowserInfoBarDelegate( InfoBarService* infobar_service, PrefService* prefs, @@ -171,20 +184,13 @@ void NotifyNotDefaultBrowserCallback(chrome::HostDesktopType desktop_type) { if (!web_contents) return; - // Don't show the info-bar if there are already info-bars showing. - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - if (infobar_service->GetInfoBarCount() > 0) - return; - bool interactive_flow = ShellIntegration::CanSetAsDefaultBrowser() == ShellIntegration::SET_DEFAULT_INTERACTIVE; Profile* profile = Profile::FromBrowserContext(web_contents->GetBrowserContext()); - infobar_service->AddInfoBar( - new DefaultBrowserInfoBarDelegate(infobar_service, - profile->GetPrefs(), - interactive_flow)); + DefaultBrowserInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents), profile->GetPrefs(), + interactive_flow); } void CheckDefaultBrowserCallback(chrome::HostDesktopType desktop_type) { diff --git a/chrome/browser/ui/startup/obsolete_os_info_bar.cc b/chrome/browser/ui/startup/obsolete_os_info_bar.cc index 0414ffe..744e0b5 100644 --- a/chrome/browser/ui/startup/obsolete_os_info_bar.cc +++ b/chrome/browser/ui/startup/obsolete_os_info_bar.cc @@ -6,23 +6,43 @@ #include "chrome/browser/api/infobars/infobar_service.h" #include "content/public/browser/web_contents.h" +#include "grit/chromium_strings.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" +#if defined(TOOLKIT_GTK) +#include <gtk/gtk.h> +#endif + using content::OpenURLParams; using content::Referrer; namespace chrome { -ObsoleteOSInfoBar::ObsoleteOSInfoBar(InfoBarService* infobar_service, - const string16& message, - const GURL& url) - : ConfirmInfoBarDelegate(infobar_service), - message_(message), - learn_more_url_(url) { -} +// static +void ObsoleteOSInfoBar::Create(InfoBarService* infobar_service) { +#if defined(TOOLKIT_GTK) + // We've deprecated support for Ubuntu Hardy. Rather than attempting to + // determine whether you're using that, we instead key off the GTK version; + // this will also deprecate other distributions (including variants of Ubuntu) + // that are of a similar age. + // Version key: + // Ubuntu Hardy: GTK 2.12 + // RHEL 6: GTK 2.18 + // Ubuntu Lucid: GTK 2.20 + if (!gtk_check_version(2, 18, 0)) + return; +#else + // No other platforms currently show this infobar. + return; +#endif -ObsoleteOSInfoBar::~ObsoleteOSInfoBar() { + string16 message = l10n_util::GetStringUTF16(IDS_SYSTEM_OBSOLETE_MESSAGE); + // Link to an article in the help center on minimum system requirements. + const char* kLearnMoreURL = + "http://www.google.com/support/chrome/bin/answer.py?answer=95411"; + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new ObsoleteOSInfoBar(infobar_service, message, GURL(kLearnMoreURL)))); } string16 ObsoleteOSInfoBar::GetMessageText() const { @@ -45,4 +65,15 @@ bool ObsoleteOSInfoBar::LinkClicked(WindowOpenDisposition disposition) { return false; } +ObsoleteOSInfoBar::ObsoleteOSInfoBar(InfoBarService* infobar_service, + const string16& message, + const GURL& url) + : ConfirmInfoBarDelegate(infobar_service), + message_(message), + learn_more_url_(url) { +} + +ObsoleteOSInfoBar::~ObsoleteOSInfoBar() { +} + } // namespace chrome diff --git a/chrome/browser/ui/startup/obsolete_os_info_bar.h b/chrome/browser/ui/startup/obsolete_os_info_bar.h index 8d614c9..fd3589d 100644 --- a/chrome/browser/ui/startup/obsolete_os_info_bar.h +++ b/chrome/browser/ui/startup/obsolete_os_info_bar.h @@ -17,10 +17,8 @@ namespace chrome { // An infobar that is run with a string and a "Learn More" link. class ObsoleteOSInfoBar : public ConfirmInfoBarDelegate { public: - ObsoleteOSInfoBar(InfoBarService* infobar_service, - const string16& message, - const GURL& url); - virtual ~ObsoleteOSInfoBar(); + // Creates an obsolete OS delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service); private: virtual string16 GetMessageText() const OVERRIDE; @@ -28,6 +26,11 @@ class ObsoleteOSInfoBar : public ConfirmInfoBarDelegate { virtual string16 GetLinkText() const OVERRIDE; virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE; + ObsoleteOSInfoBar(InfoBarService* infobar_service, + const string16& message, + const GURL& url); + virtual ~ObsoleteOSInfoBar(); + const string16 message_; const GURL learn_more_url_; diff --git a/chrome/browser/ui/startup/obsolete_os_prompt.cc b/chrome/browser/ui/startup/obsolete_os_prompt.cc deleted file mode 100644 index 052ddf4..0000000 --- a/chrome/browser/ui/startup/obsolete_os_prompt.cc +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) 2012 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. - -#include "chrome/browser/ui/startup/obsolete_os_prompt.h" - -namespace chrome { - -#if !defined(TOOLKIT_GTK) -void ShowObsoleteOSPrompt(Browser* browser) { - // Only shown on Gtk. -} -#endif - -} // namespace chrome diff --git a/chrome/browser/ui/startup/obsolete_os_prompt.h b/chrome/browser/ui/startup/obsolete_os_prompt.h deleted file mode 100644 index 4db6844..0000000 --- a/chrome/browser/ui/startup/obsolete_os_prompt.h +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) 2012 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_STARTUP_OBSOLETE_OS_PROMPT_H_ -#define CHROME_BROWSER_UI_STARTUP_OBSOLETE_OS_PROMPT_H_ - -#include "build/build_config.h" - -class Browser; -class PrefService; - -namespace chrome { - -// Shows a warning notification in |browser| that the app is being run on an -// unsupported operating system. -void ShowObsoleteOSPrompt(Browser* browser); - -} // namespace chrome - -#endif // CHROME_BROWSER_UI_STARTUP_OBSOLETE_OS_PROMPT_H_ diff --git a/chrome/browser/ui/startup/obsolete_os_prompt_gtk.cc b/chrome/browser/ui/startup/obsolete_os_prompt_gtk.cc deleted file mode 100644 index 8c09769..0000000 --- a/chrome/browser/ui/startup/obsolete_os_prompt_gtk.cc +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) 2012 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. - -#include "chrome/browser/ui/startup/obsolete_os_prompt.h" - -#include <gtk/gtk.h> - -#include "chrome/browser/api/infobars/infobar_service.h" -#include "chrome/browser/ui/browser_tabstrip.h" -#include "chrome/browser/ui/startup/obsolete_os_info_bar.h" -#include "grit/chromium_strings.h" -#include "ui/base/l10n/l10n_util.h" - -namespace chrome { - -void ShowObsoleteOSPrompt(Browser* browser) { - // We've deprecated support for Ubuntu Hardy. Rather than attempting to - // determine whether you're using that, we instead key off the GTK version; - // this will also deprecate other distributions (including variants of Ubuntu) - // that are of a similar age. - // Version key: - // Ubuntu Hardy: GTK 2.12 - // RHEL 6: GTK 2.18 - // Ubuntu Lucid: GTK 2.20 - if (gtk_check_version(2, 18, 0)) { - string16 message = l10n_util::GetStringUTF16(IDS_SYSTEM_OBSOLETE_MESSAGE); - // Link to an article in the help center on minimum system requirements. - const char* kLearnMoreURL = - "http://www.google.com/support/chrome/bin/answer.py?answer=95411"; - content::WebContents* web_contents = chrome::GetActiveWebContents(browser); - if (!web_contents) - return; - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents); - infobar_service->AddInfoBar(new ObsoleteOSInfoBar(infobar_service, message, - GURL(kLearnMoreURL))); - } -} - -} // namespace chrome diff --git a/chrome/browser/ui/startup/session_crashed_prompt.cc b/chrome/browser/ui/startup/session_crashed_prompt.cc index 8550841..a918d0c 100644 --- a/chrome/browser/ui/startup/session_crashed_prompt.cc +++ b/chrome/browser/ui/startup/session_crashed_prompt.cc @@ -4,7 +4,6 @@ #include "chrome/browser/ui/startup/session_crashed_prompt.h" -#include "chrome/browser/api/infobars/confirm_infobar_delegate.h" #include "chrome/browser/api/infobars/infobar_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/sessions/session_restore.h" @@ -24,37 +23,24 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -namespace { - -// A delegate for the InfoBar shown when the previous session has crashed. -class SessionCrashedInfoBarDelegate : public ConfirmInfoBarDelegate, - public content::NotificationObserver { - public: - SessionCrashedInfoBarDelegate(InfoBarService* infobar_service, - Browser* browser); - - private: - virtual ~SessionCrashedInfoBarDelegate(); - - // ConfirmInfoBarDelegate: - virtual gfx::Image* GetIcon() const OVERRIDE; - virtual string16 GetMessageText() const OVERRIDE; - virtual int GetButtons() const OVERRIDE; - virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; - virtual bool Accept() OVERRIDE; - - // content::NotificationObserver: - virtual void Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) OVERRIDE; +// static +void SessionCrashedInfoBarDelegate::Create(Browser* browser) { + // Assume that if the user is launching incognito they were previously + // running incognito so that we have nothing to restore from. + if (browser->profile()->IsOffTheRecord()) + return; - content::NotificationRegistrar registrar_; - bool accepted_; - bool removed_notification_received_; - Browser* browser_; + // In ChromeBot tests, there might be a race. This line appears to get + // called during shutdown and |web_contents| can be NULL. + content::WebContents* tab = + browser->tab_strip_model()->GetActiveWebContents(); + if (!tab) + return; - DISALLOW_COPY_AND_ASSIGN(SessionCrashedInfoBarDelegate); -}; + InfoBarService* infobar_service = InfoBarService::FromWebContents(tab); + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new SessionCrashedInfoBarDelegate(infobar_service, browser))); +} SessionCrashedInfoBarDelegate::SessionCrashedInfoBarDelegate( InfoBarService* infobar_service, @@ -128,31 +114,3 @@ void SessionCrashedInfoBarDelegate::Observe( removed_notification_received_ = true; } } - -} // namespace - -namespace chrome { - -void ShowSessionCrashedPrompt(Browser* browser) { - // Assume that if the user is launching incognito they were previously - // running incognito so that we have nothing to restore from. - if (browser->profile()->IsOffTheRecord()) - return; - - // In ChromeBot tests, there might be a race. This line appears to get - // called during shutdown and |tab| can be NULL. - content::WebContents* tab = - browser->tab_strip_model()->GetActiveWebContents(); - if (!tab) - return; - - // Don't show the info-bar if there are already info-bars showing. - InfoBarService* infobar_service = InfoBarService::FromWebContents(tab); - if (infobar_service->GetInfoBarCount() > 0) - return; - - infobar_service->AddInfoBar( - new SessionCrashedInfoBarDelegate(infobar_service, browser)); -} - -} // namespace chrome diff --git a/chrome/browser/ui/startup/session_crashed_prompt.h b/chrome/browser/ui/startup/session_crashed_prompt.h index 8427313..af17728 100644 --- a/chrome/browser/ui/startup/session_crashed_prompt.h +++ b/chrome/browser/ui/startup/session_crashed_prompt.h @@ -5,13 +5,43 @@ #ifndef CHROME_BROWSER_UI_STARTUP_SESSION_CRASHED_PROMPT_H_ #define CHROME_BROWSER_UI_STARTUP_SESSION_CRASHED_PROMPT_H_ +#include "chrome/browser/api/infobars/confirm_infobar_delegate.h" +#include "content/public/browser/notification_observer.h" +#include "content/public/browser/notification_registrar.h" + class Browser; -namespace chrome { +// A delegate for the InfoBar shown when the previous session has crashed. +class SessionCrashedInfoBarDelegate : public ConfirmInfoBarDelegate, + public content::NotificationObserver { + public: + // If |browser| is not incognito, creates a session crashed delegate and adds + // it to the InfoBarService for |browser|. + static void Create(Browser* browser); + + private: + SessionCrashedInfoBarDelegate(InfoBarService* infobar_service, + Browser* browser); + virtual ~SessionCrashedInfoBarDelegate(); + + // ConfirmInfoBarDelegate: + virtual gfx::Image* GetIcon() const OVERRIDE; + virtual string16 GetMessageText() const OVERRIDE; + virtual int GetButtons() const OVERRIDE; + virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; + virtual bool Accept() OVERRIDE; + + // content::NotificationObserver: + virtual void Observe(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) OVERRIDE; -// Shows the session crashed prompt for |browser| if required. -void ShowSessionCrashedPrompt(Browser* browser); + content::NotificationRegistrar registrar_; + bool accepted_; + bool removed_notification_received_; + Browser* browser_; -} // namespace chrome + DISALLOW_COPY_AND_ASSIGN(SessionCrashedInfoBarDelegate); +}; #endif // CHROME_BROWSER_UI_STARTUP_SESSION_CRASHED_PROMPT_H_ diff --git a/chrome/browser/ui/startup/startup_browser_creator_impl.cc b/chrome/browser/ui/startup/startup_browser_creator_impl.cc index b4a1119..523df9b 100644 --- a/chrome/browser/ui/startup/startup_browser_creator_impl.cc +++ b/chrome/browser/ui/startup/startup_browser_creator_impl.cc @@ -22,6 +22,7 @@ #include "base/string_split.h" #include "base/threading/thread_restrictions.h" #include "base/utf_string_conversions.h" +#include "chrome/browser/api/infobars/infobar_service.h" #include "chrome/browser/auto_launch_trial.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/custom_handlers/protocol_handler_registry.h" @@ -59,7 +60,7 @@ #include "chrome/browser/ui/startup/autolaunch_prompt.h" #include "chrome/browser/ui/startup/bad_flags_prompt.h" #include "chrome/browser/ui/startup/default_browser_prompt.h" -#include "chrome/browser/ui/startup/obsolete_os_prompt.h" +#include "chrome/browser/ui/startup/obsolete_os_info_bar.h" #include "chrome/browser/ui/startup/session_crashed_prompt.h" #include "chrome/browser/ui/startup/startup_browser_creator.h" #include "chrome/browser/ui/tabs/pinned_tab_codec.h" @@ -856,7 +857,7 @@ void StartupBrowserCreatorImpl::AddInfoBarsIfNecessary( return; if (HasPendingUncleanExit(browser->profile())) - chrome::ShowSessionCrashedPrompt(browser); + SessionCrashedInfoBarDelegate::Create(browser); // The bad flags info bar and the obsolete system info bar are only added to // the first profile which is launched. Other profiles might be restoring the @@ -864,7 +865,8 @@ void StartupBrowserCreatorImpl::AddInfoBarsIfNecessary( // focused tabs here. if (is_process_startup == chrome::startup::IS_PROCESS_STARTUP) { chrome::ShowBadFlagsPrompt(browser); - chrome::ShowObsoleteOSPrompt(browser); + chrome::ObsoleteOSInfoBar::Create( + InfoBarService::FromWebContents(chrome::GetActiveWebContents(browser))); if (browser_defaults::kOSSupportsOtherBrowsers && !command_line_.HasSwitch(switches::kNoDefaultBrowserCheck)) { diff --git a/chrome/browser/ui/sync/one_click_signin_helper.cc b/chrome/browser/ui/sync/one_click_signin_helper.cc index ca59fc2..0c4990f 100644 --- a/chrome/browser/ui/sync/one_click_signin_helper.cc +++ b/chrome/browser/ui/sync/one_click_signin_helper.cc @@ -232,13 +232,19 @@ const void* const OneClickSigninRequestUserData::kUserDataKey = // of this infobar. class OneClickInfoBarDelegateImpl : public OneClickSigninInfoBarDelegate { public: + // Creates a one click signin delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service, + const std::string& session_index, + const std::string& email, + const std::string& password); + + private: OneClickInfoBarDelegateImpl(InfoBarService* owner, - const std::string& session_index, - const std::string& email, - const std::string& password); + const std::string& session_index, + const std::string& email, + const std::string& password); virtual ~OneClickInfoBarDelegateImpl(); - private: // InfoBarDelegate overrides. virtual InfoBarAutomationType GetInfoBarAutomationType() const OVERRIDE; virtual void InfoBarDismissed() OVERRIDE; @@ -270,6 +276,16 @@ class OneClickInfoBarDelegateImpl : public OneClickSigninInfoBarDelegate { DISALLOW_COPY_AND_ASSIGN(OneClickInfoBarDelegateImpl); }; +// static +void OneClickInfoBarDelegateImpl::Create(InfoBarService* infobar_service, + const std::string& session_index, + const std::string& email, + const std::string& password) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new OneClickInfoBarDelegateImpl(infobar_service, session_index, email, + password))); +} + OneClickInfoBarDelegateImpl::OneClickInfoBarDelegateImpl( InfoBarService* owner, const std::string& session_index, @@ -867,8 +883,6 @@ void OneClickSigninHelper::DidStopLoading( Browser* browser = chrome::FindBrowserWithWebContents(contents); Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); - InfoBarService* infobar_tab_helper = - InfoBarService::FromWebContents(contents); VLOG(1) << "OneClickSigninHelper::DidStopLoading: signin is go." << " auto_accept=" << auto_accept_ @@ -881,9 +895,9 @@ void OneClickSigninHelper::DidStopLoading( one_click_signin::HISTOGRAM_DISMISSED, one_click_signin::HISTOGRAM_MAX); } else { - infobar_tab_helper->AddInfoBar( - new OneClickInfoBarDelegateImpl(infobar_tab_helper, session_index_, - email_, password_)); + OneClickInfoBarDelegateImpl::Create( + InfoBarService::FromWebContents(contents), session_index_, email_, + password_); } break; case AUTO_ACCEPT_ACCEPTED: diff --git a/chrome/browser/ui/sync/one_click_signin_infobar_delegate.cc b/chrome/browser/ui/sync/one_click_signin_infobar_delegate.cc index 8ce5b60..c024607 100644 --- a/chrome/browser/ui/sync/one_click_signin_infobar_delegate.cc +++ b/chrome/browser/ui/sync/one_click_signin_infobar_delegate.cc @@ -4,11 +4,6 @@ #include "chrome/browser/ui/sync/one_click_signin_infobar_delegate.h" -OneClickSigninInfoBarDelegate::OneClickSigninInfoBarDelegate( - InfoBarService* infobar_service) - : ConfirmInfoBarDelegate(infobar_service) { -} - OneClickSigninInfoBarDelegate::~OneClickSigninInfoBarDelegate() { } @@ -16,3 +11,8 @@ void OneClickSigninInfoBarDelegate::GetAlternateColors( AlternateColors* alt_colors) { alt_colors->enabled = false; } + +OneClickSigninInfoBarDelegate::OneClickSigninInfoBarDelegate( + InfoBarService* infobar_service) + : ConfirmInfoBarDelegate(infobar_service) { +} diff --git a/chrome/browser/ui/sync/one_click_signin_infobar_delegate.h b/chrome/browser/ui/sync/one_click_signin_infobar_delegate.h index 0996032..c4ec57a5 100644 --- a/chrome/browser/ui/sync/one_click_signin_infobar_delegate.h +++ b/chrome/browser/ui/sync/one_click_signin_infobar_delegate.h @@ -23,13 +23,15 @@ class OneClickSigninInfoBarDelegate : public ConfirmInfoBarDelegate { SkColor button_border_color; }; - explicit OneClickSigninInfoBarDelegate(InfoBarService* infobar_service); virtual ~OneClickSigninInfoBarDelegate(); // Returns the colours to use with the one click signin infobar. The colours // depend on the experimental group. virtual void GetAlternateColors(AlternateColors* alt_colors); + protected: + explicit OneClickSigninInfoBarDelegate(InfoBarService* infobar_service); + private: #if defined(TOOLKIT_VIEWS) // Because the experiment is only running for views, only override this diff --git a/chrome/browser/ui/views/collected_cookies_views.cc b/chrome/browser/ui/views/collected_cookies_views.cc index 4de3fee..38a0896 100644 --- a/chrome/browser/ui/views/collected_cookies_views.cc +++ b/chrome/browser/ui/views/collected_cookies_views.cc @@ -223,10 +223,8 @@ void CollectedCookiesViews::DeleteDelegate() { bool CollectedCookiesViews::Cancel() { if (status_changed_) { - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents_); - infobar_service->AddInfoBar( - new CollectedCookiesInfoBarDelegate(infobar_service)); + CollectedCookiesInfoBarDelegate::Create( + InfoBarService::FromWebContents(web_contents_)); } return true; diff --git a/chrome/browser/ui/views/external_tab_container_win.cc b/chrome/browser/ui/views/external_tab_container_win.cc index 9e7e084..8a06340 100644 --- a/chrome/browser/ui/views/external_tab_container_win.cc +++ b/chrome/browser/ui/views/external_tab_container_win.cc @@ -25,6 +25,7 @@ #include "chrome/browser/file_select_helper.h" #include "chrome/browser/history/history_tab_helper.h" #include "chrome/browser/history/history_types.h" +#include "chrome/browser/intents/register_intent_handler_infobar_delegate.h" #include "chrome/browser/pepper_broker_infobar_delegate.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/repost_form_warning_controller.h" @@ -34,6 +35,7 @@ #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_tab_contents.h" #include "chrome/browser/ui/browser_window.h" +#include "chrome/browser/ui/media_stream_infobar_delegate.h" #include "chrome/browser/ui/tab_modal_confirm_dialog.h" #include "chrome/browser/ui/views/infobars/infobar_container_view.h" #include "chrome/browser/ui/views/tab_contents/render_view_context_menu_win.h" @@ -800,7 +802,7 @@ void ExternalTabContainerWin::RegisterIntentHandler( WebContents* tab, const webkit_glue::WebIntentServiceData& data, bool user_gesture) { - Browser::RegisterIntentHandlerHelper(tab, data, user_gesture); + RegisterIntentHandlerInfoBarDelegate::Create(tab, data); } void ExternalTabContainerWin::WebIntentDispatch( @@ -825,7 +827,7 @@ void ExternalTabContainerWin::RequestMediaAccessPermission( content::WebContents* web_contents, const content::MediaStreamRequest& request, const content::MediaResponseCallback& callback) { - Browser::RequestMediaAccessPermissionHelper(web_contents, request, callback); + MediaStreamInfoBarDelegate::Create(web_contents, request, callback); } bool ExternalTabContainerWin::RequestPpapiBrokerPermission( @@ -833,7 +835,7 @@ bool ExternalTabContainerWin::RequestPpapiBrokerPermission( const GURL& url, const FilePath& plugin_path, const base::Callback<void(bool)>& callback) { - PepperBrokerInfoBarDelegate::Show(web_contents, url, plugin_path, callback); + PepperBrokerInfoBarDelegate::Create(web_contents, url, plugin_path, callback); return true; } diff --git a/chrome/browser/ui/website_settings/website_settings.cc b/chrome/browser/ui/website_settings/website_settings.cc index 9da367b..1f64254 100644 --- a/chrome/browser/ui/website_settings/website_settings.cc +++ b/chrome/browser/ui/website_settings/website_settings.cc @@ -220,10 +220,8 @@ void WebsiteSettings::OnSiteDataAccessed() { } void WebsiteSettings::OnUIClosing() { - if (show_info_bar_) { - infobar_service_->AddInfoBar( - new WebsiteSettingsInfobarDelegate(infobar_service_)); - } + if (show_info_bar_) + WebsiteSettingsInfobarDelegate::Create(infobar_service_); } void WebsiteSettings::Init(Profile* profile, diff --git a/chrome/browser/ui/website_settings/website_settings_infobar_delegate.cc b/chrome/browser/ui/website_settings/website_settings_infobar_delegate.cc index a23be8a..83675c8 100644 --- a/chrome/browser/ui/website_settings/website_settings_infobar_delegate.cc +++ b/chrome/browser/ui/website_settings/website_settings_infobar_delegate.cc @@ -13,6 +13,12 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" +// static +void WebsiteSettingsInfobarDelegate::Create(InfoBarService* infobar_service) { + infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( + new WebsiteSettingsInfobarDelegate(infobar_service))); +} + WebsiteSettingsInfobarDelegate::WebsiteSettingsInfobarDelegate( InfoBarService* infobar_service) : ConfirmInfoBarDelegate(infobar_service) { diff --git a/chrome/browser/ui/website_settings/website_settings_infobar_delegate.h b/chrome/browser/ui/website_settings/website_settings_infobar_delegate.h index c6e90e8..21cabd4 100644 --- a/chrome/browser/ui/website_settings/website_settings_infobar_delegate.h +++ b/chrome/browser/ui/website_settings/website_settings_infobar_delegate.h @@ -16,9 +16,12 @@ class InfoBarService; // the reload right from the infobar. class WebsiteSettingsInfobarDelegate : public ConfirmInfoBarDelegate { public: - explicit WebsiteSettingsInfobarDelegate(InfoBarService* infobar_service); + // Creates a website settings delegate and adds it to |infobar_service|. + static void Create(InfoBarService* infobar_service); private: + explicit WebsiteSettingsInfobarDelegate(InfoBarService* infobar_service); + // Overwridden from ConfirmInfoBarDelegate: virtual gfx::Image* GetIcon() const OVERRIDE; virtual Type GetInfoBarType() const OVERRIDE; diff --git a/chrome/browser/ui/website_settings/website_settings_unittest.cc b/chrome/browser/ui/website_settings/website_settings_unittest.cc index b8f7608c..767d041 100644 --- a/chrome/browser/ui/website_settings/website_settings_unittest.cc +++ b/chrome/browser/ui/website_settings/website_settings_unittest.cc @@ -402,8 +402,4 @@ TEST_F(WebsiteSettingsTest, ShowInfoBar) { scoped_ptr<InfoBarDelegate> delegate( infobar_service()->GetInfoBarDelegateAt(0)); infobar_service()->RemoveInfoBar(delegate.get()); - // Right now InfoBarDelegates delete themselves via - // InfoBarClosed(); once InfoBars own their delegates, this can become a - // simple reset() call - delegate.release()->InfoBarClosed(); } |