Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / trace_model / object_collection.html
1 <!DOCTYPE html>
2 <!--
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.
6 -->
7
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">
13
14 <script>
15 'use strict';
16
17 /**
18  * @fileoverview Provides the ObjectCollection class.
19  */
20 tvcm.exportTo('tracing.trace_model', function() {
21   var ObjectInstance = tracing.trace_model.ObjectInstance;
22
23   /**
24    * A collection of object instances and their snapshots, accessible by id and
25    * time, or by object name.
26    *
27    * @constructor
28    */
29   function ObjectCollection(parent) {
30     this.parent = parent;
31     this.bounds = new tvcm.Range();
32     this.instanceMapsById_ = {}; // id -> TimeToObjectInstanceMap
33     this.instancesByTypeName_ = {};
34     this.createObjectInstance_ = this.createObjectInstance_.bind(this);
35   }
36
37   ObjectCollection.prototype = {
38     __proto__: Object.prototype,
39
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;
50       }
51       instancesOfTypeName.push(instance);
52       return instance;
53     },
54
55     getOrCreateInstanceMap_: function(id) {
56       var instanceMap = this.instanceMapsById_[id];
57       if (instanceMap)
58         return instanceMap;
59       instanceMap = new tracing.trace_model.TimeToObjectInstanceMap(
60           this.createObjectInstance_, this.parent, id);
61       this.instanceMapsById_[id] = instanceMap;
62       return instanceMap;
63     },
64
65     idWasCreated: function(id, category, name, ts) {
66       var instanceMap = this.getOrCreateInstanceMap_(id);
67       return instanceMap.idWasCreated(category, name, ts);
68     },
69
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;
79         throw new Error(msg);
80       }
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);
87       }
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);
92       }
93       return snapshot;
94     },
95
96     idWasDeleted: function(id, category, name, ts) {
97       var instanceMap = this.getOrCreateInstanceMap_(id);
98       var deletedInstance = instanceMap.idWasDeleted(category, name, ts);
99       if (!deletedInstance)
100         return;
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);
108       }
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.');
114       }
115     },
116
117     autoDeleteObjects: function(maxTimestamp) {
118       tvcm.iterItems(this.instanceMapsById_, function(id, i2imap) {
119         var lastInstance = i2imap.lastInstance;
120         if (lastInstance.deletionTs != Number.MAX_VALUE)
121           return;
122         i2imap.idWasDeleted(
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;
127       });
128     },
129
130     getObjectInstanceAt: function(id, ts) {
131       var instanceMap = this.instanceMapsById_[id];
132       if (!instanceMap)
133         return undefined;
134       return instanceMap.getInstanceAt(ts);
135     },
136
137     getSnapshotAt: function(id, ts) {
138       var instance = this.getObjectInstanceAt(id, ts);
139       if (!instance)
140         return undefined;
141       return instance.getSnapshotAt(ts);
142     },
143
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);
148       });
149     },
150
151     getAllObjectInstances: function() {
152       var instances = [];
153       this.iterObjectInstances(function(i) { instances.push(i); });
154       return instances;
155     },
156
157     getAllInstancesNamed: function(name) {
158       return this.instancesByTypeName_[name];
159     },
160
161     getAllInstancesByTypeName: function() {
162       return this.instancesByTypeName_;
163     },
164
165     preInitializeAllObjects: function() {
166       this.iterObjectInstances(function(instance) {
167         instance.preInitialize();
168       });
169     },
170
171     initializeAllObjects: function() {
172       this.iterObjectInstances(function(instance) {
173         instance.initialize();
174       });
175     },
176
177     initializeInstances: function() {
178       this.iterObjectInstances(function(instance) {
179         instance.initialize();
180       });
181     },
182
183     updateBounds: function() {
184       this.bounds.reset();
185       this.iterObjectInstances(function(instance) {
186         instance.updateBounds();
187         this.bounds.addRange(instance.bounds);
188       }, this);
189     },
190
191     shiftTimestampsForward: function(amount) {
192       this.iterObjectInstances(function(instance) {
193         instance.shiftTimestampsForward(amount);
194       });
195     },
196
197     addCategoriesToDict: function(categoriesDict) {
198       this.iterObjectInstances(function(instance) {
199         categoriesDict[instance.category] = true;
200       });
201     },
202
203     toJSON: function() {
204       // TODO(nduca): Implement this if we need it.
205       return {};
206     },
207
208     iterateAllEvents: function(callback, opt_this) {
209       this.iterObjectInstances(function(instance) {
210         callback.call(this, instance);
211         instance.snapshots.forEach(callback);
212       }, opt_this);
213     }
214   };
215
216   return {
217     ObjectCollection: ObjectCollection
218   };
219 });
220 </script>