Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / 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, 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;
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, 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;
72         throw new Error(msg);
73       }
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);
80       }
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);
85       }
86       return snapshot;
87     },
88
89     idWasDeleted: function(id, category, name, ts) {
90       var instanceMap = this.getOrCreateInstanceMap_(id);
91       var deletedInstance = instanceMap.idWasDeleted(category, name, ts);
92       if (!deletedInstance)
93         return;
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);
101       }
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.')
107       }
108     },
109
110     autoDeleteObjects: function(maxTimestamp) {
111       tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
112         var lastInstance = i2imap.lastInstance;
113         if (lastInstance.deletionTs != Number.MAX_VALUE)
114           return;
115         i2imap.idWasDeleted(
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;
120       });
121     },
122
123     getObjectInstanceAt: function(id, ts) {
124       var instanceMap = this.instanceMapsById_[id];
125       if (!instanceMap)
126         return undefined;
127       return instanceMap.getInstanceAt(ts);
128     },
129
130     getSnapshotAt: function(id, ts) {
131       var instance = this.getObjectInstanceAt(id, ts);
132       if (!instance)
133         return undefined;
134       return instance.getSnapshotAt(ts);
135     },
136
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);
141       });
142     },
143
144     getAllObjectInstances: function() {
145       var instances = [];
146       this.iterObjectInstances(function(i) { instances.push(i); });
147       return instances;
148     },
149
150     getAllInstancesNamed: function(name) {
151       return this.instancesByTypeName_[name];
152     },
153
154     getAllInstancesByTypeName: function() {
155       return this.instancesByTypeName_;
156     },
157
158     preInitializeAllObjects: function() {
159       this.iterObjectInstances(function(instance) {
160         instance.preInitialize();
161       });
162     },
163
164     initializeAllObjects: function() {
165       this.iterObjectInstances(function(instance) {
166         instance.initialize();
167       });
168     },
169
170     initializeInstances: function() {
171       this.iterObjectInstances(function(instance) {
172         instance.initialize();
173       });
174     },
175
176     updateBounds: function() {
177       this.bounds.reset();
178       this.iterObjectInstances(function(instance) {
179         instance.updateBounds();
180         this.bounds.addRange(instance.bounds);
181       }, this);
182     },
183
184     shiftTimestampsForward: function(amount) {
185       this.iterObjectInstances(function(instance) {
186         instance.shiftTimestampsForward(amount);
187       });
188     },
189
190     addCategoriesToDict: function(categoriesDict) {
191       this.iterObjectInstances(function(instance) {
192         categoriesDict[instance.category] = true;
193       });
194     },
195
196     toJSON: function() {
197       // TODO(nduca): Implement this if we need it.
198       return {};
199     },
200
201     iterateAllEvents: function(callback, opt_this) {
202       this.iterObjectInstances(function(instance) {
203         callback.call(this, instance);
204         instance.snapshots.forEach(callback);
205       }, opt_this);
206     }
207   };
208
209   return {
210     ObjectCollection: ObjectCollection
211   };
212 });