Home | History | Annotate | Download | only in ui
      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() or Close() has been made before.
     47   // This method is safe to call even from an OnAccepted(), OnCanceled(),
     48   // OnClosed() or OnLinkClicked() callback.
     49   void Accept();
     50 
     51   // Cancels the confirmation prompt and calls OnCanceled() if no other call
     52   // to Accept(), Cancel() or Close() has been made before.
     53   // This method is safe to call even from an OnAccepted(), OnCanceled(),
     54   // OnClosed() or OnLinkClicked() callback.
     55   void Cancel();
     56 
     57   // Called when the dialog is closed without selecting an option, e.g. by
     58   // pressing the close button on the dialog, using a window manager gesture,
     59   // closing the parent tab or navigating in the parent tab.
     60   // Calls OnClosed() and closes the dialog if no other call to Accept(),
     61   // Cancel() or Close() has been made before.
     62   // This method is safe to call even from an OnAccepted(), OnCanceled(),
     63   // OnClosed() or OnLinkClicked() callback.
     64   void Close();
     65 
     66   // Called when the link is clicked. Calls OnLinkClicked() if the dialog is
     67   // not in the process of closing. The |disposition| specifies how the
     68   // resulting document should be loaded (based on the event flags present when
     69   // the link was clicked).
     70   void LinkClicked(WindowOpenDisposition disposition);
     71 
     72   // The title of the dialog. Note that the title is not shown on all platforms.
     73   virtual base::string16 GetTitle() = 0;
     74   virtual base::string16 GetDialogMessage() = 0;
     75 
     76   // Icon to show for the dialog. If this method is not overridden, a default
     77   // icon (like the application icon) is shown.
     78   virtual gfx::Image* GetIcon();
     79 
     80   // Title for the accept and the cancel buttons.
     81   // The default implementation uses IDS_OK and IDS_CANCEL.
     82   virtual base::string16 GetAcceptButtonTitle();
     83   virtual base::string16 GetCancelButtonTitle();
     84 
     85   // Returns the text of the link to be displayed, if any. Otherwise returns
     86   // an empty string.
     87   virtual base::string16 GetLinkText() const;
     88 
     89   // GTK stock icon names for the accept and cancel buttons, respectively.
     90   // The icons are only used on GTK. If these methods are not overriden,
     91   // the buttons have no stock icons.
     92   virtual const char* GetAcceptButtonIcon();
     93   virtual const char* GetCancelButtonIcon();
     94 
     95  protected:
     96   TabModalConfirmDialogCloseDelegate* close_delegate() {
     97     return close_delegate_;
     98   }
     99 
    100   // content::NotificationObserver implementation.
    101   // Watch for a new load or a closed tab and dismiss the dialog if they occur.
    102   virtual void Observe(int type,
    103                        const content::NotificationSource& source,
    104                        const content::NotificationDetails& details) OVERRIDE;
    105 
    106   content::NotificationRegistrar registrar_;
    107 
    108  private:
    109   // It is guaranteed that exactly one of OnAccepted(), OnCanceled() or
    110   // OnClosed() is eventually called. These method are private to enforce this
    111   // guarantee. Access to them is controlled by Accept(), Cancel() and Close().
    112 
    113   // Called when the user accepts or cancels the dialog, respectively.
    114   virtual void OnAccepted();
    115   virtual void OnCanceled();
    116 
    117   // Called when the dialog is closed.
    118   virtual void OnClosed();
    119 
    120   // Called when the link is clicked. Acces to the method is controlled by
    121   // LinkClicked(), which checks that the dialog is not in the process of
    122   // closing. It's correct to close the dialog by calling Accept(), Cancel()
    123   // or Close() from this callback.
    124   virtual void OnLinkClicked(WindowOpenDisposition disposition);
    125 
    126   // Close the dialog.
    127   void CloseDialog();
    128 
    129   TabModalConfirmDialogCloseDelegate* close_delegate_;
    130 
    131   // True iff we are in the process of closing, to avoid running callbacks
    132   // multiple times.
    133   bool closing_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(TabModalConfirmDialogDelegate);
    136 };
    137 
    138 #endif  // CHROME_BROWSER_UI_TAB_MODAL_CONFIRM_DIALOG_DELEGATE_H_
    139