File: drive_internals.js

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (475 lines) | stat: -rw-r--r-- 15,860 bytes parent folder | download | duplicates (5)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Copyright 2012 The Chromium Authors
// 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) {
  const 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) {
  const 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'];
}

/**
 * Updates the Path Configurations section.
 * @param {Array} paths List of dictionaries describing paths.
 */
function updatePathConfigurations(paths) {
  const 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) {
  const tbody = $('gcache-contents');
  for (let i = 0; i < gcacheContents.length; i++) {
    const entry = gcacheContents[i];
    const tr = document.createElement('tr');

    // Add some suffix based on the type.
    let 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 Cache Contents section.
 * @param {Object} cacheEntry Dictionary describing a cache entry.
 * The function is called from the C++ side repeatedly.
 */
function updateCacheContents(cacheEntry) {
  const 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);
}

function updateBulkPinningVisible(enabled) {
  $('bulk-pinning-visible').checked = enabled;
}

function updateVerboseLogging(enabled) {
  $('verbose-logging-toggle').checked = enabled;
}

function updateMirroring(enabled) {
  $('mirroring-toggle').checked = enabled;
}

function updateBulkPinning(enabled) {
  $('bulk-pinning-toggle').checked = enabled;
}

function onBulkPinningProgress(progress) {
  updateBulkPinning(progress.enabled);
  $('bulk-pinning-stage').innerText = progress.stage;
  $('bulk-pinning-free-space').innerText = progress.free_space;
  $('bulk-pinning-required-space').innerText = progress.required_space;
  $('bulk-pinning-bytes-to-pin').innerText = progress.bytes_to_pin;
  $('bulk-pinning-pinned-bytes').innerText = progress.pinned_bytes;
  $('bulk-pinning-pinned-bytes-percent').innerText =
      progress.pinned_bytes_percent;
  $('bulk-pinning-files-to-pin').innerText = progress.files_to_pin;
  $('bulk-pinning-pinned-files').innerText = progress.pinned_files;
  $('bulk-pinning-pinned-files-percent').innerText =
      progress.pinned_files_percent;
  $('bulk-pinning-failed-files').innerText = progress.failed_files;
  $('bulk-pinning-syncing-files').innerText = progress.syncing_files;
  $('bulk-pinning-skipped-items').innerText = progress.skipped_items;
  $('bulk-pinning-listed-items').innerText = progress.listed_items;
  $('bulk-pinning-listed-dirs').innerText = progress.listed_dirs;
  $('bulk-pinning-listed-files').innerText = progress.listed_files;
  $('bulk-pinning-listed-docs').innerText = progress.listed_docs;
  $('bulk-pinning-listed-shortcuts').innerText = progress.listed_shortcuts;
  $('bulk-pinning-active-queries').innerText = progress.active_queries;
  $('bulk-pinning-max-active-queries').innerText = progress.max_active_queries;
  $('bulk-pinning-time-spent-listing-items').innerText =
      progress.time_spent_listing_items;
  $('bulk-pinning-time-spent-pinning-files').innerText =
      progress.time_spent_pinning_files;
  $('bulk-pinning-remaining-time').innerText = progress.remaining_time;
}

function updateStartupArguments(args) {
  $('startup-arguments-input').value = args;
}

/**
 * Updates the Local Storage summary.
 * @param {Object} localStorageSummary Dictionary describing the status of local
 * stogage.
 */
function updateLocalStorageUsage(localStorageSummary) {
  const 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) {
  const container = $('in-flight-operations-contents');

  // Reset the table. Remove children in reverse order. Otherwides each
  // existingNodes[i] changes as a side effect of removeChild.
  const existingNodes = container.childNodes;
  for (let i = existingNodes.length - 1; i >= 0; i--) {
    const node = existingNodes[i];
    if (node.className === 'in-flight-operation') {
      container.removeChild(node);
    }
  }

  // Add in-flight operations.
  for (let i = 0; i < inFlightOperations.length; i++) {
    const operation = inFlightOperations[i];
    const 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));
    let progress = operation.progress_current + '/' + operation.progress_total;
    if (operation.progress_total > 0) {
      const 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) {
  const quotaTotalInMb = toMegaByteString(aboutResource['account-quota-total']);
  const 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 delta update status.
 * @param {Object} deltaUpdateStatus Dictionary describing delta update status.
 */
function updateDeltaUpdateStatus(deltaUpdateStatus) {
  const itemContainer = $('delta-update-status');
  for (let i = 0; i < deltaUpdateStatus['items'].length; i++) {
    const update = deltaUpdateStatus['items'][i];
    const tr = document.createElement('tr');
    tr.className = 'delta-update';
    tr.appendChild(createElementFromText('td', update.id));
    tr.appendChild(createElementFromText('td', update.root_entry_path));
    const startPageToken = update.start_page_token;
    tr.appendChild(createElementFromText(
        'td',
        startPageToken + (startPageToken ? ' (loaded)' : ' (not loaded)')));
    tr.appendChild(createElementFromText('td', update.last_check_time));
    tr.appendChild(createElementFromText('td', update.last_check_result));
    tr.appendChild(createElementFromText('td', update.refreshing));

    itemContainer.appendChild(tr);
  }
}

/**
 * Updates the event log section.
 * @param {Array} log Array of events.
 */
function updateEventLog(log) {
  const ul = $('event-log');
  updateKeyValueList(ul, log);
}

/**
 * Updates the service log section.
 * @param {Array} log Log lines.
 */
function updateServiceLog(log) {
  const ul = $('service-log');
  updateKeyValueList(ul, log);
}

/**
 * Updates the service log section.
 * @param {Array} log Log lines.
 */
function updateOtherServiceLogsUrl(url) {
  const link = $('other-logs');
  link.setAttribute('href', url);
}

/**
 * Adds a new row to the syncing paths table upon successful completion.
 * @param {string} path The path that was synced.
 * @param {string} status The drive::FileError as a string without the
 *     "FILE_ERROR_" prefix.
 */
function onAddSyncPath(path, status) {
  $('mirroring-path-status').textContent = status;
  if (status !== 'OK') {
    console.error(`Cannot add sync path '${path}': ${status}`);
    return;
  }

  // Avoid adding paths to the table if they already exist.
  if ($(`mirroring-${path}`)) {
    return;
  }

  const newRow = document.createElement('tr');
  newRow.id = `mirroring-${path}`;
  const deleteButton = createElementFromText('button', 'Delete');
  deleteButton.addEventListener('click', function(e) {
    e.preventDefault();
    chrome.send('removeSyncPath', [path]);
  });
  const deleteCell = document.createElement('td');
  deleteCell.appendChild(deleteButton);
  newRow.appendChild(deleteCell);
  const pathCell = createElementFromText('td', path);
  newRow.appendChild(pathCell);
  $('mirror-sync-paths').appendChild(newRow);
}

/**
 * Remove a path from the syncing table.
 * @param {string} path The path that was synced.
 * @param {string} status The drive::FileError as a string without the
 *     "FILE_ERROR_" prefix.
 */
function onRemoveSyncPath(path, status) {
  if (status !== 'OK') {
    console.error(`Cannot remove sync path '${path}': ${status}`);
    return;
  }

  if (!$(`mirroring-${path}`)) {
    return;
  }

  $(`mirroring-${path}`).remove();
}

/**
 * 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) {
  const 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 (let i = 0; i < list.length; i++) {
    const item = list[i];
    let text = item.key;
    if (item.value !== '') {
      text += ': ' + item.value;
    }

    const li = createElementFromText('li', text);
    if (item.class) {
      li.classList.add(item.class);
    }
    ul.appendChild(li);
  }
}

function updateStartupArgumentsStatus(success) {
  $('arguments-status-text').textContent = (success ? 'success' : 'failed');
}

/**
 * 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');
}

/**
 * Makes up-to-date table of contents.
 */
function updateToc() {
  const toc = $('toc');
  while (toc.firstChild) {
    toc.removeChild(toc.firstChild);
  }
  const sections = document.getElementsByTagName('section');
  for (let i = 0; i < sections.length; i++) {
    const section = sections[i];
    if (!section.hidden) {
      const header = section.getElementsByTagName('h2')[0];
      const a = createElementFromText('a', header.textContent);
      a.href = '#' + section.id;
      const li = document.createElement('li');
      li.appendChild(a);
      toc.appendChild(li);
    }
  }
}

/**
 * Shows or hides a section.
 * @param {string} section Which section to change.
 * @param {boolean} enabled Whether to enable.
 */
function setSectionEnabled(section, enable) {
  const element = $(section);
  if (element.hidden !== !enable) {
    element.hidden = !enable;
    updateToc();
  }
}

function onZipDone(success) {
  $('button-export-logs').removeAttribute('disabled');
}

document.addEventListener('DOMContentLoaded', () => {
  chrome.send('pageLoaded');

  updateToc();

  $('bulk-pinning-visible')
      .addEventListener(
          'change',
          e => chrome.send('setBulkPinningVisible', [e.target.checked]));

  $('verbose-logging-toggle')
      .addEventListener(
          'change',
          e => chrome.send('setVerboseLoggingEnabled', [e.target.checked]));

  $('mirroring-toggle')
      .addEventListener(
          'change',
          e => chrome.send('setMirroringEnabled', [e.target.checked]));

  $('bulk-pinning-toggle')
      .addEventListener(
          'change',
          e => chrome.send('setBulkPinningEnabled', [e.target.checked]));

  $('startup-arguments-form').addEventListener('submit', e => {
    e.preventDefault();
    $('arguments-status-text').textContent = 'applying...';
    chrome.send('setStartupArguments', [$('startup-arguments-input').value]);
  });

  $('mirror-path-form').addEventListener('submit', e => {
    e.preventDefault();
    $('mirroring-path-status').textContent = 'adding...';
    chrome.send('addSyncPath', [$('mirror-path-input').value]);
  });

  $('button-enable-tracing')
      .addEventListener('click', () => chrome.send('enableTracing'));

  $('button-disable-tracing')
      .addEventListener('click', () => chrome.send('disableTracing'));

  $('button-enable-networking')
      .addEventListener('click', () => chrome.send('enableNetworking'));

  $('button-disable-networking')
      .addEventListener('click', () => chrome.send('disableNetworking'));

  $('button-enable-force-pause-syncing')
      .addEventListener('click', () => chrome.send('enableForcePauseSyncing'));

  $('button-disable-force-pause-syncing')
      .addEventListener('click', () => chrome.send('disableForcePauseSyncing'));

  $('button-dump-account-settings')
      .addEventListener('click', () => chrome.send('dumpAccountSettings'));

  $('button-load-account-settings')
      .addEventListener('click', () => chrome.send('loadAccountSettings'));

  $('button-restart-drive')
      .addEventListener('click', () => chrome.send('restartDrive'));

  $('button-reset-drive-filesystem').addEventListener('click', () => {
    if (window.confirm(
            'Warning: Any local changes not yet uploaded to the Drive server ' +
            'will be lost, continue?')) {
      $('reset-status-text').textContent = 'resetting...';
      chrome.send('resetDriveFileSystem');
    }
  });

  $('button-export-logs').addEventListener('click', () => {
    $('button-export-logs').setAttribute('disabled', 'true');
    chrome.send('zipLogs');
  });

  window.setInterval(() => chrome.send('periodicUpdate'), 1000);
});