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.
8 * @fileoverview Provides the ObjectSnapshot and ObjectHistory classes.
10 base.require('base.range');
11 base.require('base.sorted_array_utils');
12 base.require('tracing.trace_model.event');
13 base.require('tracing.trace_model.object_snapshot');
15 base.exportTo('tracing.trace_model', function() {
16 var ObjectSnapshot = tracing.trace_model.ObjectSnapshot;
19 * An object with a specific id, whose state has been snapshotted several
24 function ObjectInstance(parent, id, category, name, creationTs) {
25 tracing.trace_model.Event.call(this);
28 this.category = category;
30 this.creationTs = creationTs;
31 this.creationTsWasExplicit = false;
32 this.deletionTs = Number.MAX_VALUE;
33 this.deletionTsWasExplicit = false;
35 this.bounds = new base.Range();
37 this.hasImplicitSnapshots = false;
40 ObjectInstance.prototype = {
41 __proto__: tracing.trace_model.Event.prototype,
47 addBoundsToRange: function(range) {
48 range.addRange(this.bounds);
51 addSnapshot: function(ts, args) {
52 if (ts < this.creationTs)
53 throw new Error('Snapshots must be >= instance.creationTs');
54 if (ts >= this.deletionTs)
55 throw new Error('Snapshots cannot be added after ' +
56 'an objects deletion timestamp.');
59 if (this.snapshots.length > 0) {
60 lastSnapshot = this.snapshots[this.snapshots.length - 1];
61 if (lastSnapshot.ts == ts)
62 throw new Error('Snapshots already exists at this time!');
63 if (ts < lastSnapshot.ts) {
65 'Snapshots must be added in increasing timestamp order');
69 var snapshotConstructor =
70 tracing.trace_model.ObjectSnapshot.getConstructor(this.name);
71 var snapshot = new snapshotConstructor(this, ts, args);
72 this.snapshots.push(snapshot);
76 wasDeleted: function(ts) {
78 if (this.snapshots.length > 0) {
79 lastSnapshot = this.snapshots[this.snapshots.length - 1];
80 if (lastSnapshot.ts > ts)
82 'Instance cannot be deleted at ts=' +
83 ts + '. A snapshot exists that is older.');
86 this.deletionTsWasExplicit = true;
90 * See ObjectSnapshot constructor notes on object initialization.
92 preInitialize: function() {
93 for (var i = 0; i < this.snapshots.length; i++)
94 this.snapshots[i].preInitialize();
98 * See ObjectSnapshot constructor notes on object initialization.
100 initialize: function() {
101 for (var i = 0; i < this.snapshots.length; i++)
102 this.snapshots[i].initialize();
105 getSnapshotAt: function(ts) {
106 if (ts < this.creationTs) {
107 if (this.creationTsWasExplicit)
108 throw new Error('ts must be within lifetime of this instance');
109 return this.snapshots[0];
111 if (ts > this.deletionTs)
112 throw new Error('ts must be within lifetime of this instance');
114 var snapshots = this.snapshots;
115 var i = base.findLowIndexInSortedIntervals(
117 function(snapshot) { return snapshot.ts; },
118 function(snapshot, i) {
119 if (i == snapshots.length - 1)
120 return snapshots[i].objectInstance.deletionTs;
121 return snapshots[i + 1].ts - snapshots[i].ts;
125 // Note, this is a little bit sketchy: this lets early ts point at the
126 // first snapshot, even before it is taken. We do this because raster
127 // tasks usually post before their tile snapshots are dumped. This may
128 // be a good line of code to re-visit if we start seeing strange and
129 // confusing object references showing up in the traces.
130 return this.snapshots[0];
132 if (i >= this.snapshots.length)
133 return this.snapshots[this.snapshots.length - 1];
134 return this.snapshots[i];
137 updateBounds: function() {
139 this.bounds.addValue(this.creationTs);
140 if (this.deletionTs != Number.MAX_VALUE)
141 this.bounds.addValue(this.deletionTs);
142 else if (this.snapshots.length > 0)
143 this.bounds.addValue(this.snapshots[this.snapshots.length - 1].ts);
146 shiftTimestampsForward: function(amount) {
147 this.creationTs += amount;
148 if (this.deletionTs != Number.MAX_VALUE)
149 this.deletionTs += amount;
150 this.snapshots.forEach(function(snapshot) {
151 snapshot.ts += amount;
156 ObjectInstance.nameToConstructorMap_ = {};
157 ObjectInstance.register = function(name, constructor) {
158 if (ObjectInstance.nameToConstructorMap_[name])
159 throw new Error('Constructor already registerd for ' + name);
160 ObjectInstance.nameToConstructorMap_[name] = constructor;
163 ObjectInstance.unregister = function(name) {
164 delete ObjectInstance.nameToConstructorMap_[name];
167 ObjectInstance.getConstructor = function(name) {
168 if (ObjectInstance.nameToConstructorMap_[name])
169 return ObjectInstance.nameToConstructorMap_[name];
170 return ObjectInstance;
174 ObjectInstance: ObjectInstance