Home | History | Annotate | Download | only in infobars
      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/notification_observer.h"
     12 #include "content/public/browser/notification_registrar.h"
     13 #include "content/public/browser/web_contents_observer.h"
     14 #include "content/public/browser/web_contents_user_data.h"
     15 
     16 class InfoBarDelegate;
     17 
     18 // Provides access to creating, removing and enumerating info bars
     19 // attached to a tab.
     20 class InfoBarService : public content::WebContentsObserver,
     21                        public content::NotificationObserver,
     22                        public content::WebContentsUserData<InfoBarService> {
     23  public:
     24   // Changes whether infobars are enabled.  The default is true.
     25   void set_infobars_enabled(bool enabled) { infobars_enabled_ = enabled; }
     26 
     27   // Adds an InfoBar for the specified |delegate|.
     28   //
     29   // If infobars are disabled for this tab or the tab already has a delegate
     30   // which returns true for InfoBarDelegate::EqualsDelegate(delegate),
     31   // |delegate| is closed immediately without being added.
     32   //
     33   // Returns the delegate if it was successfully added.
     34   InfoBarDelegate* AddInfoBar(scoped_ptr<InfoBarDelegate> infobar);
     35 
     36   // Removes the InfoBar for the specified |delegate|.
     37   //
     38   // 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
     40   // delegate (see above).
     41   void RemoveInfoBar(InfoBarDelegate* infobar);
     42 
     43   // Replaces one infobar with another, without any animation in between.
     44   //
     45   // If infobars are disabled for this tab, |new_delegate| is closed immediately
     46   // without being added, and nothing else happens.
     47   //
     48   // Returns the new delegate if it was successfully added.
     49   //
     50   // NOTE: This does not perform any EqualsDelegate() checks like AddInfoBar().
     51   InfoBarDelegate* ReplaceInfoBar(InfoBarDelegate* old_infobar,
     52                                   scoped_ptr<InfoBarDelegate> 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   InfoBarDelegate* 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   typedef std::vector<InfoBarDelegate*> InfoBars;
     72 
     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);
     78   virtual ~InfoBarService();
     79 
     80   // content::WebContentsObserver:
     81   virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
     82   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     83 
     84   // content::NotificationObserver:
     85   virtual void Observe(int type,
     86                        const content::NotificationSource& source,
     87                        const content::NotificationDetails& details) OVERRIDE;
     88 
     89   void RemoveInfoBarInternal(InfoBarDelegate* 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   content::NotificationRegistrar registrar_;
    100 
    101   DISALLOW_COPY_AND_ASSIGN(InfoBarService);
    102 };
    103 
    104 #endif  // CHROME_BROWSER_INFOBARS_INFOBAR_SERVICE_H_
    105