Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ui / file_manager / file_manager / background / js / drive_sync_handler.js
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.
4
5 'use strict';
6
7 /**
8  * Handler of the background page for the drive sync events.
9  * @param {ProgressCenter} progressCenter Progress center to submit the
10  *     progressing items.
11  * @constructor
12  */
13 function DriveSyncHandler(progressCenter) {
14   /**
15    * Progress center to submit the progressing item.
16    * @type {ProgressCenter}
17    * @private
18    */
19   this.progressCenter_ = progressCenter;
20
21   /**
22    * Counter for error ID.
23    * @type {number}
24    * @private
25    */
26   this.errorIdCounter_ = 0;
27
28   /**
29    * Progress center item.
30    * @type {ProgressCenterItem}
31    * @private
32    */
33   this.item_ = new ProgressCenterItem();
34   this.item_.id = 'drive-sync';
35
36   /**
37    * If the property is true, this item is syncing.
38    * @type {boolean}
39    * @private
40    */
41   this.syncing_ = false;
42
43   /**
44    * Async queue.
45    * @type {AsyncUtil.Queue}
46    * @private
47    */
48   this.queue_ = new AsyncUtil.Queue();
49
50   // Register events.
51   chrome.fileManagerPrivate.onFileTransfersUpdated.addListener(
52       this.onFileTransfersUpdated_.bind(this));
53   chrome.fileManagerPrivate.onDriveSyncError.addListener(
54       this.onDriveSyncError_.bind(this));
55 }
56
57 /**
58  * Completed event name.
59  * @type {string}
60  * @const
61  */
62 DriveSyncHandler.COMPLETED_EVENT = 'completed';
63
64 /**
65  * Progress ID of the drive sync error.
66  * @type {string}
67  * @const
68  */
69 DriveSyncHandler.DRIVE_SYNC_ERROR_PREFIX = 'drive-sync-error-';
70
71 DriveSyncHandler.prototype = {
72   __proto__: cr.EventTarget.prototype,
73
74   /**
75    * @return {boolean} Whether the handler is having syncing items or not.
76    */
77   get syncing() {
78     return this.syncing_;
79   }
80 };
81
82 /**
83  * Handles file transfer updated events.
84  * @param {FileTransferStatus} status Transfer status.
85  * @private
86  */
87 DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(status) {
88   switch (status.transferState) {
89     case 'added':
90     case 'in_progress':
91     case 'started':
92       this.updateItem_(status);
93       break;
94     case 'completed':
95     case 'failed':
96       if (status.num_total_jobs === 1)
97         this.removeItem_(status);
98       break;
99     default:
100       throw new Error(
101           'Invalid transfer state: ' + status.transferState + '.');
102   }
103 };
104
105 /**
106  * Updates the item involved with the given status.
107  * @param {FileTransferStatus} status Transfer status.
108  * @private
109  */
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);
119       else
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_);
125       callback();
126     }.bind(this), function(error) {
127       console.warn('Resolving URL ' + status.fileUrl + ' is failed: ', error);
128       callback();
129     });
130   }.bind(this));
131 };
132
133 /**
134  * Removes the item involved with the given status.
135  * @param {FileTransferStatus} status Transfer status.
136  * @private
137  */
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));
145     callback();
146   }.bind(this));
147 };
148
149 /**
150  * Requests to cancel for the given files' drive sync.
151  * @param {Entry} entry Entry to be canceled.
152  * @private
153  */
154 DriveSyncHandler.prototype.requestCancel_ = function(entry) {
155   // Cancel all jobs.
156   chrome.fileManagerPrivate.cancelFileTransfers(function() {});
157 };
158
159 /**
160  * Handles drive's sync errors.
161  * @param {DriveSyncErrorEvent} event Drive sync error event.
162  * @private
163  */
164 DriveSyncHandler.prototype.onDriveSyncError_ = function(event) {
165   webkitResolveLocalFileSystemURL(event.fileUrl, function(entry) {
166     var item = new ProgressCenterItem();
167     item.id =
168         DriveSyncHandler.DRIVE_SYNC_ERROR_PREFIX + (this.errorIdCounter_++);
169     item.type = ProgressItemType.SYNC;
170     item.quiet = true;
171     item.state = ProgressItemState.ERROR;
172     switch (event.type) {
173       case 'delete_without_permission':
174         item.message =
175             strf('SYNC_DELETE_WITHOUT_PERMISSION_ERROR', entry.name);
176         break;
177       case 'service_unavailable':
178         item.message = str('SYNC_SERVICE_UNAVAILABLE_ERROR');
179         break;
180       case 'misc':
181         item.message = strf('SYNC_MISC_ERROR', entry.name);
182         break;
183     }
184     this.progressCenter_.updateItem(item);
185   }.bind(this));
186 };