Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.media.ui; | |
| 6 | |
| 7 import org.chromium.base.ApplicationStatus; | |
| 8 import org.chromium.base.Log; | |
| 9 import org.chromium.chrome.browser.EmptyTabObserver; | |
| 10 import org.chromium.chrome.browser.Tab; | |
| 11 import org.chromium.chrome.browser.TabObserver; | |
| 12 import org.chromium.chrome.browser.UrlUtilities; | |
| 13 import org.chromium.content_public.browser.WebContents; | |
| 14 import org.chromium.content_public.browser.WebContentsObserver; | |
| 15 | |
| 16 import java.net.URI; | |
| 17 import java.net.URISyntaxException; | |
| 18 | |
| 19 /** | |
| 20 * A tab helper responsible for enabling/disabling media controls and passing | |
| 21 * media actions from the controls to the {@link org.chromium.content.browser.Me diaSession} | |
| 22 */ | |
| 23 public class MediaSessionTabHelper { | |
| 24 private static final String TAG = "cr.MediaSession"; | |
| 25 | |
| 26 private Tab mTab; | |
| 27 private WebContents mWebContents; | |
| 28 private WebContentsObserver mWebContentsObserver; | |
| 29 | |
| 30 private MediaPlaybackListener mControlsListener = new MediaPlaybackListener( ) { | |
| 31 @Override | |
| 32 public void onPlay() { | |
| 33 assert mWebContents != null; | |
| 34 mWebContents.resumeMediaSession(); | |
| 35 } | |
| 36 | |
| 37 @Override | |
| 38 public void onPause() { | |
| 39 assert mWebContents != null; | |
| 40 mWebContents.suspendMediaSession(); | |
| 41 } | |
| 42 }; | |
| 43 | |
| 44 private WebContentsObserver createWebContentsObserver(WebContents webContent s) { | |
| 45 return new WebContentsObserver(webContents) { | |
| 46 @Override | |
| 47 public void destroy() { | |
| 48 int tabId = mTab == null ? Tab.INVALID_TAB_ID : mTab.getId(); | |
|
Ted C
2015/07/06 22:18:28
you want to call super.destroy() as well.
whywhat
2015/07/07 15:44:57
Oops, didn't expect any of the WebContentsObserver
| |
| 49 NotificationMediaPlaybackControls.hide(tabId); | |
| 50 } | |
| 51 | |
| 52 @Override | |
| 53 public void mediaSessionStateChanged(boolean isControllable, boolean isPaused) { | |
| 54 assert mTab != null; | |
| 55 if (!isControllable) { | |
| 56 NotificationMediaPlaybackControls.hide(mTab.getId()); | |
| 57 return; | |
| 58 } | |
| 59 String origin = mTab.getUrl(); | |
| 60 try { | |
| 61 origin = UrlUtilities.getOriginForDisplay(new URI(origin), t rue); | |
| 62 } catch (URISyntaxException e) { | |
| 63 Log.e(TAG, "Wrong syntax for the tab URL when playing media. Showing the URL."); | |
|
Ted C
2015/07/06 22:18:28
I would say, "Unable to parse the origin from the
whywhat
2015/07/07 15:44:57
Done.
| |
| 64 } | |
| 65 NotificationMediaPlaybackControls.show( | |
| 66 ApplicationStatus.getApplicationContext(), | |
| 67 new MediaInfo( | |
| 68 mTab.getTitle(), | |
| 69 isPaused, | |
| 70 origin, | |
| 71 mTab.getId(), | |
| 72 mControlsListener)); | |
| 73 } | |
| 74 }; | |
| 75 | |
| 76 } | |
| 77 | |
| 78 private void cleanupWebContents() { | |
| 79 if (mWebContents != null && mWebContentsObserver != null) { | |
|
Ted C
2015/07/06 22:18:28
here, just check mWebContentsObserver != null and
whywhat
2015/07/07 15:44:57
Done.
| |
| 80 mWebContents.removeObserver(mWebContentsObserver); | |
| 81 } | |
| 82 mWebContentsObserver = null; | |
| 83 mWebContents = null; | |
| 84 } | |
| 85 | |
| 86 private final TabObserver mTabObserver = new EmptyTabObserver() { | |
| 87 @Override | |
| 88 public void onContentChanged(Tab tab) { | |
| 89 assert tab == mTab; | |
| 90 | |
| 91 // Usually means that tab's web contents changed. | |
|
Ted C
2015/07/06 22:18:28
this also happens when you navigate to a native pa
whywhat
2015/07/07 15:44:57
Isn't this included in "web contents changed" as i
Ted C
2015/07/07 17:11:19
No, the webcontents only changes for a tab when yo
whywhat
2015/07/07 19:19:04
I think I understand now but just to be clear - th
Ted C
2015/07/07 20:04:12
Correct, nothing to change. Just wanted to fully
| |
| 92 cleanupWebContents(); | |
| 93 mWebContents = tab.getWebContents(); | |
| 94 if (mWebContents != null) { | |
| 95 mWebContentsObserver = createWebContentsObserver(mWebContents); | |
| 96 mWebContents.addObserver(mWebContentsObserver); | |
|
whywhat
2015/07/07 15:44:57
Also wasn't needed - WebContentsObserver adds itse
| |
| 97 } | |
| 98 } | |
| 99 | |
| 100 @Override | |
| 101 public void onDestroyed(Tab tab) { | |
| 102 assert mTab == tab; | |
| 103 | |
| 104 cleanupWebContents(); | |
| 105 | |
| 106 NotificationMediaPlaybackControls.hide(mTab.getId()); | |
| 107 mTab.removeObserver(this); | |
| 108 mTab = null; | |
| 109 } | |
| 110 }; | |
| 111 | |
| 112 private MediaSessionTabHelper(Tab tab) { | |
| 113 mTab = tab; | |
| 114 mTab.addObserver(mTabObserver); | |
|
Ted C
2015/07/06 22:18:28
For safety sake, I would do
if (tab.getWebContent
whywhat
2015/07/07 15:44:57
Done.
| |
| 115 } | |
| 116 | |
| 117 /** | |
| 118 * Creates the {@link MediaSessionTabHelper} for the given {@link Tab}. | |
| 119 * @param tab the tab to attach the helper to. | |
| 120 */ | |
| 121 public static void createForTab(Tab tab) { | |
| 122 new MediaSessionTabHelper(tab); | |
| 123 } | |
| 124 } | |
| OLD | NEW |