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 * Progress center item. |
23 * @type {number} | 23 * @type {ProgressCenterItem} |
24 * @private | 24 * @private |
25 */ | 25 */ |
26 this.idCounter_ = 0; | 26 this.item_ = new ProgressCenterItem(); |
27 | 27 this.item_.id = 'drive-sync'; |
28 /** | |
29 * Map of file urls and progress center items. | |
30 * @type {Object.<string, ProgressCenterItem>} | |
31 * @private | |
32 */ | |
33 this.items_ = {}; | |
34 | 28 |
35 /** | 29 /** |
36 * Async queue. | 30 * Async queue. |
37 * @type {AsyncUtil.Queue} | 31 * @type {AsyncUtil.Queue} |
38 * @private | 32 * @private |
39 */ | 33 */ |
40 this.queue_ = new AsyncUtil.Queue(); | 34 this.queue_ = new AsyncUtil.Queue(); |
41 | 35 |
42 // Register events. | 36 // Register events. |
43 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( | 37 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( |
(...skipping 17 matching lines...) Expand all Loading... | |
61 DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX = 'drive-sync-'; | 55 DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX = 'drive-sync-'; |
62 | 56 |
63 DriveSyncHandler.prototype = { | 57 DriveSyncHandler.prototype = { |
64 __proto__: cr.EventTarget.prototype, | 58 __proto__: cr.EventTarget.prototype, |
65 | 59 |
66 /** | 60 /** |
67 * @return {boolean} Whether the handler is having syncing items or not. | 61 * @return {boolean} Whether the handler is having syncing items or not. |
68 */ | 62 */ |
69 get syncing() { | 63 get syncing() { |
70 // Check if this.items_ has properties or not. | 64 // Check if this.items_ has properties or not. |
71 for (var url in this.items_) { | 65 if (this.item_.sync) { |
hirono
2014/08/29 05:38:30
We also does not add {} for 1-line body in JavaScr
iseki
2014/08/29 08:25:09
Done.
| |
72 return true; | 66 return true; |
73 } | 67 } |
74 return false; | 68 return false; |
75 } | 69 } |
76 }; | 70 }; |
77 | 71 |
78 /** | 72 /** |
79 * Handles file transfer updated events. | 73 * Handles file transfer updated events. |
80 * @param {Array.<FileTransferStatus>} statusList List of drive status. | 74 * @param {Array.<FileTransferStatus>} statusList List of drive status. |
81 * @private | 75 * @private |
82 */ | 76 */ |
83 DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(statusList) { | 77 DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(statusList) { |
84 var completed = false; | 78 var completed = false; |
85 for (var i = 0; i < statusList.length; i++) { | 79 for (var i = 0; i < statusList.length; i++) { |
86 var status = statusList[i]; | 80 var status = statusList[i]; |
87 switch (status.transferState) { | 81 switch (status.transferState) { |
82 case 'added': | |
88 case 'in_progress': | 83 case 'in_progress': |
89 case 'started': | 84 case 'started': |
90 this.updateItem_(status); | 85 this.updateItem_(status); |
91 break; | 86 break; |
92 case 'completed': | 87 case 'completed': |
93 case 'failed': | 88 case 'failed': |
94 this.removeItem_(status); | 89 if (status.num_total_jobs === 0) |
90 this.removeItem_(status); | |
95 if (!this.syncing) | 91 if (!this.syncing) |
96 this.dispatchEvent(new Event(DriveSyncHandler.COMPLETED_EVENT)); | 92 this.dispatchEvent(new Event(DriveSyncHandler.COMPLETED_EVENT)); |
97 break; | 93 break; |
98 default: | 94 default: |
99 throw new Error( | 95 throw new Error( |
100 'Invalid transfer state: ' + status.transferState + '.'); | 96 'Invalid transfer state: ' + status.transferState + '.'); |
101 } | 97 } |
102 } | 98 } |
103 }; | 99 }; |
104 | 100 |
105 /** | 101 /** |
106 * Updates the item involved with the given status. | 102 * Updates the item involved with the given status. |
107 * @param {FileTransferStatus} status Transfer status. | 103 * @param {FileTransferStatus} status Transfer status. |
108 * @private | 104 * @private |
109 */ | 105 */ |
110 DriveSyncHandler.prototype.updateItem_ = function(status) { | 106 DriveSyncHandler.prototype.updateItem_ = function(status) { |
111 this.queue_.run(function(callback) { | 107 this.queue_.run(function(callback) { |
112 if (this.items_[status.fileUrl]) { | |
113 callback(); | |
114 return; | |
115 } | |
116 webkitResolveLocalFileSystemURL(status.fileUrl, function(entry) { | 108 webkitResolveLocalFileSystemURL(status.fileUrl, function(entry) { |
117 var item = new ProgressCenterItem(); | 109 if (this.item_.state !== ProgressItemState.PROGRESSING) { |
hirono
2014/08/29 05:38:31
We also does not add {} for 1-line body in JavaScr
iseki
2014/08/29 08:25:09
Done.
| |
118 item.id = | 110 this.item_.state = ProgressItemState.PROGRESSING; |
119 DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX + (this.idCounter_++); | 111 } |
120 item.type = ProgressItemType.SYNC; | 112 this.item_.type = ProgressItemType.SYNC; |
121 item.quiet = true; | 113 this.item_.quiet = true; |
122 item.message = strf('SYNC_FILE_NAME', entry.name); | 114 this.item_.sync = true; |
123 item.cancelCallback = this.requestCancel_.bind(this, entry); | 115 if (status.num_total_jobs > 1) { |
hirono
2014/08/29 05:38:30
Even if it has an else block, we also does not add
iseki
2014/08/29 08:25:10
Done.
| |
124 this.items_[status.fileUrl] = item; | 116 this.item_.message = strf('SYNC_FILE_NUMBER', status.num_total_jobs); |
117 } else { | |
118 this.item_.message = strf('SYNC_FILE_NAME', entry.name); | |
119 } | |
120 this.item_.cancelCallback = this.requestCancel_.bind(this, entry); | |
125 callback(); | 121 callback(); |
126 }.bind(this), function(error) { | 122 }.bind(this), function(error) { |
127 console.warn('Resolving URL ' + status.fileUrl + ' is failed: ', error); | 123 console.warn('Resolving URL ' + status.fileUrl + ' is failed: ', error); |
128 callback(); | 124 callback(); |
129 }); | 125 }); |
130 }.bind(this)); | 126 }.bind(this)); |
131 this.queue_.run(function(callback) { | 127 this.queue_.run(function(callback) { |
132 var item = this.items_[status.fileUrl]; | 128 if (!this.item_) { |
133 if (!item) { | |
134 callback(); | 129 callback(); |
135 return; | 130 return; |
136 } | 131 } |
137 item.progressValue = status.processed || 0; | 132 this.item_.progressValue = status.processed || 0; |
138 item.progressMax = status.total || 1; | 133 this.item_.progressMax = status.total || 1; |
139 this.progressCenter_.updateItem(item); | 134 this.progressCenter_.updateItem(this.item_); |
140 callback(); | 135 callback(); |
141 }.bind(this)); | 136 }.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 if (!this.item_) { |
152 if (!item) { | |
153 callback(); | 147 callback(); |
154 return; | 148 return; |
155 } | 149 } |
156 item.state = status.transferState === 'completed' ? | 150 this.item_.state = status.transferState === 'completed' ? |
157 ProgressItemState.COMPLETED : ProgressItemState.CANCELED; | 151 ProgressItemState.COMPLETED : ProgressItemState.CANCELED; |
158 this.progressCenter_.updateItem(item); | 152 this.progressCenter_.updateItem(this.item_); |
159 delete this.items_[status.fileUrl]; | 153 this.item_.sync = false; |
160 callback(); | 154 callback(); |
161 }.bind(this)); | 155 }.bind(this)); |
162 }; | 156 }; |
163 | 157 |
164 /** | 158 /** |
165 * Requests to cancel for the given files' drive sync. | 159 * Requests to cancel for the given files' drive sync. |
166 * @param {Entry} entry Entry to be canceled. | 160 * @param {Entry} entry Entry to be canceled. |
167 * @private | 161 * @private |
168 */ | 162 */ |
169 DriveSyncHandler.prototype.requestCancel_ = function(entry) { | 163 DriveSyncHandler.prototype.requestCancel_ = function(entry) { |
170 chrome.fileBrowserPrivate.cancelFileTransfers([entry.toURL()], function() {}); | 164 chrome.fileBrowserPrivate.cancelFileTransfers([entry.toURL()], function() {}); |
171 }; | 165 }; |
172 | 166 |
173 /** | 167 /** |
174 * Handles drive's sync errors. | 168 * Handles drive's sync errors. |
175 * @param {DriveSyncErrorEvent} event Drive sync error event. | 169 * @param {DriveSyncErrorEvent} event Drive sync error event. |
176 * @private | 170 * @private |
177 */ | 171 */ |
178 DriveSyncHandler.prototype.onDriveSyncError_ = function(event) { | 172 DriveSyncHandler.prototype.onDriveSyncError_ = function(event) { |
179 webkitResolveLocalFileSystemURL(event.fileUrl, function(entry) { | 173 webkitResolveLocalFileSystemURL(event.fileUrl, function(entry) { |
180 var item; | 174 this.item_.type = ProgressItemType.SYNC; |
181 item = new ProgressCenterItem(); | 175 this.item_.quiet = true; |
182 item.id = DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX + (this.idCounter_++); | 176 this.item_.sync = false; |
183 item.type = ProgressItemType.SYNC; | 177 this.item_.state = ProgressItemState.ERROR; |
184 item.quiet = true; | |
185 item.state = ProgressItemState.ERROR; | |
186 switch (event.type) { | 178 switch (event.type) { |
187 case 'delete_without_permission': | 179 case 'delete_without_permission': |
188 item.message = | 180 this.item_.message = |
189 strf('SYNC_DELETE_WITHOUT_PERMISSION_ERROR', entry.name); | 181 strf('SYNC_DELETE_WITHOUT_PERMISSION_ERROR', entry.name); |
190 break; | 182 break; |
191 case 'service_unavailable': | 183 case 'service_unavailable': |
192 item.message = str('SYNC_SERVICE_UNAVAILABLE_ERROR'); | 184 this.item_.message = str('SYNC_SERVICE_UNAVAILABLE_ERROR'); |
193 break; | 185 break; |
194 case 'misc': | 186 case 'misc': |
195 item.message = strf('SYNC_MISC_ERROR', entry.name); | 187 this.item_.message = strf('SYNC_MISC_ERROR', entry.name); |
196 break; | 188 break; |
197 } | 189 } |
198 this.progressCenter_.updateItem(item); | 190 this.progressCenter_.updateItem(this.item_); |
199 }.bind(this)); | 191 }.bind(this)); |
200 }; | 192 }; |
OLD | NEW |