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

Side by Side 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, 1 month 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 #include "content/browser/media/session/media_session_delegate_android.h"
6
7 #include "base/android/context_utils.h"
8 #include "base/android/jni_android.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "content/browser/media/session/media_session.h"
11 #include "content/common/android/media_metadata_android.h"
12 #include "jni/MediaSessionDelegate_jni.h"
13
14 using base::android::JavaParamRef;
15
16 namespace content {
17
18 // static
19 bool MediaSessionDelegateAndroid::Register(JNIEnv* env) {
20 return RegisterNativesImpl(env);
21 }
22
23 // static
24 void MediaSessionDelegateAndroid::CreateAndConnect(
25 MediaSession* session,
26 JNIEnv* env,
27 const base::android::JavaParamRef<jobject>& j_delegate) {
28 MediaSessionDelegateAndroid* delegate =
29 new MediaSessionDelegateAndroid(session, env, j_delegate);
30 session->PassObserverOwnership(
31 std::unique_ptr<MediaSessionDelegateAndroid>(delegate));
32 Java_MediaSessionDelegate_onMediaSessionConnected(
whywhat 2016/10/21 18:28:14 I think "connect/disconnect" terms are misused and
33 env, j_delegate, reinterpret_cast<intptr_t>(delegate));
34 }
35
36 MediaSessionDelegateAndroid::MediaSessionDelegateAndroid(
37 MediaSession* session,
38 JNIEnv* env,
39 const base::android::JavaParamRef<jobject>& j_delegate)
40 : MediaSessionObserver(session), j_delegate_(env, j_delegate) {}
41
42 MediaSessionDelegateAndroid::~MediaSessionDelegateAndroid() {
43 MediaSessionDisconnected();
44 }
45
46 void MediaSessionDelegateAndroid::MediaSessionDisconnected() {
47 MediaSessionObserver::MediaSessionDisconnected();
48 JNIEnv* env = base::android::AttachCurrentThread();
49 DCHECK(env);
50
51 ScopedJavaLocalRef<jobject> j_delegate_local = j_delegate_.get(env);
52 if (j_delegate_local.is_null()) return;
53
54 Java_MediaSessionDelegate_onMediaSessionDisconnected(env, j_delegate_local);
55 }
56
57 void MediaSessionDelegateAndroid::MediaSessionStateChanged(bool is_controllable,
58 bool is_active) {
59 JNIEnv* env = base::android::AttachCurrentThread();
60 DCHECK(env);
61
62 ScopedJavaLocalRef<jobject> j_delegate_local = j_delegate_.get(env);
63 if (j_delegate_local.is_null()) return;
64
65 Java_MediaSessionDelegate_mediaSessionStateChanged(
66 env, j_delegate_local, is_controllable, is_active);
67 }
68
69 void MediaSessionDelegateAndroid::MediaSessionMetadataChanged(
70 const base::Optional<MediaMetadata>& metadata) {
71 JNIEnv* env = base::android::AttachCurrentThread();
72 DCHECK(env);
73
74 ScopedJavaLocalRef<jobject> j_delegate_local = j_delegate_.get(env);
75 if (j_delegate_local.is_null()) return;
76
77 base::android::ScopedJavaLocalRef<jobject> j_metadata;
78 if (metadata.has_value()) {
79 j_metadata =
80 MediaMetadataAndroid::CreateJavaObject(env, metadata.value());
81 }
82 Java_MediaSessionDelegate_mediaSessionMetadataChanged(
83 env, j_delegate_local.get(env), j_metadata);
84 }
85
86 void MediaSessionDelegateAndroid::ResumeMediaSession(
87 JNIEnv* env,
88 const JavaParamRef<jobject>& obj) {
89 if (session())
90 session()->Resume(MediaSession::SuspendType::UI);
91 }
92
93 void MediaSessionDelegateAndroid::SuspendMediaSession(
94 JNIEnv* env,
95 const JavaParamRef<jobject>& obj) {
96 if (session())
97 session()->Suspend(MediaSession::SuspendType::UI);
98 }
99
100 void MediaSessionDelegateAndroid::StopMediaSession(
101 JNIEnv* env,
102 const JavaParamRef<jobject>& obj) {
103 if (session())
104 session()->Stop(MediaSession::SuspendType::UI);
105 }
106
107 void MediaSessionDelegateAndroid::DisconnectMediaSession(
108 JNIEnv* env,
109 const JavaParamRef<jobject>& obj) {
110 // Will destroy this and send `onMediaSessionDisconnected` to Java.
111 if (session())
112 session()->RemoveObserver(this);
113 }
114
115 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698