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 #include "chrome/browser/memory/tab_manager.h" | 5 #include "chrome/browser/memory/tab_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <set> | 10 #include <set> |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 // Things to collect on the browser thread (because TabStripModel isn't thread | 198 // Things to collect on the browser thread (because TabStripModel isn't thread |
199 // safe): | 199 // safe): |
200 // 1) whether or not a tab is pinned | 200 // 1) whether or not a tab is pinned |
201 // 2) last time a tab was selected | 201 // 2) last time a tab was selected |
202 // 3) is the tab currently selected | 202 // 3) is the tab currently selected |
203 TabStatsList TabManager::GetTabStats() { | 203 TabStatsList TabManager::GetTabStats() { |
204 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 204 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
205 TabStatsList stats_list; | 205 TabStatsList stats_list; |
206 stats_list.reserve(32); // 99% of users have < 30 tabs open | 206 stats_list.reserve(32); // 99% of users have < 30 tabs open |
207 | 207 |
208 // Go through each window to get all the tabs. Depending on the platform, | 208 // Go through each window to get all the tabs. |
209 // windows are either native or ash or both. The goal is to make sure to go | 209 AddTabStats(&stats_list); |
210 // through them all, starting with the active window first (use | |
211 // chrome::GetActiveDesktop to get the current used type). | |
212 AddTabStats(BrowserList::GetInstance(chrome::GetActiveDesktop()), true, | |
213 &stats_list); | |
214 if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_NATIVE) { | |
215 AddTabStats(BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE), | |
216 false, &stats_list); | |
217 } else if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH) { | |
218 AddTabStats(BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH), false, | |
219 &stats_list); | |
220 } | |
221 | 210 |
222 // Sort the collected data so that least desirable to be killed is first, most | 211 // Sort the collected data so that least desirable to be killed is first, most |
223 // desirable is last. | 212 // desirable is last. |
224 std::sort(stats_list.begin(), stats_list.end(), CompareTabStats); | 213 std::sort(stats_list.begin(), stats_list.end(), CompareTabStats); |
225 return stats_list; | 214 return stats_list; |
226 } | 215 } |
227 | 216 |
228 bool TabManager::IsTabDiscarded(content::WebContents* contents) const { | 217 bool TabManager::IsTabDiscarded(content::WebContents* contents) const { |
229 return GetWebContentsData(contents)->IsDiscarded(); | 218 return GetWebContentsData(contents)->IsDiscarded(); |
230 } | 219 } |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 } | 391 } |
403 } | 392 } |
404 | 393 |
405 int TabManager::GetTabCount() const { | 394 int TabManager::GetTabCount() const { |
406 int tab_count = 0; | 395 int tab_count = 0; |
407 for (chrome::BrowserIterator it; !it.done(); it.Next()) | 396 for (chrome::BrowserIterator it; !it.done(); it.Next()) |
408 tab_count += it->tab_strip_model()->count(); | 397 tab_count += it->tab_strip_model()->count(); |
409 return tab_count; | 398 return tab_count; |
410 } | 399 } |
411 | 400 |
412 void TabManager::AddTabStats(BrowserList* browser_list, | 401 void TabManager::AddTabStats(TabStatsList* stats_list) { |
413 bool active_desktop, | 402 BrowserList* browser_list = BrowserList::GetInstance(); |
414 TabStatsList* stats_list) { | 403 // The first window will be the active one. |
415 // If it's the active desktop, the first window will be the active one. | 404 bool browser_active = true; |
416 // Otherwise, assume no active windows. | |
417 bool browser_active = active_desktop; | |
418 for (BrowserList::const_reverse_iterator browser_iterator = | 405 for (BrowserList::const_reverse_iterator browser_iterator = |
419 browser_list->begin_last_active(); | 406 browser_list->begin_last_active(); |
420 browser_iterator != browser_list->end_last_active(); | 407 browser_iterator != browser_list->end_last_active(); |
421 ++browser_iterator) { | 408 ++browser_iterator) { |
422 Browser* browser = *browser_iterator; | 409 Browser* browser = *browser_iterator; |
423 bool is_browser_for_app = browser->is_app(); | 410 bool is_browser_for_app = browser->is_app(); |
424 const TabStripModel* model = browser->tab_strip_model(); | 411 const TabStripModel* model = browser->tab_strip_model(); |
425 for (int i = 0; i < model->count(); i++) { | 412 for (int i = 0; i < model->count(); i++) { |
426 WebContents* contents = model->GetWebContentsAt(i); | 413 WebContents* contents = model->GetWebContentsAt(i); |
427 if (!contents->IsCrashed()) { | 414 if (!contents->IsCrashed()) { |
(...skipping 25 matching lines...) Expand all Loading... |
453 } | 440 } |
454 | 441 |
455 // This function is called when |update_timer_| fires. It will adjust the clock | 442 // This function is called when |update_timer_| fires. It will adjust the clock |
456 // if needed (if it detects that the machine was asleep) and will fire the stats | 443 // if needed (if it detects that the machine was asleep) and will fire the stats |
457 // updating on ChromeOS via the delegate. | 444 // updating on ChromeOS via the delegate. |
458 void TabManager::UpdateTimerCallback() { | 445 void TabManager::UpdateTimerCallback() { |
459 // If Chrome is shutting down, do not do anything. | 446 // If Chrome is shutting down, do not do anything. |
460 if (g_browser_process->IsShuttingDown()) | 447 if (g_browser_process->IsShuttingDown()) |
461 return; | 448 return; |
462 | 449 |
463 if (BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH)->empty() && | 450 if (BrowserList::GetInstance()->empty()) |
464 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE)->empty()) | |
465 return; | 451 return; |
466 | 452 |
467 // Check for a discontinuity in time caused by the machine being suspended. | 453 // Check for a discontinuity in time caused by the machine being suspended. |
468 if (!last_adjust_time_.is_null()) { | 454 if (!last_adjust_time_.is_null()) { |
469 TimeDelta suspend_time = NowTicks() - last_adjust_time_; | 455 TimeDelta suspend_time = NowTicks() - last_adjust_time_; |
470 if (suspend_time.InSeconds() > kSuspendThresholdSeconds) { | 456 if (suspend_time.InSeconds() > kSuspendThresholdSeconds) { |
471 // System was probably suspended, move the event timers forward in time so | 457 // System was probably suspended, move the event timers forward in time so |
472 // when they get subtracted out later, "uptime" is being counted. | 458 // when they get subtracted out later, "uptime" is being counted. |
473 start_time_ += suspend_time; | 459 start_time_ += suspend_time; |
474 if (!last_discard_time_.is_null()) | 460 if (!last_discard_time_.is_null()) |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 } | 648 } |
663 | 649 |
664 TimeTicks TabManager::NowTicks() const { | 650 TimeTicks TabManager::NowTicks() const { |
665 if (!test_tick_clock_) | 651 if (!test_tick_clock_) |
666 return TimeTicks::Now(); | 652 return TimeTicks::Now(); |
667 | 653 |
668 return test_tick_clock_->NowTicks(); | 654 return test_tick_clock_->NowTicks(); |
669 } | 655 } |
670 | 656 |
671 } // namespace memory | 657 } // namespace memory |
OLD | NEW |