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