| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/task_manager/task_manager_resource_providers.h" | 5 #include "chrome/browser/task_manager/task_manager_resource_providers.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/file_version_info.h" | 8 #include "base/file_version_info.h" |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
| 11 #include "base/stl_util-inl.h" | 11 #include "base/stl_util-inl.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "chrome/app/chrome_command_ids.h" | 16 #include "chrome/app/chrome_command_ids.h" |
| 17 #include "chrome/browser/background_contents_service.h" | 17 #include "chrome/browser/background_contents_service.h" |
| 18 #include "chrome/browser/background_contents_service_factory.h" | 18 #include "chrome/browser/background_contents_service_factory.h" |
| 19 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/extensions/extension_host.h" | 20 #include "chrome/browser/extensions/extension_host.h" |
| 21 #include "chrome/browser/extensions/extension_process_manager.h" | 21 #include "chrome/browser/extensions/extension_process_manager.h" |
| 22 #include "chrome/browser/extensions/extension_service.h" | 22 #include "chrome/browser/extensions/extension_service.h" |
| 23 #include "chrome/browser/favicon/favicon_tab_helper.h" | 23 #include "chrome/browser/favicon/favicon_tab_helper.h" |
| 24 #include "chrome/browser/notifications/balloon_collection.h" | |
| 25 #include "chrome/browser/notifications/balloon_host.h" | |
| 26 #include "chrome/browser/notifications/notification_ui_manager.h" | |
| 27 #include "chrome/browser/prerender/prerender_manager.h" | 24 #include "chrome/browser/prerender/prerender_manager.h" |
| 28 #include "chrome/browser/profiles/profile_manager.h" | 25 #include "chrome/browser/profiles/profile_manager.h" |
| 29 #include "chrome/browser/tab_contents/background_contents.h" | 26 #include "chrome/browser/tab_contents/background_contents.h" |
| 30 #include "chrome/browser/tab_contents/tab_util.h" | 27 #include "chrome/browser/tab_contents/tab_util.h" |
| 31 #include "chrome/browser/ui/browser_list.h" | 28 #include "chrome/browser/ui/browser_list.h" |
| 32 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 29 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 33 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
| 34 #include "chrome/common/extensions/extension.h" | 31 #include "chrome/common/extensions/extension.h" |
| 35 #include "chrome/common/render_messages.h" | 32 #include "chrome/common/render_messages.h" |
| 36 #include "content/browser/browser_child_process_host.h" | 33 #include "content/browser/browser_child_process_host.h" |
| (...skipping 1138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1175 pid_to_resources_.find(resource->process_id()); | 1172 pid_to_resources_.find(resource->process_id()); |
| 1176 DCHECK(pid_iter != pid_to_resources_.end()); | 1173 DCHECK(pid_iter != pid_to_resources_.end()); |
| 1177 if (pid_iter != pid_to_resources_.end()) | 1174 if (pid_iter != pid_to_resources_.end()) |
| 1178 pid_to_resources_.erase(pid_iter); | 1175 pid_to_resources_.erase(pid_iter); |
| 1179 | 1176 |
| 1180 // Finally, delete the resource. | 1177 // Finally, delete the resource. |
| 1181 delete resource; | 1178 delete resource; |
| 1182 } | 1179 } |
| 1183 | 1180 |
| 1184 //////////////////////////////////////////////////////////////////////////////// | 1181 //////////////////////////////////////////////////////////////////////////////// |
| 1185 // TaskManagerNotificationResource class | |
| 1186 //////////////////////////////////////////////////////////////////////////////// | |
| 1187 | |
| 1188 SkBitmap* TaskManagerNotificationResource::default_icon_ = NULL; | |
| 1189 | |
| 1190 TaskManagerNotificationResource::TaskManagerNotificationResource( | |
| 1191 BalloonHost* balloon_host) | |
| 1192 : balloon_host_(balloon_host) { | |
| 1193 if (!default_icon_) { | |
| 1194 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 1195 default_icon_ = rb.GetBitmapNamed(IDR_PLUGIN); | |
| 1196 } | |
| 1197 process_handle_ = balloon_host_->render_view_host()->process()->GetHandle(); | |
| 1198 pid_ = base::GetProcId(process_handle_); | |
| 1199 title_ = l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_NOTIFICATION_PREFIX, | |
| 1200 balloon_host_->GetSource()); | |
| 1201 } | |
| 1202 | |
| 1203 TaskManagerNotificationResource::~TaskManagerNotificationResource() { | |
| 1204 } | |
| 1205 | |
| 1206 string16 TaskManagerNotificationResource::GetTitle() const { | |
| 1207 return title_; | |
| 1208 } | |
| 1209 | |
| 1210 SkBitmap TaskManagerNotificationResource::GetIcon() const { | |
| 1211 return *default_icon_; | |
| 1212 } | |
| 1213 | |
| 1214 base::ProcessHandle TaskManagerNotificationResource::GetProcess() const { | |
| 1215 return process_handle_; | |
| 1216 } | |
| 1217 | |
| 1218 TaskManager::Resource::Type TaskManagerNotificationResource::GetType() const { | |
| 1219 return NOTIFICATION; | |
| 1220 } | |
| 1221 | |
| 1222 bool TaskManagerNotificationResource::SupportNetworkUsage() const { | |
| 1223 return false; | |
| 1224 } | |
| 1225 | |
| 1226 //////////////////////////////////////////////////////////////////////////////// | |
| 1227 // TaskManagerNotificationResourceProvider class | |
| 1228 //////////////////////////////////////////////////////////////////////////////// | |
| 1229 | |
| 1230 TaskManagerNotificationResourceProvider:: | |
| 1231 TaskManagerNotificationResourceProvider(TaskManager* task_manager) | |
| 1232 : task_manager_(task_manager), | |
| 1233 updating_(false) { | |
| 1234 } | |
| 1235 | |
| 1236 TaskManagerNotificationResourceProvider:: | |
| 1237 ~TaskManagerNotificationResourceProvider() { | |
| 1238 } | |
| 1239 | |
| 1240 TaskManager::Resource* TaskManagerNotificationResourceProvider::GetResource( | |
| 1241 int origin_pid, | |
| 1242 int render_process_host_id, | |
| 1243 int routing_id) { | |
| 1244 // TODO(johnnyg): provide resources by pid if necessary. | |
| 1245 return NULL; | |
| 1246 } | |
| 1247 | |
| 1248 void TaskManagerNotificationResourceProvider::StartUpdating() { | |
| 1249 DCHECK(!updating_); | |
| 1250 updating_ = true; | |
| 1251 | |
| 1252 // Add all the existing BalloonHosts. | |
| 1253 BalloonCollection* collection = | |
| 1254 g_browser_process->notification_ui_manager()->balloon_collection(); | |
| 1255 const BalloonCollection::Balloons& balloons = collection->GetActiveBalloons(); | |
| 1256 for (BalloonCollection::Balloons::const_iterator it = balloons.begin(); | |
| 1257 it != balloons.end(); ++it) { | |
| 1258 AddToTaskManager((*it)->view()->GetHost()); | |
| 1259 } | |
| 1260 | |
| 1261 // Register for notifications about extension process changes. | |
| 1262 registrar_.Add(this, NotificationType::NOTIFY_BALLOON_CONNECTED, | |
| 1263 NotificationService::AllSources()); | |
| 1264 registrar_.Add(this, NotificationType::NOTIFY_BALLOON_DISCONNECTED, | |
| 1265 NotificationService::AllSources()); | |
| 1266 } | |
| 1267 | |
| 1268 void TaskManagerNotificationResourceProvider::StopUpdating() { | |
| 1269 DCHECK(updating_); | |
| 1270 updating_ = false; | |
| 1271 | |
| 1272 // Unregister for notifications about extension process changes. | |
| 1273 registrar_.Remove(this, NotificationType::NOTIFY_BALLOON_CONNECTED, | |
| 1274 NotificationService::AllSources()); | |
| 1275 registrar_.Remove(this, NotificationType::NOTIFY_BALLOON_DISCONNECTED, | |
| 1276 NotificationService::AllSources()); | |
| 1277 | |
| 1278 // Delete all the resources. | |
| 1279 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); | |
| 1280 resources_.clear(); | |
| 1281 } | |
| 1282 | |
| 1283 void TaskManagerNotificationResourceProvider::Observe( | |
| 1284 NotificationType type, | |
| 1285 const NotificationSource& source, | |
| 1286 const NotificationDetails& details) { | |
| 1287 switch (type.value) { | |
| 1288 case NotificationType::NOTIFY_BALLOON_CONNECTED: | |
| 1289 AddToTaskManager(Source<BalloonHost>(source).ptr()); | |
| 1290 break; | |
| 1291 case NotificationType::NOTIFY_BALLOON_DISCONNECTED: | |
| 1292 RemoveFromTaskManager(Source<BalloonHost>(source).ptr()); | |
| 1293 break; | |
| 1294 default: | |
| 1295 NOTREACHED() << "Unexpected notification."; | |
| 1296 return; | |
| 1297 } | |
| 1298 } | |
| 1299 | |
| 1300 void TaskManagerNotificationResourceProvider::AddToTaskManager( | |
| 1301 BalloonHost* balloon_host) { | |
| 1302 TaskManagerNotificationResource* resource = | |
| 1303 new TaskManagerNotificationResource(balloon_host); | |
| 1304 DCHECK(resources_.find(balloon_host) == resources_.end()); | |
| 1305 resources_[balloon_host] = resource; | |
| 1306 task_manager_->AddResource(resource); | |
| 1307 } | |
| 1308 | |
| 1309 void TaskManagerNotificationResourceProvider::RemoveFromTaskManager( | |
| 1310 BalloonHost* balloon_host) { | |
| 1311 if (!updating_) | |
| 1312 return; | |
| 1313 std::map<BalloonHost*, TaskManagerNotificationResource*>::iterator iter = | |
| 1314 resources_.find(balloon_host); | |
| 1315 if (iter == resources_.end()) | |
| 1316 return; | |
| 1317 | |
| 1318 // Remove the resource from the Task Manager. | |
| 1319 TaskManagerNotificationResource* resource = iter->second; | |
| 1320 task_manager_->RemoveResource(resource); | |
| 1321 | |
| 1322 // Remove it from the map. | |
| 1323 resources_.erase(iter); | |
| 1324 | |
| 1325 // Finally, delete the resource. | |
| 1326 delete resource; | |
| 1327 } | |
| 1328 | |
| 1329 //////////////////////////////////////////////////////////////////////////////// | |
| 1330 // TaskManagerBrowserProcessResource class | 1182 // TaskManagerBrowserProcessResource class |
| 1331 //////////////////////////////////////////////////////////////////////////////// | 1183 //////////////////////////////////////////////////////////////////////////////// |
| 1332 | 1184 |
| 1333 SkBitmap* TaskManagerBrowserProcessResource::default_icon_ = NULL; | 1185 SkBitmap* TaskManagerBrowserProcessResource::default_icon_ = NULL; |
| 1334 | 1186 |
| 1335 TaskManagerBrowserProcessResource::TaskManagerBrowserProcessResource() | 1187 TaskManagerBrowserProcessResource::TaskManagerBrowserProcessResource() |
| 1336 : title_() { | 1188 : title_() { |
| 1337 int pid = base::GetCurrentProcId(); | 1189 int pid = base::GetCurrentProcId(); |
| 1338 bool success = base::OpenPrivilegedProcessHandle(pid, &process_); | 1190 bool success = base::OpenPrivilegedProcessHandle(pid, &process_); |
| 1339 DCHECK(success); | 1191 DCHECK(success); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1454 | 1306 |
| 1455 return &resource_; | 1307 return &resource_; |
| 1456 } | 1308 } |
| 1457 | 1309 |
| 1458 void TaskManagerBrowserProcessResourceProvider::StartUpdating() { | 1310 void TaskManagerBrowserProcessResourceProvider::StartUpdating() { |
| 1459 task_manager_->AddResource(&resource_); | 1311 task_manager_->AddResource(&resource_); |
| 1460 } | 1312 } |
| 1461 | 1313 |
| 1462 void TaskManagerBrowserProcessResourceProvider::StopUpdating() { | 1314 void TaskManagerBrowserProcessResourceProvider::StopUpdating() { |
| 1463 } | 1315 } |
| OLD | NEW |