| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.h" | 5 #include "chrome/browser/task_manager/task_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/i18n/number_formatting.h" | 9 #include "base/i18n/number_formatting.h" |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 } | 77 } |
| 78 | 78 |
| 79 } // namespace | 79 } // namespace |
| 80 | 80 |
| 81 //////////////////////////////////////////////////////////////////////////////// | 81 //////////////////////////////////////////////////////////////////////////////// |
| 82 // TaskManagerModel class | 82 // TaskManagerModel class |
| 83 //////////////////////////////////////////////////////////////////////////////// | 83 //////////////////////////////////////////////////////////////////////////////// |
| 84 | 84 |
| 85 TaskManagerModel::TaskManagerModel(TaskManager* task_manager) | 85 TaskManagerModel::TaskManagerModel(TaskManager* task_manager) |
| 86 : update_requests_(0), | 86 : update_requests_(0), |
| 87 listen_requests_(0), |
| 87 update_state_(IDLE), | 88 update_state_(IDLE), |
| 88 goat_salt_(base::RandUint64()), | 89 goat_salt_(base::RandUint64()), |
| 89 last_unique_id_(0) { | 90 last_unique_id_(0) { |
| 90 AddResourceProvider( | 91 AddResourceProvider( |
| 91 new TaskManagerBrowserProcessResourceProvider(task_manager)); | 92 new TaskManagerBrowserProcessResourceProvider(task_manager)); |
| 92 AddResourceProvider( | 93 AddResourceProvider( |
| 93 new TaskManagerBackgroundContentsResourceProvider(task_manager)); | 94 new TaskManagerBackgroundContentsResourceProvider(task_manager)); |
| 94 AddResourceProvider(new TaskManagerTabContentsResourceProvider(task_manager)); | 95 AddResourceProvider(new TaskManagerTabContentsResourceProvider(task_manager)); |
| 95 AddResourceProvider( | 96 AddResourceProvider( |
| 96 new TaskManagerChildProcessResourceProvider(task_manager)); | 97 new TaskManagerChildProcessResourceProvider(task_manager)); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 size_t phys_mem; | 203 size_t phys_mem; |
| 203 GetPhysicalMemory(index, &phys_mem); | 204 GetPhysicalMemory(index, &phys_mem); |
| 204 return GetMemCellText(phys_mem); | 205 return GetMemCellText(phys_mem); |
| 205 } | 206 } |
| 206 | 207 |
| 207 int TaskManagerModel::GetProcessId(int index) const { | 208 int TaskManagerModel::GetProcessId(int index) const { |
| 208 CHECK_LT(index, ResourceCount()); | 209 CHECK_LT(index, ResourceCount()); |
| 209 return base::GetProcId(resources_[index]->GetProcess()); | 210 return base::GetProcId(resources_[index]->GetProcess()); |
| 210 } | 211 } |
| 211 | 212 |
| 213 base::ProcessHandle TaskManagerModel::GetProcess(int index) const { |
| 214 CHECK_LT(index, ResourceCount()); |
| 215 return resources_[index]->GetProcess(); |
| 216 } |
| 217 |
| 212 string16 TaskManagerModel::GetResourceProcessId(int index) const { | 218 string16 TaskManagerModel::GetResourceProcessId(int index) const { |
| 213 return base::IntToString16(GetProcessId(index)); | 219 return base::IntToString16(GetProcessId(index)); |
| 214 } | 220 } |
| 215 | 221 |
| 216 string16 TaskManagerModel::GetResourceGoatsTeleported(int index) const { | 222 string16 TaskManagerModel::GetResourceGoatsTeleported(int index) const { |
| 217 CHECK_LT(index, ResourceCount()); | 223 CHECK_LT(index, ResourceCount()); |
| 218 return base::FormatNumber(GetGoatsTeleported(index)); | 224 return base::FormatNumber(GetGoatsTeleported(index)); |
| 219 } | 225 } |
| 220 | 226 |
| 221 string16 TaskManagerModel::GetResourceWebCoreImageCacheSize( | 227 string16 TaskManagerModel::GetResourceWebCoreImageCacheSize( |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 | 586 |
| 581 bool TaskManagerModel::GetV8Memory(int index, size_t* result) const { | 587 bool TaskManagerModel::GetV8Memory(int index, size_t* result) const { |
| 582 *result = 0; | 588 *result = 0; |
| 583 if (!resources_[index]->ReportsV8MemoryStats()) | 589 if (!resources_[index]->ReportsV8MemoryStats()) |
| 584 return false; | 590 return false; |
| 585 | 591 |
| 586 *result = resources_[index]->GetV8MemoryAllocated(); | 592 *result = resources_[index]->GetV8MemoryAllocated(); |
| 587 return true; | 593 return true; |
| 588 } | 594 } |
| 589 | 595 |
| 596 bool TaskManagerModel::GetV8MemoryUsed(int index, size_t* result) const { |
| 597 *result = 0; |
| 598 if (!resources_[index]->ReportsV8MemoryStats()) |
| 599 return false; |
| 600 |
| 601 *result = resources_[index]->GetV8MemoryUsed(); |
| 602 return true; |
| 603 } |
| 604 |
| 590 bool TaskManagerModel::CanActivate(int index) const { | 605 bool TaskManagerModel::CanActivate(int index) const { |
| 591 CHECK_LT(index, ResourceCount()); | 606 CHECK_LT(index, ResourceCount()); |
| 592 return GetResourceTabContents(index) != NULL; | 607 return GetResourceTabContents(index) != NULL; |
| 593 } | 608 } |
| 594 | 609 |
| 595 bool TaskManagerModel::CanInspect(int index) const { | 610 bool TaskManagerModel::CanInspect(int index) const { |
| 596 CHECK_LT(index, ResourceCount()); | 611 CHECK_LT(index, ResourceCount()); |
| 597 return resources_[index]->CanInspect(); | 612 return resources_[index]->CanInspect(); |
| 598 } | 613 } |
| 599 | 614 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 614 // Adjust number string if necessary. | 629 // Adjust number string if necessary. |
| 615 base::i18n::AdjustStringForLocaleDirection(&str); | 630 base::i18n::AdjustStringForLocaleDirection(&str); |
| 616 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_MEM_CELL_TEXT, str); | 631 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_MEM_CELL_TEXT, str); |
| 617 #else | 632 #else |
| 618 // System expectation is to show "100 kB", "200 MB", etc. | 633 // System expectation is to show "100 kB", "200 MB", etc. |
| 619 // TODO(thakis): Switch to metric units (as opposed to powers of two). | 634 // TODO(thakis): Switch to metric units (as opposed to powers of two). |
| 620 return ui::FormatBytes(number); | 635 return ui::FormatBytes(number); |
| 621 #endif | 636 #endif |
| 622 } | 637 } |
| 623 | 638 |
| 639 void TaskManagerModel::StartListening() { |
| 640 // Multiple StartListening requests may come in and we only need to take |
| 641 // action the first time. |
| 642 listen_requests_++; |
| 643 if (listen_requests_ > 1) |
| 644 return; |
| 645 DCHECK_EQ(1, listen_requests_); |
| 646 |
| 647 // Notify resource providers that we should start listening to events. |
| 648 for (ResourceProviderList::iterator iter = providers_.begin(); |
| 649 iter != providers_.end(); ++iter) { |
| 650 (*iter)->StartUpdating(); |
| 651 } |
| 652 } |
| 653 |
| 654 void TaskManagerModel::StopListening() { |
| 655 // Don't actually stop listening until we have heard as many calls as those |
| 656 // to StartListening. |
| 657 listen_requests_--; |
| 658 if (listen_requests_ > 0) |
| 659 return; |
| 660 |
| 661 DCHECK_EQ(0, listen_requests_); |
| 662 |
| 663 // Notify resource providers that we are done listening. |
| 664 for (ResourceProviderList::const_iterator iter = providers_.begin(); |
| 665 iter != providers_.end(); ++iter) { |
| 666 (*iter)->StopUpdating(); |
| 667 } |
| 668 |
| 669 // Must clear the resources before the next attempt to start listening. |
| 670 Clear(); |
| 671 } |
| 672 |
| 624 void TaskManagerModel::StartUpdating() { | 673 void TaskManagerModel::StartUpdating() { |
| 625 // Multiple StartUpdating requests may come in, and we only need to take | 674 // Multiple StartUpdating requests may come in, and we only need to take |
| 626 // action the first time. | 675 // action the first time. |
| 627 update_requests_++; | 676 update_requests_++; |
| 628 if (update_requests_ > 1) | 677 if (update_requests_ > 1) |
| 629 return; | 678 return; |
| 630 DCHECK_EQ(1, update_requests_); | 679 DCHECK_EQ(1, update_requests_); |
| 631 DCHECK_NE(TASK_PENDING, update_state_); | 680 DCHECK_NE(TASK_PENDING, update_state_); |
| 632 | 681 |
| 633 // If update_state_ is STOPPING, it means a task is still pending. Setting | 682 // If update_state_ is STOPPING, it means a task is still pending. Setting |
| 634 // it to TASK_PENDING ensures the tasks keep being posted (by Refresh()). | 683 // it to TASK_PENDING ensures the tasks keep being posted (by Refresh()). |
| 635 if (update_state_ == IDLE) { | 684 if (update_state_ == IDLE) { |
| 636 MessageLoop::current()->PostTask( | 685 MessageLoop::current()->PostTask( |
| 637 FROM_HERE, | 686 FROM_HERE, |
| 638 base::Bind(&TaskManagerModel::Refresh, this)); | 687 base::Bind(&TaskManagerModel::Refresh, this)); |
| 639 } | 688 } |
| 640 update_state_ = TASK_PENDING; | 689 update_state_ = TASK_PENDING; |
| 641 | 690 |
| 642 // Notify resource providers that we are updating. | 691 // Notify resource providers that we are updating. |
| 643 for (ResourceProviderList::iterator iter = providers_.begin(); | 692 StartListening(); |
| 644 iter != providers_.end(); ++iter) { | |
| 645 (*iter)->StartUpdating(); | |
| 646 } | |
| 647 | 693 |
| 648 if (!resources_.empty()) { | 694 if (!resources_.empty()) { |
| 649 FOR_EACH_OBSERVER(TaskManagerModelObserver, observer_list_, | 695 FOR_EACH_OBSERVER(TaskManagerModelObserver, observer_list_, |
| 650 OnReadyPeriodicalUpdate()); | 696 OnReadyPeriodicalUpdate()); |
| 651 } | 697 } |
| 652 } | 698 } |
| 653 | 699 |
| 654 void TaskManagerModel::StopUpdating() { | 700 void TaskManagerModel::StopUpdating() { |
| 655 // Don't actually stop updating until we have heard as many calls as those | 701 // Don't actually stop updating until we have heard as many calls as those |
| 656 // to StartUpdating. | 702 // to StartUpdating. |
| 657 update_requests_--; | 703 update_requests_--; |
| 658 if (update_requests_ > 0) | 704 if (update_requests_ > 0) |
| 659 return; | 705 return; |
| 660 // Make sure that update_requests_ cannot go negative. | 706 // Make sure that update_requests_ cannot go negative. |
| 661 CHECK_EQ(0, update_requests_); | 707 CHECK_EQ(0, update_requests_); |
| 662 DCHECK_EQ(TASK_PENDING, update_state_); | 708 DCHECK_EQ(TASK_PENDING, update_state_); |
| 663 update_state_ = STOPPING; | 709 update_state_ = STOPPING; |
| 664 | 710 |
| 665 // Notify resource providers that we are done updating. | 711 // Notify resource providers that we are done updating. |
| 666 for (ResourceProviderList::const_iterator iter = providers_.begin(); | 712 StopListening(); |
| 667 iter != providers_.end(); ++iter) { | |
| 668 (*iter)->StopUpdating(); | |
| 669 } | |
| 670 | |
| 671 // Must clear the resources before the next attempt to start updating. | |
| 672 Clear(); | |
| 673 } | 713 } |
| 674 | 714 |
| 675 void TaskManagerModel::AddResourceProvider( | 715 void TaskManagerModel::AddResourceProvider( |
| 676 TaskManager::ResourceProvider* provider) { | 716 TaskManager::ResourceProvider* provider) { |
| 677 DCHECK(provider); | 717 DCHECK(provider); |
| 678 providers_.push_back(provider); | 718 providers_.push_back(provider); |
| 679 } | 719 } |
| 680 | 720 |
| 681 void TaskManagerModel::AddResource(TaskManager::Resource* resource) { | 721 void TaskManagerModel::AddResource(TaskManager::Resource* resource) { |
| 682 resource->unique_id_ = ++last_unique_id_; | 722 resource->unique_id_ = ++last_unique_id_; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 if (pm_iter != metrics_map_.end()) { | 792 if (pm_iter != metrics_map_.end()) { |
| 753 delete pm_iter->second; | 793 delete pm_iter->second; |
| 754 metrics_map_.erase(process); | 794 metrics_map_.erase(process); |
| 755 } | 795 } |
| 756 // And we don't need the CPU usage anymore either. | 796 // And we don't need the CPU usage anymore either. |
| 757 CPUUsageMap::iterator cpu_iter = cpu_usage_map_.find(process); | 797 CPUUsageMap::iterator cpu_iter = cpu_usage_map_.find(process); |
| 758 if (cpu_iter != cpu_usage_map_.end()) | 798 if (cpu_iter != cpu_usage_map_.end()) |
| 759 cpu_usage_map_.erase(cpu_iter); | 799 cpu_usage_map_.erase(cpu_iter); |
| 760 } | 800 } |
| 761 | 801 |
| 762 // Remove the entry from the model list. | 802 // Prepare to remove the entry from the model list. |
| 763 iter = std::find(resources_.begin(), resources_.end(), resource); | 803 iter = std::find(resources_.begin(), resources_.end(), resource); |
| 764 DCHECK(iter != resources_.end()); | 804 DCHECK(iter != resources_.end()); |
| 765 int index = static_cast<int>(iter - resources_.begin()); | 805 int index = static_cast<int>(iter - resources_.begin()); |
| 806 |
| 807 // Notify the observers that the contents will change. |
| 808 FOR_EACH_OBSERVER(TaskManagerModelObserver, observer_list_, |
| 809 OnItemsToBeRemoved(index, 1)); |
| 810 |
| 811 // Now actually remove the entry from the model list. |
| 766 resources_.erase(iter); | 812 resources_.erase(iter); |
| 767 | 813 |
| 768 // Remove the entry from the network maps. | 814 // Remove the entry from the network maps. |
| 769 ResourceValueMap::iterator net_iter = | 815 ResourceValueMap::iterator net_iter = |
| 770 current_byte_count_map_.find(resource); | 816 current_byte_count_map_.find(resource); |
| 771 if (net_iter != current_byte_count_map_.end()) | 817 if (net_iter != current_byte_count_map_.end()) |
| 772 current_byte_count_map_.erase(net_iter); | 818 current_byte_count_map_.erase(net_iter); |
| 773 net_iter = displayed_network_usage_map_.find(resource); | 819 net_iter = displayed_network_usage_map_.find(resource); |
| 774 if (net_iter != displayed_network_usage_map_.end()) | 820 if (net_iter != displayed_network_usage_map_.end()) |
| 775 displayed_network_usage_map_.erase(net_iter); | 821 displayed_network_usage_map_.erase(net_iter); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 919 int64 TaskManagerModel::GetNetworkUsageForResource( | 965 int64 TaskManagerModel::GetNetworkUsageForResource( |
| 920 TaskManager::Resource* resource) const { | 966 TaskManager::Resource* resource) const { |
| 921 ResourceValueMap::const_iterator iter = | 967 ResourceValueMap::const_iterator iter = |
| 922 displayed_network_usage_map_.find(resource); | 968 displayed_network_usage_map_.find(resource); |
| 923 if (iter == displayed_network_usage_map_.end()) | 969 if (iter == displayed_network_usage_map_.end()) |
| 924 return 0; | 970 return 0; |
| 925 return iter->second; | 971 return iter->second; |
| 926 } | 972 } |
| 927 | 973 |
| 928 void TaskManagerModel::BytesRead(BytesReadParam param) { | 974 void TaskManagerModel::BytesRead(BytesReadParam param) { |
| 929 if (update_state_ != TASK_PENDING) { | 975 if (update_state_ != TASK_PENDING || listen_requests_ == 0) { |
| 930 // A notification sneaked in while we were stopping the updating, just | 976 // A notification sneaked in while we were stopping the updating, just |
| 931 // ignore it. | 977 // ignore it. |
| 932 return; | 978 return; |
| 933 } | 979 } |
| 934 | 980 |
| 935 if (param.byte_count == 0) { | 981 if (param.byte_count == 0) { |
| 936 // Nothing to do if no bytes were actually read. | 982 // Nothing to do if no bytes were actually read. |
| 937 return; | 983 return; |
| 938 } | 984 } |
| 939 | 985 |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1166 // Count the number of extensions with background pages (including | 1212 // Count the number of extensions with background pages (including |
| 1167 // incognito). | 1213 // incognito). |
| 1168 count += CountExtensionBackgroundPagesForProfile(profile); | 1214 count += CountExtensionBackgroundPagesForProfile(profile); |
| 1169 if (profile->HasOffTheRecordProfile()) { | 1215 if (profile->HasOffTheRecordProfile()) { |
| 1170 count += CountExtensionBackgroundPagesForProfile( | 1216 count += CountExtensionBackgroundPagesForProfile( |
| 1171 profile->GetOffTheRecordProfile()); | 1217 profile->GetOffTheRecordProfile()); |
| 1172 } | 1218 } |
| 1173 } | 1219 } |
| 1174 return count; | 1220 return count; |
| 1175 } | 1221 } |
| OLD | NEW |