Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(875)

Side by Side Diff: chrome/browser/resources/file_manager/js/directory_model.js

Issue 9379023: Tweaks for improving file manager performance (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Finish plumbing callbacks through Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 507 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 this.updateRootsListSelection_(); 518 this.updateRootsListSelection_();
519 this.scan_(onRescanComplete); 519 this.scan_(onRescanComplete);
520 520
521 var e = new cr.Event('directory-changed'); 521 var e = new cr.Event('directory-changed');
522 e.previousDirEntry = this.currentEntry; 522 e.previousDirEntry = this.currentEntry;
523 e.newDirEntry = dirEntry; 523 e.newDirEntry = dirEntry;
524 e.initial = initial; 524 e.initial = initial;
525 this.dispatchEvent(e); 525 this.dispatchEvent(e);
526 }, 526 },
527 527
528 setupPath: function(path, opt_pathResolveCallback) { 528 /**
529 * Change the state of the model to reflect the specified path (either a
530 * file or directory).
531 *
532 * @param {string} path The root path to use
533 * @param {function} opt_loadedCallback Invoked when the entire directory has
534 * been loaded.
535 * @param {function} opt_pathResolveCallback Invoked as soon as the path has
536 * been resolved, and called with the base and leaf portions of the path name.
537 */
538 setupPath: function(path, opt_loadedCallback, opt_pathResolveCallback) {
529 // Split the dirname from the basename. 539 // Split the dirname from the basename.
530 var ary = path.match(/^(?:(.*)\/)?([^\/]*)$/); 540 var ary = path.match(/^(?:(.*)\/)?([^\/]*)$/);
531 var autoSelect = this.selectIndex.bind(this, this.autoSelectIndex_); 541 var autoSelect = function() {
542 this.selectIndex(this.autoSelectIndex_);
543 if (opt_loadedCallback)
544 opt_loadedCallback();
545 }.bind(this);
546
532 if (!ary) { 547 if (!ary) {
533 console.warn('Unable to split default path: ' + path); 548 console.warn('Unable to split default path: ' + path);
534 this.changeDirectoryEntry_(this.root_, autoSelect, true); 549 this.changeDirectoryEntry_(this.root_, autoSelect, true);
535 return; 550 return;
536 } 551 }
537 552
538 var baseName = ary[1]; 553 var baseName = ary[1];
539 var leafName = ary[2]; 554 var leafName = ary[2];
540 555
541 function callBack() { 556 function resolvedCallback() {
542 if (opt_pathResolveCallback) 557 if (opt_pathResolveCallback)
543 opt_pathResolveCallback(baseName, leafName); 558 opt_pathResolveCallback(baseName, leafName);
544 } 559 }
545 560
546 function onLeafFound(baseDirEntry, leafEntry) { 561 function onLeafFound(baseDirEntry, leafEntry) {
547 if (leafEntry.isDirectory) { 562 if (leafEntry.isDirectory) {
548 baseName = path; 563 baseName = path;
549 leafName = ''; 564 leafName = '';
550 callBack(); 565 resolvedCallback();
551 this.changeDirectoryEntry_(leafEntry, autoSelect, true); 566 this.changeDirectoryEntry_(leafEntry, autoSelect, true);
552 return; 567 return;
553 } 568 }
554 569
555 callBack(); 570 resolvedCallback();
556 // Leaf is an existing file, cd to its parent directory and select it. 571 // Leaf is an existing file, cd to its parent directory and select it.
557 this.changeDirectoryEntry_(baseDirEntry, 572 var loadedCallback = function() {
558 this.selectEntry.bind(this, leafEntry.name), 573 this.selectEntry(leafEntry.name);
559 true); 574 if (opt_loadedCallback)
575 opt_loadedCallback();
576 }.bind(this);
577 this.changeDirectoryEntry_(baseDirEntry, loadedCallback, true);
560 } 578 }
561 579
562 function onLeafError(baseDirEntry, err) { 580 function onLeafError(baseDirEntry, err) {
563 callBack(); 581 resolvedCallback();
564 // Usually, leaf does not exist, because it's just a suggested file name. 582 // Usually, leaf does not exist, because it's just a suggested file name.
565 if (err != FileError.NOT_FOUND_ERR) 583 if (err != FileError.NOT_FOUND_ERR)
566 console.log('Unexpected error resolving default leaf: ' + err); 584 console.log('Unexpected error resolving default leaf: ' + err);
567 this.changeDirectoryEntry_(baseDirEntry, autoSelect, true); 585 this.changeDirectoryEntry_(baseDirEntry, autoSelect, true);
568 } 586 }
569 587
570 var onBaseError = function(err) { 588 var onBaseError = function(err) {
571 callBack(); 589 resolvedCallback();
572 console.log('Unexpected error resolving default base "' + 590 console.log('Unexpected error resolving default base "' +
573 baseName + '": ' + err); 591 baseName + '": ' + err);
574 if (path != '/' + DirectoryModel.DOWNLOADS_DIRECTORY) { 592 if (path != '/' + DirectoryModel.DOWNLOADS_DIRECTORY) {
575 // Can't find the provided path, let's go to default one instead. 593 // Can't find the provided path, let's go to default one instead.
576 this.setupDefaultPath(); 594 this.setupDefaultPath(opt_loadedCallback);
577 } else { 595 } else {
578 // Well, we can't find the downloads dir. Let's just show something, 596 // Well, we can't find the downloads dir. Let's just show something,
579 // or we will get an infinite recursion. 597 // or we will get an infinite recursion.
580 this.changeDirectory('/', undefined, true); 598 this.changeDirectory('/', opt_loadedCallback, true);
581 } 599 }
582 }.bind(this); 600 }.bind(this);
583 601
584 var onBaseFound = function(baseDirEntry) { 602 var onBaseFound = function(baseDirEntry) {
585 if (!leafName) { 603 if (!leafName) {
586 // Default path is just a directory, cd to it and we're done. 604 // Default path is just a directory, cd to it and we're done.
587 this.changeDirectoryEntry_(baseDirEntry, autoSelect, true); 605 this.changeDirectoryEntry_(baseDirEntry, autoSelect, true);
588 return; 606 return;
589 } 607 }
590 608
591 util.resolvePath(this.root_, path, 609 util.resolvePath(this.root_, path,
592 onLeafFound.bind(this, baseDirEntry), 610 onLeafFound.bind(this, baseDirEntry),
593 onLeafError.bind(this, baseDirEntry)); 611 onLeafError.bind(this, baseDirEntry));
594 }.bind(this); 612 }.bind(this);
595 613
596 var root = this.root_; 614 var root = this.root_;
597 if (baseName) { 615 if (baseName) {
598 root.getDirectory( 616 root.getDirectory(
599 baseName, {create: false}, onBaseFound, onBaseError); 617 baseName, {create: false}, onBaseFound, onBaseError);
600 } else { 618 } else {
601 this.getDefaultDirectory_(function(defaultDir) { 619 this.getDefaultDirectory_(function(defaultDir) {
602 baseName = defaultDir; 620 baseName = defaultDir;
603 root.getDirectory( 621 root.getDirectory(
604 baseName, {create: false}, onBaseFound, onBaseError); 622 baseName, {create: false}, onBaseFound, onBaseError);
605 }); 623 });
606 } 624 }
607 }, 625 },
608 626
609 setupDefaultPath: function() { 627 setupDefaultPath: function(opt_callback) {
610 this.getDefaultDirectory_(this.setupPath.bind(this)); 628 this.getDefaultDirectory_(function(path) {
629 this.setupPath(path, opt_callback);
630 }.bind(this));
611 }, 631 },
612 632
613 getDefaultDirectory_: function(callback) { 633 getDefaultDirectory_: function(callback) {
614 function onGetDirectoryComplete(entries, error) { 634 function onGetDirectoryComplete(entries, error) {
615 if (entries.length > 0) 635 if (entries.length > 0)
616 callback(entries[0].fullPath); 636 callback(entries[0].fullPath);
617 else 637 else
618 callback('/' + DirectoryModel.DOWNLOADS_DIRECTORY); 638 callback('/' + DirectoryModel.DOWNLOADS_DIRECTORY);
619 } 639 }
620 640
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 751
732 var root = this.root_; 752 var root = this.root_;
733 root.getDirectory(DirectoryModel.DOWNLOADS_DIRECTORY, { create: false }, 753 root.getDirectory(DirectoryModel.DOWNLOADS_DIRECTORY, { create: false },
734 onDownloads, onDownloadsError); 754 onDownloads, onDownloadsError);
735 util.readDirectory(root, DirectoryModel.ARCHIVE_DIRECTORY, 755 util.readDirectory(root, DirectoryModel.ARCHIVE_DIRECTORY,
736 append.bind(this, 'archives')); 756 append.bind(this, 'archives'));
737 util.readDirectory(root, DirectoryModel.REMOVABLE_DIRECTORY, 757 util.readDirectory(root, DirectoryModel.REMOVABLE_DIRECTORY,
738 append.bind(this, 'removables')); 758 append.bind(this, 'removables'));
739 }, 759 },
740 760
741 updateRoots: function(opt_changeDirectoryTo) { 761 updateRoots: function(opt_callback) {
742 var self = this; 762 var self = this;
743 this.resolveRoots_(function(rootEntries) { 763 this.resolveRoots_(function(rootEntries) {
744 var dm = self.rootsList_; 764 var dm = self.rootsList_;
745 var args = [0, dm.length].concat(rootEntries); 765 var args = [0, dm.length].concat(rootEntries);
746 dm.splice.apply(dm, args); 766 dm.splice.apply(dm, args);
747 767
748 self.updateRootsListSelection_(); 768 self.updateRootsListSelection_();
749 769 if (opt_callback)
750 if (opt_changeDirectoryTo) 770 opt_callback();
751 self.changeDirectory(opt_changeDirectoryTo);
752 }); 771 });
753 }, 772 },
754 773
755 onRootsSelectionChanged_: function(event) { 774 onRootsSelectionChanged_: function(event) {
756 var root = this.rootsList.item(this.rootsListSelection.selectedIndex); 775 var root = this.rootsList.item(this.rootsListSelection.selectedIndex);
757 var current = this.currentEntry.fullPath; 776 var current = this.currentEntry.fullPath;
758 if (root && this.rootPath != root.fullPath) 777 if (root && this.rootPath != root.fullPath)
759 this.changeDirectory(root.fullPath); 778 this.changeDirectory(root.fullPath);
760 }, 779 },
761 780
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
887 906
888 recordMetrics_: function() { 907 recordMetrics_: function() {
889 metrics.recordInterval('DirectoryScan'); 908 metrics.recordInterval('DirectoryScan');
890 if (this.dir_.fullPath == 909 if (this.dir_.fullPath ==
891 '/' + DirectoryModel.DOWNLOADS_DIRECTORY) { 910 '/' + DirectoryModel.DOWNLOADS_DIRECTORY) {
892 metrics.recordMediumCount("DownloadsCount", this.list_.length); 911 metrics.recordMediumCount("DownloadsCount", this.list_.length);
893 } 912 }
894 } 913 }
895 }; 914 };
896 915
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698