summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/frame_memory_manager.cc
blob: 708bddfa61b9cefd1a5a8d9c95a661717e4eea2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/frame_memory_manager.h"

#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/sys_info.h"

namespace content {

namespace {

size_t MaxNumberOfSavedFrames() {
  return std::min(5, 2 + (base::SysInfo::AmountOfPhysicalMemoryMB() / 256));
}

}  // namespace

FrameMemoryManager* FrameMemoryManager::GetInstance() {
  return Singleton<FrameMemoryManager>::get();
}

void FrameMemoryManager::AddFrame(FrameContainer* frame, bool visible) {
  RemoveFrame(frame);
  if (visible)
    visible_frames_.insert(frame);
  else
    hidden_frames_.push_front(frame);
  CullHiddenFrames();
}

void FrameMemoryManager::RemoveFrame(FrameContainer* frame) {
  visible_frames_.erase(frame);
  hidden_frames_.remove(frame);
}

void FrameMemoryManager::SetFrameVisibility(FrameContainer* frame,
                                            bool visible) {
  if (visible) {
    hidden_frames_.remove(frame);
    visible_frames_.insert(frame);
  } else {
    visible_frames_.erase(frame);
    hidden_frames_.push_front(frame);
    CullHiddenFrames();
  }
}

FrameMemoryManager::FrameMemoryManager() {}

FrameMemoryManager::~FrameMemoryManager() {}

void FrameMemoryManager::CullHiddenFrames() {
  while (!hidden_frames_.empty() &&
         hidden_frames_.size() + visible_frames_.size() >
             MaxNumberOfSavedFrames()) {
    size_t old_size = hidden_frames_.size();
    // Should remove self from list.
    hidden_frames_.back()->ReleaseCurrentFrame();
    DCHECK_EQ(hidden_frames_.size() + 1, old_size);
  }
}

}  // namespace content