Bug 1649604, remove OS.File usage from JSONFile.jsm r=Gijs,robwu

Differential Revision: https://phabricator.services.mozilla.com/D102041
This commit is contained in:
Emma Malysz 2021-08-03 15:14:18 +00:00
Родитель a94ddcbd7f
Коммит d61a9b0565
4 изменённых файлов: 58 добавлений и 55 удалений

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

@ -4,6 +4,9 @@
"use strict";
const { PushDB, PushService, PushServiceWebSocket } = serviceExports;
// Create the profile directory early to ensure pushBroadcastService
// is initialized with the correct path
do_get_profile();
const { BroadcastService } = ChromeUtils.import(
"resource://gre/modules/PushBroadcastService.jsm",
null
@ -23,7 +26,6 @@ const userAgentID = "bd744428-f125-436a-b6d0-dd0c9845837f";
const channelID = "0ef2ad4a-6c49-41ad-af6e-95d2425276bf";
function run_test() {
do_get_profile();
setPrefs({
userAgentID,
alwaysConnect: true,

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

@ -1,6 +1,16 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const { PromiseTestUtils } = ChromeUtils.import(
"resource://testing-common/PromiseTestUtils.jsm"
);
PromiseTestUtils.allowMatchingRejectionsGlobally(
/Unable to arm timer, the object has been finalized\./
);
PromiseTestUtils.allowMatchingRejectionsGlobally(
/IOUtils\.profileBeforeChange getter: IOUtils: profileBeforeChange phase has already finished/
);
const { Preferences } = ChromeUtils.import(
"resource://gre/modules/Preferences.jsm"
);

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

@ -36,11 +36,6 @@ const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"AsyncShutdown",
"resource://gre/modules/AsyncShutdown.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"DeferredTask",
@ -51,7 +46,6 @@ ChromeUtils.defineModuleGetter(
"FileUtils",
"resource://gre/modules/FileUtils.jsm"
);
ChromeUtils.defineModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
ChromeUtils.defineModuleGetter(
this,
"NetUtil",
@ -62,10 +56,6 @@ XPCOMUtils.defineLazyGetter(this, "gTextDecoder", function() {
return new TextDecoder();
});
XPCOMUtils.defineLazyGetter(this, "gTextEncoder", function() {
return new TextEncoder();
});
const FileInputStream = Components.Constructor(
"@mozilla.org/network/file-input-stream;1",
"nsIFileInputStream",
@ -109,7 +99,7 @@ const TELEMETRY_BASENAMES = new Set(["logins", "autofillprofiles"]);
* intermediate directories before saving. The file will
* not be saved if the promise rejects or the function
* throws an exception.
* - finalizeAt: An `AsyncShutdown` phase or barrier client that should
* - finalizeAt: An `IOUtils` phase or barrier client that should
* automatically finalize the file when triggered. Defaults
* to `profileBeforeChange`; exposed as an option for
* testing.
@ -138,14 +128,14 @@ function JSONFile(config) {
this._options = {};
if (config.compression) {
this._options.compression = config.compression;
this._options.decompress = this._options.compress = true;
}
if (config.backupTo) {
this._options.backupTo = config.backupTo;
this._options.backupFile = this._options.backupTo = config.backupTo;
}
this._finalizeAt = config.finalizeAt || AsyncShutdown.profileBeforeChange;
this._finalizeAt = config.finalizeAt || IOUtils.profileBeforeChange;
this._finalizeInternalBound = this._finalizeInternal.bind(this);
this._finalizeAt.addBlocker(
"JSON store: writing data",
@ -223,14 +213,12 @@ JSONFile.prototype = {
let data = {};
try {
let bytes = await OS.File.read(this.path, this._options);
data = await IOUtils.readJSON(this.path, this._options);
// If synchronous loading happened in the meantime, exit now.
if (this.dataReady) {
return;
}
data = JSON.parse(gTextDecoder.decode(bytes));
} catch (ex) {
// If an exception occurs because the file does not exist or it cannot be read,
// we do two things.
@ -243,7 +231,7 @@ JSONFile.prototype = {
// In the event that the file exists, but an exception is thrown because it cannot be read,
// we store it as a .corrupt file for debugging purposes.
let cleansedBasename = OS.Path.basename(this.path)
let cleansedBasename = PathUtils.filename(this.path)
.replace(/\.json$/, "")
.replaceAll(/[^a-zA-Z0-9_.]/g, "");
let errorNo = ex.winLastError || ex.unixErrno;
@ -252,30 +240,29 @@ JSONFile.prototype = {
cleansedBasename,
errorNo ? errorNo.toString() : ""
);
if (!(ex instanceof OS.File.Error && ex.becauseNoSuchFile)) {
if (!(ex instanceof DOMException && ex.name == "NotFoundError")) {
Cu.reportError(ex);
// Move the original file to a backup location, ignoring errors.
try {
let openInfo = await OS.File.openUnique(this.path + ".corrupt", {
humanReadable: true,
});
await openInfo.file.close();
await OS.File.move(this.path, openInfo.path);
let uniquePath = await PathUtils.createUniquePath(
this.path + ".corrupt"
);
await IOUtils.move(this.path, uniquePath);
this._recordTelemetry("load", cleansedBasename, "invalid_json");
} catch (e2) {
Cu.reportError(e2);
}
}
if (this._options.backupTo) {
if (this._options.backupFile) {
// Restore the original file from the backup here so fresh writes to empty
// json files don't happen at any time in the future compromising the backup
// in the process.
try {
await OS.File.copy(this._options.backupTo, this.path);
await IOUtils.copy(this._options.backupFile, this.path);
} catch (e) {
if (!(e instanceof OS.File.Error && ex.becauseNoSuchFile)) {
if (!(e instanceof DOMException && e.name == "NotFoundError")) {
Cu.reportError(e);
}
}
@ -284,23 +271,24 @@ JSONFile.prototype = {
// We still read from the backup file here instead of the original file in case
// access to the original file is blocked, e.g. by anti-virus software on the
// user's computer.
let bytes = await OS.File.read(this._options.backupTo, this._options);
data = await IOUtils.readJSON(
this._options.backupFile,
this._options
);
// If synchronous loading happened in the meantime, exit now.
if (this.dataReady) {
return;
}
data = JSON.parse(gTextDecoder.decode(bytes));
this._recordTelemetry("load", cleansedBasename, "used_backup");
} catch (e3) {
if (!(e3 instanceof OS.File.Error && ex.becauseNoSuchFile)) {
if (!(e3 instanceof DOMException && e3.name == "NotFoundError")) {
Cu.reportError(e3);
}
}
}
// In some rare cases it's possible for data to have been added to
// our database between the call to OS.File.read and when we've been
// our database between the call to IOUtils.read and when we've been
// notified that there was a problem with it. In that case, leave the
// synchronously-added data alone.
if (this.dataReady) {
@ -373,13 +361,13 @@ JSONFile.prototype = {
}
}
if (this._options.backupTo) {
if (this._options.backupFile) {
// Restore the original file from the backup here so fresh writes to empty
// json files don't happen at any time in the future compromising the backup
// in the process.
try {
let basename = OS.Path.basename(this.path);
let backupFile = new FileUtils.File(this._options.backupTo);
let basename = PathUtils.filename(this.path);
let backupFile = new FileUtils.File(this._options.backupFile);
backupFile.copyTo(null, basename);
} catch (e) {
if (
@ -396,7 +384,7 @@ JSONFile.prototype = {
// user's computer.
// This reads the file and automatically detects the UTF-8 encoding.
let inputStream = new FileInputStream(
new FileUtils.File(this._options.backupTo),
new FileUtils.File(this._options.backupFile),
FileUtils.MODE_RDONLY,
FileUtils.PERMS_FILE,
0
@ -441,28 +429,27 @@ JSONFile.prototype = {
* @rejects JavaScript exception.
*/
async _save() {
let json;
try {
json = JSON.stringify(this._data);
} catch (e) {
// If serialization fails, try fallback safe JSON converter.
if (typeof this._data.toJSONSafe == "function") {
json = JSON.stringify(this._data.toJSONSafe());
} else {
throw e;
}
}
// Create or overwrite the file.
let bytes = gTextEncoder.encode(json);
if (this._beforeSave) {
await Promise.resolve(this._beforeSave());
}
await OS.File.writeAtomic(
this.path,
bytes,
Object.assign({ tmpPath: this.path + ".tmp" }, this._options)
);
try {
await IOUtils.writeJSON(
this.path,
this._data,
Object.assign({ tmpPath: this.path + ".tmp" }, this._options)
);
} catch (ex) {
if (typeof this._data.toJSONSafe == "function") {
// If serialization fails, try fallback safe JSON converter.
await IOUtils.writeUTF8(
this.path,
this._data.toJSONSafe(),
Object.assign({ tmpPath: this.path + ".tmp" }, this._options)
);
}
}
},
/**

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

@ -8,6 +8,10 @@
// turn on Cu.isInAutomation
Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, true);
PromiseTestUtils.allowMatchingRejectionsGlobally(
/IOUtils: Shutting down and refusing additional I\/O tasks/
);
// The `compatbile` array defines which of the tests below the add-on
// should be compatible in. It's pretty gross.
const ADDONS = [