1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* Converts a number in bytes to a string in megabytes split by comma into
* three digit block.
* @param {number} bytes The number in bytes.
* @return {string} Formatted string in megabytes.
*/
function ToMegaByteString(bytes) {
var mb = Math.floor(bytes / (1 << 20));
return mb.toString().replace(
/\d+?(?=(\d{3})+$)/g, // Digit sequence (\d+) followed (?=) by 3n digits.
function(three_digit_block) { return three_digit_block + ','; }
);
}
/**
* Updates the Drive related Preferences section.
* @param {Array} preferences List of dictionaries describing preferences.
*/
function updateDriveRelatedPreferences(preferences) {
var ul = $('drive-related-preferences');
updateKeyValueList(ul, preferences);
}
/**
* Updates the Connection Status section.
* @param {Object} connStatus Dictionary containing connection status.
*/
function updateConnectionStatus(connStatus) {
$('connection-status').textContent = connStatus['status'];
$('has-refresh-token').textContent = connStatus['has-refresh-token'];
$('has-access-token').textContent = connStatus['has-access-token'];
}
/**
* Updates the Path Configurations section.
* @param {Array} paths List of dictionaries describing paths.
*/
function updatePathConfigurations(paths) {
var ul = $('path-configurations');
updateKeyValueList(ul, paths);
}
/**
* Updates the GCache Contents section.
* @param {Array} gcacheContents List of dictionaries describing metadata
* of files and directories under the GCache directory.
* @param {Object} gcacheSummary Dictionary of summary of GCache.
*/
function updateGCacheContents(gcacheContents, gcacheSummary) {
var tbody = $('gcache-contents');
for (var i = 0; i < gcacheContents.length; i++) {
var entry = gcacheContents[i];
var tr = document.createElement('tr');
// Add some suffix based on the type.
var path = entry.path;
if (entry.is_directory)
path += '/';
else if (entry.is_symbolic_link)
path += '@';
tr.appendChild(createElementFromText('td', path));
tr.appendChild(createElementFromText('td', entry.size));
tr.appendChild(createElementFromText('td', entry.last_modified));
tr.appendChild(createElementFromText('td', entry.permission));
tbody.appendChild(tr);
}
$('gcache-summary-total-size').textContent =
ToMegaByteString(gcacheSummary['total_size']);
}
/**
* Updates the File System Contents section. The function is called from the
* C++ side repeatedly with contents of a directory.
* @param {string} directoryContentsAsText Pre-formatted string representation
* of contents a directory in the file system.
*/
function updateFileSystemContents(directoryContentsAsText) {
var div = $('file-system-contents');
div.appendChild(createElementFromText('pre', directoryContentsAsText));
}
/**
* Updates the Cache Contents section.
* @param {Object} cacheEntry Dictionary describing a cache entry.
* The function is called from the C++ side repeatedly.
*/
function updateCacheContents(cacheEntry) {
var tr = document.createElement('tr');
tr.appendChild(createElementFromText('td', cacheEntry.local_id));
tr.appendChild(createElementFromText('td', cacheEntry.md5));
tr.appendChild(createElementFromText('td', cacheEntry.is_present));
tr.appendChild(createElementFromText('td', cacheEntry.is_pinned));
tr.appendChild(createElementFromText('td', cacheEntry.is_dirty));
$('cache-contents').appendChild(tr);
}
/**
* Updates the Local Storage summary.
* @param {Object} localStorageSummary Dictionary describing the status of local
* stogage.
*/
function updateLocalStorageUsage(localStorageSummary) {
var freeSpaceInMB = ToMegaByteString(localStorageSummary.free_space);
$('local-storage-freespace').innerText = freeSpaceInMB;
}
/**
* Updates the summary about in-flight operations.
* @param {Array} inFlightOperations List of dictionaries describing the status
* of in-flight operations.
*/
function updateInFlightOperations(inFlightOperations) {
var container = $('in-flight-operations-contents');
// Reset the table. Remove children in reverse order. Otherwides each
// existingNodes[i] changes as a side effect of removeChild.
var existingNodes = container.childNodes;
for (var i = existingNodes.length - 1; i >= 0; i--) {
var node = existingNodes[i];
if (node.className == 'in-flight-operation')
container.removeChild(node);
}
// Add in-flight operations.
for (var i = 0; i < inFlightOperations.length; i++) {
var operation = inFlightOperations[i];
var tr = document.createElement('tr');
tr.className = 'in-flight-operation';
tr.appendChild(createElementFromText('td', operation.id));
tr.appendChild(createElementFromText('td', operation.type));
tr.appendChild(createElementFromText('td', operation.file_path));
tr.appendChild(createElementFromText('td', operation.state));
var progress = operation.progress_current + '/' + operation.progress_total;
if (operation.progress_total > 0) {
var percent = operation.progress_current / operation.progress_total * 100;
progress += ' (' + Math.round(percent) + '%)';
}
tr.appendChild(createElementFromText('td', progress));
container.appendChild(tr);
}
}
/**
* Updates the summary about about resource.
* @param {Object} aboutResource Dictionary describing about resource.
*/
function updateAboutResource(aboutResource) {
var quotaTotalInMb = ToMegaByteString(aboutResource['account-quota-total']);
var quotaUsedInMb = ToMegaByteString(aboutResource['account-quota-used']);
$('account-quota-info').textContent =
quotaUsedInMb + ' / ' + quotaTotalInMb + ' (MB)';
$('account-largest-changestamp-remote').textContent =
aboutResource['account-largest-changestamp-remote'];
$('root-resource-id').textContent = aboutResource['root-resource-id'];
}
/**
* Updates the summary about app list.
* @param {Object} appList Dictionary describing app list.
*/
function updateAppList(appList) {
$('app-list-etag').textContent = appList['etag'];
var itemContainer = $('app-list-items');
for (var i = 0; i < appList['items'].length; i++) {
var app = appList['items'][i];
var tr = document.createElement('tr');
tr.className = 'installed-app';
tr.appendChild(createElementFromText('td', app.name));
tr.appendChild(createElementFromText('td', app.application_id));
tr.appendChild(createElementFromText('td', app.object_type));
tr.appendChild(createElementFromText('td', app.supports_create));
itemContainer.appendChild(tr);
}
}
/**
* Updates the local cache information about account metadata.
* @param {Object} localMetadata Dictionary describing account metadata.
*/
function updateLocalMetadata(localMetadata) {
var changestamp = localMetadata['account-largest-changestamp-local'];
$('account-largest-changestamp-local').textContent =
changestamp.toString() +
(changestamp > 0 ? ' (loaded)' : ' (not loaded)') +
(localMetadata['account-metadata-refreshing'] ? ' (refreshing)' : '');
}
/**
* Updates the summary about delta update status.
* @param {Object} deltaUpdateStatus Dictionary describing delta update status.
*/
function updateDeltaUpdateStatus(deltaUpdateStatus) {
$('push-notification-enabled').textContent =
deltaUpdateStatus['push-notification-enabled'];
$('last-update-check-time').textContent =
deltaUpdateStatus['last-update-check-time'];
$('last-update-check-error').textContent =
deltaUpdateStatus['last-update-check-error'];
}
/**
* Updates the event log section.
* @param {Array} log Array of events.
*/
function updateEventLog(log) {
var ul = $('event-log');
updateKeyValueList(ul, log);
}
/**
* Creates an element named |elementName| containing the content |text|.
* @param {string} elementName Name of the new element to be created.
* @param {string} text Text to be contained in the new element.
* @return {HTMLElement} The newly created HTML element.
*/
function createElementFromText(elementName, text) {
var element = document.createElement(elementName);
element.appendChild(document.createTextNode(text));
return element;
}
/**
* Updates <ul> element with the given key-value list.
* @param {HTMLElement} ul <ul> element to be modified.
* @param {Array} list List of dictionaries containing 'key', 'value' (optional)
* and 'class' (optional). For each element <li> element with specified class is
* created.
*/
function updateKeyValueList(ul, list) {
for (var i = 0; i < list.length; i++) {
var item = list[i];
var text = item.key;
if (item.value != '')
text += ': ' + item.value;
var li = createElementFromText('li', text);
if (item.class)
li.classList.add(item.class);
ul.appendChild(li);
}
}
/**
* Updates the text next to the 'reset' button to update the status.
* @param {boolean} success whether or not resetting has succeeded.
*/
function updateResetStatus(success) {
$('reset-status-text').textContent = (success ? 'success' : 'failed');
}
document.addEventListener('DOMContentLoaded', function() {
chrome.send('pageLoaded');
// Update the table of contents.
var toc = $('toc');
var sections = document.getElementsByTagName('h2');
for (var i = 0; i < sections.length; i++) {
var section = sections[i];
var a = createElementFromText('a', section.textContent);
a.href = '#' + section.id;
var li = document.createElement('li');
li.appendChild(a);
toc.appendChild(li);
}
$('button-clear-access-token').addEventListener('click', function() {
chrome.send('clearAccessToken');
});
$('button-clear-refresh-token').addEventListener('click', function() {
chrome.send('clearRefreshToken');
});
$('button-reset-drive-filesystem').addEventListener('click', function() {
$('reset-status-text').textContent = 'resetting...';
chrome.send('resetDriveFileSystem');
});
$('button-show-file-entries').addEventListener('click', function() {
var button = $('button-show-file-entries');
button.parentNode.removeChild(button);
chrome.send('listFileEntries');
});
window.setInterval(function() {
chrome.send('periodicUpdate');
}, 1000);
});
|