summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/mre/function_handle.html
blob: 8a2a5fc1dad8eb206c6f1034fb024fd6187a0c96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html>
<!--
Copyright 2015 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.
-->
<link rel="import" href="/tracing/base/utils.html">

<script>
'use strict';

tr.exportTo('tr.mre', function() {
  const FunctionRegistry = {
    allFunctions_: [],
    allFunctionsByName_: {},
    get allFunctions() { return this.allFunctions_; },
    get allFunctionsByName() { return this.allFunctionsByName_; }
  };

  FunctionRegistry.getFunction = function(name) {
    return this.allFunctionsByName_[name];
  };

  FunctionRegistry.register = function(func) {
    if (func.name === '') {
      throw new Error('Registered functions must not be anonymous');
    }
    if (this.allFunctionsByName[func.name] !== undefined) {
      throw new Error('Function named ' + func.name + 'is already registered.');
    }
    this.allFunctionsByName[func.name] = func;
    this.allFunctions.push(func);
  };

  function ModuleToLoad(href, filename) {
    if ((href !== undefined) ? (filename !== undefined) :
      (filename === undefined)) {
      throw new Error('ModuleToLoad must specify exactly one of href or ' +
                      'filename');
    }
    this.href = href;
    this.filename = filename;
  }

  ModuleToLoad.prototype = {
    asDict() {
      if (this.href !== undefined) {
        return {'href': this.href};
      }
      return {'filename': this.filename};
    },

    toString() {
      if (this.href !== undefined) {
        return 'ModuleToLoad(href="' + this.href + '")';
      }
      return 'ModuleToLoad(filename="' + this.filename + '")';
    }
  };

  ModuleToLoad.fromDict = function(moduleDict) {
    return new ModuleToLoad(moduleDict.href, moduleDict.filename);
  };

  function FunctionHandle(modulesToLoad, functionName, opt_options) {
    if (!(modulesToLoad instanceof Array)) {
      throw new Error('modulesToLoad in FunctionHandle must be an array');
    }
    if (typeof(functionName) !== 'string') {
      throw new Error('functionName in FunctionHandle must be a string');
    }
    this.modulesToLoad = modulesToLoad;
    this.functionName = functionName;
    this.options_ = opt_options;
  }

  FunctionHandle.prototype = {
    get options() {
      return this.options_;
    },

    asDict() {
      return {
        'modules_to_load': this.modulesToLoad.map(
            function(m) {return m.asDict();}),
        'function_name': this.functionName,
        'options': this.options_
      };
    },

    asUserFriendlyString() {
      const parts = this.modulesToLoad.map(mtl => mtl.filename);
      parts.push(this.functionName);
      parts.push(JSON.stringify(this.options_));
      return parts.join(',');
    },

    hasHrefs() {
      for (const module in this.modulesToLoad) {
        if (this.modulesToLoad[module].href !== undefined) {
          return true;
        }
      }
      return false;
    },

    load() {
      if (this.hasHrefs()) {
        const err = new Error(
            'FunctionHandle named ' + this.functionName +
            ' specifies hrefs, which cannot be loaded.');
        err.name = 'FunctionLoadingError';
        throw err;
      }

      for (const module in this.modulesToLoad) {
        const filename = this.modulesToLoad[module].filename;
        try {
          HTMLImportsLoader.loadHTMLFile(filename);
        } catch (err) {
          err.name = 'FunctionLoadingError';
          throw err;
        }
      }

      const func = FunctionRegistry.getFunction(this.functionName);
      if (func === undefined) {
        const err = new Error(
            'No registered function named ' + this.functionName);
        err.name = 'FunctionNotDefinedError';
        throw err;
      }

      return func;
    },

    toString() {
      const modulesToLoadStr = this.modulesToLoad.map(function(module) {
        return module.toString();
      });
      return 'FunctionHandle(modulesToLoad=[' + modulesToLoadStr + '], ' +
          'functionName="' + this.functionName + '", options="' +
          JSON.stringify(this.options_) + '")';
    }
  };

  FunctionHandle.loadFromFilename_ = function(filename) {
    try {
      const numFunctionsBefore = FunctionRegistry.allFunctions.length;
      HTMLImportsLoader.loadHTMLFile(filename);
    } catch (err) {
      err.name = 'FunctionLoadingError';
      throw err;
    }

    // Verify a new function was registered.
    const numFunctionsNow = FunctionRegistry.allFunctions.length;
    if (numFunctionsNow !== (numFunctionsBefore + 1)) {
      const err = new Error(
          filename + ' didn\'t call FunctionRegistry.register');
      err.name = 'FunctionNotDefinedError';
      throw err;
    }

    return FunctionRegistry.allFunctions[numFunctionsNow - 1];
  };

  FunctionHandle.fromDict = function(handleDict) {
    const options = handleDict.options;
    let modulesToLoad;
    if (handleDict.modules_to_load !== undefined) {
      modulesToLoad = handleDict.modules_to_load.map(function(module) {
        return ModuleToLoad.fromDict(module);
      });
    }
    return new FunctionHandle(modulesToLoad, handleDict.function_name, options);
  };

  return {
    FunctionHandle,
    ModuleToLoad,
    FunctionRegistry,
  };
});
</script>