Upstream version 7.36.149.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(
37         parent, id, category, name, creationTs, opt_baseTypeName) {
38       var constructor = tracing.trace_model.ObjectInstance.getConstructor(name);
39       var instance = new constructor(
40           parent, id, category, name, creationTs, opt_baseTypeName);
41       var typeName = instance.typeName;
42       var instancesOfTypeName = this.instancesByTypeName_[typeName];
43       if (!instancesOfTypeName) {
44         instancesOfTypeName = [];
45         this.instancesByTypeName_[typeName] = instancesOfTypeName;
46       }
47       instancesOfTypeName.push(instance);
48       return instance;
49     },
50
51     getOrCreateInstanceMap_: function(id) {
52       var instanceMap = this.instanceMapsById_[id];
53       if (instanceMap)
54         return instanceMap;
55       instanceMap = new tracing.trace_model.TimeToObjectInstanceMap(
56           this.createObjectInstance_, this.parent, id);
57       this.instanceMapsById_[id] = instanceMap;
58       return instanceMap;
59     },
60
61     idWasCreated: function(id, category, name, ts) {
62       var instanceMap = this.getOrCreateInstanceMap_(id);
63       return instanceMap.idWasCreated(category, name, ts);
64     },
65
66     addSnapshot: function(id, category, name, ts, args, opt_baseTypeName) {
67       var instanceMap = this.getOrCreateInstanceMap_(id);
68       var snapshot = instanceMap.addSnapshot(
69           category, name, ts, args, opt_baseTypeName);
70       if (snapshot.objectInstance.category != category) {
71         var msg = 'Added snapshot name=' + name + ' with cat=' + category +
72             ' impossible. It instance was created/snapshotted with cat=' +
73             snapshot.objectInstance.category + ' name=' +
74             snapshot.objectInstance.name;
75         throw new Error(msg);
76       }
77       if (opt_baseTypeName &&
78           snapshot.objectInstance.baseTypeName != opt_baseTypeName) {
79         throw new Error('Could not add snapshot with baseTypeName=' +
80                         opt_baseTypeName + '. It ' +
81                         'was previously created with name=' +
82                         snapshot.objectInstance.baseTypeName);
83       }
84       if (snapshot.objectInstance.name != name) {
85         throw new Error('Could not add snapshot with name=' + name + '. It ' +
86                         'was previously created with name=' +
87                         snapshot.objectInstance.name);
88       }
89       return snapshot;
90     },
91
92     idWasDeleted: function(id, category, name, ts) {
93       var instanceMap = this.getOrCreateInstanceMap_(id);
94       var deletedInstance = instanceMap.idWasDeleted(category, name, ts);
95       if (!deletedInstance)
96         return;
97       if (deletedInstance.category != category) {
98         var msg = 'Deleting object ' + deletedInstance.name +
99             ' with a different category ' +
100             'than when it was created. It previous had cat=' +
101             deletedInstance.category + ' but the delete command ' +
102             'had cat=' + category;
103         throw new Error(msg);
104       }
105       if (deletedInstance.baseTypeName != name) {
106         throw new Error('Deletion requested for name=' +
107                         name + ' could not proceed: ' +
108                         'An existing object with baseTypeName=' +
109                         deletedInstance.baseTypeName + ' existed.');
110       }
111     },
112
113     autoDeleteObjects: function(maxTimestamp) {
114       tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
115         var lastInstance = i2imap.lastInstance;
116         if (lastInstance.deletionTs != Number.MAX_VALUE)
117           return;
118         i2imap.idWasDeleted(
119             lastInstance.category, lastInstance.name, maxTimestamp);
120         // idWasDeleted will cause lastInstance.deletionTsWasExplicit to be set
121         // to true. Unset it here.
122         lastInstance.deletionTsWasExplicit = false;
123       });
124     },
125
126     getObjectInstanceAt: function(id, ts) {
127       var instanceMap = this.instanceMapsById_[id];
128       if (!instanceMap)
129         return undefined;
130       return instanceMap.getInstanceAt(ts);
131     },
132
133     getSnapshotAt: function(id, ts) {
134       var instance = this.getObjectInstanceAt(id, ts);
135       if (!instance)
136         return undefined;
137       return instance.getSnapshotAt(ts);
138     },
139
140     iterObjectInstances: function(iter, opt_this) {
141       opt_this = opt_this || this;
142       tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
143         i2imap.instances.forEach(iter, opt_this);
144       });
145     },
146
147     getAllObjectInstances: function() {
148       var instances = [];
149       this.iterObjectInstances(function(i) { instances.push(i); });
150       return instances;
151     },
152
153     getAllInstancesNamed: function(name) {
154       return this.instancesByTypeName_[name];
155     },
156
157     getAllInstancesByTypeName: function() {
158       return this.instancesByTypeName_;
159     },
160
161     preInitializeAllObjects: function() {
162       this.iterObjectInstances(function(instance) {
163         instance.preInitialize();
164       });
165     },
166
167     initializeAllObjects: function() {
168       this.iterObjectInstances(function(instance) {
169         instance.initialize();
170       });
171     },
172
173     initializeInstances: function() {
174       this.iterObjectInstances(function(instance) {
175         instance.initialize();
176       });
177     },
178
179     updateBounds: function() {
180       this.bounds.reset();
181       this.iterObjectInstances(function(instance) {
182         instance.updateBounds();
183         this.bounds.addRange(instance.bounds);
184       }, this);
185     },
186
187     shiftTimestampsForward: function(amount) {
188       this.iterObjectInstances(function(instance) {
189         instance.shiftTimestampsForward(amount);
190       });
191     },
192
193     addCategoriesToDict: function(categoriesDict) {
194       this.iterObjectInstances(function(instance) {
195         categoriesDict[instance.category] = true;
196       });
197     },
198
199     toJSON: function() {
200       // TODO(nduca): Implement this if we need it.
201       return {};
202     },
203
204     iterateAllEvents: function(callback, opt_this) {
205       this.iterObjectInstances(function(instance) {
206         callback.call(this, instance);
207         instance.snapshots.forEach(callback);
208       }, opt_this);
209     }
210   };
211
212   return {
213     ObjectCollection: ObjectCollection
214   };
215 });