| 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 // Helper function to get the InfoBarManager attached to |web_contents|. |
| 24 class Observer { | 30 static InfoBarManager* InfoBarManagerFromWebContents( |
| 25 public: | 31 content::WebContents* web_contents); |
| 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 | 32 |
| 33 // Adds the specified |infobar|, which already owns a delegate. | 33 // These methods are simple pass-throughs to InfoBarManager, and are here to |
| 34 // | 34 // prepare for the componentization of Infobars, see http://crbug.com/354379. |
| 35 // If infobars are disabled for this tab or the tab already has an infobar | 35 InfoBar* AddInfoBar(scoped_ptr<InfoBar> 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, | 36 InfoBar* ReplaceInfoBar(InfoBar* old_infobar, |
| 61 scoped_ptr<InfoBar> new_infobar); | 37 scoped_ptr<InfoBar> new_infobar); |
| 62 | 38 |
| 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. | 39 // Retrieve the WebContents for the tab this service is associated with. |
| 73 content::WebContents* web_contents() { | 40 content::WebContents* web_contents() { |
| 74 return content::WebContentsObserver::web_contents(); | 41 return content::WebContentsObserver::web_contents(); |
| 75 } | 42 } |
| 76 | 43 |
| 77 void AddObserver(Observer* obs); | 44 InfoBarManager* infobar_manager() { return &infobar_manager_; } |
| 78 void RemoveObserver(Observer* obs); | |
| 79 | 45 |
| 80 private: | 46 private: |
| 81 friend class content::WebContentsUserData<InfoBarService>; | 47 friend class content::WebContentsUserData<InfoBarService>; |
| 82 | 48 |
| 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); | 49 explicit InfoBarService(content::WebContents* web_contents); |
| 91 virtual ~InfoBarService(); | 50 virtual ~InfoBarService(); |
| 92 | 51 |
| 93 // content::WebContentsObserver: | 52 // content::WebContentsObserver: |
| 94 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; | 53 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; |
| 95 virtual void NavigationEntryCommitted( | 54 virtual void NavigationEntryCommitted( |
| 96 const content::LoadCommittedDetails& load_details) OVERRIDE; | 55 const content::LoadCommittedDetails& load_details) OVERRIDE; |
| 97 virtual void WebContentsDestroyed( | 56 virtual void WebContentsDestroyed( |
| 98 content::WebContents* web_contents) OVERRIDE; | 57 content::WebContents* web_contents) OVERRIDE; |
| 99 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 58 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 100 | 59 |
| 101 void RemoveInfoBarInternal(InfoBar* infobar, bool animate); | 60 // InfoBarManager::Observer: |
| 102 void RemoveAllInfoBars(bool animate); | 61 virtual void OnInfoBarAdded(InfoBar* infobar) OVERRIDE; |
| 62 virtual void OnInfoBarReplaced(InfoBar* old_infobar, |
| 63 InfoBar* new_infobar) OVERRIDE; |
| 64 virtual void OnInfoBarRemoved(InfoBar* infobar, bool animate) OVERRIDE; |
| 65 virtual void OnManagerShuttingDown(InfoBarManager* manager) OVERRIDE; |
| 103 | 66 |
| 104 // Message handlers. | 67 // Message handlers. |
| 105 void OnDidBlockDisplayingInsecureContent(); | 68 void OnDidBlockDisplayingInsecureContent(); |
| 106 void OnDidBlockRunningInsecureContent(); | 69 void OnDidBlockRunningInsecureContent(); |
| 107 | 70 |
| 108 InfoBars infobars_; | 71 InfoBarManager infobar_manager_; |
| 109 bool infobars_enabled_; | |
| 110 ObserverList<Observer, true> observer_list_; | |
| 111 | 72 |
| 112 DISALLOW_COPY_AND_ASSIGN(InfoBarService); | 73 DISALLOW_COPY_AND_ASSIGN(InfoBarService); |
| 113 }; | 74 }; |
| 114 | 75 |
| 115 #endif // CHROME_BROWSER_INFOBARS_INFOBAR_SERVICE_H_ | 76 #endif // CHROME_BROWSER_INFOBARS_INFOBAR_SERVICE_H_ |
| OLD | NEW |