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/extensions/extension_processes_api.h" | 5 #include "chrome/browser/extensions/extension_processes_api.h" |
6 | 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/json/json_writer.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/string_number_conversions.h" |
| 11 #include "base/task.h" |
| 12 #include "base/utf_string_conversions.h" |
7 #include "base/values.h" | 13 #include "base/values.h" |
8 | 14 |
| 15 #include "chrome/browser/extensions/extension_message_service.h" |
| 16 #include "chrome/browser/extensions/extension_processes_api_constants.h" |
9 #include "chrome/browser/extensions/extension_tabs_module.h" | 17 #include "chrome/browser/extensions/extension_tabs_module.h" |
10 #include "chrome/browser/extensions/extension_processes_api_constants.h" | 18 #include "chrome/browser/profile.h" |
11 #include "chrome/browser/renderer_host/render_process_host.h" | 19 #include "chrome/browser/renderer_host/render_process_host.h" |
12 #include "chrome/browser/tab_contents/tab_contents.h" | 20 #include "chrome/browser/tab_contents/tab_contents.h" |
| 21 #include "chrome/browser/task_manager/task_manager.h" |
| 22 #include "chrome/common/notification_service.h" |
| 23 #include "chrome/common/notification_type.h" |
13 | 24 |
14 namespace keys = extension_processes_api_constants; | 25 namespace keys = extension_processes_api_constants; |
15 | 26 |
16 DictionaryValue* CreateProcessValue(int process_id) { | 27 DictionaryValue* CreateProcessValue(int process_id, |
| 28 std::string type, |
| 29 double cpu, |
| 30 int net, |
| 31 int pr_mem, |
| 32 int sh_mem) { |
17 DictionaryValue* result = new DictionaryValue(); | 33 DictionaryValue* result = new DictionaryValue(); |
18 result->SetInteger(keys::kIdKey, process_id); | 34 result->SetInteger(keys::kIdKey, process_id); |
| 35 result->SetString(keys::kTypeKey, type); |
| 36 result->SetReal(keys::kCpuKey, cpu); |
| 37 result->SetInteger(keys::kNetworkKey, net); |
| 38 result->SetInteger(keys::kPrivateMemoryKey, pr_mem); |
| 39 result->SetInteger(keys::kSharedMemoryKey, sh_mem); |
| 40 return result; |
| 41 } |
19 | 42 |
20 return result; | 43 ExtensionProcessesEventRouter* ExtensionProcessesEventRouter::GetInstance() { |
| 44 return Singleton<ExtensionProcessesEventRouter>::get(); |
| 45 } |
| 46 |
| 47 ExtensionProcessesEventRouter::ExtensionProcessesEventRouter() { |
| 48 model_ = TaskManager::GetInstance()->model(); |
| 49 model_->AddObserver(this); |
| 50 } |
| 51 |
| 52 ExtensionProcessesEventRouter::~ExtensionProcessesEventRouter() { |
| 53 model_->RemoveObserver(this); |
| 54 } |
| 55 |
| 56 void ExtensionProcessesEventRouter::ObserveProfile(Profile* profile) { |
| 57 profiles_.insert(profile); |
| 58 } |
| 59 |
| 60 void ExtensionProcessesEventRouter::ListenerAdded() { |
| 61 model_->StartUpdating(); |
| 62 } |
| 63 |
| 64 void ExtensionProcessesEventRouter::ListenerRemoved() { |
| 65 model_->StopUpdating(); |
| 66 } |
| 67 |
| 68 void ExtensionProcessesEventRouter::OnItemsChanged(int start, int length) { |
| 69 if (model_) { |
| 70 ListValue args; |
| 71 DictionaryValue* processes = new DictionaryValue(); |
| 72 for (int i = start; i < start + length; i++) { |
| 73 if (model_->IsResourceFirstInGroup(i)) { |
| 74 int id = model_->GetProcessId(i); |
| 75 std::string type = keys::kProcessTypePlugin; |
| 76 TabContents* contents = model_->GetResourceTabContents(i); |
| 77 if (contents) { |
| 78 type = keys::kProcessTypeRenderer; |
| 79 } else if (model_->GetResourceExtension(i)) { |
| 80 type = keys::kProcessTypeExtension; |
| 81 } else if (TaskManager::GetInstance()->IsBrowserProcess(i)) { |
| 82 type = keys::kProcessTypeBrowser; |
| 83 } |
| 84 |
| 85 // Get process metrics as numbers |
| 86 double cpu = model_->GetCPUUsage(i); |
| 87 // TODO(creis): Network is actually reported per-resource (tab), |
| 88 // not per-process. We should aggregate it here. |
| 89 int net = (int) model_->GetNetworkUsage(i); |
| 90 size_t mem; |
| 91 int pr_mem = (model_->GetPrivateMemory(i, &mem) ? mem : -1); |
| 92 int sh_mem = (model_->GetSharedMemory(i, &mem) ? mem : -1); |
| 93 |
| 94 // Store each process indexed by the string version of its id |
| 95 processes->Set(base::IntToString(id), |
| 96 CreateProcessValue(id, type, cpu, net, pr_mem, sh_mem)); |
| 97 } |
| 98 } |
| 99 args.Append(processes); |
| 100 |
| 101 std::string json_args; |
| 102 base::JSONWriter::Write(&args, false, &json_args); |
| 103 |
| 104 // Notify each profile that is interested. |
| 105 for (ProfileSet::iterator it = profiles_.begin(); |
| 106 it != profiles_.end(); it++) { |
| 107 Profile* profile = *it; |
| 108 DispatchEvent(profile, keys::kOnUpdated, json_args); |
| 109 } |
| 110 } |
| 111 } |
| 112 |
| 113 void ExtensionProcessesEventRouter::DispatchEvent(Profile* profile, |
| 114 const char* event_name, |
| 115 const std::string& json_args) { |
| 116 if (profile && profile->GetExtensionMessageService()) { |
| 117 profile->GetExtensionMessageService()->DispatchEventToRenderers( |
| 118 event_name, json_args, NULL, GURL()); |
| 119 } |
21 } | 120 } |
22 | 121 |
23 bool GetProcessForTabFunction::RunImpl() { | 122 bool GetProcessForTabFunction::RunImpl() { |
24 int tab_id; | 123 int tab_id; |
25 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | 124 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); |
26 | 125 |
27 TabContents* contents = NULL; | 126 TabContents* contents = NULL; |
28 int tab_index = -1; | 127 int tab_index = -1; |
29 if (!ExtensionTabUtil::GetTabById(tab_id, profile(), include_incognito(), | 128 if (!ExtensionTabUtil::GetTabById(tab_id, profile(), include_incognito(), |
30 NULL, NULL, &contents, &tab_index)) | 129 NULL, NULL, &contents, &tab_index)) |
31 return false; | 130 return false; |
32 | 131 |
33 int process_id = contents->GetRenderProcessHost()->id(); | 132 // Return the process ID of the tab as an integer. |
34 result_.reset(CreateProcessValue(process_id)); | 133 int id = base::GetProcId(contents->GetRenderProcessHost()->GetHandle()); |
| 134 result_.reset(Value::CreateIntegerValue(id)); |
35 return true; | 135 return true; |
36 } | 136 } |
OLD | NEW |