| Index: chrome/android/java/src/org/chromium/chrome/browser/media/ui/MediaSessionTabHelper.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/media/ui/MediaSessionTabHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/media/ui/MediaSessionTabHelper.java
|
| index 19252e1106b262e2fa3f854d5bc5f0e043cc2386..f4e5bebec0eb533444b876cb5d5d52d2687b8e93 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/media/ui/MediaSessionTabHelper.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/media/ui/MediaSessionTabHelper.java
|
| @@ -9,9 +9,11 @@ import android.content.Intent;
|
| import android.graphics.Bitmap;
|
| import android.media.AudioManager;
|
| import android.text.TextUtils;
|
| +import android.util.SparseArray;
|
|
|
| import org.chromium.base.ContextUtils;
|
| import org.chromium.base.Log;
|
| +import org.chromium.base.VisibleForTesting;
|
| import org.chromium.chrome.R;
|
| import org.chromium.chrome.browser.metrics.MediaNotificationUma;
|
| import org.chromium.chrome.browser.metrics.MediaSessionUMA;
|
| @@ -19,14 +21,16 @@ import org.chromium.chrome.browser.tab.EmptyTabObserver;
|
| import org.chromium.chrome.browser.tab.Tab;
|
| import org.chromium.chrome.browser.tab.TabObserver;
|
| import org.chromium.components.url_formatter.UrlFormatter;
|
| +import org.chromium.content_public.browser.MediaSessionDelegate;
|
| import org.chromium.content_public.browser.WebContents;
|
| -import org.chromium.content_public.browser.WebContentsObserver;
|
| import org.chromium.content_public.common.MediaMetadata;
|
| import org.chromium.ui.base.WindowAndroid;
|
|
|
| import java.net.URI;
|
| import java.net.URISyntaxException;
|
|
|
| +import javax.annotation.Nullable;
|
| +
|
| /**
|
| * A tab helper responsible for enabling/disabling media controls and passing
|
| * media actions from the controls to the {@link org.chromium.content.browser.MediaSession}
|
| @@ -43,7 +47,7 @@ public class MediaSessionTabHelper implements MediaImageCallback {
|
| private Bitmap mCurrentMediaImage = null;
|
| private String mOrigin = null;
|
| private WebContents mWebContents;
|
| - private WebContentsObserver mWebContentsObserver;
|
| + private MediaSessionDelegate mMediaSessionDelegate;
|
| private int mPreviousVolumeControlStream = AudioManager.USE_DEFAULT_STREAM_TYPE;
|
| private MediaNotificationInfo.Builder mNotificationInfoBuilder = null;
|
| // The fallback title if |mPageMetadata| is null or its title is empty.
|
| @@ -54,29 +58,41 @@ public class MediaSessionTabHelper implements MediaImageCallback {
|
| private MediaMetadata mCurrentMetadata = null;
|
| private MediaImageManager mMediaImageManager = null;
|
|
|
| + private static SparseArray<MediaSessionTabHelper> sTabToHelperMap =
|
| + new SparseArray<MediaSessionTabHelper>();
|
| +
|
| + @VisibleForTesting
|
| + @Nullable
|
| + static MediaSessionTabHelper getHelperFromTabForTesting(Tab tab) {
|
| + return sTabToHelperMap.get(tab.getId());
|
| + }
|
| +
|
| + @VisibleForTesting
|
| + @Nullable
|
| + MediaSessionDelegate getMediaSessionDelegateForTesting() {
|
| + return mMediaSessionDelegate;
|
| + }
|
| +
|
| private MediaNotificationListener mControlsListener = new MediaNotificationListener() {
|
| @Override
|
| public void onPlay(int actionSource) {
|
| MediaSessionUMA
|
| .recordPlay(MediaSessionTabHelper.convertMediaActionSourceToUMA(actionSource));
|
| -
|
| - mWebContents.resumeMediaSession();
|
| + if (mMediaSessionDelegate != null) mMediaSessionDelegate.resumeMediaSession();
|
| }
|
|
|
| @Override
|
| public void onPause(int actionSource) {
|
| MediaSessionUMA.recordPause(
|
| MediaSessionTabHelper.convertMediaActionSourceToUMA(actionSource));
|
| -
|
| - mWebContents.suspendMediaSession();
|
| + if (mMediaSessionDelegate != null) mMediaSessionDelegate.suspendMediaSession();
|
| }
|
|
|
| @Override
|
| public void onStop(int actionSource) {
|
| MediaSessionUMA
|
| .recordStop(MediaSessionTabHelper.convertMediaActionSourceToUMA(actionSource));
|
| -
|
| - mWebContents.stopMediaSession();
|
| + if (mMediaSessionDelegate != null) mMediaSessionDelegate.stopMediaSession();
|
| }
|
| };
|
|
|
| @@ -92,12 +108,11 @@ public class MediaSessionTabHelper implements MediaImageCallback {
|
| mNotificationInfoBuilder = null;
|
| }
|
|
|
| - private WebContentsObserver createWebContentsObserver(WebContents webContents) {
|
| - return new WebContentsObserver(webContents) {
|
| + private MediaSessionDelegate createMediaSessionDelegate(WebContents webContents) {
|
| + MediaSessionDelegate delegate = new MediaSessionDelegate() {
|
| @Override
|
| - public void destroy() {
|
| + public void mediaSessionDisconnected() {
|
| hideNotification();
|
| - super.destroy();
|
| }
|
|
|
| @Override
|
| @@ -150,6 +165,8 @@ public class MediaSessionTabHelper implements MediaImageCallback {
|
| updateNotificationMetadata();
|
| }
|
| };
|
| + webContents.addMediaSessionDelegate(delegate);
|
| + return delegate;
|
| }
|
|
|
| private void setWebContents(WebContents webContents) {
|
| @@ -157,13 +174,15 @@ public class MediaSessionTabHelper implements MediaImageCallback {
|
|
|
| cleanupWebContents();
|
| mWebContents = webContents;
|
| - if (mWebContents != null) mWebContentsObserver = createWebContentsObserver(mWebContents);
|
| + if (mWebContents != null) {
|
| + mMediaSessionDelegate = createMediaSessionDelegate(mWebContents);
|
| + }
|
| mMediaImageManager.setWebContents(mWebContents);
|
| }
|
|
|
| private void cleanupWebContents() {
|
| - if (mWebContentsObserver != null) mWebContentsObserver.destroy();
|
| - mWebContentsObserver = null;
|
| + if (mMediaSessionDelegate != null) mMediaSessionDelegate.disconnectMediaSession();
|
| + mMediaSessionDelegate = null;
|
| mWebContents = null;
|
| }
|
|
|
| @@ -227,6 +246,7 @@ public class MediaSessionTabHelper implements MediaImageCallback {
|
| hideNotification();
|
| mTab.removeObserver(this);
|
| mTab = null;
|
| + sTabToHelperMap.remove(tab.getId());
|
| }
|
| };
|
|
|
| @@ -241,6 +261,7 @@ public class MediaSessionTabHelper implements MediaImageCallback {
|
| if (activity != null) {
|
| mPreviousVolumeControlStream = activity.getVolumeControlStream();
|
| }
|
| + sTabToHelperMap.put(tab.getId(), this);
|
| }
|
|
|
| /**
|
|
|