Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / trace_model / object_instance.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/event.html">
9 <link rel="import" href="/tracing/trace_model/object_snapshot.html">
10 <link rel="import" href="/base/range.html">
11 <link rel="import" href="/base/sorted_array_utils.html">
12
13 <script>
14 'use strict';
15
16 /**
17  * @fileoverview Provides the ObjectSnapshot and ObjectHistory classes.
18  */
19 tv.exportTo('tracing.trace_model', function() {
20   var ObjectSnapshot = tracing.trace_model.ObjectSnapshot;
21
22   /**
23    * An object with a specific id, whose state has been snapshotted several
24    * times.
25    *
26    * @constructor
27    */
28   function ObjectInstance(
29       parent, id, category, name, creationTs, opt_baseTypeName) {
30     tracing.trace_model.Event.call(this);
31     this.parent = parent;
32     this.id = id;
33     this.category = category;
34     this.baseTypeName = opt_baseTypeName ? opt_baseTypeName : name;
35     this.name = name;
36     this.creationTs = creationTs;
37     this.creationTsWasExplicit = false;
38     this.deletionTs = Number.MAX_VALUE;
39     this.deletionTsWasExplicit = false;
40     this.colorId = 0;
41     this.bounds = new tv.Range();
42     this.snapshots = [];
43     this.hasImplicitSnapshots = false;
44   }
45
46   ObjectInstance.prototype = {
47     __proto__: tracing.trace_model.Event.prototype,
48
49     get typeName() {
50       return this.name;
51     },
52
53     addBoundsToRange: function(range) {
54       range.addRange(this.bounds);
55     },
56
57     addSnapshot: function(ts, args, opt_name, opt_baseTypeName) {
58       if (ts < this.creationTs)
59         throw new Error('Snapshots must be >= instance.creationTs');
60       if (ts >= this.deletionTs)
61         throw new Error('Snapshots cannot be added after ' +
62                         'an objects deletion timestamp.');
63
64       var lastSnapshot;
65       if (this.snapshots.length > 0) {
66         lastSnapshot = this.snapshots[this.snapshots.length - 1];
67         if (lastSnapshot.ts == ts)
68           throw new Error('Snapshots already exists at this time!');
69         if (ts < lastSnapshot.ts) {
70           throw new Error(
71               'Snapshots must be added in increasing timestamp order');
72         }
73       }
74
75       // Update baseTypeName if needed.
76       if (opt_name &&
77           (this.name != opt_name)) {
78         if (!opt_baseTypeName)
79           throw new Error('Must provide base type name for name update');
80         if (this.baseTypeName != opt_baseTypeName)
81           throw new Error('Cannot update type name: base types dont match');
82         this.name = opt_name;
83       }
84
85       var snapshotConstructor =
86           tracing.trace_model.ObjectSnapshot.getConstructor(
87               this.category, this.name);
88       var snapshot = new snapshotConstructor(this, ts, args);
89       this.snapshots.push(snapshot);
90       return snapshot;
91     },
92
93     wasDeleted: function(ts) {
94       var lastSnapshot;
95       if (this.snapshots.length > 0) {
96         lastSnapshot = this.snapshots[this.snapshots.length - 1];
97         if (lastSnapshot.ts > ts)
98           throw new Error(
99               'Instance cannot be deleted at ts=' +
100               ts + '. A snapshot exists that is older.');
101       }
102       this.deletionTs = ts;
103       this.deletionTsWasExplicit = true;
104     },
105
106     /**
107      * See ObjectSnapshot constructor notes on object initialization.
108      */
109     preInitialize: function() {
110       for (var i = 0; i < this.snapshots.length; i++)
111         this.snapshots[i].preInitialize();
112     },
113
114     /**
115      * See ObjectSnapshot constructor notes on object initialization.
116      */
117     initialize: function() {
118       for (var i = 0; i < this.snapshots.length; i++)
119         this.snapshots[i].initialize();
120     },
121
122     getSnapshotAt: function(ts) {
123       if (ts < this.creationTs) {
124         if (this.creationTsWasExplicit)
125           throw new Error('ts must be within lifetime of this instance');
126         return this.snapshots[0];
127       }
128       if (ts > this.deletionTs)
129         throw new Error('ts must be within lifetime of this instance');
130
131       var snapshots = this.snapshots;
132       var i = tv.findLowIndexInSortedIntervals(
133           snapshots,
134           function(snapshot) { return snapshot.ts; },
135           function(snapshot, i) {
136             if (i == snapshots.length - 1)
137               return snapshots[i].objectInstance.deletionTs;
138             return snapshots[i + 1].ts - snapshots[i].ts;
139           },
140           ts);
141       if (i < 0) {
142         // Note, this is a little bit sketchy: this lets early ts point at the
143         // first snapshot, even before it is taken. We do this because raster
144         // tasks usually post before their tile snapshots are dumped. This may
145         // be a good line of code to re-visit if we start seeing strange and
146         // confusing object references showing up in the traces.
147         return this.snapshots[0];
148       }
149       if (i >= this.snapshots.length)
150         return this.snapshots[this.snapshots.length - 1];
151       return this.snapshots[i];
152     },
153
154     updateBounds: function() {
155       this.bounds.reset();
156       this.bounds.addValue(this.creationTs);
157       if (this.deletionTs != Number.MAX_VALUE)
158         this.bounds.addValue(this.deletionTs);
159       else if (this.snapshots.length > 0)
160         this.bounds.addValue(this.snapshots[this.snapshots.length - 1].ts);
161     },
162
163     shiftTimestampsForward: function(amount) {
164       this.creationTs += amount;
165       if (this.deletionTs != Number.MAX_VALUE)
166         this.deletionTs += amount;
167       this.snapshots.forEach(function(snapshot) {
168         snapshot.ts += amount;
169       });
170     }
171   };
172
173   tracing.trace_model.Event.decorateSubtype(ObjectInstance);
174
175   return {
176     ObjectInstance: ObjectInstance
177   };
178 });
179 </script>