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

Unified Diff: content/browser/media/session/media_session_delegate_android.cc

Issue 2439483003: Link MediaSessionTabHelper with native MediaSession [CL is going to be split] (Closed)
Patch Set: addressed comments 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/browser/media/session/media_session_delegate_android.cc
diff --git a/content/browser/media/session/media_session_delegate_android.cc b/content/browser/media/session/media_session_delegate_android.cc
new file mode 100644
index 0000000000000000000000000000000000000000..312cdff058e4bdf4a0acef22732baacf5a15f904
--- /dev/null
+++ b/content/browser/media/session/media_session_delegate_android.cc
@@ -0,0 +1,115 @@
+// 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.
+
+#include "content/browser/media/session/media_session_delegate_android.h"
+
+#include "base/android/context_utils.h"
+#include "base/android/jni_android.h"
+#include "base/android/scoped_java_ref.h"
+#include "content/browser/media/session/media_session.h"
+#include "content/common/android/media_metadata_android.h"
+#include "jni/MediaSessionDelegate_jni.h"
+
+using base::android::JavaParamRef;
+
+namespace content {
+
+// static
+bool MediaSessionDelegateAndroid::Register(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+// static
+void MediaSessionDelegateAndroid::CreateAndConnect(
+ MediaSession* session,
+ JNIEnv* env,
+ const base::android::JavaParamRef<jobject>& j_delegate) {
+ MediaSessionDelegateAndroid* delegate =
+ new MediaSessionDelegateAndroid(session, env, j_delegate);
+ session->PassObserverOwnership(
+ std::unique_ptr<MediaSessionDelegateAndroid>(delegate));
+ Java_MediaSessionDelegate_onMediaSessionConnected(
whywhat 2016/10/21 18:28:14 I think "connect/disconnect" terms are misused and
+ env, j_delegate, reinterpret_cast<intptr_t>(delegate));
+}
+
+MediaSessionDelegateAndroid::MediaSessionDelegateAndroid(
+ MediaSession* session,
+ JNIEnv* env,
+ const base::android::JavaParamRef<jobject>& j_delegate)
+ : MediaSessionObserver(session), j_delegate_(env, j_delegate) {}
+
+MediaSessionDelegateAndroid::~MediaSessionDelegateAndroid() {
+ MediaSessionDisconnected();
+}
+
+void MediaSessionDelegateAndroid::MediaSessionDisconnected() {
+ MediaSessionObserver::MediaSessionDisconnected();
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DCHECK(env);
+
+ ScopedJavaLocalRef<jobject> j_delegate_local = j_delegate_.get(env);
+ if (j_delegate_local.is_null()) return;
+
+ Java_MediaSessionDelegate_onMediaSessionDisconnected(env, j_delegate_local);
+}
+
+void MediaSessionDelegateAndroid::MediaSessionStateChanged(bool is_controllable,
+ bool is_active) {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DCHECK(env);
+
+ ScopedJavaLocalRef<jobject> j_delegate_local = j_delegate_.get(env);
+ if (j_delegate_local.is_null()) return;
+
+ Java_MediaSessionDelegate_mediaSessionStateChanged(
+ env, j_delegate_local, is_controllable, is_active);
+}
+
+void MediaSessionDelegateAndroid::MediaSessionMetadataChanged(
+ const base::Optional<MediaMetadata>& metadata) {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DCHECK(env);
+
+ ScopedJavaLocalRef<jobject> j_delegate_local = j_delegate_.get(env);
+ if (j_delegate_local.is_null()) return;
+
+ base::android::ScopedJavaLocalRef<jobject> j_metadata;
+ if (metadata.has_value()) {
+ j_metadata =
+ MediaMetadataAndroid::CreateJavaObject(env, metadata.value());
+ }
+ Java_MediaSessionDelegate_mediaSessionMetadataChanged(
+ env, j_delegate_local.get(env), j_metadata);
+}
+
+void MediaSessionDelegateAndroid::ResumeMediaSession(
+ JNIEnv* env,
+ const JavaParamRef<jobject>& obj) {
+ if (session())
+ session()->Resume(MediaSession::SuspendType::UI);
+}
+
+void MediaSessionDelegateAndroid::SuspendMediaSession(
+ JNIEnv* env,
+ const JavaParamRef<jobject>& obj) {
+ if (session())
+ session()->Suspend(MediaSession::SuspendType::UI);
+}
+
+void MediaSessionDelegateAndroid::StopMediaSession(
+ JNIEnv* env,
+ const JavaParamRef<jobject>& obj) {
+ if (session())
+ session()->Stop(MediaSession::SuspendType::UI);
+}
+
+void MediaSessionDelegateAndroid::DisconnectMediaSession(
+ JNIEnv* env,
+ const JavaParamRef<jobject>& obj) {
+ // Will destroy this and send `onMediaSessionDisconnected` to Java.
+ if (session())
+ session()->RemoveObserver(this);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698