{"size": 3204, "ext": "js", "max_stars_repo_path": "lib/third_party/tracing/extras/chrome/chrome_model_helper.js", "max_stars_repo_name": "tkadlec/node-big-rig", "max_stars_repo_stars_event_min_datetime": "2015-11-05T22:37:17.000Z", "max_stars_repo_stars_event_max_datetime": "2017-09-15T19:43:39.000Z", "max_issues_repo_path": "lib/third_party/tracing/extras/chrome/chrome_model_helper.js", "max_issues_repo_name": "tkadlec/node-big-rig", "max_issues_repo_issues_event_min_datetime": "2015-11-09T10:56:52.000Z", "max_issues_repo_issues_event_max_datetime": "2016-10-25T17:16:07.000Z", "max_forks_repo_path": "lib/third_party/tracing/extras/chrome/chrome_model_helper.js", "max_forks_repo_name": "tkadlec/node-big-rig", "max_forks_repo_forks_event_min_datetime": "2015-11-08T07:07:24.000Z", "max_forks_repo_forks_event_max_datetime": "2017-06-05T17:36:33.000Z"}
{"max_stars_count": 441, "max_issues_count": 16, "max_forks_count": 21, "avg_line_length": 25.03125, "max_line_length": 77, "alphanum_fraction": 0.6641697878}
/**
Copyright (c) 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
**/

require("../../base/base.js");
require("../../base/iteration_helpers.js");
require("./chrome_browser_helper.js");
require("./chrome_gpu_helper.js");
require("./chrome_renderer_helper.js");

'use strict';

/**
 * @fileoverview Utilities for accessing trace data about the Chrome browser.
 */
global.tr.exportTo('tr.e.audits', function() {
  function findChromeBrowserProcess(model) {
    var browserProcesses = [];
    model.getAllProcesses().forEach(function(process) {
      if (!tr.e.audits.ChromeBrowserHelper.isBrowserProcess(process))
        return;
      browserProcesses.push(process);
    }, this);
    if (browserProcesses.length === 0)
      return undefined;
    if (browserProcesses.length > 1)
      return undefined;
    return browserProcesses[0];
  }

  function findChromeRenderProcesses(model) {
    var rendererProcesses = [];
    model.getAllProcesses().forEach(function(process) {
      if (!tr.e.audits.ChromeRendererHelper.isRenderProcess(process))
        return;
      rendererProcesses.push(process);
    });
    return rendererProcesses;
  }

  function findChromeGpuProcess(model) {
    var gpuProcesses = model.getAllProcesses().filter(
      tr.e.audits.ChromeGpuHelper.isGpuProcess);
    if (gpuProcesses.length != 1)
      return undefined;
    return gpuProcesses[0];
  }

  /**
   * @constructor
   */
  function ChromeModelHelper(model) {
    this.model_ = model;

    // Find browserHelper.
    this.browserProcess_ = findChromeBrowserProcess(model);
    if (this.browserProcess_) {
      this.browserHelper_ = new tr.e.audits.ChromeBrowserHelper(
          this, this.browserProcess_);
    } else {
      this.browserHelper_ = undefined;
    }

    // Find gpuHelper.
    var gpuProcess = findChromeGpuProcess(model);
    if (gpuProcess) {
      this.gpuHelper_ = new tr.e.audits.ChromeGpuHelper(
          this, gpuProcess);
    } else {
      this.gpuHelper_ = undefined;
    }

    // Find rendererHelpers.
    var rendererProcesses_ = findChromeRenderProcesses(model);

    this.rendererHelpers_ = {};
    rendererProcesses_.forEach(function(renderProcess) {
      var rendererHelper = new tr.e.audits.ChromeRendererHelper(
        this, renderProcess);
      this.rendererHelpers_[rendererHelper.pid] = rendererHelper;
    }, this);
  }

  ChromeModelHelper.supportsModel = function(model) {
    if (findChromeBrowserProcess(model) !== undefined)
      return true;
    if (findChromeRenderProcesses(model).length)
      return true;
    return false;
  }

  ChromeModelHelper.prototype = {
    get pid() {
      throw new Error('woah');
    },

    get process() {
      throw new Error('woah');
    },

    get model() {
      return this.model_;
    },

    get browserProcess() {
      return this.browserProcess_;
    },

    get browserHelper() {
      return this.browserHelper_;
    },

    get gpuHelper() {
      return this.gpuHelper_;
    },

    get rendererHelpers() {
      return this.rendererHelpers_;
    }
  };

  return {
    ChromeModelHelper: ChromeModelHelper
  };
});
