 Chromium Code Reviews
 Chromium Code Reviews Issue 136783003:
  Files.app; Serializes event handler calls for particular URLs.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 136783003:
  Files.app; Serializes event handler calls for particular URLs.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| OLD | NEW | 
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 'use strict'; | 5 'use strict'; | 
| 6 | 6 | 
| 7 /** | 7 /** | 
| 8 * Handler of the background page for the drive sync events. | 8 * Handler of the background page for the drive sync events. | 
| 9 * @param {ProgressCenter} progressCenter Progress center to submit the | 9 * @param {ProgressCenter} progressCenter Progress center to submit the | 
| 10 * progressing items. | 10 * progressing items. | 
| 11 * @constructor | 11 * @constructor | 
| 12 */ | 12 */ | 
| 13 function DriveSyncHandler(progressCenter) { | 13 function DriveSyncHandler(progressCenter) { | 
| 14 /** | 14 /** | 
| 15 * Progress center to submit the progressng item. | 15 * Progress center to submit the progressng item. | 
| 16 * @type {ProgressCenter} | 16 * @type {ProgressCenter} | 
| 17 * @private | 17 * @private | 
| 18 */ | 18 */ | 
| 19 this.progressCenter_ = progressCenter; | 19 this.progressCenter_ = progressCenter; | 
| 20 | 20 | 
| 21 /** | 21 /** | 
| 22 * Counter for progress ID. | 22 * Counter for progress ID. | 
| 23 * @type {number} | 23 * @type {number} | 
| 24 * @private | 24 * @private | 
| 25 */ | 25 */ | 
| 26 this.idCounter_ = 0; | 26 this.idCounter_ = 0; | 
| 27 | 27 | 
| 28 /** | 28 /** | 
| 29 * Progressing file names. | 29 * Progressing file names. | 
| 
mtomasz
2014/01/29 10:34:29
nit: Please update this comment, eg. that this is
 
hirono
2014/01/30 04:51:47
Done.
 | |
| 30 * @type {Object.<string, ProgressCenterItem>} Map a file URL and a progress | 30 * @type {Object.<string, Promise>} Map a file URL and promise. | 
| 
mtomasz
2014/01/29 10:34:29
Hm. This solution is cool, but I'm a little bit co
 
hirono
2014/01/30 04:51:47
Just do it simple! Thanks!
 | |
| 31 * center item. | |
| 32 * @private | 31 * @private | 
| 33 */ | 32 */ | 
| 34 this.items_ = {}; | 33 this.items_ = {}; | 
| 35 | 34 | 
| 36 // Register events. | 35 // Register events. | 
| 37 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( | 36 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( | 
| 38 this.onFileTransfersUpdated_.bind(this)); | 37 this.onFileTransfersUpdated_.bind(this)); | 
| 39 chrome.fileBrowserPrivate.onDriveSyncError.addListener( | 38 chrome.fileBrowserPrivate.onDriveSyncError.addListener( | 
| 40 this.onDriveSyncError_.bind(this)); | 39 this.onDriveSyncError_.bind(this)); | 
| 41 } | 40 } | 
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 } | 94 } | 
| 96 } | 95 } | 
| 97 }; | 96 }; | 
| 98 | 97 | 
| 99 /** | 98 /** | 
| 100 * Updates the item involved with the given status. | 99 * Updates the item involved with the given status. | 
| 101 * @param {FileTransferStatus} status Transfer status. | 100 * @param {FileTransferStatus} status Transfer status. | 
| 102 * @private | 101 * @private | 
| 103 */ | 102 */ | 
| 104 DriveSyncHandler.prototype.updateItem_ = function(status) { | 103 DriveSyncHandler.prototype.updateItem_ = function(status) { | 
| 105 webkitResolveLocalFileSystemURL(status.fileUrl, function(entry) { | 104 if (!this.items_[status.fileUrl]) { | 
| 106 var item; | 105 // Promise also handles the error case of webkitResolveLocalFileSystemURL | 
| 107 if (!this.items_[status.fileUrl]) { | 106 // and skips the following instructions until the catach method is called. | 
| 108 item = new ProgressCenterItem(); | 107 this.items_[status.fileUrl] = | 
| 109 item.id = DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX + (this.idCounter_++); | 108 new Promise(webkitResolveLocalFileSystemURL.bind(null, status.fileUrl)). | 
| 110 item.type = ProgressItemType.SYNC; | 109 then(function(entry) { | 
| 111 item.quiet = true; | 110 var item = new ProgressCenterItem(); | 
| 112 item.message = strf('SYNC_FILE_NAME', entry.name); | 111 item.id = | 
| 113 item.cancelCallback = this.requestCancel_.bind(this, entry); | 112 DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX + (this.idCounter_++); | 
| 114 this.items_[status.fileUrl] = item; | 113 item.type = ProgressItemType.SYNC; | 
| 115 } else { | 114 item.quiet = true; | 
| 116 item = this.items_[status.fileUrl]; | 115 item.message = strf('SYNC_FILE_NAME', entry.name); | 
| 117 } | 116 item.cancelCallback = this.requestCancel_.bind(this, entry); | 
| 117 return item; | |
| 118 }.bind(this)); | |
| 
mtomasz
2014/01/29 10:34:29
Please print a warning to console that resolving f
 
hirono
2014/01/30 04:51:47
The error case of webkitResolveLocalFileSystemURL
 
mtomasz
2014/01/30 05:11:13
My concern was, that the error in resolving was sh
 | |
| 119 } | |
| 120 this.items_[status.fileUrl].then(function(item) { | |
| 118 item.progressValue = status.processed || 0; | 121 item.progressValue = status.processed || 0; | 
| 119 item.progressMax = status.total || 1; | 122 item.progressMax = status.total || 1; | 
| 120 this.progressCenter_.updateItem(item); | 123 this.progressCenter_.updateItem(item); | 
| 121 }.bind(this), function() { | 124 }.bind(this)); | 
| 122 console.error('Cannot resolve the URL: ' + status.fileUrl); | |
| 123 }); | |
| 124 }; | 125 }; | 
| 125 | 126 | 
| 126 /** | 127 /** | 
| 127 * Removes the item involved with the given status. | 128 * Removes the item involved with the given status. | 
| 128 * @param {FileTransferStatus} status Transfer status. | 129 * @param {FileTransferStatus} status Transfer status. | 
| 129 * @private | 130 * @private | 
| 130 */ | 131 */ | 
| 131 DriveSyncHandler.prototype.removeItem_ = function(status) { | 132 DriveSyncHandler.prototype.removeItem_ = function(status) { | 
| 132 var item = this.items_[status.fileUrl]; | 133 if (!this.items_[status.fileUrl]) | 
| 133 delete this.items_[status.fileUrl]; | 134 return; | 
| 134 if (item) { | 135 this.items_[status.fileUrl].then(function(item) { | 
| 135 item.state = status.transferState === 'completed' ? | 136 item.state = status.transferState === 'completed' ? | 
| 136 ProgressItemState.COMPLETED : ProgressItemState.CANCELED; | 137 ProgressItemState.COMPLETED : ProgressItemState.CANCELED; | 
| 137 this.progressCenter_.updateItem(item); | 138 this.progressCenter_.updateItem(item); | 
| 138 } | 139 return null; | 
| 140 }.bind(this)).catch (function(error) { | |
| 141 console.error(error); | |
| 
mtomasz
2014/01/29 10:34:29
Can you please handle this catch in updateItem_, w
 
hirono
2014/01/30 04:51:47
The position of the catch method was important bec
 
mtomasz
2014/01/30 05:11:13
Interesting. What would happen if we called catch(
 | |
| 142 }); | |
| 143 delete this.items_[status.fileUrl]; | |
| 139 }; | 144 }; | 
| 140 | 145 | 
| 141 /** | 146 /** | 
| 142 * Requests to cancel for the given files' drive sync. | 147 * Requests to cancel for the given files' drive sync. | 
| 143 * @param {Entry} entry Entry to be canceled. | 148 * @param {Entry} entry Entry to be canceled. | 
| 144 * @private | 149 * @private | 
| 145 */ | 150 */ | 
| 146 DriveSyncHandler.prototype.requestCancel_ = function(entry) { | 151 DriveSyncHandler.prototype.requestCancel_ = function(entry) { | 
| 147 chrome.fileBrowserPrivate.cancelFileTransfers([entry.toURL()], function() {}); | 152 chrome.fileBrowserPrivate.cancelFileTransfers([entry.toURL()], function() {}); | 
| 148 }; | 153 }; | 
| (...skipping 19 matching lines...) Expand all Loading... | |
| 168 case 'service_unavailable': | 173 case 'service_unavailable': | 
| 169 item.message = str('SYNC_SERVICE_UNAVAILABLE_ERROR'); | 174 item.message = str('SYNC_SERVICE_UNAVAILABLE_ERROR'); | 
| 170 break; | 175 break; | 
| 171 case 'misc': | 176 case 'misc': | 
| 172 item.message = strf('SYNC_MISC_ERROR', entry.name); | 177 item.message = strf('SYNC_MISC_ERROR', entry.name); | 
| 173 break; | 178 break; | 
| 174 } | 179 } | 
| 175 this.progressCenter_.updateItem(item); | 180 this.progressCenter_.updateItem(item); | 
| 176 }.bind(this)); | 181 }.bind(this)); | 
| 177 }; | 182 }; | 
| OLD | NEW |