Skip to content

Commit

Permalink
Bug 1478208 - Implement HTMLMediaElement.allowedToPlay. r=alwu,bz
Browse files Browse the repository at this point in the history
Various web authors have expressed desire to know in advance whether autoplay
will work.

They want this in order to avoid paying the price for downloading media that
won't play. Or they want to take other action such as showing a poster image
instead.

This is of particular interest to Firefox, as we're planning on showing a
prompt to ask the user whether they would like a site to play. If sites want to
determine whether they can autoplay but avoid the prompt showing, they won't be
able to just call play() in Firefox and see whether it works, as that would
likely show the prompt if the user doesn't already have a stored permission.

We've been working out a spec here:
whatwg/html#3617 (comment)

This implements what is the consensus to date there;
HTMLMediaElement.allowedToPlay, which returns true when a play() call would not
be blocked with NotAllowedError by autoplay blocking policies.

MozReview-Commit-ID: AkBu0G7uCJ0

UltraBlame original commit: f5f55e363d318effdfd504061c50f87e06301c6f
  • Loading branch information
marco-c committed Oct 3, 2019
1 parent d100cec commit 391fa7f
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 0 deletions.
26 changes: 26 additions & 0 deletions dom/html/HTMLMediaElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10441,6 +10441,32 @@ LoadFromSourceChildren
}
}
}
bool
HTMLMediaElement
:
:
AllowedToPlay
(
)
const
{
return
AutoplayPolicy
:
:
IsAllowedToPlay
(
*
this
)
=
=
nsIAutoplay
:
:
ALLOWED
;
}
void
HTMLMediaElement
:
Expand Down
6 changes: 6 additions & 0 deletions dom/html/HTMLMediaElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -1730,6 +1730,12 @@ mIsCasting
aShow
;
}
bool
AllowedToPlay
(
)
const
;
already_AddRefed
<
MediaSource
Expand Down
9 changes: 9 additions & 0 deletions dom/media/test/AutoplayTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ appendChild
element
)
;
let
allowedToPlay
=
element
.
allowedToPlay
;
element
.
play
Expand All @@ -79,6 +86,7 @@ postMessage
played
:
true
allowedToPlay
}
"
*
Expand All @@ -99,6 +107,7 @@ postMessage
played
:
false
allowedToPlay
}
"
*
Expand Down
27 changes: 27 additions & 0 deletions dom/media/test/test_autoplay_policy_activation.html
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,37 @@
.
data
.
allowedToPlay
test_case
.
should_play
"
allowed
-
"
+
test_case
.
name
)
;
SimpleTest
.
is
(
result
.
data
.
played
test_case
.
should_play
"
played
-
"
+
test_case
.
name
Expand Down
25 changes: 25 additions & 0 deletions dom/media/test/test_autoplay_policy_permission.html
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,35 @@
.
data
.
allowedToPlay
testCase
.
shouldPlay
"
allowedToPlay
-
"
+
testCase
.
message
)
;
is
(
result
.
data
.
played
testCase
.
shouldPlay
"
played
-
"
+
testCase
.
message
Expand Down
66 changes: 66 additions & 0 deletions dom/webidl/HTMLMediaElement.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -1483,3 +1483,69 @@ hasSuspendTaint
;
}
;
/
*
*
API
that
exposes
whether
a
call
to
HTMLMediaElement
.
play
(
)
would
be
*
blocked
by
autoplay
policies
;
whether
the
promise
returned
by
play
(
)
*
would
be
rejected
with
NotAllowedError
.
*
/
partial
interface
HTMLMediaElement
{
[
Pref
=
"
media
.
allowed
-
to
-
play
.
enabled
"
]
readonly
attribute
boolean
allowedToPlay
;
}
;
39 changes: 39 additions & 0 deletions modules/libpref/init/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -5259,6 +5259,45 @@ false
;
#
endif
#
ifdef
NIGHTLY_BUILD
pref
(
"
media
.
allowed
-
to
-
play
.
enabled
"
true
)
;
#
else
pref
(
"
media
.
allowed
-
to
-
play
.
enabled
"
false
)
;
#
endif
pref
(
"
Expand Down
16 changes: 16 additions & 0 deletions testing/profiles/common/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,19 @@ webaudio
false
)
;
user_pref
(
"
media
.
allowed
-
to
-
play
.
enabled
"
true
)
;

0 comments on commit 391fa7f

Please sign in to comment.