OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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. |
| 4 |
| 5 'use strict'; |
| 6 |
| 7 /** |
| 8 * Handler of the background page for the drive sync events. |
| 9 * @param {ProgressCenter} progressCenter Progress center to submit the |
| 10 * progressing items. |
| 11 * @constructor |
| 12 */ |
| 13 function DriveSyncHandler(progressCenter) { |
| 14 /** |
| 15 * Progress center to submit the progressng item. |
| 16 * @type {ProgressCenter} |
| 17 * @private |
| 18 */ |
| 19 this.progressCenter_ = progressCenter; |
| 20 |
| 21 /** |
| 22 * Counter for progress ID. |
| 23 * @type {number} |
| 24 * @private |
| 25 */ |
| 26 this.idCounter_ = 0; |
| 27 |
| 28 /** |
| 29 * Progressing file names. |
| 30 * @type {Object.<string, ProgressCenterItem>} Map a file URL and a progress |
| 31 * center item. |
| 32 * @private |
| 33 */ |
| 34 this.items_ = {}; |
| 35 |
| 36 // Register events. |
| 37 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( |
| 38 this.onFileTransfersUpdated_.bind(this)); |
| 39 } |
| 40 |
| 41 /** |
| 42 * Completed event name. |
| 43 * @type {string} |
| 44 * @const |
| 45 */ |
| 46 DriveSyncHandler.COMPLETED_EVENT = 'completed'; |
| 47 |
| 48 /** |
| 49 * Progress ID of the drive sync. |
| 50 * @type {string} |
| 51 * @const |
| 52 */ |
| 53 DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX = 'drive-sync-'; |
| 54 |
| 55 DriveSyncHandler.prototype = { |
| 56 __proto__: cr.EventTarget.prototype, |
| 57 |
| 58 /** |
| 59 * @return {boolean} Whether the handler is having syncing items or not. |
| 60 */ |
| 61 get syncing() { |
| 62 // Check if this.items_ has properties or not. |
| 63 for (var url in this.items_) { |
| 64 return true; |
| 65 } |
| 66 return false; |
| 67 } |
| 68 }; |
| 69 |
| 70 /** |
| 71 * Handles file transfer updated events. |
| 72 * @param {Array.<FileTransferStatus>} statusList List of drive status. |
| 73 * @private |
| 74 */ |
| 75 DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(statusList) { |
| 76 var completed = false; |
| 77 for (var i = 0; i < statusList.length; i++) { |
| 78 var status = statusList[i]; |
| 79 switch (status.transferState) { |
| 80 case 'in_progress': |
| 81 case 'started': |
| 82 this.updateItem_(status); |
| 83 break; |
| 84 case 'completed': |
| 85 case 'failed': |
| 86 this.removeItem_(status); |
| 87 if (!this.syncing) |
| 88 this.dispatchEvent(new Event(DriveSyncHandler.COMPLETED_EVENT)); |
| 89 break; |
| 90 default: |
| 91 throw new Error( |
| 92 'Invalid transfer state: ' + status.transferState + '.'); |
| 93 } |
| 94 } |
| 95 }; |
| 96 |
| 97 /** |
| 98 * Updates the item involved with the given status. |
| 99 * @param {FileTransferStatus} status Transfer status. |
| 100 * @private |
| 101 */ |
| 102 DriveSyncHandler.prototype.updateItem_ = function(status) { |
| 103 webkitResolveLocalFileSystemURL(status.fileUrl, function(entry) { |
| 104 var item; |
| 105 if (!this.items_[status.fileUrl]) { |
| 106 item = new ProgressCenterItem(); |
| 107 item.id = DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX + (this.idCounter_++); |
| 108 item.type = ProgressItemType.SYNC; |
| 109 item.quiet = true; |
| 110 item.message = strf('SYNC_FILE_NAME', entry.name); |
| 111 item.cancelCallback = this.requestCancel_.bind(this, entry); |
| 112 this.items_[status.fileUrl] = item; |
| 113 } else { |
| 114 item = this.items_[status.fileUrl]; |
| 115 } |
| 116 item.progressValue = status.processed || 0; |
| 117 item.progressMax = status.total || 1; |
| 118 this.progressCenter_.updateItem(item); |
| 119 }.bind(this), function() { |
| 120 console.error('Cannot resolve the URL: ' + status.fileUrl); |
| 121 }); |
| 122 }; |
| 123 |
| 124 /** |
| 125 * Removes the item involved with the given status. |
| 126 * @param {FileTransferStatus} status Transfer status. |
| 127 * @private |
| 128 */ |
| 129 DriveSyncHandler.prototype.removeItem_ = function(status) { |
| 130 var item = this.items_[status.fileUrl]; |
| 131 delete this.items_[status.fileUrl]; |
| 132 if (item) { |
| 133 item.state = status.transferState === 'completed' ? |
| 134 ProgressItemState.COMPLETED : ProgressItemState.CANCELED; |
| 135 this.progressCenter_.updateItem(item); |
| 136 } |
| 137 }; |
| 138 |
| 139 /** |
| 140 * Requests to cancel for the given files' drive sync. |
| 141 * @param {Entry} entry Entry to be canceled. |
| 142 * @private |
| 143 */ |
| 144 DriveSyncHandler.prototype.requestCancel_ = function(entry) { |
| 145 chrome.fileBrowserPrivate.cancelFileTransfers([entry.toURL()], function() {}); |
| 146 }; |
OLD | NEW |