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