| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 * Utilities for FileOperationManager. | 8 * Utilities for FileOperationManager. |
| 9 */ | 9 */ |
| 10 var fileOperationUtil = {}; | 10 var fileOperationUtil = {}; |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 */ | 405 */ |
| 406 this.totalBytes = 0; | 406 this.totalBytes = 0; |
| 407 | 407 |
| 408 /** | 408 /** |
| 409 * Total number of already processed bytes. Updated periodically. | 409 * Total number of already processed bytes. Updated periodically. |
| 410 * @type {number} | 410 * @type {number} |
| 411 */ | 411 */ |
| 412 this.processedBytes = 0; | 412 this.processedBytes = 0; |
| 413 | 413 |
| 414 /** | 414 /** |
| 415 * Index of the progressing entry in sourceEntries. |
| 416 * @type {number} |
| 417 * @private |
| 418 */ |
| 419 this.processingSourceIndex_ = 0; |
| 420 |
| 421 /** |
| 415 * Set to true when cancel is requested. | 422 * Set to true when cancel is requested. |
| 416 * @private {boolean} | 423 * @private {boolean} |
| 417 */ | 424 */ |
| 418 this.cancelRequested_ = false; | 425 this.cancelRequested_ = false; |
| 419 | 426 |
| 420 /** | 427 /** |
| 421 * Callback to cancel the running process. | 428 * Callback to cancel the running process. |
| 422 * @private {function()} | 429 * @private {function()} |
| 423 */ | 430 */ |
| 424 this.cancelCallback_ = null; | 431 this.cancelCallback_ = null; |
| 425 | 432 |
| 426 // TODO(hidehiko): After we support recursive copy, we don't need this. | 433 // TODO(hidehiko): After we support recursive copy, we don't need this. |
| 427 // If directory already exists, we try to make a copy named 'dir (X)', | 434 // If directory already exists, we try to make a copy named 'dir (X)', |
| 428 // where X is a number. When we do this, all subsequent copies from | 435 // where X is a number. When we do this, all subsequent copies from |
| 429 // inside the subtree should be mapped to the new directory name. | 436 // inside the subtree should be mapped to the new directory name. |
| 430 // For example, if 'dir' was copied as 'dir (1)', then 'dir\file.txt' should | 437 // For example, if 'dir' was copied as 'dir (1)', then 'dir\file.txt' should |
| 431 // become 'dir (1)\file.txt'. | 438 // become 'dir (1)\file.txt'. |
| 432 this.renamedDirectories_ = []; | 439 this.renamedDirectories_ = []; |
| 433 }; | 440 }; |
| 434 | 441 |
| 435 /** | 442 /** |
| 436 * @param {function()} callback When entries resolved. | 443 * @param {function()} callback When entries resolved. |
| 437 */ | 444 */ |
| 438 FileOperationManager.Task.prototype.initialize = function(callback) { | 445 FileOperationManager.Task.prototype.initialize = function(callback) { |
| 439 }; | 446 }; |
| 440 | 447 |
| 441 /** | 448 /** |
| 442 * Updates copy progress status for the entry. | |
| 443 * | |
| 444 * @param {number} size Number of bytes that has been copied since last update. | |
| 445 */ | |
| 446 FileOperationManager.Task.prototype.updateFileCopyProgress = function(size) { | |
| 447 this.completedBytes += size; | |
| 448 }; | |
| 449 | |
| 450 /** | |
| 451 * Requests cancellation of this task. | 449 * Requests cancellation of this task. |
| 452 * When the cancellation is done, it is notified via callbacks of run(). | 450 * When the cancellation is done, it is notified via callbacks of run(). |
| 453 */ | 451 */ |
| 454 FileOperationManager.Task.prototype.requestCancel = function() { | 452 FileOperationManager.Task.prototype.requestCancel = function() { |
| 455 this.cancelRequested_ = true; | 453 this.cancelRequested_ = true; |
| 456 if (this.cancelCallback_) { | 454 if (this.cancelCallback_) { |
| 457 this.cancelCallback_(); | 455 this.cancelCallback_(); |
| 458 this.cancelCallback_ = null; | 456 this.cancelCallback_ = null; |
| 459 } | 457 } |
| 460 }; | 458 }; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 473 FileOperationManager.Task.prototype.run = function( | 471 FileOperationManager.Task.prototype.run = function( |
| 474 entryChangedCallback, progressCallback, successCallback, errorCallback) { | 472 entryChangedCallback, progressCallback, successCallback, errorCallback) { |
| 475 }; | 473 }; |
| 476 | 474 |
| 477 /** | 475 /** |
| 478 * Get states of the task. | 476 * Get states of the task. |
| 479 * TOOD(hirono): Removes this method and sets a task to progress events. | 477 * TOOD(hirono): Removes this method and sets a task to progress events. |
| 480 * @return {object} Status object. | 478 * @return {object} Status object. |
| 481 */ | 479 */ |
| 482 FileOperationManager.Task.prototype.getStatus = function() { | 480 FileOperationManager.Task.prototype.getStatus = function() { |
| 483 var numRemainingItems = this.countRemainingItems(); | 481 var processingEntry = this.sourceEntries[this.processingSourceIndex_]; |
| 484 return { | 482 return { |
| 485 operationType: this.operationType, | 483 operationType: this.operationType, |
| 486 numRemainingItems: numRemainingItems, | 484 numRemainingItems: this.sourceEntries.length - this.processingSourceIndex_, |
| 487 totalBytes: this.totalBytes, | 485 totalBytes: this.totalBytes, |
| 488 processedBytes: this.processedBytes, | 486 processedBytes: this.processedBytes, |
| 489 processingEntry: this.getSingleEntry() | 487 processingEntryName: processingEntry ? processingEntry.name : '' |
| 490 }; | 488 }; |
| 491 }; | 489 }; |
| 492 | 490 |
| 493 /** | 491 /** |
| 494 * Counts the number of remaining items. | 492 * Obtains the number of total processed bytes. |
| 495 * @return {number} Number of remaining items. | 493 * @return {number} Number of total processed bytes. |
| 494 * @private |
| 496 */ | 495 */ |
| 497 FileOperationManager.Task.prototype.countRemainingItems = function() { | 496 FileOperationManager.Task.prototype.calcProcessedBytes_ = function() { |
| 498 var count = 0; | 497 var bytes = 0; |
| 499 for (var i = 0; i < this.processingEntries.length; i++) { | 498 for (var i = 0; i < this.processingSourceIndex_ + 1; i++) { |
| 500 for (var entryURL in this.processingEntries[i]) { | 499 var entryMap = this.processingEntries[i]; |
| 501 count++; | 500 if (!entryMap) |
| 501 break; |
| 502 for (var name in entryMap) { |
| 503 bytes += i < this.processingSourceIndex_ ? |
| 504 entryMap[name].size : entryMap[name].processedBytes; |
| 502 } | 505 } |
| 503 } | 506 } |
| 504 return count; | 507 return bytes; |
| 505 }; | 508 }; |
| 506 | 509 |
| 507 /** | 510 /** |
| 508 * Obtains the single processing entry. If there are multiple processing | |
| 509 * entries, it returns null. | |
| 510 * @return {Entry} First entry. | |
| 511 */ | |
| 512 FileOperationManager.Task.prototype.getSingleEntry = function() { | |
| 513 if (this.countRemainingItems() !== 1) | |
| 514 return null; | |
| 515 for (var i = 0; i < this.processingEntries.length; i++) { | |
| 516 var entryMap = this.processingEntries[i]; | |
| 517 for (var name in entryMap) | |
| 518 return entryMap[name]; | |
| 519 } | |
| 520 return null; | |
| 521 }; | |
| 522 | |
| 523 /** | |
| 524 * Task to copy entries. | 511 * Task to copy entries. |
| 525 * | 512 * |
| 526 * @param {Array.<Entry>} sourceEntries Array of source entries. | 513 * @param {Array.<Entry>} sourceEntries Array of source entries. |
| 527 * @param {DirectoryEntry} targetDirEntry Target directory. | 514 * @param {DirectoryEntry} targetDirEntry Target directory. |
| 528 * @param {boolean} deleteAfterCopy Whether the delete original files after | 515 * @param {boolean} deleteAfterCopy Whether the delete original files after |
| 529 * copy. | 516 * copy. |
| 530 * @constructor | 517 * @constructor |
| 531 * @extends {FileOperationManager.Task} | 518 * @extends {FileOperationManager.Task} |
| 532 */ | 519 */ |
| 533 FileOperationManager.CopyTask = function(sourceEntries, | 520 FileOperationManager.CopyTask = function(sourceEntries, |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 return; | 633 return; |
| 647 } | 634 } |
| 648 progressCallback(); | 635 progressCallback(); |
| 649 this.processEntry_( | 636 this.processEntry_( |
| 650 entry, this.targetDirEntry, | 637 entry, this.targetDirEntry, |
| 651 function(sourceEntry, destinationEntry) { | 638 function(sourceEntry, destinationEntry) { |
| 652 // Finalize the entry's progress state. | 639 // Finalize the entry's progress state. |
| 653 var sourceEntryURL = sourceEntry.toURL(); | 640 var sourceEntryURL = sourceEntry.toURL(); |
| 654 var processedEntry = | 641 var processedEntry = |
| 655 this.processingEntries[index][sourceEntryURL]; | 642 this.processingEntries[index][sourceEntryURL]; |
| 656 if (processedEntry) { | 643 |
| 657 this.processedBytes += | 644 // Update current source index. |
| 658 processedEntry.size - processedEntry.processedBytes; | 645 this.processingSourceIndex_ = index + 1; |
| 659 progressCallback(); | 646 this.processedBytes = this.calcProcessedBytes_(); |
| 660 delete this.processingEntries[index][sourceEntryURL]; | |
| 661 } | |
| 662 | 647 |
| 663 // The destination entry may be null, if the copied file got | 648 // The destination entry may be null, if the copied file got |
| 664 // deleted just after copying. | 649 // deleted just after copying. |
| 665 if (destinationEntry) { | 650 if (destinationEntry) { |
| 666 entryChangedCallback( | 651 entryChangedCallback( |
| 667 util.EntryChangedKind.CREATED, destinationEntry); | 652 util.EntryChangedKind.CREATED, destinationEntry); |
| 668 } | 653 } |
| 669 }.bind(this), | 654 }.bind(this), |
| 670 function(sourceEntry, size) { | 655 function(sourceEntry, size) { |
| 671 var sourceEntryURL = sourceEntry.toURL(); | 656 var sourceEntryURL = sourceEntry.toURL(); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 807 if (this.cancelRequested_) { | 792 if (this.cancelRequested_) { |
| 808 errorCallback(new FileOperationManager.Error( | 793 errorCallback(new FileOperationManager.Error( |
| 809 util.FileOperationErrorType.FILESYSTEM_ERROR, | 794 util.FileOperationErrorType.FILESYSTEM_ERROR, |
| 810 util.createDOMError(util.FileError.ABORT_ERR))); | 795 util.createDOMError(util.FileError.ABORT_ERR))); |
| 811 return; | 796 return; |
| 812 } | 797 } |
| 813 progressCallback(); | 798 progressCallback(); |
| 814 FileOperationManager.MoveTask.processEntry_( | 799 FileOperationManager.MoveTask.processEntry_( |
| 815 entry, this.targetDirEntry, entryChangedCallback, | 800 entry, this.targetDirEntry, entryChangedCallback, |
| 816 function() { | 801 function() { |
| 817 // Erase the processing entry. | 802 // Update current source index. |
| 818 this.processingEntries[index] = {}; | 803 this.processingSourceIndex_ = index + 1; |
| 819 this.processedBytes++; | 804 this.processedBytes = this.calcProcessedBytes_(); |
| 820 callback(); | 805 callback(); |
| 821 }.bind(this), | 806 }.bind(this), |
| 822 errorCallback); | 807 errorCallback); |
| 823 }, | 808 }, |
| 824 function() { | 809 function() { |
| 825 successCallback(); | 810 successCallback(); |
| 826 }.bind(this), | 811 }.bind(this), |
| 827 this); | 812 this); |
| 828 }; | 813 }; |
| 829 | 814 |
| (...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1360 | 1345 |
| 1361 /** | 1346 /** |
| 1362 * Generates new task ID. | 1347 * Generates new task ID. |
| 1363 * | 1348 * |
| 1364 * @return {string} New task ID. | 1349 * @return {string} New task ID. |
| 1365 * @private | 1350 * @private |
| 1366 */ | 1351 */ |
| 1367 FileOperationManager.prototype.generateTaskId_ = function() { | 1352 FileOperationManager.prototype.generateTaskId_ = function() { |
| 1368 return 'file-operation-' + this.taskIdCounter_++; | 1353 return 'file-operation-' + this.taskIdCounter_++; |
| 1369 }; | 1354 }; |
| OLD | NEW |