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/child_process_task.h"
7 #include "base/i18n/rtl.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "chrome/browser/process_resource_usage.h"
10 #include "chrome/browser/task_management/task_manager_observer.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "components/nacl/common/nacl_process_type.h"
13 #include "content/public/browser/browser_child_process_host.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/child_process_data.h"
16 #include "content/public/common/process_type.h"
17 #include "content/public/common/service_registry.h"
18 #include "grit/theme_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
22 namespace task_management
{
26 gfx::ImageSkia
* g_default_icon
= nullptr;
28 gfx::ImageSkia
* GetDefaultIcon() {
29 if (!g_default_icon
&& ResourceBundle::HasSharedInstance()) {
30 g_default_icon
= ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
34 return g_default_icon
;
37 base::string16
GetLocalizedTitle(const base::string16
& title
,
39 base::string16 result_title
= title
;
40 if (result_title
.empty()) {
41 switch (process_type
) {
42 case content::PROCESS_TYPE_PLUGIN
:
43 case content::PROCESS_TYPE_PPAPI_PLUGIN
:
44 case content::PROCESS_TYPE_PPAPI_BROKER
:
45 result_title
= l10n_util::GetStringUTF16(
46 IDS_TASK_MANAGER_UNKNOWN_PLUGIN_NAME
);
49 // Nothing to do for non-plugin processes.
54 // Explicitly mark name as LTR if there is no strong RTL character,
55 // to avoid the wrong concatenation result similar to "!Yahoo Mail: the
56 // best web-based Email: NIGULP", in which "NIGULP" stands for the Hebrew
57 // or Arabic word for "plugin".
58 base::i18n::AdjustStringForLocaleDirection(&result_title
);
60 switch (process_type
) {
61 case content::PROCESS_TYPE_UTILITY
:
62 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX
,
64 case content::PROCESS_TYPE_GPU
:
65 return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_GPU_PREFIX
);
66 case content::PROCESS_TYPE_PLUGIN
:
67 case content::PROCESS_TYPE_PPAPI_PLUGIN
:
68 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PLUGIN_PREFIX
,
70 case content::PROCESS_TYPE_PPAPI_BROKER
:
71 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PLUGIN_BROKER_PREFIX
,
73 case PROCESS_TYPE_NACL_BROKER
:
74 return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NACL_BROKER_PREFIX
);
75 case PROCESS_TYPE_NACL_LOADER
:
76 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_NACL_PREFIX
,
78 // These types don't need display names or get them from elsewhere.
79 case content::PROCESS_TYPE_BROWSER
:
80 case content::PROCESS_TYPE_RENDERER
:
81 case content::PROCESS_TYPE_ZYGOTE
:
82 case content::PROCESS_TYPE_SANDBOX_HELPER
:
83 case content::PROCESS_TYPE_MAX
:
85 case content::PROCESS_TYPE_UNKNOWN
:
86 NOTREACHED() << "Need localized name for child process type.";
92 // Connects the |resource_reporter| to the ServiceRegistry of the
93 // BrowserChildProcessHost whose unique ID is |unique_child_process_id|.
94 void ConnectResourceReporterOnIOThread(
95 int unique_child_process_id
,
96 mojo::InterfaceRequest
<ResourceUsageReporter
> resource_reporter
) {
97 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
99 content::BrowserChildProcessHost
* host
=
100 content::BrowserChildProcessHost::FromID(unique_child_process_id
);
104 content::ServiceRegistry
* registry
= host
->GetServiceRegistry();
108 registry
->ConnectToRemoteService(resource_reporter
.Pass());
111 // Creates the Mojo service wrapper that will be used to sample the V8 memory
112 // usage of the browser child process whose unique ID is
113 // |unique_child_process_id|.
114 ProcessResourceUsage
* CreateProcessResourcesSampler(
115 int unique_child_process_id
) {
116 ResourceUsageReporterPtr service
;
117 mojo::InterfaceRequest
<ResourceUsageReporter
> usage_reporter
=
118 mojo::GetProxy(&service
);
120 content::BrowserThread::PostTask(
121 content::BrowserThread::IO
,
123 base::Bind(&ConnectResourceReporterOnIOThread
,
124 unique_child_process_id
,
125 base::Passed(&usage_reporter
)));
127 return new ProcessResourceUsage(service
.Pass());
132 ChildProcessTask::ChildProcessTask(const content::ChildProcessData
& data
)
133 : Task(GetLocalizedTitle(data
.name
, data
.process_type
),
136 process_resources_sampler_(CreateProcessResourcesSampler(data
.id
)),
137 v8_memory_allocated_(-1),
139 unique_child_process_id_(data
.id
),
140 process_type_(data
.process_type
) {
143 ChildProcessTask::~ChildProcessTask() {
146 void ChildProcessTask::Refresh(const base::TimeDelta
& update_interval
,
147 int64 refresh_flags
) {
148 Task::Refresh(update_interval
, refresh_flags
);
150 if ((refresh_flags
& REFRESH_TYPE_V8_MEMORY
) == 0)
153 // The child process resources refresh is performed asynchronously, we will
154 // invoke it and record the current values (which might be invalid at the
155 // moment. We can safely ignore that and count on future refresh cycles
156 // potentially having valid values).
157 process_resources_sampler_
->Refresh(base::Closure());
159 v8_memory_allocated_
= base::saturated_cast
<int64
>(
160 process_resources_sampler_
->GetV8MemoryAllocated());
161 v8_memory_used_
= base::saturated_cast
<int64
>(
162 process_resources_sampler_
->GetV8MemoryUsed());
165 Task::Type
ChildProcessTask::GetType() const {
166 // Convert |content::ProcessType| to |task_management::Task::Type|.
167 switch (process_type_
) {
168 case content::PROCESS_TYPE_PLUGIN
:
169 case content::PROCESS_TYPE_PPAPI_PLUGIN
:
170 case content::PROCESS_TYPE_PPAPI_BROKER
:
172 case content::PROCESS_TYPE_UTILITY
:
173 return Task::UTILITY
;
174 case content::PROCESS_TYPE_ZYGOTE
:
176 case content::PROCESS_TYPE_SANDBOX_HELPER
:
177 return Task::SANDBOX_HELPER
;
178 case content::PROCESS_TYPE_GPU
:
180 case PROCESS_TYPE_NACL_LOADER
:
181 case PROCESS_TYPE_NACL_BROKER
:
184 return Task::UNKNOWN
;
188 int ChildProcessTask::GetChildProcessUniqueID() const {
189 return unique_child_process_id_
;
192 bool ChildProcessTask::ReportsV8Memory() const {
193 return process_resources_sampler_
->ReportsV8MemoryStats();
196 int64
ChildProcessTask::GetV8MemoryAllocated() const {
197 return v8_memory_allocated_
;
200 int64
ChildProcessTask::GetV8MemoryUsed() const {
201 return v8_memory_used_
;
204 } // namespace task_management