1<!DOCTYPE html>
2<!--
3Copyright 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
8<link rel="import" href="/tracing/base/extension_registry.html">
9<link rel="import" href="/tracing/core/scripting_object.html">
10
11<script>
12'use strict';
13
14tr.exportTo('tr.c', function() {
15  function ScriptingController(brushingStateController) {
16    this.brushingStateController_ = brushingStateController;
17    this.scriptObjectNames_ = [];
18    this.scriptObjectValues_ = [];
19    this.brushingStateController.addEventListener(
20        'model-changed', this.onModelChanged_.bind(this));
21
22    // Register all scripting objects.
23    var typeInfos = ScriptingObjectRegistry.getAllRegisteredTypeInfos();
24    typeInfos.forEach(function(typeInfo) {
25      this.addScriptObject(typeInfo.metadata.name, typeInfo.constructor);
26      // Also make the object available to the DevTools inspector.
27      global[typeInfo.metadata.name] = typeInfo.constructor;
28    }, this);
29  }
30
31  function ScriptingObjectRegistry() {
32  }
33  var options = new tr.b.ExtensionRegistryOptions(tr.b.BASIC_REGISTRY_MODE);
34  tr.b.decorateExtensionRegistry(ScriptingObjectRegistry, options);
35
36  ScriptingController.prototype = {
37    get brushingStateController() {
38      return this.brushingStateController_;
39    },
40
41    onModelChanged_: function() {
42      this.scriptObjectValues_.forEach(function(v) {
43        if (v.onModelChanged)
44          v.onModelChanged(this.brushingStateController.model);
45      }, this);
46    },
47
48    addScriptObject: function(name, value) {
49      this.scriptObjectNames_.push(name);
50      this.scriptObjectValues_.push(value);
51    },
52
53    executeCommand: function(command) {
54      var f = new Function(
55          this.scriptObjectNames_, 'return eval(' + command + ')');
56      return f.apply(null, this.scriptObjectValues_);
57    }
58  };
59
60  return {
61    ScriptingController: ScriptingController,
62    ScriptingObjectRegistry: ScriptingObjectRegistry
63  };
64});
65</script>
66