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 // If directory files changes too often, don't rescan directory more than once | 5 // If directory files changes too often, don't rescan directory more than once |
6 // per specified interval | 6 // per specified interval |
7 const SIMULTANEOUS_RESCAN_INTERVAL = 1000; | 7 const SIMULTANEOUS_RESCAN_INTERVAL = 1000; |
8 | 8 |
9 /** | 9 /** |
10 * Data model of the file manager. | 10 * Data model of the file manager. |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 this.updateRootsListSelection_(); | 515 this.updateRootsListSelection_(); |
516 this.scan_(onRescanComplete); | 516 this.scan_(onRescanComplete); |
517 | 517 |
518 var e = new cr.Event('directory-changed'); | 518 var e = new cr.Event('directory-changed'); |
519 e.previousDirEntry = this.currentEntry; | 519 e.previousDirEntry = this.currentEntry; |
520 e.newDirEntry = dirEntry; | 520 e.newDirEntry = dirEntry; |
521 e.initial = initial; | 521 e.initial = initial; |
522 this.dispatchEvent(e); | 522 this.dispatchEvent(e); |
523 }, | 523 }, |
524 | 524 |
525 /** | 525 setupPath: function(path, opt_pathResolveCallback, opt_fileSelectCallback) { |
526 * Change the state of the model to reflect the specified path (either a | |
527 * file or directory). | |
528 * | |
529 * @param {string} path The root path to use | |
530 * @param {Function=} opt_loadedCallback Invoked when the entire directory | |
531 * has been loaded and any default file selected. | |
532 * @param {Function=} opt_pathResolveCallback Invoked as soon as the path has | |
533 * been resolved, and called with the base and leaf portions of the path | |
534 * name, and a flag indicating if the entry exists. | |
535 */ | |
536 setupPath: function(path, opt_loadedCallback, opt_pathResolveCallback) { | |
537 // Split the dirname from the basename. | 526 // Split the dirname from the basename. |
538 var ary = path.match(/^(?:(.*)\/)?([^\/]*)$/); | 527 var ary = path.match(/^(?:(.*)\/)?([^\/]*)$/); |
539 var autoSelect = function() { | 528 var autoSelect = this.selectIndex.bind(this, this.autoSelectIndex_); |
540 this.selectIndex(this.autoSelectIndex_); | |
541 if (opt_loadedCallback) | |
542 opt_loadedCallback(); | |
543 }.bind(this); | |
544 | |
545 if (!ary) { | 529 if (!ary) { |
546 console.warn('Unable to split default path: ' + path); | 530 console.warn('Unable to split default path: ' + path); |
547 this.changeDirectoryEntry_(this.root_, autoSelect, true); | 531 this.changeDirectoryEntry_(this.root_, autoSelect, true); |
548 return; | 532 return; |
549 } | 533 } |
550 | 534 |
551 var baseName = ary[1]; | 535 var baseName = ary[1]; |
552 var leafName = ary[2]; | 536 var leafName = ary[2]; |
553 | 537 |
554 function resolveCallback(exists) { | 538 function resolveCallback(exists) { |
555 if (opt_pathResolveCallback) | 539 if (opt_pathResolveCallback) |
556 opt_pathResolveCallback(baseName, leafName, exists); | 540 opt_pathResolveCallback(baseName, leafName, exists); |
557 } | 541 } |
558 | 542 |
559 function onLeafFound(baseDirEntry, leafEntry) { | 543 function onLeafFound(baseDirEntry, leafEntry) { |
560 if (leafEntry.isDirectory) { | 544 if (leafEntry.isDirectory) { |
561 baseName = path; | 545 baseName = path; |
562 leafName = ''; | 546 leafName = ''; |
563 resolveCallback(true); | 547 resolveCallback(true); |
564 this.changeDirectoryEntry_(leafEntry, autoSelect, true); | 548 this.changeDirectoryEntry_(leafEntry, autoSelect, true); |
565 return; | 549 return; |
566 } | 550 } |
567 | 551 |
568 resolveCallback(true); | 552 resolveCallback(true); |
569 // Leaf is an existing file, cd to its parent directory and select it. | 553 // Leaf is an existing file, cd to its parent directory and select it. |
570 this.changeDirectoryEntry_(baseDirEntry, | 554 this.changeDirectoryEntry_(baseDirEntry, |
571 function() { | 555 function() { |
572 this.selectEntry(leafEntry.name); | 556 this.selectEntry(leafEntry.name); |
573 if (opt_loadedCallback) | 557 if (opt_fileSelectCallback) |
574 opt_loadedCallback(); | 558 opt_fileSelectCallback(); |
575 }.bind(this), | 559 }.bind(this), |
576 false /*HACK*/); | 560 false /*HACK*/); |
577 // TODO(kaznacheev): Fix history.replaceState for the File Browser and | 561 // TODO(kaznacheev): Fix history.replaceState for the File Browser and |
578 // change the last parameter back to |true|. Passing |false| makes things | 562 // change the last parameter back to |true|. Passing |false| makes things |
579 // less ugly for now. | 563 // less ugly for now. |
580 } | 564 } |
581 | 565 |
582 function onLeafError(baseDirEntry, err) { | 566 function onLeafError(baseDirEntry, err) { |
583 resolveCallback(false); | 567 resolveCallback(false); |
584 // Usually, leaf does not exist, because it's just a suggested file name. | 568 // Usually, leaf does not exist, because it's just a suggested file name. |
585 if (err != FileError.NOT_FOUND_ERR) | 569 if (err != FileError.NOT_FOUND_ERR) |
586 console.log('Unexpected error resolving default leaf: ' + err); | 570 console.log('Unexpected error resolving default leaf: ' + err); |
587 this.changeDirectoryEntry_(baseDirEntry, autoSelect, true); | 571 this.changeDirectoryEntry_(baseDirEntry, autoSelect, true); |
588 } | 572 } |
589 | 573 |
590 var onBaseError = function(err) { | 574 var onBaseError = function(err) { |
591 resolveCallback(false); | 575 resolveCallback(false); |
592 console.log('Unexpected error resolving default base "' + | 576 console.log('Unexpected error resolving default base "' + |
593 baseName + '": ' + err); | 577 baseName + '": ' + err); |
594 if (path != '/' + DirectoryModel.DOWNLOADS_DIRECTORY) { | 578 if (path != '/' + DirectoryModel.DOWNLOADS_DIRECTORY) { |
595 // Can't find the provided path, let's go to default one instead. | 579 // Can't find the provided path, let's go to default one instead. |
596 this.setupDefaultPath(opt_loadedCallback); | 580 this.setupDefaultPath(); |
597 } else { | 581 } else { |
598 // Well, we can't find the downloads dir. Let's just show something, | 582 // Well, we can't find the downloads dir. Let's just show something, |
599 // or we will get an infinite recursion. | 583 // or we will get an infinite recursion. |
600 this.changeDirectory('/', opt_loadedCallback, true); | 584 this.changeDirectory('/', undefined, true); |
601 } | 585 } |
602 }.bind(this); | 586 }.bind(this); |
603 | 587 |
604 var onBaseFound = function(baseDirEntry) { | 588 var onBaseFound = function(baseDirEntry) { |
605 if (!leafName) { | 589 if (!leafName) { |
606 // Default path is just a directory, cd to it and we're done. | 590 // Default path is just a directory, cd to it and we're done. |
607 this.changeDirectoryEntry_(baseDirEntry, autoSelect, true); | 591 this.changeDirectoryEntry_(baseDirEntry, autoSelect, true); |
608 return; | 592 return; |
609 } | 593 } |
610 | 594 |
611 util.resolvePath(this.root_, path, | 595 util.resolvePath(this.root_, path, |
612 onLeafFound.bind(this, baseDirEntry), | 596 onLeafFound.bind(this, baseDirEntry), |
613 onLeafError.bind(this, baseDirEntry)); | 597 onLeafError.bind(this, baseDirEntry)); |
614 }.bind(this); | 598 }.bind(this); |
615 | 599 |
616 var root = this.root_; | 600 var root = this.root_; |
617 if (baseName) { | 601 if (baseName) { |
618 root.getDirectory( | 602 root.getDirectory( |
619 baseName, {create: false}, onBaseFound, onBaseError); | 603 baseName, {create: false}, onBaseFound, onBaseError); |
620 } else { | 604 } else { |
621 this.getDefaultDirectory_(function(defaultDir) { | 605 this.getDefaultDirectory_(function(defaultDir) { |
622 baseName = defaultDir; | 606 baseName = defaultDir; |
623 root.getDirectory( | 607 root.getDirectory( |
624 baseName, {create: false}, onBaseFound, onBaseError); | 608 baseName, {create: false}, onBaseFound, onBaseError); |
625 }); | 609 }); |
626 } | 610 } |
627 }, | 611 }, |
628 | 612 |
629 setupDefaultPath: function(opt_callback) { | 613 setupDefaultPath: function() { |
630 this.getDefaultDirectory_(function(path) { | 614 this.getDefaultDirectory_(this.setupPath.bind(this)); |
631 this.setupPath(path, opt_callback); | |
632 }.bind(this)); | |
633 }, | 615 }, |
634 | 616 |
635 getDefaultDirectory_: function(callback) { | 617 getDefaultDirectory_: function(callback) { |
636 function onGetDirectoryComplete(entries, error) { | 618 function onGetDirectoryComplete(entries, error) { |
637 if (entries.length > 0) | 619 if (entries.length > 0) |
638 callback(entries[0].fullPath); | 620 callback(entries[0].fullPath); |
639 else | 621 else |
640 callback('/' + DirectoryModel.DOWNLOADS_DIRECTORY); | 622 callback('/' + DirectoryModel.DOWNLOADS_DIRECTORY); |
641 } | 623 } |
642 | 624 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
767 root.getDirectory(DirectoryModel.DOWNLOADS_DIRECTORY, { create: false }, | 749 root.getDirectory(DirectoryModel.DOWNLOADS_DIRECTORY, { create: false }, |
768 onDownloads, onDownloadsError); | 750 onDownloads, onDownloadsError); |
769 util.readDirectory(root, DirectoryModel.ARCHIVE_DIRECTORY, | 751 util.readDirectory(root, DirectoryModel.ARCHIVE_DIRECTORY, |
770 append.bind(this, 'archives')); | 752 append.bind(this, 'archives')); |
771 util.readDirectory(root, DirectoryModel.REMOVABLE_DIRECTORY, | 753 util.readDirectory(root, DirectoryModel.REMOVABLE_DIRECTORY, |
772 append.bind(this, 'removables')); | 754 append.bind(this, 'removables')); |
773 root.getDirectory(DirectoryModel.GDATA_DIRECTORY, { create: false }, | 755 root.getDirectory(DirectoryModel.GDATA_DIRECTORY, { create: false }, |
774 onGData, onGDataError); | 756 onGData, onGDataError); |
775 }, | 757 }, |
776 | 758 |
777 updateRoots: function(opt_callback) { | 759 updateRoots: function(opt_changeDirectoryTo) { |
778 var self = this; | 760 var self = this; |
779 this.resolveRoots_(function(rootEntries) { | 761 this.resolveRoots_(function(rootEntries) { |
780 var dm = self.rootsList_; | 762 var dm = self.rootsList_; |
781 var args = [0, dm.length].concat(rootEntries); | 763 var args = [0, dm.length].concat(rootEntries); |
782 dm.splice.apply(dm, args); | 764 dm.splice.apply(dm, args); |
783 | 765 |
784 self.updateRootsListSelection_(); | 766 self.updateRootsListSelection_(); |
785 if (opt_callback) | 767 |
786 opt_callback(); | 768 if (opt_changeDirectoryTo) |
| 769 self.changeDirectory(opt_changeDirectoryTo); |
787 }); | 770 }); |
788 }, | 771 }, |
789 | 772 |
790 onRootsSelectionChanged_: function(event) { | 773 onRootsSelectionChanged_: function(event) { |
791 var root = this.rootsList.item(this.rootsListSelection.selectedIndex); | 774 var root = this.rootsList.item(this.rootsListSelection.selectedIndex); |
792 var current = this.currentEntry.fullPath; | 775 var current = this.currentEntry.fullPath; |
793 if (root && this.rootPath != root.fullPath) | 776 if (root && this.rootPath != root.fullPath) |
794 this.changeDirectory(root.fullPath); | 777 this.changeDirectory(root.fullPath); |
795 }, | 778 }, |
796 | 779 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
926 | 909 |
927 recordMetrics_: function() { | 910 recordMetrics_: function() { |
928 metrics.recordInterval('DirectoryScan'); | 911 metrics.recordInterval('DirectoryScan'); |
929 if (this.dir_.fullPath == | 912 if (this.dir_.fullPath == |
930 '/' + DirectoryModel.DOWNLOADS_DIRECTORY) { | 913 '/' + DirectoryModel.DOWNLOADS_DIRECTORY) { |
931 metrics.recordMediumCount("DownloadsCount", this.list_.length); | 914 metrics.recordMediumCount("DownloadsCount", this.list_.length); |
932 } | 915 } |
933 } | 916 } |
934 }; | 917 }; |
935 | 918 |
OLD | NEW |