3 Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
8 <link rel="import" href="/base/guid.html">
14 * @fileoverview Provides the Event class.
16 tv.exportTo('tracing.trace_model', function() {
19 * The SelectionState enum defines how Events are displayed in the view.
21 var SelectionState = {
28 // Cached values for getCategoryParts.
29 var categoryPartsFor = {};
32 * Categories are stored in comma-separated form, e.g: 'a,b' meaning
33 * that the event is part of the a and b category.
35 * This function returns the category split by string, caching the
36 * array for performance.
38 * Do not mutate the returned array!!!!
40 function getCategoryParts(category) {
41 var parts = categoryPartsFor[category];
42 if (parts !== undefined)
44 parts = category.split(',');
45 categoryPartsFor[category] = parts;
50 * An Event is the base type for any non-container, selectable piece
51 * of data in the trace model.
56 this.guid_ = tv.GUID.allocate();
57 this.selectionState = SelectionState.NONE;
66 return this.selectionState === SelectionState.SELECTED;
70 Event.decorateSubtype = function(baseConstructor) {
71 if (baseConstructor.register !== undefined ||
72 baseConstructor.unregister !== undefined ||
73 baseConstructor.getConstructor !== undefined) {
74 throw new Error('Already decorated: ' + baseConstructor);
77 baseConstructor.categoryToConstructorMap_ = {};
78 baseConstructor.nameToConstructorMap_ = {};
80 baseConstructor.register = function(name, constructor) {
81 if (baseConstructor.nameToConstructorMap_[name])
82 throw new Error('Constructor already registered for ' + name);
83 baseConstructor.nameToConstructorMap_[name] = constructor;
86 baseConstructor.unregister = function(name) {
87 delete baseConstructor.nameToConstructorMap_[name];
90 baseConstructor.registerCategory = function(category, constructor) {
91 if (baseConstructor.categoryToConstructorMap_[category])
92 throw new Error('Constructor already registered for ' + category);
93 baseConstructor.categoryToConstructorMap_[category] = constructor;
95 baseConstructor.unregisterCategory = function(category, constructor) {
96 delete baseConstructor.categoryToConstructorMap_[category];
99 baseConstructor.getConstructor = function(category, name) {
100 var categoryParts = getCategoryParts(category);
101 for (var i = 0; i < categoryParts.length; i++) {
102 var categoryPart = categoryParts[i];
103 if (baseConstructor.categoryToConstructorMap_[categoryPart])
104 return baseConstructor.categoryToConstructorMap_[categoryPart];
106 if (baseConstructor.nameToConstructorMap_[name])
107 return baseConstructor.nameToConstructorMap_[name];
108 return baseConstructor;
114 SelectionState: SelectionState