| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/i18n/number_formatting.h" | 10 #include "base/i18n/number_formatting.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 FormatBytes(stat.liveSize, DATA_UNITS_KIBIBYTE, false)); | 63 FormatBytes(stat.liveSize, DATA_UNITS_KIBIBYTE, false)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 } // namespace | 66 } // namespace |
| 67 | 67 |
| 68 //////////////////////////////////////////////////////////////////////////////// | 68 //////////////////////////////////////////////////////////////////////////////// |
| 69 // TaskManagerModel class | 69 // TaskManagerModel class |
| 70 //////////////////////////////////////////////////////////////////////////////// | 70 //////////////////////////////////////////////////////////////////////////////// |
| 71 | 71 |
| 72 TaskManagerModel::TaskManagerModel(TaskManager* task_manager) | 72 TaskManagerModel::TaskManagerModel(TaskManager* task_manager) |
| 73 : update_state_(IDLE), | 73 : update_requests_(0), |
| 74 update_state_(IDLE), |
| 74 goat_salt_(rand()) { | 75 goat_salt_(rand()) { |
| 75 | 76 |
| 76 TaskManagerBrowserProcessResourceProvider* browser_provider = | 77 TaskManagerBrowserProcessResourceProvider* browser_provider = |
| 77 new TaskManagerBrowserProcessResourceProvider(task_manager); | 78 new TaskManagerBrowserProcessResourceProvider(task_manager); |
| 78 browser_provider->AddRef(); | 79 browser_provider->AddRef(); |
| 79 providers_.push_back(browser_provider); | 80 providers_.push_back(browser_provider); |
| 80 TaskManagerTabContentsResourceProvider* wc_provider = | 81 TaskManagerTabContentsResourceProvider* wc_provider = |
| 81 new TaskManagerTabContentsResourceProvider(task_manager); | 82 new TaskManagerTabContentsResourceProvider(task_manager); |
| 82 wc_provider->AddRef(); | 83 wc_provider->AddRef(); |
| 83 providers_.push_back(wc_provider); | 84 providers_.push_back(wc_provider); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 112 | 113 |
| 113 void TaskManagerModel::RemoveObserver(TaskManagerModelObserver* observer) { | 114 void TaskManagerModel::RemoveObserver(TaskManagerModelObserver* observer) { |
| 114 observer_list_.RemoveObserver(observer); | 115 observer_list_.RemoveObserver(observer); |
| 115 } | 116 } |
| 116 | 117 |
| 117 string16 TaskManagerModel::GetResourceTitle(int index) const { | 118 string16 TaskManagerModel::GetResourceTitle(int index) const { |
| 118 DCHECK(index < ResourceCount()); | 119 DCHECK(index < ResourceCount()); |
| 119 return WideToUTF16Hack(resources_[index]->GetTitle()); | 120 return WideToUTF16Hack(resources_[index]->GetTitle()); |
| 120 } | 121 } |
| 121 | 122 |
| 123 int64 TaskManagerModel::GetNetworkUsage(int index) const { |
| 124 DCHECK(index < ResourceCount()); |
| 125 return GetNetworkUsage(resources_[index]); |
| 126 } |
| 127 |
| 122 string16 TaskManagerModel::GetResourceNetworkUsage(int index) const { | 128 string16 TaskManagerModel::GetResourceNetworkUsage(int index) const { |
| 123 DCHECK(index < ResourceCount()); | 129 int64 net_usage = GetNetworkUsage(index); |
| 124 int64 net_usage = GetNetworkUsage(resources_[index]); | |
| 125 if (net_usage == -1) | 130 if (net_usage == -1) |
| 126 return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT); | 131 return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT); |
| 127 if (net_usage == 0) | 132 if (net_usage == 0) |
| 128 return ASCIIToUTF16("0"); | 133 return ASCIIToUTF16("0"); |
| 129 string16 net_byte = FormatSpeed(net_usage, GetByteDisplayUnits(net_usage), | 134 string16 net_byte = FormatSpeed(net_usage, GetByteDisplayUnits(net_usage), |
| 130 true); | 135 true); |
| 131 // Force number string to have LTR directionality. | 136 // Force number string to have LTR directionality. |
| 132 return base::i18n::GetDisplayStringInLTRDirectionality(net_byte); | 137 return base::i18n::GetDisplayStringInLTRDirectionality(net_byte); |
| 133 } | 138 } |
| 134 | 139 |
| 140 double TaskManagerModel::GetCPUUsage(int index) const { |
| 141 DCHECK(index < ResourceCount()); |
| 142 return GetCPUUsage(resources_[index]); |
| 143 } |
| 144 |
| 135 string16 TaskManagerModel::GetResourceCPUUsage(int index) const { | 145 string16 TaskManagerModel::GetResourceCPUUsage(int index) const { |
| 136 DCHECK(index < ResourceCount()); | 146 DCHECK(index < ResourceCount()); |
| 137 return WideToUTF16Hack(StringPrintf( | 147 return WideToUTF16Hack(StringPrintf( |
| 138 #if defined(OS_MACOSX) | 148 #if defined(OS_MACOSX) |
| 139 // Activity Monitor shows %cpu with one decimal digit -- be | 149 // Activity Monitor shows %cpu with one decimal digit -- be |
| 140 // consistent with that. | 150 // consistent with that. |
| 141 L"%.1f", | 151 L"%.1f", |
| 142 #else | 152 #else |
| 143 L"%.0f", | 153 L"%.0f", |
| 144 #endif | 154 #endif |
| (...skipping 13 matching lines...) Expand all Loading... |
| 158 return ASCIIToUTF16("N/A"); | 168 return ASCIIToUTF16("N/A"); |
| 159 return GetMemCellText(shared_mem); | 169 return GetMemCellText(shared_mem); |
| 160 } | 170 } |
| 161 | 171 |
| 162 string16 TaskManagerModel::GetResourcePhysicalMemory(int index) const { | 172 string16 TaskManagerModel::GetResourcePhysicalMemory(int index) const { |
| 163 size_t phys_mem; | 173 size_t phys_mem; |
| 164 GetPhysicalMemory(index, &phys_mem); | 174 GetPhysicalMemory(index, &phys_mem); |
| 165 return GetMemCellText(phys_mem); | 175 return GetMemCellText(phys_mem); |
| 166 } | 176 } |
| 167 | 177 |
| 178 int TaskManagerModel::GetProcessId(int index) const { |
| 179 DCHECK(index < ResourceCount()); |
| 180 return base::GetProcId(resources_[index]->GetProcess()); |
| 181 } |
| 182 |
| 168 string16 TaskManagerModel::GetResourceProcessId(int index) const { | 183 string16 TaskManagerModel::GetResourceProcessId(int index) const { |
| 169 DCHECK(index < ResourceCount()); | 184 return base::IntToString16(GetProcessId(index)); |
| 170 return base::IntToString16(base::GetProcId(resources_[index]->GetProcess())); | |
| 171 } | 185 } |
| 172 | 186 |
| 173 string16 TaskManagerModel::GetResourceGoatsTeleported(int index) const { | 187 string16 TaskManagerModel::GetResourceGoatsTeleported(int index) const { |
| 174 DCHECK(index < ResourceCount()); | 188 DCHECK(index < ResourceCount()); |
| 175 return base::FormatNumber(GetGoatsTeleported(index)); | 189 return base::FormatNumber(GetGoatsTeleported(index)); |
| 176 } | 190 } |
| 177 | 191 |
| 178 string16 TaskManagerModel::GetResourceWebCoreImageCacheSize( | 192 string16 TaskManagerModel::GetResourceWebCoreImageCacheSize( |
| 179 int index) const { | 193 int index) const { |
| 180 DCHECK(index < ResourceCount()); | 194 DCHECK(index < ResourceCount()); |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 base::i18n::AdjustStringForLocaleDirection(str, &str); | 489 base::i18n::AdjustStringForLocaleDirection(str, &str); |
| 476 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_MEM_CELL_TEXT, str); | 490 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_MEM_CELL_TEXT, str); |
| 477 #else | 491 #else |
| 478 // System expectation is to show "100 KB", "200 MB", etc. | 492 // System expectation is to show "100 KB", "200 MB", etc. |
| 479 // TODO(thakis): Switch to metric units (as opposed to powers of two). | 493 // TODO(thakis): Switch to metric units (as opposed to powers of two). |
| 480 return FormatBytes(number, GetByteDisplayUnits(number), /*show_units=*/true); | 494 return FormatBytes(number, GetByteDisplayUnits(number), /*show_units=*/true); |
| 481 #endif | 495 #endif |
| 482 } | 496 } |
| 483 | 497 |
| 484 void TaskManagerModel::StartUpdating() { | 498 void TaskManagerModel::StartUpdating() { |
| 499 // Multiple StartUpdating requests may come in, and we only need to take |
| 500 // action the first time. |
| 501 update_requests_++; |
| 502 if (update_requests_ > 1) |
| 503 return; |
| 504 DCHECK_EQ(1, update_requests_); |
| 485 DCHECK_NE(TASK_PENDING, update_state_); | 505 DCHECK_NE(TASK_PENDING, update_state_); |
| 486 | 506 |
| 487 // If update_state_ is STOPPING, it means a task is still pending. Setting | 507 // If update_state_ is STOPPING, it means a task is still pending. Setting |
| 488 // it to TASK_PENDING ensures the tasks keep being posted (by Refresh()). | 508 // it to TASK_PENDING ensures the tasks keep being posted (by Refresh()). |
| 489 if (update_state_ == IDLE) { | 509 if (update_state_ == IDLE) { |
| 490 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 510 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 491 NewRunnableMethod(this, &TaskManagerModel::Refresh), | 511 NewRunnableMethod(this, &TaskManagerModel::Refresh), |
| 492 kUpdateTimeMs); | 512 kUpdateTimeMs); |
| 493 } | 513 } |
| 494 update_state_ = TASK_PENDING; | 514 update_state_ = TASK_PENDING; |
| 495 | 515 |
| 496 // Register jobs notifications so we can compute network usage (it must be | 516 // Register jobs notifications so we can compute network usage (it must be |
| 497 // done from the IO thread). | 517 // done from the IO thread). |
| 498 ChromeThread::PostTask( | 518 ChromeThread::PostTask( |
| 499 ChromeThread::IO, FROM_HERE, | 519 ChromeThread::IO, FROM_HERE, |
| 500 NewRunnableMethod( | 520 NewRunnableMethod( |
| 501 this, &TaskManagerModel::RegisterForJobDoneNotifications)); | 521 this, &TaskManagerModel::RegisterForJobDoneNotifications)); |
| 502 | 522 |
| 503 // Notify resource providers that we are updating. | 523 // Notify resource providers that we are updating. |
| 504 for (ResourceProviderList::iterator iter = providers_.begin(); | 524 for (ResourceProviderList::iterator iter = providers_.begin(); |
| 505 iter != providers_.end(); ++iter) { | 525 iter != providers_.end(); ++iter) { |
| 506 (*iter)->StartUpdating(); | 526 (*iter)->StartUpdating(); |
| 507 } | 527 } |
| 508 } | 528 } |
| 509 | 529 |
| 510 void TaskManagerModel::StopUpdating() { | 530 void TaskManagerModel::StopUpdating() { |
| 531 // Don't actually stop updating until we have heard as many calls as those |
| 532 // to StartUpdating. |
| 533 update_requests_--; |
| 534 if (update_requests_ > 0) |
| 535 return; |
| 536 DCHECK_EQ(0, update_requests_); |
| 511 DCHECK_EQ(TASK_PENDING, update_state_); | 537 DCHECK_EQ(TASK_PENDING, update_state_); |
| 512 update_state_ = STOPPING; | 538 update_state_ = STOPPING; |
| 513 | 539 |
| 514 // Notify resource providers that we are done updating. | 540 // Notify resource providers that we are done updating. |
| 515 for (ResourceProviderList::const_iterator iter = providers_.begin(); | 541 for (ResourceProviderList::const_iterator iter = providers_.begin(); |
| 516 iter != providers_.end(); ++iter) { | 542 iter != providers_.end(); ++iter) { |
| 517 (*iter)->StopUpdating(); | 543 (*iter)->StopUpdating(); |
| 518 } | 544 } |
| 519 | 545 |
| 520 // Unregister jobs notification (must be done from the IO thread). | 546 // Unregister jobs notification (must be done from the IO thread). |
| 521 ChromeThread::PostTask( | 547 ChromeThread::PostTask( |
| 522 ChromeThread::IO, FROM_HERE, | 548 ChromeThread::IO, FROM_HERE, |
| 523 NewRunnableMethod( | 549 NewRunnableMethod( |
| 524 this, &TaskManagerModel::UnregisterForJobDoneNotifications)); | 550 this, &TaskManagerModel::UnregisterForJobDoneNotifications)); |
| 551 |
| 552 // Must clear the resources before the next attempt to start updating. |
| 553 Clear(); |
| 525 } | 554 } |
| 526 | 555 |
| 527 void TaskManagerModel::AddResourceProvider( | 556 void TaskManagerModel::AddResourceProvider( |
| 528 TaskManager::ResourceProvider* provider) { | 557 TaskManager::ResourceProvider* provider) { |
| 529 DCHECK(provider); | 558 DCHECK(provider); |
| 530 providers_.push_back(provider); | 559 providers_.push_back(provider); |
| 531 } | 560 } |
| 532 | 561 |
| 533 void TaskManagerModel::RemoveResourceProvider( | 562 void TaskManagerModel::RemoveResourceProvider( |
| 534 TaskManager::ResourceProvider* provider) { | 563 TaskManager::ResourceProvider* provider) { |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 918 void TaskManager::AddResource(Resource* resource) { | 947 void TaskManager::AddResource(Resource* resource) { |
| 919 model_->AddResource(resource); | 948 model_->AddResource(resource); |
| 920 } | 949 } |
| 921 | 950 |
| 922 void TaskManager::RemoveResource(Resource* resource) { | 951 void TaskManager::RemoveResource(Resource* resource) { |
| 923 model_->RemoveResource(resource); | 952 model_->RemoveResource(resource); |
| 924 } | 953 } |
| 925 | 954 |
| 926 void TaskManager::OnWindowClosed() { | 955 void TaskManager::OnWindowClosed() { |
| 927 model_->StopUpdating(); | 956 model_->StopUpdating(); |
| 928 model_->Clear(); | |
| 929 } | 957 } |
| 930 | 958 |
| 931 // static | 959 // static |
| 932 TaskManager* TaskManager::GetInstance() { | 960 TaskManager* TaskManager::GetInstance() { |
| 933 return Singleton<TaskManager>::get(); | 961 return Singleton<TaskManager>::get(); |
| 934 } | 962 } |
| 935 | 963 |
| 936 void TaskManager::OpenAboutMemory() { | 964 void TaskManager::OpenAboutMemory() { |
| 937 Browser* browser = BrowserList::GetLastActive(); | 965 Browser* browser = BrowserList::GetLastActive(); |
| 938 | 966 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 971 MetricsMap::const_iterator iter = metrics_map_.find(handle); | 999 MetricsMap::const_iterator iter = metrics_map_.find(handle); |
| 972 if (iter == metrics_map_.end()) | 1000 if (iter == metrics_map_.end()) |
| 973 return false; | 1001 return false; |
| 974 | 1002 |
| 975 if (!iter->second->GetMemoryBytes(&usage->first, &usage->second)) | 1003 if (!iter->second->GetMemoryBytes(&usage->first, &usage->second)) |
| 976 return false; | 1004 return false; |
| 977 | 1005 |
| 978 memory_usage_map_.insert(std::make_pair(handle, *usage)); | 1006 memory_usage_map_.insert(std::make_pair(handle, *usage)); |
| 979 return true; | 1007 return true; |
| 980 } | 1008 } |
| OLD | NEW |