Bug 1877969 - Consume user activation when showing any picker r=dom-core,edgar

Consume transient user activation when showing file/color picker
at any time. Do not open if there is no transent user activation.

Spec PR: https://github.com/whatwg/html/pull/10344

Differential Revision: https://phabricator.services.mozilla.com/D201096
This commit is contained in:
Oliver Medhurst
2024-06-04 11:33:36 +00:00
parent de85b160be
commit 4bf748dbcb
2 changed files with 23 additions and 11 deletions

View File

@@ -677,16 +677,11 @@ nsColorPickerShownCallback::Done(const nsAString& aColor) {
NS_IMPL_ISUPPORTS(nsColorPickerShownCallback, nsIColorPickerShownCallback)
static bool IsPopupBlocked(Document* aDoc) {
static bool IsPickerBlocked(Document* aDoc) {
if (aDoc->ConsumeTransientUserGestureActivation()) {
return false;
}
WindowContext* wc = aDoc->GetWindowContext();
if (wc && wc->CanShowPopup()) {
return false;
}
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, "DOM"_ns, aDoc,
nsContentUtils::eDOM_PROPERTIES,
"InputPickerBlockedNoUserActivation");
@@ -735,7 +730,7 @@ nsresult HTMLInputElement::InitColorPicker() {
return NS_ERROR_FAILURE;
}
if (IsPopupBlocked(doc)) {
if (IsPickerBlocked(doc)) {
return NS_OK;
}
@@ -783,7 +778,7 @@ nsresult HTMLInputElement::InitFilePicker(FilePickerType aType) {
return NS_ERROR_FAILURE;
}
if (IsPopupBlocked(doc)) {
if (IsPickerBlocked(doc)) {
return NS_OK;
}
@@ -3577,7 +3572,7 @@ nsresult HTMLInputElement::MaybeInitPickers(EventChainPostVisitor& aVisitor) {
// open a color picker when we receive a click on a <input type='color'>.
// A click is handled if it's the left mouse button.
// We do not prevent non-trusted click because authors can already use
// .click(). However, the pickers will follow the rules of popup-blocking.
// .click(). However, the pickers will check and consume user activation.
WidgetMouseEvent* mouseEvent = aVisitor.mEvent->AsMouseEvent();
if (!(mouseEvent && mouseEvent->IsLeftClickEvent())) {
return NS_OK;
@@ -5845,7 +5840,11 @@ void HTMLInputElement::ShowPicker(ErrorResult& aRv) {
// Step 2. If element is not mutable, then return.
// (See above.)
// Step 3. If element's type attribute is in the File Upload state, then run
// Step 3. Consume user activation given element's relevant global object.
// InitFilePicker() and InitColorPicker() consume it themselves,
// so only consume in this function if not those.
// Step 4. If element's type attribute is in the File Upload state, then run
// these steps in parallel:
if (mType == FormControlType::InputFile) {
FilePickerType type = FILE_PICKER_FILE;
@@ -5857,7 +5856,7 @@ void HTMLInputElement::ShowPicker(ErrorResult& aRv) {
return;
}
// Step 4. Otherwise, the user agent should show any relevant user interface
// Step 5. Otherwise, the user agent should show any relevant user interface
// for selecting a value for element, in the way it normally would when the
// user interacts with the control
if (mType == FormControlType::InputColor) {
@@ -5865,6 +5864,9 @@ void HTMLInputElement::ShowPicker(ErrorResult& aRv) {
return;
}
// See Step 3.
OwnerDoc()->ConsumeTransientUserGestureActivation();
if (!IsInComposedDoc()) {
return;
}