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

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

Issue 10914075: [filemanager] Implementing undo delete functionality. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 3 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
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 /** 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_ = [];
11 this.deleteTasks_ = [];
12 this.lastDeleteId_ = 0;
11 this.cancelObservers_ = []; 13 this.cancelObservers_ = [];
12 this.cancelRequested_ = false; 14 this.cancelRequested_ = false;
13 this.root_ = root; 15 this.root_ = root;
14 this.unloadTimeout_ = null; 16 this.unloadTimeout_ = null;
15 } 17 }
16 18
17 var fileCopyManagerInstance = null; 19 var fileCopyManagerInstance = null;
18 20
19 /** 21 /**
20 * Get FileCopyManager instance. In case is hasn't been initialized, a new 22 * Get FileCopyManager instance. In case is hasn't been initialized, a new
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 * @param {Object} eventArgs An object with arbitrary event parameters. 266 * @param {Object} eventArgs An object with arbitrary event parameters.
265 */ 267 */
266 FileCopyManager.prototype.sendEvent_ = function(eventName, eventArgs) { 268 FileCopyManager.prototype.sendEvent_ = function(eventName, eventArgs) {
267 var windows = chrome.extension.getViews(); 269 var windows = chrome.extension.getViews();
268 for (var i = 0; i < windows.length; i++) { 270 for (var i = 0; i < windows.length; i++) {
269 var w = windows[i]; 271 var w = windows[i];
270 if (w.fileCopyManagerWrapper) 272 if (w.fileCopyManagerWrapper)
271 w.fileCopyManagerWrapper.onEvent(eventName, eventArgs); 273 w.fileCopyManagerWrapper.onEvent(eventName, eventArgs);
272 } 274 }
273 275
274 if (this.copyTasks_.length === 0) { 276 if (this.copyTasks_.length === 0 && this.deleteTasks_.length === 0) {
275 if (this.unloadTimeout_ === null) 277 if (this.unloadTimeout_ === null)
276 this.unloadTimeout_ = setTimeout(close, 5000); 278 this.unloadTimeout_ = setTimeout(close, 5000);
277 } else { 279 } else {
278 this.unloadTimeout_ = null; 280 this.unloadTimeout_ = null;
279 } 281 }
280 }; 282 };
281 283
282 /** 284 /**
283 * Write to console.log on all the active FileManager windows. 285 * Write to console.log on all the active FileManager windows.
284 * @private 286 * @private
(...skipping 650 matching lines...) Expand 10 before | Expand all | Expand 10 after
935 }; 937 };
936 938
937 writer.write(file); 939 writer.write(file);
938 } 940 }
939 941
940 targetEntry.createWriter(onWriterCreated, errorCallback); 942 targetEntry.createWriter(onWriterCreated, errorCallback);
941 } 943 }
942 944
943 sourceEntry.file(onSourceFileFound, errorCallback); 945 sourceEntry.file(onSourceFileFound, errorCallback);
944 }; 946 };
947
948 /**
949 * Timeout before files are really deleted (to allow undo).
950 */
951 FileCopyManager.DELETE_TIMEOUT = 15 * 1000;
952
953 /**
954 * Schedules the files deletion.
955 * @param {Array.<Entry>} entries The entries.
956 * @param {function(number)} callback Callback gets the scheduled task id.
957 */
958 FileCopyManager.prototype.deleteEntries = function(entries, callback) {
959 var id = ++this.lastDeleteId_;
960 var task = {
961 entries: entries,
962 id: id,
963 timeout: setTimeout(this.forceDeleteTask.bind(this, id),
964 FileCopyManager.DELETE_TIMEOUT)
965 };
966 this.deleteTasks_.push(task);
967 callback(id);
968 this.sendDeleteEvent_(task, 'SCHEDULED');
969 };
970
971 /**
972 * Force deletion before timeout runs out.
973 * @param {number} id The delete task id (as returned by deleteEntries).
974 */
975 FileCopyManager.prototype.forceDeleteTask = function(id) {
976 var task = this.findDeleteTaskAndCancelTimeout_(id);
977 if (task) this.serviceDeleteTask_(task);
978 };
979
980 /**
981 * Cancels the scheduled deletion.
982 * @param {number} id The delete task id (as returned by deleteEntries).
983 */
984 FileCopyManager.prototype.cancelDeleteTask = function(id) {
985 var task = this.findDeleteTaskAndCancelTimeout_(id);
986 if (task) this.sendDeleteEvent_(task, 'CANCELLED');
987 };
988
989 /**
990 * Finds the delete task, removes it from list and cancels the timeout.
991 * @param {number} id The delete task id (as returned by deleteEntries).
992 * @return {object} The delete task.
993 */
994 FileCopyManager.prototype.findDeleteTaskAndCancelTimeout_ = function(id) {
995 for (var index = 0; index < this.deleteTasks_.length; index++) {
996 var task = this.deleteTasks_[index];
997 if (task.id == id) {
998 this.deleteTasks_.splice(index, 1);
999 if (task.timeout) {
1000 clearTimeout(task.timeout);
1001 task.timeout = null;
1002 }
1003 return task;
1004 }
1005 }
1006 return null;
1007 };
1008
1009 /**
1010 * Performs the deletion.
1011 * @param {object} task The delete task (see deleteEntries function).
1012 */
1013 FileCopyManager.prototype.serviceDeleteTask_ = function(task) {
1014 var downcount = task.entries.length + 1;
1015
1016 var onComplete = function() {
1017 if (--downcount == 0)
1018 this.sendDeleteEvent_(task, 'SUCCESS');
1019 }.bind(this);
1020
1021 for (var i = 0; i < task.entries.length; i++) {
1022 var entry = task.entries[i];
1023 util.removeFileOrDirectory(
1024 entry,
1025 onComplete,
1026 onComplete); // We ignore error, because we can't do anything here.
1027 }
1028 onComplete();
1029 };
1030
1031 FileCopyManager.prototype.sendDeleteEvent_ = function(task, reason) {
1032 this.sendEvent_('delete', {
1033 reason: reason,
1034 id: task.id,
1035 urls: task.entries.map(function(e) {
1036 return util.makeFilesystemUrl(e.fullPath)
1037 })
1038 });
1039 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698