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