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.
5 #include "chrome/browser/task_manager/child_process_resource_provider.h"
9 #include "base/i18n/rtl.h"
10 #include "base/strings/string16.h"
11 #include "chrome/browser/task_manager/resource_provider.h"
12 #include "chrome/browser/task_manager/task_manager.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "components/nacl/common/nacl_process_type.h"
15 #include "content/public/browser/browser_child_process_host_iterator.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/child_process_data.h"
18 #include "grit/theme_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/gfx/image/image_skia.h"
23 using content::BrowserChildProcessHostIterator
;
24 using content::BrowserThread
;
25 using content::WebContents
;
27 namespace task_manager
{
29 class ChildProcessResource
: public Resource
{
31 ChildProcessResource(int process_type
,
32 const base::string16
& name
,
33 base::ProcessHandle handle
,
34 int unique_process_id
);
35 ~ChildProcessResource() override
;
38 base::string16
GetTitle() const override
;
39 base::string16
GetProfileName() const override
;
40 gfx::ImageSkia
GetIcon() const override
;
41 base::ProcessHandle
GetProcess() const override
;
42 int GetUniqueChildProcessId() const override
;
43 Type
GetType() const override
;
44 bool SupportNetworkUsage() const override
;
45 void SetSupportNetworkUsage() override
;
47 // Returns the pid of the child process.
48 int process_id() const { return pid_
; }
51 // Returns a localized title for the child process. For example, a plugin
52 // process would be "Plugin: Flash" when name is "Flash".
53 base::string16
GetLocalizedTitle() const;
57 base::ProcessHandle handle_
;
59 int unique_process_id_
;
60 mutable base::string16 title_
;
61 bool network_usage_support_
;
63 // The icon painted for the child processs.
64 // TODO(jcampan): we should have plugin specific icons for well-known
66 static gfx::ImageSkia
* default_icon_
;
68 DISALLOW_COPY_AND_ASSIGN(ChildProcessResource
);
71 gfx::ImageSkia
* ChildProcessResource::default_icon_
= NULL
;
73 ChildProcessResource::ChildProcessResource(
75 const base::string16
& name
,
76 base::ProcessHandle handle
,
77 int unique_process_id
)
78 : process_type_(process_type
),
81 unique_process_id_(unique_process_id
),
82 network_usage_support_(false) {
83 // We cache the process id because it's not cheap to calculate, and it won't
84 // be available when we get the plugin disconnected notification.
85 pid_
= base::GetProcId(handle
);
87 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
88 default_icon_
= rb
.GetImageSkiaNamed(IDR_PLUGINS_FAVICON
);
89 // TODO(jabdelmalek): use different icon for web workers.
93 ChildProcessResource::~ChildProcessResource() {
97 base::string16
ChildProcessResource::GetTitle() const {
99 title_
= GetLocalizedTitle();
104 base::string16
ChildProcessResource::GetProfileName() const {
105 return base::string16();
108 gfx::ImageSkia
ChildProcessResource::GetIcon() const {
109 return *default_icon_
;
112 base::ProcessHandle
ChildProcessResource::GetProcess() const {
116 int ChildProcessResource::GetUniqueChildProcessId() const {
117 return unique_process_id_
;
120 Resource::Type
ChildProcessResource::GetType() const {
121 // Translate types to Resource::Type, since ChildProcessData's type
122 // is not available for all TaskManager resources.
123 switch (process_type_
) {
124 case content::PROCESS_TYPE_PLUGIN
:
125 case content::PROCESS_TYPE_PPAPI_PLUGIN
:
126 case content::PROCESS_TYPE_PPAPI_BROKER
:
127 return Resource::PLUGIN
;
128 case content::PROCESS_TYPE_UTILITY
:
129 return Resource::UTILITY
;
130 case content::PROCESS_TYPE_ZYGOTE
:
131 return Resource::ZYGOTE
;
132 case content::PROCESS_TYPE_SANDBOX_HELPER
:
133 return Resource::SANDBOX_HELPER
;
134 case content::PROCESS_TYPE_GPU
:
135 return Resource::GPU
;
136 case PROCESS_TYPE_NACL_LOADER
:
137 case PROCESS_TYPE_NACL_BROKER
:
138 return Resource::NACL
;
140 return Resource::UNKNOWN
;
144 bool ChildProcessResource::SupportNetworkUsage() const {
145 return network_usage_support_
;
148 void ChildProcessResource::SetSupportNetworkUsage() {
149 network_usage_support_
= true;
152 base::string16
ChildProcessResource::GetLocalizedTitle() const {
153 base::string16 title
= name_
;
155 switch (process_type_
) {
156 case content::PROCESS_TYPE_PLUGIN
:
157 case content::PROCESS_TYPE_PPAPI_PLUGIN
:
158 case content::PROCESS_TYPE_PPAPI_BROKER
:
159 title
= l10n_util::GetStringUTF16(IDS_TASK_MANAGER_UNKNOWN_PLUGIN_NAME
);
162 // Nothing to do for non-plugin processes.
167 // Explicitly mark name as LTR if there is no strong RTL character,
168 // to avoid the wrong concatenation result similar to "!Yahoo Mail: the
169 // best web-based Email: NIGULP", in which "NIGULP" stands for the Hebrew
170 // or Arabic word for "plugin".
171 base::i18n::AdjustStringForLocaleDirection(&title
);
173 switch (process_type_
) {
174 case content::PROCESS_TYPE_UTILITY
:
175 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_UTILITY_PREFIX
, title
);
176 case content::PROCESS_TYPE_GPU
:
177 return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_GPU_PREFIX
);
178 case content::PROCESS_TYPE_PLUGIN
:
179 case content::PROCESS_TYPE_PPAPI_PLUGIN
:
180 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PLUGIN_PREFIX
, title
);
181 case content::PROCESS_TYPE_PPAPI_BROKER
:
182 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PLUGIN_BROKER_PREFIX
,
184 case PROCESS_TYPE_NACL_BROKER
:
185 return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NACL_BROKER_PREFIX
);
186 case PROCESS_TYPE_NACL_LOADER
:
187 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_NACL_PREFIX
, title
);
188 // These types don't need display names or get them from elsewhere.
189 case content::PROCESS_TYPE_BROWSER
:
190 case content::PROCESS_TYPE_RENDERER
:
191 case content::PROCESS_TYPE_ZYGOTE
:
192 case content::PROCESS_TYPE_SANDBOX_HELPER
:
193 case content::PROCESS_TYPE_MAX
:
196 case content::PROCESS_TYPE_UNKNOWN
:
197 NOTREACHED() << "Need localized name for child process type.";
203 ////////////////////////////////////////////////////////////////////////////////
204 // ChildProcessResourceProvider class
205 ////////////////////////////////////////////////////////////////////////////////
207 ChildProcessResourceProvider::
208 ChildProcessResourceProvider(TaskManager
* task_manager
)
209 : task_manager_(task_manager
),
213 ChildProcessResourceProvider::~ChildProcessResourceProvider() {
216 Resource
* ChildProcessResourceProvider::GetResource(
220 PidResourceMap::iterator iter
= pid_to_resources_
.find(origin_pid
);
221 if (iter
!= pid_to_resources_
.end())
227 void ChildProcessResourceProvider::StartUpdating() {
231 // Get the existing child processes.
232 BrowserThread::PostTask(
233 BrowserThread::IO
, FROM_HERE
,
235 &ChildProcessResourceProvider::RetrieveChildProcessData
,
238 BrowserChildProcessObserver::Add(this);
241 void ChildProcessResourceProvider::StopUpdating() {
245 // Delete all the resources.
246 STLDeleteContainerPairSecondPointers(resources_
.begin(), resources_
.end());
249 pid_to_resources_
.clear();
251 BrowserChildProcessObserver::Remove(this);
254 void ChildProcessResourceProvider::BrowserChildProcessHostConnected(
255 const content::ChildProcessData
& data
) {
258 if (resources_
.count(data
.handle
)) {
259 // The case may happen that we have added a child_process_info as part of
260 // the iteration performed during StartUpdating() call but the notification
261 // that it has connected was not fired yet. So when the notification
262 // happens, we already know about this plugin and just ignore it.
265 AddToTaskManager(data
);
268 void ChildProcessResourceProvider::
269 BrowserChildProcessHostDisconnected(
270 const content::ChildProcessData
& data
) {
273 ChildProcessMap::iterator iter
= resources_
.find(data
.handle
);
274 if (iter
== resources_
.end()) {
275 // ChildProcessData disconnection notifications are asynchronous, so we
276 // might be notified for a plugin we don't know anything about (if it was
277 // closed before the task manager was shown and destroyed after that).
280 // Remove the resource from the Task Manager.
281 ChildProcessResource
* resource
= iter
->second
;
282 task_manager_
->RemoveResource(resource
);
283 // Remove it from the provider.
284 resources_
.erase(iter
);
285 // Remove it from our pid map.
286 PidResourceMap::iterator pid_iter
=
287 pid_to_resources_
.find(resource
->process_id());
288 DCHECK(pid_iter
!= pid_to_resources_
.end());
289 if (pid_iter
!= pid_to_resources_
.end())
290 pid_to_resources_
.erase(pid_iter
);
292 // Finally, delete the resource.
296 void ChildProcessResourceProvider::AddToTaskManager(
297 const content::ChildProcessData
& child_process_data
) {
298 ChildProcessResource
* resource
=
299 new ChildProcessResource(
300 child_process_data
.process_type
,
301 child_process_data
.name
,
302 child_process_data
.handle
,
303 child_process_data
.id
);
304 resources_
[child_process_data
.handle
] = resource
;
305 pid_to_resources_
[resource
->process_id()] = resource
;
306 task_manager_
->AddResource(resource
);
309 // The ChildProcessData::Iterator has to be used from the IO thread.
310 void ChildProcessResourceProvider::RetrieveChildProcessData() {
311 std::vector
<content::ChildProcessData
> child_processes
;
312 for (BrowserChildProcessHostIterator iter
; !iter
.Done(); ++iter
) {
313 // Only add processes which are already started, since we need their handle.
314 if (iter
.GetData().handle
== base::kNullProcessHandle
)
316 child_processes
.push_back(iter
.GetData());
318 // Now notify the UI thread that we have retrieved information about child
320 BrowserThread::PostTask(
321 BrowserThread::UI
, FROM_HERE
,
323 &ChildProcessResourceProvider::ChildProcessDataRetreived
,
324 this, child_processes
));
327 // This is called on the UI thread.
328 void ChildProcessResourceProvider::ChildProcessDataRetreived(
329 const std::vector
<content::ChildProcessData
>& child_processes
) {
330 for (size_t i
= 0; i
< child_processes
.size(); ++i
)
331 AddToTaskManager(child_processes
[i
]);
333 task_manager_
->model()->NotifyDataReady();
336 } // namespace task_manager