Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: chrome/browser/ui/browser_list_impl.cc

Issue 10690151: Move implementation of BrowserList onto an inner, instantiatable class, BrowserListImpl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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 #include "chrome/browser/ui/browser_list_impl.h"
6
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/browser_shutdown.h"
11 #include "chrome/browser/lifetime/application_lifetime.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_list_observer.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/common/chrome_notification_types.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "content/public/browser/notification_service.h"
19
20 // #include "build/build_config.h"
21 // #include "chrome/browser/prefs/pref_service.h"
22
23
24 #if defined(OS_CHROMEOS)
25 #include "chrome/browser/chromeos/login/user_manager.h"
26 #endif
27
28 namespace chrome {
29
30 // static
31 BrowserListImpl* BrowserListImpl::native_instance_ = NULL;
32 BrowserListImpl* BrowserListImpl::ash_instance_ = NULL;
33
34 ////////////////////////////////////////////////////////////////////////////////
35 // BrowserListImpl, public:
36
37 // static
38 BrowserListImpl* BrowserListImpl::GetInstance(HostDesktopType type) {
39 BrowserListImpl** list = NULL;
40 if (type == HOST_DESKTOP_TYPE_NATIVE)
41 list = &native_instance_;
42 else if (type == HOST_DESKTOP_TYPE_ASH)
43 list = &ash_instance_;
44 else
45 NOTREACHED();
46 if (!*list)
47 *list = new BrowserListImpl;
48 return *list;
49 }
50
51 void BrowserListImpl::AddBrowser(Browser* browser) {
52 DCHECK(browser);
53 browsers_.push_back(browser);
54
55 g_browser_process->AddRefModule();
56
57 content::NotificationService::current()->Notify(
58 chrome::NOTIFICATION_BROWSER_OPENED,
59 content::Source<Browser>(browser),
60 content::NotificationService::NoDetails());
61
62 // Send out notifications after add has occurred. Do some basic checking to
63 // try to catch evil observers that change the list from under us.
64 size_t original_count = observers_.size();
65 FOR_EACH_OBSERVER(BrowserListObserver, observers_, OnBrowserAdded(browser));
66 DCHECK_EQ(original_count, observers_.size())
67 << "observer list modified during notification";
68 }
69
70 void BrowserListImpl::RemoveBrowser(Browser* browser) {
71 RemoveBrowserFrom(browser, &last_active_browsers_);
72
73 // Many UI tests rely on closing the last browser window quitting the
74 // application.
75 // Mac: Closing all windows does not indicate quitting the application. Lie
76 // for now and ignore behavior outside of unit tests.
77 // ChromeOS: Force closing last window to close app with flag.
78 // TODO(andybons | pkotwicz): Fix the UI tests to Do The Right Thing.
79 #if defined(OS_CHROMEOS)
80 bool closing_app;
81 if (CommandLine::ForCurrentProcess()->HasSwitch(
82 switches::kDisableZeroBrowsersOpenForTests))
83 closing_app = (browsers_.size() == 1);
84 else
85 closing_app = (browsers_.size() == 1 &&
86 browser_shutdown::IsTryingToQuit());
87 #else
88 bool closing_app = (browsers_.size() == 1);
89 #endif // OS_CHROMEOS
90
91 content::NotificationService::current()->Notify(
92 chrome::NOTIFICATION_BROWSER_CLOSED,
93 content::Source<Browser>(browser),
94 content::Details<bool>(&closing_app));
95
96 RemoveBrowserFrom(browser, &browsers_);
97
98 // Do some basic checking to try to catch evil observers
99 // that change the list from under us.
100 size_t original_count = observers_.size();
101 FOR_EACH_OBSERVER(BrowserListObserver, observers_, OnBrowserRemoved(browser));
102 DCHECK_EQ(original_count, observers_.size())
103 << "observer list modified during notification";
104
105 g_browser_process->ReleaseModule();
106
107 // If we're exiting, send out the APP_TERMINATING notification to allow other
108 // modules to shut themselves down.
109 if (browsers_.empty() &&
110 (browser_shutdown::IsTryingToQuit() ||
111 g_browser_process->IsShuttingDown())) {
112 // Last browser has just closed, and this is a user-initiated quit or there
113 // is no module keeping the app alive, so send out our notification. No need
114 // to call ProfileManager::ShutdownSessionServices() as part of the
115 // shutdown, because Browser::WindowClosing() already makes sure that the
116 // SessionService is created and notified.
117 browser::NotifyAppTerminating();
118 browser::OnAppExiting();
119 }
120 }
121
122 void BrowserListImpl::AddObserver(BrowserListObserver* observer) {
123 observers_.AddObserver(observer);
124 }
125
126 void BrowserListImpl::RemoveObserver(BrowserListObserver* observer) {
127 observers_.RemoveObserver(observer);
128 }
129
130 void BrowserListImpl::SetLastActive(Browser* browser) {
131 // If the browser is currently trying to quit, we don't want to set the last
132 // active browser because that can alter the last active browser that the user
133 // intended depending on the order in which the windows close.
134 if (browser_shutdown::IsTryingToQuit())
135 return;
136 RemoveBrowserFrom(browser, &last_active_browsers_);
137 last_active_browsers_.push_back(browser);
138
139 FOR_EACH_OBSERVER(BrowserListObserver, observers_,
140 OnBrowserSetLastActive(browser));
141 }
142
143 Browser* BrowserListImpl::GetLastActive() {
144 if (!last_active_browsers_.empty())
145 return *(last_active_browsers_.rbegin());
146 return NULL;
147 }
148
149 void BrowserListImpl::CloseAllBrowsersWithProfile(Profile* profile) {
150 BrowserVector browsers_to_close;
151 for (BrowserListImpl::const_iterator i = BrowserListImpl::begin();
152 i != BrowserListImpl::end(); ++i) {
153 if ((*i)->profile()->GetOriginalProfile() == profile->GetOriginalProfile())
154 browsers_to_close.push_back(*i);
155 }
156
157 for (BrowserVector::const_iterator i = browsers_to_close.begin();
158 i != browsers_to_close.end(); ++i) {
159 (*i)->window()->Close();
160 }
161 }
162
163 BrowserListImpl::const_iterator BrowserListImpl::begin() {
164 return browsers_.begin();
165 }
166
167 BrowserListImpl::const_iterator BrowserListImpl::end() {
168 return browsers_.end();
169 }
170
171 bool BrowserListImpl::empty() {
172 return browsers_.empty();
173 }
174
175 size_t BrowserListImpl::size() {
176 return browsers_.size();
177 }
178
179 BrowserListImpl::const_reverse_iterator BrowserListImpl::begin_last_active() {
180 return last_active_browsers_.rbegin();
181 }
182
183 BrowserListImpl::const_reverse_iterator BrowserListImpl::end_last_active() {
184 return last_active_browsers_.rend();
185 }
186
187 bool BrowserListImpl::IsIncognitoWindowOpen() {
188 for (BrowserListImpl::const_iterator i = BrowserListImpl::begin();
189 i != BrowserListImpl::end(); ++i) {
190 if ((*i)->profile()->IsOffTheRecord())
191 return true;
192 }
193 return false;
194 }
195
196 bool BrowserListImpl::IsIncognitoWindowOpenForProfile(Profile* profile) {
197 #if defined(OS_CHROMEOS)
198 // In ChromeOS, we assume that the default profile is always valid, so if
199 // we are in guest mode, keep the OTR profile active so it won't be deleted.
200 if (chromeos::UserManager::Get()->IsLoggedInAsGuest())
201 return true;
202 #endif
203 for (BrowserListImpl::const_iterator i = BrowserListImpl::begin();
204 i != BrowserListImpl::end(); ++i) {
205 if ((*i)->profile()->IsSameProfile(profile) &&
206 (*i)->profile()->IsOffTheRecord()) {
207 return true;
208 }
209 }
210 return false;
211 }
212
213 ////////////////////////////////////////////////////////////////////////////////
214 // BrowserListImpl, private:
215
216 BrowserListImpl::BrowserListImpl() {
217 }
218
219 BrowserListImpl::~BrowserListImpl() {
220 }
221
222 void BrowserListImpl::RemoveBrowserFrom(Browser* browser,
223 BrowserVector* browser_list) {
224 const iterator remove_browser =
225 std::find(browser_list->begin(), browser_list->end(), browser);
226 if (remove_browser != browser_list->end())
227 browser_list->erase(remove_browser);
228 }
229
230 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698