Skip to content

Commit

Permalink
[Chromium] Add null checks for MediaSessionDelegate access
Browse files Browse the repository at this point in the history
The first thing the session does when closing a session is cleaning
up the session listeners. This involves, among other things, setting
all the delegates to null.

It's still possible to get media events from the web engine after we
proceed to close the session (like inactive notifications) so we must
ensure that we don't run any job in that case and ignore the notification.

We went the extra mile and we've also added null checks for a few
more insecure usages of getMediaSessionDelegate().

Fixes #915
  • Loading branch information
svillar committed Sep 5, 2023
1 parent a23bf90 commit 118ba3f
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,30 @@ public void mediaSessionDestroyed() {
@Override
public void mediaSessionStateChanged(boolean isControllable, boolean isSuspended, boolean isActive) {
assert mMediaSession != null;

// If the Session is being closed just ignore the change notification.
WMediaSession.Delegate delegate = mSession.getMediaSessionDelegate();
if (delegate == null)
return;

if (isActive != mIsActive) {
mIsActive = isActive;
if (mIsActive) {
mSession.getMediaSessionDelegate().onActivated(mSession, mMediaSession);
delegate.onActivated(mSession, mMediaSession);
} else {
stopUpdatingPosition();
mSession.getMediaSessionDelegate().onStop(mSession, mMediaSession);
mSession.getMediaSessionDelegate().onDeactivated(mSession, mMediaSession);
delegate.onStop(mSession, mMediaSession);
delegate.onDeactivated(mSession, mMediaSession);
}
}
if (isSuspended != mIsSuspended) {
mIsSuspended = isSuspended;
if (!mIsSuspended) {
startUpdatingPosition();
mSession.getMediaSessionDelegate().onPlay(mSession, mMediaSession);
delegate.onPlay(mSession, mMediaSession);
} else {
stopUpdatingPosition();
mSession.getMediaSessionDelegate().onPause(mSession, mMediaSession);
delegate.onPause(mSession, mMediaSession);
}
}
}
Expand Down Expand Up @@ -116,8 +122,9 @@ public void mediaSessionPositionChanged(@Nullable MediaPosition position) {

public void onMediaFullscreen(boolean isFullscreen) {
assert mMediaSession != null;
mSession.getMediaSessionDelegate().onFullscreen(
mSession, mMediaSession, isFullscreen, null);
if (mSession.getMediaSessionDelegate() != null)
mSession.getMediaSessionDelegate().onFullscreen(
mSession, mMediaSession, isFullscreen, null);
}

/* package */ class WMediaSessionImpl implements WMediaSession {
Expand Down Expand Up @@ -205,7 +212,7 @@ public void muteAudio(boolean mute) {
}

private void updatePosition() {
if (mMediaPosition == null || !mIsActive)
if (mMediaPosition == null || !mIsActive || mSession.getMediaSessionDelegate() == null)
return;

assert mMediaSession != null;
Expand Down Expand Up @@ -267,6 +274,8 @@ private Bitmap scaleBitmap(Bitmap image, int size) {

private void updateMetaData() {
assert mMediaSession != null;
if (mSession.getMediaSessionDelegate() == null)
return;

WMediaSession.Metadata metadata;
if (mMetadata != null) {
Expand Down

0 comments on commit 118ba3f

Please sign in to comment.