1// Copyright (c) 2011 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_TAB_CONTENTS_INFOBAR_DELEGATE_H_
6#define CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/string16.h"
11#include "content/browser/tab_contents/navigation_controller.h"
12#include "webkit/glue/window_open_disposition.h"
13
14class ConfirmInfoBarDelegate;
15class ExtensionInfoBarDelegate;
16class InfoBar;
17class LinkInfoBarDelegate;
18class PluginInstallerInfoBarDelegate;
19class SkBitmap;
20class ThemeInstalledInfoBarDelegate;
21class TranslateInfoBarDelegate;
22
23// An interface implemented by objects wishing to control an InfoBar.
24// Implementing this interface is not sufficient to use an InfoBar, since it
25// does not map to a specific InfoBar type. Instead, you must implement either
26// LinkInfoBarDelegate or ConfirmInfoBarDelegate, or override with your own
27// delegate for your own InfoBar variety.
28//
29// --- WARNING ---
30// When creating your InfoBarDelegate subclass, it is recommended that you
31// design it such that you instantiate a brand new delegate for every call to
32// AddInfoBar, rather than re-using/sharing a delegate object. Otherwise,
33// you need to consider the fact that more than one InfoBar instance can exist
34// and reference the same delegate -- even though it is also true that we only
35// ever fully show one infobar (they don't stack). The dual-references occur
36// because a second InfoBar can be added while the first one is in the process
37// of closing (the animations). This can cause problems because when the first
38// one does finally fully close InfoBarDelegate::InfoBarClosed() is called,
39// and the delegate is free to clean itself up or reset state, which may have
40// fatal consequences for the InfoBar that was in the process of opening (or is
41// now fully opened) -- it is referencing a delegate that may not even exist
42// anymore.
43// As such, it is generally much safer to dedicate a delegate instance to
44// AddInfoBar!
45class InfoBarDelegate {
46 public:
47  // The type of the infobar. It controls its appearance, such as its background
48  // color.
49  enum Type {
50    WARNING_TYPE,
51    PAGE_ACTION_TYPE,
52  };
53
54  virtual ~InfoBarDelegate();
55
56  // Called to create the InfoBar. Implementation of this method is
57  // platform-specific.
58  virtual InfoBar* CreateInfoBar() = 0;
59
60  // Returns true if the supplied |delegate| is equal to this one. Equality is
61  // left to the implementation to define. This function is called by the
62  // TabContents when determining whether or not a delegate should be added
63  // because a matching one already exists. If this function returns true, the
64  // TabContents will not add the new delegate because it considers one to
65  // already be present.
66  virtual bool EqualsDelegate(InfoBarDelegate* delegate) const;
67
68  // Returns true if the InfoBar should be closed automatically after the page
69  // is navigated. The default behavior is to return true if the page is
70  // navigated somewhere else or reloaded.
71  virtual bool ShouldExpire(
72      const NavigationController::LoadCommittedDetails& details) const;
73
74  // Called when the user clicks on the close button to dismiss the infobar.
75  virtual void InfoBarDismissed();
76
77  // Called after the InfoBar is closed. The delegate is free to delete itself
78  // at this point.
79  virtual void InfoBarClosed();
80
81  // Return the icon to be shown for this InfoBar. If the returned bitmap is
82  // NULL, no icon is shown.
83  virtual SkBitmap* GetIcon() const;
84
85  // Returns the type of the infobar.  The type determines the appearance (such
86  // as background color) of the infobar.
87  virtual Type GetInfoBarType() const;
88
89  // Type-checking downcast routines:
90  virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate();
91  virtual ExtensionInfoBarDelegate* AsExtensionInfoBarDelegate();
92  virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate();
93  virtual PluginInstallerInfoBarDelegate* AsPluginInstallerInfoBarDelegate();
94  virtual ThemeInstalledInfoBarDelegate* AsThemePreviewInfobarDelegate();
95  virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate();
96
97 protected:
98  // Provided to subclasses as a convenience to initialize the state of this
99  // object. If |contents| is non-NULL, its active entry's unique ID will be
100  // stored using StoreActiveEntryUniqueID automatically.
101  explicit InfoBarDelegate(TabContents* contents);
102
103  // Store the unique id for the active entry in the specified TabContents, to
104  // be used later upon navigation to determine if this InfoBarDelegate should
105  // be expired from |contents_|.
106  void StoreActiveEntryUniqueID(TabContents* contents);
107
108 private:
109  // The unique id of the active NavigationEntry of the TabContents that we were
110  // opened for. Used to help expire on navigations.
111  int contents_unique_id_;
112
113  DISALLOW_COPY_AND_ASSIGN(InfoBarDelegate);
114};
115
116#endif  // CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_
117