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_INFOBARS_INFOBAR_DELEGATE_H_
6#define CHROME_BROWSER_INFOBARS_INFOBAR_DELEGATE_H_
7
8#include "base/basictypes.h"
9#include "base/strings/string16.h"
10#include "chrome/browser/infobars/infobar_service.h"
11#include "ui/base/window_open_disposition.h"
12
13class AutoLoginInfoBarDelegate;
14class ConfirmInfoBarDelegate;
15class ExtensionInfoBarDelegate;
16class InfoBar;
17class InsecureContentInfoBarDelegate;
18class MediaStreamInfoBarDelegate;
19class RegisterProtocolHandlerInfoBarDelegate;
20class ScreenCaptureInfoBarDelegate;
21class ThemeInstalledInfoBarDelegate;
22class ThreeDAPIInfoBarDelegate;
23class TranslateInfoBarDelegate;
24
25namespace gfx {
26class Image;
27}
28
29// An interface implemented by objects wishing to control an InfoBar.
30// Implementing this interface is not sufficient to use an InfoBar, since it
31// does not map to a specific InfoBar type. Instead, you must implement
32// ConfirmInfoBarDelegate, or override with your own delegate for your own
33// InfoBar variety.
34class InfoBarDelegate {
35 public:
36  // The type of the infobar. It controls its appearance, such as its background
37  // color.
38  enum Type {
39    WARNING_TYPE,
40    PAGE_ACTION_TYPE,
41  };
42
43  enum InfoBarAutomationType {
44    CONFIRM_INFOBAR,
45    PASSWORD_INFOBAR,
46    RPH_INFOBAR,
47    UNKNOWN_INFOBAR,
48  };
49
50  // Value to use when the InfoBar has no icon to show.
51  static const int kNoIconID;
52
53  virtual ~InfoBarDelegate();
54
55  virtual InfoBarAutomationType GetInfoBarAutomationType() const;
56
57  // Called to create the InfoBar. Implementation of this method is
58  // platform-specific.
59  virtual InfoBar* CreateInfoBar(InfoBarService* owner) = 0;
60
61  // TODO(pkasting): Move to InfoBar once InfoBars own their delegates.
62  InfoBarService* owner() { return owner_; }
63
64  void clear_owner() { owner_ = NULL; }
65
66  // Returns true if the supplied |delegate| is equal to this one. Equality is
67  // left to the implementation to define. This function is called by the
68  // InfoBarService when determining whether or not a delegate should be
69  // added because a matching one already exists. If this function returns true,
70  // the InfoBarService will not add the new delegate because it considers
71  // one to already be present.
72  virtual bool EqualsDelegate(InfoBarDelegate* delegate) const;
73
74  // Returns true if the InfoBar should be closed automatically after the page
75  // is navigated. By default this returns true if the navigation is to a new
76  // page (not including reloads).  Subclasses wishing to change this behavior
77  // can override either this function or ShouldExpireInternal(), depending on
78  // what level of control they need.
79  virtual bool ShouldExpire(const content::LoadCommittedDetails& details) const;
80
81  // Called when the user clicks on the close button to dismiss the infobar.
82  virtual void InfoBarDismissed();
83
84  // Return the resource ID of the icon to be shown for this InfoBar.  If the
85  // value is equal to |kNoIconID|, no icon is shown.
86  virtual int GetIconID() const;
87
88  // Returns the type of the infobar.  The type determines the appearance (such
89  // as background color) of the infobar.
90  virtual Type GetInfoBarType() const;
91
92  // Type-checking downcast routines:
93  virtual AutoLoginInfoBarDelegate* AsAutoLoginInfoBarDelegate();
94  virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate();
95  virtual ExtensionInfoBarDelegate* AsExtensionInfoBarDelegate();
96  virtual InsecureContentInfoBarDelegate* AsInsecureContentInfoBarDelegate();
97  virtual MediaStreamInfoBarDelegate* AsMediaStreamInfoBarDelegate();
98  virtual RegisterProtocolHandlerInfoBarDelegate*
99      AsRegisterProtocolHandlerInfoBarDelegate();
100  virtual ScreenCaptureInfoBarDelegate* AsScreenCaptureInfoBarDelegate();
101  virtual ThemeInstalledInfoBarDelegate* AsThemePreviewInfobarDelegate();
102  virtual ThreeDAPIInfoBarDelegate* AsThreeDAPIInfoBarDelegate();
103  virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate();
104
105  // Return the icon to be shown for this InfoBar. If the returned Image is
106  // empty, no icon is shown.
107  virtual gfx::Image GetIcon() const;
108
109  content::WebContents* web_contents() {
110    return owner_ ? owner_->web_contents() : NULL;
111  }
112
113 protected:
114  // If |contents| is non-NULL, its active entry's unique ID will be stored
115  // using StoreActiveEntryUniqueID automatically.
116  explicit InfoBarDelegate(InfoBarService* owner);
117
118  // Store the unique id for the active entry in our WebContents, to be used
119  // later upon navigation to determine if this InfoBarDelegate should be
120  // expired.
121  void StoreActiveEntryUniqueID();
122
123  int contents_unique_id() const { return contents_unique_id_; }
124
125  // Returns true if the navigation is to a new URL or a reload occured.
126  virtual bool ShouldExpireInternal(
127      const content::LoadCommittedDetails& details) const;
128
129  // Removes ourself from |owner_| if we haven't already been removed.
130  // TODO(pkasting): Move to InfoBar.
131  void RemoveSelf();
132
133 private:
134  // The unique id of the active NavigationEntry of the WebContents that we were
135  // opened for. Used to help expire on navigations.
136  int contents_unique_id_;
137
138  // TODO(pkasting): Remove.
139  InfoBarService* owner_;
140
141  DISALLOW_COPY_AND_ASSIGN(InfoBarDelegate);
142};
143
144#endif  // CHROME_BROWSER_INFOBARS_INFOBAR_DELEGATE_H_
145