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(parent, id, category, name, creationTs, opt_baseTypeName) {
37 var constructor = tracing.trace_model.ObjectInstance.getConstructor(name);
38 var instance = new constructor(parent, id, category, name, creationTs, opt_baseTypeName);
39 var typeName = instance.typeName;
40 var instancesOfTypeName = this.instancesByTypeName_[typeName];
41 if (!instancesOfTypeName) {
42 instancesOfTypeName = [];
43 this.instancesByTypeName_[typeName] = instancesOfTypeName;
45 instancesOfTypeName.push(instance);
49 getOrCreateInstanceMap_: function(id) {
50 var instanceMap = this.instanceMapsById_[id];
53 instanceMap = new tracing.trace_model.TimeToObjectInstanceMap(
54 this.createObjectInstance_, this.parent, id);
55 this.instanceMapsById_[id] = instanceMap;
59 idWasCreated: function(id, category, name, ts) {
60 var instanceMap = this.getOrCreateInstanceMap_(id);
61 return instanceMap.idWasCreated(category, name, ts);
64 addSnapshot: function(id, category, name, ts, args, opt_baseTypeName) {
65 var instanceMap = this.getOrCreateInstanceMap_(id);
66 var snapshot = instanceMap.addSnapshot(category, name, ts, args, opt_baseTypeName);
67 if (snapshot.objectInstance.category != category) {
68 var msg = 'Added snapshot name=' + name + ' with cat=' + category +
69 ' impossible. It instance was created/snapshotted with cat=' +
70 snapshot.objectInstance.category + ' name=' +
71 snapshot.objectInstance.name;
74 if (opt_baseTypeName &&
75 snapshot.objectInstance.baseTypeName != opt_baseTypeName) {
76 throw new Error('Could not add snapshot with baseTypeName=' +
77 opt_baseTypeName + '. It ' +
78 'was previously created with name=' +
79 snapshot.objectInstance.baseTypeName);
81 if (snapshot.objectInstance.name != name) {
82 throw new Error('Could not add snapshot with name=' + name + '. It ' +
83 'was previously created with name=' +
84 snapshot.objectInstance.name);
89 idWasDeleted: function(id, category, name, ts) {
90 var instanceMap = this.getOrCreateInstanceMap_(id);
91 var deletedInstance = instanceMap.idWasDeleted(category, name, ts);
94 if (deletedInstance.category != category) {
95 var msg = 'Deleting object ' + deletedInstance.name +
96 ' with a different category ' +
97 'than when it was created. It previous had cat=' +
98 deletedInstance.category + ' but the delete command ' +
99 'had cat=' + category;
100 throw new Error(msg);
102 if (deletedInstance.baseTypeName != name) {
103 throw new Error('Deletion requested for name=' +
104 name + ' could not proceed: ' +
105 'An existing object with baseTypeName=' +
106 deletedInstance.baseTypeName + ' existed.')
110 autoDeleteObjects: function(maxTimestamp) {
111 tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
112 var lastInstance = i2imap.lastInstance;
113 if (lastInstance.deletionTs != Number.MAX_VALUE)
116 lastInstance.category, lastInstance.name, maxTimestamp);
117 // idWasDeleted will cause lastInstance.deletionTsWasExplicit to be set
118 // to true. Unset it here.
119 lastInstance.deletionTsWasExplicit = false;
123 getObjectInstanceAt: function(id, ts) {
124 var instanceMap = this.instanceMapsById_[id];
127 return instanceMap.getInstanceAt(ts);
130 getSnapshotAt: function(id, ts) {
131 var instance = this.getObjectInstanceAt(id, ts);
134 return instance.getSnapshotAt(ts);
137 iterObjectInstances: function(iter, opt_this) {
138 opt_this = opt_this || this;
139 tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
140 i2imap.instances.forEach(iter, opt_this);
144 getAllObjectInstances: function() {
146 this.iterObjectInstances(function(i) { instances.push(i); });
150 getAllInstancesNamed: function(name) {
151 return this.instancesByTypeName_[name];
154 getAllInstancesByTypeName: function() {
155 return this.instancesByTypeName_;
158 preInitializeAllObjects: function() {
159 this.iterObjectInstances(function(instance) {
160 instance.preInitialize();
164 initializeAllObjects: function() {
165 this.iterObjectInstances(function(instance) {
166 instance.initialize();
170 initializeInstances: function() {
171 this.iterObjectInstances(function(instance) {
172 instance.initialize();
176 updateBounds: function() {
178 this.iterObjectInstances(function(instance) {
179 instance.updateBounds();
180 this.bounds.addRange(instance.bounds);
184 shiftTimestampsForward: function(amount) {
185 this.iterObjectInstances(function(instance) {
186 instance.shiftTimestampsForward(amount);
190 addCategoriesToDict: function(categoriesDict) {
191 this.iterObjectInstances(function(instance) {
192 categoriesDict[instance.category] = true;
197 // TODO(nduca): Implement this if we need it.
201 iterateAllEvents: function(callback, opt_this) {
202 this.iterObjectInstances(function(instance) {
203 callback.call(this, instance);
204 instance.snapshots.forEach(callback);
210 ObjectCollection: ObjectCollection