Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / src / tracing / trace_model / object_collection.js
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.
4
5 'use strict';
6
7 /**
8  * @fileoverview Provides the ObjectCollection class.
9  */
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');
15
16 tvcm.exportTo('tracing.trace_model', function() {
17   var ObjectInstance = tracing.trace_model.ObjectInstance;
18
19   /**
20    * A collection of object instances and their snapshots, accessible by id and
21    * time, or by object name.
22    *
23    * @constructor
24    */
25   function ObjectCollection(parent) {
26     this.parent = parent;
27     this.bounds = new tvcm.Range();
28     this.instanceMapsById_ = {}; // id -> TimeToObjectInstanceMap
29     this.instancesByTypeName_ = {};
30     this.createObjectInstance_ = this.createObjectInstance_.bind(this);
31   }
32
33   ObjectCollection.prototype = {
34     __proto__: Object.prototype,
35
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;
44       }
45       instancesOfTypeName.push(instance);
46       return instance;
47     },
48
49     getOrCreateInstanceMap_: function(id) {
50       var instanceMap = this.instanceMapsById_[id];
51       if (instanceMap)
52         return instanceMap;
53       instanceMap = new tracing.trace_model.TimeToObjectInstanceMap(
54           this.createObjectInstance_, this.parent, id);
55       this.instanceMapsById_[id] = instanceMap;
56       return instanceMap;
57     },
58
59     idWasCreated: function(id, category, name, ts) {
60       var instanceMap = this.getOrCreateInstanceMap_(id);
61       return instanceMap.idWasCreated(category, name, ts);
62     },
63
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);
71       }
72       if (snapshot.objectInstance.name != name) {
73         throw new Error('Added snapshot with different name than ' +
74                         'when it was created');
75       }
76       return snapshot;
77     },
78
79     idWasDeleted: function(id, category, name, ts) {
80       var instanceMap = this.getOrCreateInstanceMap_(id, category, name, ts);
81       var deletedInstance = instanceMap.idWasDeleted(category, name, ts);
82       if (!deletedInstance)
83         return;
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);
90       }
91       if (deletedInstance.name != name) {
92         throw new Error('Deleting an object with a different name than ' +
93                         'when it was created');
94       }
95     },
96
97     autoDeleteObjects: function(maxTimestamp) {
98       tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
99         var lastInstance = i2imap.lastInstance;
100         if (lastInstance.deletionTs != Number.MAX_VALUE)
101           return;
102         i2imap.idWasDeleted(
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;
107       });
108     },
109
110     getObjectInstanceAt: function(id, ts) {
111       var instanceMap = this.instanceMapsById_[id];
112       if (!instanceMap)
113         return undefined;
114       return instanceMap.getInstanceAt(ts);
115     },
116
117     getSnapshotAt: function(id, ts) {
118       var instance = this.getObjectInstanceAt(id, ts);
119       if (!instance)
120         return undefined;
121       return instance.getSnapshotAt(ts);
122     },
123
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);
128       });
129     },
130
131     getAllObjectInstances: function() {
132       var instances = [];
133       this.iterObjectInstances(function(i) { instances.push(i); });
134       return instances;
135     },
136
137     getAllInstancesNamed: function(name) {
138       return this.instancesByTypeName_[name];
139     },
140
141     getAllInstancesByTypeName: function() {
142       return this.instancesByTypeName_;
143     },
144
145     preInitializeAllObjects: function() {
146       this.iterObjectInstances(function(instance) {
147         instance.preInitialize();
148       });
149     },
150
151     initializeAllObjects: function() {
152       this.iterObjectInstances(function(instance) {
153         instance.initialize();
154       });
155     },
156
157     initializeInstances: function() {
158       this.iterObjectInstances(function(instance) {
159         instance.initialize();
160       });
161     },
162
163     updateBounds: function() {
164       this.bounds.reset();
165       this.iterObjectInstances(function(instance) {
166         instance.updateBounds();
167         this.bounds.addRange(instance.bounds);
168       }, this);
169     },
170
171     shiftTimestampsForward: function(amount) {
172       this.iterObjectInstances(function(instance) {
173         instance.shiftTimestampsForward(amount);
174       });
175     },
176
177     addCategoriesToDict: function(categoriesDict) {
178       this.iterObjectInstances(function(instance) {
179         categoriesDict[instance.category] = true;
180       });
181     },
182
183     toJSON: function() {
184       // TODO(nduca): Implement this if we need it.
185       return {};
186     },
187
188     iterateAllEvents: function(callback) {
189       this.iterObjectInstances(function(instance) {
190         callback(instance);
191         instance.snapshots.forEach(callback);
192       });
193     }
194   };
195
196   return {
197     ObjectCollection: ObjectCollection
198   };
199 });