Home | History | Annotate | Download | only in renderer_host
      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.
      4 
      5 #include "content/browser/renderer_host/renderer_frame_manager.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/sys_info.h"
      9 
     10 namespace content {
     11 
     12 RendererFrameManager* RendererFrameManager::GetInstance() {
     13   return Singleton<RendererFrameManager>::get();
     14 }
     15 
     16 void RendererFrameManager::AddFrame(RendererFrameManagerClient* frame,
     17                                     bool visible) {
     18   RemoveFrame(frame);
     19   if (visible)
     20     visible_frames_.insert(frame);
     21   else
     22     hidden_frames_.push_front(frame);
     23   CullHiddenFrames();
     24 }
     25 
     26 void RendererFrameManager::RemoveFrame(RendererFrameManagerClient* frame) {
     27   visible_frames_.erase(frame);
     28   hidden_frames_.remove(frame);
     29 }
     30 
     31 void RendererFrameManager::SetFrameVisibility(RendererFrameManagerClient* frame,
     32                                               bool visible) {
     33   if (visible) {
     34     hidden_frames_.remove(frame);
     35     visible_frames_.insert(frame);
     36   } else {
     37     visible_frames_.erase(frame);
     38     hidden_frames_.push_front(frame);
     39     CullHiddenFrames();
     40   }
     41 }
     42 
     43 RendererFrameManager::RendererFrameManager()
     44     : max_number_of_saved_frames_(
     45           std::min(5, 2 + (base::SysInfo::AmountOfPhysicalMemoryMB() / 256))) {}
     46 
     47 RendererFrameManager::~RendererFrameManager() {}
     48 
     49 void RendererFrameManager::CullHiddenFrames() {
     50   while (!hidden_frames_.empty() &&
     51          hidden_frames_.size() + visible_frames_.size() >
     52              max_number_of_saved_frames()) {
     53     size_t old_size = hidden_frames_.size();
     54     // Should remove self from list.
     55     hidden_frames_.back()->EvictCurrentFrame();
     56     DCHECK_EQ(hidden_frames_.size() + 1, old_size);
     57   }
     58 }
     59 
     60 }  // namespace content
     61