Chromium Code Reviews| Index: chrome/browser/resources/file_manager/background/js/drive_sync_handler.js |
| diff --git a/chrome/browser/resources/file_manager/background/js/drive_sync_handler.js b/chrome/browser/resources/file_manager/background/js/drive_sync_handler.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a65ff591324e07cccab3ff4be6749c3c07c45d3a |
| --- /dev/null |
| +++ b/chrome/browser/resources/file_manager/background/js/drive_sync_handler.js |
| @@ -0,0 +1,140 @@ |
| +// Copyright 2013 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. |
| + |
| +'use strict'; |
| + |
| +/** |
| + * Handler of the background page for the drive sync events. |
| + * @param {ProgressCenter} progressCenter Progress center to submit the |
| + * progressing items. |
| + * @constructor |
| + */ |
| +function DriveSyncHandler(progressCenter) { |
| + /** |
| + * Progress center to submit the progressng item. |
| + * @type {ProgressCenter} |
| + * @private |
| + */ |
| + this.progressCenter_ = progressCenter; |
| + |
| + /** |
| + * Counter for progress ID. |
| + * @type {number} |
| + * @private |
| + */ |
| + this.idCounter_ = 0; |
| + |
| + /** |
| + * Progressing file names. |
| + * @type {Object.<string, ProgressCenterItem>} Map a file URL and a progress |
| + * center item. |
| + * @private |
| + */ |
| + this.items_ = {}; |
| + |
| + // Register event. |
|
mtomasz
2014/01/15 06:38:01
nit: event -> events / the event.
hirono
2014/01/16 05:05:52
Done.
|
| + chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( |
| + this.onFileTransfersUpdated_.bind(this)); |
| +} |
| + |
| +/** |
| + * Complete event name. |
|
mtomasz
2014/01/15 06:38:01
nit: jsdoc missing.
nit: Complete -> Completion
hirono
2014/01/16 05:05:52
Done.
|
| + */ |
| +DriveSyncHandler.COMPLETE_EVENT = 'complete'; |
| + |
| +/** |
| + * Progress ID of the drive sync. |
| + * @type {string} |
| + * @const |
| + */ |
| +DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX = 'drive-sync-'; |
| + |
| +DriveSyncHandler.prototype = { |
| + __proto__: cr.EventTarget.prototype, |
| + |
| + /** |
| + * @return {boolean} Whether the handler is having synching items or not. |
| + */ |
| + get synching() { |
|
mtomasz
2014/01/15 06:38:01
nit: synching -> syncing
hirono
2014/01/16 05:05:52
Done.
|
| + for (var url in this.items_) { |
| + return true; |
|
mtomasz
2014/01/15 06:38:01
This looks tricky. Why not if (this.items_) { ...
hirono
2014/01/16 05:05:52
items_ is an object and this is checking if it doe
|
| + } |
| + return false; |
| + } |
| +}; |
| + |
| +/** |
| + * Handle file transfer updated events. |
|
mtomasz
2014/01/15 06:38:01
nit: Handles
hirono
2014/01/16 05:05:52
Done.
|
| + * @param {Array.<object>} statusList List of drive status. |
|
mtomasz
2014/01/15 06:38:01
nit: object -> Object
hirono
2014/01/16 05:05:52
Replaced with FileTransferStatus, which is defined
|
| + * @private |
| + */ |
| +DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(statusList) { |
| + var completed = false; |
| + for (var i = 0; i < statusList.length; i++) { |
| + var status = statusList[i]; |
| + switch (status.transferState) { |
| + case 'in_progress': |
| + case 'started': |
| + this.updateItem_(status); |
| + break; |
| + case 'completed': |
|
mtomasz
2014/01/15 06:38:01
Let's be consistent. Please choose one consistentl
hirono
2014/01/16 05:05:52
Done.
|
| + case 'failed': |
| + this.removeItem_(status); |
| + if (!this.synching) |
|
mtomasz
2014/01/15 06:38:01
nit: ditto typo
hirono
2014/01/16 05:05:52
Done.
|
| + this.dispatchEvent(new Event(DriveSyncHandler.COMPLETE_EVENT)); |
|
mtomasz
2014/01/15 06:38:01
ditto: -> completion event. 'Complete event' is mi
hirono
2014/01/16 05:05:52
Done.
|
| + break; |
| + default: |
| + throw new Error( |
| + 'Invalid transfer state: ' + status.transferState + '.'); |
| + } |
| + } |
| +}; |
| + |
| +/** |
| + * Updates the item involved with the given status. |
| + * @param {FileTransferStatus} status Transfer status. |
| + * @private |
| + */ |
| +DriveSyncHandler.prototype.updateItem_ = function(status) { |
| + var item; |
| + if (!this.items_[status.fileUrl]) { |
| + var fileName = status.fileUrl.replace(/.*\//, ''); |
|
mtomasz
2014/01/15 06:38:01
Can we avoid working on URLs? Can we convert to En
hirono
2014/01/16 05:05:52
Done.
|
| + item = new ProgressCenterItem(); |
| + item.id = DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX + (this.idCounter_++); |
| + item.type = ProgressItemType.SYNC; |
| + item.quiet = true; |
| + item.message = strf('SYNC_FILE_NAME', fileName); |
| + item.cancelCallback = this.requestCancel_.bind(this, status.fileUrl); |
| + this.items_[status.fileUrl] = item; |
| + } else { |
| + item = this.items_[status.fileUrl]; |
| + } |
| + item.progressValue = status.processed || 0; |
| + item.progressMax = status.total || 1; |
| + this.progressCenter_.updateItem(item); |
| +}; |
| + |
| +/** |
| + * Removes the item involved with the given status. |
| + * @param {FileTransferStatus} status Transfer status. |
| + * @private |
| + */ |
| +DriveSyncHandler.prototype.removeItem_ = function(status) { |
| + var item = this.items_[status.fileUrl]; |
| + delete this.items_[status.fileUrl]; |
| + if (item) { |
| + item.state = status.transferState === 'completed' ? |
| + ProgressItemState.COMPLETED : ProgressItemState.CANCELED; |
| + this.progressCenter_.updateItem(item); |
| + } |
| +}; |
| + |
| +/** |
| + * Requests to cancel for the given files' drive sync. |
| + * @param {string} url URL of the file to be canceled. |
|
mtomasz
2014/01/15 06:38:01
Can we work on Entries instead of URLs?
hirono
2014/01/16 05:05:52
Done.
|
| + * @private |
| + */ |
| +DriveSyncHandler.prototype.requestCancel_ = function(url) { |
| + chrome.fileBrowserPrivate.cancelFileTransfers([url], function() {}); |
| +}; |