1<!DOCTYPE html> 2<!-- 3Copyright (c) 2013 The Chromium Authors. All rights reserved. 4Use of this source code is governed by a BSD-style license that can be 5found in the LICENSE file. 6--> 7 8<link rel="import" href="/tracing/base/quad.html"> 9<link rel="import" href="/tracing/base/rect.html"> 10<link rel="import" href="/tracing/model/object_instance.html"> 11<script> 12 13'use strict'; 14 15tr.exportTo('tr.e.cc', function() { 16 var convertedNameCache = {}; 17 function convertNameToJSConvention(name) { 18 if (name in convertedNameCache) 19 return convertedNameCache[name]; 20 21 if (name[0] == '_' || 22 name[name.length - 1] == '_') { 23 convertedNameCache[name] = name; 24 return name; 25 } 26 27 var words = name.split('_'); 28 if (words.length == 1) { 29 convertedNameCache[name] = words[0]; 30 return words[0]; 31 } 32 33 for (var i = 1; i < words.length; i++) 34 words[i] = words[i][0].toUpperCase() + words[i].substring(1); 35 36 convertedNameCache[name] = words.join(''); 37 return convertedNameCache[name]; 38 } 39 40 function convertObjectFieldNamesToJSConventions(object) { 41 tr.b.iterObjectFieldsRecursively( 42 object, 43 function(object, fieldName, fieldValue) { 44 delete object[fieldName]; 45 object[newFieldName] = fieldValue; 46 return newFieldName; 47 }); 48 } 49 50 function convertQuadSuffixedTypesToQuads(object) { 51 tr.b.iterObjectFieldsRecursively( 52 object, 53 function(object, fieldName, fieldValue) { 54 }); 55 } 56 57 function convertObject(object) { 58 convertObjectFieldNamesToJSConventions(object); 59 convertQuadSuffixedTypesToQuads(object); 60 } 61 62 function moveRequiredFieldsFromArgsToToplevel(object, fields) { 63 for (var i = 0; i < fields.length; i++) { 64 var key = fields[i]; 65 if (object.args[key] === undefined) 66 throw Error('Expected field ' + key + ' not found in args'); 67 if (object[key] !== undefined) 68 throw Error('Field ' + key + ' already in object'); 69 object[key] = object.args[key]; 70 delete object.args[key]; 71 } 72 } 73 74 function moveOptionalFieldsFromArgsToToplevel(object, fields) { 75 for (var i = 0; i < fields.length; i++) { 76 var key = fields[i]; 77 if (object.args[key] === undefined) 78 continue; 79 if (object[key] !== undefined) 80 throw Error('Field ' + key + ' already in object'); 81 object[key] = object.args[key]; 82 delete object.args[key]; 83 } 84 } 85 86 function preInitializeObject(object) { 87 preInitializeObjectInner(object.args, false); 88 } 89 90 function preInitializeObjectInner(object, hasRecursed) { 91 if (!(object instanceof Object)) 92 return; 93 94 if (object instanceof Array) { 95 for (var i = 0; i < object.length; i++) 96 preInitializeObjectInner(object[i], true); 97 return; 98 } 99 100 if (hasRecursed && 101 (object instanceof tr.model.ObjectSnapshot || 102 object instanceof tr.model.ObjectInstance)) 103 return; 104 105 for (var key in object) { 106 var newKey = convertNameToJSConvention(key); 107 if (newKey != key) { 108 var value = object[key]; 109 delete object[key]; 110 object[newKey] = value; 111 key = newKey; 112 } 113 114 // Convert objects with keys ending with Quad to tr.b.Quad type. 115 if (/Quad$/.test(key) && !(object[key] instanceof tr.b.Quad)) { 116 var q; 117 try { 118 q = tr.b.Quad.from8Array(object[key]); 119 } catch (e) { 120 console.log(e); 121 } 122 object[key] = q; 123 continue; 124 } 125 126 // Convert objects with keys ending with Rect to tr.b.Rect type. 127 if (/Rect$/.test(key) && !(object[key] instanceof tr.b.Rect)) { 128 var r; 129 try { 130 r = tr.b.Rect.fromArray(object[key]); 131 } catch (e) { 132 console.log(e); 133 } 134 object[key] = r; 135 } 136 137 preInitializeObjectInner(object[key], true); 138 } 139 } 140 141 function bytesToRoundedMegabytes(bytes) { 142 return Math.round(bytes / 100000.0) / 10.0; 143 } 144 145 return { 146 preInitializeObject: preInitializeObject, 147 convertNameToJSConvention: convertNameToJSConvention, 148 moveRequiredFieldsFromArgsToToplevel: moveRequiredFieldsFromArgsToToplevel, 149 moveOptionalFieldsFromArgsToToplevel: moveOptionalFieldsFromArgsToToplevel, 150 bytesToRoundedMegabytes: bytesToRoundedMegabytes 151 }; 152}); 153</script> 154