Chromium Code Reviews| 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..bfbbe64f21512df262e6f558dd0309acd615ba84 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 |
| @@ -12,6 +12,7 @@ import android.text.TextUtils; |
| 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,13 +20,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 java.util.HashMap; |
| + |
| +import javax.annotation.Nullable; |
| /** |
| * A tab helper responsible for enabling/disabling media controls and passing |
| @@ -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 HashMap<Tab, MediaSessionTabHelper> sTabToHelperMap = |
|
whywhat
2016/10/20 22:56:53
having an extra method used just for testing is fi
Zhiqiang Zhang (Slow)
2016/10/21 13:43:52
Ahh, using SparseArray<> now, with TabId as key.
|
| + new HashMap<Tab, MediaSessionTabHelper>(); |
| + |
| + @VisibleForTesting |
| + @Nullable |
| + static MediaSessionTabHelper getHelperFromTabForTesting(Tab tab) { |
| + return sTabToHelperMap.get(tab); |
| + } |
| + |
| + @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); |
|
whywhat
2016/10/20 22:56:53
nit: why "add" can WC have more than one delegate?
Zhiqiang Zhang (Slow)
2016/10/21 13:43:52
Currently not. Using "set" might make content::Med
|
| + return delegate; |
| } |
| private void setWebContents(WebContents webContents) { |
| @@ -157,13 +174,17 @@ public class MediaSessionTabHelper implements MediaImageCallback { |
| cleanupWebContents(); |
| mWebContents = webContents; |
| - if (mWebContents != null) mWebContentsObserver = createWebContentsObserver(mWebContents); |
| + if (mWebContents != null) { |
| + if (mMediaSessionDelegate != null) mMediaSessionDelegate.disconnectMediaSession(); |
|
whywhat
2016/10/20 22:56:53
this will always be null after cleanupWebContents.
Zhiqiang Zhang (Slow)
2016/10/21 13:43:52
Done.
|
| + |
| + 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 +248,7 @@ public class MediaSessionTabHelper implements MediaImageCallback { |
| hideNotification(); |
| mTab.removeObserver(this); |
| mTab = null; |
| + sTabToHelperMap.remove(tab); |
| } |
| }; |
| @@ -241,6 +263,7 @@ public class MediaSessionTabHelper implements MediaImageCallback { |
| if (activity != null) { |
| mPreviousVolumeControlStream = activity.getVolumeControlStream(); |
| } |
| + sTabToHelperMap.put(tab, this); |
| } |
| /** |