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_ || util.isFakeEntry(this.entry_)) {
62 // If entry is not specified or a fake, we cannot read it.
63 errorCallback(util.createDOMError(
64 util.FileError.INVALID_MODIFICATION_ERR));
68 metrics.startInterval('DirectoryScan');
69 var reader = this.entry_.createReader();
70 var readEntries = function() {
73 if (this.cancelled_) {
74 errorCallback(util.createDOMError(util.FileError.ABORT_ERR));
78 if (entries.length === 0) {
79 // All entries are read.
80 metrics.recordInterval('DirectoryScan');
85 entriesCallback(entries);
94 * Scanner of the entries for the search results on Drive File System.
95 * @param {string} query The query string.
97 * @extends {ContentScanner}
99 function DriveSearchContentScanner(query) {
100 ContentScanner.call(this);
105 * Extends ContentScanner.
107 DriveSearchContentScanner.prototype.__proto__ = ContentScanner.prototype;
110 * Delay in milliseconds to be used for drive search scan, in order to reduce
111 * the number of server requests while user is typing the query.
116 DriveSearchContentScanner.SCAN_DELAY_ = 200;
119 * Maximum number of results which is shown on the search.
124 DriveSearchContentScanner.MAX_RESULTS_ = 100;
127 * Starts to search on Drive File System.
130 DriveSearchContentScanner.prototype.scan = function(
131 entriesCallback, successCallback, errorCallback) {
132 var numReadEntries = 0;
133 var readEntries = function(nextFeed) {
134 chrome.fileBrowserPrivate.searchDrive(
135 {query: this.query_, nextFeed: nextFeed},
136 function(entries, nextFeed) {
137 if (this.cancelled_) {
138 errorCallback(util.createDOMError(util.FileError.ABORT_ERR));
142 // TODO(tbarzic): Improve error handling.
144 console.error('Drive search encountered an error.');
145 errorCallback(util.createDOMError(
146 util.FileError.INVALID_MODIFICATION_ERR));
150 var numRemainingEntries =
151 DriveSearchContentScanner.MAX_RESULTS_ - numReadEntries;
152 if (entries.length >= numRemainingEntries) {
153 // The limit is hit, so quit the scan here.
154 entries = entries.slice(0, numRemainingEntries);
158 numReadEntries += entries.length;
159 if (entries.length > 0)
160 entriesCallback(entries);
165 readEntries(nextFeed);
169 // Let's give another search a chance to cancel us before we begin.
172 // Check cancelled state before read the entries.
173 if (this.cancelled_) {
174 errorCallback(util.createDOMError(util.FileError.ABORT_ERR));
179 DriveSearchContentScanner.SCAN_DELAY_);
183 * Scanner of the entries of the file name search on the directory tree, whose
185 * @param {DirectoryEntry} entry The root of the search target directory tree.
186 * @param {string} query The query of the search.
188 * @extends {ContentScanner}
190 function LocalSearchContentScanner(entry, query) {
191 ContentScanner.call(this);
193 this.query_ = query.toLowerCase();
197 * Extends ContentScanner.
199 LocalSearchContentScanner.prototype.__proto__ = ContentScanner.prototype;
202 * Starts the file name search.
205 LocalSearchContentScanner.prototype.scan = function(
206 entriesCallback, successCallback, errorCallback) {
207 var numRunningTasks = 0;
209 var maybeRunCallback = function() {
210 if (numRunningTasks === 0) {
212 errorCallback(util.createDOMError(util.FileError.ABORT_ERR));
214 errorCallback(error);
220 var processEntry = function(entry) {
222 var onError = function(fileError) {
229 var onSuccess = function(entries) {
230 if (this.cancelled_ || error || entries.length === 0) {
236 // Filters by the query, and if found, run entriesCallback.
237 var foundEntries = entries.filter(function(entry) {
238 return entry.name.toLowerCase().indexOf(this.query_) >= 0;
240 if (foundEntries.length > 0)
241 entriesCallback(foundEntries);
243 // Start to process sub directories.
244 for (var i = 0; i < entries.length; i++) {
245 if (entries[i].isDirectory)
246 processEntry(entries[i]);
249 // Read remaining entries.
250 reader.readEntries(onSuccess, onError);
253 var reader = entry.createReader();
254 reader.readEntries(onSuccess, onError);
257 processEntry(this.entry_);
261 * Scanner of the entries for the metadata search on Drive File System.
262 * @param {DriveMetadataSearchContentScanner.SearchType} searchType The option
265 * @extends {ContentScanner}
267 function DriveMetadataSearchContentScanner(searchType) {
268 ContentScanner.call(this);
269 this.searchType_ = searchType;
273 * Extends ContentScanner.
275 DriveMetadataSearchContentScanner.prototype.__proto__ =
276 ContentScanner.prototype;
279 * The search types on the Drive File System.
282 DriveMetadataSearchContentScanner.SearchType = Object.freeze({
284 SEARCH_SHARED_WITH_ME: 'SHARED_WITH_ME',
285 SEARCH_RECENT_FILES: 'EXCLUDE_DIRECTORIES',
286 SEARCH_OFFLINE: 'OFFLINE'
290 * Starts to metadata-search on Drive File System.
293 DriveMetadataSearchContentScanner.prototype.scan = function(
294 entriesCallback, successCallback, errorCallback) {
295 chrome.fileBrowserPrivate.searchDriveMetadata(
296 {query: '', types: this.searchType_, maxResults: 500},
298 if (this.cancelled_) {
299 errorCallback(util.createDOMError(util.FileError.ABORT_ERR));
304 console.error('Drive search encountered an error.');
305 errorCallback(util.createDOMError(
306 util.FileError.INVALID_MODIFICATION_ERR));
310 var entries = results.map(function(result) { return result.entry; });
311 if (entries.length > 0)
312 entriesCallback(entries);
318 * This class manages filters and determines a file should be shown or not.
319 * When filters are changed, a 'changed' event is fired.
321 * @param {MetadataCache} metadataCache Metadata cache service.
322 * @param {boolean} showHidden If files starting with '.' are shown.
324 * @extends {cr.EventTarget}
326 function FileFilter(metadataCache, showHidden) {
328 * @type {MetadataCache}
331 this.metadataCache_ = metadataCache;
334 * @type Object.<string, Function>
338 this.setFilterHidden(!showHidden);
340 // Do not show entries marked as 'deleted'.
341 this.addFilter('deleted', function(entry) {
342 var internal = this.metadataCache_.getCached(entry, 'internal');
343 return !(internal && internal.deleted);
348 * FileFilter extends cr.EventTarget.
350 FileFilter.prototype = {__proto__: cr.EventTarget.prototype};
353 * @param {string} name Filter identifier.
354 * @param {function(Entry)} callback A filter — a function receiving an Entry,
355 * and returning bool.
357 FileFilter.prototype.addFilter = function(name, callback) {
358 this.filters_[name] = callback;
359 cr.dispatchSimpleEvent(this, 'changed');
363 * @param {string} name Filter identifier.
365 FileFilter.prototype.removeFilter = function(name) {
366 delete this.filters_[name];
367 cr.dispatchSimpleEvent(this, 'changed');
371 * @param {boolean} value If do not show hidden files.
373 FileFilter.prototype.setFilterHidden = function(value) {
377 function(entry) { return entry.name.substr(0, 1) !== '.'; }
380 this.removeFilter('hidden');
385 * @return {boolean} If the files with names starting with "." are not shown.
387 FileFilter.prototype.isFilterHiddenOn = function() {
388 return 'hidden' in this.filters_;
392 * @param {Entry} entry File entry.
393 * @return {boolean} True if the file should be shown, false otherwise.
395 FileFilter.prototype.filter = function(entry) {
396 for (var name in this.filters_) {
397 if (!this.filters_[name](entry))
404 * A context of DirectoryContents.
405 * TODO(yoshiki): remove this. crbug.com/224869.
407 * @param {FileFilter} fileFilter The file-filter context.
408 * @param {MetadataCache} metadataCache Metadata cache service.
411 function FileListContext(fileFilter, metadataCache) {
413 * @type {cr.ui.ArrayDataModel}
415 this.fileList = new cr.ui.ArrayDataModel([]);
418 * @type {MetadataCache}
420 this.metadataCache = metadataCache;
425 this.fileFilter = fileFilter;
429 * This class is responsible for scanning directory (or search results),
430 * and filling the fileList. Different descendants handle various types of
431 * directory contents shown: basic directory, drive search results, local search
433 * TODO(hidehiko): Remove EventTarget from this.
435 * @param {FileListContext} context The file list context.
436 * @param {boolean} isSearch True for search directory contents, otherwise
438 * @param {DirectoryEntry} directoryEntry The entry of the current directory.
439 * @param {function():ContentScanner} scannerFactory The factory to create
440 * ContentScanner instance.
442 * @extends {cr.EventTarget}
444 function DirectoryContents(context,
448 this.context_ = context;
449 this.fileList_ = context.fileList;
451 this.isSearch_ = isSearch;
452 this.directoryEntry_ = directoryEntry;
454 this.scannerFactory_ = scannerFactory;
455 this.scanner_ = null;
456 this.prefetchMetadataQueue_ = new AsyncUtil.Queue();
457 this.scanCancelled_ = false;
461 * DirectoryContents extends cr.EventTarget.
463 DirectoryContents.prototype.__proto__ = cr.EventTarget.prototype;
466 * Create the copy of the object, but without scan started.
467 * @return {DirectoryContents} Object copy.
469 DirectoryContents.prototype.clone = function() {
470 return new DirectoryContents(
473 this.directoryEntry_,
474 this.scannerFactory_);
478 * Use a given fileList instead of the fileList from the context.
479 * @param {Array|cr.ui.ArrayDataModel} fileList The new file list.
481 DirectoryContents.prototype.setFileList = function(fileList) {
482 if (fileList instanceof cr.ui.ArrayDataModel)
483 this.fileList_ = fileList;
485 this.fileList_ = new cr.ui.ArrayDataModel(fileList);
486 this.context_.metadataCache.setCacheSize(this.fileList_.length);
490 * Use the filelist from the context and replace its contents with the entries
491 * from the current fileList.
493 DirectoryContents.prototype.replaceContextFileList = function() {
494 if (this.context_.fileList !== this.fileList_) {
495 var spliceArgs = this.fileList_.slice();
496 var fileList = this.context_.fileList;
497 spliceArgs.unshift(0, fileList.length);
498 fileList.splice.apply(fileList, spliceArgs);
499 this.fileList_ = fileList;
500 this.context_.metadataCache.setCacheSize(this.fileList_.length);
505 * @return {boolean} If the scan is active.
507 DirectoryContents.prototype.isScanning = function() {
508 return this.scanner_ || this.prefetchMetadataQueue_.isRunning();
512 * @return {boolean} True if search results (drive or local).
514 DirectoryContents.prototype.isSearch = function() {
515 return this.isSearch_;
519 * @return {DirectoryEntry} A DirectoryEntry for current directory. In case of
520 * search -- the top directory from which search is run.
522 DirectoryContents.prototype.getDirectoryEntry = function() {
523 return this.directoryEntry_;
527 * Start directory scan/search operation. Either 'scan-completed' or
528 * 'scan-failed' event will be fired upon completion.
530 DirectoryContents.prototype.scan = function() {
532 * Invoked when the scanning is completed successfully.
533 * @this {DirectoryContents}
535 function completionCallback() {
536 this.onScanFinished_();
537 this.onScanCompleted_();
541 * Invoked when the scanning is finished but is not completed due to error.
542 * @this {DirectoryContents}
544 function errorCallback() {
545 this.onScanFinished_();
549 // TODO(hidehiko,mtomasz): this scan method must be called at most once.
550 // Remove such a limitation.
551 this.scanner_ = this.scannerFactory_();
552 this.scanner_.scan(this.onNewEntries_.bind(this),
553 completionCallback.bind(this),
554 errorCallback.bind(this));
558 * Cancels the running scan.
560 DirectoryContents.prototype.cancelScan = function() {
561 if (this.scanCancelled_)
563 this.scanCancelled_ = true;
565 this.scanner_.cancel();
567 this.onScanFinished_();
569 this.prefetchMetadataQueue_.cancel();
570 cr.dispatchSimpleEvent(this, 'scan-cancelled');
574 * Called when the scanning by scanner_ is done, even when the scanning is
575 * succeeded or failed. This is called before completion (or error) callback.
579 DirectoryContents.prototype.onScanFinished_ = function() {
580 this.scanner_ = null;
582 this.prefetchMetadataQueue_.run(function(callback) {
583 // TODO(yoshiki): Here we should fire the update event of changed
584 // items. Currently we have a method this.fileList_.updateIndex() to
585 // fire an event, but this method takes only 1 argument and invokes sort
586 // one by one. It is obviously time wasting. Instead, we call sort
588 // In future, we should implement a good method like updateIndexes and
590 var status = this.fileList_.sortStatus;
592 this.fileList_.sort(status.field, status.direction);
599 * Called when the scanning by scanner_ is succeeded.
602 DirectoryContents.prototype.onScanCompleted_ = function() {
603 if (this.scanCancelled_)
606 this.prefetchMetadataQueue_.run(function(callback) {
607 // Call callback first, so isScanning() returns false in the event handlers.
610 cr.dispatchSimpleEvent(this, 'scan-completed');
615 * Called in case scan has failed. Should send the event.
618 DirectoryContents.prototype.onScanError_ = function() {
619 if (this.scanCancelled_)
622 this.prefetchMetadataQueue_.run(function(callback) {
623 // Call callback first, so isScanning() returns false in the event handlers.
625 cr.dispatchSimpleEvent(this, 'scan-failed');
630 * Called when some chunk of entries are read by scanner.
631 * @param {Array.<Entry>} entries The list of the scanned entries.
634 DirectoryContents.prototype.onNewEntries_ = function(entries) {
635 if (this.scanCancelled_)
638 var entriesFiltered = [].filter.call(
639 entries, this.context_.fileFilter.filter.bind(this.context_.fileFilter));
641 // Caching URL to reduce a number of calls of toURL in sort.
642 // This is a temporary solution. We need to fix a root cause of slow toURL.
643 // See crbug.com/370908 for detail.
644 entriesFiltered.forEach(function(entry) { entry.cachedUrl = entry.toURL(); });
646 // Update the filelist without waiting the metadata.
647 this.fileList_.push.apply(this.fileList_, entriesFiltered);
648 cr.dispatchSimpleEvent(this, 'scan-updated');
650 this.context_.metadataCache.setCacheSize(this.fileList_.length);
652 // Because the prefetchMetadata can be slow, throttling by splitting entries
653 // into smaller chunks to reduce UI latency.
654 // TODO(hidehiko,mtomasz): This should be handled in MetadataCache.
655 var MAX_CHUNK_SIZE = 50;
656 for (var i = 0; i < entriesFiltered.length; i += MAX_CHUNK_SIZE) {
657 var chunk = entriesFiltered.slice(i, i + MAX_CHUNK_SIZE);
658 this.prefetchMetadataQueue_.run(function(chunk, callback) {
659 this.prefetchMetadata(chunk, function() {
660 if (this.scanCancelled_) {
661 // Do nothing if the scanning is cancelled.
666 cr.dispatchSimpleEvent(this, 'scan-updated');
669 }.bind(this, chunk));
674 * @param {Array.<Entry>} entries Files.
675 * @param {function(Object)} callback Callback on done.
677 DirectoryContents.prototype.prefetchMetadata = function(entries, callback) {
678 this.context_.metadataCache.get(entries, 'filesystem|drive', callback);
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.
694 return new DirectoryContentScanner(directoryEntry);
699 * Creates a DirectoryContents instance to show the result of the search on
702 * @param {FileListContext} context File list context.
703 * @param {DirectoryEntry} directoryEntry The current directory entry.
704 * @param {string} query Search query.
705 * @return {DirectoryContents} Created DirectoryContents instance.
707 DirectoryContents.createForDriveSearch = function(
708 context, directoryEntry, query) {
709 return new DirectoryContents(
714 return new DriveSearchContentScanner(query);
719 * Creates a DirectoryContents instance to show the result of the search on
722 * @param {FileListContext} context File list context.
723 * @param {DirectoryEntry} directoryEntry The current directory entry.
724 * @param {string} query Search query.
725 * @return {DirectoryContents} Created DirectoryContents instance.
727 DirectoryContents.createForLocalSearch = function(
728 context, directoryEntry, query) {
729 return new DirectoryContents(
734 return new LocalSearchContentScanner(directoryEntry, query);
739 * Creates a DirectoryContents instance to show the result of metadata search
740 * on Drive File System.
742 * @param {FileListContext} context File list context.
743 * @param {DirectoryEntry} fakeDirectoryEntry Fake directory entry representing
744 * the set of result entries. This serves as a top directory for the
746 * @param {DriveMetadataSearchContentScanner.SearchType} searchType The type of
747 * the search. The scanner will restricts the entries based on the given
749 * @return {DirectoryContents} Created DirectoryContents instance.
751 DirectoryContents.createForDriveMetadataSearch = function(
752 context, fakeDirectoryEntry, searchType) {
753 return new DirectoryContents(
758 return new DriveMetadataSearchContentScanner(searchType);