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