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

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

Powered by Google App Engine
This is Rietveld 408576698