1a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// found in the LICENSE file.
4a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/task_manager/renderer_resource.h"
6a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
7a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/basictypes.h"
8a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chrome/browser/devtools/devtools_window.h"
923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/task_manager/resource_provider.h"
1123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "chrome/browser/task_manager/task_manager_util.h"
12a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chrome/common/render_messages.h"
13a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "content/public/browser/render_process_host.h"
14a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "content/public/browser/render_view_host.h"
15a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace task_manager {
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)RendererResource::RendererResource(base::ProcessHandle process,
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                   content::RenderViewHost* render_view_host)
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : process_(process),
21a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      render_view_host_(render_view_host),
22a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      pending_stats_update_(false),
23a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      v8_memory_allocated_(0),
24a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      v8_memory_used_(0),
25a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      pending_v8_memory_allocated_update_(false) {
26a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // We cache the process and pid as when a Tab/BackgroundContents is closed the
27a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // process reference becomes NULL and the TaskManager still needs it.
28a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  pid_ = base::GetProcId(process_);
29a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  unique_process_id_ = render_view_host_->GetProcess()->GetID();
30a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  memset(&stats_, 0, sizeof(stats_));
31a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
32a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)RendererResource::~RendererResource() {
34a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
35a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void RendererResource::Refresh() {
37a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!pending_stats_update_) {
38a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    render_view_host_->Send(new ChromeViewMsg_GetCacheResourceStats);
39a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    pending_stats_update_ = true;
40a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
41a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!pending_v8_memory_allocated_update_) {
42a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    render_view_host_->Send(new ChromeViewMsg_GetV8HeapStats);
43a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    pending_v8_memory_allocated_update_ = true;
44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
45a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
46a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)blink::WebCache::ResourceTypeStats
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)RendererResource::GetWebCoreCacheStats() const {
49a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return stats_;
50a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
51a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)size_t RendererResource::GetV8MemoryAllocated() const {
53a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return v8_memory_allocated_;
54a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
55a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)size_t RendererResource::GetV8MemoryUsed() const {
57a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return v8_memory_used_;
58a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void RendererResource::NotifyResourceTypeStats(
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const blink::WebCache::ResourceTypeStats& stats) {
62a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  stats_ = stats;
63a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  pending_stats_update_ = false;
64a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
65a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void RendererResource::NotifyV8HeapStats(
67a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    size_t v8_memory_allocated, size_t v8_memory_used) {
68a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  v8_memory_allocated_ = v8_memory_allocated;
69a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  v8_memory_used_ = v8_memory_used;
70a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  pending_v8_memory_allocated_update_ = false;
71a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
72a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
7323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)base::string16 RendererResource::GetProfileName() const {
7423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return util::GetProfileNameFromInfoCache(Profile::FromBrowserContext(
7523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      render_view_host_->GetProcess()->GetBrowserContext()));
7623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
7723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)base::ProcessHandle RendererResource::GetProcess() const {
79a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return process_;
80a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
81a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)int RendererResource::GetUniqueChildProcessId() const {
83a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return unique_process_id_;
84a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
85a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)Resource::Type RendererResource::GetType() const {
87a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return RENDERER;
88a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
89a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)int RendererResource::GetRoutingID() const {
91a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return render_view_host_->GetRoutingID();
92a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
93a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool RendererResource::ReportsCacheStats() const {
95a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return true;
96a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
97a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool RendererResource::ReportsV8MemoryStats() const {
99a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return true;
100a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
101a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool RendererResource::SupportNetworkUsage() const {
103a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return true;
104a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
105a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace task_manager
107