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 34bcee0dfe135c12fe5fd14100820d5d7e564ceb..384ac7b04bf8da4e5361e48de4691316e254c82b 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,14 +20,17 @@ 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.MediaSession; |
| +import org.chromium.content_public.browser.MediaSessionObserver; |
| 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} |
| @@ -42,8 +46,7 @@ public class MediaSessionTabHelper implements MediaImageCallback { |
| private Bitmap mFavicon = null; |
| private Bitmap mCurrentMediaImage = null; |
| private String mOrigin = null; |
| - private WebContents mWebContents; |
| - private WebContentsObserver mWebContentsObserver; |
| + private MediaSessionObserver mMediaSessionObserver; |
| 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,13 +57,19 @@ public class MediaSessionTabHelper implements MediaImageCallback { |
| private MediaMetadata mCurrentMetadata = null; |
| private MediaImageManager mMediaImageManager = null; |
| + @VisibleForTesting |
| + @Nullable |
| + MediaSessionObserver getMediaSessionObserverForTesting() { |
| + return mMediaSessionObserver; |
| + } |
| + |
| private MediaNotificationListener mControlsListener = new MediaNotificationListener() { |
| @Override |
| public void onPlay(int actionSource) { |
| MediaSessionUMA |
| .recordPlay(MediaSessionTabHelper.convertMediaActionSourceToUMA(actionSource)); |
| - mWebContents.resumeMediaSession(); |
| + if (mMediaSessionObserver != null) mMediaSessionObserver.getMediaSession().resume(); |
| } |
| @Override |
| @@ -68,7 +77,7 @@ public class MediaSessionTabHelper implements MediaImageCallback { |
| MediaSessionUMA.recordPause( |
| MediaSessionTabHelper.convertMediaActionSourceToUMA(actionSource)); |
| - mWebContents.suspendMediaSession(); |
| + if (mMediaSessionObserver != null) mMediaSessionObserver.getMediaSession().suspend(); |
| } |
| @Override |
| @@ -76,7 +85,7 @@ public class MediaSessionTabHelper implements MediaImageCallback { |
| MediaSessionUMA |
| .recordStop(MediaSessionTabHelper.convertMediaActionSourceToUMA(actionSource)); |
| - mWebContents.stopMediaSession(); |
| + if (mMediaSessionObserver != null) mMediaSessionObserver.getMediaSession().stop(); |
| } |
| }; |
| @@ -92,12 +101,11 @@ public class MediaSessionTabHelper implements MediaImageCallback { |
| mNotificationInfoBuilder = null; |
| } |
| - private WebContentsObserver createWebContentsObserver(WebContents webContents) { |
| - return new WebContentsObserver(webContents) { |
| + private MediaSessionObserver createMediaSessionObserver(MediaSession mediaSession) { |
| + return new MediaSessionObserver(mediaSession) { |
| @Override |
| - public void destroy() { |
| + public void mediaSessionDestroyed() { |
| hideNotification(); |
| - super.destroy(); |
| } |
| @Override |
| @@ -154,18 +162,24 @@ public class MediaSessionTabHelper implements MediaImageCallback { |
| } |
| private void setWebContents(WebContents webContents) { |
| - if (mWebContents == webContents) return; |
| + MediaSession mediaSession = MediaSession.fromWebContents(webContents); |
| + if (mMediaSessionObserver != null |
| + && mediaSession == mMediaSessionObserver.getMediaSession()) { |
| + return; |
| + } |
| - cleanupWebContents(); |
| - mWebContents = webContents; |
| - if (mWebContents != null) mWebContentsObserver = createWebContentsObserver(mWebContents); |
| - mMediaImageManager.setWebContents(mWebContents); |
| + cleanupMediaSessionObserver(); |
| + if (webContents != null) { |
| + mMediaSessionObserver = createMediaSessionObserver(mediaSession); |
|
mlamouri (slow - plz ping)
2016/10/28 10:24:50
Why not checking for `mediaSession != null`?
Zhiqiang Zhang (Slow)
2016/10/28 11:18:47
Done.
|
| + } |
| + mMediaImageManager.setWebContents(webContents); |
| } |
| - private void cleanupWebContents() { |
| - if (mWebContentsObserver != null) mWebContentsObserver.destroy(); |
| - mWebContentsObserver = null; |
| - mWebContents = null; |
| + private void cleanupMediaSessionObserver() { |
| + if (mMediaSessionObserver != null) { |
| + mMediaSessionObserver.getMediaSession().removeObserver(mMediaSessionObserver); |
| + } |
| + mMediaSessionObserver = null; |
|
mlamouri (slow - plz ping)
2016/10/28 10:24:50
style:
```
if (mMediaSessionObserver == null) retu
Zhiqiang Zhang (Slow)
2016/10/28 11:18:47
Done.
|
| } |
| private final TabObserver mTabObserver = new EmptyTabObserver() { |
| @@ -223,7 +237,7 @@ public class MediaSessionTabHelper implements MediaImageCallback { |
| public void onDestroyed(Tab tab) { |
| assert mTab == tab; |
| - cleanupWebContents(); |
| + cleanupMediaSessionObserver(); |
| hideNotification(); |
| mTab.removeObserver(this); |