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

Side by Side Diff: chrome/browser/memory/tab_manager.cc

Issue 1978343002: [TabManager] Start function refactoring. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
« no previous file with comments | « chrome/browser/memory/tab_manager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 TabManager::~TabManager() { 158 TabManager::~TabManager() {
159 Stop(); 159 Stop();
160 } 160 }
161 161
162 void TabManager::Start() { 162 void TabManager::Start() {
163 #if defined(OS_WIN) || defined(OS_MACOSX) 163 #if defined(OS_WIN) || defined(OS_MACOSX)
164 // If the feature is not enabled, do nothing. 164 // If the feature is not enabled, do nothing.
165 if (!base::FeatureList::IsEnabled(features::kAutomaticTabDiscarding)) 165 if (!base::FeatureList::IsEnabled(features::kAutomaticTabDiscarding))
166 return; 166 return;
167 167
168 // Check the variation parameter to see if a tab be discarded more than once.
169 // Default is to only discard once per tab.
170 std::string allow_multiple_discards = variations::GetVariationParamValue(
171 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards");
172 if (allow_multiple_discards == "true")
173 discard_once_ = false;
174 else
175 discard_once_ = true;
176
177 // Check the variation parameter to see if a tab is to be protected for an 168 // Check the variation parameter to see if a tab is to be protected for an
178 // amount of time after being backgrounded. The value is in seconds. 169 // amount of time after being backgrounded. The value is in seconds.
179 std::string minimum_protection_time_string = 170 std::string minimum_protection_time_string =
180 variations::GetVariationParamValue(features::kAutomaticTabDiscarding.name, 171 variations::GetVariationParamValue(features::kAutomaticTabDiscarding.name,
181 "MinimumProtectionTime"); 172 "MinimumProtectionTime");
182 if (!minimum_protection_time_string.empty()) { 173 if (!minimum_protection_time_string.empty()) {
183 unsigned int minimum_protection_time_seconds = 0; 174 unsigned int minimum_protection_time_seconds = 0;
184 if (base::StringToUint(minimum_protection_time_string, 175 if (base::StringToUint(minimum_protection_time_string,
185 &minimum_protection_time_seconds)) { 176 &minimum_protection_time_seconds)) {
186 if (minimum_protection_time_seconds > 0) 177 if (minimum_protection_time_seconds > 0)
187 minimum_protection_time_ = 178 minimum_protection_time_ =
188 base::TimeDelta::FromSeconds(minimum_protection_time_seconds); 179 base::TimeDelta::FromSeconds(minimum_protection_time_seconds);
189 } 180 }
190 } 181 }
182 #endif
191 183
192 #elif defined(OS_CHROMEOS) 184 // Check if only one discard is allowed.
193 // On Chrome OS, tab manager is always started and tabs can be discarded more 185 discard_once_ = CanOnlyDiscardOnce();
194 // than once.
195 discard_once_ = false;
196 #endif
197 186
198 if (!update_timer_.IsRunning()) { 187 if (!update_timer_.IsRunning()) {
199 update_timer_.Start(FROM_HERE, 188 update_timer_.Start(FROM_HERE,
200 TimeDelta::FromSeconds(kAdjustmentIntervalSeconds), 189 TimeDelta::FromSeconds(kAdjustmentIntervalSeconds),
201 this, &TabManager::UpdateTimerCallback); 190 this, &TabManager::UpdateTimerCallback);
202 } 191 }
203 192
204 // MemoryPressureMonitor is not implemented on Linux so far and tabs are never 193 // MemoryPressureMonitor is not implemented on Linux so far and tabs are never
205 // discarded. 194 // discarded.
206 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) 195 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime)) 543 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime))
555 return; 544 return;
556 int purge_and_suspend_time = 0; 545 int purge_and_suspend_time = 0;
557 if (!base::StringToInt( 546 if (!base::StringToInt(
558 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime), 547 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime),
559 &purge_and_suspend_time)) { 548 &purge_and_suspend_time)) {
560 return; 549 return;
561 } 550 }
562 if (purge_and_suspend_time <= 0) 551 if (purge_and_suspend_time <= 0)
563 return; 552 return;
564 auto purge_and_suspend_time_threshold = NowTicks() - 553 auto purge_and_suspend_time_threshold =
565 base::TimeDelta::FromSeconds(purge_and_suspend_time); 554 NowTicks() - base::TimeDelta::FromSeconds(purge_and_suspend_time);
566 auto tab_stats = GetUnsortedTabStats(); 555 auto tab_stats = GetUnsortedTabStats();
567 for (auto& tab : tab_stats) { 556 for (auto& tab : tab_stats) {
568 if (!tab.render_process_host->IsProcessBackgrounded()) 557 if (!tab.render_process_host->IsProcessBackgrounded())
569 continue; 558 continue;
570 // TODO(hajimehoshi): Now calling PurgeAndSuspend is implemented without 559 // TODO(hajimehoshi): Now calling PurgeAndSuspend is implemented without
571 // timers for simplicity, so PurgeAndSuspend is called even after the 560 // timers for simplicity, so PurgeAndSuspend is called even after the
572 // renderer is purged and suspended once. This should be replaced with 561 // renderer is purged and suspended once. This should be replaced with
573 // timers if we want necessary and sufficient signals. 562 // timers if we want necessary and sufficient signals.
574 if (tab.last_active > purge_and_suspend_time_threshold) 563 if (tab.last_active > purge_and_suspend_time_threshold)
575 continue; 564 continue;
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 for (TabStatsList::const_reverse_iterator stats_rit = stats.rbegin(); 840 for (TabStatsList::const_reverse_iterator stats_rit = stats.rbegin();
852 stats_rit != stats.rend(); ++stats_rit) { 841 stats_rit != stats.rend(); ++stats_rit) {
853 int64_t least_important_tab_id = stats_rit->tab_contents_id; 842 int64_t least_important_tab_id = stats_rit->tab_contents_id;
854 if (CanDiscardTab(least_important_tab_id) && 843 if (CanDiscardTab(least_important_tab_id) &&
855 DiscardTabById(least_important_tab_id)) 844 DiscardTabById(least_important_tab_id))
856 return true; 845 return true;
857 } 846 }
858 return false; 847 return false;
859 } 848 }
860 849
850 // Check the variation parameter to see if a tab can be discarded only once or
851 // multiple times.
852 // Default is to only discard once per tab.
853 bool TabManager::CanOnlyDiscardOnce() {
854 #if defined(OS_WIN) || defined(OS_MACOSX)
855 // On Windows and MacOS, default to discarding only once unless otherwise
856 // specified by the variation parameter.
857 // TODO(georgesak): add condition for linux when TabManager is supported.
Georges Khalil 2016/05/17 18:43:16 nit: change this comment to Add Linux when automat
Anderson Silva 2016/05/17 18:50:30 Acknowledged.
858 std::string allow_multiple_discards = variations::GetVariationParamValue(
859 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards");
860 return (allow_multiple_discards != "true");
861 #else
862 return false;
863 #endif
864 }
865
861 // Things to collect on the browser thread (because TabStripModel isn't thread 866 // Things to collect on the browser thread (because TabStripModel isn't thread
862 // safe): 867 // safe):
863 // 1) whether or not a tab is pinned 868 // 1) whether or not a tab is pinned
864 // 2) last time a tab was selected 869 // 2) last time a tab was selected
865 // 3) is the tab currently selected 870 // 3) is the tab currently selected
866 TabStatsList TabManager::GetUnsortedTabStats() { 871 TabStatsList TabManager::GetUnsortedTabStats() {
867 DCHECK_CURRENTLY_ON(BrowserThread::UI); 872 DCHECK_CURRENTLY_ON(BrowserThread::UI);
868 TabStatsList stats_list; 873 TabStatsList stats_list;
869 stats_list.reserve(32); // 99% of users have < 30 tabs open. 874 stats_list.reserve(32); // 99% of users have < 30 tabs open.
870 875
871 // TODO(chrisha): Move this code to a TabStripModel enumeration delegate! 876 // TODO(chrisha): Move this code to a TabStripModel enumeration delegate!
872 if (!test_tab_strip_models_.empty()) { 877 if (!test_tab_strip_models_.empty()) {
873 for (size_t i = 0; i < test_tab_strip_models_.size(); ++i) { 878 for (size_t i = 0; i < test_tab_strip_models_.size(); ++i) {
874 AddTabStats(test_tab_strip_models_[i].first, // tab_strip_model 879 AddTabStats(test_tab_strip_models_[i].first, // tab_strip_model
875 test_tab_strip_models_[i].second, // is_app 880 test_tab_strip_models_[i].second, // is_app
876 i == 0, // is_active 881 i == 0, // is_active
877 &stats_list); 882 &stats_list);
878 } 883 }
879 } else { 884 } else {
880 // The code here can only be tested under a full browser test. 885 // The code here can only be tested under a full browser test.
881 AddTabStats(&stats_list); 886 AddTabStats(&stats_list);
882 } 887 }
883 888
884 return stats_list; 889 return stats_list;
885 } 890 }
886 891
887 } // namespace memory 892 } // namespace memory
OLDNEW
« no previous file with comments | « chrome/browser/memory/tab_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698