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.
6 * Handler of the background page for the drive sync events.
7 * @param {ProgressCenter} progressCenter Progress center to submit the
10 * @extends {cr.EventTarget}
11 * @suppress {checkStructDictInheritance}
14 function DriveSyncHandler(progressCenter) {
16 * Progress center to submit the progressing item.
17 * @type {ProgressCenter}
21 this.progressCenter_ = progressCenter;
24 * Counter for error ID.
28 this.errorIdCounter_ = 0;
31 * Progress center item.
32 * @type {ProgressCenterItem}
36 this.item_ = new ProgressCenterItem();
37 this.item_.id = 'drive-sync';
40 * If the property is true, this item is syncing.
44 this.syncing_ = false;
47 * Whether the sync is disabled on cellular network or not.
51 this.cellularDisabled_ = false;
55 * @type {AsyncUtil.Queue}
59 this.queue_ = new AsyncUtil.Queue();
62 chrome.fileManagerPrivate.onFileTransfersUpdated.addListener(
63 this.onFileTransfersUpdated_.bind(this));
64 chrome.fileManagerPrivate.onDriveSyncError.addListener(
65 this.onDriveSyncError_.bind(this));
66 chrome.notifications.onButtonClicked.addListener(
67 this.onNotificationButtonClicked_.bind(this));
68 chrome.fileManagerPrivate.onPreferencesChanged.addListener(
69 this.onPreferencesChanged_.bind(this));
71 // Set initial values.
72 this.onPreferencesChanged_();
76 * Completed event name.
80 DriveSyncHandler.COMPLETED_EVENT = 'completed';
83 * Progress ID of the drive sync error.
87 DriveSyncHandler.DRIVE_SYNC_ERROR_PREFIX = 'drive-sync-error-';
90 * Notification ID of the disabled mobile sync notification.
95 DriveSyncHandler.DISABLED_MOBILE_SYNC_NOTIFICATION_ID_ = 'disabled-mobile-sync';
97 DriveSyncHandler.prototype = /** @struct */ {
98 __proto__: cr.EventTarget.prototype,
101 * @return {boolean} Whether the handler is having syncing items or not.
104 return this.syncing_;
109 * Returns whether the drive sync is currently suppressed or not.
113 DriveSyncHandler.prototype.isSyncSuppressed = function() {
114 return navigator.connection.type === 'cellular' &&
115 this.cellularDisabled_;
119 * Shows the notification saying that the drive sync is disabled on cellular
122 DriveSyncHandler.prototype.showDisabledMobileSyncNotification = function() {
123 chrome.notifications.create(
124 DriveSyncHandler.DISABLED_MOBILE_SYNC_NOTIFICATION_ID_,
127 title: chrome.runtime.getManifest().name,
128 message: str('DISABLED_MOBILE_SYNC_NOTIFICATION_MESSAGE'),
129 iconUrl: chrome.runtime.getURL('/common/images/icon96.png'),
131 {title: str('DISABLED_MOBILE_SYNC_NOTIFICATION_ENABLE_BUTTON')}
138 * Handles file transfer updated events.
139 * @param {FileTransferStatus} status Transfer status.
142 DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(status) {
143 switch (status.transferState) {
147 this.updateItem_(status);
151 if (status.num_total_jobs === 1)
152 this.removeItem_(status);
156 'Invalid transfer state: ' + status.transferState + '.');
161 * Updates the item involved with the given status.
162 * @param {FileTransferStatus} status Transfer status.
165 DriveSyncHandler.prototype.updateItem_ = function(status) {
166 this.queue_.run(function(callback) {
167 window.webkitResolveLocalFileSystemURL(status.fileUrl, function(entry) {
168 this.item_.state = ProgressItemState.PROGRESSING;
169 this.item_.type = ProgressItemType.SYNC;
170 this.item_.quiet = true;
171 this.syncing_ = true;
172 if (status.num_total_jobs > 1)
173 this.item_.message = strf('SYNC_FILE_NUMBER', status.num_total_jobs);
175 this.item_.message = strf('SYNC_FILE_NAME', entry.name);
176 this.item_.cancelCallback = this.requestCancel_.bind(this, entry);
177 this.item_.progressValue = status.processed;
178 this.item_.progressMax = status.total;
179 this.progressCenter_.updateItem(this.item_);
181 }.bind(this), function(error) {
182 console.warn('Resolving URL ' + status.fileUrl + ' is failed: ', error);
189 * Removes the item involved with the given status.
190 * @param {FileTransferStatus} status Transfer status.
193 DriveSyncHandler.prototype.removeItem_ = function(status) {
194 this.queue_.run(function(callback) {
195 this.item_.state = status.transferState === 'completed' ?
196 ProgressItemState.COMPLETED : ProgressItemState.CANCELED;
197 this.progressCenter_.updateItem(this.item_);
198 this.syncing_ = false;
199 this.dispatchEvent(new Event(DriveSyncHandler.COMPLETED_EVENT));
205 * Requests to cancel for the given files' drive sync.
206 * @param {Entry} entry Entry to be canceled.
209 DriveSyncHandler.prototype.requestCancel_ = function(entry) {
211 chrome.fileManagerPrivate.cancelFileTransfers();
215 * Handles drive's sync errors.
216 * @param {DriveSyncErrorEvent} event Drive sync error event.
219 DriveSyncHandler.prototype.onDriveSyncError_ = function(event) {
220 window.webkitResolveLocalFileSystemURL(event.fileUrl, function(entry) {
221 var item = new ProgressCenterItem();
223 DriveSyncHandler.DRIVE_SYNC_ERROR_PREFIX + (this.errorIdCounter_++);
224 item.type = ProgressItemType.SYNC;
226 item.state = ProgressItemState.ERROR;
227 switch (event.type) {
228 case 'delete_without_permission':
230 strf('SYNC_DELETE_WITHOUT_PERMISSION_ERROR', entry.name);
232 case 'service_unavailable':
233 item.message = str('SYNC_SERVICE_UNAVAILABLE_ERROR');
236 item.message = strf('SYNC_MISC_ERROR', entry.name);
239 this.progressCenter_.updateItem(item);
244 * Handles notification's button click.
245 * @param {string} notificationId Notification ID.
246 * @param {number} buttonIndex Index of the button.
249 DriveSyncHandler.prototype.onNotificationButtonClicked_ = function(
250 notificationId, buttonIndex) {
251 if (notificationId !== DriveSyncHandler.DISABLED_MOBILE_SYNC_NOTIFICATION_ID_)
253 chrome.notifications.clear(
254 DriveSyncHandler.DISABLED_MOBILE_SYNC_NOTIFICATION_ID_,
256 chrome.fileManagerPrivate.setPreferences({cellularDisabled: false});
260 * Handles preferences change.
263 DriveSyncHandler.prototype.onPreferencesChanged_ = function() {
264 chrome.fileManagerPrivate.getPreferences(function(pref) {
265 this.cellularDisabled_ = pref.cellularDisabled;