diff options
author | bauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-17 02:21:02 +0000 |
---|---|---|
committer | bauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-17 02:21:02 +0000 |
commit | d830d93a73c919f83624a4d18fdc9a0d44eab40f (patch) | |
tree | e7b2ec991ab84e63fa2127868c33ddfe5698f4c4 | |
parent | 8c77c434f3013c48411d69a98caa832e2da93000 (diff) | |
download | chromium_src-d830d93a73c919f83624a4d18fdc9a0d44eab40f.zip chromium_src-d830d93a73c919f83624a4d18fdc9a0d44eab40f.tar.gz chromium_src-d830d93a73c919f83624a4d18fdc9a0d44eab40f.tar.bz2 |
Add TabModalConfirmDialogDelegate to show simple tab-modal confirmation dialogs.
This CL carves a TabModalConfirmDialogDelegate class (modeled after ConfirmInfobarDelegate) out of RepostFormWarningController and makes it a subclass thereof.
It also removes the ShowRepostFormWarning method from BrowserWindow and its subclasses, in favor of a method in browser_dialogs.h.
BUG=92795
TEST=none
Review URL: http://codereview.chromium.org/8658005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114898 0039d316-1c4b-4281-b951-d872f2087c98
40 files changed, 810 insertions, 661 deletions
diff --git a/chrome/browser/browser_resources.grd b/chrome/browser/browser_resources.grd index 575ff3c..4ade6d7 100644 --- a/chrome/browser/browser_resources.grd +++ b/chrome/browser/browser_resources.grd @@ -168,7 +168,7 @@ <if expr="(is_linux and pp_ifdef('toolkit_views')) or pp_ifdef('use_aura')"> <include name="IDR_COLLECTED_COOKIES_HTML" file="resources\collected_cookies.html" flattenhtml="true" type="BINDATA" /> <include name="IDR_HTTP_AUTH_HTML" file="resources\http_auth.html" flattenhtml="true" type="BINDATA" /> - <include name="IDR_REPOST_FORM_WARNING_HTML" file="resources\repost_form_warning.html" flattenhtml="true" type="BINDATA" /> + <include name="IDR_TAB_MODAL_CONFIRM_DIALOG_HTML" file="resources\tab_modal_confirm_dialog.html" flattenhtml="true" type="BINDATA" /> </if> <if expr="pp_ifdef('use_aura')"> <include name="IDR_APP_LIST_HTML" file="resources\aura\app_list\app_list.html" flattenhtml="true" type="BINDATA" /> diff --git a/chrome/browser/external_tab_container_win.cc b/chrome/browser/external_tab_container_win.cc index 459936d..056076d 100644 --- a/chrome/browser/external_tab_container_win.cc +++ b/chrome/browser/external_tab_container_win.cc @@ -23,9 +23,11 @@ #include "chrome/browser/history/history_types.h" #include "chrome/browser/infobars/infobar_tab_helper.h" #include "chrome/browser/profiles/profile.h" +#include "chrome/browser/repost_form_warning_controller.h" #include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h" #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" #include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_dialogs.h" #include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" #include "chrome/browser/ui/views/browser_dialogs.h" @@ -696,7 +698,9 @@ void ExternalTabContainer::BeforeUnloadFired(TabContents* tab, void ExternalTabContainer::ShowRepostFormWarningDialog( TabContents* tab_contents) { - browser::ShowRepostFormWarningDialog(GetNativeView(), tab_contents); + browser::ShowTabModalConfirmDialog( + new RepostFormWarningController(tab_contents), + GetNativeView(), tab_contents); } void ExternalTabContainer::RunFileChooser( diff --git a/chrome/browser/repost_form_warning_controller.cc b/chrome/browser/repost_form_warning_controller.cc index 987b7d6..a53e95e 100644 --- a/chrome/browser/repost_form_warning_controller.cc +++ b/chrome/browser/repost_form_warning_controller.cc @@ -4,66 +4,68 @@ #include "chrome/browser/repost_form_warning_controller.h" -#include "chrome/browser/ui/constrained_window.h" +#if defined(TOOLKIT_USES_GTK) +#include <gtk/gtk.h> +#endif + +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "content/browser/tab_contents/navigation_controller.h" #include "content/browser/tab_contents/tab_contents.h" #include "content/public/browser/notification_source.h" #include "content/public/browser/notification_types.h" +#include "grit/generated_resources.h" +#include "ui/base/l10n/l10n_util.h" RepostFormWarningController::RepostFormWarningController( TabContents* tab_contents) - : tab_contents_(tab_contents), - window_(NULL) { - NavigationController* controller = &tab_contents->controller(); - registrar_.Add(this, content::NOTIFICATION_LOAD_START, - content::Source<NavigationController>(controller)); - registrar_.Add(this, content::NOTIFICATION_TAB_CLOSING, - content::Source<NavigationController>(controller)); + : TabModalConfirmDialogDelegate(tab_contents), + navigation_controller_(&tab_contents->controller()) { registrar_.Add(this, content::NOTIFICATION_REPOST_WARNING_SHOWN, - content::Source<NavigationController>(controller)); + content::Source<NavigationController>(navigation_controller_)); } RepostFormWarningController::~RepostFormWarningController() { - // If we end up here, the constrained window has been closed, so make sure we - // don't close it again. - window_ = NULL; - // Make sure everything is cleaned up. - Cancel(); } -void RepostFormWarningController::Cancel() { - if (tab_contents_) { - tab_contents_->controller().CancelPendingReload(); - CloseDialog(); - } +string16 RepostFormWarningController::GetTitle() { + return l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING_TITLE); } -void RepostFormWarningController::Continue() { - if (tab_contents_) { - tab_contents_->controller().ContinuePendingReload(); - // If we reload the page, the dialog will be closed anyway. - } +string16 RepostFormWarningController::GetMessage() { + return l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING); } -void RepostFormWarningController::Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) { - // Close the dialog if we load a page (because reloading might not apply to - // the same page anymore) or if the tab is closed, because then we won't have - // a navigation controller anymore. - if (tab_contents_ && - (type == content::NOTIFICATION_LOAD_START || - type == content::NOTIFICATION_TAB_CLOSING || - type == content::NOTIFICATION_REPOST_WARNING_SHOWN)) { - DCHECK_EQ(content::Source<NavigationController>(source).ptr(), - &tab_contents_->controller()); - Cancel(); - } +string16 RepostFormWarningController::GetAcceptButtonTitle() { + return l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING_RESEND); +} + +#if defined(TOOLKIT_USES_GTK) +const char* RepostFormWarningController::GetAcceptButtonIcon() { + return GTK_STOCK_REFRESH; +} + +const char* RepostFormWarningController::GetCancelButtonIcon() { + return GTK_STOCK_CANCEL; } +#endif // defined(TOOLKIT_USES_GTK) -void RepostFormWarningController::CloseDialog() { - // Make sure we won't do anything when |Cancel()| is called again. - tab_contents_ = NULL; - if (window_) { - window_->CloseConstrainedWindow(); - } +void RepostFormWarningController::OnAccepted() { + navigation_controller_->ContinuePendingReload(); +} + +void RepostFormWarningController::OnCanceled() { + navigation_controller_->CancelPendingReload(); +} + +void RepostFormWarningController::Observe( + int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) { + // Close the dialog if we show an additional dialog, to avoid them + // stacking up. + if (type == content::NOTIFICATION_REPOST_WARNING_SHOWN) + Cancel(); + else + TabModalConfirmDialogDelegate::Observe(type, source, details); } diff --git a/chrome/browser/repost_form_warning_controller.h b/chrome/browser/repost_form_warning_controller.h index 7b8114a..808c9d3 100644 --- a/chrome/browser/repost_form_warning_controller.h +++ b/chrome/browser/repost_form_warning_controller.h @@ -7,44 +7,37 @@ #pragma once #include "base/compiler_specific.h" -#include "content/public/browser/notification_observer.h" -#include "content/public/browser/notification_registrar.h" +#include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" -class ConstrainedWindow; +class NavigationController; class TabContents; // This class is used to continue or cancel a pending reload when the // repost form warning is shown. It is owned by the platform-dependent // |RepostFormWarning{Gtk,Mac,View}| classes. -class RepostFormWarningController : public content::NotificationObserver { +class RepostFormWarningController : public TabModalConfirmDialogDelegate { public: explicit RepostFormWarningController(TabContents* tab_contents); virtual ~RepostFormWarningController(); - // Cancel the reload. - void Cancel(); - - // Continue the reload. - void Continue(); - - void set_window(ConstrainedWindow* window) { window_ = window; } + // TabModalConfirmDialogDelegate methods: + virtual string16 GetTitle() OVERRIDE; + virtual string16 GetMessage() OVERRIDE; + virtual string16 GetAcceptButtonTitle() OVERRIDE; +#if defined(TOOLKIT_USES_GTK) + virtual const char* GetAcceptButtonIcon() OVERRIDE; + virtual const char* GetCancelButtonIcon() OVERRIDE; +#endif // defined(TOOLKIT_USES_GTK) + virtual void OnAccepted() OVERRIDE; + virtual void OnCanceled() OVERRIDE; private: - // content::NotificationObserver implementation. - // Watch for a new load or a closed tab and dismiss the dialog if they occur. virtual void Observe(int type, const content::NotificationSource& source, const content::NotificationDetails& details) OVERRIDE; - // Close the warning dialog. - void CloseDialog(); - - content::NotificationRegistrar registrar_; - - // Tab contents, used to continue the reload. - TabContents* tab_contents_; - - ConstrainedWindow* window_; + // Weak pointer; this dialog is cancelled when the TabContents is closed. + NavigationController* navigation_controller_; DISALLOW_COPY_AND_ASSIGN(RepostFormWarningController); }; diff --git a/chrome/browser/resources/repost_form_warning.html b/chrome/browser/resources/tab_modal_confirm_dialog.html index a2d8834..8637543 100644 --- a/chrome/browser/resources/repost_form_warning.html +++ b/chrome/browser/resources/tab_modal_confirm_dialog.html @@ -22,6 +22,7 @@ } </style> + <script src="chrome://resources/js/i18n_template.js"></script> <script> function $(o) { return document.getElementById(o); @@ -29,7 +30,7 @@ function disableControls() { $('cancel').disabled = true; - $('resend').disabled = true; + $('accept').disabled = true; } function cancel() { @@ -51,6 +52,7 @@ } function load() { + i18nTemplate.process(document, JSON.parse(chrome.dialogArguments)); document.addEventListener('keydown', handleKeyDown); $('form').onsubmit = handleSubmit; $('cancel').onclick = cancel; @@ -61,11 +63,11 @@ </script> </head> <body i18n-values=".style.fontFamily:fontfamily;.style.fontSize:fontsize"> - <div id="explanation" i18n-content="explanation"></div> + <div id="explanation" i18n-content="message"></div> <form id="form"> <div id="buttons"> <input id="cancel" type="reset" i18n-values="value:cancel" autofocus> - <input id="resend" type="submit" i18n-values="value:resend"> + <input id="accept" type="submit" i18n-values="value:accept"> </div> </form> </body> diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc index d862822..ab62271 100644 --- a/chrome/browser/ui/browser.cc +++ b/chrome/browser/ui/browser.cc @@ -77,6 +77,7 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_metrics.h" +#include "chrome/browser/repost_form_warning_controller.h" #include "chrome/browser/sessions/restore_tab_helper.h" #include "chrome/browser/sessions/session_service.h" #include "chrome/browser/sessions/session_service_factory.h" @@ -3795,7 +3796,9 @@ void Browser::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) { } void Browser::ShowRepostFormWarningDialog(TabContents *tab_contents) { - window()->ShowRepostFormWarningDialog(tab_contents); + browser::ShowTabModalConfirmDialog( + new RepostFormWarningController(tab_contents), + window()->GetNativeHandle(), tab_contents); } void Browser::ShowContentSettingsPage(ContentSettingsType content_type) { diff --git a/chrome/browser/ui/browser_dialogs.h b/chrome/browser/ui/browser_dialogs.h index ca42868..3a715be 100644 --- a/chrome/browser/ui/browser_dialogs.h +++ b/chrome/browser/ui/browser_dialogs.h @@ -16,6 +16,7 @@ class HtmlDialogUIDelegate; class Profile; class SkBitmap; class TabContents; +class TabModalConfirmDialogDelegate; class TemplateURL; namespace browser { @@ -62,6 +63,11 @@ void HideNativeHungRendererDialog(TabContents* contents); void ConfirmAddSearchProvider(const TemplateURL* template_url, Profile* profile); +// Shows a tab-modal dialog box. +void ShowTabModalConfirmDialog(TabModalConfirmDialogDelegate* delegate, + gfx::NativeWindow parent_window, + TabContents* tab_contents); + } // namespace browser #endif // CHROME_BROWSER_UI_BROWSER_DIALOGS_H_ diff --git a/chrome/browser/ui/browser_window.h b/chrome/browser/ui/browser_window.h index 41b321d..598b9ff 100644 --- a/chrome/browser/ui/browser_window.h +++ b/chrome/browser/ui/browser_window.h @@ -239,9 +239,6 @@ class BrowserWindow { // Returns the DownloadShelf. virtual DownloadShelf* GetDownloadShelf() = 0; - // Shows the repost form confirmation dialog box. - virtual void ShowRepostFormWarningDialog(TabContents* tab_contents) = 0; - // Shows the collected cookies dialog box. virtual void ShowCollectedCookiesDialog(TabContentsWrapper* tab_contents) = 0; diff --git a/chrome/browser/ui/cocoa/browser_window_cocoa.h b/chrome/browser/ui/cocoa/browser_window_cocoa.h index 425e55e..fdf714f 100644 --- a/chrome/browser/ui/cocoa/browser_window_cocoa.h +++ b/chrome/browser/ui/cocoa/browser_window_cocoa.h @@ -91,7 +91,6 @@ class BrowserWindowCocoa : public BrowserWindow, bool already_bookmarked) OVERRIDE; virtual bool IsDownloadShelfVisible() const OVERRIDE; virtual DownloadShelf* GetDownloadShelf() OVERRIDE; - virtual void ShowRepostFormWarningDialog(TabContents* tab_contents) OVERRIDE; virtual void ShowCollectedCookiesDialog(TabContentsWrapper* wrapper) OVERRIDE; virtual void ConfirmBrowserCloseWithPendingDownloads() OVERRIDE; virtual void UserChangedTheme() OVERRIDE; diff --git a/chrome/browser/ui/cocoa/browser_window_cocoa.mm b/chrome/browser/ui/cocoa/browser_window_cocoa.mm index 46b289d..9db4de1 100644 --- a/chrome/browser/ui/cocoa/browser_window_cocoa.mm +++ b/chrome/browser/ui/cocoa/browser_window_cocoa.mm @@ -31,7 +31,6 @@ #import "chrome/browser/ui/cocoa/info_bubble_view.h" #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h" -#include "chrome/browser/ui/cocoa/repost_form_warning_mac.h" #include "chrome/browser/ui/cocoa/restart_browser.h" #include "chrome/browser/ui/cocoa/status_bubble_mac.h" #include "chrome/browser/ui/cocoa/task_manager_mac.h" @@ -433,11 +432,6 @@ DownloadShelf* BrowserWindowCocoa::GetDownloadShelf() { return [shelfController bridge]; } -void BrowserWindowCocoa::ShowRepostFormWarningDialog( - TabContents* tab_contents) { - RepostFormWarningMac::Create(GetNativeHandle(), tab_contents); -} - void BrowserWindowCocoa::ShowCollectedCookiesDialog( TabContentsWrapper* wrapper) { // Deletes itself on close. diff --git a/chrome/browser/ui/cocoa/repost_form_warning_mac.h b/chrome/browser/ui/cocoa/repost_form_warning_mac.h deleted file mode 100644 index 700cfd9..0000000 --- a/chrome/browser/ui/cocoa/repost_form_warning_mac.h +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_UI_COCOA_REPOST_FORM_WARNING_MAC_H_ -#define CHROME_BROWSER_UI_COCOA_REPOST_FORM_WARNING_MAC_H_ -#pragma once - -#import <Cocoa/Cocoa.h> - -#include "base/memory/scoped_ptr.h" -#include "chrome/browser/ui/cocoa/constrained_window_mac.h" - -class RepostFormWarningController; -class TabContents; - -// Displays a dialog that warns the user that they are about to resubmit -// a form. To show the dialog, call the |Create| method. It will open the -// dialog and then delete itself when the user dismisses the dialog. -class RepostFormWarningMac : public ConstrainedWindowMacDelegateSystemSheet { - public: - // Convenience method that creates a new |RepostFormWarningController| and - // then a new |RepostFormWarningMac| from that. - static RepostFormWarningMac* Create(NSWindow* parent, - TabContents* tab_contents); - - RepostFormWarningMac(NSWindow* parent, - RepostFormWarningController* controller, - TabContents* tab_contents); - - // ConstrainedWindowDelegateMacSystemSheet methods: - virtual void DeleteDelegate() OVERRIDE; - - private: - virtual ~RepostFormWarningMac(); - - scoped_ptr<RepostFormWarningController> controller_; - TabContents* tab_contents_; - - DISALLOW_COPY_AND_ASSIGN(RepostFormWarningMac); -}; - -#endif // CHROME_BROWSER_UI_COCOA_REPOST_FORM_WARNING_MAC_H_ diff --git a/chrome/browser/ui/cocoa/repost_form_warning_mac.mm b/chrome/browser/ui/cocoa/repost_form_warning_mac.mm deleted file mode 100644 index 4300ac1..0000000 --- a/chrome/browser/ui/cocoa/repost_form_warning_mac.mm +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/ui/cocoa/repost_form_warning_mac.h" - -#include "base/memory/scoped_nsobject.h" -#include "chrome/browser/repost_form_warning_controller.h" -#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" -#include "grit/generated_resources.h" -#include "ui/base/l10n/l10n_util_mac.h" - -// The delegate of the NSAlert used to display the dialog. Forwards the alert's -// completion event to the C++ class |RepostFormWarningController|. -@interface RepostDelegate : NSObject { - RepostFormWarningController* warning_; // weak -} -- (id)initWithWarning:(RepostFormWarningController*)warning; -- (void)alertDidEnd:(NSAlert*)alert - returnCode:(int)returnCode - contextInfo:(void*)contextInfo; -@end - -@implementation RepostDelegate -- (id)initWithWarning:(RepostFormWarningController*)warning { - if ((self = [super init])) { - warning_ = warning; - } - return self; -} - -- (void)alertDidEnd:(NSAlert*)alert - returnCode:(int)returnCode - contextInfo:(void*)contextInfo { - if (returnCode == NSAlertFirstButtonReturn) { - warning_->Continue(); - } else { - warning_->Cancel(); - } -} -@end - -RepostFormWarningMac* RepostFormWarningMac::Create(NSWindow* parent, - TabContents* tab_contents) { - return new RepostFormWarningMac( - parent, - new RepostFormWarningController(tab_contents), - tab_contents); -} - -RepostFormWarningMac::RepostFormWarningMac( - NSWindow* parent, - RepostFormWarningController* controller, - TabContents* tab_contents) - : ConstrainedWindowMacDelegateSystemSheet( - [[[RepostDelegate alloc] initWithWarning:controller] - autorelease], - @selector(alertDidEnd:returnCode:contextInfo:)), - controller_(controller), - tab_contents_(tab_contents) { - scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); - [alert setMessageText: - l10n_util::GetNSStringWithFixup(IDS_HTTP_POST_WARNING_TITLE)]; - [alert setInformativeText: - l10n_util::GetNSStringWithFixup(IDS_HTTP_POST_WARNING)]; - [alert addButtonWithTitle: - l10n_util::GetNSStringWithFixup(IDS_HTTP_POST_WARNING_RESEND)]; - [alert addButtonWithTitle: - l10n_util::GetNSStringWithFixup(IDS_CANCEL)]; - - set_sheet(alert); - - TabContentsWrapper* wrapper = - TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); - controller->set_window(new ConstrainedWindowMac(wrapper, this)); -} - -RepostFormWarningMac::~RepostFormWarningMac() { - NSWindow* window = [(NSAlert*)sheet() window]; - if (window && is_sheet_open()) { - [NSApp endSheet:window - returnCode:NSAlertSecondButtonReturn]; - } -} - -void RepostFormWarningMac::DeleteDelegate() { - delete this; -} diff --git a/chrome/browser/ui/cocoa/tab_modal_confirm_dialog_mac.h b/chrome/browser/ui/cocoa/tab_modal_confirm_dialog_mac.h new file mode 100644 index 0000000..26398e4 --- /dev/null +++ b/chrome/browser/ui/cocoa/tab_modal_confirm_dialog_mac.h @@ -0,0 +1,40 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_COCOA_TAB_MODAL_CONFIRM_DIALOG_MAC_H_ +#define CHROME_BROWSER_UI_COCOA_TAB_MODAL_CONFIRM_DIALOG_MAC_H_ +#pragma once + +#import <Cocoa/Cocoa.h> + +#include "base/memory/scoped_ptr.h" +#include "chrome/browser/ui/cocoa/constrained_window_mac.h" + +class TabModalConfirmDialogDelegate; +class TabContents; + +// Displays a tab-modal dialog, i.e. a dialog that will block the current page +// but still allow the user to switch to a different page. +// To display the dialog, allocate this object on the heap. It will open the +// dialog from its constructor and then delete itself when the user dismisses +// the dialog. +class TabModalConfirmDialogMac + : public ConstrainedWindowMacDelegateSystemSheet { + public: + TabModalConfirmDialogMac(NSWindow* parent, + TabModalConfirmDialogDelegate* delegate, + TabContents* tab_contents); + + // ConstrainedWindowDelegateMacSystemSheet methods: + virtual void DeleteDelegate() OVERRIDE; + + private: + virtual ~TabModalConfirmDialogMac(); + + scoped_ptr<TabModalConfirmDialogDelegate> delegate_; + + DISALLOW_COPY_AND_ASSIGN(TabModalConfirmDialogMac); +}; + +#endif // CHROME_BROWSER_UI_COCOA_TAB_MODAL_CONFIRM_DIALOG_MAC_H_ diff --git a/chrome/browser/ui/cocoa/tab_modal_confirm_dialog_mac.mm b/chrome/browser/ui/cocoa/tab_modal_confirm_dialog_mac.mm new file mode 100644 index 0000000..564b5dc --- /dev/null +++ b/chrome/browser/ui/cocoa/tab_modal_confirm_dialog_mac.mm @@ -0,0 +1,96 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/cocoa/tab_modal_confirm_dialog_mac.h" + +#include "base/memory/scoped_nsobject.h" +#include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" +#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" +#include "ui/base/l10n/l10n_util_mac.h" +#include "ui/gfx/image/image.h" + +// The delegate of the NSAlert used to display the dialog. Forwards the alert's +// completion event to the C++ class |TabModalConfirmDialogDelegate|. +@interface TabModalConfirmDialogMacBridge : NSObject { + TabModalConfirmDialogDelegate* delegate_; // weak +} +- (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate; +- (void)alertDidEnd:(NSAlert*)alert + returnCode:(int)returnCode + contextInfo:(void*)contextInfo; +@end + +@implementation TabModalConfirmDialogMacBridge +- (id)initWithDelegate:(TabModalConfirmDialogDelegate*)delegate { + if ((self = [super init])) { + delegate_ = delegate; + } + return self; +} + +- (void)alertDidEnd:(NSAlert*)alert + returnCode:(int)returnCode + contextInfo:(void*)contextInfo { + if (returnCode == NSAlertFirstButtonReturn) { + delegate_->Accept(); + } else { + delegate_->Cancel(); + } +} +@end + +namespace browser { + +// Declared in browser_dialogs.h so others don't have to depend on our header. +void ShowTabModalConfirmDialog(TabModalConfirmDialogDelegate* delegate, + gfx::NativeWindow parent_window, + TabContents* tab_contents) { + // Deletes itself when closed. + new TabModalConfirmDialogMac(parent_window, delegate, tab_contents); +} + +} + +TabModalConfirmDialogMac::TabModalConfirmDialogMac( + NSWindow* parent, + TabModalConfirmDialogDelegate* delegate, + TabContents* tab_contents) + : ConstrainedWindowMacDelegateSystemSheet( + [[[TabModalConfirmDialogMacBridge alloc] initWithDelegate:delegate] + autorelease], + @selector(alertDidEnd:returnCode:contextInfo:)), + delegate_(delegate) { + scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); + [alert setMessageText: + l10n_util::FixUpWindowsStyleLabel(delegate->GetTitle())]; + [alert setInformativeText: + l10n_util::FixUpWindowsStyleLabel(delegate->GetMessage())]; + [alert addButtonWithTitle: + l10n_util::FixUpWindowsStyleLabel(delegate->GetAcceptButtonTitle())]; + [alert addButtonWithTitle: + l10n_util::FixUpWindowsStyleLabel(delegate->GetCancelButtonTitle())]; + gfx::Image* icon = delegate->GetIcon(); + if (icon) + [alert setIcon:icon->ToNSImage()]; + + set_sheet(alert); + + TabContentsWrapper* wrapper = + TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); + delegate->set_window(new ConstrainedWindowMac(wrapper, this)); +} + +TabModalConfirmDialogMac::~TabModalConfirmDialogMac() { + NSWindow* window = [(NSAlert*)sheet() window]; + if (window && is_sheet_open()) { + [NSApp endSheet:window + returnCode:NSAlertSecondButtonReturn]; + } +} + +// "DeleteDelegate" refers to this class being a ConstrainedWindow delegate +// and deleting itself, not to deleting the member variable |delegate_|. +void TabModalConfirmDialogMac::DeleteDelegate() { + delete this; +} diff --git a/chrome/browser/ui/gtk/browser_window_gtk.cc b/chrome/browser/ui/gtk/browser_window_gtk.cc index 9e37e02..879b2f0 100644 --- a/chrome/browser/ui/gtk/browser_window_gtk.cc +++ b/chrome/browser/ui/gtk/browser_window_gtk.cc @@ -62,7 +62,6 @@ #include "chrome/browser/ui/gtk/location_bar_view_gtk.h" #include "chrome/browser/ui/gtk/nine_box.h" #include "chrome/browser/ui/gtk/reload_button_gtk.h" -#include "chrome/browser/ui/gtk/repost_form_warning_gtk.h" #include "chrome/browser/ui/gtk/status_bubble_gtk.h" #include "chrome/browser/ui/gtk/tab_contents_container_gtk.h" #include "chrome/browser/ui/gtk/tabs/tab_strip_gtk.h" @@ -1046,10 +1045,6 @@ DownloadShelf* BrowserWindowGtk::GetDownloadShelf() { return download_shelf_.get(); } -void BrowserWindowGtk::ShowRepostFormWarningDialog(TabContents* tab_contents) { - new RepostFormWarningGtk(GetNativeHandle(), tab_contents); -} - void BrowserWindowGtk::ShowCollectedCookiesDialog(TabContentsWrapper* wrapper) { // Deletes itself on close. new CollectedCookiesGtk(GetNativeHandle(), wrapper); diff --git a/chrome/browser/ui/gtk/browser_window_gtk.h b/chrome/browser/ui/gtk/browser_window_gtk.h index 201aba2..4e95d87 100644 --- a/chrome/browser/ui/gtk/browser_window_gtk.h +++ b/chrome/browser/ui/gtk/browser_window_gtk.h @@ -128,7 +128,6 @@ class BrowserWindowGtk : public BrowserWindow, bool already_bookmarked) OVERRIDE; virtual bool IsDownloadShelfVisible() const OVERRIDE; virtual DownloadShelf* GetDownloadShelf() OVERRIDE; - virtual void ShowRepostFormWarningDialog(TabContents* tab_contents) OVERRIDE; virtual void ShowCollectedCookiesDialog( TabContentsWrapper* tab_contents) OVERRIDE; virtual void ConfirmBrowserCloseWithPendingDownloads() OVERRIDE; diff --git a/chrome/browser/ui/gtk/repost_form_warning_gtk.cc b/chrome/browser/ui/gtk/repost_form_warning_gtk.cc deleted file mode 100644 index db4e838..0000000 --- a/chrome/browser/ui/gtk/repost_form_warning_gtk.cc +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/ui/gtk/repost_form_warning_gtk.h" - -#include "base/message_loop.h" -#include "chrome/browser/repost_form_warning_controller.h" -#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" -#include "content/browser/tab_contents/navigation_controller.h" -#include "content/browser/tab_contents/tab_contents.h" -#include "content/public/browser/browser_thread.h" -#include "content/public/browser/notification_types.h" -#include "grit/generated_resources.h" -#include "ui/base/gtk/gtk_hig_constants.h" -#include "ui/base/l10n/l10n_util.h" - -RepostFormWarningGtk::RepostFormWarningGtk(GtkWindow* parent, - TabContents* tab_contents) - : controller_(new RepostFormWarningController(tab_contents)) { - dialog_ = gtk_vbox_new(FALSE, ui::kContentAreaBorder); - gtk_box_set_spacing(GTK_BOX(dialog_), ui::kContentAreaSpacing); - GtkWidget* label = gtk_label_new( - l10n_util::GetStringUTF8(IDS_HTTP_POST_WARNING).c_str()); - GtkWidget* image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION, - GTK_ICON_SIZE_DIALOG); - gtk_misc_set_alignment(GTK_MISC(image), 0.5, 0.0); - - gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); - gtk_label_set_selectable(GTK_LABEL(label), TRUE); - - GtkWidget *hbox = gtk_hbox_new(FALSE, ui::kControlSpacing); - - gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0); - - gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0); - - gtk_box_pack_start(GTK_BOX(dialog_), hbox, FALSE, FALSE, 0); - - GtkWidget* buttonBox = gtk_hbutton_box_new(); - gtk_button_box_set_layout(GTK_BUTTON_BOX(buttonBox), GTK_BUTTONBOX_END); - gtk_box_set_spacing(GTK_BOX(buttonBox), ui::kControlSpacing); - gtk_box_pack_end(GTK_BOX(dialog_), buttonBox, FALSE, TRUE, 0); - - cancel_ = gtk_button_new_from_stock(GTK_STOCK_CANCEL); - gtk_button_set_label(GTK_BUTTON(cancel_), - l10n_util::GetStringUTF8(IDS_CANCEL).c_str()); - g_signal_connect(cancel_, "clicked", G_CALLBACK(OnCancelThunk), this); - gtk_box_pack_end(GTK_BOX(buttonBox), cancel_, FALSE, TRUE, 0); - - ok_ = gtk_button_new_from_stock(GTK_STOCK_REFRESH); - gtk_button_set_label( - GTK_BUTTON(ok_), - l10n_util::GetStringUTF8(IDS_HTTP_POST_WARNING_RESEND).c_str()); - g_signal_connect(ok_, "clicked", G_CALLBACK(OnRefreshThunk), this); - gtk_box_pack_end(GTK_BOX(buttonBox), ok_, FALSE, TRUE, 0); - - TabContentsWrapper* wrapper = - TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); - controller_->set_window(new ConstrainedWindowGtk(wrapper, this)); -} - -GtkWidget* RepostFormWarningGtk::GetWidgetRoot() { - return dialog_; -} - -GtkWidget* RepostFormWarningGtk::GetFocusWidget() { - return cancel_; -} - -void RepostFormWarningGtk::DeleteDelegate() { - delete this; -} - -RepostFormWarningGtk::~RepostFormWarningGtk() { - gtk_widget_destroy(dialog_); -} - -void RepostFormWarningGtk::OnRefresh(GtkWidget* widget) { - controller_->Continue(); -} - -void RepostFormWarningGtk::OnCancel(GtkWidget* widget) { - controller_->Cancel(); -} diff --git a/chrome/browser/ui/gtk/repost_form_warning_gtk.h b/chrome/browser/ui/gtk/repost_form_warning_gtk.h deleted file mode 100644 index f14d335..0000000 --- a/chrome/browser/ui/gtk/repost_form_warning_gtk.h +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_UI_GTK_REPOST_FORM_WARNING_GTK_H_ -#define CHROME_BROWSER_UI_GTK_REPOST_FORM_WARNING_GTK_H_ -#pragma once - -#include <gtk/gtk.h> - -#include "base/compiler_specific.h" -#include "base/memory/scoped_ptr.h" -#include "chrome/browser/ui/gtk/constrained_window_gtk.h" -#include "ui/base/gtk/gtk_signal.h" - -class RepostFormWarningController; -class TabContents; - -// Displays a dialog that warns the user that they are about to resubmit -// a form. -// To display the dialog, allocate this object on the heap. It will open the -// dialog from its constructor and then delete itself when the user dismisses -// the dialog. -class RepostFormWarningGtk : public ConstrainedWindowGtkDelegate { - public: - RepostFormWarningGtk(GtkWindow* parent, TabContents* tab_contents); - - // ConstrainedWindowGtkDelegate methods - virtual GtkWidget* GetWidgetRoot() OVERRIDE; - virtual GtkWidget* GetFocusWidget() OVERRIDE; - virtual void DeleteDelegate() OVERRIDE; - - private: - virtual ~RepostFormWarningGtk(); - - // Callbacks - CHROMEGTK_CALLBACK_0(RepostFormWarningGtk, void, OnRefresh); - CHROMEGTK_CALLBACK_0(RepostFormWarningGtk, void, OnCancel); - - scoped_ptr<RepostFormWarningController> controller_; - - GtkWidget* dialog_; - GtkWidget* ok_; - GtkWidget* cancel_; - - DISALLOW_COPY_AND_ASSIGN(RepostFormWarningGtk); -}; - -#endif // CHROME_BROWSER_UI_GTK_REPOST_FORM_WARNING_GTK_H_ diff --git a/chrome/browser/ui/gtk/tab_modal_confirm_dialog_gtk.cc b/chrome/browser/ui/gtk/tab_modal_confirm_dialog_gtk.cc new file mode 100644 index 0000000..8cdafdb --- /dev/null +++ b/chrome/browser/ui/gtk/tab_modal_confirm_dialog_gtk.cc @@ -0,0 +1,109 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/gtk/tab_modal_confirm_dialog_gtk.h" + +#include "base/message_loop.h" +#include "base/utf_string_conversions.h" +#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" +#include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" +#include "content/browser/tab_contents/navigation_controller.h" +#include "content/browser/tab_contents/tab_contents.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/notification_types.h" +#include "grit/generated_resources.h" +#include "ui/base/gtk/gtk_hig_constants.h" +#include "ui/base/l10n/l10n_util.h" +#include "ui/gfx/image/image.h" + +namespace browser { + +// Declared in browser_dialogs.h so others don't have to depend on our header. +void ShowTabModalConfirmDialog(TabModalConfirmDialogDelegate* delegate, + gfx::NativeWindow parent_window, + TabContents* tab_contents) { + new TabModalConfirmDialogGtk(parent_window, delegate, tab_contents); +} + +} + +TabModalConfirmDialogGtk::TabModalConfirmDialogGtk( + GtkWindow* parent, + TabModalConfirmDialogDelegate* delegate, + TabContents* tab_contents) + : delegate_(delegate) { + dialog_ = gtk_vbox_new(FALSE, ui::kContentAreaBorder); + gtk_box_set_spacing(GTK_BOX(dialog_), ui::kContentAreaSpacing); + GtkWidget* label = gtk_label_new( + UTF16ToUTF8(delegate->GetMessage()).c_str()); + gfx::Image* icon = delegate->GetIcon(); + GtkWidget* image = icon ? gtk_image_new_from_pixbuf(icon->ToGdkPixbuf()) + : gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION, + GTK_ICON_SIZE_DIALOG); + gtk_misc_set_alignment(GTK_MISC(image), 0.5, 0.0); + + gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); + gtk_label_set_selectable(GTK_LABEL(label), TRUE); + + GtkWidget *hbox = gtk_hbox_new(FALSE, ui::kControlSpacing); + + gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0); + + gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0); + + gtk_box_pack_start(GTK_BOX(dialog_), hbox, FALSE, FALSE, 0); + + GtkWidget* buttonBox = gtk_hbutton_box_new(); + gtk_button_box_set_layout(GTK_BUTTON_BOX(buttonBox), GTK_BUTTONBOX_END); + gtk_box_set_spacing(GTK_BOX(buttonBox), ui::kControlSpacing); + gtk_box_pack_end(GTK_BOX(dialog_), buttonBox, FALSE, TRUE, 0); + + cancel_ = gtk_button_new_with_label( + UTF16ToUTF8(delegate->GetCancelButtonTitle()).c_str()); + const char* cancel_button_icon_id = delegate->GetCancelButtonIcon(); + if (cancel_button_icon_id) { + gtk_button_set_image(GTK_BUTTON(cancel_), gtk_image_new_from_stock( + cancel_button_icon_id, GTK_ICON_SIZE_BUTTON)); + } + g_signal_connect(cancel_, "clicked", G_CALLBACK(OnCancelThunk), this); + gtk_box_pack_end(GTK_BOX(buttonBox), cancel_, FALSE, TRUE, 0); + + ok_ = gtk_button_new_with_label( + UTF16ToUTF8(delegate->GetAcceptButtonTitle()).c_str()); + const char* accept_button_icon_id = delegate->GetAcceptButtonIcon(); + if (accept_button_icon_id) { + gtk_button_set_image(GTK_BUTTON(ok_), gtk_image_new_from_stock( + accept_button_icon_id, GTK_ICON_SIZE_BUTTON)); + } + g_signal_connect(ok_, "clicked", G_CALLBACK(OnRefreshThunk), this); + gtk_box_pack_end(GTK_BOX(buttonBox), ok_, FALSE, TRUE, 0); + + TabContentsWrapper* wrapper = + TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); + delegate->set_window(new ConstrainedWindowGtk(wrapper, this)); +} + +GtkWidget* TabModalConfirmDialogGtk::GetWidgetRoot() { + return dialog_; +} + +GtkWidget* TabModalConfirmDialogGtk::GetFocusWidget() { + return cancel_; +} + +void TabModalConfirmDialogGtk::DeleteDelegate() { + delete this; +} + +TabModalConfirmDialogGtk::~TabModalConfirmDialogGtk() { + gtk_widget_destroy(dialog_); +} + +void TabModalConfirmDialogGtk::OnRefresh(GtkWidget* widget) { + delegate_->Accept(); +} + +void TabModalConfirmDialogGtk::OnCancel(GtkWidget* widget) { + delegate_->Cancel(); +} diff --git a/chrome/browser/ui/gtk/tab_modal_confirm_dialog_gtk.h b/chrome/browser/ui/gtk/tab_modal_confirm_dialog_gtk.h new file mode 100644 index 0000000..3cfbfed --- /dev/null +++ b/chrome/browser/ui/gtk/tab_modal_confirm_dialog_gtk.h @@ -0,0 +1,51 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_GTK_TAB_MODAL_CONFIRM_DIALOG_GTK_H_ +#define CHROME_BROWSER_UI_GTK_TAB_MODAL_CONFIRM_DIALOG_GTK_H_ +#pragma once + +#include <gtk/gtk.h> + +#include "base/compiler_specific.h" +#include "base/memory/scoped_ptr.h" +#include "chrome/browser/ui/gtk/constrained_window_gtk.h" +#include "ui/base/gtk/gtk_signal.h" + +class TabContents; +class TabModalConfirmDialogDelegate; + +// Displays a tab-modal dialog, i.e. a dialog that will block the current page +// but still allow the user to switch to a different page. +// To display the dialog, allocate this object on the heap. It will open the +// dialog from its constructor and then delete itself when the user dismisses +// the dialog. +class TabModalConfirmDialogGtk : public ConstrainedWindowGtkDelegate { + public: + TabModalConfirmDialogGtk(GtkWindow* parent, + TabModalConfirmDialogDelegate* delegate, + TabContents* tab_contents); + + // ConstrainedWindowGtkDelegate methods + virtual GtkWidget* GetWidgetRoot() OVERRIDE; + virtual GtkWidget* GetFocusWidget() OVERRIDE; + virtual void DeleteDelegate() OVERRIDE; + + private: + virtual ~TabModalConfirmDialogGtk(); + + // Callbacks + CHROMEGTK_CALLBACK_0(TabModalConfirmDialogGtk, void, OnRefresh); + CHROMEGTK_CALLBACK_0(TabModalConfirmDialogGtk, void, OnCancel); + + scoped_ptr<TabModalConfirmDialogDelegate> delegate_; + + GtkWidget* dialog_; + GtkWidget* ok_; + GtkWidget* cancel_; + + DISALLOW_COPY_AND_ASSIGN(TabModalConfirmDialogGtk); +}; + +#endif // CHROME_BROWSER_UI_GTK_TAB_MODAL_CONFIRM_DIALOG_GTK_H_ diff --git a/chrome/browser/ui/panels/panel.cc b/chrome/browser/ui/panels/panel.cc index d09e373..1e4d991 100644 --- a/chrome/browser/ui/panels/panel.cc +++ b/chrome/browser/ui/panels/panel.cc @@ -427,10 +427,6 @@ DownloadShelf* Panel::GetDownloadShelf() { return tabbed_browser->window()->GetDownloadShelf(); } -void Panel::ShowRepostFormWarningDialog(TabContents* tab_contents) { - NOTIMPLEMENTED(); -} - void Panel::ShowCollectedCookiesDialog(TabContentsWrapper* wrapper) { NOTIMPLEMENTED(); } diff --git a/chrome/browser/ui/panels/panel.h b/chrome/browser/ui/panels/panel.h index d3e6b49..547ccff 100644 --- a/chrome/browser/ui/panels/panel.h +++ b/chrome/browser/ui/panels/panel.h @@ -140,7 +140,6 @@ class Panel : public BrowserWindow, const GURL& url, bool already_bookmarked) OVERRIDE; virtual bool IsDownloadShelfVisible() const OVERRIDE; virtual DownloadShelf* GetDownloadShelf() OVERRIDE; - virtual void ShowRepostFormWarningDialog(TabContents* tab_contents) OVERRIDE; virtual void ShowCollectedCookiesDialog(TabContentsWrapper* wrapper) OVERRIDE; virtual void ConfirmBrowserCloseWithPendingDownloads() OVERRIDE; virtual void UserChangedTheme() OVERRIDE; diff --git a/chrome/browser/ui/tab_modal_confirm_dialog_delegate.cc b/chrome/browser/ui/tab_modal_confirm_dialog_delegate.cc new file mode 100644 index 0000000..f2b3590 --- /dev/null +++ b/chrome/browser/ui/tab_modal_confirm_dialog_delegate.cc @@ -0,0 +1,93 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" + +#include "chrome/browser/ui/constrained_window.h" +#include "content/browser/tab_contents/tab_contents.h" +#include "content/public/browser/notification_source.h" +#include "content/public/browser/notification_types.h" +#include "grit/generated_resources.h" +#include "ui/base/l10n/l10n_util.h" + +TabModalConfirmDialogDelegate::TabModalConfirmDialogDelegate( + TabContents* tab_contents) + : window_(NULL), + closing_(false) { + NavigationController* controller = &tab_contents->controller(); + registrar_.Add(this, content::NOTIFICATION_LOAD_START, + content::Source<NavigationController>(controller)); + registrar_.Add(this, content::NOTIFICATION_TAB_CLOSING, + content::Source<NavigationController>(controller)); +} + +TabModalConfirmDialogDelegate::~TabModalConfirmDialogDelegate() { + // If we end up here, the constrained window has been closed, so make sure we + // don't close it again. + window_ = NULL; + // Make sure everything is cleaned up. + Cancel(); +} + +void TabModalConfirmDialogDelegate::Cancel() { + if (closing_) + return; + OnCanceled(); + CloseDialog(); +} + +void TabModalConfirmDialogDelegate::Accept() { + if (closing_) + return; + OnAccepted(); + CloseDialog(); +} + + +void TabModalConfirmDialogDelegate::Observe( + int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) { + // Close the dialog if we load a page (because the action might not apply to + // the same page anymore) or if the tab is closed. + if (type == content::NOTIFICATION_LOAD_START || + type == content::NOTIFICATION_TAB_CLOSING) { + Cancel(); + } else { + NOTREACHED(); + } +} + +gfx::Image* TabModalConfirmDialogDelegate::GetIcon() { + return NULL; +} + +string16 TabModalConfirmDialogDelegate::GetAcceptButtonTitle() { + return l10n_util::GetStringUTF16(IDS_OK); +} + +string16 TabModalConfirmDialogDelegate::GetCancelButtonTitle() { + return l10n_util::GetStringUTF16(IDS_CANCEL); +} + +const char* TabModalConfirmDialogDelegate::GetAcceptButtonIcon() { + return NULL; +} + +const char* TabModalConfirmDialogDelegate::GetCancelButtonIcon() { + return NULL; +} + +void TabModalConfirmDialogDelegate::OnAccepted() { +} + +void TabModalConfirmDialogDelegate::OnCanceled() { +} + +void TabModalConfirmDialogDelegate::CloseDialog() { + // Make sure we won't do anything when |Cancel()| is called again. + closing_ = true; + if (window_) + window_->CloseConstrainedWindow(); +} diff --git a/chrome/browser/ui/tab_modal_confirm_dialog_delegate.h b/chrome/browser/ui/tab_modal_confirm_dialog_delegate.h new file mode 100644 index 0000000..7055a6f --- /dev/null +++ b/chrome/browser/ui/tab_modal_confirm_dialog_delegate.h @@ -0,0 +1,79 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_TAB_MODAL_CONFIRM_DIALOG_DELEGATE_H_ +#define CHROME_BROWSER_UI_TAB_MODAL_CONFIRM_DIALOG_DELEGATE_H_ +#pragma once + +#include "base/callback.h" +#include "base/compiler_specific.h" +#include "base/string16.h" +#include "content/public/browser/notification_observer.h" +#include "content/public/browser/notification_registrar.h" + +namespace gfx { +class Image; +} +class ConstrainedWindow; +class TabContents; + +// This class acts as the delegate for a simple tab-modal dialog confirming +// whether the user wants to execute a certain action. +class TabModalConfirmDialogDelegate : public content::NotificationObserver { + public: + explicit TabModalConfirmDialogDelegate(TabContents* tab_contents); + virtual ~TabModalConfirmDialogDelegate(); + + void set_window(ConstrainedWindow* window) { window_ = window; } + + void Accept(); + void Cancel(); + + // The title of the dialog. Note that the title is not shown on all platforms. + virtual string16 GetTitle() = 0; + virtual string16 GetMessage() = 0; + + // Icon to show for the dialog. If this method is not overridden, a default + // icon (like the application icon) is shown. + virtual gfx::Image* GetIcon(); + + // Title for the accept and the cancel buttons. + // The default implementation uses IDS_OK and IDS_CANCEL. + virtual string16 GetAcceptButtonTitle(); + virtual string16 GetCancelButtonTitle(); + + // GTK stock icon names for the accept and cancel buttons, respectively. + // The icons are only used on GTK. If these methods are not overriden, + // the buttons have no stock icons. + virtual const char* GetAcceptButtonIcon(); + virtual const char* GetCancelButtonIcon(); + + protected: + ConstrainedWindow* window() { return window_; } + + // content::NotificationObserver implementation. + // Watch for a new load or a closed tab and dismiss the dialog if they occur. + virtual void Observe(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) OVERRIDE; + + content::NotificationRegistrar registrar_; + + private: + // Called when the user accepts or cancels the dialog, respectively. + virtual void OnAccepted(); + virtual void OnCanceled(); + + // Close the dialog. + void CloseDialog(); + + ConstrainedWindow* window_; + // True iff we are in the process of closing, to avoid running callbacks + // multiple times. + bool closing_; + + DISALLOW_COPY_AND_ASSIGN(TabModalConfirmDialogDelegate); +}; + +#endif // CHROME_BROWSER_UI_TAB_MODAL_CONFIRM_DIALOG_DELEGATE_H_ diff --git a/chrome/browser/ui/views/browser_dialogs.h b/chrome/browser/ui/views/browser_dialogs.h index af21b2c..4afb960 100644 --- a/chrome/browser/ui/views/browser_dialogs.h +++ b/chrome/browser/ui/views/browser_dialogs.h @@ -82,15 +82,10 @@ void EditSearchEngine(gfx::NativeWindow parent, EditSearchEngineControllerDelegate* delegate, Profile* profile); -// Shows the repost form confirmation dialog box. -void ShowRepostFormWarningDialog(gfx::NativeWindow parent_window, - TabContents* tab_contents); - // Shows the collected cookies dialog box. void ShowCollectedCookiesDialog(gfx::NativeWindow parent_window, TabContentsWrapper* tab_contents); - // Shows the create web app shortcut dialog box. void ShowCreateWebAppShortcutsDialog(gfx::NativeWindow parent_window, TabContentsWrapper* tab_contents); diff --git a/chrome/browser/ui/views/dialog_stubs_gtk.cc b/chrome/browser/ui/views/dialog_stubs_gtk.cc index 5306594..e255bd5 100644 --- a/chrome/browser/ui/views/dialog_stubs_gtk.cc +++ b/chrome/browser/ui/views/dialog_stubs_gtk.cc @@ -10,18 +10,12 @@ #include "base/logging.h" #include "chrome/browser/ui/gtk/edit_search_engine_dialog.h" #include "chrome/browser/ui/views/browser_dialogs.h" -#include "chrome/browser/ui/webui/collected_cookies_ui_delegate.h" #include "ui/gfx/native_widget_types.h" #if !defined(WEBUI_TASK_MANAGER) #include "chrome/browser/ui/gtk/task_manager_gtk.h" #endif -#if !defined(OS_CHROMEOS) -#include "chrome/browser/ui/gtk/collected_cookies_gtk.h" -#include "chrome/browser/ui/gtk/repost_form_warning_gtk.h" -#endif - namespace browser { #if !defined(WEBUI_TASK_MANAGER) diff --git a/chrome/browser/ui/views/frame/browser_view.cc b/chrome/browser/ui/views/frame/browser_view.cc index d6b30c7..f8f7b6e 100644 --- a/chrome/browser/ui/views/frame/browser_view.cc +++ b/chrome/browser/ui/views/frame/browser_view.cc @@ -1146,10 +1146,6 @@ DownloadShelf* BrowserView::GetDownloadShelf() { #endif } -void BrowserView::ShowRepostFormWarningDialog(TabContents* tab_contents) { - browser::ShowRepostFormWarningDialog(GetNativeHandle(), tab_contents); -} - void BrowserView::ShowCollectedCookiesDialog(TabContentsWrapper* wrapper) { browser::ShowCollectedCookiesDialog(GetNativeHandle(), wrapper); } diff --git a/chrome/browser/ui/views/frame/browser_view.h b/chrome/browser/ui/views/frame/browser_view.h index 24c010e..ba32747 100644 --- a/chrome/browser/ui/views/frame/browser_view.h +++ b/chrome/browser/ui/views/frame/browser_view.h @@ -307,7 +307,6 @@ class BrowserView : public BrowserWindow, void SetDownloadShelfVisible(bool visible); virtual bool IsDownloadShelfVisible() const OVERRIDE; virtual DownloadShelf* GetDownloadShelf() OVERRIDE; - virtual void ShowRepostFormWarningDialog(TabContents* tab_contents) OVERRIDE; virtual void ShowCollectedCookiesDialog(TabContentsWrapper* wrapper) OVERRIDE; virtual void ConfirmBrowserCloseWithPendingDownloads() OVERRIDE; virtual void UserChangedTheme() OVERRIDE; diff --git a/chrome/browser/ui/views/repost_form_warning_view.cc b/chrome/browser/ui/views/tab_modal_confirm_dialog_views.cc index 36d9290..761a7a1 100644 --- a/chrome/browser/ui/views/repost_form_warning_view.cc +++ b/chrome/browser/ui/views/tab_modal_confirm_dialog_views.cc @@ -2,13 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chrome/browser/ui/views/repost_form_warning_view.h" +#include "chrome/browser/ui/views/tab_modal_confirm_dialog_views.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/repost_form_warning_controller.h" #include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" +#include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" #include "chrome/browser/ui/views/constrained_window_views.h" #include "content/browser/tab_contents/navigation_controller.h" #include "content/browser/tab_contents/tab_contents.h" @@ -19,74 +19,76 @@ namespace browser { // Declared in browser_dialogs.h so others don't have to depend on our header. -void ShowRepostFormWarningDialog(gfx::NativeWindow parent_window, - TabContents* tab_contents) { - new RepostFormWarningView(parent_window, tab_contents); +void ShowTabModalConfirmDialog(TabModalConfirmDialogDelegate* delegate, + gfx::NativeWindow parent_window, + TabContents* tab_contents) { + new TabModalConfirmDialogViews(delegate, parent_window, tab_contents); } } // namespace browser ////////////////////////////////////////////////////////////////////////////// -// RepostFormWarningView, constructor & destructor: +// TabModalConfirmDialogViews, constructor & destructor: -RepostFormWarningView::RepostFormWarningView( +TabModalConfirmDialogViews::TabModalConfirmDialogViews( + TabModalConfirmDialogDelegate* delegate, gfx::NativeWindow parent_window, TabContents* tab_contents) - : controller_(new RepostFormWarningController(tab_contents)), - message_box_view_(NULL) { + : delegate_(delegate), + message_box_view_(NULL) { message_box_view_ = new views::MessageBoxView( views::MessageBoxView::NO_OPTIONS, - l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING), + delegate->GetMessage(), string16()); TabContentsWrapper* wrapper = TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); - controller_->set_window(new ConstrainedWindowViews(wrapper, this)); + delegate_->set_window(new ConstrainedWindowViews(wrapper, this)); } -RepostFormWarningView::~RepostFormWarningView() { +TabModalConfirmDialogViews::~TabModalConfirmDialogViews() { } ////////////////////////////////////////////////////////////////////////////// -// RepostFormWarningView, views::DialogDelegate implementation: +// TabModalConfirmDialogViews, views::DialogDelegate implementation: -string16 RepostFormWarningView::GetWindowTitle() const { - return l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING_TITLE); +string16 TabModalConfirmDialogViews::GetWindowTitle() const { + return delegate_->GetTitle(); } -string16 RepostFormWarningView::GetDialogButtonLabel( +string16 TabModalConfirmDialogViews::GetDialogButtonLabel( ui::DialogButton button) const { if (button == ui::DIALOG_BUTTON_OK) - return l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING_RESEND); + return delegate_->GetAcceptButtonTitle(); if (button == ui::DIALOG_BUTTON_CANCEL) - return l10n_util::GetStringUTF16(IDS_CANCEL); + return delegate_->GetCancelButtonTitle(); return string16(); } -views::View* RepostFormWarningView::GetContentsView() { - return message_box_view_; +bool TabModalConfirmDialogViews::Cancel() { + delegate_->Cancel(); + return true; } -views::Widget* RepostFormWarningView::GetWidget() { - return message_box_view_->GetWidget(); +bool TabModalConfirmDialogViews::Accept() { + delegate_->Accept(); + return true; } -const views::Widget* RepostFormWarningView::GetWidget() const { - return message_box_view_->GetWidget(); -} +/////////////////////////////////////////////////////////////////////////////// +// TabModalConfirmDialogViews, views::WidgetDelegate implementation: -bool RepostFormWarningView::Cancel() { - controller_->Cancel(); - return true; +views::View* TabModalConfirmDialogViews::GetContentsView() { + return message_box_view_; } -bool RepostFormWarningView::Accept() { - controller_->Continue(); - return true; +views::Widget* TabModalConfirmDialogViews::GetWidget() { + return message_box_view_->GetWidget(); } -/////////////////////////////////////////////////////////////////////////////// -// RepostFormWarningView, RepostFormWarning implementation: +const views::Widget* TabModalConfirmDialogViews::GetWidget() const { + return message_box_view_->GetWidget(); +} -void RepostFormWarningView::DeleteDelegate() { +void TabModalConfirmDialogViews::DeleteDelegate() { delete this; } diff --git a/chrome/browser/ui/views/repost_form_warning_view.h b/chrome/browser/ui/views/tab_modal_confirm_dialog_views.h index d12301f..86c106c 100644 --- a/chrome/browser/ui/views/repost_form_warning_view.h +++ b/chrome/browser/ui/views/tab_modal_confirm_dialog_views.h @@ -2,35 +2,34 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_BROWSER_UI_VIEWS_REPOST_FORM_WARNING_VIEW_H_ -#define CHROME_BROWSER_UI_VIEWS_REPOST_FORM_WARNING_VIEW_H_ +#ifndef CHROME_BROWSER_UI_VIEWS_TAB_MODAL_CONFIRM_DIALOG_VIEWS_H_ +#define CHROME_BROWSER_UI_VIEWS_TAB_MODAL_CONFIRM_DIALOG_VIEWS_H_ #pragma once #include "ui/gfx/native_widget_types.h" #include "ui/views/window/dialog_delegate.h" -class RepostFormWarningController; +class TabModalConfirmDialogDelegate; class TabContents; namespace views { class MessageBoxView; } -// Displays a dialog that warns the user that they are about to resubmit -// a form. +// Displays a tab-modal dialog, i.e. a dialog that will block the current page +// but still allow the user to switch to a different page. // To display the dialog, allocate this object on the heap. It will open the // dialog from its constructor and then delete itself when the user dismisses // the dialog. -class RepostFormWarningView : public views::DialogDelegate { +class TabModalConfirmDialogViews : public views::DialogDelegate { public: - // Use BrowserWindow::ShowRepostFormWarningDialog to use. - RepostFormWarningView(gfx::NativeWindow parent_window, - TabContents* tab_contents); + TabModalConfirmDialogViews(TabModalConfirmDialogDelegate* delegate, + gfx::NativeWindow parent_window, + TabContents* tab_contents); // views::DialogDelegate: virtual string16 GetWindowTitle() const OVERRIDE; virtual string16 GetDialogButtonLabel(ui::DialogButton button) const OVERRIDE; - virtual void DeleteDelegate() OVERRIDE; virtual bool Cancel() OVERRIDE; virtual bool Accept() OVERRIDE; @@ -38,16 +37,17 @@ class RepostFormWarningView : public views::DialogDelegate { virtual views::View* GetContentsView() OVERRIDE; virtual views::Widget* GetWidget() OVERRIDE; virtual const views::Widget* GetWidget() const OVERRIDE; + virtual void DeleteDelegate() OVERRIDE; private: - virtual ~RepostFormWarningView(); + virtual ~TabModalConfirmDialogViews(); - scoped_ptr<RepostFormWarningController> controller_; + scoped_ptr<TabModalConfirmDialogDelegate> delegate_; // The message box view whose commands we handle. views::MessageBoxView* message_box_view_; - DISALLOW_COPY_AND_ASSIGN(RepostFormWarningView); + DISALLOW_COPY_AND_ASSIGN(TabModalConfirmDialogViews); }; -#endif // CHROME_BROWSER_UI_VIEWS_REPOST_FORM_WARNING_VIEW_H_ +#endif // CHROME_BROWSER_UI_VIEWS_TAB_MODAL_CONFIRM_DIALOG_VIEWS_H_ diff --git a/chrome/browser/ui/webui/chrome_url_data_manager_backend.cc b/chrome/browser/ui/webui/chrome_url_data_manager_backend.cc index 5c3295a..c3bc631 100644 --- a/chrome/browser/ui/webui/chrome_url_data_manager_backend.cc +++ b/chrome/browser/ui/webui/chrome_url_data_manager_backend.cc @@ -78,7 +78,7 @@ class ChromeURLContentSecurityPolicyExceptionSet #if defined(OS_CHROMEOS) || defined(USE_AURA) insert(chrome::kChromeUICollectedCookiesHost); insert(chrome::kChromeUIHttpAuthHost); - insert(chrome::kChromeUIRepostFormWarningHost); + insert(chrome::kChromeUITabModalConfirmDialogHost); #endif #if defined(USE_AURA) insert(chrome::kChromeUIAppListHost); diff --git a/chrome/browser/ui/webui/chrome_web_ui_factory.cc b/chrome/browser/ui/webui/chrome_web_ui_factory.cc index fab6328..39f88e5 100644 --- a/chrome/browser/ui/webui/chrome_web_ui_factory.cc +++ b/chrome/browser/ui/webui/chrome_web_ui_factory.cc @@ -261,7 +261,7 @@ WebUIFactoryFunction GetWebUIFactoryFunction(TabContents* tab_contents, #if (defined(OS_LINUX) && defined(TOOLKIT_VIEWS)) || defined(USE_AURA) if (url.host() == chrome::kChromeUICollectedCookiesHost || url.host() == chrome::kChromeUIHttpAuthHost || - url.host() == chrome::kChromeUIRepostFormWarningHost) { + url.host() == chrome::kChromeUITabModalConfirmDialogHost) { return &NewWebUI<ConstrainedHtmlUI>; } #endif diff --git a/chrome/browser/ui/webui/repost_form_warning_ui.cc b/chrome/browser/ui/webui/repost_form_warning_ui.cc deleted file mode 100644 index 652fdcb..0000000 --- a/chrome/browser/ui/webui/repost_form_warning_ui.cc +++ /dev/null @@ -1,166 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/ui/webui/repost_form_warning_ui.h" - -#include <string> - -#include "base/basictypes.h" -#include "base/json/json_reader.h" -#include "base/message_loop.h" -#include "base/string_piece.h" -#include "base/utf_string_conversions.h" -#include "base/values.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/repost_form_warning_controller.h" -#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" -#include "chrome/browser/ui/webui/chrome_url_data_manager.h" -#include "chrome/browser/ui/webui/constrained_html_ui.h" -#include "chrome/browser/ui/webui/html_dialog_ui.h" -#include "chrome/common/jstemplate_builder.h" -#include "chrome/common/url_constants.h" -#include "content/browser/tab_contents/tab_contents.h" -#include "grit/browser_resources.h" -#include "grit/generated_resources.h" -#include "ui/base/l10n/l10n_util.h" -#include "ui/base/resource/resource_bundle.h" -#include "ui/gfx/size.h" - -using std::string; - -namespace browser { - -// Declared in browser_dialogs.h so others don't have to depend on our header. -void ShowRepostFormWarningDialog(gfx::NativeWindow parent_window, - TabContents* tab_contents) { - new RepostFormWarningUI(parent_window, tab_contents); -} - -} // namespace browser - -class RepostFormWarningSource : public ChromeURLDataManager::DataSource { - public: - RepostFormWarningSource() - : DataSource(chrome::kChromeUIRepostFormWarningHost, - MessageLoop::current()) { - } - - virtual void StartDataRequest(const std::string& path, - bool is_off_the_record, - int request_id) OVERRIDE { - DictionaryValue dict; - dict.SetString("explanation", - l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING)); - dict.SetString("resend", - l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING_RESEND)); - dict.SetString("cancel", l10n_util::GetStringUTF16(IDS_CANCEL)); - - SetFontAndTextDirection(&dict); - base::StringPiece html = - ResourceBundle::GetSharedInstance().GetRawDataResource( - IDR_REPOST_FORM_WARNING_HTML); - string response = jstemplate_builder::GetI18nTemplateHtml(html, &dict); - SendResponse(request_id, base::RefCountedString::TakeString(&response)); - } - - virtual string GetMimeType(const std::string& path) const OVERRIDE { - return "text/html"; - } - - static void RegisterDataSource(Profile* profile) { - ChromeURLDataManager* url_manager = profile->GetChromeURLDataManager(); - url_manager->AddDataSource(new RepostFormWarningSource()); - } - - private: - virtual ~RepostFormWarningSource() {} - - DISALLOW_COPY_AND_ASSIGN(RepostFormWarningSource); -}; - -class RepostFormWarningHtmlDelegate : public HtmlDialogUIDelegate { - public: - explicit RepostFormWarningHtmlDelegate(RepostFormWarningUI* ui) : ui_(ui) {} - - virtual ~RepostFormWarningHtmlDelegate() {} - - // HtmlDialogUIDelegate implementation. - virtual bool IsDialogModal() const OVERRIDE { - return true; - } - - virtual string16 GetDialogTitle() const OVERRIDE { - return l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING_TITLE); - } - - virtual GURL GetDialogContentURL() const OVERRIDE { - return GURL(chrome::kChromeUIRepostFormWarningURL); - } - - virtual void GetWebUIMessageHandlers( - std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {} - - virtual void GetDialogSize(gfx::Size* size) const OVERRIDE { - size->SetSize(kDialogWidth, kDialogHeight); - } - - virtual std::string GetDialogArgs() const OVERRIDE { - return string(); - } - - virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { - bool repost = false; - if (!json_retval.empty()) { - base::JSONReader reader; - scoped_ptr<Value> value(reader.JsonToValue(json_retval, false, false)); - if (!value.get() || !value->GetAsBoolean(&repost)) - NOTREACHED() << "Missing or unreadable response from dialog"; - } - - ui_->OnDialogClosed(repost); - ui_ = NULL; - delete this; - } - - virtual void OnCloseContents(TabContents* source, - bool* out_close_dialog) OVERRIDE {} - - virtual bool ShouldShowDialogTitle() const OVERRIDE { - return true; - } - - private: - static const int kDialogWidth = 400; - static const int kDialogHeight = 108; - - RepostFormWarningUI* ui_; // not owned - - DISALLOW_COPY_AND_ASSIGN(RepostFormWarningHtmlDelegate); -}; - -RepostFormWarningUI::RepostFormWarningUI(gfx::NativeWindow parent_window, - TabContents* tab_contents) - : controller_(new RepostFormWarningController(tab_contents)) { - TabContentsWrapper* wrapper = - TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); - Profile* profile = wrapper->profile(); - RepostFormWarningSource::RegisterDataSource(profile); - RepostFormWarningHtmlDelegate* html_delegate = - new RepostFormWarningHtmlDelegate(this); - ConstrainedHtmlUIDelegate* dialog_delegate = - ConstrainedHtmlUI::CreateConstrainedHtmlDialog( - profile, html_delegate, wrapper); - controller_->set_window(dialog_delegate->window()); -} - -RepostFormWarningUI::~RepostFormWarningUI() {} - -void RepostFormWarningUI::OnDialogClosed(bool repost) { - controller_->set_window(NULL); - if (repost) - controller_->Continue(); - else - controller_->Cancel(); - delete this; -} diff --git a/chrome/browser/ui/webui/repost_form_warning_ui.h b/chrome/browser/ui/webui/repost_form_warning_ui.h deleted file mode 100644 index a8a3e72..0000000 --- a/chrome/browser/ui/webui/repost_form_warning_ui.h +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_UI_WEBUI_REPOST_FORM_WARNING_UI_H_ -#define CHROME_BROWSER_UI_WEBUI_REPOST_FORM_WARNING_UI_H_ -#pragma once - -#include "base/memory/scoped_ptr.h" -#include "ui/gfx/native_widget_types.h" - -class BrowserWindow; -class RepostFormWarningController; -class TabContents; - -namespace browser { -void ShowRepostFormWarningDialog(gfx::NativeWindow parent_window, - TabContents* tab_contents); -} - -// Displays a dialog that warns the user that they are about to resubmit -// a form. -// To display the dialog, allocate this object on the heap. It will open the -// dialog from its constructor and then delete itself when the user dismisses -// the dialog. -class RepostFormWarningUI { - public: - // Invoked when the dialog is closed. Notifies the controller of the user's - // response and deletes this object. - void OnDialogClosed(bool repost); - - private: - friend void browser::ShowRepostFormWarningDialog( - gfx::NativeWindow parent_window, TabContents* tab_contents); - - // Call BrowserWindow::ShowRepostFormWarningDialog() to use. - RepostFormWarningUI(gfx::NativeWindow parent_window, - TabContents* tab_contents); - - virtual ~RepostFormWarningUI(); - - scoped_ptr<RepostFormWarningController> controller_; - - DISALLOW_COPY_AND_ASSIGN(RepostFormWarningUI); -}; - -#endif // CHROME_BROWSER_UI_WEBUI_REPOST_FORM_WARNING_UI_H_ diff --git a/chrome/browser/ui/webui/tab_modal_confirm_dialog_webui.cc b/chrome/browser/ui/webui/tab_modal_confirm_dialog_webui.cc new file mode 100644 index 0000000..ece1b11 --- /dev/null +++ b/chrome/browser/ui/webui/tab_modal_confirm_dialog_webui.cc @@ -0,0 +1,143 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/webui/tab_modal_confirm_dialog_webui.h" + +#include <string> + +#include "base/basictypes.h" +#include "base/json/json_reader.h" +#include "base/json/json_writer.h" +#include "base/string_piece.h" +#include "base/utf_string_conversions.h" +#include "base/values.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/constrained_window.h" +#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" +#include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" +#include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" +#include "chrome/browser/ui/webui/constrained_html_ui.h" +#include "chrome/browser/ui/webui/html_dialog_ui.h" +#include "chrome/common/jstemplate_builder.h" +#include "chrome/common/url_constants.h" +#include "content/browser/tab_contents/tab_contents.h" +#include "grit/browser_resources.h" +#include "grit/generated_resources.h" +#include "ui/base/l10n/l10n_util.h" +#include "ui/base/resource/resource_bundle.h" +#include "ui/gfx/size.h" + +using std::string; + +namespace browser { + +// Declared in browser_dialogs.h so others don't have to depend on our header. +void ShowTabModalConfirmDialog(TabModalConfirmDialogDelegate* delegate, + TabContents* tab_contents) { + new TabModalConfirmDialogUI(delegate, tab_contents); +} + +} // namespace browser + +class TabModalConfirmDialogHtmlDelegate : public HtmlDialogUIDelegate { + public: + TabModalConfirmDialogHtmlDelegate( + TabModalConfirmDialogUI* ui, + TabModalConfirmDialogDelegate* dialog_delegate) + : ui_(ui), + dialog_delegate_(dialog_delegate) {} + + virtual ~TabModalConfirmDialogHtmlDelegate() {} + + // HtmlDialogUIDelegate implementation. + virtual bool IsDialogModal() const OVERRIDE { + return true; + } + + virtual string16 GetDialogTitle() const OVERRIDE { + return dialog_delegate_->GetTitle(); + } + + virtual GURL GetDialogContentURL() const OVERRIDE { + return GURL(chrome::kChromeUITabModalConfirmDialogURL); + } + + virtual void GetWebUIMessageHandlers( + std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {} + + virtual void GetDialogSize(gfx::Size* size) const OVERRIDE { + size->SetSize(kDialogWidth, kDialogHeight); + } + + virtual std::string GetDialogArgs() const OVERRIDE { + DictionaryValue dict; + dict.SetString("message", dialog_delegate_->GetMessage()); + dict.SetString("accept", dialog_delegate_->GetAcceptButtonTitle()); + dict.SetString("cancel", dialog_delegate_->GetCancelButtonTitle()); + ChromeWebUIDataSource::SetFontAndTextDirection(&dict); + std::string json; + base::JSONWriter::Write(&dict, false, &json); + return json; + } + + virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { + bool accepted = false; + if (!json_retval.empty()) { + base::JSONReader reader; + scoped_ptr<Value> value(reader.JsonToValue(json_retval, false, false)); + DCHECK(value.get() && value->GetAsBoolean(&accepted)) + << "Missing or unreadable response from dialog"; + } + + ui_->OnDialogClosed(accepted); + delete this; + } + + virtual void OnCloseContents(TabContents* source, + bool* out_close_dialog) OVERRIDE {} + + virtual bool ShouldShowDialogTitle() const OVERRIDE { + return true; + } + + private: + static const int kDialogWidth = 400; + static const int kDialogHeight = 120; + + scoped_ptr<TabModalConfirmDialogUI> ui_; + // Owned by TabModalConfirmDialogUI, which we own. + TabModalConfirmDialogDelegate* dialog_delegate_; + + DISALLOW_COPY_AND_ASSIGN(TabModalConfirmDialogHtmlDelegate); +}; + +TabModalConfirmDialogUI::TabModalConfirmDialogUI( + TabModalConfirmDialogDelegate* delegate, + TabContents* tab_contents) + : delegate_(delegate) { + TabContentsWrapper* wrapper = + TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); + Profile* profile = wrapper->profile(); + ChromeWebUIDataSource* data_source = + new ChromeWebUIDataSource(chrome::kChromeUITabModalConfirmDialogHost); + data_source->set_default_resource(IDR_TAB_MODAL_CONFIRM_DIALOG_HTML); + profile->GetChromeURLDataManager()->AddDataSource(data_source); + + TabModalConfirmDialogHtmlDelegate* html_delegate = + new TabModalConfirmDialogHtmlDelegate(this, delegate); + ConstrainedHtmlUIDelegate* dialog_delegate = + ConstrainedHtmlUI::CreateConstrainedHtmlDialog(profile, html_delegate, + wrapper); + delegate_->set_window(dialog_delegate->window()); +} + +TabModalConfirmDialogUI::~TabModalConfirmDialogUI() {} + +void TabModalConfirmDialogUI::OnDialogClosed(bool accepted) { + delegate_->set_window(NULL); + if (accepted) + delegate_->Accept(); + else + delegate_->Cancel(); +} diff --git a/chrome/browser/ui/webui/tab_modal_confirm_dialog_webui.h b/chrome/browser/ui/webui/tab_modal_confirm_dialog_webui.h new file mode 100644 index 0000000..93dc7a6 --- /dev/null +++ b/chrome/browser/ui/webui/tab_modal_confirm_dialog_webui.h @@ -0,0 +1,40 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_WEBUI_TAB_MODAL_CONFIRM_DIALOG_WEBUI_H_ +#define CHROME_BROWSER_UI_WEBUI_TAB_MODAL_CONFIRM_DIALOG_WEBUI_H_ +#pragma once + +#if !(defined(USE_AURA) || defined(TOOLKIT_VIEWS)) +#error Tab-modal confirm dialog should be shown with native UI. +#endif + +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" + +class TabContents; +class TabModalConfirmDialogDelegate; + +// Displays a tab-modal dialog, i.e. a dialog that will block the current page +// but still allow the user to switch to a different page. +// To display the dialog, allocate this object on the heap. It will open the +// dialog from its constructor and then delete itself when the user dismisses +// the dialog. +class TabModalConfirmDialogUI { + public: + TabModalConfirmDialogUI(TabModalConfirmDialogDelegate* delegate, + TabContents* tab_contents); + ~TabModalConfirmDialogUI(); + + // Invoked when the dialog is closed. Notifies the controller of the user's + // response. + void OnDialogClosed(bool accept); + + private: + scoped_ptr<TabModalConfirmDialogDelegate> delegate_; + + DISALLOW_COPY_AND_ASSIGN(TabModalConfirmDialogUI); +}; + +#endif // CHROME_BROWSER_UI_WEBUI_TAB_MODAL_CONFIRM_DIALOG_WEBUI_H_ diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 7e46c902..6c39ffb 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -2837,8 +2837,6 @@ 'browser/ui/cocoa/presentation_mode_prefs.mm', 'browser/ui/cocoa/profile_menu_controller.h', 'browser/ui/cocoa/profile_menu_controller.mm', - 'browser/ui/cocoa/repost_form_warning_mac.h', - 'browser/ui/cocoa/repost_form_warning_mac.mm', 'browser/ui/cocoa/restart_browser.h', 'browser/ui/cocoa/restart_browser.mm', 'browser/ui/cocoa/search_engine_dialog_controller.h', @@ -2870,6 +2868,8 @@ 'browser/ui/cocoa/tab_contents/sad_tab_view.mm', 'browser/ui/cocoa/tab_contents/tab_contents_controller.h', 'browser/ui/cocoa/tab_contents/tab_contents_controller.mm', + 'browser/ui/cocoa/tab_modal_confirm_dialog_mac.h', + 'browser/ui/cocoa/tab_modal_confirm_dialog_mac.mm', 'browser/ui/cocoa/tab_view_picker_table.h', 'browser/ui/cocoa/tab_view_picker_table.mm', 'browser/ui/cocoa/table_row_nsimage_cache.h', @@ -3141,8 +3141,6 @@ 'browser/ui/gtk/process_singleton_dialog.h', 'browser/ui/gtk/reload_button_gtk.cc', 'browser/ui/gtk/reload_button_gtk.h', - 'browser/ui/gtk/repost_form_warning_gtk.cc', - 'browser/ui/gtk/repost_form_warning_gtk.h', 'browser/ui/gtk/rounded_window.cc', 'browser/ui/gtk/rounded_window.h', 'browser/ui/gtk/sad_tab_gtk.cc', @@ -3163,6 +3161,8 @@ 'browser/ui/gtk/status_icons/status_tray_gtk.h', 'browser/ui/gtk/tab_contents_container_gtk.cc', 'browser/ui/gtk/tab_contents_container_gtk.h', + 'browser/ui/gtk/tab_modal_confirm_dialog_gtk.cc', + 'browser/ui/gtk/tab_modal_confirm_dialog_gtk.h', 'browser/ui/gtk/tabs/drag_data.cc', 'browser/ui/gtk/tabs/drag_data.h', 'browser/ui/gtk/tabs/dragged_tab_controller_gtk.cc', @@ -3292,6 +3292,8 @@ 'browser/ui/tab_contents/core_tab_helper_delegate.h', 'browser/ui/tab_contents/tab_contents_wrapper.cc', 'browser/ui/tab_contents/tab_contents_wrapper.h', + 'browser/ui/tab_modal_confirm_dialog_delegate.cc', + 'browser/ui/tab_modal_confirm_dialog_delegate.h', 'browser/ui/tabs/dock_info.cc', 'browser/ui/tabs/dock_info.h', 'browser/ui/tabs/dock_info_aura.cc', @@ -3579,8 +3581,6 @@ 'browser/ui/views/page_info_bubble_view.h', 'browser/ui/views/reload_button.cc', 'browser/ui/views/reload_button.h', - 'browser/ui/views/repost_form_warning_view.cc', - 'browser/ui/views/repost_form_warning_view.h', 'browser/ui/views/restart_message_box.cc', 'browser/ui/views/restart_message_box.h', 'browser/ui/views/sad_tab_view.cc', @@ -3631,6 +3631,8 @@ 'browser/ui/views/tab_contents/tab_contents_view_views.h', 'browser/ui/views/tab_icon_view.cc', 'browser/ui/views/tab_icon_view.h', + 'browser/ui/views/tab_modal_confirm_dialog_views.cc', + 'browser/ui/views/tab_modal_confirm_dialog_views.h', 'browser/ui/views/tabs/abstract_tab_strip_view.h', 'browser/ui/views/tabs/base_tab.cc', 'browser/ui/views/tabs/base_tab.h', @@ -4040,8 +4042,6 @@ 'browser/ui/webui/quota_internals_types.h', 'browser/ui/webui/quota_internals_ui.cc', 'browser/ui/webui/quota_internals_ui.h', - 'browser/ui/webui/repost_form_warning_ui.cc', - 'browser/ui/webui/repost_form_warning_ui.h', 'browser/ui/webui/screenshot_source.cc', 'browser/ui/webui/screenshot_source.h', 'browser/ui/webui/sessions_ui.cc', @@ -4062,6 +4062,8 @@ 'browser/ui/webui/sync_setup_handler2.h', 'browser/ui/webui/sync_setup_handler.cc', 'browser/ui/webui/sync_setup_handler.h', + 'browser/ui/webui/tab_modal_confirm_dialog_webui.cc', + 'browser/ui/webui/tab_modal_confirm_dialog_webui.h', 'browser/ui/webui/task_manager_dialog.cc', 'browser/ui/webui/task_manager_dialog.h', 'browser/ui/webui/task_manager_handler.cc', @@ -4261,8 +4263,8 @@ 'browser/platform_util_linux.cc', 'browser/printing/print_dialog_gtk.cc', 'browser/printing/print_dialog_gtk.h', - 'browser/ui/gtk/repost_form_warning_gtk.cc', - 'browser/ui/gtk/repost_form_warning_gtk.h', + 'browser/ui/gtk/tab_modal_confirm_dialog_gtk.cc', + 'browser/ui/gtk/tab_modal_confirm_dialog_gtk.h', 'browser/upgrade_detector_impl.cc', 'browser/upgrade_detector_impl.h', ], @@ -4705,8 +4707,8 @@ 'browser/ui/webui/collected_cookies_ui_delegate.h', 'browser/ui/webui/cookies_tree_model_adapter.cc', 'browser/ui/webui/cookies_tree_model_adapter.h', - 'browser/ui/webui/repost_form_warning_ui.cc', - 'browser/ui/webui/repost_form_warning_ui.h', + 'browser/ui/webui/tab_modal_confirm_dialog_webui.cc', + 'browser/ui/webui/tab_modal_confirm_dialog_webui.h', ], 'conditions': [ ['win_use_allocator_shim==1', { @@ -4809,9 +4811,9 @@ ['include', '^browser/ui/gtk/nine_box.h'], ['include', '^browser/ui/gtk/omnibox/omnibox_view_gtk.cc'], ['include', '^browser/ui/gtk/omnibox/omnibox_view_gtk.h'], - ['include', '^browser/ui/gtk/repost_form_warning_gtk.cc'], - ['include', '^browser/ui/gtk/repost_form_warning_gtk.h'], ['include', '^browser/ui/gtk/ssl_client_certificate_selector.cc'], + ['include', '^browser/ui/gtk/tab_modal_confirm_dialog_gtk.cc'], + ['include', '^browser/ui/gtk/tab_modal_confirm_dialog_gtk.h'], ['include', '^browser/ui/gtk/task_manager_gtk.cc'], ['include', '^browser/ui/gtk/task_manager_gtk.h'], ['include', '^browser/ui/gtk/view_id_util.cc'], @@ -5036,12 +5038,12 @@ ['include', '^browser/ui/views/window.h'], ['include', '^browser/ui/views/wrench_menu.cc'], ['include', '^browser/ui/views/wrench_menu.h'], - ['include', '^browser/ui/webui/repost_form_warning_ui.cc'], - ['include', '^browser/ui/webui/repost_form_warning_ui.h'], ['include', '^browser/ui/webui/collected_cookies_ui_delegate.cc'], ['include', '^browser/ui/webui/collected_cookies_ui_delegate.h'], ['include', '^browser/ui/webui/cookies_tree_model_adapter.cc'], ['include', '^browser/ui/webui/cookies_tree_model_adapter.h'], + ['include', '^browser/ui/webui/tab_modal_confirm_dialog_webui.cc'], + ['include', '^browser/ui/webui/tab_modal_confirm_dialog_webui.h'], ['include', '^browser/ui/window_sizer.cc'], ['include', '^browser/ui/window_sizer.h'], @@ -5071,8 +5073,8 @@ ['exclude', '^browser/ui/webui/collected_cookies_ui_delegate.h'], ['exclude', '^browser/ui/webui/cookies_tree_model_adapter.cc'], ['exclude', '^browser/ui/webui/cookies_tree_model_adapter.h'], - ['exclude', '^browser/ui/webui/repost_form_warning_ui.cc'], - ['exclude', '^browser/ui/webui/repost_form_warning_ui.h'], + ['exclude', '^browser/ui/webui/tab_modal_confirm_dialog_webui.cc'], + ['exclude', '^browser/ui/webui/tab_modal_confirm_dialog_webui.h'], ] }], # Exclude these toolkit_views specific files again. diff --git a/chrome/common/url_constants.cc b/chrome/common/url_constants.cc index 8ef0d66..be45ba8 100644 --- a/chrome/common/url_constants.cc +++ b/chrome/common/url_constants.cc @@ -106,7 +106,8 @@ const char kChromeUIFileManagerURL[] = "chrome://files/"; #if (defined(OS_LINUX) && defined(TOOLKIT_VIEWS)) || defined(USE_AURA) const char kChromeUICollectedCookiesURL[] = "chrome://collected-cookies/"; const char kChromeUIHttpAuthURL[] = "chrome://http-auth/"; -const char kChromeUIRepostFormWarningURL[] = "chrome://repost-form-warning/"; +const char kChromeUITabModalConfirmDialogURL[] = + "chrome://tab-modal-confirm-dialog/"; #endif #if defined(USE_AURA) @@ -237,7 +238,7 @@ const char kChromeUIFileManagerHost[] = "files"; #if (defined(OS_LINUX) && defined(TOOLKIT_VIEWS)) || defined(USE_AURA) const char kChromeUICollectedCookiesHost[] = "collected-cookies"; const char kChromeUIHttpAuthHost[] = "http-auth"; -const char kChromeUIRepostFormWarningHost[] = "repost-form-warning"; +const char kChromeUITabModalConfirmDialogHost[] = "tab-modal-confirm-dialog"; #endif #if defined(USE_AURA) diff --git a/chrome/common/url_constants.h b/chrome/common/url_constants.h index 9b710c2..cdc4d87 100644 --- a/chrome/common/url_constants.h +++ b/chrome/common/url_constants.h @@ -98,7 +98,7 @@ extern const char kChromeUIFileManagerURL[]; #if (defined(OS_LINUX) && defined(TOOLKIT_VIEWS)) || defined(USE_AURA) extern const char kChromeUICollectedCookiesURL[]; extern const char kChromeUIHttpAuthURL[]; -extern const char kChromeUIRepostFormWarningURL[]; +extern const char kChromeUITabModalConfirmDialogURL[]; #endif #if defined(USE_AURA) @@ -227,7 +227,7 @@ extern const char kChromeUIFileManagerHost[]; #if (defined(OS_LINUX) && defined(TOOLKIT_VIEWS)) || defined(USE_AURA) extern const char kChromeUICollectedCookiesHost[]; extern const char kChromeUIHttpAuthHost[]; -extern const char kChromeUIRepostFormWarningHost[]; +extern const char kChromeUITabModalConfirmDialogHost[]; #endif #if defined(USE_AURA) diff --git a/chrome/test/base/test_browser_window.h b/chrome/test/base/test_browser_window.h index 2708ff9..da5b01d 100644 --- a/chrome/test/base/test_browser_window.h +++ b/chrome/test/base/test_browser_window.h @@ -90,8 +90,6 @@ class TestBrowserWindow : public BrowserWindow { bool already_bookmarked) OVERRIDE {} virtual bool IsDownloadShelfVisible() const OVERRIDE; virtual DownloadShelf* GetDownloadShelf() OVERRIDE; - virtual void ShowRepostFormWarningDialog( - TabContents* tab_contents) OVERRIDE {} virtual void ShowCollectedCookiesDialog( TabContentsWrapper* wrapper) OVERRIDE {} virtual void ConfirmBrowserCloseWithPendingDownloads() OVERRIDE {} |