Bug 492153 - login manager doesn't always notify observers when it handles form. r=dolske, a192=beltzner

This commit is contained in:
Myk Melez 2009-05-20 20:22:35 -07:00
parent 52c601d409
commit 15b91227ff
4 changed files with 291 additions and 14 deletions

View File

@ -1106,6 +1106,11 @@ LoginManager.prototype = {
return [false, foundLogins];
// The reason we didn't end up filling the form, if any. We include
// this in the formInfo object we send with the passwordmgr-found-logins
// notification. See the _notifyFoundLogins docs for possible values.
var didntFillReason = null;
// Attach autocomplete stuff to the username field, if we have
// one. This is normally used to select from multiple accounts,
// but even with one account we should refill if the user edits.
@ -1113,8 +1118,12 @@ LoginManager.prototype = {
this._attachToInput(usernameField);
// Don't clobber an existing password.
if (passwordField.value)
if (passwordField.value) {
didntFillReason = "existingPassword";
this._notifyFoundLogins(didntFillReason, usernameField,
passwordField, foundLogins, null);
return [false, foundLogins];
}
// If the form has an autocomplete=off attribute in play, don't
// fill in the login automatically. We check this after attaching
@ -1141,11 +1150,13 @@ LoginManager.prototype = {
let matchingLogins = logins.filter(function(l)
l.username.toLowerCase() == username);
if (matchingLogins.length)
if (matchingLogins.length) {
selectedLogin = matchingLogins[0];
else
} else {
didntFillReason = "existingUsername";
this.log("Password not filled. None of the stored " +
"logins match the username already present.");
}
} else if (logins.length == 1) {
selectedLogin = logins[0];
} else {
@ -1158,10 +1169,12 @@ LoginManager.prototype = {
matchingLogins = logins.filter(function(l) l.username);
else
matchingLogins = logins.filter(function(l) !l.username);
if (matchingLogins.length == 1)
if (matchingLogins.length == 1) {
selectedLogin = matchingLogins[0];
else
} else {
didntFillReason = "multipleLogins";
this.log("Multiple logins for form, so not filling any.");
}
}
var didFillForm = false;
@ -1174,18 +1187,74 @@ LoginManager.prototype = {
} else if (selectedLogin && !autofillForm) {
// For when autofillForm is false, but we still have the information
// to fill a form, we notify observers.
this._observerService.notifyObservers(form, "passwordmgr-found-form", "noAutofillForms");
didntFillReason = "noAutofillForms";
this._observerService.notifyObservers(form, "passwordmgr-found-form", didntFillReason);
this.log("autofillForms=false but form can be filled; notified observers");
} else if (selectedLogin && isFormDisabled) {
// For when autocomplete is off, but we still have the information
// to fill a form, we notify observers.
this._observerService.notifyObservers(form, "passwordmgr-found-form", "autocompleteOff");
didntFillReason = "autocompleteOff";
this._observerService.notifyObservers(form, "passwordmgr-found-form", didntFillReason);
this.log("autocomplete=off but form can be filled; notified observers");
}
this._notifyFoundLogins(didntFillReason, usernameField, passwordField,
foundLogins, selectedLogin);
return [didFillForm, foundLogins];
},
/**
* Notify observers about an attempt to fill a form that resulted in some
* saved logins being found for the form.
*
* This does not get called if the login manager attempts to fill a form
* but does not find any saved logins. It does, however, get called when
* the login manager does find saved logins whether or not it actually
* fills the form with one of them.
*
* @param didntFillReason {String}
* the reason the login manager didn't fill the form, if any;
* if the value of this parameter is null, then the form was filled;
* otherwise, this parameter will be one of these values:
* existingUsername: the username field already contains a username
* that doesn't match any stored usernames
* existingPassword: the password field already contains a password
* autocompleteOff: autocomplete has been disabled for the form
* or its username or password fields
* multipleLogins: we have multiple logins for the form
* noAutofillForms: the autofillForms pref is set to false
*
* @param usernameField {HTMLInputElement}
* the username field detected by the login manager, if any;
* otherwise null
*
* @param passwordField {HTMLInputElement}
* the password field detected by the login manager
*
* @param foundLogins {Array}
* an array of nsILoginInfos that can be used to fill the form
*
* @param selectedLogin {nsILoginInfo}
* the nsILoginInfo that was/would be used to fill the form, if any;
* otherwise null; whether or not it was actually used depends on
* the value of the didntFillReason parameter
*/
_notifyFoundLogins : function (didntFillReason, usernameField,
passwordField, foundLogins, selectedLogin) {
let formInfo = Cc["@mozilla.org/hash-property-bag;1"].
createInstance(Ci.nsIWritablePropertyBag2);
formInfo.setPropertyAsACString("didntFillReason", didntFillReason);
formInfo.setPropertyAsInterface("usernameField", usernameField);
formInfo.setPropertyAsInterface("passwordField", passwordField);
formInfo.setPropertyAsInterface("foundLogins", foundLogins.concat());
formInfo.setPropertyAsInterface("selectedLogin", selectedLogin);
this._observerService.notifyObservers(formInfo,
"passwordmgr-found-logins",
null);
},
/*
* fillForm

View File

@ -64,6 +64,7 @@ MOCHI_TESTS = \
test_basic_form_autocomplete.html \
test_basic_form_observer_autofillForms.html \
test_basic_form_observer_autocomplete.html \
test_basic_form_observer_foundLogins.html \
test_basic_form_pwonly.html \
test_bug_227640.html \
test_bug_242956.html \

View File

@ -43,18 +43,23 @@ prefs = prefs.getBranch("signon.");
prefs.setBoolPref("autofillForms", false);
var TestObserver = {
receivedNotification : false,
data : "",
receivedNotificationFoundForm : false,
receivedNotificationFoundLogins : false,
dataFoundForm : "",
dataFoundLogins : null,
QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
observe : function (subject, topic, data) {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var pwmgr = Cc["@mozilla.org/login-manager;1"].
getService(Ci.nsILoginManager);
if (topic == "passwordmgr-found-form") {
this.receivedNotification = true;
this.data = data;
this.receivedNotificationFoundForm = true;
this.dataFoundForm = data;
// Now fill the form
pwmgr.fillForm(subject);
} else if (topic == "passwordmgr-found-logins") {
this.receivedNotificationFoundLogins = true;
this.dataFoundLogins = subject.QueryInterface(Ci.nsIPropertyBag2);
}
}
};
@ -63,22 +68,35 @@ var TestObserver = {
var os = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
os.addObserver(TestObserver, "passwordmgr-found-form", false);
os.addObserver(TestObserver, "passwordmgr-found-logins", false);
function startTest(){
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
// Test that observer is notified & got correct data
is(TestObserver.receivedNotification, true, "Checking observer was notified");
is(TestObserver.data, "noAutofillForms", "Checking observer got correct data");
// Test that found-form observer is notified & got correct data
is(TestObserver.receivedNotificationFoundForm, true, "Checking found-form observer was notified");
is(TestObserver.dataFoundForm, "noAutofillForms", "Checking found-form observer got correct data");
// Check that form1 was filled
is($_(1, "uname").value, "testuser", "Checking for filled username");
is($_(1, "pword").value, "testpass", "Checking for filled password");
// Test that found-logins observer is notified & got correct data
is(TestObserver.receivedNotificationFoundLogins, true, "Checking found-logins observer was notified");
is(TestObserver.dataFoundLogins.get("didntFillReason"), "noAutofillForms", "Checking didntFillReason is noAutofillForms");
is(TestObserver.dataFoundLogins.get("usernameField"), $_(1, "uname"), "Checking username field is correct");
is(TestObserver.dataFoundLogins.get("passwordField"), $_(1, "pword"), "Checking password field is correct");
is(TestObserver.dataFoundLogins.get("foundLogins").constructor.name, "Array", "Checking foundLogins is array");
is(TestObserver.dataFoundLogins.get("foundLogins").length, 1, "Checking foundLogins contains one login");
ok(TestObserver.dataFoundLogins.get("selectedLogin").QueryInterface(Ci.nsILoginInfo), "Checking selectedLogin is nsILoginInfo");
ok(TestObserver.dataFoundLogins.get("selectedLogin").equals(TestObserver.dataFoundLogins.get("foundLogins")[0]),
"Checking selectedLogin is found login");
// Reset pref (since we assumed it was true to start)
prefs.setBoolPref("autofillForms", true);
// Remove the observer
os.removeObserver(TestObserver, "passwordmgr-found-form");
os.removeObserver(TestObserver, "passwordmgr-found-logins");
SimpleTest.finish();
}

View File

@ -0,0 +1,189 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for Login Manager</title>
<script type="text/javascript" src="/MochiKit/packed.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="pwmgr_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Login Manager test: notifying observers of passwordmgr-found-logins
<p id="display"></p>
<div id="content" style="display: block">
<form id="form1" action="formtest.js">
<p>This is form 1.</p>
<input type="text" name="uname">
<input type="password" name="pword">
<button type="submit">Submit</button>
<button type="reset"> Reset </button>
</form>
<form id="form2" action="formtest.js">
<p>This is form 2.</p>
<input type="text" name="uname" value="existing">
<input type="password" name="pword">
<button type="submit">Submit</button>
<button type="reset"> Reset </button>
</form>
<form id="form3" action="formtest.js">
<p>This is form 3.</p>
<input type="text" name="uname">
<input type="password" name="pword" value="existing">
<button type="submit">Submit</button>
<button type="reset"> Reset </button>
</form>
<form id="form4" action="formtest.js" autocomplete="off">
<p>This is form 4.</p>
<input type="text" name="uname">
<input type="password" name="pword">
<button type="submit">Submit</button>
<button type="reset"> Reset </button>
</form>
<form id="form5" action="http://www.example.com">
<p>This is form 5.</p>
<input type="text" name="uname">
<input type="password" name="pword">
<button type="submit">Submit</button>
<button type="reset"> Reset </button>
</form>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Login Manager: notifying observers of passwordmgr-found-logins **/
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
// Configure the login manager with two logins for one of the forms
// so we can do a multiple logins test.
var nsLoginInfo =
new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
Components.interfaces.nsILoginInfo);
var login1 = new nsLoginInfo();
login1.init("http://localhost:8888", "http://www.example.com", null,
"testuser1", "testpass1", "uname", "pword");
var login2 = new nsLoginInfo();
login2.init("http://localhost:8888", "http://www.example.com", null,
"testuser2", "testpass2", "uname", "pword");
var pwmgr = Cc["@mozilla.org/login-manager;1"].
getService(Ci.nsILoginManager);
pwmgr.addLogin(login1);
pwmgr.addLogin(login2);
var TestObserver = {
results: {},
QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
observe : function (subject, topic, data) {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
if (topic == "passwordmgr-found-logins") {
var formInfo = subject.QueryInterface(Ci.nsIPropertyBag2);
var id = formInfo.get("passwordField").form.id;
this.results[id].receivedNotification = true;
this.results[id].data = formInfo;
}
}
};
// Initialize the object that stores the results of notifications.
for each (var formID in ["form1", "form2", "form3", "form4", "form5"])
TestObserver.results[formID] = { receivedNotification: false, data: null };
// Add the observer
var os = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
os.addObserver(TestObserver, "passwordmgr-found-logins", false);
function startTest(){
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
// Test notification of a form that was filled.
is(TestObserver.results["form1"].receivedNotification, true, "Checking observer was notified");
is(TestObserver.results["form1"].data.get("didntFillReason"), null, "Checking didntFillReason is null");
is(TestObserver.results["form1"].data.get("usernameField"), $_(1, "uname"), "Checking username field is correct");
is(TestObserver.results["form1"].data.get("passwordField"), $_(1, "pword"), "Checking password field is correct");
is(TestObserver.results["form1"].data.get("foundLogins").constructor.name, "Array", "Checking foundLogins is array");
is(TestObserver.results["form1"].data.get("foundLogins").length, 1, "Checking foundLogins contains one login");
ok(TestObserver.results["form1"].data.get("selectedLogin").QueryInterface(Ci.nsILoginInfo), "Checking selectedLogin is nsILoginInfo");
ok(TestObserver.results["form1"].data.get("selectedLogin").equals(TestObserver.results["form1"].data.get("foundLogins")[0]),
"Checking selectedLogin is found login");
// Test notification of a form that wasn't filled because its username field
// already contained a value.
is(TestObserver.results["form2"].receivedNotification, true, "Checking observer was notified");
is(TestObserver.results["form2"].data.get("didntFillReason"), "existingUsername", "Checking didntFillReason is existingUsername");
is(TestObserver.results["form2"].data.get("usernameField"), $_(2, "uname"), "Checking username field is correct");
is(TestObserver.results["form2"].data.get("passwordField"), $_(2, "pword"), "Checking password field is correct");
is(TestObserver.results["form2"].data.get("foundLogins").constructor.name, "Array", "Checking foundLogins is array");
is(TestObserver.results["form2"].data.get("foundLogins").length, 1, "Checking foundLogins contains one login");
is(TestObserver.results["form2"].data.get("selectedLogin"), null, "Checking selectedLogin is null");
// Test notification of a form that wasn't filled because its password field
// already contained a value.
is(TestObserver.results["form3"].receivedNotification, true, "Checking observer was notified");
is(TestObserver.results["form3"].data.get("didntFillReason"), "existingPassword", "Checking didntFillReason is existingPassword");
is(TestObserver.results["form3"].data.get("usernameField"), $_(3, "uname"), "Checking username field is correct");
is(TestObserver.results["form3"].data.get("passwordField"), $_(3, "pword"), "Checking password field is correct");
is(TestObserver.results["form3"].data.get("foundLogins").constructor.name, "Array", "Checking foundLogins is array");
is(TestObserver.results["form3"].data.get("foundLogins").length, 1, "Checking foundLogins contains one login");
is(TestObserver.results["form3"].data.get("selectedLogin"), null, "Checking selectedLogin is null");
// Test notification of a form that wasn't filled because autocomplete is off.
is(TestObserver.results["form4"].receivedNotification, true, "Checking observer was notified");
is(TestObserver.results["form4"].data.get("didntFillReason"), "autocompleteOff", "Checking didntFillReason is autocompleteOff");
is(TestObserver.results["form4"].data.get("usernameField"), $_(4, "uname"), "Checking username field is correct");
is(TestObserver.results["form4"].data.get("passwordField"), $_(4, "pword"), "Checking password field is correct");
is(TestObserver.results["form4"].data.get("foundLogins").constructor.name, "Array", "Checking foundLogins is array");
is(TestObserver.results["form4"].data.get("foundLogins").length, 1, "Checking foundLogins contains one login");
ok(TestObserver.results["form4"].data.get("selectedLogin").QueryInterface(Ci.nsILoginInfo), "Checking selectedLogin is nsILoginInfo");
ok(TestObserver.results["form4"].data.get("selectedLogin").equals(TestObserver.results["form4"].data.get("foundLogins")[0]),
"Checking selectedLogin is found login");
// Test notification of a form that wasn't filled because multiple logins
// are available for the form.
is(TestObserver.results["form5"].receivedNotification, true, "Checking observer was notified");
is(TestObserver.results["form5"].data.get("didntFillReason"), "multipleLogins", "Checking didntFillReason is multipleLogins");
is(TestObserver.results["form5"].data.get("usernameField"), $_(5, "uname"), "Checking username field is correct");
is(TestObserver.results["form5"].data.get("passwordField"), $_(5, "pword"), "Checking password field is correct");
is(TestObserver.results["form5"].data.get("foundLogins").constructor.name, "Array", "Checking foundLogins is array");
is(TestObserver.results["form5"].data.get("foundLogins").length, 2, "Checking foundLogins contains two logins");
is(TestObserver.results["form5"].data.get("selectedLogin"), null, "Checking selectedLogin is null");
// Test notification of a form that wasn't filled because autofillForms
// was set to false (didntFillReason == noAutofillForms) is done in
// test_basic_form_observer_autofillForms.html.
// Remove the observer
os.removeObserver(TestObserver, "passwordmgr-found-logins");
// Remove the logins added for the multiple logins test.
pwmgr.removeLogin(login1);
pwmgr.removeLogin(login2);
SimpleTest.finish();
}
window.onload = startTest;
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>