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 error ID.
26 this.errorIdCounter_ = 0;
29 * Progress center item.
30 * @type {ProgressCenterItem}
33 this.item_ = new ProgressCenterItem();
34 this.item_.id = 'drive-sync';
37 * If the property is true, this item is syncing.
41 this.syncing_ = false;
45 * @type {AsyncUtil.Queue}
48 this.queue_ = new AsyncUtil.Queue();
51 chrome.fileManagerPrivate.onFileTransfersUpdated.addListener(
52 this.onFileTransfersUpdated_.bind(this));
53 chrome.fileManagerPrivate.onDriveSyncError.addListener(
54 this.onDriveSyncError_.bind(this));
58 * Completed event name.
62 DriveSyncHandler.COMPLETED_EVENT = 'completed';
65 * Progress ID of the drive sync error.
69 DriveSyncHandler.DRIVE_SYNC_ERROR_PREFIX = 'drive-sync-error-';
71 DriveSyncHandler.prototype = {
72 __proto__: cr.EventTarget.prototype,
75 * @return {boolean} Whether the handler is having syncing items or not.
83 * Handles file transfer updated events.
84 * @param {FileTransferStatus} status Transfer status.
87 DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(status) {
88 switch (status.transferState) {
92 this.updateItem_(status);
96 if (status.num_total_jobs === 1)
97 this.removeItem_(status);
101 '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 webkitResolveLocalFileSystemURL(status.fileUrl, function(entry) {
113 this.item_.state = ProgressItemState.PROGRESSING;
114 this.item_.type = ProgressItemType.SYNC;
115 this.item_.quiet = true;
116 this.syncing_ = true;
117 if (status.num_total_jobs > 1)
118 this.item_.message = strf('SYNC_FILE_NUMBER', status.num_total_jobs);
120 this.item_.message = strf('SYNC_FILE_NAME', entry.name);
121 this.item_.cancelCallback = this.requestCancel_.bind(this, entry);
122 this.item_.progressValue = status.processed;
123 this.item_.progressMax = status.total;
124 this.progressCenter_.updateItem(this.item_);
126 }.bind(this), function(error) {
127 console.warn('Resolving URL ' + status.fileUrl + ' is failed: ', error);
134 * Removes the item involved with the given status.
135 * @param {FileTransferStatus} status Transfer status.
138 DriveSyncHandler.prototype.removeItem_ = function(status) {
139 this.queue_.run(function(callback) {
140 this.item_.state = status.transferState === 'completed' ?
141 ProgressItemState.COMPLETED : ProgressItemState.CANCELED;
142 this.progressCenter_.updateItem(this.item_);
143 this.syncing_ = false;
144 this.dispatchEvent(new Event(DriveSyncHandler.COMPLETED_EVENT));
150 * Requests to cancel for the given files' drive sync.
151 * @param {Entry} entry Entry to be canceled.
154 DriveSyncHandler.prototype.requestCancel_ = function(entry) {
156 chrome.fileManagerPrivate.cancelFileTransfers(function() {});
160 * Handles drive's sync errors.
161 * @param {DriveSyncErrorEvent} event Drive sync error event.
164 DriveSyncHandler.prototype.onDriveSyncError_ = function(event) {
165 webkitResolveLocalFileSystemURL(event.fileUrl, function(entry) {
166 var item = new ProgressCenterItem();
168 DriveSyncHandler.DRIVE_SYNC_ERROR_PREFIX + (this.errorIdCounter_++);
169 item.type = ProgressItemType.SYNC;
171 item.state = ProgressItemState.ERROR;
172 switch (event.type) {
173 case 'delete_without_permission':
175 strf('SYNC_DELETE_WITHOUT_PERMISSION_ERROR', entry.name);
177 case 'service_unavailable':
178 item.message = str('SYNC_SERVICE_UNAVAILABLE_ERROR');
181 item.message = strf('SYNC_MISC_ERROR', entry.name);
184 this.progressCenter_.updateItem(item);