| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_resource_providers.h" | 5 #include "chrome/browser/task_manager_resource_providers.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
| 10 #include <atlbase.h> | 10 #include <atlbase.h> |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 // Can happen if the tab was closed while a network request was being | 130 // Can happen if the tab was closed while a network request was being |
| 131 // performed. | 131 // performed. |
| 132 return NULL; | 132 return NULL; |
| 133 | 133 |
| 134 return res_iter->second; | 134 return res_iter->second; |
| 135 } | 135 } |
| 136 | 136 |
| 137 void TaskManagerTabContentsResourceProvider::StartUpdating() { | 137 void TaskManagerTabContentsResourceProvider::StartUpdating() { |
| 138 DCHECK(!updating_); | 138 DCHECK(!updating_); |
| 139 updating_ = true; | 139 updating_ = true; |
| 140 |
| 140 // Add all the existing TabContents. | 141 // Add all the existing TabContents. |
| 141 for (TabContentsIterator iterator; !iterator.done(); iterator++) { | 142 for (TabContentsIterator iterator; !iterator.done(); iterator++) |
| 142 TabContents* tab_contents = *iterator; | 143 Add(*iterator); |
| 143 // Don't add dead tabs or tabs that haven't yet connected. | 144 |
| 144 // Also ignore tabs which display extension content. We collapse | |
| 145 // all of these into one extension row. | |
| 146 if (tab_contents->process()->process().handle() && | |
| 147 tab_contents->notify_disconnection() && | |
| 148 !tab_contents->HostsExtension()) | |
| 149 AddToTaskManager(tab_contents); | |
| 150 } | |
| 151 // Then we register for notifications to get new tabs. | 145 // Then we register for notifications to get new tabs. |
| 152 registrar_.Add(this, NotificationType::TAB_CONTENTS_CONNECTED, | 146 registrar_.Add(this, NotificationType::TAB_CONTENTS_CONNECTED, |
| 153 NotificationService::AllSources()); | 147 NotificationService::AllSources()); |
| 154 registrar_.Add(this, NotificationType::TAB_CONTENTS_SWAPPED, | 148 registrar_.Add(this, NotificationType::TAB_CONTENTS_SWAPPED, |
| 155 NotificationService::AllSources()); | 149 NotificationService::AllSources()); |
| 156 registrar_.Add(this, NotificationType::TAB_CONTENTS_DISCONNECTED, | 150 registrar_.Add(this, NotificationType::TAB_CONTENTS_DISCONNECTED, |
| 157 NotificationService::AllSources()); | 151 NotificationService::AllSources()); |
| 158 // TAB_CONTENTS_DISCONNECTED should be enough to know when to remove a | 152 // TAB_CONTENTS_DISCONNECTED should be enough to know when to remove a |
| 159 // resource. This is an attempt at mitigating a crasher that seem to | 153 // resource. This is an attempt at mitigating a crasher that seem to |
| 160 // indicate a resource is still referencing a deleted TabContents | 154 // indicate a resource is still referencing a deleted TabContents |
| (...skipping 27 matching lines...) Expand all Loading... |
| 188 TaskManagerTabContentsResource* resource = | 182 TaskManagerTabContentsResource* resource = |
| 189 new TaskManagerTabContentsResource(tab_contents); | 183 new TaskManagerTabContentsResource(tab_contents); |
| 190 resources_[tab_contents] = resource; | 184 resources_[tab_contents] = resource; |
| 191 task_manager_->AddResource(resource); | 185 task_manager_->AddResource(resource); |
| 192 } | 186 } |
| 193 | 187 |
| 194 void TaskManagerTabContentsResourceProvider::Add(TabContents* tab_contents) { | 188 void TaskManagerTabContentsResourceProvider::Add(TabContents* tab_contents) { |
| 195 if (!updating_) | 189 if (!updating_) |
| 196 return; | 190 return; |
| 197 | 191 |
| 198 if (!tab_contents->process()->process().handle()) { | 192 // Don't add dead tabs or tabs that haven't yet connected. |
| 199 // Don't add sad tabs, we would have no information to show for them since | 193 // Also ignore tabs which display extension content. We collapse |
| 200 // they have no associated process. | 194 // all of these into one extension row. |
| 195 if (!tab_contents->process()->process().handle() || |
| 196 !tab_contents->notify_disconnection() || |
| 197 tab_contents->HostsExtension()) { |
| 201 return; | 198 return; |
| 202 } | 199 } |
| 203 | 200 |
| 204 std::map<TabContents*, TaskManagerTabContentsResource*>::const_iterator | 201 std::map<TabContents*, TaskManagerTabContentsResource*>::const_iterator |
| 205 iter = resources_.find(tab_contents); | 202 iter = resources_.find(tab_contents); |
| 206 if (iter != resources_.end()) { | 203 if (iter != resources_.end()) { |
| 207 // The case may happen that we have added a TabContents as part of the | 204 // The case may happen that we have added a TabContents as part of the |
| 208 // iteration performed during StartUpdating() call but the notification that | 205 // iteration performed during StartUpdating() call but the notification that |
| 209 // it has connected was not fired yet. So when the notification happens, we | 206 // it has connected was not fired yet. So when the notification happens, we |
| 210 // already know about this tab and just ignore it. | 207 // already know about this tab and just ignore it. |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 pid_to_resources_.find(origin_pid); | 521 pid_to_resources_.find(origin_pid); |
| 525 if (iter != pid_to_resources_.end()) | 522 if (iter != pid_to_resources_.end()) |
| 526 return iter->second; | 523 return iter->second; |
| 527 else | 524 else |
| 528 return NULL; | 525 return NULL; |
| 529 } | 526 } |
| 530 | 527 |
| 531 void TaskManagerExtensionProcessResourceProvider::StartUpdating() { | 528 void TaskManagerExtensionProcessResourceProvider::StartUpdating() { |
| 532 DCHECK(!updating_); | 529 DCHECK(!updating_); |
| 533 updating_ = true; | 530 updating_ = true; |
| 531 |
| 534 // Add all the existing ExtensionHosts. | 532 // Add all the existing ExtensionHosts. |
| 535 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 533 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 536 for (ProfileManager::const_iterator it = profile_manager->begin(); | 534 for (ProfileManager::const_iterator it = profile_manager->begin(); |
| 537 it != profile_manager->end(); ++it) { | 535 it != profile_manager->end(); ++it) { |
| 538 ExtensionProcessManager* process_manager = | 536 ExtensionProcessManager* process_manager = |
| 539 (*it)->GetExtensionProcessManager(); | 537 (*it)->GetExtensionProcessManager(); |
| 540 ExtensionProcessManager::const_iterator jt; | 538 ExtensionProcessManager::const_iterator jt; |
| 541 for (jt = process_manager->begin(); jt != process_manager->end(); ++jt) | 539 for (jt = process_manager->begin(); jt != process_manager->end(); ++jt) |
| 542 AddToTaskManager(*jt); | 540 AddToTaskManager(*jt); |
| 543 } | 541 } |
| 544 // TODO(phajdan.jr): Also look for TabContents which are displaying | 542 |
| 545 // extension content to aggregate network usage. | 543 // Register for notifications to get new extension processes. |
| 546 // TODO(phajdan.jr): Register for notifications. | 544 registrar_.Add(this, NotificationType::EXTENSION_HOST_CREATED, |
| 545 NotificationService::AllSources()); |
| 546 registrar_.Add(this, NotificationType::EXTENSION_HOST_DESTROYED, |
| 547 NotificationService::AllSources()); |
| 547 } | 548 } |
| 548 | 549 |
| 549 void TaskManagerExtensionProcessResourceProvider::StopUpdating() { | 550 void TaskManagerExtensionProcessResourceProvider::StopUpdating() { |
| 550 DCHECK(updating_); | 551 DCHECK(updating_); |
| 551 updating_ = false; | 552 updating_ = false; |
| 552 | 553 |
| 553 // TODO(phajdan.jr): Unregister notifications. | 554 // Unregister for notifications to get new extension processes. |
| 555 registrar_.Remove(this, NotificationType::EXTENSION_HOST_CREATED, |
| 556 NotificationService::AllSources()); |
| 557 registrar_.Remove(this, NotificationType::EXTENSION_HOST_DESTROYED, |
| 558 NotificationService::AllSources()); |
| 554 | 559 |
| 555 // Delete all the resources. | 560 // Delete all the resources. |
| 556 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); | 561 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); |
| 557 | 562 |
| 558 resources_.clear(); | 563 resources_.clear(); |
| 559 pid_to_resources_.clear(); | 564 pid_to_resources_.clear(); |
| 560 } | 565 } |
| 561 | 566 |
| 567 void TaskManagerExtensionProcessResourceProvider::Observe( |
| 568 NotificationType type, |
| 569 const NotificationSource& source, |
| 570 const NotificationDetails& details) { |
| 571 switch (type.value) { |
| 572 case NotificationType::EXTENSION_HOST_CREATED: |
| 573 AddToTaskManager(Details<ExtensionHost>(details).ptr()); |
| 574 break; |
| 575 case NotificationType::EXTENSION_HOST_DESTROYED: |
| 576 RemoveFromTaskManager(Details<ExtensionHost>(details).ptr()); |
| 577 break; |
| 578 default: |
| 579 NOTREACHED() << "Unexpected notification."; |
| 580 return; |
| 581 } |
| 582 } |
| 583 |
| 562 void TaskManagerExtensionProcessResourceProvider::AddToTaskManager( | 584 void TaskManagerExtensionProcessResourceProvider::AddToTaskManager( |
| 563 ExtensionHost* extension_host) { | 585 ExtensionHost* extension_host) { |
| 564 TaskManagerExtensionProcessResource* resource = | 586 TaskManagerExtensionProcessResource* resource = |
| 565 new TaskManagerExtensionProcessResource(extension_host); | 587 new TaskManagerExtensionProcessResource(extension_host); |
| 566 DCHECK(resources_.find(extension_host) == resources_.end()); | 588 DCHECK(resources_.find(extension_host) == resources_.end()); |
| 567 resources_[extension_host] = resource; | 589 resources_[extension_host] = resource; |
| 568 pid_to_resources_[resource->process_id()] = resource; | 590 pid_to_resources_[resource->process_id()] = resource; |
| 569 task_manager_->AddResource(resource); | 591 task_manager_->AddResource(resource); |
| 570 } | 592 } |
| 571 | 593 |
| 594 void TaskManagerExtensionProcessResourceProvider::RemoveFromTaskManager( |
| 595 ExtensionHost* extension_host) { |
| 596 if (!updating_) |
| 597 return; |
| 598 std::map<ExtensionHost*, TaskManagerExtensionProcessResource*> |
| 599 ::iterator iter = resources_.find(extension_host); |
| 600 if (iter == resources_.end()) |
| 601 return; |
| 602 |
| 603 // Remove the resource from the Task Manager. |
| 604 TaskManagerExtensionProcessResource* resource = iter->second; |
| 605 task_manager_->RemoveResource(resource); |
| 606 |
| 607 // Remove it from the provider. |
| 608 resources_.erase(iter); |
| 609 |
| 610 // Remove it from our pid map. |
| 611 std::map<int, TaskManagerExtensionProcessResource*>::iterator pid_iter = |
| 612 pid_to_resources_.find(resource->process_id()); |
| 613 DCHECK(pid_iter != pid_to_resources_.end()); |
| 614 if (pid_iter != pid_to_resources_.end()) |
| 615 pid_to_resources_.erase(pid_iter); |
| 616 |
| 617 // Finally, delete the resource. |
| 618 delete resource; |
| 619 } |
| 620 |
| 572 //////////////////////////////////////////////////////////////////////////////// | 621 //////////////////////////////////////////////////////////////////////////////// |
| 573 // TaskManagerBrowserProcessResource class | 622 // TaskManagerBrowserProcessResource class |
| 574 //////////////////////////////////////////////////////////////////////////////// | 623 //////////////////////////////////////////////////////////////////////////////// |
| 575 | 624 |
| 576 SkBitmap* TaskManagerBrowserProcessResource::default_icon_ = NULL; | 625 SkBitmap* TaskManagerBrowserProcessResource::default_icon_ = NULL; |
| 577 | 626 |
| 578 TaskManagerBrowserProcessResource::TaskManagerBrowserProcessResource() | 627 TaskManagerBrowserProcessResource::TaskManagerBrowserProcessResource() |
| 579 : network_usage_support_(false), | 628 : network_usage_support_(false), |
| 580 title_() { | 629 title_() { |
| 581 pid_ = base::GetCurrentProcId(); | 630 pid_ = base::GetCurrentProcId(); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 | 694 |
| 646 return &resource_; | 695 return &resource_; |
| 647 } | 696 } |
| 648 | 697 |
| 649 void TaskManagerBrowserProcessResourceProvider::StartUpdating() { | 698 void TaskManagerBrowserProcessResourceProvider::StartUpdating() { |
| 650 task_manager_->AddResource(&resource_); | 699 task_manager_->AddResource(&resource_); |
| 651 } | 700 } |
| 652 | 701 |
| 653 void TaskManagerBrowserProcessResourceProvider::StopUpdating() { | 702 void TaskManagerBrowserProcessResourceProvider::StopUpdating() { |
| 654 } | 703 } |
| OLD | NEW |