| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 |
| 6 // Collects CPU/memory usage information and posts to the page. |
| 7 function collectData(port) { |
| 8 function processCpuListener(processes) { |
| 9 _postData(processes, port, 'cpu'); |
| 10 } |
| 11 |
| 12 function processMemoryListener(processes) { |
| 13 _postData(processes, port, 'privateMemory'); |
| 14 } |
| 15 |
| 16 chrome.processes.onUpdated.addListener(processCpuListener); |
| 17 chrome.processes.onUpdatedWithMemory.addListener(processMemoryListener); |
| 18 port.onDisconnect.addListener(function() { |
| 19 chrome.processes.onUpdated.removeListener(processCpuListener); |
| 20 chrome.processes.onUpdated.removeListener(processMemoryListener); |
| 21 }); |
| 22 } |
| 23 |
| 24 /** |
| 25 * Posts the metric data to the page. |
| 26 * |
| 27 * @param processes list of current processes. |
| 28 * @param port the port used for the communication between the page and |
| 29 * extension. |
| 30 * @param metric_name the metric name, e.g cpu. |
| 31 */ |
| 32 function _postData(processes, port, metric_name) { |
| 33 var tabPid = port.sender.tab.id; |
| 34 if (!tabPid) { |
| 35 return; |
| 36 } |
| 37 var tabProcess = processes[tabPid]; |
| 38 if (!tabProcess) { |
| 39 return; |
| 40 } |
| 41 var message = {}; |
| 42 message[metric_name] = {'current_tab': tabProcess[metric_name]}; |
| 43 for (var pid in processes) { |
| 44 var process = processes[pid]; |
| 45 data = process[metric_name]; |
| 46 if (['browser', 'gpu', 'extension'].indexOf(process.type) > -1) { |
| 47 if (process.type == 'extension'){ |
| 48 for (var index in process.tasks) { |
| 49 var task = process.tasks[index]; |
| 50 if (task.title && task.title.indexOf('Chrome Media Router') > -1) { |
| 51 message[metric_name]['mr_' + process.type] = data; |
| 52 } |
| 53 } |
| 54 } else { |
| 55 message[metric_name][process.type] = data; |
| 56 } |
| 57 } |
| 58 } |
| 59 port.postMessage(message); |
| 60 } |
| 61 |
| 62 chrome.runtime.onConnectExternal.addListener(function(port) { |
| 63 if (port.name == 'collectData') { |
| 64 collectData(port); |
| 65 } else { |
| 66 console.warn('Unknown port, disconnect the port.'); |
| 67 port.disconnect(); |
| 68 } |
| 69 }); |
| OLD | NEW |