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
3 // found in the LICENSE file.
8 * Scanner of the entries.
11 function ContentScanner() {
12 this.cancelled_ = false;
16 * Starts to scan the entries. For example, starts to read the entries in a
17 * directory, or starts to search with some query on a file system.
18 * Derived classes must override this method.
20 * @param {function(Array.<Entry>)} entriesCallback Called when some chunk of
21 * entries are read. This can be called a couple of times until the
23 * @param {function()} successCallback Called when the scan is completed
25 * @param {function(FileError)} errorCallback Called an error occurs.
27 ContentScanner.prototype.scan = function(
28 entriesCallback, successCallback, errorCallback) {
32 * Request cancelling of the running scan. When the cancelling is done,
33 * an error will be reported from errorCallback passed to scan().
35 ContentScanner.prototype.cancel = function() {
36 this.cancelled_ = true;
40 * Scanner of the entries in a directory.
41 * @param {DirectoryEntry} entry The directory to be read.
43 * @extends {ContentScanner}
45 function DirectoryContentScanner(entry) {
46 ContentScanner.call(this);
51 * Extends ContentScanner.
53 DirectoryContentScanner.prototype.__proto__ = ContentScanner.prototype;
56 * Starts to read the entries in the directory.
59 DirectoryContentScanner.prototype.scan = function(
60 entriesCallback, successCallback, errorCallback) {
61 if (!this.entry_ || this.entry_ === DirectoryModel.fakeDriveEntry_) {
62 // If entry is not specified or a fake, we cannot read it.
63 errorCallback(util.createFileError(FileError.INVALID_MODIFICATION_ERR));
67 metrics.startInterval('DirectoryScan');
68 var reader = this.entry_.createReader();
69 var readEntries = function() {
72 if (this.cancelled_) {
73 errorCallback(util.createFileError(FileError.ABORT_ERR));
77 if (entries.length === 0) {
78 // All entries are read.
79 metrics.recordInterval('DirectoryScan');
84 entriesCallback(entries);
93 * Scanner of the entries for the search results on Drive File System.
94 * @param {string} query The query string.
96 * @extends {ContentScanner}
98 function DriveSearchContentScanner(query) {
99 ContentScanner.call(this);
104 * Extends ContentScanner.
106 DriveSearchContentScanner.prototype.__proto__ = ContentScanner.prototype;
109 * Delay in milliseconds to be used for drive search scan, in order to reduce
110 * the number of server requests while user is typing the query.
115 DriveSearchContentScanner.SCAN_DELAY_ = 200;
118 * Maximum number of results which is shown on the search.
123 DriveSearchContentScanner.MAX_RESULTS_ = 100;
126 * Starts to search on Drive File System.
129 DriveSearchContentScanner.prototype.scan = function(
130 entriesCallback, successCallback, errorCallback) {
131 var numReadEntries = 0;
132 var readEntries = function(nextFeed) {
133 chrome.fileBrowserPrivate.searchDrive(
134 {query: this.query_, nextFeed: nextFeed},
135 function(entries, nextFeed) {
136 if (this.cancelled_) {
137 errorCallback(util.createFileError(FileError.ABORT_ERR));
141 // TODO(tbarzic): Improve error handling.
143 console.error('Drive search encountered an error.');
144 errorCallback(util.createFileError(
145 FileError.INVALID_MODIFICATION_ERR));
149 var numRemainingEntries =
150 DriveSearchContentScanner.MAX_RESULTS_ - numReadEntries;
151 if (entries.length >= numRemainingEntries) {
152 // The limit is hit, so quit the scan here.
153 entries = entries.slice(0, numRemainingEntries);
157 numReadEntries += entries.length;
158 if (entries.length > 0)
159 entriesCallback(entries);
164 readEntries(nextFeed);
168 // Let's give another search a chance to cancel us before we begin.
171 // Check cancelled state before read the entries.
172 if (this.cancelled_) {
173 errorCallback(util.createFileError(FileError.ABORT_ERR));
178 DriveSearchContentScanner.SCAN_DELAY_);
182 * Scanner of the entries of the file name search on the directory tree, whose
184 * @param {DirectoryEntry} entry The root of the search target directory tree.
185 * @param {string} query The query of the search.
187 * @extends {ContentScanner}
189 function LocalSearchContentScanner(entry, query) {
190 ContentScanner.call(this);
192 this.query_ = query.toLowerCase();
196 * Extedns ContentScanner.
198 LocalSearchContentScanner.prototype.__proto__ = ContentScanner.prototype;
201 * Starts the file name search.
204 LocalSearchContentScanner.prototype.scan = function(
205 entriesCallback, successCallback, errorCallback) {
206 var numRunningTasks = 0;
208 var maybeRunCallback = function() {
209 if (numRunningTasks === 0) {
211 errorCallback(util.createFileError(FileError.ABORT_ERR));
213 errorCallback(error);
219 var processEntry = function(entry) {
221 var onError = function(fileError) {
228 var onSuccess = function(entries) {
229 if (this.cancelled_ || error || entries.length === 0) {
235 // Filters by the query, and if found, run entriesCallback.
236 var foundEntries = entries.filter(function(entry) {
237 return entry.name.toLowerCase().indexOf(this.query_) >= 0;
239 if (foundEntries.length > 0)
240 entriesCallback(foundEntries);
242 // Start to process sub directories.
243 for (var i = 0; i < entries.length; i++) {
244 if (entries[i].isDirectory)
245 processEntry(entries[i]);
248 // Read remaining entries.
249 reader.readEntries(onSuccess, onError);
252 var reader = entry.createReader();
253 reader.readEntries(onSuccess, onError);
256 processEntry(this.entry_);
260 * Scanner of the entries for the metadata search on Drive File System.
261 * @param {string} query The query of the search.
262 * @param {DriveMetadataSearchContentScanner.SearchType} searchType The option
265 * @extends {ContentScanner}
267 function DriveMetadataSearchContentScanner(query, searchType) {
268 ContentScanner.call(this);
270 this.searchType_ = searchType;
274 * Extends ContentScanner.
276 DriveMetadataSearchContentScanner.prototype.__proto__ =
277 ContentScanner.prototype;
280 * The search types on the Drive File System.
283 DriveMetadataSearchContentScanner.SearchType = Object.freeze({
285 SEARCH_SHARED_WITH_ME: 'SHARED_WITH_ME',
286 SEARCH_RECENT_FILES: 'EXCLUDE_DIRECTORIES',
287 SEARCH_OFFLINE: 'OFFLINE'
291 * Starts to metadata-search on Drive File System.
294 DriveMetadataSearchContentScanner.prototype.scan = function(
295 entriesCallback, successCallback, errorCallback) {
296 chrome.fileBrowserPrivate.searchDriveMetadata(
297 {query: this.query_, types: this.searchType_, maxResults: 500},
299 if (this.cancelled_) {
300 errorCallback(util.createFileError(FileError.ABORT_ERR));
305 console.error('Drive search encountered an error.');
306 errorCallback(util.createFileError(
307 FileError.INVALID_MODIFICATION_ERR));
311 var entries = results.map(function(result) { return result.entry; });
312 if (entries.length > 0)
313 entriesCallback(entries);
319 * This class manages filters and determines a file should be shown or not.
320 * When filters are changed, a 'changed' event is fired.
322 * @param {MetadataCache} metadataCache Metadata cache service.
323 * @param {boolean} showHidden If files starting with '.' are shown.
325 * @extends {cr.EventTarget}
327 function FileFilter(metadataCache, showHidden) {
329 * @type {MetadataCache}
332 this.metadataCache_ = metadataCache;
335 * @type Object.<string, Function>
339 this.setFilterHidden(!showHidden);
341 // Do not show entries marked as 'deleted'.
342 this.addFilter('deleted', function(entry) {
343 var internal = this.metadataCache_.getCached(entry, 'internal');
344 return !(internal && internal.deleted);
349 * FileFilter extends cr.EventTarget.
351 FileFilter.prototype = {__proto__: cr.EventTarget.prototype};
354 * @param {string} name Filter identifier.
355 * @param {function(Entry)} callback A filter — a function receiving an Entry,
356 * and returning bool.
358 FileFilter.prototype.addFilter = function(name, callback) {
359 this.filters_[name] = callback;
360 cr.dispatchSimpleEvent(this, 'changed');
364 * @param {string} name Filter identifier.
366 FileFilter.prototype.removeFilter = function(name) {
367 delete this.filters_[name];
368 cr.dispatchSimpleEvent(this, 'changed');
372 * @param {boolean} value If do not show hidden files.
374 FileFilter.prototype.setFilterHidden = function(value) {
378 function(entry) { return entry.name.substr(0, 1) !== '.'; }
381 this.removeFilter('hidden');
386 * @return {boolean} If the files with names starting with "." are not shown.
388 FileFilter.prototype.isFilterHiddenOn = function() {
389 return 'hidden' in this.filters_;
393 * @param {Entry} entry File entry.
394 * @return {boolean} True if the file should be shown, false otherwise.
396 FileFilter.prototype.filter = function(entry) {
397 for (var name in this.filters_) {
398 if (!this.filters_[name](entry))
405 * A context of DirectoryContents.
406 * TODO(yoshiki): remove this. crbug.com/224869.
408 * @param {FileFilter} fileFilter The file-filter context.
409 * @param {MetadataCache} metadataCache Metadata cache service.
412 function FileListContext(fileFilter, metadataCache) {
414 * @type {cr.ui.ArrayDataModel}
416 this.fileList = new cr.ui.ArrayDataModel([]);
419 * @type {MetadataCache}
421 this.metadataCache = metadataCache;
426 this.fileFilter = fileFilter;
430 * This class is responsible for scanning directory (or search results),
431 * and filling the fileList. Different descendants handle various types of
432 * directory contents shown: basic directory, drive search results, local search
434 * TODO(hidehiko): Remove EventTarget from this.
436 * @param {FileListContext} context The file list context.
437 * @param {boolean} isSearch True for search directory contents, otherwise
439 * @param {DirectoryEntry} directoryEntry The entry of the current directory.
440 * @param {DirectoryEntry} lastNonSearchDirectoryEntry The entry of the last
441 * non-search directory.
442 * @param {function():ContentScanner} scannerFactory The factory to create
443 * ContentScanner instance.
445 * @extends {cr.EventTarget}
447 function DirectoryContents(context, isSearch, directoryEntry,
448 lastNonSearchDirectoryEntry,
450 this.context_ = context;
451 this.fileList_ = context.fileList;
453 this.isSearch_ = isSearch;
454 this.directoryEntry_ = directoryEntry;
455 this.lastNonSearchDirectoryEntry_ = lastNonSearchDirectoryEntry;
457 this.scannerFactory_ = scannerFactory;
458 this.scanner_ = null;
459 this.prefetchMetadataQueue_ = new AsyncUtil.Queue();
460 this.scanCancelled_ = false;
461 this.fileList_.prepareSort = this.prepareSort_.bind(this);
465 * DirectoryContents extends cr.EventTarget.
467 DirectoryContents.prototype.__proto__ = cr.EventTarget.prototype;
470 * Create the copy of the object, but without scan started.
471 * @return {DirectoryContents} Object copy.
473 DirectoryContents.prototype.clone = function() {
474 return new DirectoryContents(
475 this.context_, this.isSearch_, this.directoryEntry_,
476 this.lastNonSearchDirectoryEntry_, this.scannerFactory_);
480 * Use a given fileList instead of the fileList from the context.
481 * @param {Array|cr.ui.ArrayDataModel} fileList The new file list.
483 DirectoryContents.prototype.setFileList = function(fileList) {
484 this.fileList_ = fileList;
485 this.fileList_.prepareSort = this.prepareSort_.bind(this);
489 * Use the filelist from the context and replace its contents with the entries
490 * from the current fileList.
492 DirectoryContents.prototype.replaceContextFileList = function() {
493 if (this.context_.fileList !== this.fileList_) {
494 var spliceArgs = [].slice.call(this.fileList_);
495 var fileList = this.context_.fileList;
496 spliceArgs.unshift(0, fileList.length);
497 fileList.splice.apply(fileList, spliceArgs);
498 this.fileList_ = fileList;
503 * @return {boolean} If the scan is active.
505 DirectoryContents.prototype.isScanning = function() {
506 return this.scanner_ || this.prefetchMetadataQueue_.isRunning();
510 * @return {boolean} True if search results (drive or local).
512 DirectoryContents.prototype.isSearch = function() {
513 return this.isSearch_;
517 * @return {DirectoryEntry} A DirectoryEntry for current directory. In case of
518 * search -- the top directory from which search is run.
520 DirectoryContents.prototype.getDirectoryEntry = function() {
521 return this.directoryEntry_;
525 * @return {DirectoryEntry} A DirectoryEntry for the last non search contents.
527 DirectoryContents.prototype.getLastNonSearchDirectoryEntry = function() {
528 return this.lastNonSearchDirectoryEntry_;
532 * Start directory scan/search operation. Either 'scan-completed' or
533 * 'scan-failed' event will be fired upon completion.
535 DirectoryContents.prototype.scan = function() {
536 // TODO(hidehiko,mtomasz): this scan method must be called at most once.
537 // Remove such a limitation.
538 this.scanner_ = this.scannerFactory_();
539 this.scanner_.scan(this.onNewEntries_.bind(this),
540 this.onScanCompleted_.bind(this),
541 this.onScanError_.bind(this));
545 * Cancels the running scan.
547 DirectoryContents.prototype.cancelScan = function() {
548 if (this.scanCancelled_)
550 this.scanCancelled_ = true;
552 this.scanner_.cancel();
554 this.prefetchMetadataQueue_.cancel();
555 cr.dispatchSimpleEvent(this, 'scan-cancelled');
559 * Called when the scanning by scanner_ is done.
562 DirectoryContents.prototype.onScanCompleted_ = function() {
563 this.scanner_ = null;
564 if (this.scanCancelled_)
567 this.prefetchMetadataQueue_.run(function(callback) {
568 // Call callback first, so isScanning() returns false in the event handlers.
570 cr.dispatchSimpleEvent(this, 'scan-completed');
575 * Called in case scan has failed. Should send the event.
578 DirectoryContents.prototype.onScanError_ = function() {
579 this.scanner_ = null;
580 if (this.scanCancelled_)
583 this.prefetchMetadataQueue_.run(function(callback) {
584 // Call callback first, so isScanning() returns false in the event handlers.
586 cr.dispatchSimpleEvent(this, 'scan-failed');
591 * Called when some chunk of entries are read by scanner.
592 * @param {Array.<Entry>} entries The list of the scanned entries.
595 DirectoryContents.prototype.onNewEntries_ = function(entries) {
596 if (this.scanCancelled_)
599 var entriesFiltered = [].filter.call(
600 entries, this.context_.fileFilter.filter.bind(this.context_.fileFilter));
602 // Update the filelist without waiting the metadata.
603 this.fileList_.push.apply(this.fileList_, entriesFiltered);
604 cr.dispatchSimpleEvent(this, 'scan-updated');
606 // Because the prefetchMetadata can be slow, throttling by splitting entries
607 // into smaller chunks to reduce UI latency.
608 // TODO(hidehiko,mtomasz): This should be handled in MetadataCache.
609 var MAX_CHUNK_SIZE = 50;
610 for (var i = 0; i < entriesFiltered.length; i += MAX_CHUNK_SIZE) {
611 var chunk = entriesFiltered.slice(i, i + MAX_CHUNK_SIZE);
612 this.prefetchMetadataQueue_.run(function(chunk, callback) {
613 this.prefetchMetadata(chunk, function() {
614 if (this.scanCancelled_) {
615 // Do nothing if the scanning is cancelled.
620 cr.dispatchSimpleEvent(this, 'scan-updated');
623 }.bind(this, chunk));
628 * Cache necessary data before a sort happens.
630 * This is called by the table code before a sort happens, so that we can
631 * go fetch data for the sort field that we may not have yet.
632 * @param {string} field Sort field.
633 * @param {function(Object)} callback Called when done.
636 DirectoryContents.prototype.prepareSort_ = function(field, callback) {
637 this.prefetchMetadata(this.fileList_.slice(), callback);
641 * @param {Array.<Entry>} entries Files.
642 * @param {function(Object)} callback Callback on done.
644 DirectoryContents.prototype.prefetchMetadata = function(entries, callback) {
645 this.context_.metadataCache.get(entries, 'filesystem', callback);
649 * @param {Array.<Entry>} entries Files.
650 * @param {function(Object)} callback Callback on done.
652 DirectoryContents.prototype.reloadMetadata = function(entries, callback) {
653 this.context_.metadataCache.clear(entries, '*');
654 this.context_.metadataCache.get(entries, 'filesystem', callback);
658 * @param {string} name Directory name.
659 * @param {function(DirectoryEntry)} successCallback Called on success.
660 * @param {function(FileError)} errorCallback On error.
662 DirectoryContents.prototype.createDirectory = function(
663 name, successCallback, errorCallback) {
664 // TODO(hidehiko): createDirectory should not be the part of
666 if (this.isSearch_ || !this.directoryEntry_) {
667 errorCallback(util.createFileError(FileError.INVALID_MODIFICATION_ERR));
671 var onSuccess = function(newEntry) {
672 this.reloadMetadata([newEntry], function() {
673 successCallback(newEntry);
677 this.directoryEntry_.getDirectory(name, {create: true, exclusive: true},
678 onSuccess.bind(this), errorCallback);
682 * Creates a DirectoryContents instance to show entries in a directory.
684 * @param {FileListContext} context File list context.
685 * @param {DirectoryEntry} directoryEntry The current directory entry.
686 * @return {DirectoryContents} Created DirectoryContents instance.
688 DirectoryContents.createForDirectory = function(context, directoryEntry) {
689 return new DirectoryContents(
691 false, // Non search.
695 return new DirectoryContentScanner(directoryEntry);
700 * Creates a DirectoryContents instance to show the result of the search on
703 * @param {FileListContext} context File list context.
704 * @param {DirectoryEntry} directoryEntry The current directory entry.
705 * @param {DirectoryEntry} previousDirectoryEntry The DirectoryEntry that was
706 * current before the search.
707 * @param {string} query Search query.
708 * @return {DirectoryContents} Created DirectoryContents instance.
710 DirectoryContents.createForDriveSearch = function(
711 context, directoryEntry, previousDirectoryEntry, query) {
712 return new DirectoryContents(
716 previousDirectoryEntry,
718 return new DriveSearchContentScanner(query);
723 * Creates a DirectoryContents instance to show the result of the search on
726 * @param {FileListContext} context File list context.
727 * @param {DirectoryEntry} directoryEntry The current directory entry.
728 * @param {string} query Search query.
729 * @return {DirectoryContents} Created DirectoryContents instance.
731 DirectoryContents.createForLocalSearch = function(
732 context, directoryEntry, query) {
733 return new DirectoryContents(
739 return new LocalSearchContentScanner(directoryEntry, query);
744 * Creates a DirectoryContents instance to show the result of metadata search
745 * on Drive File System.
747 * @param {FileListContext} context File list context.
748 * @param {DirectoryEntry} fakeDirectoryEntry Fake directory entry representing
749 * the set of result entries. This serves as a top directory for the
751 * @param {DirectoryEntry} driveDirectoryEntry Directory for the actual drive.
752 * @param {string} query Search query.
753 * @param {DriveMetadataSearchContentScanner.SearchType} searchType The type of
754 * the search. The scanner will restricts the entries based on the given
756 * @return {DirectoryContents} Created DirectoryContents instance.
758 DirectoryContents.createForDriveMetadataSearch = function(
759 context, fakeDirectoryEntry, driveDirectoryEntry, query, searchType) {
760 return new DirectoryContents(
766 return new DriveMetadataSearchContentScanner(query, searchType);