Home | History | Annotate | Download | only in task_manager
      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 "chrome/browser/task_manager/renderer_resource.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "chrome/browser/devtools/devtools_window.h"
      9 #include "chrome/browser/task_manager/resource_provider.h"
     10 #include "chrome/common/render_messages.h"
     11 #include "content/public/browser/render_process_host.h"
     12 #include "content/public/browser/render_view_host.h"
     13 
     14 namespace task_manager {
     15 
     16 RendererResource::RendererResource(base::ProcessHandle process,
     17                                    content::RenderViewHost* render_view_host)
     18     : process_(process),
     19       render_view_host_(render_view_host),
     20       pending_stats_update_(false),
     21       fps_(0.0f),
     22       pending_fps_update_(false),
     23       v8_memory_allocated_(0),
     24       v8_memory_used_(0),
     25       pending_v8_memory_allocated_update_(false) {
     26   // We cache the process and pid as when a Tab/BackgroundContents is closed the
     27   // process reference becomes NULL and the TaskManager still needs it.
     28   pid_ = base::GetProcId(process_);
     29   unique_process_id_ = render_view_host_->GetProcess()->GetID();
     30   memset(&stats_, 0, sizeof(stats_));
     31 }
     32 
     33 RendererResource::~RendererResource() {
     34 }
     35 
     36 void RendererResource::Refresh() {
     37   if (!pending_stats_update_) {
     38     render_view_host_->Send(new ChromeViewMsg_GetCacheResourceStats);
     39     pending_stats_update_ = true;
     40   }
     41   if (!pending_fps_update_) {
     42     render_view_host_->Send(
     43         new ChromeViewMsg_GetFPS(render_view_host_->GetRoutingID()));
     44     pending_fps_update_ = true;
     45   }
     46   if (!pending_v8_memory_allocated_update_) {
     47     render_view_host_->Send(new ChromeViewMsg_GetV8HeapStats);
     48     pending_v8_memory_allocated_update_ = true;
     49   }
     50 }
     51 
     52 blink::WebCache::ResourceTypeStats
     53 RendererResource::GetWebCoreCacheStats() const {
     54   return stats_;
     55 }
     56 
     57 float RendererResource::GetFPS() const {
     58   return fps_;
     59 }
     60 
     61 size_t RendererResource::GetV8MemoryAllocated() const {
     62   return v8_memory_allocated_;
     63 }
     64 
     65 size_t RendererResource::GetV8MemoryUsed() const {
     66   return v8_memory_used_;
     67 }
     68 
     69 void RendererResource::NotifyResourceTypeStats(
     70     const blink::WebCache::ResourceTypeStats& stats) {
     71   stats_ = stats;
     72   pending_stats_update_ = false;
     73 }
     74 
     75 void RendererResource::NotifyFPS(float fps) {
     76   fps_ = fps;
     77   pending_fps_update_ = false;
     78 }
     79 
     80 void RendererResource::NotifyV8HeapStats(
     81     size_t v8_memory_allocated, size_t v8_memory_used) {
     82   v8_memory_allocated_ = v8_memory_allocated;
     83   v8_memory_used_ = v8_memory_used;
     84   pending_v8_memory_allocated_update_ = false;
     85 }
     86 
     87 base::ProcessHandle RendererResource::GetProcess() const {
     88   return process_;
     89 }
     90 
     91 int RendererResource::GetUniqueChildProcessId() const {
     92   return unique_process_id_;
     93 }
     94 
     95 Resource::Type RendererResource::GetType() const {
     96   return RENDERER;
     97 }
     98 
     99 int RendererResource::GetRoutingID() const {
    100   return render_view_host_->GetRoutingID();
    101 }
    102 
    103 bool RendererResource::ReportsCacheStats() const {
    104   return true;
    105 }
    106 
    107 bool RendererResource::ReportsFPS() const {
    108   return true;
    109 }
    110 
    111 bool RendererResource::ReportsV8MemoryStats() const {
    112   return true;
    113 }
    114 
    115 bool RendererResource::CanInspect() const {
    116   return true;
    117 }
    118 
    119 void RendererResource::Inspect() const {
    120   DevToolsWindow::OpenDevToolsWindow(render_view_host_);
    121 }
    122 
    123 bool RendererResource::SupportNetworkUsage() const {
    124   return true;
    125 }
    126 
    127 }  // namespace task_manager
    128