Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / renderer_host / renderer_frame_manager.cc
blob6f3df4c1d4b3ec199dc56a3cb603f0b52ecdf23e
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/renderer_frame_manager.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/memory/memory_pressure_listener.h"
12 #include "base/memory/shared_memory.h"
13 #include "base/sys_info.h"
14 #include "content/common/host_shared_bitmap_manager.h"
16 #if defined(OS_CHROMEOS)
17 #include "base/chromeos/memory_pressure_observer_chromeos.h"
18 #include "content/browser/browser_main_loop.h"
19 #endif
21 namespace content {
22 namespace {
24 const int kModeratePressurePercentage = 50;
25 const int kCriticalPressurePercentage = 10;
27 } // namespace
29 RendererFrameManager* RendererFrameManager::GetInstance() {
30 return Singleton<RendererFrameManager>::get();
33 void RendererFrameManager::AddFrame(RendererFrameManagerClient* frame,
34 bool locked) {
35 RemoveFrame(frame);
36 if (locked)
37 locked_frames_[frame] = 1;
38 else
39 unlocked_frames_.push_front(frame);
40 CullUnlockedFrames(GetMaxNumberOfSavedFrames());
43 void RendererFrameManager::RemoveFrame(RendererFrameManagerClient* frame) {
44 std::map<RendererFrameManagerClient*, size_t>::iterator locked_iter =
45 locked_frames_.find(frame);
46 if (locked_iter != locked_frames_.end())
47 locked_frames_.erase(locked_iter);
48 unlocked_frames_.remove(frame);
51 void RendererFrameManager::LockFrame(RendererFrameManagerClient* frame) {
52 std::list<RendererFrameManagerClient*>::iterator unlocked_iter =
53 std::find(unlocked_frames_.begin(), unlocked_frames_.end(), frame);
54 if (unlocked_iter != unlocked_frames_.end()) {
55 DCHECK(locked_frames_.find(frame) == locked_frames_.end());
56 unlocked_frames_.remove(frame);
57 locked_frames_[frame] = 1;
58 } else {
59 DCHECK(locked_frames_.find(frame) != locked_frames_.end());
60 locked_frames_[frame]++;
64 void RendererFrameManager::UnlockFrame(RendererFrameManagerClient* frame) {
65 DCHECK(locked_frames_.find(frame) != locked_frames_.end());
66 size_t locked_count = locked_frames_[frame];
67 DCHECK(locked_count);
68 if (locked_count > 1) {
69 locked_frames_[frame]--;
70 } else {
71 RemoveFrame(frame);
72 unlocked_frames_.push_front(frame);
73 CullUnlockedFrames(GetMaxNumberOfSavedFrames());
77 size_t RendererFrameManager::GetMaxNumberOfSavedFrames() const {
78 #if defined(OS_CHROMEOS)
79 content::BrowserMainLoop* browser_main_loop =
80 content::BrowserMainLoop::GetInstance();
82 // Unit tests can come here without a BrowserMainLoop instance.
83 if (!browser_main_loop)
84 return max_number_of_saved_frames_;
86 base::MemoryPressureObserverChromeOS* observer =
87 browser_main_loop->memory_pressure_observer();
89 if (!observer)
90 return max_number_of_saved_frames_;
92 // Until we have a global OnMemoryPressureChanged event we need to query the
93 // value from our specific pressure observer.
94 int percentage = 100;
95 switch (observer->GetCurrentPressureLevel()) {
96 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
97 percentage = 100;
98 break;
99 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
100 percentage = kModeratePressurePercentage;
101 break;
102 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
103 percentage = kCriticalPressurePercentage;
104 break;
106 size_t frames = (max_number_of_saved_frames_ * percentage) / 100;
107 return std::max(static_cast<size_t>(1), frames);
108 #else
109 return max_number_of_saved_frames_;
110 #endif
113 RendererFrameManager::RendererFrameManager()
114 : memory_pressure_listener_(
115 base::Bind(&RendererFrameManager::OnMemoryPressure,
116 base::Unretained(this))) {
117 // Note: With the destruction of this class the |memory_pressure_listener_|
118 // gets destroyed and the observer will remove itself.
119 max_number_of_saved_frames_ =
120 #if defined(OS_ANDROID)
122 #else
123 std::min(5, 2 + (base::SysInfo::AmountOfPhysicalMemoryMB() / 256));
124 #endif
125 max_handles_ = base::SharedMemory::GetHandleLimit() / 8.0f;
128 RendererFrameManager::~RendererFrameManager() {}
130 void RendererFrameManager::CullUnlockedFrames(size_t saved_frame_limit) {
131 if (unlocked_frames_.size() + locked_frames_.size() > 0) {
132 float handles_per_frame =
133 HostSharedBitmapManager::current()->AllocatedBitmapCount() * 1.0f /
134 (unlocked_frames_.size() + locked_frames_.size());
136 saved_frame_limit = std::max(
138 static_cast<int>(std::min(static_cast<float>(saved_frame_limit),
139 max_handles_ / handles_per_frame)));
141 while (!unlocked_frames_.empty() &&
142 unlocked_frames_.size() + locked_frames_.size() > saved_frame_limit) {
143 size_t old_size = unlocked_frames_.size();
144 // Should remove self from list.
145 unlocked_frames_.back()->EvictCurrentFrame();
146 DCHECK_EQ(unlocked_frames_.size() + 1, old_size);
150 void RendererFrameManager::OnMemoryPressure(
151 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
152 int saved_frame_limit = max_number_of_saved_frames_;
153 if (saved_frame_limit <= 1)
154 return;
155 int percentage = 100;
156 switch (memory_pressure_level) {
157 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
158 percentage = kModeratePressurePercentage;
159 break;
160 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
161 percentage = kCriticalPressurePercentage;
162 break;
163 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
164 // No need to change anything when there is no pressure.
165 return;
167 CullUnlockedFrames(std::max(1, (saved_frame_limit * percentage) / 100));
170 } // namespace content