OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "content/browser/memory/memory_coordinator_default_policy.h" | 5 #include "content/browser/memory/memory_coordinator_default_policy.h" |
6 | 6 |
7 namespace content { | 7 namespace content { |
8 | 8 |
9 namespace { | |
10 | |
11 const int kDefaultBackgroundChildPurgeCandidatePeriodSeconds = 30; | |
12 | |
13 MemoryState CalculateMemoryStateForProcess(MemoryCondition condition) { | |
14 if (condition == MemoryCondition::CRITICAL) | |
15 return MemoryState::THROTTLED; | |
16 return MemoryState::NORMAL; | |
17 } | |
18 | |
19 } // namespace | |
20 | |
21 MemoryCoordinatorDefaultPolicy::MemoryCoordinatorDefaultPolicy( | 9 MemoryCoordinatorDefaultPolicy::MemoryCoordinatorDefaultPolicy( |
22 MemoryCoordinatorImpl* coordinator) | 10 MemoryCoordinatorImpl* coordinator) |
23 : coordinator_(coordinator) { | 11 : coordinator_(coordinator) { |
24 DCHECK(coordinator_); | 12 DCHECK(coordinator_); |
25 } | 13 } |
26 | 14 |
27 MemoryCoordinatorDefaultPolicy::~MemoryCoordinatorDefaultPolicy() {} | 15 MemoryCoordinatorDefaultPolicy::~MemoryCoordinatorDefaultPolicy() {} |
28 | 16 |
29 void MemoryCoordinatorDefaultPolicy::OnCriticalCondition() { | 17 void MemoryCoordinatorDefaultPolicy::OnCriticalCondition() { |
| 18 // Just trigger tab discarding for now. |
30 coordinator_->DiscardTab(); | 19 coordinator_->DiscardTab(); |
31 | |
32 // Prefer to purge memory from child processes than browser process because | |
33 // we prioritize the brower process. | |
34 if (TryToPurgeMemoryFromChildren(PurgeTarget::ALL)) | |
35 return; | |
36 | |
37 coordinator_->TryToPurgeMemoryFromBrowser(); | |
38 } | 20 } |
39 | 21 |
40 void MemoryCoordinatorDefaultPolicy::OnConditionChanged(MemoryCondition prev, | 22 void MemoryCoordinatorDefaultPolicy::OnConditionChanged(MemoryCondition prev, |
41 MemoryCondition next) { | 23 MemoryCondition next) {} |
42 DCHECK(prev != next); | |
43 if (next == MemoryCondition::NORMAL) { | |
44 // Set NORMAL state to all clients/processes. | |
45 coordinator_->SetBrowserMemoryState(MemoryState::NORMAL); | |
46 SetMemoryStateToAllChildren(MemoryState::NORMAL); | |
47 } else if (next == MemoryCondition::CRITICAL) { | |
48 // Set THROTTLED state to all clients/processes. | |
49 coordinator_->SetBrowserMemoryState(MemoryState::THROTTLED); | |
50 SetMemoryStateToAllChildren(MemoryState::THROTTLED); | |
51 } | |
52 } | |
53 | 24 |
54 void MemoryCoordinatorDefaultPolicy::OnChildVisibilityChanged( | 25 void MemoryCoordinatorDefaultPolicy::OnChildVisibilityChanged( |
55 int render_process_id, | 26 int render_process_id, |
56 bool is_visible) { | 27 bool is_visible) {} |
57 auto iter = coordinator_->children().find(render_process_id); | |
58 if (iter == coordinator_->children().end()) | |
59 return; | |
60 | |
61 iter->second.is_visible = is_visible; | |
62 if (!is_visible) { | |
63 // A backgrounded process becomes a candidate for purging memory when | |
64 // the process remains backgrounded for a certian period of time. | |
65 iter->second.can_purge_after = | |
66 coordinator_->NowTicks() + | |
67 base::TimeDelta::FromSeconds( | |
68 kDefaultBackgroundChildPurgeCandidatePeriodSeconds); | |
69 } | |
70 MemoryState new_state = | |
71 CalculateMemoryStateForProcess(coordinator_->GetMemoryCondition()); | |
72 coordinator_->SetChildMemoryState(render_process_id, new_state); | |
73 } | |
74 | |
75 void MemoryCoordinatorDefaultPolicy::SetMemoryStateToAllChildren( | |
76 MemoryState state) { | |
77 // It's OK to call SetChildMemoryState() unconditionally because it checks | |
78 // whether this state transition is valid. | |
79 for (auto& iter : coordinator_->children()) | |
80 coordinator_->SetChildMemoryState(iter.first, state); | |
81 } | |
82 | |
83 bool MemoryCoordinatorDefaultPolicy::TryToPurgeMemoryFromChildren( | |
84 PurgeTarget target) { | |
85 // TODO(bashi): Better to sort child processes based on their priorities. | |
86 for (auto& iter : coordinator_->children()) { | |
87 if (iter.second.is_visible && target == PurgeTarget::BACKGROUNDED) | |
88 continue; | |
89 if (coordinator_->TryToPurgeMemoryFromChild(iter.first)) | |
90 return true; | |
91 } | |
92 return false; | |
93 } | |
94 | 28 |
95 } // namespace content | 29 } // namespace content |
OLD | NEW |