    case REQUEST_WORKERS_SUCCESS: {
      action.otherWorkers = toComponentData(action.otherWorkers);
      action.serviceWorkers = toComponentData(action.serviceWorkers, true);
      action.sharedWorkers = toComponentData(action.sharedWorkers);
      break;
    }
  }

  return next(action);
};

function getServiceWorkerStatus(worker) {
  const isActive = worker.state === Ci.nsIServiceWorkerInfo.STATE_ACTIVATED;
  const isRunning = !!worker.workerTargetFront;

  if (isActive && isRunning) {
    return SERVICE_WORKER_STATUSES.RUNNING;
  } else if (isActive) {
    return SERVICE_WORKER_STATUSES.STOPPED;
  }

  // We cannot get service worker registrations unless the registration is in
  // ACTIVE state. Unable to know the actual state ("installing", "waiting"), we
  // display a custom state "registering" for now. See Bug 1153292.
  return SERVICE_WORKER_STATUSES.REGISTERING;
}

function toComponentData(workers, isServiceWorker) {
  return workers.map(worker => {
    // Here `worker` is the worker object created by RootFront.listAllWorkers
    const type = DEBUG_TARGETS.WORKER;
    const icon = "chrome://devtools/skin/images/debugging-workers.svg";
    let { fetch } = worker;
    const { id, name, registrationFront, scope, subscription } = worker;

    let pushServiceEndpoint = null;
    let status = null;

    if (isServiceWorker) {
      fetch = fetch
        ? SERVICE_WORKER_FETCH_STATES.LISTENING
        : SERVICE_WORKER_FETCH_STATES.NOT_LISTENING;
      status = getServiceWorkerStatus(worker);
      pushServiceEndpoint = subscription ? subscription.endpoint : null;
    }

    return {
      details: {
        fetch,
        pushServiceEndpoint,
        registrationFront,
        scope,
        status,
      },
      icon,
      id,
      name,
      type,
    };
  });
}

module.exports = workerComponentDataMiddleware;
PK