Upstream version 11.40.277.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="/base/utils.html">
11 <link rel="import" href="/base/range.html">
12 <link rel="import" href="/base/sorted_array_utils.html">
13
14 <script>
15 'use strict';
16
17 /**
18  * @fileoverview Provides the ObjectCollection class.
19  */
20 tv.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 tv.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(
43           category, name);
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;
51       }
52       instancesOfTypeName.push(instance);
53       return instance;
54     },
55
56     getOrCreateInstanceMap_: function(id) {
57       var instanceMap = this.instanceMapsById_[id];
58       if (instanceMap)
59         return instanceMap;
60       instanceMap = new tracing.trace_model.TimeToObjectInstanceMap(
61           this.createObjectInstance_, this.parent, id);
62       this.instanceMapsById_[id] = instanceMap;
63       return instanceMap;
64     },
65
66     idWasCreated: function(id, category, name, ts) {
67       var instanceMap = this.getOrCreateInstanceMap_(id);
68       return instanceMap.idWasCreated(category, name, ts);
69     },
70
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;
80         throw new Error(msg);
81       }
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);
88       }
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);
93       }
94       return snapshot;
95     },
96
97     idWasDeleted: function(id, category, name, ts) {
98       var instanceMap = this.getOrCreateInstanceMap_(id);
99       var deletedInstance = instanceMap.idWasDeleted(category, name, ts);
100       if (!deletedInstance)
101         return;
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);
109       }
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.');
115       }
116     },
117
118     autoDeleteObjects: function(maxTimestamp) {
119       tv.iterItems(this.instanceMapsById_, function(id, i2imap) {
120         var lastInstance = i2imap.lastInstance;
121         if (lastInstance.deletionTs != Number.MAX_VALUE)
122           return;
123         i2imap.idWasDeleted(
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;
128       });
129     },
130
131     getObjectInstanceAt: function(id, ts) {
132       var instanceMap = this.instanceMapsById_[id];
133       if (!instanceMap)
134         return undefined;
135       return instanceMap.getInstanceAt(ts);
136     },
137
138     getSnapshotAt: function(id, ts) {
139       var instance = this.getObjectInstanceAt(id, ts);
140       if (!instance)
141         return undefined;
142       return instance.getSnapshotAt(ts);
143     },
144
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);
149       });
150     },
151
152     getAllObjectInstances: function() {
153       var instances = [];
154       this.iterObjectInstances(function(i) { instances.push(i); });
155       return instances;
156     },
157
158     getAllInstancesNamed: function(name) {
159       return this.instancesByTypeName_[name];
160     },
161
162     getAllInstancesByTypeName: function() {
163       return this.instancesByTypeName_;
164     },
165
166     preInitializeAllObjects: function() {
167       this.iterObjectInstances(function(instance) {
168         instance.preInitialize();
169       });
170     },
171
172     initializeAllObjects: function() {
173       this.iterObjectInstances(function(instance) {
174         instance.initialize();
175       });
176     },
177
178     initializeInstances: function() {
179       this.iterObjectInstances(function(instance) {
180         instance.initialize();
181       });
182     },
183
184     updateBounds: function() {
185       this.bounds.reset();
186       this.iterObjectInstances(function(instance) {
187         instance.updateBounds();
188         this.bounds.addRange(instance.bounds);
189       }, this);
190     },
191
192     shiftTimestampsForward: function(amount) {
193       this.iterObjectInstances(function(instance) {
194         instance.shiftTimestampsForward(amount);
195       });
196     },
197
198     addCategoriesToDict: function(categoriesDict) {
199       this.iterObjectInstances(function(instance) {
200         categoriesDict[instance.category] = true;
201       });
202     },
203
204     toJSON: function() {
205       // TODO(nduca): Implement this if we need it.
206       return {};
207     },
208
209     iterateAllEvents: function(callback, opt_this) {
210       this.iterObjectInstances(function(instance) {
211         callback.call(this, instance);
212         instance.snapshots.forEach(callback);
213       }, opt_this);
214     }
215   };
216
217   return {
218     ObjectCollection: ObjectCollection
219   };
220 });
221 </script>