1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_UI_TAB_MODAL_CONFIRM_DIALOG_DELEGATE_H_ 6 #define CHROME_BROWSER_UI_TAB_MODAL_CONFIRM_DIALOG_DELEGATE_H_ 7 8 #include "base/callback.h" 9 #include "base/compiler_specific.h" 10 #include "base/strings/string16.h" 11 #include "content/public/browser/notification_observer.h" 12 #include "content/public/browser/notification_registrar.h" 13 #include "ui/base/window_open_disposition.h" 14 15 namespace content { 16 class WebContents; 17 } 18 19 namespace gfx { 20 class Image; 21 } 22 23 class TabModalConfirmDialogCloseDelegate { 24 public: 25 TabModalConfirmDialogCloseDelegate() {} 26 virtual ~TabModalConfirmDialogCloseDelegate() {} 27 28 virtual void CloseDialog() = 0; 29 30 private: 31 DISALLOW_COPY_AND_ASSIGN(TabModalConfirmDialogCloseDelegate); 32 }; 33 34 // This class acts as the delegate for a simple tab-modal dialog confirming 35 // whether the user wants to execute a certain action. 36 class TabModalConfirmDialogDelegate : public content::NotificationObserver { 37 public: 38 explicit TabModalConfirmDialogDelegate(content::WebContents* web_contents); 39 virtual ~TabModalConfirmDialogDelegate(); 40 41 void set_close_delegate(TabModalConfirmDialogCloseDelegate* close_delegate) { 42 close_delegate_ = close_delegate; 43 } 44 45 // Accepts the confirmation prompt and calls |OnAccepted| if no other call 46 // to |Accept|, |Cancel|, |LinkClicked| or |Close| has been made before. 47 // This method is safe to call even from an |OnAccepted| or |OnCanceled| 48 // callback. 49 void Accept(); 50 51 // Cancels the confirmation prompt and calls |OnCanceled| if no other call 52 // to |Accept|, |Cancel|, |LinkClicked| or |Close| has been made before. 53 // This method is safe to call even from an |OnAccepted| or |OnCanceled| 54 // callback. 55 void Cancel(); 56 57 // Called when the link (if any) is clicked. Calls |OnLinkClicked| and closes 58 // the dialog if no other call to |Accept|, |Cancel|, |LinkClicked| or 59 // |Close| has been made before. The |disposition| specifies how the 60 // resulting document should be loaded (based on the event flags present when 61 // the link was clicked). 62 void LinkClicked(WindowOpenDisposition disposition); 63 64 // Called when the dialog is closed without selecting an option, e.g. by 65 // pressing the close button on the dialog, using a window manager gesture, 66 // closing the parent tab or navigating in the parent tab. 67 // Calls |OnClosed| and closes the dialog if no other call to |Accept|, 68 // |Cancel|, |LinkClicked| or |Close| has been made before. 69 void Close(); 70 71 // The title of the dialog. Note that the title is not shown on all platforms. 72 virtual string16 GetTitle() = 0; 73 virtual string16 GetMessage() = 0; 74 75 // Icon to show for the dialog. If this method is not overridden, a default 76 // icon (like the application icon) is shown. 77 virtual gfx::Image* GetIcon(); 78 79 // Title for the accept and the cancel buttons. 80 // The default implementation uses IDS_OK and IDS_CANCEL. 81 virtual string16 GetAcceptButtonTitle(); 82 virtual string16 GetCancelButtonTitle(); 83 84 // Returns the text of the link to be displayed, if any. Otherwise returns 85 // an empty string. 86 virtual string16 GetLinkText() const; 87 88 // GTK stock icon names for the accept and cancel buttons, respectively. 89 // The icons are only used on GTK. If these methods are not overriden, 90 // the buttons have no stock icons. 91 virtual const char* GetAcceptButtonIcon(); 92 virtual const char* GetCancelButtonIcon(); 93 94 protected: 95 TabModalConfirmDialogCloseDelegate* close_delegate() { 96 return close_delegate_; 97 } 98 99 // content::NotificationObserver implementation. 100 // Watch for a new load or a closed tab and dismiss the dialog if they occur. 101 virtual void Observe(int type, 102 const content::NotificationSource& source, 103 const content::NotificationDetails& details) OVERRIDE; 104 105 content::NotificationRegistrar registrar_; 106 107 private: 108 // It is guaranteed that exactly one of the |On...| methods is eventually 109 // called. These method are private to enforce this guarantee. Access to them 110 // is controlled by |Accept|, |Cancel|, |LinkClicked| and |Close|. 111 112 // Called when the user accepts or cancels the dialog, respectively. 113 virtual void OnAccepted(); 114 virtual void OnCanceled(); 115 116 // Called when the user clicks on the link (if any). 117 virtual void OnLinkClicked(WindowOpenDisposition disposition); 118 119 // Called when the dialog is closed. 120 virtual void OnClosed(); 121 122 // Close the dialog. 123 void CloseDialog(); 124 125 TabModalConfirmDialogCloseDelegate* close_delegate_; 126 // True iff we are in the process of closing, to avoid running callbacks 127 // multiple times. 128 bool closing_; 129 130 DISALLOW_COPY_AND_ASSIGN(TabModalConfirmDialogDelegate); 131 }; 132 133 #endif // CHROME_BROWSER_UI_TAB_MODAL_CONFIRM_DIALOG_DELEGATE_H_ 134