Bug 929890 - Canonicalise media preload attribute value of "" to "auto" r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D249523
This commit is contained in:
Keith Cirkel
2025-05-16 09:27:39 +00:00
committed by mozilla@keithcirkel.co.uk
parent e9b5391a5b
commit 9b9b7194e1
6 changed files with 33 additions and 110 deletions

View File

@@ -3129,20 +3129,20 @@ bool HTMLMediaElement::AllowedToPlay() const {
uint32_t HTMLMediaElement::GetPreloadDefault() const {
if (mMediaSource) {
return HTMLMediaElement::PRELOAD_ATTR_METADATA;
return HTMLMediaElement::PRELOAD_METADATA;
}
if (OnCellularConnection()) {
return Preferences::GetInt("media.preload.default.cellular",
HTMLMediaElement::PRELOAD_ATTR_NONE);
HTMLMediaElement::PRELOAD_NONE);
}
return Preferences::GetInt("media.preload.default",
HTMLMediaElement::PRELOAD_ATTR_METADATA);
HTMLMediaElement::PRELOAD_METADATA);
}
uint32_t HTMLMediaElement::GetPreloadDefaultAuto() const {
if (OnCellularConnection()) {
return Preferences::GetInt("media.preload.auto.cellular",
HTMLMediaElement::PRELOAD_ATTR_METADATA);
HTMLMediaElement::PRELOAD_METADATA);
}
return Preferences::GetInt("media.preload.auto",
HTMLMediaElement::PRELOAD_ENOUGH);
@@ -3167,14 +3167,13 @@ void HTMLMediaElement::UpdatePreloadAction() {
// media.preload.default pref, or just preload metadata if not present.
nextAction = static_cast<PreloadAction>(preloadDefault);
} else if (val->Type() == nsAttrValue::eEnum) {
PreloadAttrValue attr =
static_cast<PreloadAttrValue>(val->GetEnumValue());
if (attr == HTMLMediaElement::PRELOAD_ATTR_EMPTY ||
attr == HTMLMediaElement::PRELOAD_ATTR_AUTO) {
MediaPreloadAttrValue attr =
static_cast<MediaPreloadAttrValue>(val->GetEnumValue());
if (attr == MediaPreloadAttrValue::PRELOAD_ATTR_AUTO) {
nextAction = static_cast<PreloadAction>(preloadAuto);
} else if (attr == HTMLMediaElement::PRELOAD_ATTR_METADATA) {
} else if (attr == MediaPreloadAttrValue::PRELOAD_ATTR_METADATA) {
nextAction = HTMLMediaElement::PRELOAD_METADATA;
} else if (attr == HTMLMediaElement::PRELOAD_ATTR_NONE) {
} else if (attr == MediaPreloadAttrValue::PRELOAD_ATTR_NONE) {
nextAction = HTMLMediaElement::PRELOAD_NONE;
}
} else {
@@ -4946,21 +4945,16 @@ bool HTMLMediaElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
const nsAString& aValue,
nsIPrincipal* aMaybeScriptedPrincipal,
nsAttrValue& aResult) {
// Mappings from 'preload' attribute strings to an enumeration.
static const nsAttrValue::EnumTableEntry kPreloadTable[] = {
{"", HTMLMediaElement::PRELOAD_ATTR_EMPTY},
{"none", HTMLMediaElement::PRELOAD_ATTR_NONE},
{"metadata", HTMLMediaElement::PRELOAD_ATTR_METADATA},
{"auto", HTMLMediaElement::PRELOAD_ATTR_AUTO},
};
if (aNamespaceID == kNameSpaceID_None) {
if (aAttribute == nsGkAtoms::crossorigin) {
ParseCORSValue(aValue, aResult);
return true;
}
if (aAttribute == nsGkAtoms::preload) {
return aResult.ParseEnumValue(aValue, kPreloadTable, false);
return aResult.ParseEnumValue(aValue, kPreloadTable, false,
// The default value is "auto" if aValue is
// not a recognised value.
kPreloadDefaultType);
}
}

View File

@@ -110,6 +110,25 @@ enum class StreamCaptureBehavior : uint8_t {
FINISH_WHEN_ENDED
};
/**
* Possible values of the 'preload' attribute.
*/
enum MediaPreloadAttrValue : uint8_t {
PRELOAD_ATTR_NONE, // set to "none"
PRELOAD_ATTR_METADATA, // set to "metadata"
PRELOAD_ATTR_AUTO // set to "auto"
};
// Mappings from 'preload' attribute strings to an enumeration.
static const nsAttrValue::EnumTableEntry kPreloadTable[] = {
{"none", MediaPreloadAttrValue::PRELOAD_ATTR_NONE},
{"metadata", MediaPreloadAttrValue::PRELOAD_ATTR_METADATA},
{"auto", MediaPreloadAttrValue::PRELOAD_ATTR_AUTO},
};
static constexpr const nsAttrValue::EnumTableEntry* kPreloadDefaultType =
&kPreloadTable[std::size(kPreloadTable) - 1];
class HTMLMediaElement : public nsGenericHTMLElement,
public MediaDecoderOwner,
public PrincipalChangeObserver<MediaStreamTrack>,
@@ -521,7 +540,7 @@ class HTMLMediaElement : public nsGenericHTMLElement,
nsGkAtoms::none->ToString(aValue);
return;
}
GetEnumAttr(nsGkAtoms::preload, nullptr, aValue);
GetEnumAttr(nsGkAtoms::preload, kPreloadDefaultType->tag, aValue);
}
void SetPreload(const nsAString& aValue, ErrorResult& aRv) {
if (mSrcAttrStream) {
@@ -1165,16 +1184,6 @@ class HTMLMediaElement : public nsGenericHTMLElement,
*/
void NotifyShutdownEvent();
/**
* Possible values of the 'preload' attribute.
*/
enum PreloadAttrValue : uint8_t {
PRELOAD_ATTR_EMPTY, // set to ""
PRELOAD_ATTR_NONE, // set to "none"
PRELOAD_ATTR_METADATA, // set to "metadata"
PRELOAD_ATTR_AUTO // set to "auto"
};
/**
* The preloading action to perform. These dictate how we react to the
* preload attribute. See mPreloadAction.

View File

@@ -660,8 +660,6 @@ skip-if = [
["test_bug666200.html"]
["test_bug666666.html"]
["test_bug669012.html"]
["test_bug674558.html"]

View File

@@ -1,32 +0,0 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=666666
-->
<head>
<title>Test for Bug 666666</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="reflect.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=666666">Mozilla Bug 666666</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 666666 **/
["audio", "video"].forEach(function(element) {
reflectLimitedEnumerated({
element: document.createElement(element),
attribute: "preload",
validValues: ["none", "metadata", "auto"],
invalidValues: ["cheesecake", ""]
});
});
</script>
</pre>
</body>
</html>

View File

@@ -957,8 +957,6 @@ skip-if = ["appname == 'seamonkey'"] # Seamonkey: Bug 598252, bug 1307337, bug
["test_preload_actions.html"]
["test_preload_attribute.html"]
["test_preload_suspend.html"]
["test_preserve_playbackrate_after_ui_play.html"]

View File

@@ -1,44 +0,0 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=479863
-->
<head>
<title>Test for Bug 479863</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=479863">Mozilla Bug 479863</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<video id='v1'></video><audio id='a1'></audio>
<video id='v2' preload="auto"></video><audio id='a2' preload="auto"></audio>
<pre id="test">
<script type="application/javascript">
var v1 = document.getElementById('v1');
var a1 = document.getElementById('a1');
var v2 = document.getElementById('v2');
var a2 = document.getElementById('a2');
is(v1.getAttribute("preload"), null, "video preload via getAttribute should be null by default");
is(a1.getAttribute("preload"), null, "video preload via getAttribute should be null by default");
is(v1.preload, "", "v1.preload should be empty by default");
is(a1.preload, "", "a1.preload should be empty by default");
is(v2.preload, "auto", "v2.preload should be auto");
is(a2.preload, "auto", "a2.preload should be auto");
v1.preload = "auto";
a1.preload = "auto";
is(v1.preload, "auto", "video.preload should be auto");
is(a1.preload, "auto", "audio.preload should be auto");
is(v1.getAttribute("preload"), "auto", "video preload attribute should be auto");
is(a1.getAttribute("preload"), "auto", "video preload attribute should be auto");
</script>
</pre>
</body>
</html>