OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * Handler of the background page for the drive sync events. | 8 * Handler of the background page for the drive sync events. |
9 * @param {ProgressCenter} progressCenter Progress center to submit the | 9 * @param {ProgressCenter} progressCenter Progress center to submit the |
10 * progressing items. | 10 * progressing items. |
11 * @constructor | 11 * @constructor |
12 */ | 12 */ |
13 function DriveSyncHandler(progressCenter) { | 13 function DriveSyncHandler(progressCenter) { |
14 /** | 14 /** |
15 * Progress center to submit the progressing item. | 15 * Progress center to submit the progressing item. |
16 * @type {ProgressCenter} | 16 * @type {ProgressCenter} |
17 * @private | 17 * @private |
18 */ | 18 */ |
19 this.progressCenter_ = progressCenter; | 19 this.progressCenter_ = progressCenter; |
20 | 20 |
21 /** | 21 /** |
22 * Counter for progress ID. | 22 * Counter for error ID. |
23 * @type {number} | 23 * @type {number} |
24 * @private | 24 * @private |
25 */ | 25 */ |
26 this.idCounter_ = 0; | 26 this.errorIdCounter_ = 0; |
27 | 27 |
28 /** | 28 /** |
29 * Map of file urls and progress center items. | 29 * Progress center item. |
30 * @type {Object.<string, ProgressCenterItem>} | 30 * @type {ProgressCenterItem} |
31 * @private | 31 * @private |
32 */ | 32 */ |
33 this.items_ = {}; | 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; |
34 | 42 |
35 /** | 43 /** |
36 * Async queue. | 44 * Async queue. |
37 * @type {AsyncUtil.Queue} | 45 * @type {AsyncUtil.Queue} |
38 * @private | 46 * @private |
39 */ | 47 */ |
40 this.queue_ = new AsyncUtil.Queue(); | 48 this.queue_ = new AsyncUtil.Queue(); |
41 | 49 |
42 // Register events. | 50 // Register events. |
43 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( | 51 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( |
44 this.onFileTransfersUpdated_.bind(this)); | 52 this.onFileTransfersUpdated_.bind(this)); |
45 chrome.fileBrowserPrivate.onDriveSyncError.addListener( | 53 chrome.fileBrowserPrivate.onDriveSyncError.addListener( |
46 this.onDriveSyncError_.bind(this)); | 54 this.onDriveSyncError_.bind(this)); |
47 } | 55 } |
48 | 56 |
49 /** | 57 /** |
50 * Completed event name. | 58 * Completed event name. |
51 * @type {string} | 59 * @type {string} |
52 * @const | 60 * @const |
53 */ | 61 */ |
54 DriveSyncHandler.COMPLETED_EVENT = 'completed'; | 62 DriveSyncHandler.COMPLETED_EVENT = 'completed'; |
55 | 63 |
56 /** | 64 /** |
57 * Progress ID of the drive sync. | 65 * Progress ID of the drive sync error. |
58 * @type {string} | 66 * @type {string} |
59 * @const | 67 * @const |
60 */ | 68 */ |
61 DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX = 'drive-sync-'; | 69 DriveSyncHandler.DRIVE_SYNC_ERROR_PREFIX = 'drive-sync-error-'; |
62 | 70 |
63 DriveSyncHandler.prototype = { | 71 DriveSyncHandler.prototype = { |
64 __proto__: cr.EventTarget.prototype, | 72 __proto__: cr.EventTarget.prototype, |
65 | 73 |
66 /** | 74 /** |
67 * @return {boolean} Whether the handler is having syncing items or not. | 75 * @return {boolean} Whether the handler is having syncing items or not. |
68 */ | 76 */ |
69 get syncing() { | 77 get syncing() { |
70 // Check if this.items_ has properties or not. | 78 return this.syncing_; |
71 for (var url in this.items_) { | |
72 return true; | |
73 } | |
74 return false; | |
75 } | 79 } |
76 }; | 80 }; |
77 | 81 |
78 /** | 82 /** |
79 * Handles file transfer updated events. | 83 * Handles file transfer updated events. |
80 * @param {Array.<FileTransferStatus>} statusList List of drive status. | 84 * @param {Array.<FileTransferStatus>} statusList List of drive status. |
81 * @private | 85 * @private |
82 */ | 86 */ |
83 DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(statusList) { | 87 DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(statusList) { |
84 var completed = false; | 88 var completed = false; |
85 for (var i = 0; i < statusList.length; i++) { | 89 for (var i = 0; i < statusList.length; i++) { |
86 var status = statusList[i]; | 90 var status = statusList[i]; |
87 switch (status.transferState) { | 91 switch (status.transferState) { |
| 92 case 'added': |
88 case 'in_progress': | 93 case 'in_progress': |
89 case 'started': | 94 case 'started': |
90 this.updateItem_(status); | 95 this.updateItem_(status); |
91 break; | 96 break; |
92 case 'completed': | 97 case 'completed': |
93 case 'failed': | 98 case 'failed': |
94 this.removeItem_(status); | 99 if (status.num_total_jobs === 1) |
95 if (!this.syncing) | 100 this.removeItem_(status); |
96 this.dispatchEvent(new Event(DriveSyncHandler.COMPLETED_EVENT)); | |
97 break; | 101 break; |
98 default: | 102 default: |
99 throw new Error( | 103 throw new Error( |
100 'Invalid transfer state: ' + status.transferState + '.'); | 104 'Invalid transfer state: ' + status.transferState + '.'); |
101 } | 105 } |
102 } | 106 } |
103 }; | 107 }; |
104 | 108 |
105 /** | 109 /** |
106 * Updates the item involved with the given status. | 110 * Updates the item involved with the given status. |
107 * @param {FileTransferStatus} status Transfer status. | 111 * @param {FileTransferStatus} status Transfer status. |
108 * @private | 112 * @private |
109 */ | 113 */ |
110 DriveSyncHandler.prototype.updateItem_ = function(status) { | 114 DriveSyncHandler.prototype.updateItem_ = function(status) { |
111 this.queue_.run(function(callback) { | 115 this.queue_.run(function(callback) { |
112 if (this.items_[status.fileUrl]) { | |
113 callback(); | |
114 return; | |
115 } | |
116 webkitResolveLocalFileSystemURL(status.fileUrl, function(entry) { | 116 webkitResolveLocalFileSystemURL(status.fileUrl, function(entry) { |
117 var item = new ProgressCenterItem(); | 117 this.item_.state = ProgressItemState.PROGRESSING; |
118 item.id = | 118 this.item_.type = ProgressItemType.SYNC; |
119 DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX + (this.idCounter_++); | 119 this.item_.quiet = true; |
120 item.type = ProgressItemType.SYNC; | 120 this.syncing_ = true; |
121 item.quiet = true; | 121 if (status.num_total_jobs > 1) |
122 item.message = strf('SYNC_FILE_NAME', entry.name); | 122 this.item_.message = strf('SYNC_FILE_NUMBER', status.num_total_jobs); |
123 item.cancelCallback = this.requestCancel_.bind(this, entry); | 123 else |
124 this.items_[status.fileUrl] = item; | 124 this.item_.message = strf('SYNC_FILE_NAME', entry.name); |
| 125 this.item_.cancelCallback = this.requestCancel_.bind(this, entry); |
| 126 // status.processed does not show the process of whole of sync. |
| 127 this.item_.progressValue = 0; |
| 128 // status.total does not show the total of whole of sync. |
| 129 this.item_.progressMax = 1; |
| 130 this.progressCenter_.updateItem(this.item_); |
125 callback(); | 131 callback(); |
126 }.bind(this), function(error) { | 132 }.bind(this), function(error) { |
127 console.warn('Resolving URL ' + status.fileUrl + ' is failed: ', error); | 133 console.warn('Resolving URL ' + status.fileUrl + ' is failed: ', error); |
128 callback(); | 134 callback(); |
129 }); | 135 }); |
130 }.bind(this)); | 136 }.bind(this)); |
131 this.queue_.run(function(callback) { | |
132 var item = this.items_[status.fileUrl]; | |
133 if (!item) { | |
134 callback(); | |
135 return; | |
136 } | |
137 item.progressValue = status.processed || 0; | |
138 item.progressMax = status.total || 1; | |
139 this.progressCenter_.updateItem(item); | |
140 callback(); | |
141 }.bind(this)); | |
142 }; | 137 }; |
143 | 138 |
144 /** | 139 /** |
145 * Removes the item involved with the given status. | 140 * Removes the item involved with the given status. |
146 * @param {FileTransferStatus} status Transfer status. | 141 * @param {FileTransferStatus} status Transfer status. |
147 * @private | 142 * @private |
148 */ | 143 */ |
149 DriveSyncHandler.prototype.removeItem_ = function(status) { | 144 DriveSyncHandler.prototype.removeItem_ = function(status) { |
150 this.queue_.run(function(callback) { | 145 this.queue_.run(function(callback) { |
151 var item = this.items_[status.fileUrl]; | 146 this.item_.state = status.transferState === 'completed' ? |
152 if (!item) { | |
153 callback(); | |
154 return; | |
155 } | |
156 item.state = status.transferState === 'completed' ? | |
157 ProgressItemState.COMPLETED : ProgressItemState.CANCELED; | 147 ProgressItemState.COMPLETED : ProgressItemState.CANCELED; |
158 this.progressCenter_.updateItem(item); | 148 this.progressCenter_.updateItem(this.item_); |
159 delete this.items_[status.fileUrl]; | 149 this.syncing_ = false; |
| 150 this.dispatchEvent(new Event(DriveSyncHandler.COMPLETED_EVENT)); |
160 callback(); | 151 callback(); |
161 }.bind(this)); | 152 }.bind(this)); |
162 }; | 153 }; |
163 | 154 |
164 /** | 155 /** |
165 * Requests to cancel for the given files' drive sync. | 156 * Requests to cancel for the given files' drive sync. |
166 * @param {Entry} entry Entry to be canceled. | 157 * @param {Entry} entry Entry to be canceled. |
167 * @private | 158 * @private |
168 */ | 159 */ |
169 DriveSyncHandler.prototype.requestCancel_ = function(entry) { | 160 DriveSyncHandler.prototype.requestCancel_ = function(entry) { |
170 chrome.fileBrowserPrivate.cancelFileTransfers([entry.toURL()], function() {}); | 161 chrome.fileBrowserPrivate.cancelFileTransfers([entry.toURL()], function() {}); |
171 }; | 162 }; |
172 | 163 |
173 /** | 164 /** |
174 * Handles drive's sync errors. | 165 * Handles drive's sync errors. |
175 * @param {DriveSyncErrorEvent} event Drive sync error event. | 166 * @param {DriveSyncErrorEvent} event Drive sync error event. |
176 * @private | 167 * @private |
177 */ | 168 */ |
178 DriveSyncHandler.prototype.onDriveSyncError_ = function(event) { | 169 DriveSyncHandler.prototype.onDriveSyncError_ = function(event) { |
179 webkitResolveLocalFileSystemURL(event.fileUrl, function(entry) { | 170 webkitResolveLocalFileSystemURL(event.fileUrl, function(entry) { |
180 var item; | 171 var item = new ProgressCenterItem(); |
181 item = new ProgressCenterItem(); | 172 item.id = |
182 item.id = DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX + (this.idCounter_++); | 173 DriveSyncHandler.DRIVE_SYNC_ERROR_PREFIX + (this.errorIdCounter_++); |
183 item.type = ProgressItemType.SYNC; | 174 item.type = ProgressItemType.SYNC; |
184 item.quiet = true; | 175 item.quiet = true; |
185 item.state = ProgressItemState.ERROR; | 176 item.state = ProgressItemState.ERROR; |
186 switch (event.type) { | 177 switch (event.type) { |
187 case 'delete_without_permission': | 178 case 'delete_without_permission': |
188 item.message = | 179 item.message = |
189 strf('SYNC_DELETE_WITHOUT_PERMISSION_ERROR', entry.name); | 180 strf('SYNC_DELETE_WITHOUT_PERMISSION_ERROR', entry.name); |
190 break; | 181 break; |
191 case 'service_unavailable': | 182 case 'service_unavailable': |
192 item.message = str('SYNC_SERVICE_UNAVAILABLE_ERROR'); | 183 item.message = str('SYNC_SERVICE_UNAVAILABLE_ERROR'); |
193 break; | 184 break; |
194 case 'misc': | 185 case 'misc': |
195 item.message = strf('SYNC_MISC_ERROR', entry.name); | 186 item.message = strf('SYNC_MISC_ERROR', entry.name); |
196 break; | 187 break; |
197 } | 188 } |
198 this.progressCenter_.updateItem(item); | 189 this.progressCenter_.updateItem(item); |
199 }.bind(this)); | 190 }.bind(this)); |
200 }; | 191 }; |
OLD | NEW |