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) {
37 var constructor = tracing.trace_model.ObjectInstance.getConstructor(name);
38 var instance = new constructor(parent, id, category, name, creationTs);
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) {
65 var instanceMap = this.getOrCreateInstanceMap_(id, category, name, ts);
66 var snapshot = instanceMap.addSnapshot(category, name, ts, args);
67 if (snapshot.objectInstance.category != category) {
68 throw new Error('Added snapshot with cat=' + category + ' impossible.' +
69 'It instance was created/snapshotted with cat=' +
70 snapshot.objectInstance.category);
72 if (snapshot.objectInstance.name != name) {
73 throw new Error('Added snapshot with different name than ' +
74 'when it was created');
79 idWasDeleted: function(id, category, name, ts) {
80 var instanceMap = this.getOrCreateInstanceMap_(id, category, name, ts);
81 var deletedInstance = instanceMap.idWasDeleted(category, name, ts);
84 if (deletedInstance.category != category) {
85 throw new Error('Deleting object ' + deletedInstance.name +
86 ' with a different category ' +
87 'than when it was created. It previous had cat=' +
88 deletedInstance.category + ' but the delete command ' +
89 'had cat=' + category);
91 if (deletedInstance.name != name) {
92 throw new Error('Deleting an object with a different name than ' +
93 'when it was created');
97 autoDeleteObjects: function(maxTimestamp) {
98 tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
99 var lastInstance = i2imap.lastInstance;
100 if (lastInstance.deletionTs != Number.MAX_VALUE)
103 lastInstance.category, lastInstance.name, maxTimestamp);
104 // idWasDeleted will cause lastInstance.deletionTsWasExplicit to be set
105 // to true. Unset it here.
106 lastInstance.deletionTsWasExplicit = false;
110 getObjectInstanceAt: function(id, ts) {
111 var instanceMap = this.instanceMapsById_[id];
114 return instanceMap.getInstanceAt(ts);
117 getSnapshotAt: function(id, ts) {
118 var instance = this.getObjectInstanceAt(id, ts);
121 return instance.getSnapshotAt(ts);
124 iterObjectInstances: function(iter, opt_this) {
125 opt_this = opt_this || this;
126 tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
127 i2imap.instances.forEach(iter, opt_this);
131 getAllObjectInstances: function() {
133 this.iterObjectInstances(function(i) { instances.push(i); });
137 getAllInstancesNamed: function(name) {
138 return this.instancesByTypeName_[name];
141 getAllInstancesByTypeName: function() {
142 return this.instancesByTypeName_;
145 preInitializeAllObjects: function() {
146 this.iterObjectInstances(function(instance) {
147 instance.preInitialize();
151 initializeAllObjects: function() {
152 this.iterObjectInstances(function(instance) {
153 instance.initialize();
157 initializeInstances: function() {
158 this.iterObjectInstances(function(instance) {
159 instance.initialize();
163 updateBounds: function() {
165 this.iterObjectInstances(function(instance) {
166 instance.updateBounds();
167 this.bounds.addRange(instance.bounds);
171 shiftTimestampsForward: function(amount) {
172 this.iterObjectInstances(function(instance) {
173 instance.shiftTimestampsForward(amount);
177 addCategoriesToDict: function(categoriesDict) {
178 this.iterObjectInstances(function(instance) {
179 categoriesDict[instance.category] = true;
184 // TODO(nduca): Implement this if we need it.
188 iterateAllEvents: function(callback) {
189 this.iterObjectInstances(function(instance) {
191 instance.snapshots.forEach(callback);
197 ObjectCollection: ObjectCollection