15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_UI_BROWSER_LIST_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_UI_BROWSER_LIST_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/basictypes.h" 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/lazy_instance.h" 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/observer_list.h" 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/host_desktop.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Browser; 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Profile; 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace base { 191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass FilePath; 201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci} 211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace chrome { 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BrowserListObserver; 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Maintains a list of Browser objects present in a given HostDesktop (see 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// HostDesktopType). 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BrowserList { 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typedef std::vector<Browser*> BrowserVector; 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typedef BrowserVector::const_iterator const_iterator; 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typedef BrowserVector::const_reverse_iterator const_reverse_iterator; 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns the last active browser for this list. 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Browser* GetLastActive() const; 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Browsers are added to the list before they have constructed windows, 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // so the |window()| member function may return NULL. 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const_iterator begin() const { return browsers_.begin(); } 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const_iterator end() const { return browsers_.end(); } 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool empty() const { return browsers_.empty(); } 432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) size_t size() const { return browsers_.size(); } 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Browser* get(size_t index) const { return browsers_[index]; } 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns iterated access to list of open browsers ordered by when 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // they were last active. The underlying data structure is a vector 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // and we push_back on recent access so a reverse iterator gives the 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // latest accessed browser first. 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const_reverse_iterator begin_last_active() const { 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return last_active_browsers_.rbegin(); 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const_reverse_iterator end_last_active() const { 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return last_active_browsers_.rend(); 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static BrowserList* GetInstance(chrome::HostDesktopType type); 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Adds or removes |browser| from the list it is associated with. The browser 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // object should be valid BEFORE these calls (for the benefit of observers), 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // so notify and THEN delete the object. 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void AddBrowser(Browser* browser); 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void RemoveBrowser(Browser* browser); 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Adds and removes |observer| from the observer list for all desktops. 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Observers are responsible for making sure the notifying browser is relevant 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // to them (e.g., on the specific desktop they care about if any). 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void AddObserver(chrome::BrowserListObserver* observer); 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void RemoveObserver(chrome::BrowserListObserver* observer); 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Called by Browser objects when their window is activated (focused). This 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // allows us to determine what the last active Browser was on each desktop. 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Note: This only takes effect on the appropriate browser list as determined 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // by |browser->host_desktop_type()|. 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void SetLastActive(Browser* browser); 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Closes all browsers for |profile| across all desktops. 791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // TODO(mlerman): Move the Profile Deletion flow to use the overloaded 801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // version of this method with a callback, then remove this method. 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void CloseAllBrowsersWithProfile(Profile* profile); 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Closes all browsers for |profile| across all desktops. Uses 841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // TryToCloseBrowserList() to do the actual closing and trigger any 851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // OnBeforeUnload events. If all OnBeforeUnload events are confirmed, 861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // |on_close_success| is called. 871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci static void CloseAllBrowsersWithProfile( 881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci Profile* profile, 891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const base::Callback<void(const base::FilePath&)>& on_close_success); 901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns true if at least one incognito session is active across all 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // desktops. 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool IsOffTheRecordSessionActive(); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns true if at least one incognito session is active for |profile| 962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // across all desktops. 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool IsOffTheRecordSessionActiveForProfile(Profile* profile); 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) BrowserList(); 1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ~BrowserList(); 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Helper method to remove a browser instance from a list of browsers 1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static void RemoveBrowserFrom(Browser* browser, BrowserVector* browser_list); 1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Attempts to close |browsers_to_close| while respecting OnBeforeUnload 1071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // events. If there are no OnBeforeUnload events to be called, 1081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // |on_close_confirmed| will be called, with a parameter of |profile_path|, 1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // and the Browsers will then be closed. If at least one unfired 1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // OnBeforeUnload event is found, handle it with a callback to 1111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // PostBeforeUnloadHandlers, which upon success will recursively call this 1121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // method to handle any other OnBeforeUnload events. 1131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci static void TryToCloseBrowserList( 1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const BrowserVector& browsers_to_close, 1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const base::Callback<void(const base::FilePath&)>& on_close_success, 1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const base::FilePath& profile_path); 1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Called after handling an OnBeforeUnload event. If |tab_close_confirmed| is 1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // true, calls |TryToCloseBrowserList()|, passing the parameters 1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // |browsers_to_close|, |on_close_confirmed|, and |profile_path|. Otherwise, 1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // resets all the OnBeforeUnload event handlers. 1221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci static void PostBeforeUnloadHandlers( 1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const BrowserVector& browsers_to_close, 1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const base::Callback<void(const base::FilePath&)>& on_close_success, 1251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const base::FilePath& profile_path, 1261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool tab_close_confirmed); 1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // A vector of the browsers in this list, in the order they were added. 1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) BrowserVector browsers_; 1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // A vector of the browsers in this list that have been activated, in the 1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // reverse order in which they were last activated. 1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) BrowserVector last_active_browsers_; 1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // A list of observers which will be notified of every browser addition and 1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // removal across all BrowserLists. 1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static base::LazyInstance<ObserverList<chrome::BrowserListObserver> >::Leaky 1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) observers_; 1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Nothing fancy, since we only have two HDTs. 1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static BrowserList* native_instance_; 1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static BrowserList* ash_instance_; 1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(BrowserList); 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // CHROME_BROWSER_UI_BROWSER_LIST_H_ 147