OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 /** | 5 /** |
6 * @constructor | 6 * @constructor |
7 * @param {DirectoryEntry} root Root directory entry. | 7 * @param {DirectoryEntry} root Root directory entry. |
8 */ | 8 */ |
9 function FileCopyManager(root) { | 9 function FileCopyManager(root) { |
10 this.copyTasks_ = []; | 10 this.copyTasks_ = []; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 var rv = { | 189 var rv = { |
190 pendingItems: 0, // Files + Directories | 190 pendingItems: 0, // Files + Directories |
191 pendingFiles: 0, | 191 pendingFiles: 0, |
192 pendingDirectories: 0, | 192 pendingDirectories: 0, |
193 pendingBytes: 0, | 193 pendingBytes: 0, |
194 | 194 |
195 completedItems: 0, // Files + Directories | 195 completedItems: 0, // Files + Directories |
196 completedFiles: 0, | 196 completedFiles: 0, |
197 completedDirectories: 0, | 197 completedDirectories: 0, |
198 completedBytes: 0, | 198 completedBytes: 0, |
199 | |
200 // If source or target are on gdata we can't use completed bytes to track | |
201 // progress. | |
202 useBytesForPercentage: true | |
203 }; | 199 }; |
204 | 200 |
205 for (var i = 0; i < this.copyTasks_.length; i++) { | 201 for (var i = 0; i < this.copyTasks_.length; i++) { |
206 var task = this.copyTasks_[i]; | 202 var task = this.copyTasks_[i]; |
207 rv.pendingFiles += task.pendingFiles.length; | 203 rv.pendingFiles += task.pendingFiles.length; |
208 rv.pendingDirectories += task.pendingDirectories.length; | 204 rv.pendingDirectories += task.pendingDirectories.length; |
209 rv.pendingBytes += task.pendingBytes; | 205 rv.pendingBytes += task.pendingBytes; |
210 | 206 |
211 rv.completedFiles += task.completedFiles.length; | 207 rv.completedFiles += task.completedFiles.length; |
212 rv.completedDirectories += task.completedDirectories.length; | 208 rv.completedDirectories += task.completedDirectories.length; |
213 rv.completedBytes += task.completedBytes; | 209 rv.completedBytes += task.completedBytes; |
214 if (task.sourceOnGData || task.targetOnGData) | |
215 rv.useBytesForPercentage = false; | |
216 } | 210 } |
217 rv.pendingItems = rv.pendingFiles + rv.pendingDirectories; | 211 rv.pendingItems = rv.pendingFiles + rv.pendingDirectories; |
218 rv.completedItems = rv.completedFiles + rv.completedDirectories; | 212 rv.completedItems = rv.completedFiles + rv.completedDirectories; |
219 | 213 |
220 rv.totalFiles = rv.pendingFiles + rv.completedFiles; | 214 rv.totalFiles = rv.pendingFiles + rv.completedFiles; |
221 rv.totalDirectories = rv.pendingDirectories + rv.completedDirectories; | 215 rv.totalDirectories = rv.pendingDirectories + rv.completedDirectories; |
222 rv.totalItems = rv.pendingItems + rv.completedItems; | 216 rv.totalItems = rv.pendingItems + rv.completedItems; |
223 rv.totalBytes = rv.pendingBytes + rv.completedBytes; | 217 rv.totalBytes = rv.pendingBytes + rv.completedBytes; |
224 | 218 |
225 return rv; | 219 return rv; |
226 }; | 220 }; |
227 | 221 |
228 /** | 222 /** |
229 * Get the overall progress data of all queued copy tasks. | 223 * Get the overall progress data of all queued copy tasks. |
230 * @return {Object} An object containing the following parameters: | 224 * @return {Object} An object containing the following parameters: |
231 * percentage - The percentage (0-1) of finished items. | 225 * percentage - The percentage (0-1) of finished items. |
232 * pendingItems - The number of pending/unfinished items. | 226 * pendingItems - The number of pending/unfinished items. |
233 */ | 227 */ |
234 FileCopyManager.prototype.getProgress = function() { | 228 FileCopyManager.prototype.getProgress = function() { |
235 var status = this.getStatus(); | 229 var status = this.getStatus(); |
236 | 230 |
237 // TODO(tbarzic): We can't use completedBytes and totalBytes to estimate | 231 var percentage = status.completedBytes / status.totalBytes; |
238 // progress if the file is transferred from/to drive for two reasons: | |
239 // 1' completedBytes don't get updated for drive files. | |
240 // 2' There is no way to get completed bytes in real time. If completed bytes | |
241 // are updated when each item finished and if there is a large item to be | |
242 // copied, the progress bar would stop moving until the item is finished | |
243 // and then jump a large portion of the bar. | |
244 // | |
245 // Obviously 2' > 1'. | |
246 var percentage = status.useBytesForPercentage ? | |
247 (status.completedBytes / status.totalBytes) : | |
248 ((status.completedItems + 0.5) / status.totalItems); | |
249 | 232 |
250 return { | 233 return { |
251 percentage: percentage, | 234 percentage: percentage, |
252 pendingItems: status.pendingItems | 235 pendingItems: status.pendingItems |
253 }; | 236 }; |
254 }; | 237 }; |
255 | 238 |
256 /** | 239 /** |
257 * Dispatch a simple copy-progress event with reason and optional err data. | 240 * Dispatch a simple copy-progress event with reason and optional err data. |
258 * @private | 241 * @private |
(...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
758 return; | 741 return; |
759 } | 742 } |
760 } | 743 } |
761 | 744 |
762 // TODO(benchan): Until GDataFileSystem supports FileWriter, we use the | 745 // TODO(benchan): Until GDataFileSystem supports FileWriter, we use the |
763 // transferFile API to copy files into or out from a gdata file system. | 746 // transferFile API to copy files into or out from a gdata file system. |
764 if (sourceEntry.isFile && (task.sourceOnGData || task.targetOnGData)) { | 747 if (sourceEntry.isFile && (task.sourceOnGData || task.targetOnGData)) { |
765 var sourceFileUrl = sourceEntry.toURL(); | 748 var sourceFileUrl = sourceEntry.toURL(); |
766 var targetFileUrl = targetDirEntry.toURL() + '/' + | 749 var targetFileUrl = targetDirEntry.toURL() + '/' + |
767 encodeURIComponent(targetRelativePath); | 750 encodeURIComponent(targetRelativePath); |
| 751 var transferedBytes = 0; |
| 752 function onFileTransfersUpdated(statusList) { |
| 753 for (var i = 0; i < statusList.length; i++) { |
| 754 var s = statusList[i]; |
| 755 if ((s.fileUrl == sourceFileUrl || s.fileUrl == targetFileUrl) && |
| 756 s.processed > transferedBytes) { |
| 757 onCopyProgress(sourceEntry, s.processed - transferedBytes); |
| 758 transferedBytes = s.processed; |
| 759 } |
| 760 } |
| 761 } |
| 762 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( |
| 763 onFileTransfersUpdated); |
768 chrome.fileBrowserPrivate.transferFile( | 764 chrome.fileBrowserPrivate.transferFile( |
769 sourceFileUrl, targetFileUrl, | 765 sourceFileUrl, targetFileUrl, |
770 function() { | 766 function() { |
| 767 chrome.fileBrowserPrivate.onFileTransfersUpdated.removeListener( |
| 768 onFileTransfersUpdated); |
771 if (chrome.extension.lastError) { | 769 if (chrome.extension.lastError) { |
772 console.log( | 770 console.log( |
773 'Error copying ' + sourceFileUrl + ' to ' + targetFileUrl); | 771 'Error copying ' + sourceFileUrl + ' to ' + targetFileUrl); |
774 onFilesystemError({ | 772 onFilesystemError({ |
775 code: chrome.extension.lastError.message, | 773 code: chrome.extension.lastError.message, |
776 toGDrive: task.targetOnGData, | 774 toGDrive: task.targetOnGData, |
777 sourceFileUrl: sourceFileUrl | 775 sourceFileUrl: sourceFileUrl |
778 }); | 776 }); |
779 } else { | 777 } else { |
780 targetDirEntry.getFile(targetRelativePath, {}, | 778 targetDirEntry.getFile(targetRelativePath, {}, |
781 onFilesystemCopyComplete.bind(self, sourceEntry), | 779 function(targetEntry) { |
| 780 targetEntry.getMetadata(function(metadata) { |
| 781 if (metadata.size > transferedBytes) |
| 782 onCopyProgress(sourceEntry, |
| 783 metadata.size - transferedBytes); |
| 784 onFilesystemCopyComplete(sourceEntry, targetEntry); |
| 785 }); |
| 786 }, |
782 onFilesystemError); | 787 onFilesystemError); |
783 } | 788 } |
784 }); | 789 }); |
785 return; | 790 return; |
786 } | 791 } |
787 | 792 |
788 if (sourceEntry.isDirectory) { | 793 if (sourceEntry.isDirectory) { |
789 targetDirEntry.getDirectory( | 794 targetDirEntry.getDirectory( |
790 targetRelativePath, | 795 targetRelativePath, |
791 {create: true, exclusive: true}, | 796 {create: true, exclusive: true}, |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
876 }; | 881 }; |
877 | 882 |
878 writer.write(file); | 883 writer.write(file); |
879 } | 884 } |
880 | 885 |
881 targetEntry.createWriter(onWriterCreated, errorCallback); | 886 targetEntry.createWriter(onWriterCreated, errorCallback); |
882 } | 887 } |
883 | 888 |
884 sourceEntry.file(onSourceFileFound, errorCallback); | 889 sourceEntry.file(onSourceFileFound, errorCallback); |
885 }; | 890 }; |
OLD | NEW |