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 7 base.requireStylesheet('tracing.analysis.generic_object_view'); 8 9 base.require('base.utils'); 10 base.require('tracing.analysis.analysis_link'); 11 base.require('ui'); 12 13 base.exportTo('tracing.analysis', function() { 14 15 /** 16 * @constructor 17 */ 18 var GenericObjectView = ui.define('x-generic-object-view'); 19 20 GenericObjectView.prototype = { 21 __proto__: HTMLUnknownElement.prototype, 22 23 decorate: function() { 24 this.object_ = undefined; 25 }, 26 27 get object() { 28 return this.object_; 29 }, 30 31 set object(object) { 32 this.object_ = object; 33 this.updateContents_(); 34 }, 35 36 updateContents_: function() { 37 this.textContent = ''; 38 39 this.appendElementsForType_('', this.object_, 0, 0, 5, ''); 40 }, 41 42 appendElementsForType_: function( 43 label, object, indent, depth, maxDepth, suffix) { 44 if (depth > maxDepth) { 45 this.appendSimpleText_( 46 label, indent, '<recursion limit reached>', suffix); 47 return; 48 } 49 50 if (object === undefined) { 51 this.appendSimpleText_(label, indent, 'undefined', suffix); 52 return; 53 } 54 55 if (object === null) { 56 this.appendSimpleText_(label, indent, 'null', suffix); 57 return; 58 } 59 60 if (!(object instanceof Object)) { 61 var type = typeof object; 62 if (type == 'string') { 63 this.appendSimpleText_(label, indent, '"' + object + '"', suffix); 64 } else { 65 this.appendSimpleText_(label, indent, object, suffix); 66 } 67 return; 68 } 69 70 if (object instanceof tracing.trace_model.ObjectSnapshot) { 71 var link = new tracing.analysis.ObjectSnapshotLink(object); 72 link.objectSnapshot = object; 73 this.appendElementWithLabel_(label, indent, link, suffix); 74 return; 75 } 76 77 if (object instanceof tracing.trace_model.ObjectInstance) { 78 var link = new tracing.analysis.ObjectInstanceLink(object); 79 link.objectInstance = object; 80 this.appendElementWithLabel_(label, indent, link, suffix); 81 return; 82 } 83 84 if (object instanceof Array) { 85 this.appendElementsForArray_( 86 label, object, indent, depth, maxDepth, suffix); 87 return; 88 } 89 90 this.appendElementsForObject_( 91 label, object, indent, depth, maxDepth, suffix); 92 }, 93 94 appendElementsForArray_: function( 95 label, object, indent, depth, maxDepth, suffix) { 96 if (object.length == 0) { 97 this.appendSimpleText_(label, indent, '[]', suffix); 98 return; 99 } 100 101 this.appendElementsForType_( 102 label + '[', 103 object[0], 104 indent, depth + 1, maxDepth, 105 object.length > 1 ? ',' : ']' + suffix); 106 for (var i = 1; i < object.length; i++) { 107 this.appendElementsForType_( 108 '', 109 object[i], 110 indent + label.length + 1, depth + 1, maxDepth, 111 i < object.length - 1 ? ',' : ']' + suffix); 112 } 113 return; 114 }, 115 116 appendElementsForObject_: function( 117 label, object, indent, depth, maxDepth, suffix) { 118 var keys = base.dictionaryKeys(object); 119 if (keys.length == 0) { 120 this.appendSimpleText_(label, indent, '{}', suffix); 121 return; 122 } 123 124 this.appendElementsForType_( 125 label + '{' + keys[0] + ': ', 126 object[keys[0]], 127 indent, depth, maxDepth, 128 keys.length > 1 ? ',' : '}' + suffix); 129 for (var i = 1; i < keys.length; i++) { 130 this.appendElementsForType_( 131 keys[i] + ': ', 132 object[keys[i]], 133 indent + label.length + 1, depth + 1, maxDepth, 134 i < keys.length - 1 ? ',' : '}' + suffix); 135 } 136 }, 137 138 appendElementWithLabel_: function(label, indent, dataElement, suffix) { 139 var row = document.createElement('div'); 140 141 var indentSpan = document.createElement('span'); 142 indentSpan.style.whiteSpace = 'pre'; 143 for (var i = 0; i < indent; i++) 144 indentSpan.textContent += ' '; 145 row.appendChild(indentSpan); 146 147 var labelSpan = document.createElement('span'); 148 labelSpan.textContent = label; 149 row.appendChild(labelSpan); 150 151 row.appendChild(dataElement); 152 var suffixSpan = document.createElement('span'); 153 suffixSpan.textContent = suffix; 154 row.appendChild(suffixSpan); 155 156 row.dataElement = dataElement; 157 this.appendChild(row); 158 }, 159 160 appendSimpleText_: function(label, indent, text, suffix) { 161 var el = this.ownerDocument.createElement('span'); 162 el.textContent = text; 163 this.appendElementWithLabel_(label, indent, el, suffix); 164 return el; 165 } 166 167 }; 168 169 return { 170 GenericObjectView: GenericObjectView 171 }; 172 }); 173