Home | History | Annotate | Download | only in ui
      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_UI_BROWSER_LIST_H_
      6 #define CHROME_BROWSER_UI_BROWSER_LIST_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/lazy_instance.h"
     12 #include "base/observer_list.h"
     13 #include "chrome/browser/ui/host_desktop.h"
     14 
     15 class Browser;
     16 class Profile;
     17 
     18 namespace chrome {
     19 class BrowserListObserver;
     20 }
     21 
     22 // Maintains a list of Browser objects present in a given HostDesktop (see
     23 // HostDesktopType).
     24 class BrowserList {
     25  public:
     26   typedef std::vector<Browser*> BrowserVector;
     27   typedef BrowserVector::const_iterator const_iterator;
     28   typedef BrowserVector::const_reverse_iterator const_reverse_iterator;
     29 
     30   // Returns the last active browser for this list.
     31   Browser* GetLastActive() const;
     32 
     33   // Browsers are added to the list before they have constructed windows,
     34   // so the |window()| member function may return NULL.
     35   const_iterator begin() const { return browsers_.begin(); }
     36   const_iterator end() const { return browsers_.end(); }
     37 
     38   bool empty() const { return browsers_.empty(); }
     39   size_t size() const { return browsers_.size(); }
     40 
     41   Browser* get(size_t index) const { return browsers_[index]; }
     42 
     43   // Returns iterated access to list of open browsers ordered by when
     44   // they were last active. The underlying data structure is a vector
     45   // and we push_back on recent access so a reverse iterator gives the
     46   // latest accessed browser first.
     47   const_reverse_iterator begin_last_active() const {
     48     return last_active_browsers_.rbegin();
     49   }
     50   const_reverse_iterator end_last_active() const {
     51     return last_active_browsers_.rend();
     52   }
     53 
     54   static BrowserList* GetInstance(chrome::HostDesktopType type);
     55 
     56   // Adds or removes |browser| from the list it is associated with. The browser
     57   // object should be valid BEFORE these calls (for the benefit of observers),
     58   // so notify and THEN delete the object.
     59   static void AddBrowser(Browser* browser);
     60   static void RemoveBrowser(Browser* browser);
     61 
     62   // Adds and removes |observer| from the observer list for all desktops.
     63   // Observers are responsible for making sure the notifying browser is relevant
     64   // to them (e.g., on the specific desktop they care about if any).
     65   static void AddObserver(chrome::BrowserListObserver* observer);
     66   static void RemoveObserver(chrome::BrowserListObserver* observer);
     67 
     68   // Called by Browser objects when their window is activated (focused).  This
     69   // allows us to determine what the last active Browser was on each desktop.
     70   // Note: This only takes effect on the appropriate browser list as determined
     71   // by |browser->host_desktop_type()|.
     72   static void SetLastActive(Browser* browser);
     73 
     74   // Closes all browsers for |profile| across all desktops.
     75   static void CloseAllBrowsersWithProfile(Profile* profile);
     76 
     77   // Returns true if at least one incognito session is active across all
     78   // desktops.
     79   static bool IsOffTheRecordSessionActive();
     80 
     81   // Returns true if at least one incognito session is active for |profile|
     82   // across all desktops.
     83   static bool IsOffTheRecordSessionActiveForProfile(Profile* profile);
     84 
     85  private:
     86   BrowserList();
     87   ~BrowserList();
     88 
     89   // Helper method to remove a browser instance from a list of browsers
     90   static void RemoveBrowserFrom(Browser* browser, BrowserVector* browser_list);
     91 
     92   // A vector of the browsers in this list, in the order they were added.
     93   BrowserVector browsers_;
     94   // A vector of the browsers in this list that have been activated, in the
     95   // reverse order in which they were last activated.
     96   BrowserVector last_active_browsers_;
     97 
     98   // A list of observers which will be notified of every browser addition and
     99   // removal across all BrowserLists.
    100   static base::LazyInstance<ObserverList<chrome::BrowserListObserver> >::Leaky
    101       observers_;
    102 
    103   // Nothing fancy, since we only have two HDTs.
    104   static BrowserList* native_instance_;
    105   static BrowserList* ash_instance_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(BrowserList);
    108 };
    109 
    110 #endif  // CHROME_BROWSER_UI_BROWSER_LIST_H_
    111