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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @constructor | 8 * @constructor |
9 */ | 9 */ |
10 function FileCopyManager() { | 10 function FileCopyManager() { |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
477 this.cancelObservers_ = []; | 477 this.cancelObservers_ = []; |
478 this.maybeScheduleCloseBackgroundPage_(); | 478 this.maybeScheduleCloseBackgroundPage_(); |
479 }; | 479 }; |
480 | 480 |
481 /** | 481 /** |
482 * Request that the current copy queue be abandoned. | 482 * Request that the current copy queue be abandoned. |
483 * | 483 * |
484 * @param {function()=} opt_callback On cancel. | 484 * @param {function()=} opt_callback On cancel. |
485 */ | 485 */ |
486 FileCopyManager.prototype.requestCancel = function(opt_callback) { | 486 FileCopyManager.prototype.requestCancel = function(opt_callback) { |
487 this.cancelRequested_ = true; | 487 this.cancelRequested_ = true; |
mtomasz
2013/08/06 01:39:19
Can we remove this member and just clean the callb
hidehiko
2013/08/06 02:14:55
Added code to clean callback at L489.
We cannot re
mtomasz
2013/08/06 02:21:38
sgtm
| |
488 if (this.cancelCallback_) | 488 if (this.cancelCallback_) |
489 this.cancelCallback_(); | 489 this.cancelCallback_(); |
490 if (opt_callback) | 490 if (opt_callback) |
491 this.cancelObservers_.push(opt_callback); | 491 this.cancelObservers_.push(opt_callback); |
492 | 492 |
493 // If there is any active task it will eventually call maybeCancel_. | 493 // If there is any active task it will eventually call maybeCancel_. |
494 // Otherwise call it right now. | 494 // Otherwise call it right now. |
495 if (this.copyTasks_.length == 0) | 495 if (this.copyTasks_.length == 0) |
496 this.doCancel_(); | 496 this.doCancel_(); |
497 }; | 497 }; |
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
882 var isTargetOnDrive = PathUtil.isDriveBasedPath(targetDirEntry.fullPath); | 882 var isTargetOnDrive = PathUtil.isDriveBasedPath(targetDirEntry.fullPath); |
883 | 883 |
884 if (!isSourceOnDrive && !isTargetOnDrive) { | 884 if (!isSourceOnDrive && !isTargetOnDrive) { |
885 // Sending a file from local to local. | 885 // Sending a file from local to local. |
886 // To copy local file, we use File blob and FileWriter to take the | 886 // To copy local file, we use File blob and FileWriter to take the |
887 // progress. | 887 // progress. |
888 targetDirEntry.getFile( | 888 targetDirEntry.getFile( |
889 targetRelativePath, | 889 targetRelativePath, |
890 {create: true, exclusive: true}, | 890 {create: true, exclusive: true}, |
891 function(targetEntry) { | 891 function(targetEntry) { |
892 self.copyFileEntry_( | 892 self.cancelCallback_ = self.copyFileEntry_( |
893 sourceEntry, targetEntry, | 893 sourceEntry, targetEntry, |
894 onCopyProgress, onCopyComplete, onFilesystemError); | 894 onCopyProgress, |
895 function(entry, size) { | |
896 self.cancelCallback_ = null; | |
897 onCopyComplete(entry, size); | |
898 }, | |
899 function(error) { | |
900 self.cancelCallback_ = null; | |
901 onFilesystemError(error); | |
902 }); | |
895 }, | 903 }, |
896 util.flog('Error getting file: ' + targetRelativePath, | 904 util.flog('Error getting file: ' + targetRelativePath, |
897 onFilesystemError)); | 905 onFilesystemError)); |
898 return; | 906 return; |
899 } | 907 } |
900 | 908 |
901 // Sending a file from a) Drive to Drive, b) Drive to local or c) local to | 909 // Sending a file from a) Drive to Drive, b) Drive to local or c) local to |
902 // Drive. | 910 // Drive. |
903 var sourceFileUrl = sourceEntry.toURL(); | 911 var sourceFileUrl = sourceEntry.toURL(); |
904 var sourceFilePath = util.extractFilePath(sourceFileUrl); | 912 var sourceFilePath = util.extractFilePath(sourceFileUrl); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
984 * copied. | 992 * copied. |
985 * @param {function(FileEntry, number)} progressCallback Function that will be | 993 * @param {function(FileEntry, number)} progressCallback Function that will be |
986 * called when a part of the source entry is copied. It takes |targetEntry| | 994 * called when a part of the source entry is copied. It takes |targetEntry| |
987 * and size of the last copied chunk as parameters. | 995 * and size of the last copied chunk as parameters. |
988 * @param {function(FileEntry, number)} successCallback Function that will be | 996 * @param {function(FileEntry, number)} successCallback Function that will be |
989 * called the copy operation finishes. It takes |targetEntry| and size of | 997 * called the copy operation finishes. It takes |targetEntry| and size of |
990 * the last (not previously reported) copied chunk as parameters. | 998 * the last (not previously reported) copied chunk as parameters. |
991 * @param {function(FileError)} errorCallback Function that will be called | 999 * @param {function(FileError)} errorCallback Function that will be called |
992 * if an error is encountered. Takes error type and additional error data | 1000 * if an error is encountered. Takes error type and additional error data |
993 * as parameters. | 1001 * as parameters. |
1002 * @return {function()} Callback to cancel the current file copy operation. | |
1003 * When the cancel is done, errorCallback will be called. The returned | |
1004 * callback must not be called more than once. | |
994 * @private | 1005 * @private |
995 */ | 1006 */ |
996 FileCopyManager.prototype.copyFileEntry_ = function(sourceEntry, | 1007 FileCopyManager.prototype.copyFileEntry_ = function(sourceEntry, |
997 targetEntry, | 1008 targetEntry, |
998 progressCallback, | 1009 progressCallback, |
999 successCallback, | 1010 successCallback, |
1000 errorCallback) { | 1011 errorCallback) { |
1001 if (this.maybeCancel_()) | 1012 // Set to true when cancel is requested. |
1002 return; | 1013 var cancelRequested = false; |
1003 | 1014 |
1004 var self = this; | 1015 sourceEntry.file(function(file) { |
1016 if (cancelRequested) { | |
1017 errorCallback(util.createFileError(FileError.ABORT_ERR)); | |
1018 return; | |
1019 } | |
1005 | 1020 |
1006 var onSourceFileFound = function(file) { | 1021 targetEntry.createWriter(function(writer) { |
1007 var onWriterCreated = function(writer) { | 1022 if (cancelRequested) { |
1023 errorCallback(util.createFileError(FileError.ABORT_ERR)); | |
mtomasz
2013/08/06 01:39:19
Can we remove the target file on cancel? It may be
hidehiko
2013/08/06 02:14:55
I want to keep this as is, because, IIRC, many OS
mtomasz
2013/08/06 02:21:38
As far as I remember on Windows the file gets remo
mtomasz
2013/08/06 02:22:29
I meant when downloading a file in Chrome.
| |
1024 return; | |
1025 } | |
1026 | |
1008 var reportedProgress = 0; | 1027 var reportedProgress = 0; |
1009 writer.onerror = function(progress) { | 1028 writer.onerror = writer.onabort = function(progress) { |
1010 errorCallback(writer.error); | 1029 errorCallback(cancelRequested ? |
1030 util.createFileError(FileError.ABORT_ERR) : | |
1031 writer.error); | |
1011 }; | 1032 }; |
1012 | 1033 |
1013 writer.onprogress = function(progress) { | 1034 writer.onprogress = function(progress) { |
1014 if (self.maybeCancel_()) { | 1035 if (cancelRequested) { |
1015 // If the copy was cancelled, we should abort the operation. | 1036 // If the copy was cancelled, we should abort the operation. |
1037 // The errorCallback will be called by writer.onabort after the | |
1038 // termination. | |
1016 writer.abort(); | 1039 writer.abort(); |
1017 return; | 1040 return; |
1018 } | 1041 } |
1042 | |
1019 // |progress.loaded| will contain total amount of data copied by now. | 1043 // |progress.loaded| will contain total amount of data copied by now. |
1020 // |progressCallback| expects data amount delta from the last progress | 1044 // |progressCallback| expects data amount delta from the last progress |
1021 // update. | 1045 // update. |
1022 progressCallback(targetEntry, progress.loaded - reportedProgress); | 1046 progressCallback(targetEntry, progress.loaded - reportedProgress); |
1023 reportedProgress = progress.loaded; | 1047 reportedProgress = progress.loaded; |
1024 }; | 1048 }; |
1025 | 1049 |
1026 writer.onwrite = function() { | 1050 writer.onwrite = function() { |
1051 if (cancelRequested) { | |
1052 errorCallback(util.createFileError(FileError.ABORT_ERR)); | |
1053 return; | |
1054 } | |
1055 | |
1027 sourceEntry.getMetadata(function(metadata) { | 1056 sourceEntry.getMetadata(function(metadata) { |
1028 chrome.fileBrowserPrivate.setLastModified(targetEntry.toURL(), | 1057 if (cancelRequested) { |
1058 errorCallback(util.createFileError(FileError.ABORT_ERR)); | |
1059 return; | |
1060 } | |
1061 | |
1062 chrome.fileBrowserPrivate.setLastModified( | |
1063 targetEntry.toURL(), | |
1029 '' + Math.round(metadata.modificationTime.getTime() / 1000)); | 1064 '' + Math.round(metadata.modificationTime.getTime() / 1000)); |
1030 successCallback(targetEntry, file.size - reportedProgress); | 1065 successCallback(targetEntry, file.size - reportedProgress); |
1031 }); | 1066 }); |
1032 }; | 1067 }; |
1033 | 1068 |
1034 writer.write(file); | 1069 writer.write(file); |
1035 }; | 1070 }, errorCallback); |
1071 }, errorCallback); | |
1036 | 1072 |
1037 targetEntry.createWriter(onWriterCreated, errorCallback); | 1073 return function() { |
1074 cancelRequested = true; | |
1038 }; | 1075 }; |
1039 | |
1040 sourceEntry.file(onSourceFileFound, errorCallback); | |
1041 }; | 1076 }; |
1042 | 1077 |
1043 /** | 1078 /** |
1044 * Moves all entries in the task. | 1079 * Moves all entries in the task. |
1045 * | 1080 * |
1046 * @param {FileCopyManager.Task} task A move task to be run. | 1081 * @param {FileCopyManager.Task} task A move task to be run. |
1047 * @param {function(util.EntryChangedType, Entry)} entryChangedCallback Callback | 1082 * @param {function(util.EntryChangedType, Entry)} entryChangedCallback Callback |
1048 * invoked when an entry is changed. | 1083 * invoked when an entry is changed. |
1049 * @param {function()} progressCallback Callback invoked periodically during | 1084 * @param {function()} progressCallback Callback invoked periodically during |
1050 * the moving. | 1085 * the moving. |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1189 if (success) { | 1224 if (success) { |
1190 task.targetDirEntry.getFile( | 1225 task.targetDirEntry.getFile( |
1191 destPath, {create: false}, | 1226 destPath, {create: false}, |
1192 function(entry) { | 1227 function(entry) { |
1193 entryChangedCallback(util.EntryChangedType.CREATE, entry); | 1228 entryChangedCallback(util.EntryChangedType.CREATE, entry); |
1194 successCallback(); | 1229 successCallback(); |
1195 }, | 1230 }, |
1196 onFilesystemError); | 1231 onFilesystemError); |
1197 } else { | 1232 } else { |
1198 onFilesystemError( | 1233 onFilesystemError( |
1199 Object.create(FileError.prototype, { | 1234 util.createFileError(FileError.INVALID_MODIFICATION_ERR)); |
1200 code: { | |
1201 get: function() { return FileError.INVALID_MODIFICATION_ERR; } | |
1202 } | |
1203 })); | |
1204 } | 1235 } |
1205 }; | 1236 }; |
1206 | 1237 |
1207 progressCallback(); | 1238 progressCallback(); |
1208 chrome.fileBrowserPrivate.zipSelection(dirURL, selectionURLs, destPath, | 1239 chrome.fileBrowserPrivate.zipSelection(dirURL, selectionURLs, destPath, |
1209 onZipSelectionComplete); | 1240 onZipSelectionComplete); |
1210 }; | 1241 }; |
1211 | 1242 |
1212 FileCopyManager.Task.deduplicatePath( | 1243 FileCopyManager.Task.deduplicatePath( |
1213 task.targetDirEntry, destName + '.zip', onDeduplicated, errorCallback); | 1244 task.targetDirEntry, destName + '.zip', onDeduplicated, errorCallback); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1352 // Assume self.cancelRequested_ == false. | 1383 // Assume self.cancelRequested_ == false. |
1353 // This moved us from 0 to 1 active tasks, let the servicing begin! | 1384 // This moved us from 0 to 1 active tasks, let the servicing begin! |
1354 self.serviceAllTasks_(); | 1385 self.serviceAllTasks_(); |
1355 } else { | 1386 } else { |
1356 // Force to update the progress of butter bar when there are new tasks | 1387 // Force to update the progress of butter bar when there are new tasks |
1357 // coming while servicing current task. | 1388 // coming while servicing current task. |
1358 self.eventRouter_.sendProgressEvent('PROGRESS', self.getStatus()); | 1389 self.eventRouter_.sendProgressEvent('PROGRESS', self.getStatus()); |
1359 } | 1390 } |
1360 }); | 1391 }); |
1361 }; | 1392 }; |
OLD | NEW |