| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/task_manager/task_manager_resource_providers.h" | 5 #include "chrome/browser/task_manager/task_manager_resource_providers.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 1193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1204 if (iter != pid_to_resources_.end()) | 1204 if (iter != pid_to_resources_.end()) |
| 1205 return iter->second; | 1205 return iter->second; |
| 1206 else | 1206 else |
| 1207 return NULL; | 1207 return NULL; |
| 1208 } | 1208 } |
| 1209 | 1209 |
| 1210 void TaskManagerChildProcessResourceProvider::StartUpdating() { | 1210 void TaskManagerChildProcessResourceProvider::StartUpdating() { |
| 1211 DCHECK(!updating_); | 1211 DCHECK(!updating_); |
| 1212 updating_ = true; | 1212 updating_ = true; |
| 1213 | 1213 |
| 1214 // Register for notifications to get new child processes. | |
| 1215 registrar_.Add(this, content::NOTIFICATION_CHILD_PROCESS_HOST_CONNECTED, | |
| 1216 content::NotificationService::AllBrowserContextsAndSources()); | |
| 1217 registrar_.Add(this, content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED, | |
| 1218 content::NotificationService::AllBrowserContextsAndSources()); | |
| 1219 | |
| 1220 // Get the existing child processes. | 1214 // Get the existing child processes. |
| 1221 BrowserThread::PostTask( | 1215 BrowserThread::PostTask( |
| 1222 BrowserThread::IO, FROM_HERE, | 1216 BrowserThread::IO, FROM_HERE, |
| 1223 base::Bind( | 1217 base::Bind( |
| 1224 &TaskManagerChildProcessResourceProvider::RetrieveChildProcessData, | 1218 &TaskManagerChildProcessResourceProvider::RetrieveChildProcessData, |
| 1225 this)); | 1219 this)); |
| 1220 |
| 1221 BrowserChildProcessObserver::Add(this); |
| 1226 } | 1222 } |
| 1227 | 1223 |
| 1228 void TaskManagerChildProcessResourceProvider::StopUpdating() { | 1224 void TaskManagerChildProcessResourceProvider::StopUpdating() { |
| 1229 DCHECK(updating_); | 1225 DCHECK(updating_); |
| 1230 updating_ = false; | 1226 updating_ = false; |
| 1231 | 1227 |
| 1232 // Unregister for notifications to get new plugin processes. | |
| 1233 registrar_.Remove( | |
| 1234 this, content::NOTIFICATION_CHILD_PROCESS_HOST_CONNECTED, | |
| 1235 content::NotificationService::AllBrowserContextsAndSources()); | |
| 1236 registrar_.Remove( | |
| 1237 this, | |
| 1238 content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED, | |
| 1239 content::NotificationService::AllBrowserContextsAndSources()); | |
| 1240 | |
| 1241 // Delete all the resources. | 1228 // Delete all the resources. |
| 1242 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); | 1229 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); |
| 1243 | 1230 |
| 1244 resources_.clear(); | 1231 resources_.clear(); |
| 1245 pid_to_resources_.clear(); | 1232 pid_to_resources_.clear(); |
| 1233 |
| 1234 BrowserChildProcessObserver::Remove(this); |
| 1246 } | 1235 } |
| 1247 | 1236 |
| 1248 void TaskManagerChildProcessResourceProvider::Observe( | 1237 void TaskManagerChildProcessResourceProvider::BrowserChildProcessHostConnected( |
| 1249 int type, | 1238 const content::ChildProcessData& data) { |
| 1250 const content::NotificationSource& source, | 1239 DCHECK(updating_); |
| 1251 const content::NotificationDetails& details) { | |
| 1252 content::ChildProcessData data = | |
| 1253 *content::Details<content::ChildProcessData>(details).ptr(); | |
| 1254 switch (type) { | |
| 1255 case content::NOTIFICATION_CHILD_PROCESS_HOST_CONNECTED: | |
| 1256 Add(data); | |
| 1257 break; | |
| 1258 case content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED: | |
| 1259 Remove(data); | |
| 1260 break; | |
| 1261 default: | |
| 1262 NOTREACHED() << "Unexpected notification."; | |
| 1263 return; | |
| 1264 } | |
| 1265 } | |
| 1266 | 1240 |
| 1267 void TaskManagerChildProcessResourceProvider::Add( | 1241 // Workers are handled by TaskManagerWorkerResourceProvider. |
| 1268 const content::ChildProcessData& child_process_data) { | 1242 if (data.type == content::PROCESS_TYPE_WORKER) |
| 1269 if (!updating_) | |
| 1270 return; | 1243 return; |
| 1271 // Workers are handled by TaskManagerWorkerResourceProvider. | 1244 if (resources_.count(data.handle)) { |
| 1272 if (child_process_data.type == content::PROCESS_TYPE_WORKER) | |
| 1273 return; | |
| 1274 if (resources_.count(child_process_data.handle)) { | |
| 1275 // The case may happen that we have added a child_process_info as part of | 1245 // The case may happen that we have added a child_process_info as part of |
| 1276 // the iteration performed during StartUpdating() call but the notification | 1246 // the iteration performed during StartUpdating() call but the notification |
| 1277 // that it has connected was not fired yet. So when the notification | 1247 // that it has connected was not fired yet. So when the notification |
| 1278 // happens, we already know about this plugin and just ignore it. | 1248 // happens, we already know about this plugin and just ignore it. |
| 1279 return; | 1249 return; |
| 1280 } | 1250 } |
| 1281 AddToTaskManager(child_process_data); | 1251 AddToTaskManager(data); |
| 1282 } | 1252 } |
| 1283 | 1253 |
| 1284 void TaskManagerChildProcessResourceProvider::Remove( | 1254 void TaskManagerChildProcessResourceProvider:: |
| 1285 const content::ChildProcessData& child_process_data) { | 1255 BrowserChildProcessHostDisconnected(const content::ChildProcessData& data) { |
| 1286 if (!updating_) | 1256 DCHECK(updating_); |
| 1257 |
| 1258 if (data.type == content::PROCESS_TYPE_WORKER) |
| 1287 return; | 1259 return; |
| 1288 if (child_process_data.type == content::PROCESS_TYPE_WORKER) | 1260 ChildProcessMap::iterator iter = resources_.find(data.handle); |
| 1289 return; | |
| 1290 ChildProcessMap::iterator iter = resources_.find(child_process_data.handle); | |
| 1291 if (iter == resources_.end()) { | 1261 if (iter == resources_.end()) { |
| 1292 // ChildProcessData disconnection notifications are asynchronous, so we | 1262 // ChildProcessData disconnection notifications are asynchronous, so we |
| 1293 // might be notified for a plugin we don't know anything about (if it was | 1263 // might be notified for a plugin we don't know anything about (if it was |
| 1294 // closed before the task manager was shown and destroyed after that). | 1264 // closed before the task manager was shown and destroyed after that). |
| 1295 return; | 1265 return; |
| 1296 } | 1266 } |
| 1297 // Remove the resource from the Task Manager. | 1267 // Remove the resource from the Task Manager. |
| 1298 TaskManagerChildProcessResource* resource = iter->second; | 1268 TaskManagerChildProcessResource* resource = iter->second; |
| 1299 task_manager_->RemoveResource(resource); | 1269 task_manager_->RemoveResource(resource); |
| 1300 // Remove it from the provider. | 1270 // Remove it from the provider. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1323 task_manager_->AddResource(resource); | 1293 task_manager_->AddResource(resource); |
| 1324 } | 1294 } |
| 1325 | 1295 |
| 1326 // The ChildProcessData::Iterator has to be used from the IO thread. | 1296 // The ChildProcessData::Iterator has to be used from the IO thread. |
| 1327 void TaskManagerChildProcessResourceProvider::RetrieveChildProcessData() { | 1297 void TaskManagerChildProcessResourceProvider::RetrieveChildProcessData() { |
| 1328 std::vector<content::ChildProcessData> child_processes; | 1298 std::vector<content::ChildProcessData> child_processes; |
| 1329 for (BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) { | 1299 for (BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) { |
| 1330 // Only add processes which are already started, since we need their handle. | 1300 // Only add processes which are already started, since we need their handle. |
| 1331 if (iter.GetData().handle == base::kNullProcessHandle) | 1301 if (iter.GetData().handle == base::kNullProcessHandle) |
| 1332 continue; | 1302 continue; |
| 1303 if (iter.GetData().type == content::PROCESS_TYPE_WORKER) |
| 1304 continue; |
| 1333 child_processes.push_back(iter.GetData()); | 1305 child_processes.push_back(iter.GetData()); |
| 1334 } | 1306 } |
| 1335 // Now notify the UI thread that we have retrieved information about child | 1307 // Now notify the UI thread that we have retrieved information about child |
| 1336 // processes. | 1308 // processes. |
| 1337 BrowserThread::PostTask( | 1309 BrowserThread::PostTask( |
| 1338 BrowserThread::UI, FROM_HERE, | 1310 BrowserThread::UI, FROM_HERE, |
| 1339 base::Bind( | 1311 base::Bind( |
| 1340 &TaskManagerChildProcessResourceProvider::ChildProcessDataRetreived, | 1312 &TaskManagerChildProcessResourceProvider::ChildProcessDataRetreived, |
| 1341 this, child_processes)); | 1313 this, child_processes)); |
| 1342 } | 1314 } |
| 1343 | 1315 |
| 1344 // This is called on the UI thread. | 1316 // This is called on the UI thread. |
| 1345 void TaskManagerChildProcessResourceProvider::ChildProcessDataRetreived( | 1317 void TaskManagerChildProcessResourceProvider::ChildProcessDataRetreived( |
| 1346 const std::vector<content::ChildProcessData>& child_processes) { | 1318 const std::vector<content::ChildProcessData>& child_processes) { |
| 1347 for (size_t i = 0; i < child_processes.size(); ++i) | 1319 for (size_t i = 0; i < child_processes.size(); ++i) |
| 1348 Add(child_processes[i]); | 1320 AddToTaskManager(child_processes[i]); |
| 1349 | 1321 |
| 1350 content::NotificationService::current()->Notify( | 1322 content::NotificationService::current()->Notify( |
| 1351 chrome::NOTIFICATION_TASK_MANAGER_CHILD_PROCESSES_DATA_READY, | 1323 chrome::NOTIFICATION_TASK_MANAGER_CHILD_PROCESSES_DATA_READY, |
| 1352 content::Source<TaskManagerChildProcessResourceProvider>(this), | 1324 content::Source<TaskManagerChildProcessResourceProvider>(this), |
| 1353 content::NotificationService::NoDetails()); | 1325 content::NotificationService::NoDetails()); |
| 1354 } | 1326 } |
| 1355 | 1327 |
| 1356 //////////////////////////////////////////////////////////////////////////////// | 1328 //////////////////////////////////////////////////////////////////////////////// |
| 1357 // TaskManagerExtensionProcessResource class | 1329 // TaskManagerExtensionProcessResource class |
| 1358 //////////////////////////////////////////////////////////////////////////////// | 1330 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1926 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED: | 1898 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED: |
| 1927 Add(web_contents->GetRenderViewHost()); | 1899 Add(web_contents->GetRenderViewHost()); |
| 1928 break; | 1900 break; |
| 1929 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED: | 1901 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED: |
| 1930 Remove(web_contents->GetRenderViewHost()); | 1902 Remove(web_contents->GetRenderViewHost()); |
| 1931 break; | 1903 break; |
| 1932 default: | 1904 default: |
| 1933 NOTREACHED() << "Unexpected notification."; | 1905 NOTREACHED() << "Unexpected notification."; |
| 1934 } | 1906 } |
| 1935 } | 1907 } |
| OLD | NEW |