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 var SIMULTANEOUS_RESCAN_INTERVAL = 1000; | 7 var SIMULTANEOUS_RESCAN_INTERVAL = 1000; |
8 // Used for operations that require almost instant rescan. | 8 // Used for operations that require almost instant rescan. |
9 var SHORT_RESCAN_INTERVAL = 100; | 9 var SHORT_RESCAN_INTERVAL = 100; |
10 | 10 |
(...skipping 789 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
800 function onLeafError(baseDirEntry, err) { | 800 function onLeafError(baseDirEntry, err) { |
801 // Usually, leaf does not exist, because it's just a suggested file name. | 801 // Usually, leaf does not exist, because it's just a suggested file name. |
802 if (err.code != FileError.NOT_FOUND_ERR) | 802 if (err.code != FileError.NOT_FOUND_ERR) |
803 console.log('Unexpected error resolving default leaf: ' + err); | 803 console.log('Unexpected error resolving default leaf: ' + err); |
804 changeDirectoryEntry(baseDirEntry, autoSelect, INITIAL, !EXISTS); | 804 changeDirectoryEntry(baseDirEntry, autoSelect, INITIAL, !EXISTS); |
805 } | 805 } |
806 | 806 |
807 var onBaseError = function(err) { | 807 var onBaseError = function(err) { |
808 console.log('Unexpected error resolving default base "' + | 808 console.log('Unexpected error resolving default base "' + |
809 baseName + '": ' + err); | 809 baseName + '": ' + err); |
810 if (path != '/' + DirectoryModel.DOWNLOADS_DIRECTORY) { | 810 if (path != this.getDefaultDirectory()) { |
811 // Can't find the provided path, let's go to default one instead. | 811 // Can't find the provided path, let's go to default one instead. |
812 resolveCallback(!EXISTS); | 812 resolveCallback(!EXISTS); |
813 if (!overridden) | 813 if (!overridden) |
814 this.setupDefaultPath(opt_loadedCallback); | 814 this.setupDefaultPath(opt_loadedCallback); |
815 } else { | 815 } else { |
816 // Well, we can't find the downloads dir. Let's just show something, | 816 // Well, we can't find the downloads dir. Let's just show something, |
817 // or we will get an infinite recursion. | 817 // or we will get an infinite recursion. |
818 changeDirectoryEntry(this.root_, opt_loadedCallback, INITIAL, !EXISTS); | 818 changeDirectoryEntry(this.root_, opt_loadedCallback, INITIAL, !EXISTS); |
819 } | 819 } |
820 }.bind(this); | 820 }.bind(this); |
821 | 821 |
822 var onBaseFound = function(baseDirEntry) { | 822 var onBaseFound = function(baseDirEntry) { |
823 if (!leafName) { | 823 if (!leafName) { |
824 // Default path is just a directory, cd to it and we're done. | 824 // Default path is just a directory, cd to it and we're done. |
825 changeDirectoryEntry(baseDirEntry, autoSelect, INITIAL, !EXISTS); | 825 changeDirectoryEntry(baseDirEntry, autoSelect, INITIAL, !EXISTS); |
826 return; | 826 return; |
827 } | 827 } |
828 | 828 |
829 util.resolvePath(this.root_, path, | 829 util.resolvePath(this.root_, path, |
830 onLeafFound.bind(this, baseDirEntry), | 830 onLeafFound.bind(this, baseDirEntry), |
831 onLeafError.bind(this, baseDirEntry)); | 831 onLeafError.bind(this, baseDirEntry)); |
832 }.bind(this); | 832 }.bind(this); |
833 | 833 |
834 var root = this.root_; | 834 var root = this.root_; |
835 if (baseName) { | 835 if (!baseName) |
836 root.getDirectory( | 836 baseName = this.getDefaultDirectory(); |
837 baseName, {create: false}, onBaseFound, onBaseError); | 837 root.getDirectory(baseName, {create: false}, onBaseFound, onBaseError); |
838 } else { | |
839 this.getDefaultDirectory_(function(defaultDir) { | |
840 baseName = defaultDir; | |
841 root.getDirectory( | |
842 baseName, {create: false}, onBaseFound, onBaseError); | |
843 }); | |
844 } | |
845 }; | 838 }; |
846 | 839 |
847 /** | 840 /** |
848 * @param {function} opt_callback Callback on done. | 841 * @param {function} opt_callback Callback on done. |
849 */ | 842 */ |
850 DirectoryModel.prototype.setupDefaultPath = function(opt_callback) { | 843 DirectoryModel.prototype.setupDefaultPath = function(opt_callback) { |
851 var overridden = false; | 844 this.setupPath(this.getDefaultDirectory(), opt_callback); |
852 function onExternalDirChange() { | |
853 overridden = true; | |
854 } | |
855 this.addEventListener('directory-changed', onExternalDirChange); | |
856 | |
857 this.getDefaultDirectory_(function(path) { | |
858 this.removeEventListener('directory-changed', onExternalDirChange); | |
859 if (!overridden) | |
860 this.setupPath(path, opt_callback); | |
861 }.bind(this)); | |
862 }; | 845 }; |
863 | 846 |
864 /** | 847 /** |
865 * @private | 848 * @return {string} The default directory. |
866 * @param {function(string)} callback Called with the path to directory. | |
867 */ | 849 */ |
868 DirectoryModel.prototype.getDefaultDirectory_ = function(callback) { | 850 DirectoryModel.prototype.getDefaultDirectory = function() { |
869 function onGetDirectoryComplete(entries, error) { | 851 return '/' + DirectoryModel.DOWNLOADS_DIRECTORY; |
870 if (entries.length > 0) | |
871 callback(entries[0].fullPath); | |
872 else | |
873 callback('/' + DirectoryModel.DOWNLOADS_DIRECTORY); | |
874 } | |
875 | |
876 // No preset given, find a good place to start. | |
877 // Check for removable devices, if there are none, go to Downloads. | |
878 util.readDirectory(this.root_, DirectoryModel.REMOVABLE_DIRECTORY, | |
879 onGetDirectoryComplete); | |
880 }; | 852 }; |
881 | 853 |
882 /** | 854 /** |
883 * @param {string} name Filename. | 855 * @param {string} name Filename. |
884 */ | 856 */ |
885 DirectoryModel.prototype.selectEntry = function(name) { | 857 DirectoryModel.prototype.selectEntry = function(name) { |
886 var dm = this.fileList_; | 858 var dm = this.fileList_; |
887 for (var i = 0; i < dm.length; i++) { | 859 for (var i = 0; i < dm.length; i++) { |
888 if (dm.item(i).name == name) { | 860 if (dm.item(i).name == name) { |
889 this.selectIndex(i); | 861 this.selectIndex(i); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1060 if (roots.item(index).fullPath == path) | 1032 if (roots.item(index).fullPath == path) |
1061 return index; | 1033 return index; |
1062 } | 1034 } |
1063 return -1; | 1035 return -1; |
1064 }; | 1036 }; |
1065 | 1037 |
1066 /** | 1038 /** |
1067 * @private | 1039 * @private |
1068 */ | 1040 */ |
1069 DirectoryModel.prototype.updateRootsListSelection_ = function() { | 1041 DirectoryModel.prototype.updateRootsListSelection_ = function() { |
1070 this.rootsListSelection_.selectedIndex = | 1042 var rootPath = DirectoryModel.getRootPath(this.currentDirEntry_.fullPath); |
1071 this.findRootsListItem_(this.rootPath); | 1043 this.rootsListSelection_.selectedIndex = this.findRootsListItem_(rootPath); |
1072 }; | 1044 }; |
1073 | 1045 |
1074 /** | 1046 /** |
1075 * @private | 1047 * @private |
1076 */ | 1048 */ |
1077 DirectoryModel.prototype.updateReadonlyStatus_ = function() { | 1049 DirectoryModel.prototype.updateReadonlyStatus_ = function() { |
1078 switch (this.getRootType()) { | 1050 switch (this.getRootType()) { |
1079 case DirectoryModel.RootType.REMOVABLE: | 1051 case DirectoryModel.RootType.REMOVABLE: |
1080 this.readonly_ = !!this.currentVolumeMetadata_.isReadOnly; | 1052 this.readonly_ = !!this.currentVolumeMetadata_.isReadOnly; |
1081 break; | 1053 break; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1133 // We never need to remove this attribute because even if the unmount fails | 1105 // We never need to remove this attribute because even if the unmount fails |
1134 // the onMountCompleted handler calls updateRoots which creates a new entry | 1106 // the onMountCompleted handler calls updateRoots which creates a new entry |
1135 // object for this volume. | 1107 // object for this volume. |
1136 entry.unmounting = true; | 1108 entry.unmounting = true; |
1137 | 1109 |
1138 // Re-place the entry into the roots data model to force re-rendering. | 1110 // Re-place the entry into the roots data model to force re-rendering. |
1139 this.rootsList_.splice(index, 1, entry); | 1111 this.rootsList_.splice(index, 1, entry); |
1140 | 1112 |
1141 if (rootPath == this.rootPath) { | 1113 if (rootPath == this.rootPath) { |
1142 // TODO(kaznacheev): Consider changing to the most recently used root. | 1114 // TODO(kaznacheev): Consider changing to the most recently used root. |
1143 this.changeDirectory('/' + DirectoryModel.DOWNLOADS_DIRECTORY); | 1115 this.changeDirectory(this.getDefaultDirectory()); |
1144 } | 1116 } |
1145 }; | 1117 }; |
1146 | 1118 |
1147 /** | 1119 /** |
1148 * @param {string} path Any path. | 1120 * @param {string} path Any path. |
1149 * @return {string} The root path. | 1121 * @return {string} The root path. |
1150 */ | 1122 */ |
1151 DirectoryModel.getRootPath = function(path) { | 1123 DirectoryModel.getRootPath = function(path) { |
1152 var type = DirectoryModel.getRootType(path); | 1124 var type = DirectoryModel.getRootType(path); |
1153 | 1125 |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1298 /** | 1270 /** |
1299 * @private | 1271 * @private |
1300 */ | 1272 */ |
1301 DirectoryModel.Scanner.prototype.recordMetrics_ = function() { | 1273 DirectoryModel.Scanner.prototype.recordMetrics_ = function() { |
1302 metrics.recordInterval('DirectoryScan'); | 1274 metrics.recordInterval('DirectoryScan'); |
1303 if (this.dir_.fullPath == | 1275 if (this.dir_.fullPath == |
1304 '/' + DirectoryModel.DOWNLOADS_DIRECTORY) { | 1276 '/' + DirectoryModel.DOWNLOADS_DIRECTORY) { |
1305 metrics.recordMediumCount('DownloadsCount', this.list_.length); | 1277 metrics.recordMediumCount('DownloadsCount', this.list_.length); |
1306 } | 1278 } |
1307 }; | 1279 }; |
OLD | NEW |