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
7base.require('tracing.analysis.generic_object_view');
8base.require('tracing.analysis.analyze_selection');
9base.require('tracing.analysis.analysis_results');
10
11base.exportTo('cc', function() {
12  var tsRound = tracing.analysis.tsRound;
13
14  function Selection() {
15
16  };
17  Selection.prototype = {
18    /**
19     * If a selection is related to a specific layer, then this returns the
20     * layerId of that layer. If the selection is not related to a layer, for
21     * example if the device viewport is selected, then this returns undefined.
22     */
23    get associatedLayerId() {
24      throw new Error('Not implemented');
25    },
26
27    /**
28     * If the selected item(s) is visible on the pending tree in a way that
29     * should be highlighted, returns the quad for the item on the pending tree.
30     * Otherwise, returns undefined.
31     */
32    get quadIfPending() {
33      throw new Error('Not implemented');
34    },
35
36    /**
37     * If the selected item(s) is visible on the active tree in a way that
38     * should be highlighted, returns the quad for the item on the active tree.
39     * Otherwise, returns undefined.
40     */
41    get quadIfActive() {
42      throw new Error('Not implemented');
43    },
44
45    /**
46     * A stable string describing what is selected. Used to determine a stable
47     * color of the highlight quads for this selection.
48     */
49    get title() {
50      throw new Error('Not implemented');
51    },
52
53    /**
54     * Called when the selection is made active in the layer viewer. Must return
55     * an HTMLElement that explains this selection in detail.
56     */
57    createAnalysis: function() {
58      throw new Error('Not implemented');
59    },
60
61    /**
62     * Should try to create the equivalent selection in the provided LTHI,
63     * or undefined if it can't be done.
64     */
65    findEquivalent: function(lthi) {
66      throw new Error('Not implemented');
67    }
68  };
69
70  /**
71   * @constructor
72   */
73  function LayerSelection(layer) {
74    if (!layer)
75      throw new Error('Layer is required');
76    this.layer_ = layer;
77  }
78
79  LayerSelection.prototype = {
80    __proto__: Selection.prototype,
81
82    get associatedLayerId() {
83      return this.layer_.layerId;
84    },
85
86    get quadIfPending() {
87      return undefined;
88    },
89
90    get quadIfActive() {
91      return undefined;
92    },
93
94    createAnalysis: function() {
95      var dataView = new tracing.analysis.GenericObjectView();
96      dataView.object = this.layer_.args;
97      return dataView;
98    },
99
100    get title() {
101      return this.layer_.objectInstance.typeName;
102    },
103
104    findEquivalent: function(lthi) {
105      var layer = lthi.activeTree.findLayerWithId(this.layer_.layerId) ||
106          lthi.pendingTree.findLayerWithId(this.layer_.layerId);
107      if (!layer)
108        return undefined;
109      return new LayerSelection(layer);
110    }
111  };
112
113  /**
114   * @constructor
115   */
116  function TileSelection(tile) {
117    this.tile_ = tile;
118  }
119
120  TileSelection.prototype = {
121    __proto__: Selection.prototype,
122
123    get associatedLayerId() {
124      return this.tile_.layerId;
125    },
126
127    get quadIfPending() {
128      return this.tile_.args.pendingPriority.currentScreenQuad;
129    },
130
131    get quadIfActive() {
132      return this.tile_.args.activePriority.currentScreenQuad;
133    },
134
135    createAnalysis: function() {
136      var analysis = new tracing.analysis.GenericObjectView();
137      analysis.object = this.tile_.args;
138      return analysis;
139    },
140
141    get title() {
142      return this.tile_.objectInstance.typeName;
143    },
144
145    findEquivalent: function(lthi) {
146      var tileInstance = this.tile_.tileInstance;
147      if (lthi.ts < tileInstance.creationTs ||
148          lthi.ts >= tileInstance.deletionTs)
149        return undefined;
150      var tileSnapshot = tileInstance.getSnapshotAt(lthi.ts);
151      if (!tileSnapshot)
152        return undefined;
153      return new TileSelection(tileSnapshot);
154    }
155  };
156
157  /**
158   * @constructor
159   */
160  function RasterTaskSelection(rasterTask) {
161    this.rasterTask_ = rasterTask;
162  }
163
164  RasterTaskSelection.prototype = {
165    __proto__: Selection.prototype,
166
167    get tile() {
168      return this.rasterTask_.args.data.tile_id;
169    },
170
171    get associatedLayerId() {
172      return this.tile.layerId;
173    },
174
175    get quadIfPending() {
176      return this.tile.args.pendingPriority.currentScreenQuad;
177    },
178
179    get quadIfActive() {
180      return this.tile.args.activePriority.currentScreenQuad;
181    },
182
183    createAnalysis: function() {
184      var sel = tracing.createSelectionFromObjectAndView(
185          this.rasterTask_, this);
186      var analysis = new tracing.analysis.AnalysisResults();
187      tracing.analysis.analyzeSelection(analysis, sel);
188      return analysis;
189    },
190
191    get title() {
192      return this.rasterTask_.title;
193    },
194
195    findEquivalent: function(lthi) {
196      // Raster tasks are only valid in one LTHI.
197      return undefined;
198    }
199  };
200
201  return {
202    Selection: Selection,
203    LayerSelection: LayerSelection,
204    TileSelection: TileSelection,
205    RasterTaskSelection: RasterTaskSelection
206  };
207});
208