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
3 // found in the LICENSE file.
8 * Handler of the background page for the drive sync events.
9 * @param {ProgressCenter} progressCenter Progress center to submit the
13 function DriveSyncHandler(progressCenter) {
15 * Progress center to submit the progressing item.
16 * @type {ProgressCenter}
19 this.progressCenter_ = progressCenter;
22 * Counter for progress ID.
29 * Map of file urls and progress center items.
30 * @type {Object.<string, ProgressCenterItem>}
37 * @type {AsyncUtil.Queue}
40 this.queue_ = new AsyncUtil.Queue();
43 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener(
44 this.onFileTransfersUpdated_.bind(this));
45 chrome.fileBrowserPrivate.onDriveSyncError.addListener(
46 this.onDriveSyncError_.bind(this));
50 * Completed event name.
54 DriveSyncHandler.COMPLETED_EVENT = 'completed';
57 * Progress ID of the drive sync.
61 DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX = 'drive-sync-';
63 DriveSyncHandler.prototype = {
64 __proto__: cr.EventTarget.prototype,
67 * @return {boolean} Whether the handler is having syncing items or not.
70 // Check if this.items_ has properties or not.
71 for (var url in this.items_) {
79 * Handles file transfer updated events.
80 * @param {Array.<FileTransferStatus>} statusList List of drive status.
83 DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(statusList) {
84 var completed = false;
85 for (var i = 0; i < statusList.length; i++) {
86 var status = statusList[i];
87 switch (status.transferState) {
90 this.updateItem_(status);
94 this.removeItem_(status);
96 this.dispatchEvent(new Event(DriveSyncHandler.COMPLETED_EVENT));
100 'Invalid transfer state: ' + status.transferState + '.');
106 * Updates the item involved with the given status.
107 * @param {FileTransferStatus} status Transfer status.
110 DriveSyncHandler.prototype.updateItem_ = function(status) {
111 this.queue_.run(function(callback) {
112 if (this.items_[status.fileUrl]) {
116 webkitResolveLocalFileSystemURL(status.fileUrl, function(entry) {
117 var item = new ProgressCenterItem();
119 DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX + (this.idCounter_++);
120 item.type = ProgressItemType.SYNC;
122 item.message = strf('SYNC_FILE_NAME', entry.name);
123 item.cancelCallback = this.requestCancel_.bind(this, entry);
124 this.items_[status.fileUrl] = item;
126 }.bind(this), function(error) {
127 console.warn('Resolving URL ' + status.fileUrl + ' is failed: ', error);
131 this.queue_.run(function(callback) {
132 var item = this.items_[status.fileUrl];
137 item.progressValue = status.processed || 0;
138 item.progressMax = status.total || 1;
139 this.progressCenter_.updateItem(item);
145 * Removes the item involved with the given status.
146 * @param {FileTransferStatus} status Transfer status.
149 DriveSyncHandler.prototype.removeItem_ = function(status) {
150 this.queue_.run(function(callback) {
151 var item = this.items_[status.fileUrl];
156 item.state = status.transferState === 'completed' ?
157 ProgressItemState.COMPLETED : ProgressItemState.CANCELED;
158 this.progressCenter_.updateItem(item);
159 delete this.items_[status.fileUrl];
165 * Requests to cancel for the given files' drive sync.
166 * @param {Entry} entry Entry to be canceled.
169 DriveSyncHandler.prototype.requestCancel_ = function(entry) {
170 chrome.fileBrowserPrivate.cancelFileTransfers([entry.toURL()], function() {});
174 * Handles drive's sync errors.
175 * @param {DriveSyncErrorEvent} event Drive sync error event.
178 DriveSyncHandler.prototype.onDriveSyncError_ = function(event) {
179 webkitResolveLocalFileSystemURL(event.fileUrl, function(entry) {
181 item = new ProgressCenterItem();
182 item.id = DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX + (this.idCounter_++);
183 item.type = ProgressItemType.SYNC;
185 item.state = ProgressItemState.ERROR;
186 switch (event.type) {
187 case 'delete_without_permission':
189 strf('SYNC_DELETE_WITHOUT_PERMISSION_ERROR', entry.name);
191 case 'service_unavailable':
192 item.message = str('SYNC_SERVICE_UNAVAILABLE_ERROR');
195 item.message = strf('SYNC_MISC_ERROR', entry.name);
198 this.progressCenter_.updateItem(item);