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 <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 | 94 |
95 return -1; | 95 return -1; |
96 } | 96 } |
97 | 97 |
98 } // namespace | 98 } // namespace |
99 | 99 |
100 //////////////////////////////////////////////////////////////////////////////// | 100 //////////////////////////////////////////////////////////////////////////////// |
101 // TabManager | 101 // TabManager |
102 | 102 |
103 TabManager::TabManager() | 103 TabManager::TabManager() |
104 : discard_count_(0), recent_tab_discard_(false), discard_once_(false) { | 104 : discard_count_(0), |
| 105 recent_tab_discard_(false), |
| 106 discard_once_(false), |
| 107 browser_tab_strip_tracker_(this, nullptr, nullptr) { |
105 #if defined(OS_CHROMEOS) | 108 #if defined(OS_CHROMEOS) |
106 delegate_.reset(new TabManagerDelegate); | 109 delegate_.reset(new TabManagerDelegate); |
107 #endif | 110 #endif |
108 BrowserList::AddObserver(this); | 111 browser_tab_strip_tracker_.Init( |
| 112 BrowserTabStripTracker::InitWith::ALL_BROWERS); |
109 } | 113 } |
110 | 114 |
111 TabManager::~TabManager() { | 115 TabManager::~TabManager() { |
112 Stop(); | 116 Stop(); |
113 for (chrome::BrowserIterator iterator; !iterator.done(); iterator.Next()) | |
114 iterator->tab_strip_model()->RemoveObserver(this); | |
115 | |
116 BrowserList::RemoveObserver(this); | |
117 } | 117 } |
118 | 118 |
119 void TabManager::Start(bool discard_once) { | 119 void TabManager::Start(bool discard_once) { |
120 discard_once_ = discard_once; | 120 discard_once_ = discard_once; |
121 if (!update_timer_.IsRunning()) { | 121 if (!update_timer_.IsRunning()) { |
122 update_timer_.Start(FROM_HERE, | 122 update_timer_.Start(FROM_HERE, |
123 TimeDelta::FromSeconds(kAdjustmentIntervalSeconds), | 123 TimeDelta::FromSeconds(kAdjustmentIntervalSeconds), |
124 this, &TabManager::UpdateTimerCallback); | 124 this, &TabManager::UpdateTimerCallback); |
125 } | 125 } |
126 if (!recent_tab_discard_timer_.IsRunning()) { | 126 if (!recent_tab_discard_timer_.IsRunning()) { |
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 | 468 |
469 // For the moment we only do something when we reach a critical state. | 469 // For the moment we only do something when we reach a critical state. |
470 if (memory_pressure_level == | 470 if (memory_pressure_level == |
471 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { | 471 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
472 LogMemoryAndDiscardTab(); | 472 LogMemoryAndDiscardTab(); |
473 } | 473 } |
474 // TODO(skuhne): If more memory pressure levels are introduced, we might | 474 // TODO(skuhne): If more memory pressure levels are introduced, we might |
475 // consider to call PurgeBrowserMemory() before CRITICAL is reached. | 475 // consider to call PurgeBrowserMemory() before CRITICAL is reached. |
476 } | 476 } |
477 | 477 |
478 void TabManager::OnBrowserAdded(Browser* browser) { | |
479 browser->tab_strip_model()->AddObserver(this); | |
480 } | |
481 | |
482 void TabManager::OnBrowserRemoved(Browser* browser) { | |
483 browser->tab_strip_model()->RemoveObserver(this); | |
484 } | |
485 | |
486 bool TabManager::IsAudioTab(WebContents* contents) const { | 478 bool TabManager::IsAudioTab(WebContents* contents) const { |
487 if (contents->WasRecentlyAudible()) | 479 if (contents->WasRecentlyAudible()) |
488 return true; | 480 return true; |
489 auto delta = | 481 auto delta = |
490 TimeTicks::Now() - memory::TabDiscardState::LastAudioChangeTime(contents); | 482 TimeTicks::Now() - memory::TabDiscardState::LastAudioChangeTime(contents); |
491 return delta < TimeDelta::FromSeconds(kAudioProtectionTimeSeconds); | 483 return delta < TimeDelta::FromSeconds(kAudioProtectionTimeSeconds); |
492 } | 484 } |
493 | 485 |
494 // static | 486 // static |
495 bool TabManager::CompareTabStats(TabStats first, TabStats second) { | 487 bool TabManager::CompareTabStats(TabStats first, TabStats second) { |
(...skipping 29 matching lines...) Expand all Loading... |
525 // sudden_termination_allowed false, and that covers too many common pages | 517 // sudden_termination_allowed false, and that covers too many common pages |
526 // with ad networks and statistics scripts. Ideally we would like to check | 518 // with ad networks and statistics scripts. Ideally we would like to check |
527 // for beforeUnload handlers, which are likely to present a dialog asking | 519 // for beforeUnload handlers, which are likely to present a dialog asking |
528 // if the user wants to discard state. crbug.com/123049 | 520 // if the user wants to discard state. crbug.com/123049 |
529 | 521 |
530 // Being more recently active is more important. | 522 // Being more recently active is more important. |
531 return first.last_active > second.last_active; | 523 return first.last_active > second.last_active; |
532 } | 524 } |
533 | 525 |
534 } // namespace memory | 526 } // namespace memory |
OLD | NEW |