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="/tracing/trace_model/object_instance.html">
9 <link rel="import" href="/tracing/trace_model/time_to_object_instance_map.html">
10 <link rel="import" href="/tvcm/utils.html">
11 <link rel="import" href="/tvcm/range.html">
12 <link rel="import" href="/tvcm/sorted_array_utils.html">
18 * @fileoverview Provides the ObjectCollection class.
20 tvcm.exportTo('tracing.trace_model', function() {
21 var ObjectInstance = tracing.trace_model.ObjectInstance;
24 * A collection of object instances and their snapshots, accessible by id and
25 * time, or by object name.
29 function ObjectCollection(parent) {
31 this.bounds = new tvcm.Range();
32 this.instanceMapsById_ = {}; // id -> TimeToObjectInstanceMap
33 this.instancesByTypeName_ = {};
34 this.createObjectInstance_ = this.createObjectInstance_.bind(this);
37 ObjectCollection.prototype = {
38 __proto__: Object.prototype,
40 createObjectInstance_: function(
41 parent, id, category, name, creationTs, opt_baseTypeName) {
42 var constructor = tracing.trace_model.ObjectInstance.getConstructor(name);
43 var instance = new constructor(
44 parent, id, category, name, creationTs, opt_baseTypeName);
45 var typeName = instance.typeName;
46 var instancesOfTypeName = this.instancesByTypeName_[typeName];
47 if (!instancesOfTypeName) {
48 instancesOfTypeName = [];
49 this.instancesByTypeName_[typeName] = instancesOfTypeName;
51 instancesOfTypeName.push(instance);
55 getOrCreateInstanceMap_: function(id) {
56 var instanceMap = this.instanceMapsById_[id];
59 instanceMap = new tracing.trace_model.TimeToObjectInstanceMap(
60 this.createObjectInstance_, this.parent, id);
61 this.instanceMapsById_[id] = instanceMap;
65 idWasCreated: function(id, category, name, ts) {
66 var instanceMap = this.getOrCreateInstanceMap_(id);
67 return instanceMap.idWasCreated(category, name, ts);
70 addSnapshot: function(id, category, name, ts, args, opt_baseTypeName) {
71 var instanceMap = this.getOrCreateInstanceMap_(id);
72 var snapshot = instanceMap.addSnapshot(
73 category, name, ts, args, opt_baseTypeName);
74 if (snapshot.objectInstance.category != category) {
75 var msg = 'Added snapshot name=' + name + ' with cat=' + category +
76 ' impossible. It instance was created/snapshotted with cat=' +
77 snapshot.objectInstance.category + ' name=' +
78 snapshot.objectInstance.name;
81 if (opt_baseTypeName &&
82 snapshot.objectInstance.baseTypeName != opt_baseTypeName) {
83 throw new Error('Could not add snapshot with baseTypeName=' +
84 opt_baseTypeName + '. It ' +
85 'was previously created with name=' +
86 snapshot.objectInstance.baseTypeName);
88 if (snapshot.objectInstance.name != name) {
89 throw new Error('Could not add snapshot with name=' + name + '. It ' +
90 'was previously created with name=' +
91 snapshot.objectInstance.name);
96 idWasDeleted: function(id, category, name, ts) {
97 var instanceMap = this.getOrCreateInstanceMap_(id);
98 var deletedInstance = instanceMap.idWasDeleted(category, name, ts);
101 if (deletedInstance.category != category) {
102 var msg = 'Deleting object ' + deletedInstance.name +
103 ' with a different category ' +
104 'than when it was created. It previous had cat=' +
105 deletedInstance.category + ' but the delete command ' +
106 'had cat=' + category;
107 throw new Error(msg);
109 if (deletedInstance.baseTypeName != name) {
110 throw new Error('Deletion requested for name=' +
111 name + ' could not proceed: ' +
112 'An existing object with baseTypeName=' +
113 deletedInstance.baseTypeName + ' existed.');
117 autoDeleteObjects: function(maxTimestamp) {
118 tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
119 var lastInstance = i2imap.lastInstance;
120 if (lastInstance.deletionTs != Number.MAX_VALUE)
123 lastInstance.category, lastInstance.name, maxTimestamp);
124 // idWasDeleted will cause lastInstance.deletionTsWasExplicit to be set
125 // to true. Unset it here.
126 lastInstance.deletionTsWasExplicit = false;
130 getObjectInstanceAt: function(id, ts) {
131 var instanceMap = this.instanceMapsById_[id];
134 return instanceMap.getInstanceAt(ts);
137 getSnapshotAt: function(id, ts) {
138 var instance = this.getObjectInstanceAt(id, ts);
141 return instance.getSnapshotAt(ts);
144 iterObjectInstances: function(iter, opt_this) {
145 opt_this = opt_this || this;
146 tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
147 i2imap.instances.forEach(iter, opt_this);
151 getAllObjectInstances: function() {
153 this.iterObjectInstances(function(i) { instances.push(i); });
157 getAllInstancesNamed: function(name) {
158 return this.instancesByTypeName_[name];
161 getAllInstancesByTypeName: function() {
162 return this.instancesByTypeName_;
165 preInitializeAllObjects: function() {
166 this.iterObjectInstances(function(instance) {
167 instance.preInitialize();
171 initializeAllObjects: function() {
172 this.iterObjectInstances(function(instance) {
173 instance.initialize();
177 initializeInstances: function() {
178 this.iterObjectInstances(function(instance) {
179 instance.initialize();
183 updateBounds: function() {
185 this.iterObjectInstances(function(instance) {
186 instance.updateBounds();
187 this.bounds.addRange(instance.bounds);
191 shiftTimestampsForward: function(amount) {
192 this.iterObjectInstances(function(instance) {
193 instance.shiftTimestampsForward(amount);
197 addCategoriesToDict: function(categoriesDict) {
198 this.iterObjectInstances(function(instance) {
199 categoriesDict[instance.category] = true;
204 // TODO(nduca): Implement this if we need it.
208 iterateAllEvents: function(callback, opt_this) {
209 this.iterObjectInstances(function(instance) {
210 callback.call(this, instance);
211 instance.snapshots.forEach(callback);
217 ObjectCollection: ObjectCollection