Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(558)

Side by Side Diff: chrome/browser/task_manager/child_process_resource_provider.cc

Issue 15196003: Create task_manager namespace and wrap classes related to TaskManager with it. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: RendererResource Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/task_manager/task_manager_child_process_resource_provid er.h" 5 #include "chrome/browser/task_manager/child_process_resource_provider.h"
6 6
7 #include "base/basictypes.h" 7 #include <vector>
8
8 #include "base/i18n/rtl.h" 9 #include "base/i18n/rtl.h"
9 #include "base/string16.h" 10 #include "base/string16.h"
10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/chrome_notification_types.h" 11 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/chrome_process_type.h" 12 #include "chrome/common/chrome_process_type.h"
14 #include "content/public/browser/browser_child_process_host_iterator.h" 13 #include "content/public/browser/browser_child_process_host_iterator.h"
15 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/child_process_data.h"
16 #include "content/public/browser/notification_service.h" 16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/web_contents.h"
18 #include "grit/generated_resources.h" 17 #include "grit/generated_resources.h"
19 #include "grit/theme_resources.h" 18 #include "grit/theme_resources.h"
20 #include "ui/base/l10n/l10n_util.h" 19 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/resource/resource_bundle.h" 20 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/gfx/image/image_skia.h" 21 #include "ui/gfx/image/image_skia.h"
23 22
24 using content::BrowserChildProcessHostIterator; 23 using content::BrowserChildProcessHostIterator;
25 using content::BrowserThread; 24 using content::BrowserThread;
26 using content::WebContents; 25 using content::WebContents;
27 26
28 //////////////////////////////////////////////////////////////////////////////// 27 namespace task_manager {
29 // TaskManagerChildProcessResource class
30 ////////////////////////////////////////////////////////////////////////////////
31 gfx::ImageSkia* TaskManagerChildProcessResource::default_icon_ = NULL;
32 28
33 TaskManagerChildProcessResource::TaskManagerChildProcessResource( 29 class ChildProcessResource : public TaskManager::Resource {
30 public:
31 ChildProcessResource(int process_type,
32 const string16& name,
33 base::ProcessHandle handle,
34 int unique_process_id);
35 virtual ~ChildProcessResource();
36
37 // TaskManager::Resource methods:
38 virtual string16 GetTitle() const OVERRIDE;
39 virtual string16 GetProfileName() const OVERRIDE;
40 virtual gfx::ImageSkia GetIcon() const OVERRIDE;
41 virtual base::ProcessHandle GetProcess() const OVERRIDE;
42 virtual int GetUniqueChildProcessId() const OVERRIDE;
43 virtual Type GetType() const OVERRIDE;
44 virtual bool SupportNetworkUsage() const OVERRIDE;
45 virtual void SetSupportNetworkUsage() OVERRIDE;
46
47 // Returns the pid of the child process.
48 int process_id() const { return pid_; }
49
50 private:
51 // Returns a localized title for the child process. For example, a plugin
52 // process would be "Plug-in: Flash" when name is "Flash".
53 string16 GetLocalizedTitle() const;
54
55 int process_type_;
56 string16 name_;
57 base::ProcessHandle handle_;
58 int pid_;
59 int unique_process_id_;
60 mutable string16 title_;
61 bool network_usage_support_;
62
63 // The icon painted for the child processs.
64 // TODO(jcampan): we should have plugin specific icons for well-known
65 // plugins.
66 static gfx::ImageSkia* default_icon_;
67
68 DISALLOW_COPY_AND_ASSIGN(ChildProcessResource);
69 };
70
71 gfx::ImageSkia* ChildProcessResource::default_icon_ = NULL;
72
73 ChildProcessResource::ChildProcessResource(
34 int process_type, 74 int process_type,
35 const string16& name, 75 const string16& name,
36 base::ProcessHandle handle, 76 base::ProcessHandle handle,
37 int unique_process_id) 77 int unique_process_id)
38 : process_type_(process_type), 78 : process_type_(process_type),
39 name_(name), 79 name_(name),
40 handle_(handle), 80 handle_(handle),
41 unique_process_id_(unique_process_id), 81 unique_process_id_(unique_process_id),
42 network_usage_support_(false) { 82 network_usage_support_(false) {
43 // We cache the process id because it's not cheap to calculate, and it won't 83 // We cache the process id because it's not cheap to calculate, and it won't
44 // be available when we get the plugin disconnected notification. 84 // be available when we get the plugin disconnected notification.
45 pid_ = base::GetProcId(handle); 85 pid_ = base::GetProcId(handle);
46 if (!default_icon_) { 86 if (!default_icon_) {
47 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 87 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
48 default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON); 88 default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON);
49 // TODO(jabdelmalek): use different icon for web workers. 89 // TODO(jabdelmalek): use different icon for web workers.
50 } 90 }
51 } 91 }
52 92
53 TaskManagerChildProcessResource::~TaskManagerChildProcessResource() { 93 ChildProcessResource::~ChildProcessResource() {
54 } 94 }
55 95
56 // TaskManagerResource methods: 96 // TaskManagerResource methods:
57 string16 TaskManagerChildProcessResource::GetTitle() const { 97 string16 ChildProcessResource::GetTitle() const {
58 if (title_.empty()) 98 if (title_.empty())
59 title_ = GetLocalizedTitle(); 99 title_ = GetLocalizedTitle();
60 100
61 return title_; 101 return title_;
62 } 102 }
63 103
64 string16 TaskManagerChildProcessResource::GetProfileName() const { 104 string16 ChildProcessResource::GetProfileName() const {
65 return string16(); 105 return string16();
66 } 106 }
67 107
68 gfx::ImageSkia TaskManagerChildProcessResource::GetIcon() const { 108 gfx::ImageSkia ChildProcessResource::GetIcon() const {
69 return *default_icon_; 109 return *default_icon_;
70 } 110 }
71 111
72 base::ProcessHandle TaskManagerChildProcessResource::GetProcess() const { 112 base::ProcessHandle ChildProcessResource::GetProcess() const {
73 return handle_; 113 return handle_;
74 } 114 }
75 115
76 int TaskManagerChildProcessResource::GetUniqueChildProcessId() const { 116 int ChildProcessResource::GetUniqueChildProcessId() const {
77 return unique_process_id_; 117 return unique_process_id_;
78 } 118 }
79 119
80 TaskManager::Resource::Type TaskManagerChildProcessResource::GetType() const { 120 TaskManager::Resource::Type ChildProcessResource::GetType() const {
81 // Translate types to TaskManager::ResourceType, since ChildProcessData's type 121 // Translate types to TaskManager::ResourceType, since ChildProcessData's type
82 // is not available for all TaskManager resources. 122 // is not available for all TaskManager resources.
83 switch (process_type_) { 123 switch (process_type_) {
84 case content::PROCESS_TYPE_PLUGIN: 124 case content::PROCESS_TYPE_PLUGIN:
85 case content::PROCESS_TYPE_PPAPI_PLUGIN: 125 case content::PROCESS_TYPE_PPAPI_PLUGIN:
86 case content::PROCESS_TYPE_PPAPI_BROKER: 126 case content::PROCESS_TYPE_PPAPI_BROKER:
87 return TaskManager::Resource::PLUGIN; 127 return TaskManager::Resource::PLUGIN;
88 case content::PROCESS_TYPE_UTILITY: 128 case content::PROCESS_TYPE_UTILITY:
89 return TaskManager::Resource::UTILITY; 129 return TaskManager::Resource::UTILITY;
90 case content::PROCESS_TYPE_ZYGOTE: 130 case content::PROCESS_TYPE_ZYGOTE:
91 return TaskManager::Resource::ZYGOTE; 131 return TaskManager::Resource::ZYGOTE;
92 case content::PROCESS_TYPE_SANDBOX_HELPER: 132 case content::PROCESS_TYPE_SANDBOX_HELPER:
93 return TaskManager::Resource::SANDBOX_HELPER; 133 return TaskManager::Resource::SANDBOX_HELPER;
94 case content::PROCESS_TYPE_GPU: 134 case content::PROCESS_TYPE_GPU:
95 return TaskManager::Resource::GPU; 135 return TaskManager::Resource::GPU;
96 case PROCESS_TYPE_PROFILE_IMPORT: 136 case PROCESS_TYPE_PROFILE_IMPORT:
97 return TaskManager::Resource::PROFILE_IMPORT; 137 return TaskManager::Resource::PROFILE_IMPORT;
98 case PROCESS_TYPE_NACL_LOADER: 138 case PROCESS_TYPE_NACL_LOADER:
99 case PROCESS_TYPE_NACL_BROKER: 139 case PROCESS_TYPE_NACL_BROKER:
100 return TaskManager::Resource::NACL; 140 return TaskManager::Resource::NACL;
101 default: 141 default:
102 return TaskManager::Resource::UNKNOWN; 142 return TaskManager::Resource::UNKNOWN;
103 } 143 }
104 } 144 }
105 145
106 bool TaskManagerChildProcessResource::SupportNetworkUsage() const { 146 bool ChildProcessResource::SupportNetworkUsage() const {
107 return network_usage_support_; 147 return network_usage_support_;
108 } 148 }
109 149
110 void TaskManagerChildProcessResource::SetSupportNetworkUsage() { 150 void ChildProcessResource::SetSupportNetworkUsage() {
111 network_usage_support_ = true; 151 network_usage_support_ = true;
112 } 152 }
113 153
114 string16 TaskManagerChildProcessResource::GetLocalizedTitle() const { 154 string16 ChildProcessResource::GetLocalizedTitle() const {
115 string16 title = name_; 155 string16 title = name_;
116 if (title.empty()) { 156 if (title.empty()) {
117 switch (process_type_) { 157 switch (process_type_) {
118 case content::PROCESS_TYPE_PLUGIN: 158 case content::PROCESS_TYPE_PLUGIN:
119 case content::PROCESS_TYPE_PPAPI_PLUGIN: 159 case content::PROCESS_TYPE_PPAPI_PLUGIN:
120 case content::PROCESS_TYPE_PPAPI_BROKER: 160 case content::PROCESS_TYPE_PPAPI_BROKER:
121 title = l10n_util::GetStringUTF16(IDS_TASK_MANAGER_UNKNOWN_PLUGIN_NAME); 161 title = l10n_util::GetStringUTF16(IDS_TASK_MANAGER_UNKNOWN_PLUGIN_NAME);
122 break; 162 break;
123 default: 163 default:
124 // Nothing to do for non-plugin processes. 164 // Nothing to do for non-plugin processes.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 NOTREACHED() << "Workers are not handled by this provider."; 202 NOTREACHED() << "Workers are not handled by this provider.";
163 break; 203 break;
164 case content::PROCESS_TYPE_UNKNOWN: 204 case content::PROCESS_TYPE_UNKNOWN:
165 NOTREACHED() << "Need localized name for child process type."; 205 NOTREACHED() << "Need localized name for child process type.";
166 } 206 }
167 207
168 return title; 208 return title;
169 } 209 }
170 210
171 //////////////////////////////////////////////////////////////////////////////// 211 ////////////////////////////////////////////////////////////////////////////////
172 // TaskManagerChildProcessResourceProvider class 212 // ChildProcessResourceProvider class
173 //////////////////////////////////////////////////////////////////////////////// 213 ////////////////////////////////////////////////////////////////////////////////
174 214
175 TaskManagerChildProcessResourceProvider:: 215 ChildProcessResourceProvider::
176 TaskManagerChildProcessResourceProvider(TaskManager* task_manager) 216 ChildProcessResourceProvider(TaskManager* task_manager)
177 : task_manager_(task_manager), 217 : task_manager_(task_manager),
178 updating_(false) { 218 updating_(false) {
179 } 219 }
180 220
181 TaskManagerChildProcessResourceProvider:: 221 ChildProcessResourceProvider::~ChildProcessResourceProvider() {
182 ~TaskManagerChildProcessResourceProvider() {
183 } 222 }
184 223
185 TaskManager::Resource* TaskManagerChildProcessResourceProvider::GetResource( 224 TaskManager::Resource* ChildProcessResourceProvider::GetResource(
186 int origin_pid, 225 int origin_pid,
187 int render_process_host_id, 226 int render_process_host_id,
188 int routing_id) { 227 int routing_id) {
189 PidResourceMap::iterator iter = pid_to_resources_.find(origin_pid); 228 PidResourceMap::iterator iter = pid_to_resources_.find(origin_pid);
190 if (iter != pid_to_resources_.end()) 229 if (iter != pid_to_resources_.end())
191 return iter->second; 230 return iter->second;
192 else 231 else
193 return NULL; 232 return NULL;
194 } 233 }
195 234
196 void TaskManagerChildProcessResourceProvider::StartUpdating() { 235 void ChildProcessResourceProvider::StartUpdating() {
197 DCHECK(!updating_); 236 DCHECK(!updating_);
198 updating_ = true; 237 updating_ = true;
199 238
200 // Get the existing child processes. 239 // Get the existing child processes.
201 BrowserThread::PostTask( 240 BrowserThread::PostTask(
202 BrowserThread::IO, FROM_HERE, 241 BrowserThread::IO, FROM_HERE,
203 base::Bind( 242 base::Bind(
204 &TaskManagerChildProcessResourceProvider::RetrieveChildProcessData, 243 &ChildProcessResourceProvider::RetrieveChildProcessData,
205 this)); 244 this));
206 245
207 BrowserChildProcessObserver::Add(this); 246 BrowserChildProcessObserver::Add(this);
208 } 247 }
209 248
210 void TaskManagerChildProcessResourceProvider::StopUpdating() { 249 void ChildProcessResourceProvider::StopUpdating() {
211 DCHECK(updating_); 250 DCHECK(updating_);
212 updating_ = false; 251 updating_ = false;
213 252
214 // Delete all the resources. 253 // Delete all the resources.
215 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); 254 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end());
216 255
217 resources_.clear(); 256 resources_.clear();
218 pid_to_resources_.clear(); 257 pid_to_resources_.clear();
219 258
220 BrowserChildProcessObserver::Remove(this); 259 BrowserChildProcessObserver::Remove(this);
221 } 260 }
222 261
223 void TaskManagerChildProcessResourceProvider::BrowserChildProcessHostConnected( 262 void ChildProcessResourceProvider::BrowserChildProcessHostConnected(
224 const content::ChildProcessData& data) { 263 const content::ChildProcessData& data) {
225 DCHECK(updating_); 264 DCHECK(updating_);
226 265
227 // Workers are handled by TaskManagerWorkerResourceProvider. 266 // Workers are handled by TaskManagerWorkerResourceProvider.
228 if (data.process_type == content::PROCESS_TYPE_WORKER) 267 if (data.process_type == content::PROCESS_TYPE_WORKER)
229 return; 268 return;
230 if (resources_.count(data.handle)) { 269 if (resources_.count(data.handle)) {
231 // The case may happen that we have added a child_process_info as part of 270 // The case may happen that we have added a child_process_info as part of
232 // the iteration performed during StartUpdating() call but the notification 271 // the iteration performed during StartUpdating() call but the notification
233 // that it has connected was not fired yet. So when the notification 272 // that it has connected was not fired yet. So when the notification
234 // happens, we already know about this plugin and just ignore it. 273 // happens, we already know about this plugin and just ignore it.
235 return; 274 return;
236 } 275 }
237 AddToTaskManager(data); 276 AddToTaskManager(data);
238 } 277 }
239 278
240 void TaskManagerChildProcessResourceProvider:: 279 void ChildProcessResourceProvider::
241 BrowserChildProcessHostDisconnected(const content::ChildProcessData& data) { 280 BrowserChildProcessHostDisconnected(
281 const content::ChildProcessData& data) {
242 DCHECK(updating_); 282 DCHECK(updating_);
243 283
244 if (data.process_type == content::PROCESS_TYPE_WORKER) 284 if (data.process_type == content::PROCESS_TYPE_WORKER)
245 return; 285 return;
246 ChildProcessMap::iterator iter = resources_.find(data.handle); 286 ChildProcessMap::iterator iter = resources_.find(data.handle);
247 if (iter == resources_.end()) { 287 if (iter == resources_.end()) {
248 // ChildProcessData disconnection notifications are asynchronous, so we 288 // ChildProcessData disconnection notifications are asynchronous, so we
249 // might be notified for a plugin we don't know anything about (if it was 289 // might be notified for a plugin we don't know anything about (if it was
250 // closed before the task manager was shown and destroyed after that). 290 // closed before the task manager was shown and destroyed after that).
251 return; 291 return;
252 } 292 }
253 // Remove the resource from the Task Manager. 293 // Remove the resource from the Task Manager.
254 TaskManagerChildProcessResource* resource = iter->second; 294 ChildProcessResource* resource = iter->second;
255 task_manager_->RemoveResource(resource); 295 task_manager_->RemoveResource(resource);
256 // Remove it from the provider. 296 // Remove it from the provider.
257 resources_.erase(iter); 297 resources_.erase(iter);
258 // Remove it from our pid map. 298 // Remove it from our pid map.
259 PidResourceMap::iterator pid_iter = 299 PidResourceMap::iterator pid_iter =
260 pid_to_resources_.find(resource->process_id()); 300 pid_to_resources_.find(resource->process_id());
261 DCHECK(pid_iter != pid_to_resources_.end()); 301 DCHECK(pid_iter != pid_to_resources_.end());
262 if (pid_iter != pid_to_resources_.end()) 302 if (pid_iter != pid_to_resources_.end())
263 pid_to_resources_.erase(pid_iter); 303 pid_to_resources_.erase(pid_iter);
264 304
265 // Finally, delete the resource. 305 // Finally, delete the resource.
266 delete resource; 306 delete resource;
267 } 307 }
268 308
269 void TaskManagerChildProcessResourceProvider::AddToTaskManager( 309 void ChildProcessResourceProvider::AddToTaskManager(
270 const content::ChildProcessData& child_process_data) { 310 const content::ChildProcessData& child_process_data) {
271 TaskManagerChildProcessResource* resource = 311 ChildProcessResource* resource =
272 new TaskManagerChildProcessResource( 312 new ChildProcessResource(
273 child_process_data.process_type, 313 child_process_data.process_type,
274 child_process_data.name, 314 child_process_data.name,
275 child_process_data.handle, 315 child_process_data.handle,
276 child_process_data.id); 316 child_process_data.id);
277 resources_[child_process_data.handle] = resource; 317 resources_[child_process_data.handle] = resource;
278 pid_to_resources_[resource->process_id()] = resource; 318 pid_to_resources_[resource->process_id()] = resource;
279 task_manager_->AddResource(resource); 319 task_manager_->AddResource(resource);
280 } 320 }
281 321
282 // The ChildProcessData::Iterator has to be used from the IO thread. 322 // The ChildProcessData::Iterator has to be used from the IO thread.
283 void TaskManagerChildProcessResourceProvider::RetrieveChildProcessData() { 323 void ChildProcessResourceProvider::RetrieveChildProcessData() {
284 std::vector<content::ChildProcessData> child_processes; 324 std::vector<content::ChildProcessData> child_processes;
285 for (BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) { 325 for (BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) {
286 // Only add processes which are already started, since we need their handle. 326 // Only add processes which are already started, since we need their handle.
287 if (iter.GetData().handle == base::kNullProcessHandle) 327 if (iter.GetData().handle == base::kNullProcessHandle)
288 continue; 328 continue;
289 if (iter.GetData().process_type == content::PROCESS_TYPE_WORKER) 329 if (iter.GetData().process_type == content::PROCESS_TYPE_WORKER)
290 continue; 330 continue;
291 child_processes.push_back(iter.GetData()); 331 child_processes.push_back(iter.GetData());
292 } 332 }
293 // Now notify the UI thread that we have retrieved information about child 333 // Now notify the UI thread that we have retrieved information about child
294 // processes. 334 // processes.
295 BrowserThread::PostTask( 335 BrowserThread::PostTask(
296 BrowserThread::UI, FROM_HERE, 336 BrowserThread::UI, FROM_HERE,
297 base::Bind( 337 base::Bind(
298 &TaskManagerChildProcessResourceProvider::ChildProcessDataRetreived, 338 &ChildProcessResourceProvider::ChildProcessDataRetreived,
299 this, child_processes)); 339 this, child_processes));
300 } 340 }
301 341
302 // This is called on the UI thread. 342 // This is called on the UI thread.
303 void TaskManagerChildProcessResourceProvider::ChildProcessDataRetreived( 343 void ChildProcessResourceProvider::ChildProcessDataRetreived(
304 const std::vector<content::ChildProcessData>& child_processes) { 344 const std::vector<content::ChildProcessData>& child_processes) {
305 for (size_t i = 0; i < child_processes.size(); ++i) 345 for (size_t i = 0; i < child_processes.size(); ++i)
306 AddToTaskManager(child_processes[i]); 346 AddToTaskManager(child_processes[i]);
307 347
308 content::NotificationService::current()->Notify( 348 content::NotificationService::current()->Notify(
309 chrome::NOTIFICATION_TASK_MANAGER_CHILD_PROCESSES_DATA_READY, 349 chrome::NOTIFICATION_TASK_MANAGER_CHILD_PROCESSES_DATA_READY,
310 content::Source<TaskManagerChildProcessResourceProvider>(this), 350 content::Source<ChildProcessResourceProvider>(this),
311 content::NotificationService::NoDetails()); 351 content::NotificationService::NoDetails());
312 } 352 }
353
354 } // namespace task_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698