Bug 1097191 - Clean up passwords.js. r=trivial

This commit is contained in:
Richard Newman 2014-11-11 11:36:56 -08:00
parent 233b87b070
commit 04b384f508

View File

@ -4,10 +4,7 @@
this.EXPORTED_SYMBOLS = ['PasswordEngine', 'LoginRec']; this.EXPORTED_SYMBOLS = ['PasswordEngine', 'LoginRec'];
const Cu = Components.utils; const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
Cu.import("resource://services-sync/record.js"); Cu.import("resource://services-sync/record.js");
Cu.import("resource://services-sync/constants.js"); Cu.import("resource://services-sync/constants.js");
@ -22,8 +19,10 @@ LoginRec.prototype = {
_logName: "Sync.Record.Login", _logName: "Sync.Record.Login",
}; };
Utils.deferGetSet(LoginRec, "cleartext", ["hostname", "formSubmitURL", Utils.deferGetSet(LoginRec, "cleartext", [
"httpRealm", "username", "password", "usernameField", "passwordField"]); "hostname", "formSubmitURL",
"httpRealm", "username", "password", "usernameField", "passwordField",
]);
this.PasswordEngine = function PasswordEngine(service) { this.PasswordEngine = function PasswordEngine(service) {
@ -34,14 +33,15 @@ PasswordEngine.prototype = {
_storeObj: PasswordStore, _storeObj: PasswordStore,
_trackerObj: PasswordTracker, _trackerObj: PasswordTracker,
_recordObj: LoginRec, _recordObj: LoginRec,
applyIncomingBatchSize: PASSWORDS_STORE_BATCH_SIZE, applyIncomingBatchSize: PASSWORDS_STORE_BATCH_SIZE,
syncPriority: 2, syncPriority: 2,
_syncFinish: function _syncFinish() { _syncFinish: function () {
SyncEngine.prototype._syncFinish.call(this); SyncEngine.prototype._syncFinish.call(this);
// Delete the weave credentials from the server once // Delete the Weave credentials from the server once.
if (!Svc.Prefs.get("deletePwdFxA", false)) { if (!Svc.Prefs.get("deletePwdFxA", false)) {
try { try {
let ids = []; let ids = [];
@ -66,42 +66,41 @@ PasswordEngine.prototype = {
// record success. // record success.
Svc.Prefs.set("deletePwdFxA", true); Svc.Prefs.set("deletePwdFxA", true);
Svc.Prefs.reset("deletePwd"); // The old prefname we previously used. Svc.Prefs.reset("deletePwd"); // The old prefname we previously used.
} } catch (ex) {
catch(ex) {
this._log.debug("Password deletes failed: " + Utils.exceptionStr(ex)); this._log.debug("Password deletes failed: " + Utils.exceptionStr(ex));
} }
} }
}, },
_findDupe: function _findDupe(item) { _findDupe: function (item) {
let login = this._store._nsLoginInfoFromRecord(item); let login = this._store._nsLoginInfoFromRecord(item);
if (!login) if (!login) {
return; return;
}
let logins = Services.logins.findLogins({}, login.hostname, login.formSubmitURL, login.httpRealm);
let logins = Services.logins.findLogins(
{}, login.hostname, login.formSubmitURL, login.httpRealm);
this._store._sleep(0); // Yield back to main thread after synchronous operation. this._store._sleep(0); // Yield back to main thread after synchronous operation.
// Look for existing logins that match the hostname but ignore the password // Look for existing logins that match the hostname, but ignore the password.
for each (let local in logins) for each (let local in logins) {
if (login.matches(local, true) && local instanceof Ci.nsILoginMetaInfo) if (login.matches(local, true) && local instanceof Ci.nsILoginMetaInfo) {
return local.guid; return local.guid;
} }
}
},
}; };
function PasswordStore(name, engine) { function PasswordStore(name, engine) {
Store.call(this, name, engine); Store.call(this, name, engine);
this._nsLoginInfo = new Components.Constructor( this._nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo, "init");
"@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo, "init");
} }
PasswordStore.prototype = { PasswordStore.prototype = {
__proto__: Store.prototype, __proto__: Store.prototype,
_nsLoginInfoFromRecord: function PasswordStore__nsLoginInfoRec(record) { _nsLoginInfoFromRecord: function (record) {
if (record.formSubmitURL && if (record.formSubmitURL && record.httpRealm) {
record.httpRealm) { this._log.warn("Record " + record.id + " has both formSubmitURL and httpRealm. Skipping.");
this._log.warn("Record " + record.id +
" has both formSubmitURL and httpRealm. Skipping.");
return null; return null;
} }
@ -121,28 +120,28 @@ PasswordStore.prototype = {
return info; return info;
}, },
_getLoginFromGUID: function PasswordStore__getLoginFromGUID(id) { _getLoginFromGUID: function (id) {
let prop = Cc["@mozilla.org/hash-property-bag;1"]. let prop = Cc["@mozilla.org/hash-property-bag;1"].createInstance(Ci.nsIWritablePropertyBag2);
createInstance(Ci.nsIWritablePropertyBag2);
prop.setPropertyAsAUTF8String("guid", id); prop.setPropertyAsAUTF8String("guid", id);
let logins = Services.logins.searchLogins({}, prop); let logins = Services.logins.searchLogins({}, prop);
this._sleep(0); // Yield back to main thread after synchronous operation. this._sleep(0); // Yield back to main thread after synchronous operation.
if (logins.length > 0) { if (logins.length > 0) {
this._log.trace(logins.length + " items matching " + id + " found."); this._log.trace(logins.length + " items matching " + id + " found.");
return logins[0]; return logins[0];
} else {
this._log.trace("No items matching " + id + " found. Ignoring");
} }
this._log.trace("No items matching " + id + " found. Ignoring");
return null; return null;
}, },
getAllIDs: function PasswordStore__getAllIDs() { getAllIDs: function () {
let items = {}; let items = {};
let logins = Services.logins.getAllLogins({}); let logins = Services.logins.getAllLogins({});
for (let i = 0; i < logins.length; i++) { for (let i = 0; i < logins.length; i++) {
// Skip over Weave password/passphrase entries // Skip over Weave password/passphrase entries.
let metaInfo = logins[i].QueryInterface(Ci.nsILoginMetaInfo); let metaInfo = logins[i].QueryInterface(Ci.nsILoginMetaInfo);
if (Utils.getSyncCredentialsHosts().has(metaInfo.hostname)) { if (Utils.getSyncCredentialsHosts().has(metaInfo.hostname)) {
continue; continue;
@ -154,7 +153,7 @@ PasswordStore.prototype = {
return items; return items;
}, },
changeItemID: function PasswordStore__changeItemID(oldID, newID) { changeItemID: function (oldID, newID) {
this._log.trace("Changing item ID: " + oldID + " to " + newID); this._log.trace("Changing item ID: " + oldID + " to " + newID);
let oldLogin = this._getLoginFromGUID(oldID); let oldLogin = this._getLoginFromGUID(oldID);
@ -167,41 +166,43 @@ PasswordStore.prototype = {
return; return;
} }
let prop = Cc["@mozilla.org/hash-property-bag;1"]. let prop = Cc["@mozilla.org/hash-property-bag;1"]
createInstance(Ci.nsIWritablePropertyBag2); .createInstance(Ci.nsIWritablePropertyBag2);
prop.setPropertyAsAUTF8String("guid", newID); prop.setPropertyAsAUTF8String("guid", newID);
Services.logins.modifyLogin(oldLogin, prop); Services.logins.modifyLogin(oldLogin, prop);
}, },
itemExists: function PasswordStore__itemExists(id) { itemExists: function (id) {
if (this._getLoginFromGUID(id)) return !!this._getLoginFromGUID(id);
return true;
return false;
}, },
createRecord: function createRecord(id, collection) { createRecord: function (id, collection) {
let record = new LoginRec(collection, id); let record = new LoginRec(collection, id);
let login = this._getLoginFromGUID(id); let login = this._getLoginFromGUID(id);
if (login) { if (!login) {
record.hostname = login.hostname;
record.formSubmitURL = login.formSubmitURL;
record.httpRealm = login.httpRealm;
record.username = login.username;
record.password = login.password;
record.usernameField = login.usernameField;
record.passwordField = login.passwordField;
}
else
record.deleted = true; record.deleted = true;
return record;
}
record.hostname = login.hostname;
record.formSubmitURL = login.formSubmitURL;
record.httpRealm = login.httpRealm;
record.username = login.username;
record.password = login.password;
record.usernameField = login.usernameField;
record.passwordField = login.passwordField;
return record; return record;
}, },
create: function PasswordStore__create(record) { create: function (record) {
let login = this._nsLoginInfoFromRecord(record); let login = this._nsLoginInfoFromRecord(record);
if (!login) if (!login) {
return; return;
}
this._log.debug("Adding login for " + record.hostname); this._log.debug("Adding login for " + record.hostname);
this._log.trace("httpRealm: " + JSON.stringify(login.httpRealm) + "; " + this._log.trace("httpRealm: " + JSON.stringify(login.httpRealm) + "; " +
"formSubmitURL: " + JSON.stringify(login.formSubmitURL)); "formSubmitURL: " + JSON.stringify(login.formSubmitURL));
@ -213,7 +214,7 @@ PasswordStore.prototype = {
} }
}, },
remove: function PasswordStore__remove(record) { remove: function (record) {
this._log.trace("Removing login " + record.id); this._log.trace("Removing login " + record.id);
let loginItem = this._getLoginFromGUID(record.id); let loginItem = this._getLoginFromGUID(record.id);
@ -225,7 +226,7 @@ PasswordStore.prototype = {
Services.logins.removeLogin(loginItem); Services.logins.removeLogin(loginItem);
}, },
update: function PasswordStore__update(record) { update: function (record) {
let loginItem = this._getLoginFromGUID(record.id); let loginItem = this._getLoginFromGUID(record.id);
if (!loginItem) { if (!loginItem) {
this._log.debug("Skipping update for unknown item: " + record.hostname); this._log.debug("Skipping update for unknown item: " + record.hostname);
@ -234,8 +235,10 @@ PasswordStore.prototype = {
this._log.debug("Updating " + record.hostname); this._log.debug("Updating " + record.hostname);
let newinfo = this._nsLoginInfoFromRecord(record); let newinfo = this._nsLoginInfoFromRecord(record);
if (!newinfo) if (!newinfo) {
return; return;
}
try { try {
Services.logins.modifyLogin(loginItem, newinfo); Services.logins.modifyLogin(loginItem, newinfo);
} catch(ex) { } catch(ex) {
@ -245,9 +248,9 @@ PasswordStore.prototype = {
} }
}, },
wipe: function PasswordStore_wipe() { wipe: function () {
Services.logins.removeAllLogins(); Services.logins.removeAllLogins();
} },
}; };
function PasswordTracker(name, engine) { function PasswordTracker(name, engine) {
@ -258,15 +261,15 @@ function PasswordTracker(name, engine) {
PasswordTracker.prototype = { PasswordTracker.prototype = {
__proto__: Tracker.prototype, __proto__: Tracker.prototype,
startTracking: function() { startTracking: function () {
Svc.Obs.add("passwordmgr-storage-changed", this); Svc.Obs.add("passwordmgr-storage-changed", this);
}, },
stopTracking: function() { stopTracking: function () {
Svc.Obs.remove("passwordmgr-storage-changed", this); Svc.Obs.remove("passwordmgr-storage-changed", this);
}, },
observe: function(subject, topic, data) { observe: function (subject, topic, data) {
Tracker.prototype.observe.call(this, subject, topic, data); Tracker.prototype.observe.call(this, subject, topic, data);
if (this.ignoreAll) { if (this.ignoreAll) {
@ -278,7 +281,7 @@ PasswordTracker.prototype = {
switch (data) { switch (data) {
case "modifyLogin": case "modifyLogin":
subject = subject.QueryInterface(Ci.nsIArray).queryElementAt(1, Ci.nsILoginMetaInfo); subject = subject.QueryInterface(Ci.nsIArray).queryElementAt(1, Ci.nsILoginMetaInfo);
// fallthrough // Fall through.
case "addLogin": case "addLogin":
case "removeLogin": case "removeLogin":
// Skip over Weave password/passphrase changes. // Skip over Weave password/passphrase changes.
@ -296,5 +299,5 @@ PasswordTracker.prototype = {
this.score += SCORE_INCREMENT_XLARGE; this.score += SCORE_INCREMENT_XLARGE;
break; break;
} }
} },
}; };