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_UI_BROWSER_LIST_H_ | 5 #ifndef CHROME_BROWSER_LIFETIME_APPLICATION_LIFETIME_H_ |
6 #define CHROME_BROWSER_UI_BROWSER_LIST_H_ | 6 #define CHROME_BROWSER_LIFETIME_APPLICATION_LIFETIME_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <set> | 9 namespace browser { |
10 #include <vector> | |
11 | 10 |
12 #include "base/observer_list.h" | 11 // Starts a user initiated exit process. Called from Browser::Exit. |
13 #include "ui/gfx/native_widget_types.h" | 12 // On platforms other than ChromeOS, this is equivalent to |
| 13 // CloseAllBrowsers() On ChromeOS, this tells session manager |
| 14 // that chrome is signing out, which lets session manager send |
| 15 // SIGTERM to start actual exit process. |
| 16 void AttemptUserExit(); |
14 | 17 |
15 class Browser; | 18 // Starts a user initiated restart process. On platforms other than |
16 class Profile; | 19 // chromeos, this sets a restart bit in the preference so that |
| 20 // chrome will be restarted at the end of shutdown process. On |
| 21 // ChromeOS, this simply exits the chrome, which lets sesssion |
| 22 // manager re-launch the browser with restore last session flag. |
| 23 void AttemptRestart(); |
17 | 24 |
18 // Stores a list of all Browser objects. | 25 // Attempt to exit by closing all browsers. This is equivalent to |
19 class BrowserList { | 26 // CloseAllBrowsers() on platforms where the application exits |
20 public: | 27 // when no more windows are remaining. On other platforms (the Mac), |
21 typedef std::vector<Browser*> BrowserVector; | 28 // this will additionally exit the application if all browsers are |
22 typedef BrowserVector::iterator iterator; | 29 // successfully closed. |
23 typedef BrowserVector::const_iterator const_iterator; | 30 // Note that he exit process may be interrupted by download or |
24 typedef BrowserVector::const_reverse_iterator const_reverse_iterator; | 31 // unload handler, and the browser may or may not exit. |
25 | 32 void AttemptExit(); |
26 // It is not allowed to change the global window list (add or remove any | |
27 // browser windows while handling observer callbacks. | |
28 class Observer { | |
29 public: | |
30 // Called immediately after a browser is added to the list | |
31 virtual void OnBrowserAdded(const Browser* browser) = 0; | |
32 | |
33 // Called immediately after a browser is removed from the list | |
34 virtual void OnBrowserRemoved(const Browser* browser) = 0; | |
35 | |
36 // Called immediately after a browser is set active (SetLastActive) | |
37 virtual void OnBrowserSetLastActive(const Browser* browser) {} | |
38 | |
39 protected: | |
40 virtual ~Observer() {} | |
41 }; | |
42 | |
43 // Adds and removes browsers from the global list. The browser object should | |
44 // be valid BEFORE these calls (for the benefit of observers), so notify and | |
45 // THEN delete the object. | |
46 static void AddBrowser(Browser* browser); | |
47 static void RemoveBrowser(Browser* browser); | |
48 | |
49 static void AddObserver(Observer* observer); | |
50 static void RemoveObserver(Observer* observer); | |
51 | |
52 // Called by Browser objects when their window is activated (focused). This | |
53 // allows us to determine what the last active Browser was. | |
54 static void SetLastActive(Browser* browser); | |
55 | |
56 // Returns the Browser object whose window was most recently active. If the | |
57 // most recently open Browser's window was closed, returns the first Browser | |
58 // in the list. If no Browsers exist, returns NULL. | |
59 // | |
60 // WARNING: this is NULL until a browser becomes active. If during startup | |
61 // a browser does not become active (perhaps the user launches Chrome, then | |
62 // clicks on another app before the first browser window appears) then this | |
63 // returns NULL. | |
64 // WARNING #2: this will always be NULL in unit tests run on the bots. | |
65 static Browser* GetLastActive(); | |
66 | |
67 // Checks if the browser can be automatically restarted to install upgrades | |
68 // The browser can be automatically restarted when: | |
69 // 1. It's in the background mode (no visible windows). | |
70 // 2. An update exe is present in the install folder. | |
71 static bool CanRestartForUpdate(); | |
72 | |
73 // Starts a user initiated exit process. Called from Browser::Exit. | |
74 // On platforms other than ChromeOS, this is equivalent to | |
75 // CloseAllBrowsers() On ChromeOS, this tells session manager | |
76 // that chrome is signing out, which lets session manager send | |
77 // SIGTERM to start actual exit process. | |
78 static void AttemptUserExit(); | |
79 | |
80 // Starts a user initiated restart process. On platforms other than | |
81 // chromeos, this sets a restart bit in the preference so that | |
82 // chrome will be restarted at the end of shutdown process. On | |
83 // ChromeOS, this simply exits the chrome, which lets sesssion | |
84 // manager re-launch the browser with restore last session flag. | |
85 static void AttemptRestart(); | |
86 | |
87 // Attempt to exit by closing all browsers. This is equivalent to | |
88 // CloseAllBrowsers() on platforms where the application exits | |
89 // when no more windows are remaining. On other platforms (the Mac), | |
90 // this will additionally exit the application if all browsers are | |
91 // successfully closed. | |
92 // Note that he exit process may be interrupted by download or | |
93 // unload handler, and the browser may or may not exit. | |
94 static void AttemptExit(); | |
95 | 33 |
96 #if defined(OS_CHROMEOS) | 34 #if defined(OS_CHROMEOS) |
97 // This is equivalent to AttemptUserExit, except that it always set | 35 // This is equivalent to AttemptUserExit, except that it always set |
98 // exit cleanly bit. ChromeOS checks if it can exit without user | 36 // exit cleanly bit. ChromeOS checks if it can exit without user |
99 // interactions, so it will always exit the browser. This is used to | 37 // interactions, so it will always exit the browser. This is used to |
100 // handle SIGTERM on chromeos which is a signal to force shutdown | 38 // handle SIGTERM on chromeos which is a signal to force shutdown |
101 // the chrome. | 39 // the chrome. |
102 static void ExitCleanly(); | 40 void ExitCleanly(); |
103 #endif | 41 #endif |
104 | 42 |
105 // Closes all browsers. If the session is ending the windows are closed | 43 // Closes all browsers. If the session is ending the windows are closed |
106 // directly. Otherwise the windows are closed by way of posting a WM_CLOSE | 44 // directly. Otherwise the windows are closed by way of posting a WM_CLOSE |
107 // message. | 45 // message. |
108 static void CloseAllBrowsers(); | 46 void CloseAllBrowsers(); |
109 | 47 |
110 // Closes all browsers for |profile|. | 48 // Begins shutdown of the application when the desktop session is ending. |
111 static void CloseAllBrowsersWithProfile(Profile* profile); | 49 void SessionEnding(); |
112 | 50 |
113 // Begins shutdown of the application when the desktop session is ending. | 51 // Tells the BrowserList to keep the application alive after the last Browser |
114 static void SessionEnding(); | 52 // closes. This is implemented as a count, so callers should pair their calls |
| 53 // to StartKeepAlive() with matching calls to EndKeepAlive() when they no |
| 54 // longer need to keep the application running. |
| 55 void StartKeepAlive(); |
115 | 56 |
116 // Tells the BrowserList to keep the application alive after the last Browser | 57 // Stops keeping the application alive after the last Browser is closed. |
117 // closes. This is implemented as a count, so callers should pair their calls | 58 // Should match a previous call to StartKeepAlive(). |
118 // to StartKeepAlive() with matching calls to EndKeepAlive() when they no | 59 void EndKeepAlive(); |
119 // longer need to keep the application running. | |
120 static void StartKeepAlive(); | |
121 | 60 |
122 // Stops keeping the application alive after the last Browser is closed. | 61 // Returns true if application will continue running after the last Browser |
123 // Should match a previous call to StartKeepAlive(). | 62 // closes. |
124 static void EndKeepAlive(); | 63 bool WillKeepAlive(); |
125 | 64 |
126 // Returns true if application will continue running after the last Browser | 65 // Emits APP_TERMINATING notification. It is guaranteed that the |
127 // closes. | 66 // notification is sent only once. |
128 static bool WillKeepAlive(); | 67 void NotifyAppTerminating(); |
129 | 68 |
130 // Browsers are added to the list before they have constructed windows, | 69 // Send out notifications. |
131 // so the |window()| member function may return NULL. | 70 // For ChromeOS, also request session manager to end the session. |
132 static const_iterator begin(); | 71 void NotifyAndTerminate(bool fast_path); |
133 static const_iterator end(); | |
134 | 72 |
135 static bool empty(); | 73 // Called once the application is exiting. |
136 static size_t size(); | 74 void OnAppExiting(); |
137 | 75 |
138 // Returns iterated access to list of open browsers ordered by when | 76 // Called once the application is exiting to do any platform specific |
139 // they were last active. The underlying data structure is a vector | 77 // processing required. |
140 // and we push_back on recent access so a reverse iterator gives the | 78 void HandleAppExitingForPlatform(); |
141 // latest accessed browser first. | |
142 static const_reverse_iterator begin_last_active(); | |
143 static const_reverse_iterator end_last_active(); | |
144 | 79 |
145 // Returns true if at least one incognito session is active. | 80 } // namespace browser |
146 static bool IsOffTheRecordSessionActive(); | |
147 | 81 |
148 // Returns true if at least one incognito session is active for |profile|. | 82 #endif // CHROME_BROWSER_LIFETIME_APPLICATION_LIFETIME_H_ |
149 static bool IsOffTheRecordSessionActiveForProfile(Profile* profile); | |
150 | |
151 // Send out notifications. | |
152 // For ChromeOS, also request session manager to end the session. | |
153 static void NotifyAndTerminate(bool fast_path); | |
154 | |
155 // Called once the application is exiting. | |
156 static void OnAppExiting(); | |
157 | |
158 // Called once the application is exiting to do any platform specific | |
159 // processing required. | |
160 static void HandleAppExitingForPlatform(); | |
161 | |
162 private: | |
163 // Helper method to remove a browser instance from a list of browsers | |
164 static void RemoveBrowserFrom(Browser* browser, BrowserVector* browser_list); | |
165 static void MarkAsCleanShutdown(); | |
166 static void AttemptExitInternal(); | |
167 | |
168 // Counter of calls to StartKeepAlive(). If non-zero, the application will | |
169 // continue running after the last browser has exited. | |
170 static int keep_alive_count_; | |
171 }; | |
172 | |
173 class TabContentsWrapper; | |
174 | |
175 // Iterates through all web view hosts in all browser windows. Because the | |
176 // renderers act asynchronously, getting a host through this interface does | |
177 // not guarantee that the renderer is ready to go. Doing anything to affect | |
178 // browser windows or tabs while iterating may cause incorrect behavior. | |
179 // | |
180 // Example: | |
181 // for (TabContentsIterator iterator; !iterator.done(); ++iterator) { | |
182 // TabContentsWrapper* cur = *iterator; | |
183 // -or- | |
184 // iterator->operationOnTabContents(); | |
185 // ... | |
186 // } | |
187 class TabContentsIterator { | |
188 public: | |
189 TabContentsIterator(); | |
190 | |
191 // Returns true if we are past the last Browser. | |
192 bool done() const { | |
193 return cur_ == NULL; | |
194 } | |
195 | |
196 // Returns the Browser instance associated with the current | |
197 // TabContentsWrapper. Valid as long as !done() | |
198 Browser* browser() const { | |
199 if (browser_iterator_ != BrowserList::end()) | |
200 return *browser_iterator_; | |
201 return NULL; | |
202 } | |
203 | |
204 // Returns the current TabContentsWrapper, valid as long as !Done() | |
205 TabContentsWrapper* operator->() const { | |
206 return cur_; | |
207 } | |
208 TabContentsWrapper* operator*() const { | |
209 return cur_; | |
210 } | |
211 | |
212 // Incrementing operators, valid as long as !Done() | |
213 TabContentsWrapper* operator++() { // ++preincrement | |
214 Advance(); | |
215 return cur_; | |
216 } | |
217 TabContentsWrapper* operator++(int) { // postincrement++ | |
218 TabContentsWrapper* tmp = cur_; | |
219 Advance(); | |
220 return tmp; | |
221 } | |
222 | |
223 private: | |
224 // Loads the next host into Cur. This is designed so that for the initial | |
225 // call when browser_iterator_ points to the first browser and | |
226 // web_view_index_ is -1, it will fill the first host. | |
227 void Advance(); | |
228 | |
229 // iterator over all the Browser objects | |
230 BrowserList::const_iterator browser_iterator_; | |
231 | |
232 // tab index into the current Browser of the current web view | |
233 int web_view_index_; | |
234 | |
235 // iterator over the TabContentsWrappers doing background printing. | |
236 std::set<TabContentsWrapper*>::const_iterator bg_printing_iterator_; | |
237 | |
238 // Current TabContentsWrapper, or NULL if we're at the end of the list. This | |
239 // can be extracted given the browser iterator and index, but it's nice to | |
240 // cache this since the caller may access the current host many times. | |
241 TabContentsWrapper* cur_; | |
242 }; | |
243 | |
244 #endif // CHROME_BROWSER_UI_BROWSER_LIST_H_ | |
OLD | NEW |