Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2232)

Unified Diff: chrome/browser/resources/file_manager/background/js/drive_sync_handler.js

Issue 131403003: Files.app: Show drive sync state in the progress center. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/file_manager/background/js/drive_sync_handler.js
diff --git a/chrome/browser/resources/file_manager/background/js/drive_sync_handler.js b/chrome/browser/resources/file_manager/background/js/drive_sync_handler.js
new file mode 100644
index 0000000000000000000000000000000000000000..16c5d08c2ae2cd30866623ee2569bc1b890044ff
--- /dev/null
+++ b/chrome/browser/resources/file_manager/background/js/drive_sync_handler.js
@@ -0,0 +1,146 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+'use strict';
+
+/**
+ * Handler of the background page for the drive sync events.
+ * @param {ProgressCenter} progressCenter Progress center to submit the
+ * progressing items.
+ * @constructor
+ */
+function DriveSyncHandler(progressCenter) {
+ /**
+ * Progress center to submit the progressng item.
+ * @type {ProgressCenter}
+ * @private
+ */
+ this.progressCenter_ = progressCenter;
+
+ /**
+ * Counter for progress ID.
+ * @type {number}
+ * @private
+ */
+ this.idCounter_ = 0;
+
+ /**
+ * Progressing file names.
+ * @type {Object.<string, ProgressCenterItem>} Map a file URL and a progress
+ * center item.
+ * @private
+ */
+ this.items_ = {};
+
+ // Register events.
+ chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener(
+ this.onFileTransfersUpdated_.bind(this));
+}
+
+/**
+ * Completed event name.
+ * @type {string}
+ * @const
+ */
+DriveSyncHandler.COMPLETED_EVENT = 'completed';
+
+/**
+ * Progress ID of the drive sync.
+ * @type {string}
+ * @const
+ */
+DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX = 'drive-sync-';
+
+DriveSyncHandler.prototype = {
+ __proto__: cr.EventTarget.prototype,
+
+ /**
+ * @return {boolean} Whether the handler is having syncing items or not.
+ */
+ get syncing() {
+ // Check if this.items_ has properties or not.
+ for (var url in this.items_) {
+ return true;
+ }
+ return false;
+ }
+};
+
+/**
+ * Handles file transfer updated events.
+ * @param {Array.<FileTransferStatus>} statusList List of drive status.
+ * @private
+ */
+DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(statusList) {
+ var completed = false;
+ for (var i = 0; i < statusList.length; i++) {
+ var status = statusList[i];
+ switch (status.transferState) {
+ case 'in_progress':
+ case 'started':
+ this.updateItem_(status);
+ break;
+ case 'completed':
+ case 'failed':
+ this.removeItem_(status);
+ if (!this.syncing)
+ this.dispatchEvent(new Event(DriveSyncHandler.COMPLETED_EVENT));
+ break;
+ default:
+ throw new Error(
+ 'Invalid transfer state: ' + status.transferState + '.');
+ }
+ }
+};
+
+/**
+ * Updates the item involved with the given status.
+ * @param {FileTransferStatus} status Transfer status.
+ * @private
+ */
+DriveSyncHandler.prototype.updateItem_ = function(status) {
+ webkitResolveLocalFileSystemURL(status.fileUrl, function(entry) {
+ var item;
+ if (!this.items_[status.fileUrl]) {
+ item = new ProgressCenterItem();
+ item.id = DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX + (this.idCounter_++);
+ item.type = ProgressItemType.SYNC;
+ item.quiet = true;
+ item.message = strf('SYNC_FILE_NAME', entry.name);
+ item.cancelCallback = this.requestCancel_.bind(this, entry);
+ this.items_[status.fileUrl] = item;
+ } else {
+ item = this.items_[status.fileUrl];
+ }
+ item.progressValue = status.processed || 0;
+ item.progressMax = status.total || 1;
+ this.progressCenter_.updateItem(item);
+ }.bind(this), function() {
+ console.error('Cannot resolve the URL: ' + status.fileUrl);
+ });
+};
+
+/**
+ * Removes the item involved with the given status.
+ * @param {FileTransferStatus} status Transfer status.
+ * @private
+ */
+DriveSyncHandler.prototype.removeItem_ = function(status) {
+ var item = this.items_[status.fileUrl];
+ delete this.items_[status.fileUrl];
+ if (item) {
+ item.state = status.transferState === 'completed' ?
+ ProgressItemState.COMPLETED : ProgressItemState.CANCELED;
+ this.progressCenter_.updateItem(item);
+ }
+};
+
+/**
+ * Requests to cancel for the given files' drive sync.
+ * @param {Entry} entry Entry to be canceled.
+ * @private
+ */
+DriveSyncHandler.prototype.requestCancel_ = function(entry) {
+ chrome.fileBrowserPrivate.cancelFileTransfers([entry.toURL()], function() {});
+};

Powered by Google App Engine
This is Rietveld 408576698