blob: c4a5dd7f265439a1d7439332b6732e1e25ab8b4c [file] [log] [blame]
[email protected]381162b2010-01-28 17:29:351// 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
3// found in the LICENSE file.
4
5#include "chrome/browser/extensions/extension_processes_api.h"
6
[email protected]8a661f82010-10-19 21:47:117#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"
[email protected]c02c853d72010-08-07 06:23:2413#include "base/values.h"
14
[email protected]8a661f82010-10-19 21:47:1115#include "chrome/browser/extensions/extension_event_router.h"
[email protected]6ec6313d2010-10-13 22:24:1416#include "chrome/browser/extensions/extension_processes_api_constants.h"
[email protected]8a661f82010-10-19 21:47:1117#include "chrome/browser/extensions/extension_tabs_module.h"
18#include "chrome/browser/extensions/extension_tabs_module_constants.h"
[email protected]8ecad5e2010-12-02 21:18:3319#include "chrome/browser/profiles/profile.h"
[email protected]381162b2010-01-28 17:29:3520#include "chrome/browser/renderer_host/render_process_host.h"
21#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]8a661f82010-10-19 21:47:1122#include "chrome/browser/task_manager/task_manager.h"
[email protected]6a3ec2312010-12-02 19:30:1923#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
[email protected]8a661f82010-10-19 21:47:1124#include "chrome/common/extensions/extension_error_utils.h"
[email protected]8a661f82010-10-19 21:47:1125#include "chrome/common/notification_type.h"
[email protected]381162b2010-01-28 17:29:3526
27namespace keys = extension_processes_api_constants;
28
[email protected]8a661f82010-10-19 21:47:1129DictionaryValue* CreateProcessValue(int process_id,
30 std::string type,
31 double cpu,
32 int64 net,
33 int64 pr_mem,
34 int64 sh_mem) {
[email protected]381162b2010-01-28 17:29:3535 DictionaryValue* result = new DictionaryValue();
36 result->SetInteger(keys::kIdKey, process_id);
[email protected]8a661f82010-10-19 21:47:1137 result->SetString(keys::kTypeKey, type);
[email protected]fb534c92011-02-01 01:02:0738 result->SetDouble(keys::kCpuKey, cpu);
39 result->SetDouble(keys::kNetworkKey, static_cast<double>(net));
40 result->SetDouble(keys::kPrivateMemoryKey, static_cast<double>(pr_mem));
41 result->SetDouble(keys::kSharedMemoryKey, static_cast<double>(sh_mem));
[email protected]381162b2010-01-28 17:29:3542 return result;
43}
44
[email protected]8a661f82010-10-19 21:47:1145ExtensionProcessesEventRouter* ExtensionProcessesEventRouter::GetInstance() {
46 return Singleton<ExtensionProcessesEventRouter>::get();
47}
48
49ExtensionProcessesEventRouter::ExtensionProcessesEventRouter() {
50 model_ = TaskManager::GetInstance()->model();
51 model_->AddObserver(this);
52}
53
54ExtensionProcessesEventRouter::~ExtensionProcessesEventRouter() {
55 model_->RemoveObserver(this);
56}
57
58void ExtensionProcessesEventRouter::ObserveProfile(Profile* profile) {
59 profiles_.insert(profile);
60}
61
62void ExtensionProcessesEventRouter::ListenerAdded() {
63 model_->StartUpdating();
64}
65
66void ExtensionProcessesEventRouter::ListenerRemoved() {
67 model_->StopUpdating();
68}
69
70void ExtensionProcessesEventRouter::OnItemsChanged(int start, int length) {
71 if (model_) {
72 ListValue args;
73 DictionaryValue* processes = new DictionaryValue();
74 for (int i = start; i < start + length; i++) {
75 if (model_->IsResourceFirstInGroup(i)) {
76 int id = model_->GetProcessId(i);
77
78 // Determine process type
79 std::string type = keys::kProcessTypeOther;
80 TaskManager::Resource::Type resource_type = model_->GetResourceType(i);
81 switch (resource_type) {
82 case TaskManager::Resource::BROWSER:
83 type = keys::kProcessTypeBrowser;
84 break;
85 case TaskManager::Resource::RENDERER:
86 type = keys::kProcessTypeRenderer;
87 break;
88 case TaskManager::Resource::EXTENSION:
89 type = keys::kProcessTypeExtension;
90 break;
91 case TaskManager::Resource::NOTIFICATION:
92 type = keys::kProcessTypeNotification;
93 break;
94 case TaskManager::Resource::PLUGIN:
95 type = keys::kProcessTypePlugin;
96 break;
97 case TaskManager::Resource::WORKER:
98 type = keys::kProcessTypeWorker;
99 break;
100 case TaskManager::Resource::NACL:
101 type = keys::kProcessTypeNacl;
102 break;
103 case TaskManager::Resource::UTILITY:
104 type = keys::kProcessTypeUtility;
105 break;
106 case TaskManager::Resource::GPU:
107 type = keys::kProcessTypeGPU;
108 break;
109 case TaskManager::Resource::PROFILE_IMPORT:
110 case TaskManager::Resource::ZYGOTE:
111 case TaskManager::Resource::SANDBOX_HELPER:
112 case TaskManager::Resource::UNKNOWN:
113 type = keys::kProcessTypeOther;
114 break;
115 default:
116 NOTREACHED() << "Unknown resource type.";
117 }
118
119 // Get process metrics as numbers
120 double cpu = model_->GetCPUUsage(i);
121
122 // TODO(creis): Network is actually reported per-resource (tab),
123 // not per-process. We should aggregate it here.
124 int64 net = model_->GetNetworkUsage(i);
125 size_t mem;
126 int64 pr_mem = model_->GetPrivateMemory(i, &mem) ?
127 static_cast<int64>(mem) : -1;
128 int64 sh_mem = model_->GetSharedMemory(i, &mem) ?
129 static_cast<int64>(mem) : -1;
130
131 // Store each process indexed by the string version of its id
132 processes->Set(base::IntToString(id),
133 CreateProcessValue(id, type, cpu, net, pr_mem, sh_mem));
134 }
135 }
136 args.Append(processes);
137
138 std::string json_args;
139 base::JSONWriter::Write(&args, false, &json_args);
140
141 // Notify each profile that is interested.
142 for (ProfileSet::iterator it = profiles_.begin();
143 it != profiles_.end(); it++) {
144 Profile* profile = *it;
145 DispatchEvent(profile, keys::kOnUpdated, json_args);
146 }
147 }
148}
149
150void ExtensionProcessesEventRouter::DispatchEvent(Profile* profile,
151 const char* event_name,
152 const std::string& json_args) {
153 if (profile && profile->GetExtensionEventRouter()) {
154 profile->GetExtensionEventRouter()->DispatchEventToRenderers(
155 event_name, json_args, NULL, GURL());
156 }
157}
158
159bool GetProcessIdForTabFunction::RunImpl() {
[email protected]381162b2010-01-28 17:29:35160 int tab_id;
[email protected]438c97d2010-05-21 23:30:15161 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id));
[email protected]381162b2010-01-28 17:29:35162
[email protected]3c9e1872010-11-18 16:17:49163 TabContentsWrapper* contents = NULL;
[email protected]381162b2010-01-28 17:29:35164 int tab_index = -1;
[email protected]db7331a2010-02-25 22:10:50165 if (!ExtensionTabUtil::GetTabById(tab_id, profile(), include_incognito(),
[email protected]8a661f82010-10-19 21:47:11166 NULL, NULL, &contents, &tab_index)) {
167 error_ = ExtensionErrorUtils::FormatErrorMessage(
168 extension_tabs_module_constants::kTabNotFoundError,
169 base::IntToString(tab_id));
[email protected]381162b2010-01-28 17:29:35170 return false;
[email protected]8a661f82010-10-19 21:47:11171 }
[email protected]381162b2010-01-28 17:29:35172
[email protected]8a661f82010-10-19 21:47:11173 // Return the process ID of the tab as an integer.
[email protected]3c9e1872010-11-18 16:17:49174 int id = base::GetProcId(contents->tab_contents()->
175 GetRenderProcessHost()->GetHandle());
[email protected]8a661f82010-10-19 21:47:11176 result_.reset(Value::CreateIntegerValue(id));
[email protected]381162b2010-01-28 17:29:35177 return true;
178}