1<!DOCTYPE html>
2<!--
3Copyright (c) 2014 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/tracing/base/guid.html">
9<link rel="import" href="/tracing/base/iteration_helpers.html">
10<link rel="import" href="/tracing/model/helpers/chrome_browser_helper.html">
11<link rel="import" href="/tracing/model/helpers/chrome_gpu_helper.html">
12<link rel="import" href="/tracing/model/helpers/chrome_renderer_helper.html">
13
14<script>
15'use strict';
16
17/**
18 * @fileoverview Utilities for accessing trace data about the Chrome browser.
19 */
20tr.exportTo('tr.model.helpers', function() {
21  function findChromeBrowserProcess(model) {
22    var browserProcesses = [];
23    model.getAllProcesses().forEach(function(process) {
24      if (!tr.model.helpers.ChromeBrowserHelper.isBrowserProcess(process))
25        return;
26      browserProcesses.push(process);
27    }, this);
28    if (browserProcesses.length === 0)
29      return undefined;
30    if (browserProcesses.length > 1)
31      return undefined;
32    return browserProcesses[0];
33  }
34
35  function findChromeRenderProcesses(model) {
36    var rendererProcesses = [];
37    model.getAllProcesses().forEach(function(process) {
38      if (!tr.model.helpers.ChromeRendererHelper.isRenderProcess(process))
39        return;
40      rendererProcesses.push(process);
41    });
42    return rendererProcesses;
43  }
44
45  function findChromeGpuProcess(model) {
46    var gpuProcesses = model.getAllProcesses().filter(
47      tr.model.helpers.ChromeGpuHelper.isGpuProcess);
48    if (gpuProcesses.length != 1)
49      return undefined;
50    return gpuProcesses[0];
51  }
52
53  /**
54   * @constructor
55   */
56  function ChromeModelHelper(model) {
57    this.model_ = model;
58
59    // Find browserHelper.
60    this.browserProcess_ = findChromeBrowserProcess(model);
61    if (this.browserProcess_) {
62      this.browserHelper_ = new tr.model.helpers.ChromeBrowserHelper(
63          this, this.browserProcess_);
64    } else {
65      this.browserHelper_ = undefined;
66    }
67
68    // Find gpuHelper.
69    var gpuProcess = findChromeGpuProcess(model);
70    if (gpuProcess) {
71      this.gpuHelper_ = new tr.model.helpers.ChromeGpuHelper(
72          this, gpuProcess);
73    } else {
74      this.gpuHelper_ = undefined;
75    }
76
77    // Find rendererHelpers.
78    var rendererProcesses_ = findChromeRenderProcesses(model);
79
80    this.rendererHelpers_ = {};
81    rendererProcesses_.forEach(function(renderProcess) {
82      var rendererHelper = new tr.model.helpers.ChromeRendererHelper(
83        this, renderProcess);
84      this.rendererHelpers_[rendererHelper.pid] = rendererHelper;
85    }, this);
86  }
87
88  ChromeModelHelper.guid = tr.b.GUID.allocate();
89
90  ChromeModelHelper.supportsModel = function(model) {
91    if (findChromeBrowserProcess(model) !== undefined)
92      return true;
93    if (findChromeRenderProcesses(model).length)
94      return true;
95    return false;
96  };
97
98  ChromeModelHelper.prototype = {
99    get pid() {
100      throw new Error('woah');
101    },
102
103    get process() {
104      throw new Error('woah');
105    },
106
107    get model() {
108      return this.model_;
109    },
110
111    get browserProcess() {
112      return this.browserProcess_;
113    },
114
115    get browserHelper() {
116      return this.browserHelper_;
117    },
118
119    get gpuHelper() {
120      return this.gpuHelper_;
121    },
122
123    get rendererHelpers() {
124      return this.rendererHelpers_;
125    }
126  };
127
128  return {
129    ChromeModelHelper: ChromeModelHelper
130  };
131});
132</script>
133