Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(716)

Unified Diff: content/public/android/java/src/org/chromium/content/browser/ChromeMediaSession.java

Issue 2439483003: Link MediaSessionTabHelper with native MediaSession [CL is going to be split] (Closed)
Patch Set: Don't review, this CL is getting huge and needs to be split Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/public/android/java/src/org/chromium/content/browser/ChromeMediaSession.java
diff --git a/content/public/android/java/src/org/chromium/content/browser/ChromeMediaSession.java b/content/public/android/java/src/org/chromium/content/browser/ChromeMediaSession.java
new file mode 100644
index 0000000000000000000000000000000000000000..7630212af767b5c666d579b5982a6c591cce9778
--- /dev/null
+++ b/content/public/android/java/src/org/chromium/content/browser/ChromeMediaSession.java
@@ -0,0 +1,128 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.content.browser;
+
+import org.chromium.base.ObserverList;
+import org.chromium.base.ObserverList.RewindableIterator;
+import org.chromium.base.VisibleForTesting;
+import org.chromium.base.annotations.CalledByNative;
+import org.chromium.base.annotations.JNINamespace;
+import org.chromium.content_public.browser.WebContents;
+import org.chromium.content_public.common.MediaMetadata;
+
+/**
+ * The ChromeMediaSession Java wrapper to allow communicating with the native MediaSession object.
+ * ChromeMediaSession is created and owned by native MediaSessionAndroid.
+ */
+@JNINamespace("content")
+public class ChromeMediaSession {
+ private long mNativeMediaSessionAndroid;
+ // The observers are stored only Java and are proxied via ChromeMediaSession to observe native
+ // MediaSession.
+ private ObserverList<MediaSessionObserver> mObservers;
+ private RewindableIterator<MediaSessionObserver> mObserversIterator;
+
+ /**
+ * @returns The {@link ChromeMediaSession} for |contents|.
+ */
+ public static ChromeMediaSession fromWebContents(WebContents contents) {
+ return nativeGetMediaSessionFromWebContents(contents);
+ }
+
+ /**
+ * Add an observer. This method can only be called from {@link MediaSessionObserver}
+ * constructor.
+ */
+ protected void addObserver(MediaSessionObserver observer) {
+ mObservers.addObserver(observer);
+ }
+
+ /**
+ * Remove an observer.
+ */
+ public void removeObserver(MediaSessionObserver observer) {
+ mObservers.removeObserver(observer);
+ }
+
+ /**
+ * Wrapper for native MediaSession::Resume().
+ */
+ public void resume() {
+ nativeResume(mNativeMediaSessionAndroid);
+ }
+
+ /**
+ * Wrapper for native MediaSession::Suspend();
+ */
+ public void suspend() {
+ nativeSuspend(mNativeMediaSessionAndroid);
+ }
+
+ /**
+ * Wrapper for native MediaSession::Stop();
+ */
+ public void stop() {
+ nativeStop(mNativeMediaSessionAndroid);
+ }
+
+ @CalledByNative
+ private boolean hasObservers() {
+ return !mObservers.isEmpty();
+ }
+
+ @CalledByNative
+ private void mediaSessionDestroyed() {
+ for (mObserversIterator.rewind(); mObserversIterator.hasNext();) {
+ mObserversIterator.next().mediaSessionDestroyed();
+ }
+ }
+
+ @VisibleForTesting
+ @CalledByNative
+ public void mediaSessionStateChanged(boolean isControllable, boolean isSuspended) {
+ for (mObserversIterator.rewind(); mObserversIterator.hasNext();) {
+ mObserversIterator.next().mediaSessionStateChanged(isControllable, isSuspended);
+ }
+ }
+
+ @VisibleForTesting
+ @CalledByNative
+ public void mediaSessionMetadataChanged(MediaMetadata metadata) {
+ for (mObserversIterator.rewind(); mObserversIterator.hasNext();) {
+ mObserversIterator.next().mediaSessionMetadataChanged(metadata);
+ }
+ }
+
+ /**
+ * Create a ChromeMediaSession object for a native MediaSession.
+ */
+ @CalledByNative
+ private static ChromeMediaSession create(long nativeChromeMediaSession) {
+ return new ChromeMediaSession(nativeChromeMediaSession);
+ }
+
+ /**
+ * Called when the native MediaSessionAndroid (the owner) is destroyed.
+ */
+ @CalledByNative
+ private void tearDown() {
+ mNativeMediaSessionAndroid = 0;
+ // All observers should stop observing when receiving MediaSessionDestroyed.
+ assert mObservers.isEmpty();
+ mObservers.clear();
+ }
+
+ private ChromeMediaSession(long nativeChromeMediaSession) {
+ mNativeMediaSessionAndroid = nativeChromeMediaSession;
+ mObservers = new ObserverList<MediaSessionObserver>();
+ mObserversIterator = mObservers.rewindableIterator();
+ }
+
+ private static native ChromeMediaSession nativeGetMediaSessionFromWebContents(
+ WebContents contents);
+ private native void nativeResume(long nativeMediaSessionAndroid);
+ private native void nativeSuspend(long nativeMediaSessionAndroid);
+ private native void nativeStop(long nativeMediaSessionAndroid);
+}

Powered by Google App Engine
This is Rietveld 408576698