Skip to content

Commit

Permalink
Update MediaPositionState duration to unrestricted double
Browse files Browse the repository at this point in the history
Update for the spec change w3c/mediasession#304

Bug: 324153759
Change-Id: I006b079954cdb5818e97200b65e6ea42333ad6fb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5274534
Reviewed-by: Tommy Steimel <[email protected]>
Commit-Queue: Yiren Wang <[email protected]>
Reviewed-by: Matthew Denton <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1257586}
  • Loading branch information
Yiren Wang authored and Chromium LUCI CQ committed Feb 7, 2024
1 parent 836f765 commit a638eaf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// https://w3c.github.io/mediasession/#the-mediapositionstate-dictionary

dictionary MediaPositionState {
double duration;
unrestricted double duration;
double playbackRate;
double position;
};
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ void MediaSession::setPositionState(MediaPositionState* position_state,
return;
}

// The duration cannot be NaN.
if (std::isnan(position_state->duration())) {
exception_state.ThrowTypeError("The provided duration cannot be NaN.");
return;
}

// The duration cannot be negative.
if (position_state->duration() < 0) {
exception_state.ThrowTypeError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ class MediaSessionTest : public PageTestBase {
media_session_->setPositionState(position_state, exception_state);
}

void SetPositionStateThrowsException(double duration,
double position,
double playback_rate) {
auto* position_state = MediaPositionState::Create();
position_state->setDuration(duration);
position_state->setPosition(position);
position_state->setPlaybackRate(playback_rate);

DummyExceptionStateForTesting exception_state;
media_session_->setPositionState(position_state, exception_state);
EXPECT_TRUE(exception_state.HadException());
EXPECT_EQ(ESErrorType::kTypeError, exception_state.CodeAs<ESErrorType>());
}

void ClearPositionState() {
NonThrowableExceptionState exception_state;
media_session_->setPositionState(MediaPositionState::Create(),
Expand Down Expand Up @@ -159,6 +173,28 @@ TEST_F(MediaSessionTest, PlaybackPositionState_Playing) {
loop.Run();
}

TEST_F(MediaSessionTest, PlaybackPositionState_InfiniteDuration) {
base::RunLoop loop;
EXPECT_CALL(service(), SetPositionState(_))
.WillOnce(testing::Invoke([&](auto position_state) {
EXPECT_EQ(base::TimeDelta::Max(), position_state->duration);
EXPECT_EQ(base::Seconds(5), position_state->position);
EXPECT_EQ(1.0, position_state->playback_rate);
EXPECT_EQ(clock().NowTicks(), position_state->last_updated_time);

loop.Quit();
}));

SetPlaybackState("none");
SetPositionState(std::numeric_limits<double>::infinity(), 5, 1.0);
loop.Run();
}

TEST_F(MediaSessionTest, PlaybackPositionState_NaNDuration) {
SetPlaybackState("none");
SetPositionStateThrowsException(std::nan("10"), 5, 1.0);
}

TEST_F(MediaSessionTest, PlaybackPositionState_Paused_Clear) {
{
base::RunLoop loop;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ media_session::mojom::blink::MediaPositionPtr TypeConverter<
position) {
return media_session::mojom::blink::MediaPosition::New(
position->hasPlaybackRate() ? position->playbackRate() : 1.0,
base::Seconds(position->duration()),
position->duration() == std::numeric_limits<double>::infinity()
? base::TimeDelta::Max()
: base::Seconds(position->duration()),
position->hasPosition() ? base::Seconds(position->position())
: base::TimeDelta(),
base::TimeTicks::Now());
Expand Down

0 comments on commit a638eaf

Please sign in to comment.