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_ = []; |
| 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 Loading... |
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 Loading... |
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 * @private |
| 994 */ |
| 995 FileCopyManager.prototype.findDeleteTaskAndCancelTimeout_ = function(id) { |
| 996 for (var index = 0; index < this.deleteTasks_.length; index++) { |
| 997 var task = this.deleteTasks_[index]; |
| 998 if (task.id == id) { |
| 999 this.deleteTasks_.splice(index, 1); |
| 1000 if (task.timeout) { |
| 1001 clearTimeout(task.timeout); |
| 1002 task.timeout = null; |
| 1003 } |
| 1004 return task; |
| 1005 } |
| 1006 } |
| 1007 return null; |
| 1008 }; |
| 1009 |
| 1010 /** |
| 1011 * Performs the deletion. |
| 1012 * @param {object} task The delete task (see deleteEntries function). |
| 1013 * @private |
| 1014 */ |
| 1015 FileCopyManager.prototype.serviceDeleteTask_ = function(task) { |
| 1016 var downcount = task.entries.length + 1; |
| 1017 |
| 1018 var onComplete = function() { |
| 1019 if (--downcount == 0) |
| 1020 this.sendDeleteEvent_(task, 'SUCCESS'); |
| 1021 }.bind(this); |
| 1022 |
| 1023 for (var i = 0; i < task.entries.length; i++) { |
| 1024 var entry = task.entries[i]; |
| 1025 util.removeFileOrDirectory( |
| 1026 entry, |
| 1027 onComplete, |
| 1028 onComplete); // We ignore error, because we can't do anything here. |
| 1029 } |
| 1030 onComplete(); |
| 1031 }; |
| 1032 |
| 1033 /** |
| 1034 * Send a 'delete' event to listeners. |
| 1035 * @param {Object} task The delete task (see deleteEntries function). |
| 1036 * @param {string} reason Event reason. |
| 1037 * @private |
| 1038 */ |
| 1039 FileCopyManager.prototype.sendDeleteEvent_ = function(task, reason) { |
| 1040 this.sendEvent_('delete', { |
| 1041 reason: reason, |
| 1042 id: task.id, |
| 1043 urls: task.entries.map(function(e) { |
| 1044 return util.makeFilesystemUrl(e.fullPath); |
| 1045 }) |
| 1046 }); |
| 1047 }; |
OLD | NEW |