Bug 1742714 - Get rid of nsIFormSubmitObserver; r=geckoview-reviewers,agi,smaug

Differential Revision: https://phabricator.services.mozilla.com/D132091
This commit is contained in:
Edgar Chen 2021-12-06 12:23:08 +00:00
Родитель ed3fd395ba
Коммит be408c368a
13 изменённых файлов: 202 добавлений и 373 удалений

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

@ -51,10 +51,6 @@ class FormValidationChild extends JSWindowActorChild {
}
}
/*
* nsIFormSubmitObserver
*/
notifyInvalidSubmit(aFormElement, aInvalidElements) {
// Show a validation message on the first focusable element.
for (let element of aInvalidElements) {

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

@ -68,12 +68,6 @@ function checkPopupHide() {
);
}
var gObserver = {
QueryInterface: ChromeUtils.generateQI(["nsIFormSubmitObserver"]),
notifyInvalidSubmit(aFormElement, aInvalidElements) {},
};
var testId = 0;
function incrementTest() {
@ -440,53 +434,6 @@ add_task(async function() {
gBrowser.removeCurrentTab();
});
/**
* In this test, we check that nothing happen if the invalid form is
* submitted in a background tab.
*/
add_task(async function() {
// Observers don't propagate currently across processes. We may add support for this in the
// future via the addon compat layer.
if (gMultiProcessBrowser) {
return;
}
incrementTest();
let uri =
getDocHeader() +
"<form target='t' action='data:text/html,'><input id='i' required><input id='s' type='submit'></form>" +
getDocFooter();
let browser = await openNewTab(uri, true);
isnot(
gBrowser.selectedBrowser,
browser,
"This tab should have been loaded in background"
);
let notifierPromise = new Promise((resolve, reject) => {
gObserver.notifyInvalidSubmit = function() {
executeSoon(function() {
checkPopupHide();
// Clean-up
Services.obs.removeObserver(gObserver, "invalidformsubmit");
gObserver.notifyInvalidSubmit = function() {};
resolve();
});
};
Services.obs.addObserver(gObserver, "invalidformsubmit");
executeSoon(function() {
browser.contentDocument.getElementById("s").click();
});
});
await notifierPromise;
gBrowser.removeTab(gBrowser.getTabForBrowser(browser));
});
/**
* In this test, we check that the message is correctly updated when it changes.
*/

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

@ -51,8 +51,6 @@
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/StaticPrefs_prompts.h"
#include "mozilla/StaticPrefs_signon.h"
#include "nsIFormSubmitObserver.h"
#include "nsIObserverService.h"
#include "nsCategoryManagerUtils.h"
#include "nsIContentInlines.h"
#include "nsISimpleEnumerator.h"
@ -1876,27 +1874,18 @@ bool HTMLFormElement::CheckValidFormSubmission() {
* invalid controls in the form.
* This should not be done if the form has been submitted with .submit().
*
* NOTE: for the moment, we are also checking that there is an observer for
* NS_INVALIDFORMSUBMIT_SUBJECT so it will prevent blocking form submission
* if the browser does not have implemented a UI yet.
* NOTE: for the moment, we are also checking that whether the MozInvalidForm
* event gets prevented default so it will prevent blocking form submission if
* the browser does not have implemented a UI yet.
*
* TODO: the check for observer should be removed later when HTML5 Forms will
* be spread enough and authors will assume forms can't be submitted when
* invalid. See bug 587671.
* TODO: the check for MozInvalidForm event should be removed later when HTML5
* Forms will be spread enough and authors will assume forms can't be
* submitted when invalid. See bug 587671.
*/
NS_ASSERTION(!HasAttr(kNameSpaceID_None, nsGkAtoms::novalidate),
"We shouldn't be there if novalidate is set!");
// When .submit() is called aEvent = nullptr so we can rely on that to know if
// we have to check the validity of the form.
nsCOMPtr<nsIObserverService> service =
mozilla::services::GetObserverService();
if (!service) {
NS_WARNING("No observer service available!");
return true;
}
AutoTArray<RefPtr<Element>, 32> invalidElements;
if (CheckFormValidity(&invalidElements)) {
return true;
@ -1962,39 +1951,7 @@ bool HTMLFormElement::CheckValidFormSubmission() {
DispatchEvent(*event);
bool result = !event->DefaultPrevented();
nsCOMPtr<nsISimpleEnumerator> theEnum;
nsresult rv = service->EnumerateObservers(NS_INVALIDFORMSUBMIT_SUBJECT,
getter_AddRefs(theEnum));
NS_ENSURE_SUCCESS(rv, result);
bool hasObserver = false;
rv = theEnum->HasMoreElements(&hasObserver);
if (NS_SUCCEEDED(rv) && hasObserver) {
result = false;
nsCOMPtr<nsISupports> inst;
nsCOMPtr<nsIFormSubmitObserver> observer;
bool more = true;
while (NS_SUCCEEDED(theEnum->HasMoreElements(&more)) && more) {
theEnum->GetNext(getter_AddRefs(inst));
observer = do_QueryInterface(inst);
if (observer) {
observer->NotifyInvalidSubmit(this, invalidElements);
}
}
}
if (result) {
NS_WARNING(
"There is no observer for \"invalidformsubmit\". \
One should be implemented!");
}
return result;
return !event->DefaultPrevented();
}
void HTMLFormElement::UpdateValidity(bool aElementValidity) {

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

@ -24,7 +24,6 @@ MOCHITEST_CHROME_MANIFESTS += [
BROWSER_CHROME_MANIFESTS += ["test/browser.ini"]
XPIDL_SOURCES += [
"nsIFormSubmitObserver.idl",
"nsIMenuBuilder.idl",
]

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

@ -16,9 +16,6 @@
#include "nsISimpleEnumerator.h"
#include "nsContentUtils.h"
#include "nsIFormSubmitObserver.h"
#include "nsIObserverService.h"
const uint16_t nsIConstraintValidation::sContentSpecifiedMaxLengthMessage = 256;
using namespace mozilla;
@ -87,36 +84,6 @@ bool nsIConstraintValidation::ReportValidity() {
element->DispatchEvent(*event);
nsCOMPtr<nsIObserverService> service =
mozilla::services::GetObserverService();
if (!service) {
NS_WARNING("No observer service available!");
return true;
}
nsCOMPtr<nsISimpleEnumerator> theEnum;
nsresult rv = service->EnumerateObservers(NS_INVALIDFORMSUBMIT_SUBJECT,
getter_AddRefs(theEnum));
// Return true on error here because that's what we always did
NS_ENSURE_SUCCESS(rv, true);
bool hasObserver = false;
rv = theEnum->HasMoreElements(&hasObserver);
NS_ENSURE_SUCCESS(rv, true);
nsCOMPtr<nsISupports> inst;
nsCOMPtr<nsIFormSubmitObserver> observer;
bool more = true;
while (NS_SUCCEEDED(theEnum->HasMoreElements(&more)) && more) {
theEnum->GetNext(getter_AddRefs(inst));
observer = do_QueryInterface(inst);
if (observer) {
observer->NotifyInvalidSubmit(nullptr, invalidElements);
}
}
auto* inputElement = HTMLInputElement::FromNode(element);
if (inputElement && inputElement->State().HasState(NS_EVENT_STATE_FOCUS)) {
inputElement->UpdateValidityUIBits(true);

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

@ -1,25 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsISupports.idl"
interface mozIDOMWindow;
interface nsIURI;
interface nsIArray;
webidl HTMLFormElement;
webidl Element;
[scriptable, uuid(867cb7e7-835d-408b-9788-d2834d284e03)]
interface nsIFormSubmitObserver: nsISupports
{
void notifyInvalidSubmit(in HTMLFormElement formNode,
in Array<Element> invalidElements);
};
%{C++
#define NS_INVALIDFORMSUBMIT_SUBJECT "invalidformsubmit"
%}

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

@ -46,96 +46,79 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=589696
/** Test for Bug 589696 **/
var os = SpecialPowers.Cc['@mozilla.org/observer-service;1']
.getService(SpecialPowers.Ci.nsIObserverService);
var observers = os.enumerateObservers("invalidformsubmit");
document.getElementById('av').addEventListener("invalid", function(aEvent) {
aEvent.target.removeAttribute("invalid", arguments.callee, false);
ok(true, "formnovalidate should not apply on if not set on the submit " +
"control used for the submission");
document.getElementById('b').click();
});
document.getElementById('bv').addEventListener("invalid", function(aEvent) {
aEvent.target.removeAttribute("invalid", arguments.callee, false);
ok(true, "formnovalidate should not apply on if not set on the submit " +
"control used for the submission");
var c = document.getElementById('c');
c.focus();
synthesizeKey("KEY_Enter");
});
document.getElementById('c').addEventListener("invalid", function(aEvent) {
aEvent.target.removeAttribute("invalid", arguments.callee, false);
ok(true, "formnovalidate should only apply on submit controls");
document.getElementById('d').click();
});
document.forms[3].addEventListener("submit", function(aEvent) {
aEvent.target.removeAttribute("submit", arguments.callee, false);
ok(true, "formnovalidate applies if set on the submit control used for the submission");
document.getElementById('e').click();
});
document.forms[4].addEventListener("submit", function(aEvent) {
aEvent.target.removeAttribute("submit", arguments.callee, false);
ok(true, "formnovalidate applies if set on the submit control used for the submission");
SimpleTest.executeSoon(SimpleTest.finish);
});
/**
* formnovalidate should prevent form validation if set on the submit control
* used to submit the form.
*
* The following test should not be done if there is no observer for
* "invalidformsubmit" because the form submission will not be canceled in that
* case.
* We have to be sure invalid events behave as expected.
* They should be sent before the submit event so we can just create a test
* failure if we got one when unexpected. All of them should be caught if
* sent.
* At worst, we got random green which isn't harmful.
* If expected, they will be part of the chain reaction.
*/
if (observers.hasMoreElements()) {
document.getElementById('av').addEventListener("invalid", function(aEvent) {
aEvent.target.removeAttribute("invalid", arguments.callee, false);
ok(true, "formnovalidate should not apply on if not set on the submit " +
"control used for the submission");
document.getElementById('b').click();
});
document.getElementById('bv').addEventListener("invalid", function(aEvent) {
aEvent.target.removeAttribute("invalid", arguments.callee, false);
ok(true, "formnovalidate should not apply on if not set on the submit " +
"control used for the submission");
var c = document.getElementById('c');
c.focus();
synthesizeKey("KEY_Enter");
});
document.getElementById('c').addEventListener("invalid", function(aEvent) {
aEvent.target.removeAttribute("invalid", arguments.callee, false);
ok(true, "formnovalidate should only apply on submit controls");
document.getElementById('d').click();
});
document.forms[3].addEventListener("submit", function(aEvent) {
aEvent.target.removeAttribute("submit", arguments.callee, false);
ok(true, "formnovalidate applies if set on the submit control used for the submission");
document.getElementById('e').click();
});
document.forms[4].addEventListener("submit", function(aEvent) {
aEvent.target.removeAttribute("submit", arguments.callee, false);
ok(true, "formnovalidate applies if set on the submit control used for the submission");
SimpleTest.executeSoon(SimpleTest.finish);
});
/**
* We have to be sure invalid events behave as expected.
* They should be sent before the submit event so we can just create a test
* failure if we got one when unexpected. All of them should be caught if
* sent.
* At worst, we got random green which isn't harmful.
* If expected, they will be part of the chain reaction.
*/
function unexpectedInvalid(aEvent)
{
aEvent.target.removeAttribute("invalid", unexpectedInvalid, false);
ok(false, "invalid event should not be sent");
}
document.getElementById('dv').addEventListener("invalid", unexpectedInvalid);
document.getElementById('ev').addEventListener("invalid", unexpectedInvalid);
/**
* Some submission have to be canceled. In that case, the submit events should
* not be sent.
* Same behavior as unexpected invalid events.
*/
function unexpectedSubmit(aEvent)
{
aEvent.target.removeAttribute("submit", unexpectedSubmit, false);
ok(false, "submit event should not be sent");
}
document.forms[0].addEventListener("submit", unexpectedSubmit);
document.forms[1].addEventListener("submit", unexpectedSubmit);
document.forms[2].addEventListener("submit", unexpectedSubmit);
SimpleTest.waitForExplicitFinish();
// This is going to call all the tests (with a chain reaction).
SimpleTest.waitForFocus(function() {
document.getElementById('a').click();
});
} else {
todo(false, "No 'invalidformsubmit' observers. Skip test.");
function unexpectedInvalid(aEvent)
{
aEvent.target.removeAttribute("invalid", unexpectedInvalid, false);
ok(false, "invalid event should not be sent");
}
document.getElementById('dv').addEventListener("invalid", unexpectedInvalid);
document.getElementById('ev').addEventListener("invalid", unexpectedInvalid);
/**
* Some submission have to be canceled. In that case, the submit events should
* not be sent.
* Same behavior as unexpected invalid events.
*/
function unexpectedSubmit(aEvent)
{
aEvent.target.removeAttribute("submit", unexpectedSubmit, false);
ok(false, "submit event should not be sent");
}
document.forms[0].addEventListener("submit", unexpectedSubmit);
document.forms[1].addEventListener("submit", unexpectedSubmit);
document.forms[2].addEventListener("submit", unexpectedSubmit);
SimpleTest.waitForExplicitFinish();
// This is going to call all the tests (with a chain reaction).
SimpleTest.waitForFocus(function() {
document.getElementById('a').click();
});
</script>
</pre>
</body>

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

@ -34,9 +34,10 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=556013
/**
* novalidate should prevent form validation, thus not blocking form submission.
*
* NOTE: if there is no invalidformsubmit observer, the form submission will
* never be blocked and this test might be a false-positive but that should not
* be a problem.
* NOTE: if the MozInvalidForm event doesn't get prevented default, the form
* submission will never be blocked and this test might be a false-positive but
* that should not be a problem. We will remove the check for MozInvalidForm
* event, see bug 587671.
*/
document.forms[0].addEventListener("submit", function(aEvent) {
ok(true, "novalidate has been correctly used for first form");

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

@ -41,65 +41,57 @@ function checkPseudoClass(aElement, aExpected)
"matches(':-moz-ui-invalid') should return " + aExpected + " for " + aElement);
}
var os = SpecialPowers.Cc['@mozilla.org/observer-service;1']
.getService(SpecialPowers.Ci.nsIObserverService);
var observers = os.enumerateObservers("invalidformsubmit");
var content = document.getElementById('content');
var textarea = document.getElementsByTagName('textarea')[0];
var input = document.getElementsByTagName('input')[0];
var select = document.getElementsByTagName('select')[0];
var button = document.getElementsByTagName('button')[0];
var form = document.forms[0];
if (observers.hasMoreElements()) {
var content = document.getElementById('content');
var textarea = document.getElementsByTagName('textarea')[0];
var input = document.getElementsByTagName('input')[0];
var select = document.getElementsByTagName('select')[0];
var button = document.getElementsByTagName('button')[0];
var form = document.forms[0];
checkPseudoClass(textarea, false);
checkPseudoClass(input, false);
checkPseudoClass(select, false);
checkPseudoClass(textarea, false);
checkPseudoClass(input, false);
checkPseudoClass(select, false);
// Try to submit.
button.click();
checkPseudoClass(textarea, true);
checkPseudoClass(input, true);
checkPseudoClass(select, true);
// Try to submit.
button.click();
checkPseudoClass(textarea, true);
checkPseudoClass(input, true);
checkPseudoClass(select, true);
// No longer in the form.
content.appendChild(textarea);
content.appendChild(input);
content.appendChild(select);
checkPseudoClass(textarea, false);
checkPseudoClass(input, false);
checkPseudoClass(select, false);
// No longer in the form.
content.appendChild(textarea);
content.appendChild(input);
content.appendChild(select);
checkPseudoClass(textarea, false);
checkPseudoClass(input, false);
checkPseudoClass(select, false);
// Back in the form.
form.appendChild(textarea);
form.appendChild(input);
form.appendChild(select);
checkPseudoClass(textarea, true);
checkPseudoClass(input, true);
checkPseudoClass(select, true);
// Back in the form.
form.appendChild(textarea);
form.appendChild(input);
form.appendChild(select);
checkPseudoClass(textarea, true);
checkPseudoClass(input, true);
checkPseudoClass(select, true);
/* Case when elements get orphaned. */
var textarea = document.getElementsByTagName('textarea')[1];
var input = document.getElementsByTagName('input')[1];
var select = document.getElementsByTagName('select')[1];
var button = document.getElementsByTagName('button')[1];
var form = document.forms[1];
/* Case when elements get orphaned. */
var textarea = document.getElementsByTagName('textarea')[1];
var input = document.getElementsByTagName('input')[1];
var select = document.getElementsByTagName('select')[1];
var button = document.getElementsByTagName('button')[1];
var form = document.forms[1];
// Try to submit.
button.click();
checkPseudoClass(textarea, true);
checkPseudoClass(input, true);
checkPseudoClass(select, true);
// Try to submit.
button.click();
checkPseudoClass(textarea, true);
checkPseudoClass(input, true);
checkPseudoClass(select, true);
// Remove the form.
document.getElementsByTagName('table')[0].removeChild(form);
checkPseudoClass(textarea, false);
checkPseudoClass(input, false);
checkPseudoClass(select, false);
} else {
todo(false, "No 'invalidformsubmit' observers. Skip test.");
}
// Remove the form.
document.getElementsByTagName('table')[0].removeChild(form);
checkPseudoClass(textarea, false);
checkPseudoClass(input, false);
checkPseudoClass(select, false);
</script>
</pre>

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

@ -47,65 +47,57 @@ function checkPseudoClass(aElement, aExpected)
"matches(':-moz-ui-valid') should return " + aExpected + " for " + aElement);
}
var os = SpecialPowers.Cc['@mozilla.org/observer-service;1']
.getService(SpecialPowers.Ci.nsIObserverService);
var observers = os.enumerateObservers("invalidformsubmit");
var content = document.getElementById('content');
var textarea = document.getElementsByTagName('textarea')[0];
var input = document.getElementsByTagName('input')[0];
var button = document.getElementsByTagName('button')[0];
var select = document.getElementsByTagName('select')[0];
var form = document.forms[0];
if (observers.hasMoreElements()) {
var content = document.getElementById('content');
var textarea = document.getElementsByTagName('textarea')[0];
var input = document.getElementsByTagName('input')[0];
var button = document.getElementsByTagName('button')[0];
var select = document.getElementsByTagName('select')[0];
var form = document.forms[0];
checkPseudoClass(textarea, false);
checkPseudoClass(input, false);
checkPseudoClass(select, false);
checkPseudoClass(textarea, false);
checkPseudoClass(input, false);
checkPseudoClass(select, false);
// Try to submit.
button.click();
checkPseudoClass(textarea, true);
checkPseudoClass(input, true);
checkPseudoClass(select, true);
// Try to submit.
button.click();
checkPseudoClass(textarea, true);
checkPseudoClass(input, true);
checkPseudoClass(select, true);
// No longer in the form.
content.appendChild(textarea);
content.appendChild(input);
content.appendChild(select);
checkPseudoClass(textarea, false);
checkPseudoClass(input, false);
checkPseudoClass(select, false);
// No longer in the form.
content.appendChild(textarea);
content.appendChild(input);
content.appendChild(select);
checkPseudoClass(textarea, false);
checkPseudoClass(input, false);
checkPseudoClass(select, false);
// Back in the form.
form.appendChild(textarea);
form.appendChild(input);
form.appendChild(select);
checkPseudoClass(textarea, true);
checkPseudoClass(input, true);
checkPseudoClass(select, true);
// Back in the form.
form.appendChild(textarea);
form.appendChild(input);
form.appendChild(select);
checkPseudoClass(textarea, true);
checkPseudoClass(input, true);
checkPseudoClass(select, true);
/* Case when elements get orphaned. */
var textarea = document.getElementsByTagName('textarea')[1];
var input = document.getElementsByTagName('input')[1];
var button = document.getElementsByTagName('button')[1];
var select = document.getElementsByTagName('select')[1];
var form = document.forms[1];
/* Case when elements get orphaned. */
var textarea = document.getElementsByTagName('textarea')[1];
var input = document.getElementsByTagName('input')[1];
var button = document.getElementsByTagName('button')[1];
var select = document.getElementsByTagName('select')[1];
var form = document.forms[1];
// Try to submit.
button.click();
checkPseudoClass(textarea, true);
checkPseudoClass(input, true);
checkPseudoClass(select, true);
// Try to submit.
button.click();
checkPseudoClass(textarea, true);
checkPseudoClass(input, true);
checkPseudoClass(select, true);
// Remove the form.
document.getElementsByTagName('table')[0].removeChild(form);
checkPseudoClass(textarea, false);
checkPseudoClass(input, false);
checkPseudoClass(select, false);
} else {
todo(false, "No 'invalidformsubmit' observers. Skip test.");
}
// Remove the form.
document.getElementsByTagName('table')[0].removeChild(form);
checkPseudoClass(textarea, false);
checkPseudoClass(input, false);
checkPseudoClass(select, false);
</script>
</pre>

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

@ -0,0 +1,25 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { GeckoViewActorChild } = ChromeUtils.import(
"resource://gre/modules/GeckoViewActorChild.jsm"
);
const EXPORTED_SYMBOLS = ["GeckoViewFormValidationChild"];
class GeckoViewFormValidationChild extends GeckoViewActorChild {
handleEvent(aEvent) {
switch (aEvent.type) {
case "MozInvalidForm": {
// Handle invalid form submission. If we don't hook up to this,
// invalid forms are allowed to be submitted!
aEvent.preventDefault();
// We should show the validation message, bug 1510450.
break;
}
}
}
}

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

@ -11,6 +11,7 @@ FINAL_TARGET_FILES.actors += [
"GeckoViewAutoFillChild.jsm",
"GeckoViewContentChild.jsm",
"GeckoViewContentParent.jsm",
"GeckoViewFormValidationChild.jsm",
"GeckoViewPromptChild.jsm",
"GeckoViewSettingsChild.jsm",
"LoadURIDelegateChild.jsm",

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

@ -38,6 +38,15 @@ const JSWINDOWACTORS = {
},
allFrames: true,
},
GeckoViewFormValidation: {
child: {
moduleURI: "resource:///actors/GeckoViewFormValidationChild.jsm",
events: {
MozInvalidForm: {},
},
},
allFrames: true,
},
};
class GeckoViewStartup {
@ -124,21 +133,6 @@ class GeckoViewStartup {
}
);
// Handle invalid form submission. If we don't hook up to this,
// invalid forms are allowed to be submitted!
Services.obs.addObserver(
{
QueryInterface: ChromeUtils.generateQI([
"nsIObserver",
"nsIFormSubmitObserver",
]),
notifyInvalidSubmit: (form, element) => {
// We should show the validation message here, bug 1510450.
},
},
"invalidformsubmit"
);
if (
Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_DEFAULT
) {