[email protected] | 4abb460 | 2012-03-16 01:59:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 5 | #include "chrome/browser/extensions/api/processes/processes_api.h" |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 6 | |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 7 | #include "base/callback.h" |
| 8 | #include "base/json/json_writer.h" |
[email protected] | 01d0ffd | 2012-12-26 23:22:51 | [diff] [blame] | 9 | #include "base/lazy_instance.h" |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 10 | #include "base/message_loop.h" |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 11 | #include "base/metrics/histogram.h" |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 12 | #include "base/string_number_conversions.h" |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 13 | #include "base/utf_string_conversions.h" |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 14 | #include "base/values.h" |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 15 | #include "chrome/browser/extensions/api/processes/processes_api_constants.h" |
[email protected] | b19451b | 2012-06-08 17:36:19 | [diff] [blame] | 16 | #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 17 | #include "chrome/browser/extensions/event_router.h" |
[email protected] | 8e0438a7 | 2012-12-05 02:17:42 | [diff] [blame] | 18 | #include "chrome/browser/extensions/extension_function_registry.h" |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_function_util.h" |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 20 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension_system.h" |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_tab_util.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 23 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 24 | #include "chrome/browser/task_manager/task_manager.h" |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 25 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 26 | #include "content/public/browser/notification_details.h" |
| 27 | #include "content/public/browser/notification_service.h" |
| 28 | #include "content/public/browser/notification_source.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 29 | #include "content/public/browser/notification_types.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 30 | #include "content/public/browser/render_process_host.h" |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 31 | #include "content/public/browser/render_view_host.h" |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 32 | #include "content/public/browser/render_widget_host.h" |
[email protected] | ef9572e | 2012-01-04 22:14:12 | [diff] [blame] | 33 | #include "content/public/browser/web_contents.h" |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 34 | #include "content/public/common/result_codes.h" |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 35 | #include "extensions/common/error_utils.h" |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 36 | |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 37 | namespace extensions { |
| 38 | |
| 39 | namespace keys = processes_api_constants; |
| 40 | namespace errors = processes_api_constants; |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 41 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | #if defined(ENABLE_TASK_MANAGER) |
| 45 | |
| 46 | DictionaryValue* CreateCacheData( |
| 47 | const WebKit::WebCache::ResourceTypeStat& stat) { |
| 48 | |
| 49 | DictionaryValue* cache = new DictionaryValue(); |
| 50 | cache->SetDouble(keys::kCacheSize, static_cast<double>(stat.size)); |
| 51 | cache->SetDouble(keys::kCacheLiveSize, static_cast<double>(stat.liveSize)); |
| 52 | return cache; |
| 53 | } |
| 54 | |
| 55 | void SetProcessType(DictionaryValue* result, |
| 56 | TaskManagerModel* model, |
| 57 | int index) { |
| 58 | // Determine process type. |
| 59 | std::string type = keys::kProcessTypeOther; |
| 60 | TaskManager::Resource::Type resource_type = model->GetResourceType(index); |
| 61 | switch (resource_type) { |
| 62 | case TaskManager::Resource::BROWSER: |
| 63 | type = keys::kProcessTypeBrowser; |
| 64 | break; |
| 65 | case TaskManager::Resource::RENDERER: |
| 66 | type = keys::kProcessTypeRenderer; |
| 67 | break; |
| 68 | case TaskManager::Resource::EXTENSION: |
| 69 | type = keys::kProcessTypeExtension; |
| 70 | break; |
| 71 | case TaskManager::Resource::NOTIFICATION: |
| 72 | type = keys::kProcessTypeNotification; |
| 73 | break; |
| 74 | case TaskManager::Resource::PLUGIN: |
| 75 | type = keys::kProcessTypePlugin; |
| 76 | break; |
| 77 | case TaskManager::Resource::WORKER: |
| 78 | type = keys::kProcessTypeWorker; |
| 79 | break; |
| 80 | case TaskManager::Resource::NACL: |
| 81 | type = keys::kProcessTypeNacl; |
| 82 | break; |
| 83 | case TaskManager::Resource::UTILITY: |
| 84 | type = keys::kProcessTypeUtility; |
| 85 | break; |
| 86 | case TaskManager::Resource::GPU: |
| 87 | type = keys::kProcessTypeGPU; |
| 88 | break; |
| 89 | case TaskManager::Resource::PROFILE_IMPORT: |
| 90 | case TaskManager::Resource::ZYGOTE: |
| 91 | case TaskManager::Resource::SANDBOX_HELPER: |
| 92 | case TaskManager::Resource::UNKNOWN: |
| 93 | type = keys::kProcessTypeOther; |
| 94 | break; |
| 95 | default: |
| 96 | NOTREACHED() << "Unknown resource type."; |
| 97 | } |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 98 | result->SetString(keys::kTypeKey, type); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | ListValue* GetTabsForProcess(int process_id) { |
| 102 | ListValue* tabs_list = new ListValue(); |
| 103 | |
| 104 | // The tabs list only makes sense for render processes, so if we don't find |
| 105 | // one, just return the empty list. |
| 106 | content::RenderProcessHost* rph = |
| 107 | content::RenderProcessHost::FromID(process_id); |
| 108 | if (rph == NULL) |
| 109 | return tabs_list; |
| 110 | |
| 111 | int tab_id = -1; |
| 112 | // We need to loop through all the RVHs to ensure we collect the set of all |
| 113 | // tabs using this renderer process. |
| 114 | content::RenderProcessHost::RenderWidgetHostsIterator iter( |
| 115 | rph->GetRenderWidgetHostsIterator()); |
| 116 | for (; !iter.IsAtEnd(); iter.Advance()) { |
| 117 | const content::RenderWidgetHost* widget = iter.GetCurrentValue(); |
| 118 | DCHECK(widget); |
| 119 | if (!widget || !widget->IsRenderView()) |
| 120 | continue; |
| 121 | |
| 122 | content::RenderViewHost* host = content::RenderViewHost::From( |
| 123 | const_cast<content::RenderWidgetHost*>(widget)); |
[email protected] | 746d305 | 2012-05-22 15:15:47 | [diff] [blame] | 124 | content::WebContents* contents = |
| 125 | content::WebContents::FromRenderViewHost(host); |
| 126 | if (contents) { |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 127 | tab_id = ExtensionTabUtil::GetTabId(contents); |
| 128 | if (tab_id != -1) |
| 129 | tabs_list->Append(Value::CreateIntegerValue(tab_id)); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return tabs_list; |
| 134 | } |
| 135 | |
| 136 | // This function creates a Process object to be returned to the extensions |
| 137 | // using these APIs. For memory details, which are not added by this function, |
| 138 | // the callers need to use AddMemoryDetails. |
| 139 | DictionaryValue* CreateProcessFromModel(int process_id, |
| 140 | TaskManagerModel* model, |
| 141 | int index, |
| 142 | bool include_optional) { |
| 143 | DictionaryValue* result = new DictionaryValue(); |
| 144 | size_t mem; |
| 145 | |
| 146 | result->SetInteger(keys::kIdKey, process_id); |
| 147 | result->SetInteger(keys::kOsProcessIdKey, model->GetProcessId(index)); |
| 148 | SetProcessType(result, model, index); |
| 149 | result->SetString(keys::kProfileKey, |
| 150 | model->GetResourceProfileName(index)); |
| 151 | |
| 152 | result->Set(keys::kTabsListKey, GetTabsForProcess(process_id)); |
| 153 | |
| 154 | // If we don't need to include the optional properties, just return now. |
| 155 | if (!include_optional) |
| 156 | return result; |
| 157 | |
| 158 | result->SetDouble(keys::kCpuKey, model->GetCPUUsage(index)); |
| 159 | |
| 160 | if (model->GetV8Memory(index, &mem)) |
| 161 | result->SetDouble(keys::kJsMemoryAllocatedKey, |
| 162 | static_cast<double>(mem)); |
| 163 | |
| 164 | if (model->GetV8MemoryUsed(index, &mem)) |
| 165 | result->SetDouble(keys::kJsMemoryUsedKey, |
| 166 | static_cast<double>(mem)); |
| 167 | |
| 168 | if (model->GetSqliteMemoryUsedBytes(index, &mem)) |
| 169 | result->SetDouble(keys::kSqliteMemoryKey, |
| 170 | static_cast<double>(mem)); |
| 171 | |
| 172 | WebKit::WebCache::ResourceTypeStats cache_stats; |
| 173 | if (model->GetWebCoreCacheStats(index, &cache_stats)) { |
| 174 | result->Set(keys::kImageCacheKey, |
| 175 | CreateCacheData(cache_stats.images)); |
| 176 | result->Set(keys::kScriptCacheKey, |
| 177 | CreateCacheData(cache_stats.scripts)); |
| 178 | result->Set(keys::kCssCacheKey, |
| 179 | CreateCacheData(cache_stats.cssStyleSheets)); |
| 180 | } |
| 181 | |
| 182 | // Network and FPS are reported by the TaskManager per resource (tab), not |
| 183 | // per process, therefore we need to iterate through the group of resources |
| 184 | // and aggregate the data. |
| 185 | float fps = 0, tmp = 0; |
| 186 | int64 net = 0; |
| 187 | int length = model->GetGroupRangeForResource(index).second; |
| 188 | for (int i = 0; i < length; ++i) { |
| 189 | net += model->GetNetworkUsage(index + i); |
| 190 | if (model->GetFPS(index + i, &tmp)) |
| 191 | fps += tmp; |
| 192 | } |
| 193 | result->SetDouble(keys::kFPSKey, static_cast<double>(fps)); |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 194 | result->SetDouble(keys::kNetworkKey, static_cast<double>(net)); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 195 | |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 196 | return result; |
| 197 | } |
| 198 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 199 | // Since memory details are expensive to gather, we don't do it by default. |
| 200 | // This function is a helper to add memory details data to an existing |
| 201 | // Process object representation. |
| 202 | void AddMemoryDetails(DictionaryValue* result, |
| 203 | TaskManagerModel* model, |
| 204 | int index) { |
| 205 | size_t mem; |
| 206 | int64 pr_mem = model->GetPrivateMemory(index, &mem) ? |
| 207 | static_cast<int64>(mem) : -1; |
| 208 | result->SetDouble(keys::kPrivateMemoryKey, static_cast<double>(pr_mem)); |
| 209 | } |
| 210 | |
| 211 | #endif // defined(ENABLE_TASK_MANAGER) |
| 212 | |
[email protected] | 79a6064 | 2012-10-20 21:03:18 | [diff] [blame] | 213 | } // namespace |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 214 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 215 | ProcessesEventRouter::ProcessesEventRouter(Profile* profile) |
| 216 | : profile_(profile), |
| 217 | listeners_(0), |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 218 | task_manager_listening_(false) { |
[email protected] | 043771a | 2012-04-23 20:20:38 | [diff] [blame] | 219 | #if defined(ENABLE_TASK_MANAGER) |
[email protected] | 7da9a823 | 2011-05-27 21:07:11 | [diff] [blame] | 220 | model_ = TaskManager::GetInstance()->model(); |
| 221 | model_->AddObserver(this); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 222 | |
| 223 | registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_HANG, |
| 224 | content::NotificationService::AllSources()); |
| 225 | registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 226 | content::NotificationService::AllSources()); |
[email protected] | 043771a | 2012-04-23 20:20:38 | [diff] [blame] | 227 | #endif // defined(ENABLE_TASK_MANAGER) |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 230 | ProcessesEventRouter::~ProcessesEventRouter() { |
[email protected] | 043771a | 2012-04-23 20:20:38 | [diff] [blame] | 231 | #if defined(ENABLE_TASK_MANAGER) |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 232 | registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_HANG, |
| 233 | content::NotificationService::AllSources()); |
| 234 | registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 235 | content::NotificationService::AllSources()); |
| 236 | |
| 237 | if (task_manager_listening_) |
| 238 | model_->StopListening(); |
| 239 | |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 240 | model_->RemoveObserver(this); |
[email protected] | 043771a | 2012-04-23 20:20:38 | [diff] [blame] | 241 | #endif // defined(ENABLE_TASK_MANAGER) |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 244 | void ProcessesEventRouter::ListenerAdded() { |
[email protected] | 043771a | 2012-04-23 20:20:38 | [diff] [blame] | 245 | #if defined(ENABLE_TASK_MANAGER) |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 246 | // The task manager has its own ref count to balance other callers of |
| 247 | // StartUpdating/StopUpdating. |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 248 | model_->StartUpdating(); |
[email protected] | 043771a | 2012-04-23 20:20:38 | [diff] [blame] | 249 | #endif // defined(ENABLE_TASK_MANAGER) |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 250 | ++listeners_; |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 251 | } |
| 252 | |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 253 | void ProcessesEventRouter::ListenerRemoved() { |
[email protected] | 79a6064 | 2012-10-20 21:03:18 | [diff] [blame] | 254 | DCHECK_GT(listeners_, 0); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 255 | --listeners_; |
[email protected] | 043771a | 2012-04-23 20:20:38 | [diff] [blame] | 256 | #if defined(ENABLE_TASK_MANAGER) |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 257 | // The task manager has its own ref count to balance other callers of |
| 258 | // StartUpdating/StopUpdating. |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 259 | model_->StopUpdating(); |
[email protected] | 043771a | 2012-04-23 20:20:38 | [diff] [blame] | 260 | #endif // defined(ENABLE_TASK_MANAGER) |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 263 | void ProcessesEventRouter::StartTaskManagerListening() { |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 264 | #if defined(ENABLE_TASK_MANAGER) |
| 265 | if (!task_manager_listening_) { |
| 266 | model_->StartListening(); |
| 267 | task_manager_listening_ = true; |
| 268 | } |
| 269 | #endif // defined(ENABLE_TASK_MANAGER) |
| 270 | } |
| 271 | |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 272 | void ProcessesEventRouter::Observe( |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 273 | int type, |
| 274 | const content::NotificationSource& source, |
| 275 | const content::NotificationDetails& details) { |
| 276 | |
| 277 | switch (type) { |
| 278 | case content::NOTIFICATION_RENDERER_PROCESS_HANG: |
| 279 | ProcessHangEvent( |
| 280 | content::Source<content::RenderWidgetHost>(source).ptr()); |
| 281 | break; |
| 282 | case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: |
| 283 | ProcessClosedEvent( |
| 284 | content::Source<content::RenderProcessHost>(source).ptr(), |
| 285 | content::Details<content::RenderProcessHost::RendererClosedDetails>( |
| 286 | details).ptr()); |
| 287 | break; |
| 288 | default: |
| 289 | NOTREACHED() << "Unexpected observe of type " << type; |
| 290 | } |
| 291 | return; |
| 292 | } |
| 293 | |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 294 | void ProcessesEventRouter::OnItemsAdded(int start, int length) { |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 295 | #if defined(ENABLE_TASK_MANAGER) |
| 296 | DCHECK_EQ(length, 1); |
| 297 | int index = start; |
| 298 | |
| 299 | std::string event(keys::kOnCreated); |
| 300 | if (!HasEventListeners(event)) |
| 301 | return; |
| 302 | |
| 303 | // If the item being added is not the first one in the group, find the base |
| 304 | // index and use it for retrieving the process data. |
| 305 | if (!model_->IsResourceFirstInGroup(start)) { |
| 306 | index = model_->GetGroupIndexForResource(start); |
| 307 | } |
| 308 | |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 309 | scoped_ptr<ListValue> args(new ListValue()); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 310 | DictionaryValue* process = CreateProcessFromModel( |
| 311 | model_->GetUniqueChildProcessId(index), model_, index, false); |
| 312 | DCHECK(process != NULL); |
| 313 | |
| 314 | if (process == NULL) |
| 315 | return; |
| 316 | |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 317 | args->Append(process); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 318 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 319 | DispatchEvent(keys::kOnCreated, args.Pass()); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 320 | #endif // defined(ENABLE_TASK_MANAGER) |
| 321 | } |
| 322 | |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 323 | void ProcessesEventRouter::OnItemsChanged(int start, int length) { |
[email protected] | 043771a | 2012-04-23 20:20:38 | [diff] [blame] | 324 | #if defined(ENABLE_TASK_MANAGER) |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 325 | // If we don't have any listeners, return immediately. |
| 326 | if (listeners_ == 0) |
| 327 | return; |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 328 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 329 | if (!model_) |
| 330 | return; |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 331 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 332 | // We need to know which type of onUpdated events to fire and whether to |
| 333 | // collect memory or not. |
| 334 | std::string updated_event(keys::kOnUpdated); |
| 335 | std::string updated_event_memory(keys::kOnUpdatedWithMemory); |
| 336 | bool updated = HasEventListeners(updated_event); |
| 337 | bool updated_memory = HasEventListeners(updated_event_memory); |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 338 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 339 | DCHECK(updated || updated_memory); |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 340 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 341 | IDMap<DictionaryValue> processes_map; |
| 342 | for (int i = start; i < start + length; i++) { |
| 343 | if (model_->IsResourceFirstInGroup(i)) { |
| 344 | int id = model_->GetUniqueChildProcessId(i); |
| 345 | DictionaryValue* process = CreateProcessFromModel(id, model_, i, true); |
| 346 | processes_map.AddWithID(process, i); |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 347 | } |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 348 | } |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 349 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 350 | int id; |
| 351 | std::string idkey(keys::kIdKey); |
| 352 | DictionaryValue* processes = new DictionaryValue(); |
| 353 | |
| 354 | if (updated) { |
| 355 | IDMap<DictionaryValue>::iterator it(&processes_map); |
| 356 | for (; !it.IsAtEnd(); it.Advance()) { |
| 357 | if (!it.GetCurrentValue()->GetInteger(idkey, &id)) |
| 358 | continue; |
| 359 | |
| 360 | // Store each process indexed by the string version of its id. |
| 361 | processes->Set(base::IntToString(id), it.GetCurrentValue()); |
| 362 | } |
| 363 | |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 364 | scoped_ptr<ListValue> args(new ListValue()); |
| 365 | args->Append(processes); |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 366 | DispatchEvent(keys::kOnUpdated, args.Pass()); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 367 | } |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 368 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 369 | if (updated_memory) { |
| 370 | IDMap<DictionaryValue>::iterator it(&processes_map); |
| 371 | for (; !it.IsAtEnd(); it.Advance()) { |
| 372 | if (!it.GetCurrentValue()->GetInteger(idkey, &id)) |
| 373 | continue; |
| 374 | |
| 375 | AddMemoryDetails(it.GetCurrentValue(), model_, it.GetCurrentKey()); |
| 376 | |
| 377 | // Store each process indexed by the string version of its id if we didn't |
| 378 | // already insert it as part of the onUpdated processing above. |
| 379 | if (!updated) |
| 380 | processes->Set(base::IntToString(id), it.GetCurrentValue()); |
[email protected] | 7da9a823 | 2011-05-27 21:07:11 | [diff] [blame] | 381 | } |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 382 | |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 383 | scoped_ptr<ListValue> args(new ListValue()); |
| 384 | args->Append(processes); |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 385 | DispatchEvent(keys::kOnUpdatedWithMemory, args.Pass()); |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 386 | } |
[email protected] | 043771a | 2012-04-23 20:20:38 | [diff] [blame] | 387 | #endif // defined(ENABLE_TASK_MANAGER) |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 388 | } |
| 389 | |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 390 | void ProcessesEventRouter::OnItemsToBeRemoved(int start, int length) { |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 391 | #if defined(ENABLE_TASK_MANAGER) |
[email protected] | 79a6064 | 2012-10-20 21:03:18 | [diff] [blame] | 392 | DCHECK_EQ(length, 1); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 393 | |
| 394 | // Process exit for renderer processes has the data about exit code and |
| 395 | // termination status, therefore we will rely on notifications and not on |
| 396 | // the Task Manager data. We do use the rest of this method for non-renderer |
| 397 | // processes. |
| 398 | if (model_->GetResourceType(start) == TaskManager::Resource::RENDERER) |
| 399 | return; |
| 400 | |
| 401 | // The callback function parameters. |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 402 | scoped_ptr<ListValue> args(new ListValue()); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 403 | |
| 404 | // First arg: The id of the process that was closed. |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 405 | args->Append(Value::CreateIntegerValue( |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 406 | model_->GetUniqueChildProcessId(start))); |
| 407 | |
| 408 | // Second arg: The exit type for the process. |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 409 | args->Append(Value::CreateIntegerValue(0)); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 410 | |
| 411 | // Third arg: The exit code for the process. |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 412 | args->Append(Value::CreateIntegerValue(0)); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 413 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 414 | DispatchEvent(keys::kOnExited, args.Pass()); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 415 | #endif // defined(ENABLE_TASK_MANAGER) |
| 416 | } |
| 417 | |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 418 | void ProcessesEventRouter::ProcessHangEvent(content::RenderWidgetHost* widget) { |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 419 | #if defined(ENABLE_TASK_MANAGER) |
| 420 | std::string event(keys::kOnUnresponsive); |
| 421 | if (!HasEventListeners(event)) |
| 422 | return; |
| 423 | |
| 424 | DictionaryValue* process = NULL; |
| 425 | int count = model_->ResourceCount(); |
| 426 | int id = widget->GetProcess()->GetID(); |
| 427 | |
| 428 | for (int i = 0; i < count; ++i) { |
| 429 | if (model_->IsResourceFirstInGroup(i)) { |
| 430 | if (id == model_->GetUniqueChildProcessId(i)) { |
| 431 | process = CreateProcessFromModel(id, model_, i, false); |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | DCHECK(process); |
| 438 | if (process == NULL) |
| 439 | return; |
| 440 | |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 441 | scoped_ptr<ListValue> args(new ListValue()); |
| 442 | args->Append(process); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 443 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 444 | DispatchEvent(keys::kOnUnresponsive, args.Pass()); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 445 | #endif // defined(ENABLE_TASK_MANAGER) |
| 446 | } |
| 447 | |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 448 | void ProcessesEventRouter::ProcessClosedEvent( |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 449 | content::RenderProcessHost* rph, |
| 450 | content::RenderProcessHost::RendererClosedDetails* details) { |
| 451 | #if defined(ENABLE_TASK_MANAGER) |
| 452 | // The callback function parameters. |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 453 | scoped_ptr<ListValue> args(new ListValue()); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 454 | |
| 455 | // First arg: The id of the process that was closed. |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 456 | args->Append(Value::CreateIntegerValue(rph->GetID())); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 457 | |
| 458 | // Second arg: The exit type for the process. |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 459 | args->Append(Value::CreateIntegerValue(details->status)); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 460 | |
| 461 | // Third arg: The exit code for the process. |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 462 | args->Append(Value::CreateIntegerValue(details->exit_code)); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 463 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 464 | DispatchEvent(keys::kOnExited, args.Pass()); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 465 | #endif // defined(ENABLE_TASK_MANAGER) |
| 466 | } |
| 467 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 468 | void ProcessesEventRouter::DispatchEvent(const char* event_name, |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 469 | scoped_ptr<ListValue> event_args) { |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 470 | if (extensions::ExtensionSystem::Get(profile_)->event_router()) { |
[email protected] | 01f7a804 | 2012-12-07 07:48:02 | [diff] [blame] | 471 | scoped_ptr<extensions::Event> event(new extensions::Event( |
| 472 | event_name, event_args.Pass())); |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 473 | extensions::ExtensionSystem::Get(profile_)->event_router()-> |
[email protected] | 01f7a804 | 2012-12-07 07:48:02 | [diff] [blame] | 474 | BroadcastEvent(event.Pass()); |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 478 | bool ProcessesEventRouter::HasEventListeners(const std::string& event_name) { |
| 479 | extensions::EventRouter* router = |
| 480 | extensions::ExtensionSystem::Get(profile_)->event_router(); |
[email protected] | f05c4fc2 | 2012-12-09 09:25:21 | [diff] [blame] | 481 | if (router && router->HasEventListener(event_name)) |
| 482 | return true; |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 483 | return false; |
[email protected] | d81d3a19 | 2012-11-09 00:32:50 | [diff] [blame] | 484 | } |
| 485 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 486 | ProcessesAPI::ProcessesAPI(Profile* profile) : profile_(profile) { |
| 487 | ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 488 | this, processes_api_constants::kOnUpdated); |
| 489 | ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 490 | this, processes_api_constants::kOnUpdatedWithMemory); |
[email protected] | 8e0438a7 | 2012-12-05 02:17:42 | [diff] [blame] | 491 | ExtensionFunctionRegistry* registry = |
| 492 | ExtensionFunctionRegistry::GetInstance(); |
| 493 | registry->RegisterFunction<extensions::GetProcessIdForTabFunction>(); |
| 494 | registry->RegisterFunction<extensions::TerminateFunction>(); |
| 495 | registry->RegisterFunction<extensions::GetProcessInfoFunction>(); |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 496 | } |
[email protected] | d81d3a19 | 2012-11-09 00:32:50 | [diff] [blame] | 497 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 498 | ProcessesAPI::~ProcessesAPI() { |
| 499 | } |
| 500 | |
| 501 | void ProcessesAPI::Shutdown() { |
| 502 | ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 503 | } |
| 504 | |
[email protected] | c39df1a | 2013-01-08 18:22:07 | [diff] [blame] | 505 | static base::LazyInstance<ProfileKeyedAPIFactory<ProcessesAPI> > |
| 506 | g_factory = LAZY_INSTANCE_INITIALIZER; |
| 507 | |
| 508 | // static |
| 509 | ProfileKeyedAPIFactory<ProcessesAPI>* ProcessesAPI::GetFactoryInstance() { |
| 510 | return &g_factory.Get(); |
| 511 | } |
| 512 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 513 | // static |
| 514 | ProcessesAPI* ProcessesAPI::Get(Profile* profile) { |
[email protected] | 01d0ffd | 2012-12-26 23:22:51 | [diff] [blame] | 515 | return ProfileKeyedAPIFactory<ProcessesAPI>::GetForProfile(profile); |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | ProcessesEventRouter* ProcessesAPI::processes_event_router() { |
| 519 | if (!processes_event_router_) |
| 520 | processes_event_router_.reset(new ProcessesEventRouter(profile_)); |
| 521 | return processes_event_router_.get(); |
| 522 | } |
| 523 | |
[email protected] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 524 | void ProcessesAPI::OnListenerAdded(const EventListenerInfo& details) { |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 525 | // We lazily tell the TaskManager to start updating when listeners to the |
| 526 | // processes.onUpdated or processes.onUpdatedWithMemory events arrive. |
| 527 | processes_event_router()->ListenerAdded(); |
| 528 | } |
| 529 | |
[email protected] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 530 | void ProcessesAPI::OnListenerRemoved(const EventListenerInfo& details) { |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 531 | // If a processes.onUpdated or processes.onUpdatedWithMemory event listener |
| 532 | // is removed (or a process with one exits), then we let the extension API |
| 533 | // know that it has one fewer listener. |
| 534 | processes_event_router()->ListenerRemoved(); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 535 | } |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 536 | |
[email protected] | ef3f4f9 | 2012-10-05 03:28:29 | [diff] [blame] | 537 | GetProcessIdForTabFunction::GetProcessIdForTabFunction() : tab_id_(-1) { |
| 538 | } |
| 539 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 540 | bool GetProcessIdForTabFunction::RunImpl() { |
| 541 | #if defined(ENABLE_TASK_MANAGER) |
| 542 | EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id_)); |
| 543 | |
| 544 | // Add a reference, which is balanced in GetProcessIdForTab to keep the object |
| 545 | // around and allow for the callback to be invoked. |
| 546 | AddRef(); |
| 547 | |
| 548 | // If the task manager is already listening, just post a task to execute |
| 549 | // which will invoke the callback once we have returned from this function. |
| 550 | // Otherwise, wait for the notification that the task manager is done with |
| 551 | // the data gathering. |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 552 | if (ProcessesAPI::Get(profile_)->processes_event_router()-> |
| 553 | is_task_manager_listening()) { |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 554 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 555 | &GetProcessIdForTabFunction::GetProcessIdForTab, this)); |
| 556 | } else { |
| 557 | registrar_.Add(this, |
| 558 | chrome::NOTIFICATION_TASK_MANAGER_CHILD_PROCESSES_DATA_READY, |
| 559 | content::NotificationService::AllSources()); |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 560 | ProcessesAPI::Get(profile_)->processes_event_router()-> |
| 561 | StartTaskManagerListening(); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | return true; |
| 565 | #else |
[email protected] | c92dd3f | 2012-06-20 19:49:58 | [diff] [blame] | 566 | error_ = errors::kExtensionNotSupported; |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 567 | return false; |
| 568 | #endif // defined(ENABLE_TASK_MANAGER) |
| 569 | } |
| 570 | |
| 571 | void GetProcessIdForTabFunction::Observe( |
| 572 | int type, |
| 573 | const content::NotificationSource& source, |
| 574 | const content::NotificationDetails& details) { |
| 575 | DCHECK_EQ(type, chrome::NOTIFICATION_TASK_MANAGER_CHILD_PROCESSES_DATA_READY); |
| 576 | registrar_.RemoveAll(); |
| 577 | GetProcessIdForTab(); |
| 578 | } |
| 579 | |
| 580 | void GetProcessIdForTabFunction::GetProcessIdForTab() { |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 581 | content::WebContents* contents = NULL; |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 582 | int tab_index = -1; |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 583 | if (!ExtensionTabUtil::GetTabById(tab_id_, profile(), include_incognito(), |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 584 | NULL, NULL, &contents, &tab_index)) { |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 585 | error_ = ErrorUtils::FormatErrorMessage( |
[email protected] | b19451b | 2012-06-08 17:36:19 | [diff] [blame] | 586 | extensions::tabs_constants::kTabNotFoundError, |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 587 | base::IntToString(tab_id_)); |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 588 | SetResult(Value::CreateIntegerValue(-1)); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 589 | SendResponse(false); |
| 590 | } else { |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 591 | int process_id = contents->GetRenderProcessHost()->GetID(); |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 592 | SetResult(Value::CreateIntegerValue(process_id)); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 593 | SendResponse(true); |
[email protected] | 8a661f8 | 2010-10-19 21:47:11 | [diff] [blame] | 594 | } |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 595 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 596 | // Balance the AddRef in the RunImpl. |
| 597 | Release(); |
| 598 | } |
| 599 | |
[email protected] | ef3f4f9 | 2012-10-05 03:28:29 | [diff] [blame] | 600 | TerminateFunction::TerminateFunction() : process_id_(-1) { |
| 601 | } |
| 602 | |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 603 | bool TerminateFunction::RunImpl() { |
| 604 | #if defined(ENABLE_TASK_MANAGER) |
| 605 | EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &process_id_)); |
| 606 | |
| 607 | // Add a reference, which is balanced in TerminateProcess to keep the object |
| 608 | // around and allow for the callback to be invoked. |
| 609 | AddRef(); |
| 610 | |
| 611 | // If the task manager is already listening, just post a task to execute |
| 612 | // which will invoke the callback once we have returned from this function. |
| 613 | // Otherwise, wait for the notification that the task manager is done with |
| 614 | // the data gathering. |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 615 | if (ProcessesAPI::Get(profile_)->processes_event_router()-> |
| 616 | is_task_manager_listening()) { |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 617 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 618 | &TerminateFunction::TerminateProcess, this)); |
| 619 | } else { |
| 620 | registrar_.Add(this, |
| 621 | chrome::NOTIFICATION_TASK_MANAGER_CHILD_PROCESSES_DATA_READY, |
| 622 | content::NotificationService::AllSources()); |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 623 | ProcessesAPI::Get(profile_)->processes_event_router()-> |
| 624 | StartTaskManagerListening(); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 625 | } |
| 626 | |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 627 | return true; |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 628 | #else |
[email protected] | c92dd3f | 2012-06-20 19:49:58 | [diff] [blame] | 629 | error_ = errors::kExtensionNotSupported; |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 630 | return false; |
| 631 | #endif // defined(ENABLE_TASK_MANAGER) |
| 632 | } |
| 633 | |
| 634 | void TerminateFunction::Observe( |
| 635 | int type, |
| 636 | const content::NotificationSource& source, |
| 637 | const content::NotificationDetails& details) { |
| 638 | DCHECK_EQ(type, chrome::NOTIFICATION_TASK_MANAGER_CHILD_PROCESSES_DATA_READY); |
| 639 | registrar_.RemoveAll(); |
| 640 | TerminateProcess(); |
| 641 | } |
| 642 | |
| 643 | void TerminateFunction::TerminateProcess() { |
| 644 | TaskManagerModel* model = TaskManager::GetInstance()->model(); |
| 645 | |
| 646 | int count = model->ResourceCount(); |
| 647 | bool killed = false; |
| 648 | bool found = false; |
| 649 | |
| 650 | for (int i = 0; i < count; ++i) { |
| 651 | if (model->IsResourceFirstInGroup(i)) { |
| 652 | if (process_id_ == model->GetUniqueChildProcessId(i)) { |
| 653 | found = true; |
| 654 | killed = base::KillProcess(model->GetProcess(i), |
| 655 | content::RESULT_CODE_KILLED, true); |
| 656 | UMA_HISTOGRAM_COUNTS("ChildProcess.KilledByExtensionAPI", 1); |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | if (!found) { |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 663 | error_ = ErrorUtils::FormatErrorMessage(errors::kProcessNotFound, |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 664 | base::IntToString(process_id_)); |
| 665 | SendResponse(false); |
| 666 | } else { |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 667 | SetResult(Value::CreateBooleanValue(killed)); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 668 | SendResponse(true); |
| 669 | } |
| 670 | |
| 671 | // Balance the AddRef in the RunImpl. |
| 672 | Release(); |
| 673 | } |
| 674 | |
[email protected] | 8b8e65e | 2012-09-14 06:47:34 | [diff] [blame] | 675 | GetProcessInfoFunction::GetProcessInfoFunction() |
| 676 | #if defined(ENABLE_TASK_MANAGER) |
| 677 | : memory_(false) |
| 678 | #endif |
| 679 | { |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | GetProcessInfoFunction::~GetProcessInfoFunction() { |
| 683 | } |
| 684 | |
| 685 | bool GetProcessInfoFunction::RunImpl() { |
| 686 | #if defined(ENABLE_TASK_MANAGER) |
| 687 | Value* processes = NULL; |
| 688 | |
| 689 | EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &processes)); |
| 690 | EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &memory_)); |
| 691 | |
| 692 | EXTENSION_FUNCTION_VALIDATE(extensions::ReadOneOrMoreIntegers( |
| 693 | processes, &process_ids_)); |
| 694 | |
| 695 | // Add a reference, which is balanced in GatherProcessInfo to keep the object |
| 696 | // around and allow for the callback to be invoked. |
| 697 | AddRef(); |
| 698 | |
| 699 | // If the task manager is already listening, just post a task to execute |
| 700 | // which will invoke the callback once we have returned from this function. |
| 701 | // Otherwise, wait for the notification that the task manager is done with |
| 702 | // the data gathering. |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 703 | if (ProcessesAPI::Get(profile_)->processes_event_router()-> |
| 704 | is_task_manager_listening()) { |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 705 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 706 | &GetProcessInfoFunction::GatherProcessInfo, this)); |
| 707 | } else { |
| 708 | registrar_.Add(this, |
| 709 | chrome::NOTIFICATION_TASK_MANAGER_CHILD_PROCESSES_DATA_READY, |
| 710 | content::NotificationService::AllSources()); |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 711 | ProcessesAPI::Get(profile_)->processes_event_router()-> |
| 712 | StartTaskManagerListening(); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 713 | } |
| 714 | return true; |
| 715 | |
| 716 | #else |
[email protected] | c92dd3f | 2012-06-20 19:49:58 | [diff] [blame] | 717 | error_ = errors::kExtensionNotSupported; |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 718 | return false; |
| 719 | #endif // defined(ENABLE_TASK_MANAGER) |
| 720 | } |
| 721 | |
| 722 | void GetProcessInfoFunction::Observe( |
| 723 | int type, |
| 724 | const content::NotificationSource& source, |
| 725 | const content::NotificationDetails& details) { |
| 726 | DCHECK_EQ(type, chrome::NOTIFICATION_TASK_MANAGER_CHILD_PROCESSES_DATA_READY); |
| 727 | registrar_.RemoveAll(); |
| 728 | GatherProcessInfo(); |
| 729 | } |
| 730 | |
| 731 | void GetProcessInfoFunction::GatherProcessInfo() { |
[email protected] | c92dd3f | 2012-06-20 19:49:58 | [diff] [blame] | 732 | #if defined(ENABLE_TASK_MANAGER) |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 733 | TaskManagerModel* model = TaskManager::GetInstance()->model(); |
| 734 | DictionaryValue* processes = new DictionaryValue(); |
| 735 | |
| 736 | // If there are no process IDs specified, it means we need to return all of |
| 737 | // the ones we know of. |
| 738 | if (process_ids_.size() == 0) { |
| 739 | int resources = model->ResourceCount(); |
| 740 | for (int i = 0; i < resources; ++i) { |
| 741 | if (model->IsResourceFirstInGroup(i)) { |
| 742 | int id = model->GetUniqueChildProcessId(i); |
| 743 | DictionaryValue* d = CreateProcessFromModel(id, model, i, false); |
| 744 | if (memory_) |
| 745 | AddMemoryDetails(d, model, i); |
| 746 | processes->Set(base::IntToString(id), d); |
| 747 | } |
| 748 | } |
| 749 | } else { |
| 750 | int resources = model->ResourceCount(); |
| 751 | for (int i = 0; i < resources; ++i) { |
| 752 | if (model->IsResourceFirstInGroup(i)) { |
| 753 | int id = model->GetUniqueChildProcessId(i); |
| 754 | std::vector<int>::iterator proc_id = std::find(process_ids_.begin(), |
| 755 | process_ids_.end(), id); |
| 756 | if (proc_id != process_ids_.end()) { |
| 757 | DictionaryValue* d = CreateProcessFromModel(id, model, i, false); |
| 758 | if (memory_) |
| 759 | AddMemoryDetails(d, model, i); |
| 760 | processes->Set(base::IntToString(id), d); |
| 761 | |
| 762 | process_ids_.erase(proc_id); |
| 763 | if (process_ids_.size() == 0) |
| 764 | break; |
| 765 | } |
| 766 | } |
| 767 | } |
[email protected] | 79a6064 | 2012-10-20 21:03:18 | [diff] [blame] | 768 | DCHECK_EQ(process_ids_.size(), 0U); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 769 | } |
| 770 | |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 771 | SetResult(processes); |
[email protected] | 350584cd | 2012-05-17 18:18:26 | [diff] [blame] | 772 | SendResponse(true); |
| 773 | |
| 774 | // Balance the AddRef in the RunImpl. |
| 775 | Release(); |
[email protected] | c92dd3f | 2012-06-20 19:49:58 | [diff] [blame] | 776 | #endif // defined(ENABLE_TASK_MANAGER) |
[email protected] | 381162b | 2010-01-28 17:29:35 | [diff] [blame] | 777 | } |
[email protected] | bc500fa | 2012-09-06 09:03:30 | [diff] [blame] | 778 | |
| 779 | } // namespace extensions |