Chromium Code Reviews| 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 // For each period of this length record a statistic to indicate whether or not | 77 // For each period of this length record a statistic to indicate whether or not |
| 78 // the user experienced a low memory event. If this interval is changed, | 78 // the user experienced a low memory event. If this interval is changed, |
| 79 // Tabs.Discard.DiscardInLastMinute must be replaced with a new statistic. | 79 // Tabs.Discard.DiscardInLastMinute must be replaced with a new statistic. |
| 80 const int kRecentTabDiscardIntervalSeconds = 60; | 80 const int kRecentTabDiscardIntervalSeconds = 60; |
| 81 #endif | 81 #endif |
| 82 | 82 |
| 83 // If there has been no priority adjustment in this interval, assume the | 83 // If there has been no priority adjustment in this interval, assume the |
| 84 // machine was suspended and correct the timing statistics. | 84 // machine was suspended and correct the timing statistics. |
| 85 const int kSuspendThresholdSeconds = kAdjustmentIntervalSeconds * 4; | 85 const int kSuspendThresholdSeconds = kAdjustmentIntervalSeconds * 4; |
| 86 | 86 |
| 87 // The backgrounded renderer is allowed to be suspended for this duration | |
| 88 // before resume. | |
| 89 constexpr base::TimeDelta kMaxTimeRendererAllowedToBeSuspendedBeforeResume = | |
| 90 base::TimeDelta::FromSeconds(120); | |
| 91 | |
| 92 // When the suspended backgrounded renderer is resumed, the renderer runs | |
| 93 // for this duration. | |
| 94 constexpr base::TimeDelta kSuspendedRendererLengthOfResumption = | |
| 95 base::TimeDelta::FromSeconds(10); | |
| 96 | |
| 87 // The time during which a tab is protected from discarding after it stops being | 97 // The time during which a tab is protected from discarding after it stops being |
| 88 // audible. | 98 // audible. |
| 89 const int kAudioProtectionTimeSeconds = 60; | 99 const int kAudioProtectionTimeSeconds = 60; |
| 90 | 100 |
| 91 int FindTabStripModelById(int64_t target_web_contents_id, | 101 int FindTabStripModelById(int64_t target_web_contents_id, |
| 92 TabStripModel** model) { | 102 TabStripModel** model) { |
| 93 DCHECK(model); | 103 DCHECK(model); |
| 94 for (auto* browser : *BrowserList::GetInstance()) { | 104 for (auto* browser : *BrowserList::GetInstance()) { |
| 95 TabStripModel* local_model = browser->tab_strip_model(); | 105 TabStripModel* local_model = browser->tab_strip_model(); |
| 96 for (int idx = 0; idx < local_model->count(); idx++) { | 106 for (int idx = 0; idx < local_model->count(); idx++) { |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 411 | 421 |
| 412 bool TabManager::IsTabAutoDiscardable(content::WebContents* contents) const { | 422 bool TabManager::IsTabAutoDiscardable(content::WebContents* contents) const { |
| 413 return GetWebContentsData(contents)->IsAutoDiscardable(); | 423 return GetWebContentsData(contents)->IsAutoDiscardable(); |
| 414 } | 424 } |
| 415 | 425 |
| 416 void TabManager::SetTabAutoDiscardableState(content::WebContents* contents, | 426 void TabManager::SetTabAutoDiscardableState(content::WebContents* contents, |
| 417 bool state) { | 427 bool state) { |
| 418 GetWebContentsData(contents)->SetAutoDiscardableState(state); | 428 GetWebContentsData(contents)->SetAutoDiscardableState(state); |
| 419 } | 429 } |
| 420 | 430 |
| 431 content::WebContents* TabManager::GetWebContentsByContentsId( | |
| 432 int64_t tab_contents_id) { | |
| 433 TabStripModel* model; | |
| 434 int index = FindTabStripModelById(tab_contents_id, &model); | |
| 435 if (index == -1) | |
| 436 return nullptr; | |
| 437 return model->GetWebContentsAt(index); | |
| 438 } | |
| 439 | |
| 421 bool TabManager::CanSuspendBackgroundedRenderer(int render_process_id) { | 440 bool TabManager::CanSuspendBackgroundedRenderer(int render_process_id) { |
| 422 // A renderer can be suspended if it's not playing media. | 441 // A renderer can be suspended if it's not playing media. |
| 423 auto tab_stats = GetUnsortedTabStats(); | 442 auto tab_stats = GetUnsortedTabStats(); |
| 424 for (auto& tab : tab_stats) { | 443 for (auto& tab : tab_stats) { |
| 425 if (tab.child_process_host_id != render_process_id) | 444 if (tab.child_process_host_id != render_process_id) |
| 426 continue; | 445 continue; |
| 427 TabStripModel* model; | 446 WebContents* web_contents = GetWebContentsByContentsId(tab.tab_contents_id); |
| 428 int index = FindTabStripModelById(tab.tab_contents_id, &model); | 447 if (!web_contents) |
| 429 if (index == -1) | |
| 430 return false; | 448 return false; |
| 431 WebContents* web_contents = model->GetWebContentsAt(index); | |
| 432 if (IsMediaTab(web_contents)) | 449 if (IsMediaTab(web_contents)) |
| 433 return false; | 450 return false; |
| 434 } | 451 } |
| 435 return true; | 452 return true; |
| 436 } | 453 } |
| 437 | 454 |
| 438 // static | 455 // static |
| 439 bool TabManager::CompareTabStats(const TabStats& first, | 456 bool TabManager::CompareTabStats(const TabStats& first, |
| 440 const TabStats& second) { | 457 const TabStats& second) { |
| 441 // Being currently selected is most important to protect. | 458 // Being currently selected is most important to protect. |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 680 | 697 |
| 681 #if defined(OS_CHROMEOS) | 698 #if defined(OS_CHROMEOS) |
| 682 TabStatsList stats_list = GetTabStats(); | 699 TabStatsList stats_list = GetTabStats(); |
| 683 // This starts the CrOS specific OOM adjustments in /proc/<pid>/oom_score_adj. | 700 // This starts the CrOS specific OOM adjustments in /proc/<pid>/oom_score_adj. |
| 684 delegate_->AdjustOomPriorities(stats_list); | 701 delegate_->AdjustOomPriorities(stats_list); |
| 685 #endif | 702 #endif |
| 686 | 703 |
| 687 PurgeAndSuspendBackgroundedTabs(); | 704 PurgeAndSuspendBackgroundedTabs(); |
| 688 } | 705 } |
| 689 | 706 |
| 707 bool TabManager::ShouldUpdatePurgeAndSuspendState( | |
| 708 const base::TimeTicks& current_time, | |
| 709 const TabStats& tab, | |
| 710 const base::TimeDelta& purge_and_suspend_threshold, | |
| 711 PurgeAndSuspendState* next_state) { | |
| 712 DCHECK(next_state); | |
| 713 content::WebContents* content = | |
| 714 GetWebContentsByContentsId(tab.tab_contents_id); | |
| 715 if (!content) | |
| 716 return false; | |
| 717 | |
| 718 base::TimeTicks last_modified_time = | |
| 719 GetWebContentsData(content)->LastPurgeAndSuspendModifiedTime(); | |
| 720 PurgeAndSuspendState state = | |
| 721 GetWebContentsData(content)->GetPurgeAndSuspendState(); | |
| 722 // If the tab was foregrounded, should ignore the returned state. | |
| 723 if (tab.last_active > last_modified_time) | |
|
haraken
2016/10/19 10:40:30
Instead of adding this if check, would it be possi
tasak
2016/10/20 11:28:46
I think, it is very difficult to capture such tran
tasak
2016/10/20 11:47:36
I found that it is possible to implement TabManag
tasak
2016/10/20 12:35:12
Done.
| |
| 724 state = BACKGROUNDED; | |
| 725 | |
| 726 auto time_passed = current_time - last_modified_time; | |
| 727 switch (state) { | |
| 728 case BACKGROUNDED: | |
| 729 if (time_passed > purge_and_suspend_threshold) { | |
| 730 *next_state = SUSPENDED; | |
| 731 return true; | |
| 732 } | |
| 733 break; | |
| 734 case RESUMED: | |
| 735 if (time_passed > kSuspendedRendererLengthOfResumption) { | |
| 736 *next_state = SUSPENDED; | |
| 737 return true; | |
| 738 } | |
| 739 break; | |
| 740 case SUSPENDED: | |
| 741 if (time_passed > kMaxTimeRendererAllowedToBeSuspendedBeforeResume) { | |
| 742 *next_state = RESUMED; | |
| 743 return true; | |
| 744 } | |
| 745 break; | |
| 746 } | |
| 747 return false; | |
| 748 } | |
| 749 | |
| 690 void TabManager::PurgeAndSuspendBackgroundedTabs() { | 750 void TabManager::PurgeAndSuspendBackgroundedTabs() { |
| 691 const base::CommandLine& command_line = | 751 const base::CommandLine& command_line = |
| 692 *base::CommandLine::ForCurrentProcess(); | 752 *base::CommandLine::ForCurrentProcess(); |
| 693 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime)) | 753 if (!command_line.HasSwitch(switches::kPurgeAndSuspendTime)) |
| 694 return; | 754 return; |
| 695 int purge_and_suspend_time = 0; | 755 int purge_and_suspend_time = 0; |
| 696 if (!base::StringToInt( | 756 if (!base::StringToInt( |
| 697 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime), | 757 command_line.GetSwitchValueASCII(switches::kPurgeAndSuspendTime), |
| 698 &purge_and_suspend_time)) { | 758 &purge_and_suspend_time)) { |
| 699 return; | 759 return; |
| 700 } | 760 } |
| 701 if (purge_and_suspend_time <= 0) | 761 if (purge_and_suspend_time <= 0) |
| 702 return; | 762 return; |
| 703 auto purge_and_suspend_time_threshold = | 763 base::TimeTicks current_time = NowTicks(); |
| 704 NowTicks() - base::TimeDelta::FromSeconds(purge_and_suspend_time); | 764 base::TimeDelta purge_and_suspend_threshold = |
| 765 base::TimeDelta::FromSeconds(purge_and_suspend_time); | |
| 705 auto tab_stats = GetUnsortedTabStats(); | 766 auto tab_stats = GetUnsortedTabStats(); |
| 706 for (auto& tab : tab_stats) { | 767 for (auto& tab : tab_stats) { |
| 707 if (!tab.render_process_host->IsProcessBackgrounded()) | 768 if (!tab.render_process_host->IsProcessBackgrounded()) |
| 708 continue; | 769 continue; |
| 770 if (!CanSuspendBackgroundedRenderer(tab.child_process_host_id)) | |
| 771 continue; | |
| 772 | |
| 709 // TODO(hajimehoshi): Now calling PurgeAndSuspend is implemented without | 773 // TODO(hajimehoshi): Now calling PurgeAndSuspend is implemented without |
| 710 // timers for simplicity, so PurgeAndSuspend is called even after the | 774 // timers for simplicity, so PurgeAndSuspend is called even after the |
| 711 // renderer is purged and suspended once. This should be replaced with | 775 // renderer is purged and suspended once. This should be replaced with |
| 712 // timers if we want necessary and sufficient signals. | 776 // timers if we want necessary and sufficient signals. |
| 713 if (tab.last_active > purge_and_suspend_time_threshold) | 777 PurgeAndSuspendState state = BACKGROUNDED; |
| 778 if (!ShouldUpdatePurgeAndSuspendState(current_time, tab, | |
| 779 purge_and_suspend_threshold, &state)) | |
| 714 continue; | 780 continue; |
| 715 if (!CanSuspendBackgroundedRenderer(tab.child_process_host_id)) | 781 WebContents* content = GetWebContentsByContentsId(tab.tab_contents_id); |
| 716 continue; | 782 GetWebContentsData(content)->SetPurgeAndSuspendState(state); |
| 717 tab.render_process_host->PurgeAndSuspend(); | 783 switch (state) { |
| 784 case SUSPENDED: | |
| 785 tab.render_process_host->PurgeAndSuspend(); | |
| 786 break; | |
| 787 case RESUMED: | |
| 788 tab.render_process_host->Resume(); | |
| 789 break; | |
| 790 case BACKGROUNDED: | |
| 791 NOTREACHED(); | |
| 792 } | |
| 718 } | 793 } |
| 719 } | 794 } |
| 720 | 795 |
| 721 WebContents* TabManager::DiscardWebContentsAt(int index, TabStripModel* model) { | 796 WebContents* TabManager::DiscardWebContentsAt(int index, TabStripModel* model) { |
| 722 // Can't discard active index. | 797 // Can't discard active index. |
| 723 if (model->active_index() == index) | 798 if (model->active_index() == index) |
| 724 return nullptr; | 799 return nullptr; |
| 725 | 800 |
| 726 WebContents* old_contents = model->GetWebContentsAt(index); | 801 WebContents* old_contents = model->GetWebContentsAt(index); |
| 727 | 802 |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 959 // platform. | 1034 // platform. |
| 960 std::string allow_multiple_discards = variations::GetVariationParamValue( | 1035 std::string allow_multiple_discards = variations::GetVariationParamValue( |
| 961 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); | 1036 features::kAutomaticTabDiscarding.name, "AllowMultipleDiscards"); |
| 962 return (allow_multiple_discards != "true"); | 1037 return (allow_multiple_discards != "true"); |
| 963 #else | 1038 #else |
| 964 return false; | 1039 return false; |
| 965 #endif | 1040 #endif |
| 966 } | 1041 } |
| 967 | 1042 |
| 968 } // namespace memory | 1043 } // namespace memory |
| OLD | NEW |