1 // Copyright 2015 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.
5 #include "chrome/browser/task_management/providers/web_contents/renderer_task.h"
7 #include "base/i18n/rtl.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/favicon/favicon_utils.h"
12 #include "chrome/browser/process_resource_usage.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_info_cache.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/task_management/task_manager_observer.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/browser/web_contents_delegate.h"
21 #include "content/public/common/service_registry.h"
22 #include "ui/base/l10n/l10n_util.h"
24 namespace task_management
{
28 // Creates the Mojo service wrapper that will be used to sample the V8 memory
29 // usage and the the WebCache resource stats of the render process hosted by
30 // |render_process_host|.
31 ProcessResourceUsage
* CreateRendererResourcesSampler(
32 content::RenderProcessHost
* render_process_host
) {
33 ResourceUsageReporterPtr service
;
34 content::ServiceRegistry
* service_registry
=
35 render_process_host
->GetServiceRegistry();
37 service_registry
->ConnectToRemoteService(mojo::GetProxy(&service
));
38 return new ProcessResourceUsage(service
.Pass());
41 // Gets the profile name associated with the browser context of the given
42 // |render_process_host| from the profile info cache.
43 base::string16
GetRendererProfileName(
44 const content::RenderProcessHost
* render_process_host
) {
46 Profile::FromBrowserContext(render_process_host
->GetBrowserContext());
48 ProfileInfoCache
& cache
=
49 g_browser_process
->profile_manager()->GetProfileInfoCache();
51 cache
.GetIndexOfProfileWithPath(profile
->GetOriginalProfile()->GetPath());
52 if (index
!= std::string::npos
)
53 return cache
.GetNameOfProfileAtIndex(index
);
55 return base::string16();
58 inline bool IsRendererResourceSamplingDisabled(int64 flags
) {
59 return (flags
& (REFRESH_TYPE_V8_MEMORY
| REFRESH_TYPE_WEBCACHE_STATS
)) == 0;
64 RendererTask::RendererTask(const base::string16
& title
,
65 const gfx::ImageSkia
* icon
,
66 content::WebContents
* web_contents
)
67 : Task(title
, icon
, web_contents
->GetRenderProcessHost()->GetHandle()),
68 web_contents_(web_contents
),
69 render_process_host_(web_contents
->GetRenderProcessHost()),
70 renderer_resources_sampler_(
71 CreateRendererResourcesSampler(render_process_host_
)),
72 render_process_id_(render_process_host_
->GetID()),
73 v8_memory_allocated_(0),
76 profile_name_(GetRendererProfileName(render_process_host_
)) {
79 RendererTask::~RendererTask() {
82 void RendererTask::Activate() {
83 if (!web_contents_
->GetDelegate())
86 web_contents_
->GetDelegate()->ActivateContents(web_contents_
);
89 void RendererTask::Refresh(const base::TimeDelta
& update_interval
,
90 int64 refresh_flags
) {
91 Task::Refresh(update_interval
, refresh_flags
);
93 if (IsRendererResourceSamplingDisabled(refresh_flags
))
96 // The renderer resources refresh is performed asynchronously, we will invoke
97 // it and record the current values (which might be invalid at the moment. We
98 // can safely ignore that and count on future refresh cycles potentially
99 // having valid values).
100 renderer_resources_sampler_
->Refresh(base::Closure());
102 v8_memory_allocated_
= base::saturated_cast
<int64
>(
103 renderer_resources_sampler_
->GetV8MemoryAllocated());
104 v8_memory_used_
= base::saturated_cast
<int64
>(
105 renderer_resources_sampler_
->GetV8MemoryUsed());
106 webcache_stats_
= renderer_resources_sampler_
->GetWebCoreCacheStats();
109 Task::Type
RendererTask::GetType() const {
110 return Task::RENDERER
;
113 int RendererTask::GetChildProcessUniqueID() const {
114 return render_process_id_
;
117 base::string16
RendererTask::GetProfileName() const {
118 return profile_name_
;
121 int64
RendererTask::GetV8MemoryAllocated() const {
122 return v8_memory_allocated_
;
125 int64
RendererTask::GetV8MemoryUsed() const {
126 return v8_memory_used_
;
129 bool RendererTask::ReportsWebCacheStats() const {
133 blink::WebCache::ResourceTypeStats
RendererTask::GetWebCacheStats() const {
134 return webcache_stats_
;
138 base::string16
RendererTask::GetTitleFromWebContents(
139 content::WebContents
* web_contents
) {
140 DCHECK(web_contents
);
141 base::string16 title
= web_contents
->GetTitle();
143 GURL url
= web_contents
->GetURL();
144 title
= base::UTF8ToUTF16(url
.spec());
145 // Force URL to be LTR.
146 title
= base::i18n::GetDisplayStringInLTRDirectionality(title
);
148 // Since the title could later be concatenated with
149 // IDS_TASK_MANAGER_TAB_PREFIX (for example), we need to explicitly set the
150 // title to be LTR format if there is no strong RTL charater in it.
151 // Otherwise, if IDS_TASK_MANAGER_TAB_PREFIX is an RTL word, the
152 // concatenated result might be wrong. For example, http://mail.yahoo.com,
153 // whose title is "Yahoo! Mail: The best web-based Email!", without setting
154 // it explicitly as LTR format, the concatenated result will be "!Yahoo!
155 // Mail: The best web-based Email :BAT", in which the capital letters "BAT"
156 // stands for the Hebrew word for "tab".
157 base::i18n::AdjustStringForLocaleDirection(&title
);
163 const gfx::ImageSkia
* RendererTask::GetFaviconFromWebContents(
164 content::WebContents
* web_contents
) {
165 DCHECK(web_contents
);
167 // Tag the web_contents with a |ContentFaviconDriver| (if needed) so that
168 // we can use it to retrieve the favicon if there is one.
169 favicon::CreateContentFaviconDriverForWebContents(web_contents
);
171 favicon::ContentFaviconDriver::FromWebContents(web_contents
)->
176 return image
.ToImageSkia();
180 const base::string16
RendererTask::PrefixRendererTitle(
181 const base::string16
& title
,
185 int message_id
= IDS_TASK_MANAGER_TAB_PREFIX
;
187 if (is_incognito
&& !is_app
&& !is_extension
) {
188 message_id
= IDS_TASK_MANAGER_TAB_INCOGNITO_PREFIX
;
191 message_id
= IDS_TASK_MANAGER_APP_INCOGNITO_PREFIX
;
193 message_id
= IDS_TASK_MANAGER_APP_PREFIX
;
194 } else if (is_extension
) {
196 message_id
= IDS_TASK_MANAGER_EXTENSION_INCOGNITO_PREFIX
;
198 message_id
= IDS_TASK_MANAGER_EXTENSION_PREFIX
;
201 return l10n_util::GetStringFUTF16(message_id
, title
);
204 } // namespace task_management