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_SERVICE_H_
6#define CHROME_BROWSER_INFOBARS_INFOBAR_SERVICE_H_
7
8#include <vector>
9
10#include "base/memory/scoped_ptr.h"
11#include "content/public/browser/web_contents_observer.h"
12#include "content/public/browser/web_contents_user_data.h"
13
14class InfoBar;
15
16// Provides access to creating, removing and enumerating info bars
17// attached to a tab.
18class InfoBarService : public content::WebContentsObserver,
19                       public content::WebContentsUserData<InfoBarService> {
20 public:
21  // Changes whether infobars are enabled.  The default is true.
22  void set_infobars_enabled(bool enabled) { infobars_enabled_ = enabled; }
23
24  // Adds the specified |infobar|, which already owns a delegate.
25  //
26  // If infobars are disabled for this tab or the tab already has an infobar
27  // whose delegate returns true for
28  // InfoBarDelegate::EqualsDelegate(infobar->delegate()), |infobar| is deleted
29  // immediately without being added.
30  //
31  // Returns the infobar if it was successfully added.
32  virtual InfoBar* AddInfoBar(scoped_ptr<InfoBar> infobar);
33
34  // Removes the specified |infobar|.  This in turn may close immediately or
35  // animate closed; at the end the infobar will delete itself.
36  //
37  // If infobars are disabled for this tab, this will do nothing, on the
38  // assumption that the matching AddInfoBar() call will have already deleted
39  // the infobar (see above).
40  void RemoveInfoBar(InfoBar* infobar);
41
42  // Replaces one infobar with another, without any animation in between.  This
43  // will result in |old_infobar| being synchronously deleted.
44  //
45  // If infobars are disabled for this tab, |new_infobar| is deleted immediately
46  // without being added, and nothing else happens.
47  //
48  // Returns the new infobar if it was successfully added.
49  //
50  // NOTE: This does not perform any EqualsDelegate() checks like AddInfoBar().
51  InfoBar* ReplaceInfoBar(InfoBar* old_infobar,
52                          scoped_ptr<InfoBar> new_infobar);
53
54  // Returns the number of infobars for this tab.
55  size_t infobar_count() const { return infobars_.size(); }
56
57  // Returns the infobar at the given |index|.  The InfoBarService retains
58  // ownership.
59  //
60  // Warning: Does not sanity check |index|.
61  InfoBar* infobar_at(size_t index) { return infobars_[index]; }
62
63  // Retrieve the WebContents for the tab this service is associated with.
64  content::WebContents* web_contents() {
65    return content::WebContentsObserver::web_contents();
66  }
67
68 private:
69  friend class content::WebContentsUserData<InfoBarService>;
70
71  // InfoBars associated with this InfoBarService.  We own these pointers.
72  // However, this is not a ScopedVector, because we don't delete the infobars
73  // directly once they've been added to this; instead, when we're done with an
74  // infobar, we instruct it to delete itself and then orphan it.  See
75  // RemoveInfoBarInternal().
76  typedef std::vector<InfoBar*> InfoBars;
77
78  explicit InfoBarService(content::WebContents* web_contents);
79  virtual ~InfoBarService();
80
81  // content::WebContentsObserver:
82  virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
83  virtual void NavigationEntryCommitted(
84      const content::LoadCommittedDetails& load_details) OVERRIDE;
85  virtual void WebContentsDestroyed(
86      content::WebContents* web_contents) OVERRIDE;
87  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
88
89  void RemoveInfoBarInternal(InfoBar* infobar, bool animate);
90  void RemoveAllInfoBars(bool animate);
91
92  // Message handlers.
93  void OnDidBlockDisplayingInsecureContent();
94  void OnDidBlockRunningInsecureContent();
95
96  InfoBars infobars_;
97  bool infobars_enabled_;
98
99  DISALLOW_COPY_AND_ASSIGN(InfoBarService);
100};
101
102#endif  // CHROME_BROWSER_INFOBARS_INFOBAR_SERVICE_H_
103