Commit 20559724 authored by hirono's avatar hirono Committed by Commit bot

Files.app: Start to use new metadata models in DirectoryContents.

 * Prefetch metadata before entries are added to file list.
 * Notify the cache of entry changes.

BUG=410766
TEST=None

Review URL: https://codereview.chromium.org/904003002

Cr-Commit-Position: refs/heads/master@{#315284}
parent 3202364f
......@@ -762,8 +762,8 @@ util.getCurrentLocaleOrDefault = function() {
/**
* Converts array of entries to an array of corresponding URLs.
* @param {Array.<Entry>} entries Input array of entries.
* @return {Array.<string>} Output array of URLs.
* @param {Array<Entry>} entries Input array of entries.
* @return {!Array<string>} Output array of URLs.
*/
util.entriesToURLs = function(entries) {
return entries.map(function(entry) {
......
......@@ -408,6 +408,7 @@ function FileListModel(metadataCache) {
this.metadataCache_ = metadataCache;
// Initialize compare functions.
// TODO(hirono): Use new metadata cache for sorting.
this.setCompareFunction('name',
/** @type {function(*, *): number} */ (this.compareName_.bind(this)));
this.setCompareFunction('modificationTime',
......@@ -544,9 +545,10 @@ FileListModel.prototype.compareType_ = function(a, b) {
*
* @param {FileFilter} fileFilter The file-filter context.
* @param {MetadataCache} metadataCache Metadata cache service.
* @param {!FileSystemMetadata} fileSystemMetadata
* @constructor
*/
function FileListContext(fileFilter, metadataCache) {
function FileListContext(fileFilter, metadataCache, fileSystemMetadata) {
/**
* @type {FileListModel}
*/
......@@ -557,6 +559,12 @@ function FileListContext(fileFilter, metadataCache) {
*/
this.metadataCache = metadataCache;
/**
* @public {!FileSystemMetadata}
* @const
*/
this.fileSystemMetadata = fileSystemMetadata;
/**
* @type {FileFilter}
*/
......@@ -626,6 +634,7 @@ DirectoryContents.prototype.dispose = function() {
/**
* Make a space for current directory size in the metadata cache.
* TODO(hirono): Update size of new meatadata cache here.
*
* @param {number} size The cache size to be set.
* @private
......@@ -755,8 +764,10 @@ DirectoryContents.prototype.update = function(updatedEntries, removedUrls) {
addedList.push(updatedMap[url]);
}
if (removedUrls.length > 0)
if (removedUrls.length > 0) {
this.fileList_.metadataCache_.clearByUrl(removedUrls, '*');
this.context_.fileSystemMetadata.notifyEntriesRemoved(removedUrls);
}
this.prefetchMetadata(updatedList, true, function() {
this.onNewEntries_(true, addedList);
......@@ -914,7 +925,7 @@ DirectoryContents.prototype.onNewEntries_ = function(refresh, entries) {
};
/**
* @param {Array.<Entry>} entries Files.
* @param {!Array<!Entry>} entries Files.
* @param {boolean} refresh True to refresh metadata, or false to use cached
* one.
* @param {function(Object)} callback Callback on done.
......@@ -922,10 +933,16 @@ DirectoryContents.prototype.onNewEntries_ = function(refresh, entries) {
DirectoryContents.prototype.prefetchMetadata =
function(entries, refresh, callback) {
var TYPES = 'filesystem|external';
if (refresh)
if (refresh) {
this.context_.metadataCache.getLatest(entries, TYPES, callback);
else
this.context_.fileSystemMetadata.notifyEntriesChanged(entries);
this.context_.fileSystemMetadata.get(
entries, FileTable.METADATA_PROPERTY_NAMES);
} else {
this.context_.metadataCache.get(entries, TYPES, callback);
this.context_.fileSystemMetadata.get(
entries, FileTable.METADATA_PROPERTY_NAMES);
}
};
/**
......
......@@ -44,7 +44,7 @@ function DirectoryModel(singleSelection, fileFilter, fileWatcher, metadataCache,
this.onFilterChanged_.bind(this));
this.currentFileListContext_ = new FileListContext(
fileFilter, metadataCache);
fileFilter, metadataCache, fileSystemMetadata);
this.currentDirContents_ =
DirectoryContents.createForDirectory(this.currentFileListContext_, null);
......@@ -834,7 +834,7 @@ DirectoryModel.prototype.createDirectory = function(name,
then(function(newEntry) {
// Refresh the cache.
this.metadataCache_.clear([newEntry], '*');
this.fileSystemMetadata_.notifyEntryCreated([newEntry]);
this.fileSystemMetadata_.notifyEntriesCreated([newEntry]);
return new Promise(function(onFulfilled, onRejected) {
dirContents.prefetchMetadata(
[newEntry], false, onFulfilled.bind(null, newEntry));
......
......@@ -64,8 +64,13 @@ FileSystemMetadata.prototype.get = function(entries, names) {
}
}
// Correct property names that are valid for fileSystemMetadataProvider.
var fileSystemPropertyNames = names.filter(function(name) {
return name === 'size' || name === 'modificationTime';
});
return Promise.all([
this.fileSystemMetadataProvider_.get(localEntries, names),
this.fileSystemMetadataProvider_.get(localEntries, fileSystemPropertyNames),
this.externalMetadataProvider_.get(externalEntries, names)
]).then(function(results) {
var integratedResults = [];
......@@ -95,6 +100,23 @@ FileSystemMetadata.prototype.getCache = function(entries, names) {
* Clears old metadata for newly created entries.
* @param {!Array<!Entry>} entries
*/
FileSystemMetadata.prototype.notifyEntryCreated = function(entries) {
this.cache_.clear(entries);
FileSystemMetadata.prototype.notifyEntriesCreated = function(entries) {
this.cache_.clear(util.entriesToURLs(entries));
};
/**
* Clears metadata for deleted entries.
* @param {!Array<string>} urls Note it is not an entry list because we cannot
* obtain entries after removing them from the file system.
*/
FileSystemMetadata.prototype.notifyEntriesRemoved = function(urls) {
this.cache_.clear(urls);
};
/**
* Invalidates metadata for updated entries.
* @param {!Array<!Entry>} entries
*/
FileSystemMetadata.prototype.notifyEntriesChanged = function(entries) {
this.cache_.invalidate(this.cache_.generateRequestId(), entries);
};
......@@ -104,11 +104,11 @@ MetadataCacheSet.prototype.invalidate = function(requestId, entries) {
/**
* Clears the caches of entries.
* @param {!Array<!Entry>} entries
* @param {!Array<string>} urls
*/
MetadataCacheSet.prototype.clear = function(entries) {
for (var i = 0; i < entries.length; i++) {
this.items_.remove(entries[i].toURL());
MetadataCacheSet.prototype.clear = function(urls) {
for (var i = 0; i < urls.length; i++) {
this.items_.remove(urls[i]);
}
};
......
......@@ -130,7 +130,7 @@ function testMetadataCacheSetClear() {
assertTrue(set.hasFreshCache([entryA], ['propertyA']));
set.startRequests(1, set.createRequests([entryA], ['propertyB']));
set.clear([entryA]);
set.clear([entryA.toURL()]);
// PropertyB should not be stored because it is requsted before clear.
set.storeProperties(1, [entryA], [{propertyB: 'value'}]);
......
......@@ -121,6 +121,10 @@ NewMetadataProvider.prototype.get = function(entries, names) {
* @return {!Array<!T>}
*/
NewMetadataProvider.prototype.getCache = function(entries, names) {
// Check if the property name is correct or not.
for (var i = 0; i < names.length; i++) {
assert(this.validPropertyNames_[names[i]]);
}
return this.cache_.get(entries, names);
};
......
......@@ -231,6 +231,19 @@ function FileTable() {
throw new Error('Designed to decorate elements');
}
/**
* Metadata property names used by FileTable.
* These metadata is expected to be cached.
* @const {!Array<string>}
*/
FileTable.METADATA_PROPERTY_NAMES = [
'size',
'modificationTime',
'hosted',
'availableOffline',
'customIconUrl'
];
/**
* Inherits from cr.ui.Table.
*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment