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