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

Unified Diff: ui/file_manager/file_manager/background/js/media_scanner.js

Issue 1045663002: Cancel scans when directory changed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Also, cancel scans when window closes...and add test coverage. Created 5 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 side-by-side diff with in-line comments
Download patch
Index: ui/file_manager/file_manager/background/js/media_scanner.js
diff --git a/ui/file_manager/file_manager/background/js/media_scanner.js b/ui/file_manager/file_manager/background/js/media_scanner.js
index 02e411e5ceb444eb9b5636f9bcd9724e668f429e..4d300dd35130080f2163e505660d33ebf68fa377 100644
--- a/ui/file_manager/file_manager/background/js/media_scanner.js
+++ b/ui/file_manager/file_manager/background/js/media_scanner.js
@@ -103,10 +103,12 @@ importer.DefaultMediaScanner.prototype.removeObserver = function(observer) {
/** @override */
importer.DefaultMediaScanner.prototype.scanDirectory = function(directory) {
var scan = this.createScanResult_();
+ console.log(scan.name + ': Scanning directory ' + directory.fullPath);
mtomasz 2015/04/01 02:28:45 nit: Are these logs necessary? If not, let's remov
Steve McKay 2015/04/01 03:52:52 Useful. Changed to console.info and console.debug.
+
var watcher = this.watcherFactory_(
/** @this {importer.DefaultMediaScanner} */
function() {
- scan.invalidateScan();
+ scan.cancel();
this.notify_(importer.ScanEvent.INVALIDATED, scan);
}.bind(this));
@@ -119,6 +121,7 @@ importer.DefaultMediaScanner.prototype.scanDirectory = function(directory) {
.then(
/** @this {importer.DefaultMediaScanner} */
function() {
+ console.log(scan.name + ': Finished.');
this.notify_(importer.ScanEvent.FINALIZED, scan);
}.bind(this));
@@ -131,10 +134,14 @@ importer.DefaultMediaScanner.prototype.scanFiles = function(entries) {
throw new Error('Cannot scan empty list.');
}
var scan = this.createScanResult_();
+ console.log(
+ scan.name + ': Scanning fixed set of ' +
+ entries.length + ' entries.');
+
var watcher = this.watcherFactory_(
/** @this {importer.DefaultMediaScanner} */
function() {
- scan.invalidateScan();
+ scan.cancel();
this.notify_(importer.ScanEvent.INVALIDATED, scan);
}.bind(this));
@@ -172,20 +179,32 @@ importer.DefaultMediaScanner.prototype.scanMediaFiles_ =
* to process.
* @return {!Promise}
*/
- var scanChunk = function(begin) {
+ var scanBatch = function(begin) {
+ if (scan.canceled()) {
+ console.log(
+ scan.name + ': Skipping remaining ' +
+ (entries.length - begin) +
+ ' entries. Scan was canceled.');
+ return Promise.resolve();
+ }
+
// the second arg to slice is an exclusive end index, so we +1 batch size.
- var end = begin + importer.DefaultMediaScanner.SCAN_BATCH_SIZE + 1;
+ var end = begin + importer.DefaultMediaScanner.SCAN_BATCH_SIZE ;
+ console.log(scan.name + ': Processing batch ' + begin + '-' + (end - 1));
+ var batch = entries.slice(begin, end);
+
return Promise.all(
- entries.slice(begin, end).map(handleFileEntry))
+ batch.map(handleFileEntry))
.then(
+ /** @this {importer.DefaultMediaScanner} */
function() {
if (end < entries.length) {
- return scanChunk(end);
+ return scanBatch(end);
}
});
};
- return scanChunk(0);
+ return scanBatch(0);
};
/**
@@ -321,9 +340,16 @@ importer.ScanResult = function() {};
importer.ScanResult.prototype.isFinal;
/**
- * @return {boolean} true if scanning is invalidated.
+ * Notifies the scan to stop working. Some in progress work
+ * may continue, but no new work will be undertaken.
+ */
+importer.ScanResult.prototype.cancel;
+
+/**
+ * @return {boolean} True if the scan has been canceled. Some
+ * work started prior to cancelation may still be ongoing.
*/
-importer.ScanResult.prototype.isInvalidated;
+importer.ScanResult.prototype.canceled;
/**
* Returns all files entries discovered so far. The list will be
@@ -335,7 +361,8 @@ importer.ScanResult.prototype.isInvalidated;
importer.ScanResult.prototype.getFileEntries;
/**
- * Returns a promise that fires when scanning is complete.
+ * Returns a promise that fires when scanning is finished
+ * normally or has been canceled.
*
* @return {!Promise<!importer.ScanResult>}
*/
@@ -363,6 +390,9 @@ importer.ScanResult.Statistics;
* <p>The scan is complete, and the object will become static once the
* {@code whenFinal} promise resolves.
*
+ * Note that classes implementing this should provide a read-only
+ * {@code name} field.
+ *
* @constructor
* @struct
* @implements {importer.ScanResult}
@@ -372,6 +402,9 @@ importer.ScanResult.Statistics;
*/
importer.DefaultScanResult = function(hashGenerator) {
mtomasz 2015/04/01 02:28:45 nit: Remove \n.
Steve McKay 2015/04/01 03:52:52 Done.
+ /** @private {number} */
+ this.scanId_ = importer.generateId();
+
/** @private {function(!FileEntry): !Promise.<string>} */
this.createHashcode_ = hashGenerator;
@@ -410,7 +443,7 @@ importer.DefaultScanResult = function(hashGenerator) {
/**
* @private {boolean}
*/
- this.invalidated_ = false;
+ this.canceled_ = false;
/** @private {!importer.Resolver.<!importer.ScanResult>} */
this.resolver_ = new importer.Resolver();
@@ -419,6 +452,9 @@ importer.DefaultScanResult = function(hashGenerator) {
/** @struct */
importer.DefaultScanResult.prototype = {
mtomasz 2015/04/01 02:28:45 nit: Remove \n.
Steve McKay 2015/04/01 03:52:52 Done.
+ /** @return {string} */
+ get name() { return 'ScanResult(' + this.scanId_ + ')' },
+
/** @return {function()} */
get resolve() { return this.resolver_.resolve.bind(null, this); },
@@ -431,10 +467,6 @@ importer.DefaultScanResult.prototype.isFinal = function() {
return this.resolver_.settled;
};
-importer.DefaultScanResult.prototype.isInvalidated = function() {
- return this.invalidated_;
-};
-
/** @override */
importer.DefaultScanResult.prototype.getFileEntries = function() {
return this.fileEntries_;
@@ -445,11 +477,14 @@ importer.DefaultScanResult.prototype.whenFinal = function() {
return this.resolver_.promise;
};
-/**
- * Invalidates this scan.
- */
-importer.DefaultScanResult.prototype.invalidateScan = function() {
- this.invalidated_ = true;
+/** @override */
+importer.DefaultScanResult.prototype.cancel = function() {
+ this.canceled_ = true;
+};
+
+/** @override */
+importer.DefaultScanResult.prototype.canceled = function() {
+ return this.canceled_;
};
/**

Powered by Google App Engine
This is Rietveld 408576698