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
3 // found in the LICENSE file.
6 var dumpToTextButton = $('dump-to-text');
7 var dataDump = $('data-dump');
8 dumpToTextButton.addEventListener('click', function(event) {
9 // TODO(akalin): Add info like Chrome version, OS, date dumped, etc.
15 data += JSON.stringify(chrome.sync.aboutInfo, null, 2);
19 data += '=============\n';
20 data += 'Notifications\n';
21 data += '=============\n';
22 data += JSON.stringify(chrome.sync.notifications, null, 2);
29 data += JSON.stringify(chrome.sync.log.entries, null, 2);
32 dataDump.textContent = data;
38 'IS_UNAPPLIED_UPDATE',
42 'SERVER_VERSION_TIME',
52 function versionToDateString(version) {
53 // TODO(mmontgomery): ugly? Hacky? Is there a better way?
54 var epochLength = Date.now().toString().length;
55 var epochTime = parseInt(version.slice(0, epochLength));
56 var date = new Date(epochTime);
57 return date.toString();
60 function getFields(node) {
61 return allFields.map(function(field) {
63 if (field == 'SERVER_VERSION_TIME') {
64 var version = node['SERVER_VERSION'];
65 fieldVal = versionToDateString(version);
66 } if (field == 'BASE_VERSION_TIME') {
67 var version = node['BASE_VERSION'];
68 fieldVal = versionToDateString(version);
69 } else if ((field == 'SERVER_SPECIFICS' || field == 'SPECIFICS') &&
70 $('include-specifics').checked) {
71 fieldVal = JSON.stringify(node[field]);
73 fieldVal = node[field];
79 function isSelectedDatatype(node) {
80 var type = node.serverModelType;
81 var typeCheckbox = $(type);
82 // Some types, such as 'Top level folder', appear in the list of nodes
83 // but not in the list of selectable items.
84 if (typeCheckbox == null) {
87 return typeCheckbox.checked;
90 function makeBlobUrl(data) {
91 var textBlob = new Blob([data], {type: 'octet/stream'});
92 var blobUrl = window.webkitURL.createObjectURL(textBlob);
96 function makeDownloadName() {
97 // Format is sync-data-dump-$epoch-$year-$month-$day-$OS.csv.
99 var friendlyDate = [now.getFullYear(),
101 now.getDate()].join('-');
102 var name = ['sync-data-dump',
105 navigator.platform].join('-');
106 return [name, 'csv'].join('.');
109 function makeDateUserAgentHeader() {
110 var now = new Date();
111 var userAgent = window.navigator.userAgent;
112 var dateUaHeader = [now.toISOString(), userAgent].join(',');
116 function triggerDataDownload(data) {
117 // Prepend a header with ISO date and useragent.
118 var output = [makeDateUserAgentHeader()];
119 output.push('=====');
121 var aboutInfo = JSON.stringify(chrome.sync.aboutInfo, null, 2);
122 output.push(aboutInfo);
124 if (data != null && data.length > 0) {
125 output.push('=====');
126 var fieldLabels = allFields.join(',');
127 output.push(fieldLabels);
129 var data = data.filter(isSelectedDatatype);
130 data = data.map(getFields);
131 var dataAsString = data.join('\n');
132 output.push(dataAsString);
134 output = output.join('\n');
136 var anchor = $('dump-to-file-anchor');
137 anchor.href = makeBlobUrl(output);
138 anchor.download = makeDownloadName();
142 function createTypesCheckboxes(types) {
143 var containerElt = $('node-type-checkboxes');
145 types.map(function(type) {
146 var div = document.createElement('div');
148 var checkbox = document.createElement('input');
150 checkbox.type = 'checkbox';
151 checkbox.checked = 'yes';
152 div.appendChild(checkbox);
154 var label = document.createElement('label');
155 // Assigning to label.for doesn't work.
156 label.setAttribute('for', type);
157 label.innerText = type;
158 div.appendChild(label);
160 containerElt.appendChild(div);
164 function populateDatatypes(childNodeSummaries) {
165 var types = childNodeSummaries.map(function(n) {
168 types = types.sort();
169 createTypesCheckboxes(types);
172 document.addEventListener('DOMContentLoaded', function() {
173 chrome.sync.getRootNodeDetails(function(rootNode) {
174 chrome.sync.getChildNodeIds(rootNode.id, function(childNodeIds) {
175 chrome.sync.getNodeSummariesById(childNodeIds, populateDatatypes);
180 var dumpToFileLink = $('dump-to-file');
181 dumpToFileLink.addEventListener('click', function(event) {
182 chrome.sync.getAllNodes(triggerDataDownload);