Bug 1339105 Part 2: Fix registerFakePath in head_migration.js to work when the key has already been accessed. r=mak

This also fixes the cleanup to fully remove the fake path and put back original if it existed.
This commit is contained in:
Bob Owen 2017-05-22 20:41:28 +01:00
parent edf3a239b1
commit ad828b90f2

View File

@ -49,21 +49,25 @@ function promiseMigration(migrator, resourceType, aProfile = null) {
* Replaces a directory service entry with a given nsIFile.
*/
function registerFakePath(key, file) {
// Register our own provider for the Library directory.
let provider = {
getFile(prop, persistent) {
persistent.value = true;
if (prop == key) {
return file;
}
throw Cr.NS_ERROR_FAILURE;
},
QueryInterface: XPCOMUtils.generateQI([ Ci.nsIDirectoryServiceProvider ])
};
Services.dirsvc.QueryInterface(Ci.nsIDirectoryService)
.registerProvider(provider);
let dirsvc = Services.dirsvc.QueryInterface(Ci.nsIProperties);
let originalFile;
try {
// If a file is already provided save it and undefine, otherwise set will
// throw for persistent entries (ones that are cached).
originalFile = dirsvc.get(key, Ci.nsIFile);
dirsvc.undefine(key);
} catch (e) {
// dirsvc.get will throw if nothing provides for the key and dirsvc.undefine
// will throw if it's not a persistent entry, in either case we don't want
// to set the original file in cleanup.
originalFile = undefined;
}
dirsvc.set(key, file);
do_register_cleanup(() => {
Services.dirsvc.QueryInterface(Ci.nsIDirectoryService)
.unregisterProvider(provider);
dirsvc.undefine(key);
if (originalFile) {
dirsvc.set(key, originalFile);
}
});
}