blob: 858e97e088462633b3e8c82ffdfd3eafefb510c2 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
5#ifndef CHROME_BROWSER_BROWSER_LIST_H__
6#define CHROME_BROWSER_BROWSER_LIST_H__
7
8#include <algorithm>
9#include <vector>
10
11#include "chrome/browser/browser.h"
12
initial.commit09911bf2008-07-26 23:55:2913// Stores a list of all Browser objects.
14class BrowserList {
15 public:
16 typedef std::vector<Browser*> list_type;
17 typedef list_type::iterator iterator;
18 typedef list_type::const_iterator const_iterator;
19 typedef list_type::const_reverse_iterator const_reverse_iterator;
20
21 // It is not allowed to change the global window list (add or remove any
22 // browser windows while handling observer callbacks.
23 class Observer {
24 public:
25 // Called immediately after a browser is added to the list
26 virtual void OnBrowserAdded(const Browser* browser) = 0;
27
28 // Called immediately before a browser is removed from the list
29 virtual void OnBrowserRemoving(const Browser* browser) = 0;
[email protected]3f34599d2009-03-25 22:11:4330
31 // Called immediately after a browser is set active (SetLastActive)
32 virtual void OnBrowserSetLastActive(const Browser* browser) { };
initial.commit09911bf2008-07-26 23:55:2933 };
34
35 // Adds and removes browsers from the global list. The browser object should
36 // be valid BEFORE these calls (for the benefit of observers), so notify and
37 // THEN delete the object.
38 static void AddBrowser(Browser* browser);
39 static void RemoveBrowser(Browser* browser);
40
initial.commit09911bf2008-07-26 23:55:2941 static void AddObserver(Observer* observer);
42 static void RemoveObserver(Observer* observer);
43
44 // Called by Browser objects when their window is activated (focused). This
45 // allows us to determine what the last active Browser was.
46 static void SetLastActive(Browser* browser);
47
48 // Returns the Browser object whose window was most recently active. If the
49 // most recently open Browser's window was closed, returns the first Browser
50 // in the list. If no Browsers exist, returns NULL.
51 static Browser* GetLastActive();
52
53 // Find an existing browser window with the provided type. If the last active
54 // has the right type, it is returned. Otherwise, the next available browser
55 // is returned. Returns NULL if no such browser currently exists.
[email protected]299dabd2008-11-19 02:27:1656 static Browser* FindBrowserWithType(Profile* p, Browser::Type t);
initial.commit09911bf2008-07-26 23:55:2957
[email protected]77bc6732009-04-20 22:01:0358 // Find an existing browser with the provided ID. Returns NULL if no such
59 // browser currently exists.
60 static Browser* FindBrowserWithID(SessionID::id_type desired_id);
61
initial.commit09911bf2008-07-26 23:55:2962 // Closes all browsers. If use_post is true the windows are closed by way of
63 // posting a WM_CLOSE message, otherwise the windows are closed directly. In
64 // almost all cases you'll want to use true, the one exception is ending
65 // the session. use_post should only be false when invoked from end session.
66 static void CloseAllBrowsers(bool use_post);
67
[email protected]299dabd2008-11-19 02:27:1668 // Begins shutdown of the application when the Windows session is ending.
69 static void WindowsSessionEnding();
70
initial.commit09911bf2008-07-26 23:55:2971 // Returns true if there is at least one Browser with the specified profile.
72 static bool HasBrowserWithProfile(Profile* profile);
73
initial.commit09911bf2008-07-26 23:55:2974 static const_iterator begin() {
75 return browsers_.begin();
76 }
77
78 static const_iterator end() {
79 return browsers_.end();
80 }
81
82 static size_t size() {
83 return browsers_.size();
84 }
85
86 // Returns iterated access to list of open browsers ordered by when
87 // they were last active. The underlying data structure is a vector
88 // and we push_back on recent access so a reverse iterator gives the
89 // latest accessed browser first.
90 static const_reverse_iterator begin_last_active() {
91 return last_active_browsers_.rbegin();
92 }
93
94 static const_reverse_iterator end_last_active() {
95 return last_active_browsers_.rend();
96 }
97
98 // Return the number of browsers with the following profile which are
99 // currently open.
100 static size_t GetBrowserCount(Profile* p);
101
102 // Return the number of browsers with the following profile and type which are
103 // currently open.
[email protected]299dabd2008-11-19 02:27:16104 static size_t GetBrowserCountForType(Profile* p, Browser::Type type);
initial.commit09911bf2008-07-26 23:55:29105
106 // Returns true if at least one off the record session is active.
107 static bool IsOffTheRecordSessionActive();
108
109 private:
initial.commit09911bf2008-07-26 23:55:29110 // Helper method to remove a browser instance from a list of browsers
111 static void RemoveBrowserFrom(Browser* browser, list_type* browser_list);
112
113 static list_type browsers_;
114 static std::vector<Observer*> observers_;
115 static list_type last_active_browsers_;
initial.commit09911bf2008-07-26 23:55:29116};
117
[email protected]b6ad1cab2009-01-16 22:41:42118class WebContents;
initial.commit09911bf2008-07-26 23:55:29119
120// Iterates through all web view hosts in all browser windows. Because the
121// renderers act asynchronously, getting a host through this interface does
122// not guarantee that the renderer is ready to go. Doing anything to affect
123// browser windows or tabs while iterating may cause incorrect behavior.
124//
125// Example:
126// for (WebContentsIterator iterator; !iterator.done(); iterator++) {
127// WebContents* cur = *iterator;
128// -or-
129// iterator->operationOnWebContents();
130// ...
131// }
132class WebContentsIterator {
133 public:
134 WebContentsIterator();
135
136 // Returns true if we are past the last Browser.
137 bool done() const {
138 return cur_ == NULL;
139 }
140
141 // Returns the current WebContents, valid as long as !Done()
142 WebContents* operator->() const {
143 return cur_;
144 }
145 WebContents* operator*() const {
146 return cur_;
147 }
148
149 // Incrementing operators, valid as long as !Done()
150 WebContents* operator++() { // ++preincrement
151 Advance();
152 return cur_;
153 }
154 WebContents* operator++(int) { // postincrement++
155 WebContents* tmp = cur_;
156 Advance();
157 return tmp;
158 }
159
160 private:
161 // Loads the next host into Cur. This is designed so that for the initial
162 // call when browser_iterator_ points to the first browser and
163 // web_view_index_ is -1, it will fill the first host.
164 void Advance();
165
166 // iterator over all the Browser objects
167 BrowserList::const_iterator browser_iterator_;
168
169 // tab index into the current Browser of the current web view
170 int web_view_index_;
171
172 // Current WebContents, or NULL if we're at the end of the list. This can
173 // be extracted given the browser iterator and index, but it's nice to cache
174 // this since the caller may access the current host many times.
175 WebContents* cur_;
176};
177
178#endif // CHROME_BROWSER_BROWSER_LIST_H__