Bug 380931: use nsIFile instead of path/filename strings, patch by Justin Dolske <dolske@mozilla.com>, r=mconnor

This commit is contained in:
gavin@gavinsharp.com 2007-05-29 19:47:47 -07:00
Родитель 4a82ffd916
Коммит f8c9dfa7ed
2 изменённых файлов: 93 добавлений и 86 удалений

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

@ -170,8 +170,7 @@ LoginManager.prototype = {
this._prefBranch.addObserver("", this._observer, false); this._prefBranch.addObserver("", this._observer, false);
// Get current preference values. // Get current preference values.
if (this._prefBranch.prefHasUserValue("debug")) this._debug = this._prefBranch.getBoolPref("debug");
this._debug = this._prefBranch.getBoolPref("debug");
this._remember = this._prefBranch.getBoolPref("rememberSignons"); this._remember = this._prefBranch.getBoolPref("rememberSignons");
@ -254,14 +253,8 @@ LoginManager.prototype = {
this._pwmgr.log("got change to " + prefName + " preference"); this._pwmgr.log("got change to " + prefName + " preference");
if (prefName == "debug") { if (prefName == "debug") {
// The debug pref is hidden (so no default) this._pwmgr._debug =
if (this._pwmgr._prefBranch.prefHasUserValue("debug")) this._pwmgr._prefBranch.getBoolPref("debug");
var debug =
this._pwmgr._prefBranch.getBoolPref("debug");
else
debug = false;
this._pwmgr._debug = debug;
} else if (prefName == "rememberSignons") { } else if (prefName == "rememberSignons") {
this._pwmgr._remember = this._pwmgr._remember =
this._pwmgr._prefBranch.getBoolPref("rememberSignons"); this._pwmgr._prefBranch.getBoolPref("rememberSignons");

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

@ -52,23 +52,22 @@ LoginManagerStorage_legacy.prototype = {
__logService : null, // Console logging service, used for debugging. __logService : null, // Console logging service, used for debugging.
get _logService() { get _logService() {
if (!this.__logService) if (!this.__logService)
this.__logService = Cc["@mozilla.org/consoleservice;1"] this.__logService = Cc["@mozilla.org/consoleservice;1"].
.getService(Ci.nsIConsoleService); getService(Ci.nsIConsoleService);
return this.__logService; return this.__logService;
}, },
__decoderRing : null, // nsSecretDecoderRing service __decoderRing : null, // nsSecretDecoderRing service
get _decoderRing() { get _decoderRing() {
if (!this.__decoderRing) if (!this.__decoderRing)
this.__decoderRing = Cc["@mozilla.org/security/sdr;1"] this.__decoderRing = Cc["@mozilla.org/security/sdr;1"].
.getService(Ci.nsISecretDecoderRing); getService(Ci.nsISecretDecoderRing);
return this.__decoderRing; return this.__decoderRing;
}, },
_prefBranch : null, // Preferences service _prefBranch : null, // Preferences service
_datafile : null, // name of datafile (usually "signons2.txt") _signonsFile : null, // nsIFile for "signons2.txt" (or whatever pref is)
_datapath : null, // path to datafile (usually profile directory)
_debug : false, // mirrors signon.debug _debug : false, // mirrors signon.debug
@ -103,15 +102,13 @@ LoginManagerStorage_legacy.prototype = {
initWithFile : function(aInputFile, aOutputFile) { initWithFile : function(aInputFile, aOutputFile) {
this._datapath = aInputFile.parent.path; this._signonsFile = aInputFile;
this._datafile = aInputFile.leafName;
this.init(); this.init();
if (aOutputFile) { if (aOutputFile) {
this._datapath = aOutputFile.parent.path; this._signonsFile = aOutputFile;
this._datafile = aOutputFile.leafName; this._writeFile();
this._writeFile(this._datapath, this._datafile);
} }
}, },
@ -130,8 +127,7 @@ LoginManagerStorage_legacy.prototype = {
this._prefBranch = this._prefBranch.getBranch("signon."); this._prefBranch = this._prefBranch.getBranch("signon.");
this._prefBranch.QueryInterface(Ci.nsIPrefBranch2); this._prefBranch.QueryInterface(Ci.nsIPrefBranch2);
if (this._prefBranch.prefHasUserValue("debug")) this._debug = this._prefBranch.getBoolPref("debug");
this._debug = this._prefBranch.getBoolPref("debug");
// Check to see if the internal PKCS#11 token has been initialized. // Check to see if the internal PKCS#11 token has been initialized.
// If not, set a blank password. // If not, set a blank password.
@ -144,40 +140,26 @@ LoginManagerStorage_legacy.prototype = {
token.initPassword(""); token.initPassword("");
} }
// Get the location of the user's profile.
if (!this._datapath) {
var DIR_SERVICE = new Components.Constructor(
"@mozilla.org/file/directory_service;1", "nsIProperties");
this._datapath = (new DIR_SERVICE()).get("ProfD", Ci.nsIFile).path;
}
if (!this._datafile)
this._datafile = this._prefBranch.getCharPref("SignonFileName2");
var importFile = null; var importFile = null;
if (!this._doesFileExist(this._datapath, this._datafile)) { // If initWithFile is calling us, _signonsFile is already set.
this.log("SignonFilename2 file does not exist. (file=" + if (!this._signonsFile)
this._datafile + ") path=(" + this._datapath + ")"); [this._signonsFile, importFile] = this._getSignonsFile();
// Try reading the old file // If we have an import file, do a switcharoo before reading it.
importFile = this._prefBranch.getCharPref("SignonFileName"); if (importFile) {
if (!this._doesFileExist(this._datapath, importFile)) { this.log("Importing " + importFile.path);
this.log("SignonFilename1 file does not exist. (file=" +
importFile + ") path=(" + this._datapath + ")"); var tmp = this._signonsFile;
this.log("Creating new signons file..."); this._signonsFile = importFile;
importFile = null;
this._writeFile(this._datapath, this._datafile);
}
} }
// Read in the stored login data. // Read in the stored login data.
if (importFile) { this._readFile()
this.log("Importing " + importFile);
this._readFile(this._datapath, importFile);
this._writeFile(this._datapath, this._datafile); // If we were importing, write back to the normal file.
} else { if (importFile) {
this._readFile(this._datapath, this._datafile); this._signonsFile = tmp;
this._writeFile();
} }
}, },
@ -195,7 +177,7 @@ LoginManagerStorage_legacy.prototype = {
this._logins[key].push(login); this._logins[key].push(login);
this._writeFile(this._datapath, this._datafile); this._writeFile();
}, },
@ -223,7 +205,7 @@ LoginManagerStorage_legacy.prototype = {
if (logins.length == 0) if (logins.length == 0)
delete this._logins[key]; delete this._logins[key];
this._writeFile(this._datapath, this._datafile); this._writeFile();
}, },
@ -264,7 +246,7 @@ LoginManagerStorage_legacy.prototype = {
this._logins = {}; this._logins = {};
// Disabled hosts kept, as one presumably doesn't want to erase those. // Disabled hosts kept, as one presumably doesn't want to erase those.
this._writeFile(this._datapath, this._datafile); this._writeFile();
}, },
@ -303,7 +285,7 @@ LoginManagerStorage_legacy.prototype = {
else else
this._disabledHosts[hostname] = true; this._disabledHosts[hostname] = true;
this._writeFile(this._datapath, this._datafile); this._writeFile();
}, },
@ -349,23 +331,73 @@ LoginManagerStorage_legacy.prototype = {
/* /*
* _readFile * _getSignonsFile
*
* Determines what file to use based on prefs. Returns it as a
* nsILocalFile, along with a file to import from first (if needed)
* *
*/ */
_readFile : function (pathname, filename) { _getSignonsFile : function() {
var oldFormat = false; var importFile = null;
var writeOnFinish = false;
this.log("Reading passwords from " + pathname + "/" + filename); // Get the location of the user's profile.
var DIR_SERVICE = new Components.Constructor(
"@mozilla.org/file/directory_service;1", "nsIProperties");
var pathname = (new DIR_SERVICE()).get("ProfD", Ci.nsIFile).path;
var file = Cc["@mozilla.org/file/local;1"]
.createInstance(Ci.nsILocalFile); // First try the default pref...
var filename = this._prefBranch.getCharPref("SignonFileName2");
var file = Cc["@mozilla.org/file/local;1"].
createInstance(Ci.nsILocalFile);
file.initWithPath(pathname); file.initWithPath(pathname);
file.append(filename); file.append(filename);
if (!file.exists()) {
this.log("SignonFilename2 file does not exist. file=" +
filename + ", path=" + pathname);
// Then try the old pref...
var oldname = this._prefBranch.getCharPref("SignonFileName");
importFile = Cc["@mozilla.org/file/local;1"].
createInstance(Ci.nsILocalFile);
importFile.initWithPath(pathname);
importFile.append(oldname);
if (!importFile.exists()) {
this.log("SignonFilename1 file does not exist. file=" +
oldname + ", path=" + pathname);
importFile = null;
}
}
return [file, importFile];
},
/*
* _readFile
*
*/
_readFile : function () {
var oldFormat = false;
var writeOnFinish = false;
this.log("Reading passwords from " + this._signonsFile.path);
// If it doesn't exist, just create an empty file and bail out.
if (!this._signonsFile.exists()) {
this.log("Creating new signons file...");
this._writeFile();
return;
}
var inputStream = Cc["@mozilla.org/network/file-input-stream;1"] var inputStream = Cc["@mozilla.org/network/file-input-stream;1"]
.createInstance(Ci.nsIFileInputStream); .createInstance(Ci.nsIFileInputStream);
inputStream.init(file, 0x01, -1, null); // RD_ONLY, -1=default perm // init the stream as RD_ONLY, -1 == default permissions.
inputStream.init(this._signonsFile, 0x01, -1, null);
var lineStream = inputStream.QueryInterface(Ci.nsILineInputStream); var lineStream = inputStream.QueryInterface(Ci.nsILineInputStream);
var line = { value: "" }; var line = { value: "" };
@ -388,7 +420,7 @@ LoginManagerStorage_legacy.prototype = {
oldFormat = true; oldFormat = true;
} else if (line.value != "#2d") { } else if (line.value != "#2d") {
this.log("invalid file header (" + line.value + ")"); this.log("invalid file header (" + line.value + ")");
throw "invalid file header in " + filename; throw "invalid file header in signons file";
// We could disable later writing to file, so we // We could disable later writing to file, so we
// don't clobber whatever it is. ...however, that // don't clobber whatever it is. ...however, that
// would mean corrupt files are not self-healing. // would mean corrupt files are not self-healing.
@ -501,7 +533,7 @@ LoginManagerStorage_legacy.prototype = {
lineStream.close(); lineStream.close();
if (writeOnFinish) if (writeOnFinish)
this._writeFile(pathname, filename); this._writeFile();
return; return;
}, },
@ -511,25 +543,20 @@ LoginManagerStorage_legacy.prototype = {
* _writeFile * _writeFile
* *
*/ */
_writeFile : function (pathname, filename) { _writeFile : function () {
function writeLine(data) { function writeLine(data) {
data += "\r\n"; data += "\r\n";
outputStream.write(data, data.length); outputStream.write(data, data.length);
} }
this.log("Writing passwords to " + pathname + "/" + filename); this.log("Writing passwords to " + this._signonsFile.path);
var file = Cc["@mozilla.org/file/local;1"]
.createInstance(Ci.nsILocalFile);
file.initWithPath(pathname);
file.append(filename);
var outputStream = Cc["@mozilla.org/network/safe-file-output-stream;1"] var outputStream = Cc["@mozilla.org/network/safe-file-output-stream;1"]
.createInstance(Ci.nsIFileOutputStream); .createInstance(Ci.nsIFileOutputStream);
outputStream.QueryInterface(Ci.nsISafeOutputStream); outputStream.QueryInterface(Ci.nsISafeOutputStream);
// WR_ONLY|CREAT|TRUNC // WR_ONLY|CREAT|TRUNC
outputStream.init(file, 0x02 | 0x08 | 0x20, 0600, null); outputStream.init(this._signonsFile, 0x02 | 0x08 | 0x20, 0600, null);
// write file version header // write file version header
writeLine("#2d"); writeLine("#2d");
@ -649,19 +676,6 @@ LoginManagerStorage_legacy.prototype = {
return plainText; return plainText;
}, },
/*
* _doesFileExist
*
*/
_doesFileExist : function (filepath, filename) {
var file = Cc["@mozilla.org/file/local;1"]
.createInstance(Ci.nsILocalFile);
file.initWithPath(filepath);
file.append(filename);
return file.exists();
}
}; // end of nsLoginManagerStorage_legacy implementation }; // end of nsLoginManagerStorage_legacy implementation