• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<!--
3Copyright (c) 2015 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<link rel="import" href="/tracing/base/guid.html">
8<link rel="import" href="/tracing/base/utils.html">
9<script>
10'use strict';
11
12tr.exportTo('tr.v', function() {
13  function Value(canonicalUrl, name, opt_options, opt_groupingKeys,
14                 opt_diagnostics) {
15    if (typeof(name) !== 'string')
16      throw new Error('Expected value_name grouping key to be provided');
17
18    this.groupingKeys = opt_groupingKeys || {};
19    this.groupingKeys.name = name;
20
21    this.diagnostics = opt_diagnostics || {};
22
23    // May be undefined
24    this.diagnostics.canonical_url = canonicalUrl;
25
26    var options = opt_options || {};
27    this.description = options.description;
28    this.important = options.important !== undefined ?
29        options.important : false;
30  }
31
32  Value.fromDict = function(d) {
33    if (d.type === 'numeric')
34      return NumericValue.fromDict(d);
35
36    if (d.type === 'dict')
37      return DictValue.fromDict(d);
38
39    if (d.type == 'failure')
40      return FailureValue.fromDict(d);
41
42    if (d.type === 'skip')
43      return SkipValue.fromDict(d);
44
45    throw new Error('Not implemented');
46  };
47
48  Value.prototype = {
49    get name() {
50      return this.groupingKeys.name;
51    },
52
53    get canonicalUrl() {
54      return this.diagnostics.canonical_url;
55    },
56
57    addGroupingKey: function(keyName, key) {
58      if (this.groupingKeys.hasOwnProperty(keyName))
59          throw new Error('Tried to redefine grouping key ' + keyName);
60      this.groupingKeys[keyName] = key;
61    },
62
63    asDict: function() {
64      return this.asJSON();
65    },
66
67    asJSON: function() {
68      var d = {
69        grouping_keys: this.groupingKeys,
70        description: this.description,
71        important: this.important,
72        diagnostics: this.diagnostics
73      };
74
75      this._asDictInto(d);
76      if (d.type === undefined)
77        throw new Error('_asDictInto must set type field');
78      return d;
79    },
80
81    _asDictInto: function(d) {
82      throw new Error('Not implemented');
83    }
84  };
85
86  function NumericValue(canonicalUrl, name, numeric, opt_options,
87                        opt_groupingKeys, opt_diagnostics) {
88    if (!(numeric instanceof tr.v.NumericBase))
89      throw new Error('Expected numeric to be instance of tr.v.NumericBase');
90
91    Value.call(this, canonicalUrl, name, opt_options, opt_groupingKeys,
92               opt_diagnostics);
93    this.numeric = numeric;
94  }
95
96  NumericValue.fromDict = function(d) {
97    if (d.numeric === undefined)
98      throw new Error('Expected numeric to be provided');
99    var numeric = tr.v.NumericBase.fromDict(d.numeric);
100    return new NumericValue(d.diagnostics.canonical_url, d.grouping_keys.name,
101                            numeric, d, d.grouping_keys, d.diagnostics);
102  };
103
104  NumericValue.prototype = {
105    __proto__: Value.prototype,
106
107    _asDictInto: function(d) {
108      d.type = 'numeric';
109      d.numeric = this.numeric.asDict();
110    }
111  };
112
113  function DictValue(canonicalUrl, name, value, opt_options, opt_groupingKeys,
114                     opt_diagnostics) {
115    Value.call(this, canonicalUrl, name, opt_options, opt_groupingKeys,
116               opt_diagnostics);
117    this.value = value;
118  }
119
120  DictValue.fromDict = function(d) {
121    if (d.units !== undefined)
122      throw new Error('Expected units to be undefined');
123    if (d.value === undefined)
124      throw new Error('Expected value to be provided');
125    return new DictValue(d.diagnostics.canonical_url, d.grouping_keys.name,
126                         d.value, d, d.groupingKeys, d.diagnostics);
127  };
128
129  DictValue.prototype = {
130    __proto__: Value.prototype,
131
132    _asDictInto: function(d) {
133      d.type = 'dict';
134      d.value = this.value;
135    }
136  };
137
138
139  function FailureValue(canonicalUrl, name, opt_options, opt_groupingKeys,
140                        opt_diagnostics) {
141    var options = opt_options || {};
142
143    var stack;
144    if (options.stack === undefined) {
145      if (options.stack_str === undefined) {
146        throw new Error('Expected stack_str or stack to be provided');
147      } else {
148        stack = options.stack_str;
149      }
150    } else {
151      stack = options.stack;
152    }
153
154    if (typeof stack !== 'string')
155      throw new Error('stack must be provided as a string');
156
157    if (canonicalUrl === undefined) {
158      throw new Error('FailureValue must provide canonicalUrl');
159    }
160
161    Value.call(this, canonicalUrl, name, options, opt_groupingKeys,
162               opt_diagnostics);
163    this.stack = stack;
164  }
165
166  FailureValue.fromError = function(canonicalUrl, e) {
167    var ex = tr.b.normalizeException(e);
168    return new FailureValue(canonicalUrl, ex.typeName,
169                            {description: ex.message,
170                             stack: ex.stack});
171
172  };
173
174  FailureValue.fromDict = function(d) {
175    if (d.units !== undefined)
176      throw new Error('Expected units to be undefined');
177    if (d.stack_str === undefined)
178      throw new Error('Expected stack_str to be provided');
179    return new FailureValue(d.diagnostics.canonical_url, d.grouping_keys.name,
180                            d, d.grouping_keys, d.diagnostics);
181  };
182
183  FailureValue.prototype = {
184    __proto__: Value.prototype,
185
186    _asDictInto: function(d) {
187      d.type = 'failure';
188      d.stack_str = this.stack;
189    }
190  };
191
192
193  function SkipValue(canonicalUrl, name, opt_options, opt_groupingKeys,
194                     opt_diagnostics) {
195    Value.call(this, canonicalUrl, name, opt_options, opt_groupingKeys,
196               opt_diagnostics);
197  }
198
199  SkipValue.fromDict = function(d) {
200    if (d.units !== undefined)
201      throw new Error('Expected units to be undefined');
202    return new SkipValue(d.diagnostics.canonical_url, d.grouping_keys.name,
203                         d, d.grouping_keys, d.diagnostics);
204  };
205
206  SkipValue.prototype = {
207    __proto__: Value.prototype,
208
209    _asDictInto: function(d) {
210      d.type = 'skip';
211    }
212  };
213
214
215  return {
216    Value: Value,
217    NumericValue: NumericValue,
218    DictValue: DictValue,
219    FailureValue: FailureValue,
220    SkipValue: SkipValue
221  };
222});
223</script>
224