Bug 1364768: Part 2 - Add AsyncShutdown finalizer support to DeferredSave. r=rhelmer

MozReview-Commit-ID: LAvOErU1YB8

--HG--
extra : rebase_source : a64786b0a44ba70f500c7e3686c89023d643eb76
This commit is contained in:
Kris Maglione 2017-05-14 15:55:42 -07:00
Родитель ad0c31080f
Коммит ff0f8560f8
1 изменённых файлов: 35 добавлений и 7 удалений

Просмотреть файл

@ -43,6 +43,11 @@ parentLogger.addAppender(new Log.DumpAppender(formatter));
// at DEBUG and higher should go to JS console and standard error.
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "AsyncShutdown",
"resource://gre/modules/AsyncShutdown.jsm");
const PREF_LOGGING_ENABLED = "extensions.logging.enabled";
const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed";
@ -83,21 +88,30 @@ PrefObserver.init();
* fulfilled by a single write.
*
* @constructor
* @param aPath
* @param {string} aPath
* String representing the full path of the file where the data
* is to be written.
* @param aDataProvider
* @param {function} aDataProvider
* Callback function that takes no argument and returns the data to
* be written. If aDataProvider returns an ArrayBufferView, the
* bytes it contains are written to the file as is.
* If aDataProvider returns a String the data are UTF-8 encoded
* and then written to the file.
* @param [optional] aDelay
* @param {object | integer} [aOptions]
* The delay in milliseconds between the first saveChanges() call
* that marks the data as needing to be saved, and when the DeferredSave
* begins writing the data to disk. Default 50 milliseconds.
*
* Or, an options object containing:
* - delay: A delay in milliseconds.
* - finalizeAt: An AsyncShutdown blocker during which to
* finalize any pending writes.
*/
this.DeferredSave = function(aPath, aDataProvider, aDelay) {
this.DeferredSave = function(aPath, aDataProvider, aOptions = {}) {
if (typeof aOptions == "number") {
aOptions = {delay: aOptions};
}
// Create a new logger (child of 'DeferredSave' logger)
// for use by this particular instance of DeferredSave object
let leafName = OS.Path.basename(aPath);
@ -141,10 +155,15 @@ this.DeferredSave = function(aPath, aDataProvider, aDelay) {
// Error returned by the most recent write (if any)
this._lastError = null;
if (aDelay && (aDelay > 0))
this._delay = aDelay;
if (aOptions.delay && (aOptions.delay > 0))
this._delay = aOptions.delay;
else
this._delay = DEFAULT_SAVE_DELAY_MS;
this._finalizeAt = aOptions.finalizeAt || AsyncShutdown.profileBeforeChange;
this._finalize = this._finalize.bind(this);
this._finalizeAt.addBlocker(`DeferredSave: writing data to ${aPath}`,
this._finalize);
}
this.DeferredSave.prototype = {
@ -156,6 +175,10 @@ this.DeferredSave.prototype = {
return this._lastError;
},
get path() {
return this._path;
},
// Start the pending timer if data is dirty
_startTimer() {
if (!this._pending) {
@ -263,5 +286,10 @@ this.DeferredSave.prototype = {
}
return this._writing;
}
},
_finalize() {
return this.flush().catch(Cu.reportError);
},
};