OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function() { | 5 (function() { |
6 var dumpToTextButton = $('dump-to-text'); | 6 var dumpToTextButton = $('dump-to-text'); |
7 var dataDump = $('data-dump'); | 7 var dataDump = $('data-dump'); |
8 dumpToTextButton.addEventListener('click', function(event) { | 8 dumpToTextButton.addEventListener('click', function(event) { |
9 // TODO(akalin): Add info like Chrome version, OS, date dumped, etc. | 9 // TODO(akalin): Add info like Chrome version, OS, date dumped, etc. |
10 | 10 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 ]; | 50 ]; |
51 | 51 |
52 function versionToDateString(version) { | 52 function versionToDateString(version) { |
53 // TODO(mmontgomery): ugly? Hacky? Is there a better way? | 53 // TODO(mmontgomery): ugly? Hacky? Is there a better way? |
54 var epochLength = Date.now().toString().length; | 54 var epochLength = Date.now().toString().length; |
55 var epochTime = parseInt(version.slice(0, epochLength)); | 55 var epochTime = parseInt(version.slice(0, epochLength)); |
56 var date = new Date(epochTime); | 56 var date = new Date(epochTime); |
57 return date.toString(); | 57 return date.toString(); |
58 } | 58 } |
59 | 59 |
60 function getFields(node) { | 60 /** |
| 61 * @param {!Object} node A JavaScript represenation of a sync entity. |
| 62 * @return {string} A string representation of the sync entity. |
| 63 */ |
| 64 function serializeNode(node) { |
61 return allFields.map(function(field) { | 65 return allFields.map(function(field) { |
62 var fieldVal; | 66 var fieldVal; |
63 if (field == 'SERVER_VERSION_TIME') { | 67 if (field == 'SERVER_VERSION_TIME') { |
64 var version = node['SERVER_VERSION']; | 68 var version = node['SERVER_VERSION']; |
65 fieldVal = versionToDateString(version); | 69 fieldVal = versionToDateString(version); |
66 } if (field == 'BASE_VERSION_TIME') { | 70 } if (field == 'BASE_VERSION_TIME') { |
67 var version = node['BASE_VERSION']; | 71 var version = node['BASE_VERSION']; |
68 fieldVal = versionToDateString(version); | 72 fieldVal = versionToDateString(version); |
69 } else if ((field == 'SERVER_SPECIFICS' || field == 'SPECIFICS') && | 73 } else if ((field == 'SERVER_SPECIFICS' || field == 'SPECIFICS') && |
70 (!$('include-specifics').checked)) { | 74 (!$('include-specifics').checked)) { |
71 fieldVal = 'REDACTED'; | 75 fieldVal = 'REDACTED'; |
72 } else if ((field == 'SERVER_SPECIFICS' || field == 'SPECIFICS') && | 76 } else if ((field == 'SERVER_SPECIFICS' || field == 'SPECIFICS') && |
73 $('include-specifics').checked) { | 77 $('include-specifics').checked) { |
74 fieldVal = JSON.stringify(node[field]); | 78 fieldVal = JSON.stringify(node[field]); |
75 } else { | 79 } else { |
76 fieldVal = node[field]; | 80 fieldVal = node[field]; |
77 } | 81 } |
78 return fieldVal; | 82 return fieldVal; |
79 }); | 83 }); |
80 } | 84 } |
81 | 85 |
82 function isSelectedDatatype(node) { | 86 /** |
83 var type = node.serverModelType; | 87 * @param {string} type The name of a sync model type. |
| 88 * @return {boolean} True if the type's checkbox is selected. |
| 89 */ |
| 90 function isSelectedDatatype(type) { |
84 var typeCheckbox = $(type); | 91 var typeCheckbox = $(type); |
85 // Some types, such as 'Top level folder', appear in the list of nodes | 92 // Some types, such as 'Top level folder', appear in the list of nodes |
86 // but not in the list of selectable items. | 93 // but not in the list of selectable items. |
87 if (typeCheckbox == null) { | 94 if (typeCheckbox == null) { |
88 return false; | 95 return false; |
89 } | 96 } |
90 return typeCheckbox.checked; | 97 return typeCheckbox.checked; |
91 } | 98 } |
92 | 99 |
93 function makeBlobUrl(data) { | 100 function makeBlobUrl(data) { |
(...skipping 15 matching lines...) Expand all Loading... |
109 return [name, 'csv'].join('.'); | 116 return [name, 'csv'].join('.'); |
110 } | 117 } |
111 | 118 |
112 function makeDateUserAgentHeader() { | 119 function makeDateUserAgentHeader() { |
113 var now = new Date(); | 120 var now = new Date(); |
114 var userAgent = window.navigator.userAgent; | 121 var userAgent = window.navigator.userAgent; |
115 var dateUaHeader = [now.toISOString(), userAgent].join(','); | 122 var dateUaHeader = [now.toISOString(), userAgent].join(','); |
116 return dateUaHeader; | 123 return dateUaHeader; |
117 } | 124 } |
118 | 125 |
119 function triggerDataDownload(data) { | 126 /** |
| 127 * Builds a summary of current state and exports it as a downloaded file. |
| 128 * |
| 129 * @param {!Array.<{type: string, nodes: !Array<!Object>}>} nodesMap |
| 130 * Summary of local state by model type. |
| 131 */ |
| 132 function triggerDataDownload(nodesMap) { |
120 // Prepend a header with ISO date and useragent. | 133 // Prepend a header with ISO date and useragent. |
121 var output = [makeDateUserAgentHeader()]; | 134 var output = [makeDateUserAgentHeader()]; |
122 output.push('====='); | 135 output.push('====='); |
123 | 136 |
124 var aboutInfo = JSON.stringify(chrome.sync.aboutInfo, null, 2); | 137 var aboutInfo = JSON.stringify(chrome.sync.aboutInfo, null, 2); |
125 output.push(aboutInfo); | 138 output.push(aboutInfo); |
126 | 139 |
127 if (data != null && data.length > 0) { | 140 // Filter out non-selected types. |
| 141 var selectedTypesNodes = nodesMap.filter(function(x) { |
| 142 return isSelectedDatatype(x.type); |
| 143 }); |
| 144 |
| 145 // Serialize the remaining nodes and add them to the output. |
| 146 selectedTypesNodes.forEach(function(typeNodes) { |
128 output.push('====='); | 147 output.push('====='); |
129 var fieldLabels = allFields.join(','); | 148 output.push(typeNodes.nodes.map(serializeNode).join('\n')); |
130 output.push(fieldLabels); | 149 }); |
131 | 150 |
132 var data = data.filter(isSelectedDatatype); | |
133 data = data.map(getFields); | |
134 var dataAsString = data.join('\n'); | |
135 output.push(dataAsString); | |
136 } | |
137 output = output.join('\n'); | 151 output = output.join('\n'); |
138 | 152 |
139 var anchor = $('dump-to-file-anchor'); | 153 var anchor = $('dump-to-file-anchor'); |
140 anchor.href = makeBlobUrl(output); | 154 anchor.href = makeBlobUrl(output); |
141 anchor.download = makeDownloadName(); | 155 anchor.download = makeDownloadName(); |
142 anchor.click(); | 156 anchor.click(); |
143 } | 157 } |
144 | 158 |
145 function createTypesCheckboxes(types) { | 159 function createTypesCheckboxes(types) { |
146 var containerElt = $('node-type-checkboxes'); | 160 var containerElt = $('node-type-checkboxes'); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 'onReceivedListOfTypes', | 192 'onReceivedListOfTypes', |
179 onReceivedListOfTypes); | 193 onReceivedListOfTypes); |
180 chrome.sync.requestListOfTypes(); | 194 chrome.sync.requestListOfTypes(); |
181 }); | 195 }); |
182 | 196 |
183 var dumpToFileLink = $('dump-to-file'); | 197 var dumpToFileLink = $('dump-to-file'); |
184 dumpToFileLink.addEventListener('click', function(event) { | 198 dumpToFileLink.addEventListener('click', function(event) { |
185 chrome.sync.getAllNodes(triggerDataDownload); | 199 chrome.sync.getAllNodes(triggerDataDownload); |
186 }); | 200 }); |
187 })(); | 201 })(); |
OLD | NEW |