1 // Copyright (c) 2011 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. 4 5 #ifndef CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_RESOURCE_PROVIDERS_H_ 6 #define CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_RESOURCE_PROVIDERS_H_ 7 #pragma once 8 9 #include <map> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/compiler_specific.h" 14 #include "base/process_util.h" 15 #include "chrome/browser/task_manager/task_manager.h" 16 #include "content/common/child_process_info.h" 17 #include "content/common/notification_observer.h" 18 #include "content/common/notification_registrar.h" 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCache.h" 20 21 class BackgroundContents; 22 class BalloonHost; 23 class Extension; 24 class ExtensionHost; 25 class RenderViewHost; 26 class TabContentsWrapper; 27 28 namespace prerender { 29 class PrerenderContents; 30 } 31 32 // These file contains the resource providers used in the task manager. 33 34 // Base class for various types of render process resources that provides common 35 // functionality like stats tracking. 36 class TaskManagerRendererResource : public TaskManager::Resource { 37 public: 38 TaskManagerRendererResource(base::ProcessHandle process, 39 RenderViewHost* render_view_host); 40 virtual ~TaskManagerRendererResource(); 41 42 // TaskManager::Resource methods: 43 virtual base::ProcessHandle GetProcess() const OVERRIDE; 44 virtual Type GetType() const OVERRIDE; 45 virtual bool ReportsCacheStats() const OVERRIDE; 46 virtual WebKit::WebCache::ResourceTypeStats GetWebCoreCacheStats() const 47 OVERRIDE; 48 virtual bool ReportsV8MemoryStats() const OVERRIDE; 49 virtual size_t GetV8MemoryAllocated() const OVERRIDE; 50 virtual size_t GetV8MemoryUsed() const OVERRIDE; 51 52 // RenderResources always provide the network usage. 53 virtual bool SupportNetworkUsage() const; 54 virtual void SetSupportNetworkUsage() { } 55 56 virtual void Refresh(); 57 58 virtual void NotifyResourceTypeStats( 59 const WebKit::WebCache::ResourceTypeStats& stats); 60 61 virtual void NotifyV8HeapStats(size_t v8_memory_allocated, 62 size_t v8_memory_used); 63 64 private: 65 base::ProcessHandle process_; 66 int pid_; 67 68 // RenderViewHost we use to fetch stats. 69 RenderViewHost* render_view_host_; 70 // The stats_ field holds information about resource usage in the renderer 71 // process and so it is updated asynchronously by the Refresh() call. 72 WebKit::WebCache::ResourceTypeStats stats_; 73 // This flag is true if we are waiting for the renderer to report its stats. 74 bool pending_stats_update_; 75 76 // We do a similar dance to gather the V8 memory usage in a process. 77 size_t v8_memory_allocated_; 78 size_t v8_memory_used_; 79 bool pending_v8_memory_allocated_update_; 80 81 DISALLOW_COPY_AND_ASSIGN(TaskManagerRendererResource); 82 }; 83 84 class TaskManagerTabContentsResource : public TaskManagerRendererResource { 85 public: 86 explicit TaskManagerTabContentsResource(TabContentsWrapper* tab_contents); 87 virtual ~TaskManagerTabContentsResource(); 88 89 // TaskManager::Resource methods: 90 virtual Type GetType() const OVERRIDE; 91 virtual string16 GetTitle() const OVERRIDE; 92 virtual SkBitmap GetIcon() const OVERRIDE; 93 virtual TabContentsWrapper* GetTabContents() const OVERRIDE; 94 virtual const Extension* GetExtension() const OVERRIDE; 95 96 private: 97 TabContentsWrapper* tab_contents_; 98 99 DISALLOW_COPY_AND_ASSIGN(TaskManagerTabContentsResource); 100 }; 101 102 class TaskManagerTabContentsResourceProvider 103 : public TaskManager::ResourceProvider, 104 public NotificationObserver { 105 public: 106 explicit TaskManagerTabContentsResourceProvider(TaskManager* task_manager); 107 108 virtual TaskManager::Resource* GetResource(int origin_pid, 109 int render_process_host_id, 110 int routing_id); 111 virtual void StartUpdating(); 112 virtual void StopUpdating(); 113 114 // NotificationObserver method: 115 virtual void Observe(NotificationType type, 116 const NotificationSource& source, 117 const NotificationDetails& details); 118 119 private: 120 virtual ~TaskManagerTabContentsResourceProvider(); 121 122 void Add(TabContentsWrapper* tab_contents); 123 void Remove(TabContentsWrapper* tab_contents); 124 125 void AddToTaskManager(TabContentsWrapper* tab_contents); 126 127 // Whether we are currently reporting to the task manager. Used to ignore 128 // notifications sent after StopUpdating(). 129 bool updating_; 130 131 TaskManager* task_manager_; 132 133 // Maps the actual resources (the TabContentsWrappers) to the Task Manager 134 // resources. 135 std::map<TabContentsWrapper*, TaskManagerTabContentsResource*> resources_; 136 137 // A scoped container for notification registries. 138 NotificationRegistrar registrar_; 139 140 DISALLOW_COPY_AND_ASSIGN(TaskManagerTabContentsResourceProvider); 141 }; 142 143 class TaskManagerPrerenderResource : public TaskManagerRendererResource { 144 public: 145 explicit TaskManagerPrerenderResource(RenderViewHost* render_view_host); 146 virtual ~TaskManagerPrerenderResource(); 147 148 // TaskManager::Resource methods: 149 virtual Type GetType() const OVERRIDE; 150 virtual string16 GetTitle() const OVERRIDE; 151 virtual SkBitmap GetIcon() const OVERRIDE; 152 153 private: 154 static SkBitmap* default_icon_; 155 std::pair<int, int> process_route_id_pair_; 156 157 DISALLOW_COPY_AND_ASSIGN(TaskManagerPrerenderResource); 158 }; 159 160 class TaskManagerPrerenderResourceProvider 161 : public TaskManager::ResourceProvider, 162 public NotificationObserver { 163 public: 164 explicit TaskManagerPrerenderResourceProvider(TaskManager* task_manager); 165 166 virtual TaskManager::Resource* GetResource(int origin_pid, 167 int render_process_host_id, 168 int routing_id); 169 virtual void StartUpdating(); 170 virtual void StopUpdating(); 171 172 virtual void Observe(NotificationType type, 173 const NotificationSource& source, 174 const NotificationDetails& details); 175 private: 176 virtual ~TaskManagerPrerenderResourceProvider(); 177 178 void Add(const std::pair<int, int>& process_route_id_pair); 179 void Remove(const std::pair<int, int>& process_route_id_pair); 180 181 void AddToTaskManager(const std::pair<int, int>& process_route_id_pair); 182 183 bool updating_; 184 TaskManager* task_manager_; 185 typedef std::map<std::pair<int, int>, TaskManagerPrerenderResource*> 186 ResourceMap; 187 ResourceMap resources_; 188 NotificationRegistrar registrar_; 189 190 DISALLOW_COPY_AND_ASSIGN(TaskManagerPrerenderResourceProvider); 191 }; 192 193 class TaskManagerBackgroundContentsResource 194 : public TaskManagerRendererResource { 195 public: 196 TaskManagerBackgroundContentsResource( 197 BackgroundContents* background_contents, 198 const string16& application_name); 199 virtual ~TaskManagerBackgroundContentsResource(); 200 201 // TaskManager::Resource methods: 202 virtual string16 GetTitle() const OVERRIDE; 203 virtual SkBitmap GetIcon() const OVERRIDE; 204 virtual bool IsBackground() const OVERRIDE; 205 206 const string16& application_name() const { return application_name_; } 207 private: 208 BackgroundContents* background_contents_; 209 210 string16 application_name_; 211 212 // The icon painted for BackgroundContents. 213 // TODO(atwilson): Use the favicon when there's a way to get the favicon for 214 // BackgroundContents. 215 static SkBitmap* default_icon_; 216 217 DISALLOW_COPY_AND_ASSIGN(TaskManagerBackgroundContentsResource); 218 }; 219 220 class TaskManagerBackgroundContentsResourceProvider 221 : public TaskManager::ResourceProvider, 222 public NotificationObserver { 223 public: 224 explicit TaskManagerBackgroundContentsResourceProvider( 225 TaskManager* task_manager); 226 227 virtual TaskManager::Resource* GetResource(int origin_pid, 228 int render_process_host_id, 229 int routing_id); 230 virtual void StartUpdating(); 231 virtual void StopUpdating(); 232 233 // NotificationObserver method: 234 virtual void Observe(NotificationType type, 235 const NotificationSource& source, 236 const NotificationDetails& details); 237 238 private: 239 virtual ~TaskManagerBackgroundContentsResourceProvider(); 240 241 void Add(BackgroundContents* background_contents, const string16& title); 242 void Remove(BackgroundContents* background_contents); 243 244 void AddToTaskManager(BackgroundContents* background_contents, 245 const string16& title); 246 247 // Whether we are currently reporting to the task manager. Used to ignore 248 // notifications sent after StopUpdating(). 249 bool updating_; 250 251 TaskManager* task_manager_; 252 253 // Maps the actual resources (the BackgroundContents) to the Task Manager 254 // resources. 255 std::map<BackgroundContents*, TaskManagerBackgroundContentsResource*> 256 resources_; 257 258 // A scoped container for notification registries. 259 NotificationRegistrar registrar_; 260 261 DISALLOW_COPY_AND_ASSIGN(TaskManagerBackgroundContentsResourceProvider); 262 }; 263 264 class TaskManagerChildProcessResource : public TaskManager::Resource { 265 public: 266 explicit TaskManagerChildProcessResource(const ChildProcessInfo& child_proc); 267 virtual ~TaskManagerChildProcessResource(); 268 269 // TaskManager::Resource methods: 270 virtual string16 GetTitle() const OVERRIDE; 271 virtual SkBitmap GetIcon() const OVERRIDE; 272 virtual base::ProcessHandle GetProcess() const OVERRIDE; 273 virtual Type GetType() const OVERRIDE; 274 virtual bool SupportNetworkUsage() const OVERRIDE; 275 virtual void SetSupportNetworkUsage() OVERRIDE; 276 277 // Returns the pid of the child process. 278 int process_id() const { return pid_; } 279 280 private: 281 // Returns a localized title for the child process. For example, a plugin 282 // process would be "Plug-in: Flash" when name is "Flash". 283 string16 GetLocalizedTitle() const; 284 285 ChildProcessInfo child_process_; 286 int pid_; 287 mutable string16 title_; 288 bool network_usage_support_; 289 290 // The icon painted for the child processs. 291 // TODO(jcampan): we should have plugin specific icons for well-known 292 // plugins. 293 static SkBitmap* default_icon_; 294 295 DISALLOW_COPY_AND_ASSIGN(TaskManagerChildProcessResource); 296 }; 297 298 class TaskManagerChildProcessResourceProvider 299 : public TaskManager::ResourceProvider, 300 public NotificationObserver { 301 public: 302 explicit TaskManagerChildProcessResourceProvider(TaskManager* task_manager); 303 304 virtual TaskManager::Resource* GetResource(int origin_pid, 305 int render_process_host_id, 306 int routing_id); 307 virtual void StartUpdating(); 308 virtual void StopUpdating(); 309 310 // NotificationObserver method: 311 virtual void Observe(NotificationType type, 312 const NotificationSource& source, 313 const NotificationDetails& details); 314 315 // Retrieves the current ChildProcessInfo (performed in the IO thread). 316 virtual void RetrieveChildProcessInfo(); 317 318 // Notifies the UI thread that the ChildProcessInfo have been retrieved. 319 virtual void ChildProcessInfoRetreived(); 320 321 // Whether we are currently reporting to the task manager. Used to ignore 322 // notifications sent after StopUpdating(). 323 bool updating_; 324 325 // The list of ChildProcessInfo retrieved when starting the update. 326 std::vector<ChildProcessInfo> existing_child_process_info_; 327 328 private: 329 virtual ~TaskManagerChildProcessResourceProvider(); 330 331 void Add(const ChildProcessInfo& child_process_info); 332 void Remove(const ChildProcessInfo& child_process_info); 333 334 void AddToTaskManager(const ChildProcessInfo& child_process_info); 335 336 TaskManager* task_manager_; 337 338 // Maps the actual resources (the ChildProcessInfo) to the Task Manager 339 // resources. 340 std::map<ChildProcessInfo, TaskManagerChildProcessResource*> resources_; 341 342 // Maps the pids to the resources (used for quick access to the resource on 343 // byte read notifications). 344 std::map<int, TaskManagerChildProcessResource*> pid_to_resources_; 345 346 // A scoped container for notification registries. 347 NotificationRegistrar registrar_; 348 349 DISALLOW_COPY_AND_ASSIGN(TaskManagerChildProcessResourceProvider); 350 }; 351 352 class TaskManagerExtensionProcessResource : public TaskManager::Resource { 353 public: 354 explicit TaskManagerExtensionProcessResource(ExtensionHost* extension_host); 355 virtual ~TaskManagerExtensionProcessResource(); 356 357 // TaskManager::Resource methods: 358 virtual string16 GetTitle() const OVERRIDE; 359 virtual SkBitmap GetIcon() const OVERRIDE; 360 virtual base::ProcessHandle GetProcess() const OVERRIDE; 361 virtual Type GetType() const OVERRIDE; 362 virtual bool SupportNetworkUsage() const OVERRIDE; 363 virtual void SetSupportNetworkUsage() OVERRIDE; 364 virtual const Extension* GetExtension() const OVERRIDE; 365 366 // Returns the pid of the extension process. 367 int process_id() const { return pid_; } 368 369 // Returns true if the associated extension has a background page. 370 virtual bool IsBackground() const; 371 372 private: 373 // The icon painted for the extension process. 374 static SkBitmap* default_icon_; 375 376 ExtensionHost* extension_host_; 377 378 // Cached data about the extension. 379 base::ProcessHandle process_handle_; 380 int pid_; 381 string16 title_; 382 383 DISALLOW_COPY_AND_ASSIGN(TaskManagerExtensionProcessResource); 384 }; 385 386 class TaskManagerExtensionProcessResourceProvider 387 : public TaskManager::ResourceProvider, 388 public NotificationObserver { 389 public: 390 explicit TaskManagerExtensionProcessResourceProvider( 391 TaskManager* task_manager); 392 393 virtual TaskManager::Resource* GetResource(int origin_pid, 394 int render_process_host_id, 395 int routing_id); 396 virtual void StartUpdating(); 397 virtual void StopUpdating(); 398 399 // NotificationObserver method: 400 virtual void Observe(NotificationType type, 401 const NotificationSource& source, 402 const NotificationDetails& details); 403 404 private: 405 virtual ~TaskManagerExtensionProcessResourceProvider(); 406 407 void AddToTaskManager(ExtensionHost* extension_host); 408 void RemoveFromTaskManager(ExtensionHost* extension_host); 409 410 TaskManager* task_manager_; 411 412 // Maps the actual resources (ExtensionHost*) to the Task Manager resources. 413 std::map<ExtensionHost*, TaskManagerExtensionProcessResource*> resources_; 414 415 // Maps the pids to the resources (used for quick access to the resource on 416 // byte read notifications). 417 std::map<int, TaskManagerExtensionProcessResource*> pid_to_resources_; 418 419 // A scoped container for notification registries. 420 NotificationRegistrar registrar_; 421 422 bool updating_; 423 424 DISALLOW_COPY_AND_ASSIGN(TaskManagerExtensionProcessResourceProvider); 425 }; 426 427 class TaskManagerNotificationResource : public TaskManager::Resource { 428 public: 429 explicit TaskManagerNotificationResource(BalloonHost* balloon_host); 430 virtual ~TaskManagerNotificationResource(); 431 432 // TaskManager::Resource interface 433 virtual string16 GetTitle() const OVERRIDE; 434 virtual SkBitmap GetIcon() const OVERRIDE; 435 virtual base::ProcessHandle GetProcess() const OVERRIDE; 436 virtual Type GetType() const OVERRIDE; 437 virtual bool SupportNetworkUsage() const OVERRIDE; 438 virtual void SetSupportNetworkUsage() OVERRIDE { } 439 440 private: 441 // The icon painted for notifications. . 442 static SkBitmap* default_icon_; 443 444 // Non-owned pointer to the balloon host. 445 BalloonHost* balloon_host_; 446 447 // Cached data about the balloon host. 448 base::ProcessHandle process_handle_; 449 int pid_; 450 string16 title_; 451 452 DISALLOW_COPY_AND_ASSIGN(TaskManagerNotificationResource); 453 }; 454 455 class TaskManagerNotificationResourceProvider 456 : public TaskManager::ResourceProvider, 457 public NotificationObserver { 458 public: 459 explicit TaskManagerNotificationResourceProvider(TaskManager* task_manager); 460 461 // TaskManager::ResourceProvider interface 462 virtual TaskManager::Resource* GetResource(int origin_pid, 463 int render_process_host_id, 464 int routing_id); 465 virtual void StartUpdating(); 466 virtual void StopUpdating(); 467 468 // NotificationObserver interface 469 virtual void Observe(NotificationType type, 470 const NotificationSource& source, 471 const NotificationDetails& details); 472 473 private: 474 virtual ~TaskManagerNotificationResourceProvider(); 475 476 void AddToTaskManager(BalloonHost* balloon_host); 477 void RemoveFromTaskManager(BalloonHost* balloon_host); 478 479 TaskManager* task_manager_; 480 481 // Maps the actual resources (BalloonHost*) to the Task Manager resources. 482 std::map<BalloonHost*, TaskManagerNotificationResource*> resources_; 483 484 // A scoped container for notification registries. 485 NotificationRegistrar registrar_; 486 487 bool updating_; 488 489 DISALLOW_COPY_AND_ASSIGN(TaskManagerNotificationResourceProvider); 490 }; 491 492 class TaskManagerBrowserProcessResource : public TaskManager::Resource { 493 public: 494 TaskManagerBrowserProcessResource(); 495 virtual ~TaskManagerBrowserProcessResource(); 496 497 // TaskManager::Resource methods: 498 virtual string16 GetTitle() const OVERRIDE; 499 virtual SkBitmap GetIcon() const OVERRIDE; 500 virtual base::ProcessHandle GetProcess() const OVERRIDE; 501 virtual Type GetType() const OVERRIDE; 502 503 virtual bool SupportNetworkUsage() const OVERRIDE; 504 virtual void SetSupportNetworkUsage() OVERRIDE; 505 506 virtual bool ReportsSqliteMemoryUsed() const OVERRIDE; 507 virtual size_t SqliteMemoryUsedBytes() const OVERRIDE; 508 509 private: 510 base::ProcessHandle process_; 511 mutable string16 title_; 512 513 static SkBitmap* default_icon_; 514 515 DISALLOW_COPY_AND_ASSIGN(TaskManagerBrowserProcessResource); 516 }; 517 518 class TaskManagerBrowserProcessResourceProvider 519 : public TaskManager::ResourceProvider { 520 public: 521 explicit TaskManagerBrowserProcessResourceProvider( 522 TaskManager* task_manager); 523 524 virtual TaskManager::Resource* GetResource(int origin_pid, 525 int render_process_host_id, 526 int routing_id); 527 virtual void StartUpdating(); 528 virtual void StopUpdating(); 529 530 // Whether we are currently reporting to the task manager. Used to ignore 531 // notifications sent after StopUpdating(). 532 bool updating_; 533 534 private: 535 virtual ~TaskManagerBrowserProcessResourceProvider(); 536 537 void AddToTaskManager(ChildProcessInfo child_process_info); 538 539 TaskManager* task_manager_; 540 TaskManagerBrowserProcessResource resource_; 541 542 DISALLOW_COPY_AND_ASSIGN(TaskManagerBrowserProcessResourceProvider); 543 }; 544 545 #endif // CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_RESOURCE_PROVIDERS_H_ 546