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="/base/utils.html">
11 <link rel="import" href="/base/range.html">
12 <link rel="import" href="/base/sorted_array_utils.html">
18 * @fileoverview Provides the ObjectCollection class.
20 tv.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 tv.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(
44 var instance = new constructor(
45 parent, id, category, name, creationTs, opt_baseTypeName);
46 var typeName = instance.typeName;
47 var instancesOfTypeName = this.instancesByTypeName_[typeName];
48 if (!instancesOfTypeName) {
49 instancesOfTypeName = [];
50 this.instancesByTypeName_[typeName] = instancesOfTypeName;
52 instancesOfTypeName.push(instance);
56 getOrCreateInstanceMap_: function(id) {
57 var instanceMap = this.instanceMapsById_[id];
60 instanceMap = new tracing.trace_model.TimeToObjectInstanceMap(
61 this.createObjectInstance_, this.parent, id);
62 this.instanceMapsById_[id] = instanceMap;
66 idWasCreated: function(id, category, name, ts) {
67 var instanceMap = this.getOrCreateInstanceMap_(id);
68 return instanceMap.idWasCreated(category, name, ts);
71 addSnapshot: function(id, category, name, ts, args, opt_baseTypeName) {
72 var instanceMap = this.getOrCreateInstanceMap_(id);
73 var snapshot = instanceMap.addSnapshot(
74 category, name, ts, args, opt_baseTypeName);
75 if (snapshot.objectInstance.category != category) {
76 var msg = 'Added snapshot name=' + name + ' with cat=' + category +
77 ' impossible. It instance was created/snapshotted with cat=' +
78 snapshot.objectInstance.category + ' name=' +
79 snapshot.objectInstance.name;
82 if (opt_baseTypeName &&
83 snapshot.objectInstance.baseTypeName != opt_baseTypeName) {
84 throw new Error('Could not add snapshot with baseTypeName=' +
85 opt_baseTypeName + '. It ' +
86 'was previously created with name=' +
87 snapshot.objectInstance.baseTypeName);
89 if (snapshot.objectInstance.name != name) {
90 throw new Error('Could not add snapshot with name=' + name + '. It ' +
91 'was previously created with name=' +
92 snapshot.objectInstance.name);
97 idWasDeleted: function(id, category, name, ts) {
98 var instanceMap = this.getOrCreateInstanceMap_(id);
99 var deletedInstance = instanceMap.idWasDeleted(category, name, ts);
100 if (!deletedInstance)
102 if (deletedInstance.category != category) {
103 var msg = 'Deleting object ' + deletedInstance.name +
104 ' with a different category ' +
105 'than when it was created. It previous had cat=' +
106 deletedInstance.category + ' but the delete command ' +
107 'had cat=' + category;
108 throw new Error(msg);
110 if (deletedInstance.baseTypeName != name) {
111 throw new Error('Deletion requested for name=' +
112 name + ' could not proceed: ' +
113 'An existing object with baseTypeName=' +
114 deletedInstance.baseTypeName + ' existed.');
118 autoDeleteObjects: function(maxTimestamp) {
119 tv.iterItems(this.instanceMapsById_, function(id, i2imap) {
120 var lastInstance = i2imap.lastInstance;
121 if (lastInstance.deletionTs != Number.MAX_VALUE)
124 lastInstance.category, lastInstance.name, maxTimestamp);
125 // idWasDeleted will cause lastInstance.deletionTsWasExplicit to be set
126 // to true. Unset it here.
127 lastInstance.deletionTsWasExplicit = false;
131 getObjectInstanceAt: function(id, ts) {
132 var instanceMap = this.instanceMapsById_[id];
135 return instanceMap.getInstanceAt(ts);
138 getSnapshotAt: function(id, ts) {
139 var instance = this.getObjectInstanceAt(id, ts);
142 return instance.getSnapshotAt(ts);
145 iterObjectInstances: function(iter, opt_this) {
146 opt_this = opt_this || this;
147 tv.iterItems(this.instanceMapsById_, function(id, i2imap) {
148 i2imap.instances.forEach(iter, opt_this);
152 getAllObjectInstances: function() {
154 this.iterObjectInstances(function(i) { instances.push(i); });
158 getAllInstancesNamed: function(name) {
159 return this.instancesByTypeName_[name];
162 getAllInstancesByTypeName: function() {
163 return this.instancesByTypeName_;
166 preInitializeAllObjects: function() {
167 this.iterObjectInstances(function(instance) {
168 instance.preInitialize();
172 initializeAllObjects: function() {
173 this.iterObjectInstances(function(instance) {
174 instance.initialize();
178 initializeInstances: function() {
179 this.iterObjectInstances(function(instance) {
180 instance.initialize();
184 updateBounds: function() {
186 this.iterObjectInstances(function(instance) {
187 instance.updateBounds();
188 this.bounds.addRange(instance.bounds);
192 shiftTimestampsForward: function(amount) {
193 this.iterObjectInstances(function(instance) {
194 instance.shiftTimestampsForward(amount);
198 addCategoriesToDict: function(categoriesDict) {
199 this.iterObjectInstances(function(instance) {
200 categoriesDict[instance.category] = true;
205 // TODO(nduca): Implement this if we need it.
209 iterateAllEvents: function(callback, opt_this) {
210 this.iterObjectInstances(function(instance) {
211 callback.call(this, instance);
212 instance.snapshots.forEach(callback);
218 ObjectCollection: ObjectCollection