tab_modal_confirm_dialog_delegate.h revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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
15namespace content {
16class WebContents;
17}
18
19namespace gfx {
20class Image;
21}
22
23class 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.
36class 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|.
46  // This method is safe to call even from an |OnAccepted| or |OnCanceled|
47  // callback.
48  void Accept();
49
50  // Cancels the confirmation prompt and calls |OnCanceled|.
51  // This method is safe to call even from an |OnAccepted| or |OnCanceled|
52  // callback.
53  void Cancel();
54
55  // Called when the link (if any) is clicked. Calls |OnLinkClicked| and closes
56  // the dialog. The |disposition| specifies how the resulting document should
57  // be loaded (based on the event flags present when the link was clicked).
58  void LinkClicked(WindowOpenDisposition disposition);
59
60  // The title of the dialog. Note that the title is not shown on all platforms.
61  virtual string16 GetTitle() = 0;
62  virtual string16 GetMessage() = 0;
63
64  // Icon to show for the dialog. If this method is not overridden, a default
65  // icon (like the application icon) is shown.
66  virtual gfx::Image* GetIcon();
67
68  // Title for the accept and the cancel buttons.
69  // The default implementation uses IDS_OK and IDS_CANCEL.
70  virtual string16 GetAcceptButtonTitle();
71  virtual string16 GetCancelButtonTitle();
72
73  // Returns the text of the link to be displayed, if any. Otherwise returns
74  // an empty string.
75  virtual string16 GetLinkText() const;
76
77  // GTK stock icon names for the accept and cancel buttons, respectively.
78  // The icons are only used on GTK. If these methods are not overriden,
79  // the buttons have no stock icons.
80  virtual const char* GetAcceptButtonIcon();
81  virtual const char* GetCancelButtonIcon();
82
83 protected:
84  TabModalConfirmDialogCloseDelegate* close_delegate() {
85    return close_delegate_;
86  }
87
88  // content::NotificationObserver implementation.
89  // Watch for a new load or a closed tab and dismiss the dialog if they occur.
90  virtual void Observe(int type,
91                       const content::NotificationSource& source,
92                       const content::NotificationDetails& details) OVERRIDE;
93
94  content::NotificationRegistrar registrar_;
95
96 private:
97  // It is guaranteed that exactly one of |OnAccepted|, |OnCanceled| or
98  // |OnLinkClicked| is eventually called. These method are private to
99  // enforce this guarantee. Access to them is controlled by |Accept|,
100  // |Cancel| and |LinkClicked|.
101
102  // Called when the user accepts or cancels the dialog, respectively.
103  virtual void OnAccepted();
104  virtual void OnCanceled();
105
106  // Called when the user clicks on the link (if any).
107  virtual void OnLinkClicked(WindowOpenDisposition disposition);
108
109  // Close the dialog.
110  void CloseDialog();
111
112  TabModalConfirmDialogCloseDelegate* close_delegate_;
113  // True iff we are in the process of closing, to avoid running callbacks
114  // multiple times.
115  bool closing_;
116
117  DISALLOW_COPY_AND_ASSIGN(TabModalConfirmDialogDelegate);
118};
119
120#endif  // CHROME_BROWSER_UI_TAB_MODAL_CONFIRM_DIALOG_DELEGATE_H_
121