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

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

Powered by Google App Engine
This is Rietveld 408576698