Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: chrome/browser/resources/file_manager/js/file_copy_manager.js

Issue 22150006: Re-implement cancellation of copyFileEntry_(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/resources/file_manager/js/util.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 465 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 this.maybeScheduleCloseBackgroundPage_(); 476 this.maybeScheduleCloseBackgroundPage_();
477 }; 477 };
478 478
479 /** 479 /**
480 * Request that the current copy queue be abandoned. 480 * Request that the current copy queue be abandoned.
481 * 481 *
482 * @param {function()=} opt_callback On cancel. 482 * @param {function()=} opt_callback On cancel.
483 */ 483 */
484 FileCopyManager.prototype.requestCancel = function(opt_callback) { 484 FileCopyManager.prototype.requestCancel = function(opt_callback) {
485 this.cancelRequested_ = true; 485 this.cancelRequested_ = true;
486 if (this.cancelCallback_) 486 if (this.cancelCallback_) {
487 this.cancelCallback_(); 487 this.cancelCallback_();
488 this.cancelCallback_ = null;
489 }
488 if (opt_callback) 490 if (opt_callback)
489 this.cancelObservers_.push(opt_callback); 491 this.cancelObservers_.push(opt_callback);
490 492
491 // If there is any active task it will eventually call maybeCancel_. 493 // If there is any active task it will eventually call maybeCancel_.
492 // Otherwise call it right now. 494 // Otherwise call it right now.
493 if (this.copyTasks_.length == 0) 495 if (this.copyTasks_.length == 0)
494 this.doCancel_(); 496 this.doCancel_();
495 }; 497 };
496 498
497 /** 499 /**
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 var isTargetOnDrive = PathUtil.isDriveBasedPath(targetDirEntry.fullPath); 882 var isTargetOnDrive = PathUtil.isDriveBasedPath(targetDirEntry.fullPath);
881 883
882 if (!isSourceOnDrive && !isTargetOnDrive) { 884 if (!isSourceOnDrive && !isTargetOnDrive) {
883 // Sending a file from local to local. 885 // Sending a file from local to local.
884 // 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
885 // progress. 887 // progress.
886 targetDirEntry.getFile( 888 targetDirEntry.getFile(
887 targetRelativePath, 889 targetRelativePath,
888 {create: true, exclusive: true}, 890 {create: true, exclusive: true},
889 function(targetEntry) { 891 function(targetEntry) {
890 self.copyFileEntry_( 892 self.cancelCallback_ = self.copyFileEntry_(
891 sourceEntry, targetEntry, 893 sourceEntry, targetEntry,
892 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 });
893 }, 903 },
894 util.flog('Error getting file: ' + targetRelativePath, 904 util.flog('Error getting file: ' + targetRelativePath,
895 onFilesystemError)); 905 onFilesystemError));
896 return; 906 return;
897 } 907 }
898 908
899 // 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
900 // Drive. 910 // Drive.
901 var sourceFileUrl = sourceEntry.toURL(); 911 var sourceFileUrl = sourceEntry.toURL();
902 var sourceFilePath = util.extractFilePath(sourceFileUrl); 912 var sourceFilePath = util.extractFilePath(sourceFileUrl);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
982 * copied. 992 * copied.
983 * @param {function(FileEntry, number)} progressCallback Function that will be 993 * @param {function(FileEntry, number)} progressCallback Function that will be
984 * 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|
985 * and size of the last copied chunk as parameters. 995 * and size of the last copied chunk as parameters.
986 * @param {function(FileEntry, number)} successCallback Function that will be 996 * @param {function(FileEntry, number)} successCallback Function that will be
987 * called the copy operation finishes. It takes |targetEntry| and size of 997 * called the copy operation finishes. It takes |targetEntry| and size of
988 * the last (not previously reported) copied chunk as parameters. 998 * the last (not previously reported) copied chunk as parameters.
989 * @param {function(FileError)} errorCallback Function that will be called 999 * @param {function(FileError)} errorCallback Function that will be called
990 * 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
991 * 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.
992 * @private 1005 * @private
993 */ 1006 */
994 FileCopyManager.prototype.copyFileEntry_ = function(sourceEntry, 1007 FileCopyManager.prototype.copyFileEntry_ = function(sourceEntry,
995 targetEntry, 1008 targetEntry,
996 progressCallback, 1009 progressCallback,
997 successCallback, 1010 successCallback,
998 errorCallback) { 1011 errorCallback) {
999 if (this.maybeCancel_()) 1012 // Set to true when cancel is requested.
1000 return; 1013 var cancelRequested = false;
1001 1014
1002 var self = this; 1015 sourceEntry.file(function(file) {
1016 if (cancelRequested) {
1017 errorCallback(util.createFileError(FileError.ABORT_ERR));
1018 return;
1019 }
1003 1020
1004 var onSourceFileFound = function(file) { 1021 targetEntry.createWriter(function(writer) {
1005 var onWriterCreated = function(writer) { 1022 if (cancelRequested) {
1023 errorCallback(util.createFileError(FileError.ABORT_ERR));
1024 return;
1025 }
1026
1006 var reportedProgress = 0; 1027 var reportedProgress = 0;
1007 writer.onerror = function(progress) { 1028 writer.onerror = writer.onabort = function(progress) {
1008 errorCallback(writer.error); 1029 errorCallback(cancelRequested ?
1030 util.createFileError(FileError.ABORT_ERR) :
1031 writer.error);
1009 }; 1032 };
1010 1033
1011 writer.onprogress = function(progress) { 1034 writer.onprogress = function(progress) {
1012 if (self.maybeCancel_()) { 1035 if (cancelRequested) {
1013 // 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.
1014 writer.abort(); 1039 writer.abort();
1015 return; 1040 return;
1016 } 1041 }
1042
1017 // |progress.loaded| will contain total amount of data copied by now. 1043 // |progress.loaded| will contain total amount of data copied by now.
1018 // |progressCallback| expects data amount delta from the last progress 1044 // |progressCallback| expects data amount delta from the last progress
1019 // update. 1045 // update.
1020 progressCallback(targetEntry, progress.loaded - reportedProgress); 1046 progressCallback(targetEntry, progress.loaded - reportedProgress);
1021 reportedProgress = progress.loaded; 1047 reportedProgress = progress.loaded;
1022 }; 1048 };
1023 1049
1024 writer.onwrite = function() { 1050 writer.onwrite = function() {
1051 if (cancelRequested) {
1052 errorCallback(util.createFileError(FileError.ABORT_ERR));
1053 return;
1054 }
1055
1025 sourceEntry.getMetadata(function(metadata) { 1056 sourceEntry.getMetadata(function(metadata) {
1026 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(),
1027 '' + Math.round(metadata.modificationTime.getTime() / 1000)); 1064 '' + Math.round(metadata.modificationTime.getTime() / 1000));
1028 successCallback(targetEntry, file.size - reportedProgress); 1065 successCallback(targetEntry, file.size - reportedProgress);
1029 }); 1066 });
1030 }; 1067 };
1031 1068
1032 writer.write(file); 1069 writer.write(file);
1033 }; 1070 }, errorCallback);
1071 }, errorCallback);
1034 1072
1035 targetEntry.createWriter(onWriterCreated, errorCallback); 1073 return function() {
1074 cancelRequested = true;
1036 }; 1075 };
1037
1038 sourceEntry.file(onSourceFileFound, errorCallback);
1039 }; 1076 };
1040 1077
1041 /** 1078 /**
1042 * Moves all entries in the task. 1079 * Moves all entries in the task.
1043 * 1080 *
1044 * @param {FileCopyManager.Task} task A move task to be run. 1081 * @param {FileCopyManager.Task} task A move task to be run.
1045 * @param {function(util.EntryChangedType, Entry)} entryChangedCallback Callback 1082 * @param {function(util.EntryChangedType, Entry)} entryChangedCallback Callback
1046 * invoked when an entry is changed. 1083 * invoked when an entry is changed.
1047 * @param {function()} progressCallback Callback invoked periodically during 1084 * @param {function()} progressCallback Callback invoked periodically during
1048 * the moving. 1085 * the moving.
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1187 if (success) { 1224 if (success) {
1188 task.targetDirEntry.getFile( 1225 task.targetDirEntry.getFile(
1189 destPath, {create: false}, 1226 destPath, {create: false},
1190 function(entry) { 1227 function(entry) {
1191 entryChangedCallback(util.EntryChangedType.CREATE, entry); 1228 entryChangedCallback(util.EntryChangedType.CREATE, entry);
1192 successCallback(); 1229 successCallback();
1193 }, 1230 },
1194 onFilesystemError); 1231 onFilesystemError);
1195 } else { 1232 } else {
1196 onFilesystemError( 1233 onFilesystemError(
1197 Object.create(FileError.prototype, { 1234 util.createFileError(FileError.INVALID_MODIFICATION_ERR));
1198 code: {
1199 get: function() { return FileError.INVALID_MODIFICATION_ERR; }
1200 }
1201 }));
1202 } 1235 }
1203 }; 1236 };
1204 1237
1205 progressCallback(); 1238 progressCallback();
1206 chrome.fileBrowserPrivate.zipSelection(dirURL, selectionURLs, destPath, 1239 chrome.fileBrowserPrivate.zipSelection(dirURL, selectionURLs, destPath,
1207 onZipSelectionComplete); 1240 onZipSelectionComplete);
1208 }; 1241 };
1209 1242
1210 FileCopyManager.Task.deduplicatePath( 1243 FileCopyManager.Task.deduplicatePath(
1211 task.targetDirEntry, destName + '.zip', onDeduplicated, errorCallback); 1244 task.targetDirEntry, destName + '.zip', onDeduplicated, errorCallback);
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
1351 // Assume self.cancelRequested_ == false. 1384 // Assume self.cancelRequested_ == false.
1352 // This moved us from 0 to 1 active tasks, let the servicing begin! 1385 // This moved us from 0 to 1 active tasks, let the servicing begin!
1353 self.serviceAllTasks_(); 1386 self.serviceAllTasks_();
1354 } else { 1387 } else {
1355 // Force to update the progress of butter bar when there are new tasks 1388 // Force to update the progress of butter bar when there are new tasks
1356 // coming while servicing current task. 1389 // coming while servicing current task.
1357 self.eventRouter_.sendProgressEvent('PROGRESS', self.getStatus()); 1390 self.eventRouter_.sendProgressEvent('PROGRESS', self.getStatus());
1358 } 1391 }
1359 }); 1392 });
1360 }; 1393 };
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/file_manager/js/util.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698