OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "chrome/browser/browser_list.h" | 5 #include "chrome/browser/browser_list.h" |
6 | 6 |
7 #include "base/histogram.h" | 7 #include "base/histogram.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountPerLoad", tab_count, 1, 200, 50); | 72 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountPerLoad", tab_count, 1, 200, 50); |
73 } | 73 } |
74 | 74 |
75 NotificationRegistrar registrar_; | 75 NotificationRegistrar registrar_; |
76 | 76 |
77 DISALLOW_COPY_AND_ASSIGN(BrowserActivityObserver); | 77 DISALLOW_COPY_AND_ASSIGN(BrowserActivityObserver); |
78 }; | 78 }; |
79 | 79 |
80 BrowserActivityObserver* activity_observer = NULL; | 80 BrowserActivityObserver* activity_observer = NULL; |
81 | 81 |
| 82 // Returns true if the specified |browser| has a matching profile and type to |
| 83 // those specified. |type| can also be TYPE_ANY, which means only |profile| |
| 84 // must be matched. |
| 85 bool BrowserMatchesProfileAndType(Browser* browser, |
| 86 Profile* profile, |
| 87 Browser::Type type) { |
| 88 return (type == Browser::TYPE_ANY || browser->type() == type) && |
| 89 browser->profile() == profile; |
| 90 } |
| 91 |
| 92 // Finds a registered Browser object matching |profile| and |type|. This |
| 93 // walks the list of Browsers that have ever been activated from most recently |
| 94 // activated to least. If a Browser has never been activated, such as in a test |
| 95 // scenario, this function will _not_ find it. Fall back to |
| 96 // FindBrowserMatching() in that case. |
| 97 Browser* FindInLastActiveMatching(Profile* profile, Browser::Type type) { |
| 98 BrowserList::list_type::const_reverse_iterator browser = |
| 99 BrowserList::begin_last_active(); |
| 100 for (; browser != BrowserList::end_last_active(); ++browser) { |
| 101 if (BrowserMatchesProfileAndType(*browser, profile, type)) |
| 102 return *browser; |
| 103 } |
| 104 return NULL; |
| 105 } |
| 106 |
| 107 // Finds a registered Browser object matching |profile| and |type| even if that |
| 108 // Browser has never been activated. This is a forward walk, and intended as a |
| 109 // last ditch fallback mostly to handle tests run on machines where no window is |
| 110 // ever activated. The user experience if this function is relied on is not good |
| 111 // since matching browsers will be returned in registration (creation) order. |
| 112 Browser* FindBrowserMatching(Profile* profile, Browser::Type type) { |
| 113 BrowserList::const_iterator browser = BrowserList::begin(); |
| 114 for (; browser != BrowserList::end(); ++browser) { |
| 115 if (BrowserMatchesProfileAndType(*browser, profile, type)) |
| 116 return *browser; |
| 117 } |
| 118 return NULL; |
| 119 } |
| 120 |
82 } // namespace | 121 } // namespace |
83 | 122 |
84 BrowserList::list_type BrowserList::browsers_; | 123 BrowserList::list_type BrowserList::browsers_; |
85 std::vector<BrowserList::Observer*> BrowserList::observers_; | 124 std::vector<BrowserList::Observer*> BrowserList::observers_; |
86 | 125 |
87 // static | 126 // static |
88 void BrowserList::AddBrowser(Browser* browser) { | 127 void BrowserList::AddBrowser(Browser* browser) { |
89 DCHECK(browser); | 128 DCHECK(browser); |
90 browsers_.push_back(browser); | 129 browsers_.push_back(browser); |
91 | 130 |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 ExitProcess(ResultCodes::NORMAL_EXIT); | 274 ExitProcess(ResultCodes::NORMAL_EXIT); |
236 #else | 275 #else |
237 NOTIMPLEMENTED(); | 276 NOTIMPLEMENTED(); |
238 #endif | 277 #endif |
239 } | 278 } |
240 | 279 |
241 // static | 280 // static |
242 bool BrowserList::HasBrowserWithProfile(Profile* profile) { | 281 bool BrowserList::HasBrowserWithProfile(Profile* profile) { |
243 BrowserList::const_iterator iter; | 282 BrowserList::const_iterator iter; |
244 for (size_t i = 0; i < browsers_.size(); ++i) { | 283 for (size_t i = 0; i < browsers_.size(); ++i) { |
245 if (browsers_[i]->profile() == profile) | 284 if (BrowserMatchesProfileAndType(browsers_[i], profile, Browser::TYPE_ANY)) |
246 return true; | 285 return true; |
247 } | 286 } |
248 return false; | 287 return false; |
249 } | 288 } |
250 | 289 |
251 // static | 290 // static |
252 BrowserList::list_type BrowserList::last_active_browsers_; | 291 BrowserList::list_type BrowserList::last_active_browsers_; |
253 | 292 |
254 // static | 293 // static |
255 void BrowserList::SetLastActive(Browser* browser) { | 294 void BrowserList::SetLastActive(Browser* browser) { |
256 RemoveBrowserFrom(browser, &last_active_browsers_); | 295 RemoveBrowserFrom(browser, &last_active_browsers_); |
257 last_active_browsers_.push_back(browser); | 296 last_active_browsers_.push_back(browser); |
258 | 297 |
259 for (int i = 0; i < static_cast<int>(observers_.size()); i++) | 298 for (int i = 0; i < static_cast<int>(observers_.size()); i++) |
260 observers_[i]->OnBrowserSetLastActive(browser); | 299 observers_[i]->OnBrowserSetLastActive(browser); |
261 } | 300 } |
262 | 301 |
263 // static | 302 // static |
264 Browser* BrowserList::GetLastActive() { | 303 Browser* BrowserList::GetLastActive() { |
265 if (!last_active_browsers_.empty()) | 304 if (!last_active_browsers_.empty()) |
266 return *(last_active_browsers_.rbegin()); | 305 return *(last_active_browsers_.rbegin()); |
267 | 306 |
268 return NULL; | 307 return NULL; |
269 } | 308 } |
270 | 309 |
271 // static | 310 // static |
272 Browser* BrowserList::GetLastActiveWithProfile(Profile* p) { | 311 Browser* BrowserList::GetLastActiveWithProfile(Profile* p) { |
273 list_type::reverse_iterator browser = last_active_browsers_.rbegin(); | 312 // We are only interested in last active browsers, so we don't fall back to al
l |
274 for (; browser != last_active_browsers_.rend(); ++browser) { | 313 // browsers like FindBrowserWith* do. |
275 if ((*browser)->profile() == p) { | 314 return FindInLastActiveMatching(p, Browser::TYPE_ANY); |
276 return *browser; | |
277 } | |
278 } | |
279 | |
280 return NULL; | |
281 } | 315 } |
282 | 316 |
283 // static | 317 // static |
284 Browser* BrowserList::FindBrowserWithType(Profile* p, Browser::Type t) { | 318 Browser* BrowserList::FindBrowserWithType(Profile* p, Browser::Type t) { |
285 Browser* last_active = GetLastActive(); | 319 Browser* browser = FindInLastActiveMatching(p, t); |
286 if (last_active && last_active->profile() == p && last_active->type() == t) | 320 // Fall back to a forward scan of all Browsers if no active one was found. |
287 return last_active; | 321 return browser ? browser : FindBrowserMatching(p, t); |
288 | |
289 BrowserList::const_iterator i; | |
290 for (i = BrowserList::begin(); i != BrowserList::end(); ++i) { | |
291 if (*i == last_active) | |
292 continue; | |
293 | |
294 if ((*i)->profile() == p && (*i)->type() == t) | |
295 return *i; | |
296 } | |
297 return NULL; | |
298 } | 322 } |
299 | 323 |
300 // static | 324 // static |
301 Browser* BrowserList::FindBrowserWithProfile(Profile* p) { | 325 Browser* BrowserList::FindBrowserWithProfile(Profile* p) { |
302 Browser* last_active = GetLastActive(); | 326 Browser* browser = FindInLastActiveMatching(p, Browser::TYPE_ANY); |
303 if (last_active && last_active->profile() == p) | 327 // Fall back to a forward scan of all Browsers if no active one was found. |
304 return last_active; | 328 return browser ? browser : FindBrowserMatching(p, Browser::TYPE_ANY); |
305 | |
306 BrowserList::const_iterator i; | |
307 for (i = BrowserList::begin(); i != BrowserList::end(); ++i) { | |
308 if (*i == last_active) | |
309 continue; | |
310 | |
311 if ((*i)->profile() == p) | |
312 return *i; | |
313 } | |
314 return NULL; | |
315 } | 329 } |
316 | 330 |
317 // static | 331 // static |
318 Browser* BrowserList::FindBrowserWithID(SessionID::id_type desired_id) { | 332 Browser* BrowserList::FindBrowserWithID(SessionID::id_type desired_id) { |
319 BrowserList::const_iterator i; | 333 BrowserList::const_iterator i; |
320 for (i = BrowserList::begin(); i != BrowserList::end(); ++i) { | 334 for (i = BrowserList::begin(); i != BrowserList::end(); ++i) { |
321 if ((*i)->session_id().id() == desired_id) | 335 if ((*i)->session_id().id() == desired_id) |
322 return *i; | 336 return *i; |
323 } | 337 } |
324 return NULL; | 338 return NULL; |
325 } | 339 } |
326 | 340 |
327 // static | 341 // static |
328 size_t BrowserList::GetBrowserCountForType(Profile* p, Browser::Type type) { | 342 size_t BrowserList::GetBrowserCountForType(Profile* p, Browser::Type type) { |
329 BrowserList::const_iterator i; | 343 BrowserList::const_iterator i; |
330 size_t result = 0; | 344 size_t result = 0; |
331 for (i = BrowserList::begin(); i != BrowserList::end(); ++i) { | 345 for (i = BrowserList::begin(); i != BrowserList::end(); ++i) { |
332 if ((*i)->profile() == p && (*i)->type() == type) | 346 if (BrowserMatchesProfileAndType(*i, p, type)) |
333 result++; | 347 ++result; |
334 } | 348 } |
335 return result; | 349 return result; |
336 } | 350 } |
337 | 351 |
338 // static | 352 // static |
339 size_t BrowserList::GetBrowserCount(Profile* p) { | 353 size_t BrowserList::GetBrowserCount(Profile* p) { |
340 BrowserList::const_iterator i; | 354 BrowserList::const_iterator i; |
341 size_t result = 0; | 355 size_t result = 0; |
342 for (i = BrowserList::begin(); i != BrowserList::end(); ++i) { | 356 for (i = BrowserList::begin(); i != BrowserList::end(); ++i) { |
343 if ((*i)->profile() == p) | 357 if (BrowserMatchesProfileAndType(*i, p, Browser::TYPE_ANY)) |
344 result++; | 358 result++; |
345 } | 359 } |
346 return result; | 360 return result; |
347 } | 361 } |
348 | 362 |
349 // static | 363 // static |
350 bool BrowserList::IsOffTheRecordSessionActive() { | 364 bool BrowserList::IsOffTheRecordSessionActive() { |
351 BrowserList::const_iterator i; | 365 BrowserList::const_iterator i; |
352 for (i = BrowserList::begin(); i != BrowserList::end(); ++i) { | 366 for (i = BrowserList::begin(); i != BrowserList::end(); ++i) { |
353 if ((*i)->profile()->IsOffTheRecord()) | 367 if ((*i)->profile()->IsOffTheRecord()) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 } | 406 } |
393 | 407 |
394 TabContents* next_tab = | 408 TabContents* next_tab = |
395 (*browser_iterator_)->GetTabContentsAt(web_view_index_); | 409 (*browser_iterator_)->GetTabContentsAt(web_view_index_); |
396 if (next_tab) { | 410 if (next_tab) { |
397 cur_ = next_tab; | 411 cur_ = next_tab; |
398 return; | 412 return; |
399 } | 413 } |
400 } | 414 } |
401 } | 415 } |
OLD | NEW |