1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 * @fileoverview Provides the ObjectCollection class.
10 tvcm.require('tvcm.utils');
11 tvcm.require('tvcm.range');
12 tvcm.require('tvcm.sorted_array_utils');
13 tvcm.require('tracing.trace_model.object_instance');
14 tvcm.require('tracing.trace_model.time_to_object_instance_map');
16 tvcm.exportTo('tracing.trace_model', function() {
17 var ObjectInstance = tracing.trace_model.ObjectInstance;
20 * A collection of object instances and their snapshots, accessible by id and
21 * time, or by object name.
25 function ObjectCollection(parent) {
27 this.bounds = new tvcm.Range();
28 this.instanceMapsById_ = {}; // id -> TimeToObjectInstanceMap
29 this.instancesByTypeName_ = {};
30 this.createObjectInstance_ = this.createObjectInstance_.bind(this);
33 ObjectCollection.prototype = {
34 __proto__: Object.prototype,
36 createObjectInstance_: function(
37 parent, id, category, name, creationTs, opt_baseTypeName) {
38 var constructor = tracing.trace_model.ObjectInstance.getConstructor(name);
39 var instance = new constructor(
40 parent, id, category, name, creationTs, opt_baseTypeName);
41 var typeName = instance.typeName;
42 var instancesOfTypeName = this.instancesByTypeName_[typeName];
43 if (!instancesOfTypeName) {
44 instancesOfTypeName = [];
45 this.instancesByTypeName_[typeName] = instancesOfTypeName;
47 instancesOfTypeName.push(instance);
51 getOrCreateInstanceMap_: function(id) {
52 var instanceMap = this.instanceMapsById_[id];
55 instanceMap = new tracing.trace_model.TimeToObjectInstanceMap(
56 this.createObjectInstance_, this.parent, id);
57 this.instanceMapsById_[id] = instanceMap;
61 idWasCreated: function(id, category, name, ts) {
62 var instanceMap = this.getOrCreateInstanceMap_(id);
63 return instanceMap.idWasCreated(category, name, ts);
66 addSnapshot: function(id, category, name, ts, args, opt_baseTypeName) {
67 var instanceMap = this.getOrCreateInstanceMap_(id);
68 var snapshot = instanceMap.addSnapshot(
69 category, name, ts, args, opt_baseTypeName);
70 if (snapshot.objectInstance.category != category) {
71 var msg = 'Added snapshot name=' + name + ' with cat=' + category +
72 ' impossible. It instance was created/snapshotted with cat=' +
73 snapshot.objectInstance.category + ' name=' +
74 snapshot.objectInstance.name;
77 if (opt_baseTypeName &&
78 snapshot.objectInstance.baseTypeName != opt_baseTypeName) {
79 throw new Error('Could not add snapshot with baseTypeName=' +
80 opt_baseTypeName + '. It ' +
81 'was previously created with name=' +
82 snapshot.objectInstance.baseTypeName);
84 if (snapshot.objectInstance.name != name) {
85 throw new Error('Could not add snapshot with name=' + name + '. It ' +
86 'was previously created with name=' +
87 snapshot.objectInstance.name);
92 idWasDeleted: function(id, category, name, ts) {
93 var instanceMap = this.getOrCreateInstanceMap_(id);
94 var deletedInstance = instanceMap.idWasDeleted(category, name, ts);
97 if (deletedInstance.category != category) {
98 var msg = 'Deleting object ' + deletedInstance.name +
99 ' with a different category ' +
100 'than when it was created. It previous had cat=' +
101 deletedInstance.category + ' but the delete command ' +
102 'had cat=' + category;
103 throw new Error(msg);
105 if (deletedInstance.baseTypeName != name) {
106 throw new Error('Deletion requested for name=' +
107 name + ' could not proceed: ' +
108 'An existing object with baseTypeName=' +
109 deletedInstance.baseTypeName + ' existed.');
113 autoDeleteObjects: function(maxTimestamp) {
114 tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
115 var lastInstance = i2imap.lastInstance;
116 if (lastInstance.deletionTs != Number.MAX_VALUE)
119 lastInstance.category, lastInstance.name, maxTimestamp);
120 // idWasDeleted will cause lastInstance.deletionTsWasExplicit to be set
121 // to true. Unset it here.
122 lastInstance.deletionTsWasExplicit = false;
126 getObjectInstanceAt: function(id, ts) {
127 var instanceMap = this.instanceMapsById_[id];
130 return instanceMap.getInstanceAt(ts);
133 getSnapshotAt: function(id, ts) {
134 var instance = this.getObjectInstanceAt(id, ts);
137 return instance.getSnapshotAt(ts);
140 iterObjectInstances: function(iter, opt_this) {
141 opt_this = opt_this || this;
142 tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
143 i2imap.instances.forEach(iter, opt_this);
147 getAllObjectInstances: function() {
149 this.iterObjectInstances(function(i) { instances.push(i); });
153 getAllInstancesNamed: function(name) {
154 return this.instancesByTypeName_[name];
157 getAllInstancesByTypeName: function() {
158 return this.instancesByTypeName_;
161 preInitializeAllObjects: function() {
162 this.iterObjectInstances(function(instance) {
163 instance.preInitialize();
167 initializeAllObjects: function() {
168 this.iterObjectInstances(function(instance) {
169 instance.initialize();
173 initializeInstances: function() {
174 this.iterObjectInstances(function(instance) {
175 instance.initialize();
179 updateBounds: function() {
181 this.iterObjectInstances(function(instance) {
182 instance.updateBounds();
183 this.bounds.addRange(instance.bounds);
187 shiftTimestampsForward: function(amount) {
188 this.iterObjectInstances(function(instance) {
189 instance.shiftTimestampsForward(amount);
193 addCategoriesToDict: function(categoriesDict) {
194 this.iterObjectInstances(function(instance) {
195 categoriesDict[instance.category] = true;
200 // TODO(nduca): Implement this if we need it.
204 iterateAllEvents: function(callback, opt_this) {
205 this.iterObjectInstances(function(instance) {
206 callback.call(this, instance);
207 instance.snapshots.forEach(callback);
213 ObjectCollection: ObjectCollection