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.cancelObservers_ = []; | 11 this.cancelObservers_ = []; |
12 this.cancelRequested_ = false; | 12 this.cancelRequested_ = false; |
13 this.root_ = root; | 13 this.root_ = root; |
| 14 this.unloadTimeout_ = null; |
14 } | 15 } |
15 | 16 |
16 FileCopyManager.prototype = { | 17 var fileCopyManagerInstance = null; |
17 __proto__: cr.EventTarget.prototype | 18 |
| 19 /** |
| 20 * Get FileCopyManager instance. In case is hasn't been initialized, a new |
| 21 * instance is created. |
| 22 * @param {DirectoryEntry} root Root entry. |
| 23 * @return {FileCopyManager} A FileCopyManager instance. |
| 24 */ |
| 25 FileCopyManager.getInstance = function(root) { |
| 26 if (fileCopyManagerInstance === null) { |
| 27 fileCopyManagerInstance = new FileCopyManager(root); |
| 28 } |
| 29 return fileCopyManagerInstance; |
18 }; | 30 }; |
19 | 31 |
20 /** | 32 /** |
21 * A record of a queued copy operation. | 33 * A record of a queued copy operation. |
22 * | 34 * |
23 * Multiple copy operations may be queued at any given time. Additional | 35 * Multiple copy operations may be queued at any given time. Additional |
24 * Tasks may be added while the queue is being serviced. Though a | 36 * Tasks may be added while the queue is being serviced. Though a |
25 * cancel operation cancels everything in the queue. | 37 * cancel operation cancels everything in the queue. |
26 * | 38 * |
27 * @param {DirectoryEntry} sourceDirEntry Source directory. | 39 * @param {DirectoryEntry} sourceDirEntry Source directory. |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 | 242 |
231 var percentage = status.completedBytes / status.totalBytes; | 243 var percentage = status.completedBytes / status.totalBytes; |
232 | 244 |
233 return { | 245 return { |
234 percentage: percentage, | 246 percentage: percentage, |
235 pendingItems: status.pendingItems | 247 pendingItems: status.pendingItems |
236 }; | 248 }; |
237 }; | 249 }; |
238 | 250 |
239 /** | 251 /** |
| 252 * Send an event to all the FileManager windows. |
| 253 * @private |
| 254 * @param {string} eventName Event name. |
| 255 * @param {Object} eventArgs An object with arbitrary event parameters. |
| 256 */ |
| 257 FileCopyManager.prototype.sendEvent_ = function(eventName, eventArgs) { |
| 258 var windows = chrome.extension.getViews(); |
| 259 for (var i = 0; i < windows.length; i++) { |
| 260 var w = windows[i]; |
| 261 if (w.fileCopyManagerWrapper) |
| 262 w.fileCopyManagerWrapper.onEvent(eventName, eventArgs); |
| 263 } |
| 264 |
| 265 if (this.copyTasks_.length === 0) { |
| 266 if (this.unloadTimeout_ === null) |
| 267 this.unloadTimeout_ = setTimeout(close, 5000); |
| 268 } else { |
| 269 this.unloadTimeout_ = null; |
| 270 } |
| 271 }; |
| 272 |
| 273 /** |
| 274 * Write to console.log on all the active FileManager windows. |
| 275 * @private |
| 276 */ |
| 277 FileCopyManager.prototype.log_ = function() { |
| 278 var windows = chrome.extension.getViews(); |
| 279 for (var i = 0; i < windows.length; i++) { |
| 280 windows[i].console.log.apply(windows[i].console, arguments); |
| 281 } |
| 282 }; |
| 283 |
| 284 /** |
240 * Dispatch a simple copy-progress event with reason and optional err data. | 285 * Dispatch a simple copy-progress event with reason and optional err data. |
241 * @private | 286 * @private |
242 * @param {string} reason Event type. | 287 * @param {string} reason Event type. |
243 * @param {FileCopyManager.Error} opt_err Error. | 288 * @param {FileCopyManager.Error} opt_err Error. |
244 */ | 289 */ |
245 FileCopyManager.prototype.sendProgressEvent_ = function(reason, opt_err) { | 290 FileCopyManager.prototype.sendProgressEvent_ = function(reason, opt_err) { |
246 var event = new cr.Event('copy-progress'); | 291 var event = {}; |
247 event.reason = reason; | 292 event.reason = reason; |
248 if (opt_err) | 293 if (opt_err) |
249 event.error = opt_err; | 294 event.error = opt_err; |
250 this.dispatchEvent(event); | 295 this.sendEvent_('copy-progress', event); |
251 }; | 296 }; |
252 | 297 |
253 /** | 298 /** |
254 * Dispatch an event of file operation completion (allows to update the UI). | 299 * Dispatch an event of file operation completion (allows to update the UI). |
255 * @private | 300 * @private |
256 * @param {string} reason Completed file operation: 'movied|copied|deleted'. | 301 * @param {string} reason Completed file operation: 'movied|copied|deleted'. |
257 * @param {Array.<Entry>} affectedEntries deleted ot created entries. | 302 * @param {Array.<Entry>} affectedEntries deleted ot created entries. |
258 */ | 303 */ |
259 FileCopyManager.prototype.sendOperationEvent_ = function(reason, | 304 FileCopyManager.prototype.sendOperationEvent_ = function(reason, |
260 affectedEntries) { | 305 affectedEntries) { |
261 var event = new cr.Event('copy-operation-complete'); | 306 var event = {}; |
262 event.reason = reason; | 307 event.reason = reason; |
263 event.affectedEntries = affectedEntries; | 308 event.affectedEntries = affectedEntries; |
264 this.dispatchEvent(event); | 309 this.sendEvent_('copy-operation-complete', event); |
265 }; | 310 }; |
266 | 311 |
267 /** | 312 /** |
268 * Completely clear out the copy queue, either because we encountered an error | 313 * Completely clear out the copy queue, either because we encountered an error |
269 * or completed successfully. | 314 * or completed successfully. |
270 * @private | 315 * @private |
271 */ | 316 */ |
272 FileCopyManager.prototype.resetQueue_ = function() { | 317 FileCopyManager.prototype.resetQueue_ = function() { |
273 for (var i = 0; i < this.cancelObservers_.length; i++) | 318 for (var i = 0; i < this.cancelObservers_.length; i++) |
274 this.cancelObservers_[i](); | 319 this.cancelObservers_[i](); |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 self.sendOperationEvent_('copied', [entry]); | 664 self.sendOperationEvent_('copied', [entry]); |
620 onCopyCompleteBase(entry, size); | 665 onCopyCompleteBase(entry, size); |
621 } | 666 } |
622 | 667 |
623 function onCopyProgress(entry, size) { | 668 function onCopyProgress(entry, size) { |
624 task.updateFileCopyProgress(entry, size); | 669 task.updateFileCopyProgress(entry, size); |
625 self.sendProgressEvent_('PROGRESS'); | 670 self.sendProgressEvent_('PROGRESS'); |
626 } | 671 } |
627 | 672 |
628 function onError(reason, data) { | 673 function onError(reason, data) { |
629 console.log('serviceNextTaskEntry error: ' + reason + ':', data); | 674 this.log_('serviceNextTaskEntry error: ' + reason + ':', data); |
630 errorCallback(new FileCopyManager.Error(reason, data)); | 675 errorCallback(new FileCopyManager.Error(reason, data)); |
631 } | 676 } |
632 | 677 |
633 function onFilesystemCopyComplete(sourceEntry, targetEntry) { | 678 function onFilesystemCopyComplete(sourceEntry, targetEntry) { |
634 // TODO(benchan): We currently do not know the size of data being | 679 // TODO(benchan): We currently do not know the size of data being |
635 // copied by FileEntry.copyTo(), so task.completedBytes will not be | 680 // copied by FileEntry.copyTo(), so task.completedBytes will not be |
636 // increased. We will address this issue once we need to use | 681 // increased. We will address this issue once we need to use |
637 // task.completedBytes to track the progress. | 682 // task.completedBytes to track the progress. |
638 self.sendOperationEvent_('copied', [sourceEntry, targetEntry]); | 683 self.sendOperationEvent_('copied', [sourceEntry, targetEntry]); |
639 onCopyCompleteBase(targetEntry, 0); | 684 onCopyCompleteBase(targetEntry, 0); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
760 } | 805 } |
761 } | 806 } |
762 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( | 807 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( |
763 onFileTransfersUpdated); | 808 onFileTransfersUpdated); |
764 chrome.fileBrowserPrivate.transferFile( | 809 chrome.fileBrowserPrivate.transferFile( |
765 sourceFileUrl, targetFileUrl, | 810 sourceFileUrl, targetFileUrl, |
766 function() { | 811 function() { |
767 chrome.fileBrowserPrivate.onFileTransfersUpdated.removeListener( | 812 chrome.fileBrowserPrivate.onFileTransfersUpdated.removeListener( |
768 onFileTransfersUpdated); | 813 onFileTransfersUpdated); |
769 if (chrome.extension.lastError) { | 814 if (chrome.extension.lastError) { |
770 console.log( | 815 this.log_( |
771 'Error copying ' + sourceFileUrl + ' to ' + targetFileUrl); | 816 'Error copying ' + sourceFileUrl + ' to ' + targetFileUrl); |
772 onFilesystemError({ | 817 onFilesystemError({ |
773 code: chrome.extension.lastError.message, | 818 code: chrome.extension.lastError.message, |
774 toGDrive: task.targetOnGData, | 819 toGDrive: task.targetOnGData, |
775 sourceFileUrl: sourceFileUrl | 820 sourceFileUrl: sourceFileUrl |
776 }); | 821 }); |
777 } else { | 822 } else { |
778 targetDirEntry.getFile(targetRelativePath, {}, | 823 targetDirEntry.getFile(targetRelativePath, {}, |
779 function(targetEntry) { | 824 function(targetEntry) { |
780 targetEntry.getMetadata(function(metadata) { | 825 targetEntry.getMetadata(function(metadata) { |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
881 }; | 926 }; |
882 | 927 |
883 writer.write(file); | 928 writer.write(file); |
884 } | 929 } |
885 | 930 |
886 targetEntry.createWriter(onWriterCreated, errorCallback); | 931 targetEntry.createWriter(onWriterCreated, errorCallback); |
887 } | 932 } |
888 | 933 |
889 sourceEntry.file(onSourceFileFound, errorCallback); | 934 sourceEntry.file(onSourceFileFound, errorCallback); |
890 }; | 935 }; |
| 936 |
OLD | NEW |