function onProcessCpu(port) {
  var tabPid = port.sender.guestProcessId || undefined;
  function processListener(processes) {
    if (tabPid == undefined) {
      // getProcessIdForTab sometimes fails, and does not call the callback.
      // (Tracked at https://crbug.com/368855.)
      // This call retries it on each process update until it succeeds.
      chrome.processes.getProcessIdForTab(port.sender.tab.id, function(x) {
        tabPid = x;
      });
      return;
    }
    var tabProcess = processes[tabPid];
    if (!tabProcess) {
      return;
    }

    var browserProcessCpu, gpuProcessCpu;
    for (var pid in processes) {
      var process = processes[pid];
      if (process.type == 'browser') {
        browserProcessCpu = process.cpu;
      } else if (process.type == 'gpu') {
        gpuProcessCpu = process.cpu;
      }
      if (!!browserProcessCpu && !!gpuProcessCpu)
        break;
    }

    port.postMessage({
      'browserCpuUsage': browserProcessCpu || 0,
      'gpuCpuUsage': gpuProcessCpu || 0,
      'tabCpuUsage': tabProcess.cpu,
      'tabNetworkUsage': tabProcess.network,
      'tabPrivateMemory': tabProcess.privateMemory,
      'tabJsMemoryAllocated': tabProcess.jsMemoryAllocated,
      'tabJsMemoryUsed': tabProcess.jsMemoryUsed
    });
  }

  chrome.processes.onUpdated.addListener(processListener);
  port.onDisconnect.addListener(function() {
    chrome.processes.onUpdated.removeListener(processListener);
  });
}
// A port for continuously reporting relevant CPU usage information to the page.
A port for continuously reporting relevant CPU usage information to the page.
A port for continuously reporting relevant CPU usage information to the page .
function onProcessCpu(port) {
  var tabPid = port.sender.guestProcessId || undefined;
  function processListener(processes) {
    if (tabPid == undefined) {
      chrome.processes.getProcessIdForTab(port.sender.tab.id, function(x) {
        tabPid = x;
      });
      return;
    }
    var tabProcess = processes[tabPid];
    if (!tabProcess) {
      return;
    }
    var browserProcessCpu, gpuProcessCpu;
    for (var pid in processes) {
      var process = processes[pid];
      if (process.type == 'browser') {
        browserProcessCpu = process.cpu;
      } else if (process.type == 'gpu') {
        gpuProcessCpu = process.cpu;
      }
      if (!!browserProcessCpu && !!gpuProcessCpu)
        break;
    }
    port.postMessage({
      'browserCpuUsage': browserProcessCpu || 0,
      'gpuCpuUsage': gpuProcessCpu || 0,
      'tabCpuUsage': tabProcess.cpu,
      'tabNetworkUsage': tabProcess.network,
      'tabPrivateMemory': tabProcess.privateMemory,
      'tabJsMemoryAllocated': tabProcess.jsMemoryAllocated,
      'tabJsMemoryUsed': tabProcess.jsMemoryUsed
    });
  }
  chrome.processes.onUpdated.addListener(processListener);
  port.onDisconnect.addListener(function() {
    chrome.processes.onUpdated.removeListener(processListener);
  });
}
function onProcessCpu ( port ) { var tabPid = port . sender . guestProcessId || undefined ; function processListener ( processes ) { if ( tabPid == undefined ) { chrome . processes . getProcessIdForTab ( port . sender . tab . id , function ( x ) { tabPid = x ; } ) ; return ; } var tabProcess = processes [ tabPid ] ; if ( ! tabProcess ) { return ; } var browserProcessCpu , gpuProcessCpu ; for ( var pid in processes ) { var process = processes [ pid ] ; if ( process . type == 'browser' ) { browserProcessCpu = process . cpu ; } else if ( process . type == 'gpu' ) { gpuProcessCpu = process . cpu ; } if ( ! ! browserProcessCpu && ! ! gpuProcessCpu ) break ; } port . postMessage ( { 'browserCpuUsage' : browserProcessCpu || 0 , 'gpuCpuUsage' : gpuProcessCpu || 0 , 'tabCpuUsage' : tabProcess . cpu , 'tabNetworkUsage' : tabProcess . network , 'tabPrivateMemory' : tabProcess . privateMemory , 'tabJsMemoryAllocated' : tabProcess . jsMemoryAllocated , 'tabJsMemoryUsed' : tabProcess . jsMemoryUsed } ) ; } chrome . processes . onUpdated . addListener ( processListener ) ; port . onDisconnect . addListener ( function ( ) { chrome . processes . onUpdated . removeListener ( processListener ) ; } ) ; }
A port for continuously reporting relevant CPU usage information to the page.
A port for continuously reporting relevant CPU usage information to the page .
// getProcessIdForTab sometimes fails, and does not call the callback. // (Tracked at https://crbug.com/368855.) // This call retries it on each process update until it succeeds.