OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_INFOBARS_INFOBAR_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_INFOBARS_INFOBAR_MANAGER_H_ |
6 #define CHROME_BROWSER_INFOBARS_INFOBAR_SERVICE_H_ | 6 #define CHROME_BROWSER_INFOBARS_INFOBAR_MANAGER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
12 #include "content/public/browser/web_contents_observer.h" | 12 |
13 #include "content/public/browser/web_contents_user_data.h" | 13 namespace content { |
| 14 struct LoadCommittedDetails; |
| 15 class WebContents; |
| 16 } |
14 | 17 |
15 class InfoBar; | 18 class InfoBar; |
16 | 19 |
17 // Provides access to creating, removing and enumerating info bars | 20 // Provides access to creating, removing and enumerating info bars |
18 // attached to a tab. | 21 // attached to a tab. |
19 class InfoBarService : public content::WebContentsObserver, | 22 class InfoBarManager { |
20 public content::WebContentsUserData<InfoBarService> { | |
21 public: | 23 public: |
22 | |
23 // Observer class for infobar events. | 24 // Observer class for infobar events. |
24 class Observer { | 25 class Observer { |
25 public: | 26 public: |
26 virtual void OnInfoBarAdded(InfoBar* infobar) = 0; | 27 virtual void OnInfoBarAdded(InfoBar* infobar) = 0; |
27 virtual void OnInfoBarRemoved(InfoBar* infobar, bool animate) = 0; | 28 virtual void OnInfoBarRemoved(InfoBar* infobar, bool animate) = 0; |
28 virtual void OnInfoBarReplaced(InfoBar* old_infobar, | 29 virtual void OnInfoBarReplaced(InfoBar* old_infobar, |
29 InfoBar* new_infobar) = 0; | 30 InfoBar* new_infobar) = 0; |
30 virtual void OnServiceShuttingDown(InfoBarService* service) = 0; | 31 virtual void OnManagerShuttingDown(InfoBarManager* manager) = 0; |
31 }; | 32 }; |
32 | 33 |
| 34 explicit InfoBarManager(content::WebContents* web_contents); |
| 35 ~InfoBarManager(); |
| 36 |
33 // Adds the specified |infobar|, which already owns a delegate. | 37 // Adds the specified |infobar|, which already owns a delegate. |
34 // | 38 // |
35 // If infobars are disabled for this tab or the tab already has an infobar | 39 // If infobars are disabled for this tab or the tab already has an infobar |
36 // whose delegate returns true for | 40 // whose delegate returns true for |
37 // InfoBarDelegate::EqualsDelegate(infobar->delegate()), |infobar| is deleted | 41 // InfoBarDelegate::EqualsDelegate(infobar->delegate()), |infobar| is deleted |
38 // immediately without being added. | 42 // immediately without being added. |
39 // | 43 // |
40 // Returns the infobar if it was successfully added. | 44 // Returns the infobar if it was successfully added. |
41 virtual InfoBar* AddInfoBar(scoped_ptr<InfoBar> infobar); | 45 virtual InfoBar* AddInfoBar(scoped_ptr<InfoBar> infobar); |
42 | 46 |
43 // Removes the specified |infobar|. This in turn may close immediately or | 47 // Removes the specified |infobar|. This in turn may close immediately or |
44 // animate closed; at the end the infobar will delete itself. | 48 // animate closed; at the end the infobar will delete itself. |
45 // | 49 // |
46 // If infobars are disabled for this tab, this will do nothing, on the | 50 // If infobars are disabled for this tab, this will do nothing, on the |
47 // assumption that the matching AddInfoBar() call will have already deleted | 51 // assumption that the matching AddInfoBar() call will have already deleted |
48 // the infobar (see above). | 52 // the infobar (see above). |
49 void RemoveInfoBar(InfoBar* infobar); | 53 void RemoveInfoBar(InfoBar* infobar); |
50 | 54 |
| 55 // Removes all the infobars. |
| 56 void RemoveAllInfoBars(bool animate); |
| 57 |
51 // Replaces one infobar with another, without any animation in between. This | 58 // Replaces one infobar with another, without any animation in between. This |
52 // will result in |old_infobar| being synchronously deleted. | 59 // will result in |old_infobar| being synchronously deleted. |
53 // | 60 // |
54 // If infobars are disabled for this tab, |new_infobar| is deleted immediately | 61 // If infobars are disabled for this tab, |new_infobar| is deleted immediately |
55 // without being added, and nothing else happens. | 62 // without being added, and nothing else happens. |
56 // | 63 // |
57 // Returns the new infobar if it was successfully added. | 64 // Returns the new infobar if it was successfully added. |
58 // | 65 // |
59 // NOTE: This does not perform any EqualsDelegate() checks like AddInfoBar(). | 66 // NOTE: This does not perform any EqualsDelegate() checks like AddInfoBar(). |
60 InfoBar* ReplaceInfoBar(InfoBar* old_infobar, | 67 InfoBar* ReplaceInfoBar(InfoBar* old_infobar, |
61 scoped_ptr<InfoBar> new_infobar); | 68 scoped_ptr<InfoBar> new_infobar); |
62 | 69 |
63 // Returns the number of infobars for this tab. | 70 // Returns the number of infobars for this tab. |
64 size_t infobar_count() const { return infobars_.size(); } | 71 size_t infobar_count() const { return infobars_.size(); } |
65 | 72 |
66 // Returns the infobar at the given |index|. The InfoBarService retains | 73 // Returns the infobar at the given |index|. The InfoBarManager retains |
67 // ownership. | 74 // ownership. |
68 // | 75 // |
69 // Warning: Does not sanity check |index|. | 76 // Warning: Does not sanity check |index|. |
70 InfoBar* infobar_at(size_t index) { return infobars_[index]; } | 77 InfoBar* infobar_at(size_t index) { return infobars_[index]; } |
71 | 78 |
72 // Retrieve the WebContents for the tab this service is associated with. | 79 // Retrieve the WebContents for the tab this service is associated with. |
73 content::WebContents* web_contents() { | 80 // Do not add new call sites for this. |
74 return content::WebContentsObserver::web_contents(); | 81 // TODO(droger): remove this method. See http://crbug.com/354379. |
75 } | 82 content::WebContents* web_contents() { return web_contents_; } |
| 83 |
| 84 // Must be called when a navigation happens. |
| 85 void OnNavigation(const content::LoadCommittedDetails& load_details); |
| 86 |
| 87 // Called when the associated WebContents is being destroyed. |
| 88 void OnWebContentsDestroyed(); |
76 | 89 |
77 void AddObserver(Observer* obs); | 90 void AddObserver(Observer* obs); |
78 void RemoveObserver(Observer* obs); | 91 void RemoveObserver(Observer* obs); |
79 | 92 |
80 private: | 93 private: |
81 friend class content::WebContentsUserData<InfoBarService>; | 94 // InfoBars associated with this InfoBarManager. We own these pointers. |
82 | |
83 // InfoBars associated with this InfoBarService. We own these pointers. | |
84 // However, this is not a ScopedVector, because we don't delete the infobars | 95 // However, this is not a ScopedVector, because we don't delete the infobars |
85 // directly once they've been added to this; instead, when we're done with an | 96 // directly once they've been added to this; instead, when we're done with an |
86 // infobar, we instruct it to delete itself and then orphan it. See | 97 // infobar, we instruct it to delete itself and then orphan it. See |
87 // RemoveInfoBarInternal(). | 98 // RemoveInfoBarInternal(). |
88 typedef std::vector<InfoBar*> InfoBars; | 99 typedef std::vector<InfoBar*> InfoBars; |
89 | 100 |
90 explicit InfoBarService(content::WebContents* web_contents); | |
91 virtual ~InfoBarService(); | |
92 | |
93 // content::WebContentsObserver: | |
94 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; | |
95 virtual void NavigationEntryCommitted( | |
96 const content::LoadCommittedDetails& load_details) OVERRIDE; | |
97 virtual void WebContentsDestroyed( | |
98 content::WebContents* web_contents) OVERRIDE; | |
99 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
100 | |
101 void RemoveInfoBarInternal(InfoBar* infobar, bool animate); | 101 void RemoveInfoBarInternal(InfoBar* infobar, bool animate); |
102 void RemoveAllInfoBars(bool animate); | |
103 | |
104 // Message handlers. | |
105 void OnDidBlockDisplayingInsecureContent(); | |
106 void OnDidBlockRunningInsecureContent(); | |
107 | 102 |
108 InfoBars infobars_; | 103 InfoBars infobars_; |
109 bool infobars_enabled_; | 104 bool infobars_enabled_; |
| 105 |
| 106 // TODO(droger): remove this field. See http://crbug.com/354379. |
| 107 content::WebContents* web_contents_; |
| 108 |
110 ObserverList<Observer, true> observer_list_; | 109 ObserverList<Observer, true> observer_list_; |
111 | 110 |
112 DISALLOW_COPY_AND_ASSIGN(InfoBarService); | 111 DISALLOW_COPY_AND_ASSIGN(InfoBarManager); |
113 }; | 112 }; |
114 | 113 |
115 #endif // CHROME_BROWSER_INFOBARS_INFOBAR_SERVICE_H_ | 114 #endif // CHROME_BROWSER_INFOBARS_INFOBAR_MANAGER_H_ |
OLD | NEW |