Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / task_management / providers / child_process_task.cc
blob35f1a50ad387079ac96664b20d952e4d10e6bfa9
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 {
24 namespace {
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(
31 IDR_PLUGINS_FAVICON);
34 return g_default_icon;
37 base::string16 GetLocalizedTitle(const base::string16& title,
38 int process_type) {
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);
47 break;
48 default:
49 // Nothing to do for non-plugin processes.
50 break;
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::GetStringUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX);
63 case content::PROCESS_TYPE_GPU:
64 return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_GPU_PREFIX);
65 case content::PROCESS_TYPE_PLUGIN:
66 case content::PROCESS_TYPE_PPAPI_PLUGIN:
67 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PLUGIN_PREFIX,
68 result_title);
69 case content::PROCESS_TYPE_PPAPI_BROKER:
70 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PLUGIN_BROKER_PREFIX,
71 result_title);
72 case PROCESS_TYPE_NACL_BROKER:
73 return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NACL_BROKER_PREFIX);
74 case PROCESS_TYPE_NACL_LOADER:
75 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_NACL_PREFIX,
76 result_title);
77 // These types don't need display names or get them from elsewhere.
78 case content::PROCESS_TYPE_BROWSER:
79 case content::PROCESS_TYPE_RENDERER:
80 case content::PROCESS_TYPE_ZYGOTE:
81 case content::PROCESS_TYPE_SANDBOX_HELPER:
82 case content::PROCESS_TYPE_MAX:
83 break;
84 case content::PROCESS_TYPE_UNKNOWN:
85 NOTREACHED() << "Need localized name for child process type.";
88 return result_title;
91 // Connects the |resource_reporter| to the ServiceRegistry of the
92 // BrowserChildProcessHost whose unique ID is |unique_child_process_id|.
93 void ConnectResourceReporterOnIOThread(
94 int unique_child_process_id,
95 mojo::InterfaceRequest<ResourceUsageReporter> resource_reporter) {
96 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
98 content::BrowserChildProcessHost* host =
99 content::BrowserChildProcessHost::FromID(unique_child_process_id);
100 if (!host)
101 return;
103 content::ServiceRegistry* registry = host->GetServiceRegistry();
104 if (!registry)
105 return;
107 registry->ConnectToRemoteService(resource_reporter.Pass());
110 // Creates the Mojo service wrapper that will be used to sample the V8 memory
111 // usage of the browser child process whose unique ID is
112 // |unique_child_process_id|.
113 ProcessResourceUsage* CreateProcessResourcesSampler(
114 int unique_child_process_id) {
115 ResourceUsageReporterPtr service;
116 mojo::InterfaceRequest<ResourceUsageReporter> usage_reporter =
117 mojo::GetProxy(&service);
119 content::BrowserThread::PostTask(
120 content::BrowserThread::IO,
121 FROM_HERE,
122 base::Bind(&ConnectResourceReporterOnIOThread,
123 unique_child_process_id,
124 base::Passed(&usage_reporter)));
126 return new ProcessResourceUsage(service.Pass());
129 } // namespace
131 ChildProcessTask::ChildProcessTask(const content::ChildProcessData& data)
132 : Task(GetLocalizedTitle(data.name, data.process_type),
133 GetDefaultIcon(),
134 data.handle),
135 process_resources_sampler_(CreateProcessResourcesSampler(data.id)),
136 v8_memory_allocated_(-1),
137 v8_memory_used_(-1),
138 unique_child_process_id_(data.id),
139 process_type_(data.process_type) {
142 ChildProcessTask::~ChildProcessTask() {
145 void ChildProcessTask::Refresh(const base::TimeDelta& update_interval,
146 int64 refresh_flags) {
147 Task::Refresh(update_interval, refresh_flags);
149 if ((refresh_flags & REFRESH_TYPE_V8_MEMORY) == 0)
150 return;
152 // The child process resources refresh is performed asynchronously, we will
153 // invoke it and record the current values (which might be invalid at the
154 // moment. We can safely ignore that and count on future refresh cycles
155 // potentially having valid values).
156 process_resources_sampler_->Refresh(base::Closure());
158 v8_memory_allocated_ = base::saturated_cast<int64>(
159 process_resources_sampler_->GetV8MemoryAllocated());
160 v8_memory_used_ = base::saturated_cast<int64>(
161 process_resources_sampler_->GetV8MemoryUsed());
164 Task::Type ChildProcessTask::GetType() const {
165 // Convert |content::ProcessType| to |task_management::Task::Type|.
166 switch (process_type_) {
167 case content::PROCESS_TYPE_PLUGIN:
168 case content::PROCESS_TYPE_PPAPI_PLUGIN:
169 case content::PROCESS_TYPE_PPAPI_BROKER:
170 return Task::PLUGIN;
171 case content::PROCESS_TYPE_UTILITY:
172 return Task::UTILITY;
173 case content::PROCESS_TYPE_ZYGOTE:
174 return Task::ZYGOTE;
175 case content::PROCESS_TYPE_SANDBOX_HELPER:
176 return Task::SANDBOX_HELPER;
177 case content::PROCESS_TYPE_GPU:
178 return Task::GPU;
179 case PROCESS_TYPE_NACL_LOADER:
180 case PROCESS_TYPE_NACL_BROKER:
181 return Task::NACL;
182 default:
183 return Task::UNKNOWN;
187 int ChildProcessTask::GetChildProcessUniqueID() const {
188 return unique_child_process_id_;
191 bool ChildProcessTask::ReportsV8Memory() const {
192 return process_resources_sampler_->ReportsV8MemoryStats();
195 int64 ChildProcessTask::GetV8MemoryAllocated() const {
196 return v8_memory_allocated_;
199 int64 ChildProcessTask::GetV8MemoryUsed() const {
200 return v8_memory_used_;
203 } // namespace task_management