Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / analysis / generic_object_view.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="stylesheet" href="/tracing/analysis/generic_object_view.css">
9
10 <link rel="import" href="/tracing/analysis/analysis_link.html">
11 <link rel="import" href="/base/utils.html">
12 <link rel="import" href="/base/ui.html">
13
14 <script>
15 'use strict';
16
17 tv.exportTo('tracing.analysis', function() {
18   /**
19    * @constructor
20    */
21   var GenericObjectView = tv.ui.define('x-generic-object-view');
22
23   GenericObjectView.prototype = {
24     __proto__: HTMLUnknownElement.prototype,
25
26     decorate: function() {
27       this.object_ = undefined;
28     },
29
30     get object() {
31       return this.object_;
32     },
33
34     set object(object) {
35       this.object_ = object;
36       this.updateContents_();
37     },
38
39     updateContents_: function() {
40       this.textContent = '';
41
42       this.appendElementsForType_('', this.object_, 0, 0, 5, '');
43     },
44
45     appendElementsForType_: function(
46         label, object, indent, depth, maxDepth, suffix) {
47       if (depth > maxDepth) {
48         this.appendSimpleText_(
49             label, indent, '<recursion limit reached>', suffix);
50         return;
51       }
52
53       if (object === undefined) {
54         this.appendSimpleText_(label, indent, 'undefined', suffix);
55         return;
56       }
57
58       if (object === null) {
59         this.appendSimpleText_(label, indent, 'null', suffix);
60         return;
61       }
62
63       if (!(object instanceof Object)) {
64         var type = typeof object;
65         if (type == 'string') {
66           var objectReplaced = false;
67           if ((object[0] == '{' && object[object.length - 1] == '}') ||
68               (object[0] == '[' && object[object.length - 1] == ']')) {
69             try {
70               object = JSON.parse(object);
71               objectReplaced = true;
72             } catch (e) {
73             }
74           }
75           if (!objectReplaced)
76             return this.appendSimpleText_(
77                 label, indent, '"' + object + '"', suffix);
78           else {
79             /* Fall through to the flow below */
80           }
81         } else {
82           return this.appendSimpleText_(label, indent, object, suffix);
83         }
84       }
85
86       if (object instanceof tracing.trace_model.ObjectSnapshot) {
87         var link = new tracing.analysis.ObjectSnapshotLink(object);
88         link.objectSnapshot = object;
89         this.appendElementWithLabel_(label, indent, link, suffix);
90         return;
91       }
92
93       if (object instanceof tracing.trace_model.ObjectInstance) {
94         var link = new tracing.analysis.ObjectInstanceLink(object);
95         link.objectInstance = object;
96         this.appendElementWithLabel_(label, indent, link, suffix);
97         return;
98       }
99
100       if (object instanceof tv.Rect) {
101         this.appendSimpleText_(label, indent, object.toString(), suffix);
102         return;
103       }
104
105       if (object instanceof Array) {
106         this.appendElementsForArray_(
107             label, object, indent, depth, maxDepth, suffix);
108         return;
109       }
110
111       this.appendElementsForObject_(
112           label, object, indent, depth, maxDepth, suffix);
113     },
114
115     appendElementsForArray_: function(
116         label, object, indent, depth, maxDepth, suffix) {
117       if (object.length == 0) {
118         this.appendSimpleText_(label, indent, '[]', suffix);
119         return;
120       }
121
122       this.appendElementsForType_(
123           label + '[',
124           object[0],
125           indent, depth + 1, maxDepth,
126           object.length > 1 ? ',' : ']' + suffix);
127       for (var i = 1; i < object.length; i++) {
128         this.appendElementsForType_(
129             '',
130             object[i],
131             indent + label.length + 1, depth + 1, maxDepth,
132             i < object.length - 1 ? ',' : ']' + suffix);
133       }
134       return;
135     },
136
137     appendElementsForObject_: function(
138         label, object, indent, depth, maxDepth, suffix) {
139       var keys = tv.dictionaryKeys(object);
140       if (keys.length == 0) {
141         this.appendSimpleText_(label, indent, '{}', suffix);
142         return;
143       }
144
145       this.appendElementsForType_(
146           label + '{' + keys[0] + ': ',
147           object[keys[0]],
148           indent, depth, maxDepth,
149           keys.length > 1 ? ',' : '}' + suffix);
150       for (var i = 1; i < keys.length; i++) {
151         this.appendElementsForType_(
152             keys[i] + ': ',
153             object[keys[i]],
154             indent + label.length + 1, depth + 1, maxDepth,
155             i < keys.length - 1 ? ',' : '}' + suffix);
156       }
157     },
158
159     appendElementWithLabel_: function(label, indent, dataElement, suffix) {
160       var row = document.createElement('div');
161
162       var indentSpan = document.createElement('span');
163       indentSpan.style.whiteSpace = 'pre';
164       for (var i = 0; i < indent; i++)
165         indentSpan.textContent += ' ';
166       row.appendChild(indentSpan);
167
168       var labelSpan = document.createElement('span');
169       labelSpan.textContent = label;
170       row.appendChild(labelSpan);
171
172       row.appendChild(dataElement);
173       var suffixSpan = document.createElement('span');
174       suffixSpan.textContent = suffix;
175       row.appendChild(suffixSpan);
176
177       row.dataElement = dataElement;
178       this.appendChild(row);
179     },
180
181     appendSimpleText_: function(label, indent, text, suffix) {
182       var el = this.ownerDocument.createElement('span');
183       el.textContent = text;
184       this.appendElementWithLabel_(label, indent, el, suffix);
185       return el;
186     }
187
188   };
189
190   /**
191    * @constructor
192    */
193   var GenericObjectViewWithLabel = tv.ui.define(
194       'x-generic-object-view-with-label');
195
196   GenericObjectViewWithLabel.prototype = {
197     __proto__: HTMLUnknownElement.prototype,
198
199     decorate: function() {
200       this.labelEl_ = document.createElement('div');
201       this.genericObjectView_ = new tracing.analysis.GenericObjectView();
202       this.appendChild(this.labelEl_);
203       this.appendChild(this.genericObjectView_);
204     },
205
206     get label() {
207       return this.labelEl_.textContent;
208     },
209
210     set label(label) {
211       this.labelEl_.textContent = label;
212     },
213
214     get object() {
215       return this.genericObjectView_.object;
216     },
217
218     set object(object) {
219       this.genericObjectView_.object = object;
220     }
221   };
222
223   return {
224     GenericObjectView: GenericObjectView,
225     GenericObjectViewWithLabel: GenericObjectViewWithLabel
226   };
227 });
228 </script>