Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_SERVICE_H_ |
| 6 #define CHROME_BROWSER_INFOBARS_INFOBAR_SERVICE_H_ | 6 #define CHROME_BROWSER_INFOBARS_INFOBAR_SERVICE_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 "chrome/browser/infobars/infobar_manager.h" |
| 12 #include "content/public/browser/web_contents_observer.h" | 12 #include "content/public/browser/web_contents_observer.h" |
| 13 #include "content/public/browser/web_contents_user_data.h" | 13 #include "content/public/browser/web_contents_user_data.h" |
| 14 | 14 |
| 15 namespace content { | |
| 16 struct LoadCommittedDetails; | |
| 17 class WebContents; | |
| 18 } | |
| 19 | |
| 15 class InfoBar; | 20 class InfoBar; |
| 16 | 21 |
| 17 // Provides access to creating, removing and enumerating info bars | 22 // Associates a Tab to a InfoBarManager and manages its lifetime. |
| 18 // attached to a tab. | 23 // It manages the infobar notifications and responds to navigation events. |
| 19 class InfoBarService : public content::WebContentsObserver, | 24 class InfoBarService : public content::WebContentsObserver, |
| 20 public content::WebContentsUserData<InfoBarService> { | 25 public content::WebContentsUserData<InfoBarService>, |
| 26 public InfoBarManager::Observer { | |
| 21 public: | 27 public: |
| 22 | 28 |
| 23 // Observer class for infobar events. | 29 // These methods are simple pass-throughs to InfoBarManager, and are here to |
| 24 class Observer { | 30 // prepare for the componentization of Infobars, see http://crbug.com/354379. |
| 25 public: | 31 InfoBar* AddInfoBar(scoped_ptr<InfoBar> infobar); |
| 26 virtual void OnInfoBarAdded(InfoBar* infobar) = 0; | |
| 27 virtual void OnInfoBarRemoved(InfoBar* infobar, bool animate) = 0; | |
| 28 virtual void OnInfoBarReplaced(InfoBar* old_infobar, | |
| 29 InfoBar* new_infobar) = 0; | |
| 30 virtual void OnServiceShuttingDown(InfoBarService* service) = 0; | |
| 31 }; | |
| 32 | |
| 33 // Adds the specified |infobar|, which already owns a delegate. | |
| 34 // | |
| 35 // If infobars are disabled for this tab or the tab already has an infobar | |
| 36 // whose delegate returns true for | |
| 37 // InfoBarDelegate::EqualsDelegate(infobar->delegate()), |infobar| is deleted | |
| 38 // immediately without being added. | |
| 39 // | |
| 40 // Returns the infobar if it was successfully added. | |
| 41 virtual InfoBar* AddInfoBar(scoped_ptr<InfoBar> infobar); | |
| 42 | |
| 43 // Removes the specified |infobar|. This in turn may close immediately or | |
| 44 // animate closed; at the end the infobar will delete itself. | |
| 45 // | |
| 46 // If infobars are disabled for this tab, this will do nothing, on the | |
| 47 // assumption that the matching AddInfoBar() call will have already deleted | |
| 48 // the infobar (see above). | |
| 49 void RemoveInfoBar(InfoBar* infobar); | |
| 50 | |
| 51 // Replaces one infobar with another, without any animation in between. This | |
| 52 // will result in |old_infobar| being synchronously deleted. | |
| 53 // | |
| 54 // If infobars are disabled for this tab, |new_infobar| is deleted immediately | |
| 55 // without being added, and nothing else happens. | |
| 56 // | |
| 57 // Returns the new infobar if it was successfully added. | |
| 58 // | |
| 59 // NOTE: This does not perform any EqualsDelegate() checks like AddInfoBar(). | |
| 60 InfoBar* ReplaceInfoBar(InfoBar* old_infobar, | 32 InfoBar* ReplaceInfoBar(InfoBar* old_infobar, |
| 61 scoped_ptr<InfoBar> new_infobar); | 33 scoped_ptr<InfoBar> new_infobar); |
| 62 | 34 |
| 63 // Returns the number of infobars for this tab. | |
| 64 size_t infobar_count() const { return infobars_.size(); } | |
| 65 | |
| 66 // Returns the infobar at the given |index|. The InfoBarService retains | |
| 67 // ownership. | |
| 68 // | |
| 69 // Warning: Does not sanity check |index|. | |
| 70 InfoBar* infobar_at(size_t index) { return infobars_[index]; } | |
| 71 | |
| 72 // Retrieve the WebContents for the tab this service is associated with. | 35 // Retrieve the WebContents for the tab this service is associated with. |
| 73 content::WebContents* web_contents() { | 36 content::WebContents* web_contents() { |
| 74 return content::WebContentsObserver::web_contents(); | 37 return content::WebContentsObserver::web_contents(); |
| 75 } | 38 } |
| 76 | 39 |
| 77 void AddObserver(Observer* obs); | 40 InfoBarManager& infobar_manager() { return infobar_manager_; } |
|
Peter Kasting
2014/03/26 23:51:30
Do not return non-const refs. Return a non-const
| |
| 78 void RemoveObserver(Observer* obs); | |
| 79 | 41 |
| 80 private: | 42 private: |
| 81 friend class content::WebContentsUserData<InfoBarService>; | 43 friend class content::WebContentsUserData<InfoBarService>; |
| 82 | 44 |
| 83 // InfoBars associated with this InfoBarService. We own these pointers. | |
| 84 // 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 | |
| 86 // infobar, we instruct it to delete itself and then orphan it. See | |
| 87 // RemoveInfoBarInternal(). | |
| 88 typedef std::vector<InfoBar*> InfoBars; | |
| 89 | |
| 90 explicit InfoBarService(content::WebContents* web_contents); | 45 explicit InfoBarService(content::WebContents* web_contents); |
| 91 virtual ~InfoBarService(); | 46 virtual ~InfoBarService(); |
| 92 | 47 |
| 93 // content::WebContentsObserver: | 48 // content::WebContentsObserver: |
| 94 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; | 49 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; |
| 95 virtual void NavigationEntryCommitted( | 50 virtual void NavigationEntryCommitted( |
| 96 const content::LoadCommittedDetails& load_details) OVERRIDE; | 51 const content::LoadCommittedDetails& load_details) OVERRIDE; |
| 97 virtual void WebContentsDestroyed( | 52 virtual void WebContentsDestroyed( |
| 98 content::WebContents* web_contents) OVERRIDE; | 53 content::WebContents* web_contents) OVERRIDE; |
| 99 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 54 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 100 | 55 |
| 101 void RemoveInfoBarInternal(InfoBar* infobar, bool animate); | 56 // InfoBarManager::Observer: |
| 102 void RemoveAllInfoBars(bool animate); | 57 virtual void OnInfoBarAdded(InfoBar* infobar) OVERRIDE; |
| 58 virtual void OnInfoBarReplaced(InfoBar* old_infobar, | |
| 59 InfoBar* new_infobar) OVERRIDE; | |
| 60 virtual void OnInfoBarRemoved(InfoBar* infobar, bool animate) OVERRIDE; | |
| 61 virtual void OnManagerShuttingDown(InfoBarManager* manager) OVERRIDE; | |
| 103 | 62 |
| 104 // Message handlers. | 63 // Message handlers. |
| 105 void OnDidBlockDisplayingInsecureContent(); | 64 void OnDidBlockDisplayingInsecureContent(); |
| 106 void OnDidBlockRunningInsecureContent(); | 65 void OnDidBlockRunningInsecureContent(); |
| 107 | 66 |
| 108 InfoBars infobars_; | 67 InfoBarManager infobar_manager_; |
| 109 bool infobars_enabled_; | |
| 110 ObserverList<Observer, true> observer_list_; | |
| 111 | 68 |
| 112 DISALLOW_COPY_AND_ASSIGN(InfoBarService); | 69 DISALLOW_COPY_AND_ASSIGN(InfoBarService); |
| 113 }; | 70 }; |
| 114 | 71 |
| 115 #endif // CHROME_BROWSER_INFOBARS_INFOBAR_SERVICE_H_ | 72 #endif // CHROME_BROWSER_INFOBARS_INFOBAR_SERVICE_H_ |
| OLD | NEW |