OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "components/devtools_bridge/android/session_dependency_factory_android.
h" | |
6 | |
7 #include "base/android/jni_string.h" | |
8 #include "base/android/scoped_java_ref.h" | |
9 #include "base/bind.h" | |
10 #include "components/devtools_bridge/abstract_data_channel.h" | |
11 #include "components/devtools_bridge/abstract_peer_connection.h" | |
12 #include "components/devtools_bridge/rtc_configuration.h" | |
13 #include "components/devtools_bridge/socket_tunnel_server.h" | |
14 #include "jni/SessionDependencyFactoryNative_jni.h" | |
15 | |
16 using base::android::AttachCurrentThread; | |
17 using base::android::ConvertJavaStringToUTF8; | |
18 using base::android::ConvertUTF8ToJavaString; | |
19 | |
20 namespace devtools_bridge { | |
21 namespace android { | |
22 | |
23 namespace { | |
24 | |
25 /** | |
26 * Wraps Java observer and adapts it to native delegate. Chromium code normally | |
27 * leaves local java references for automatic disposing. It doesn't happen here | |
28 * (because calls originated from native thread). For instance, instead of | |
29 * | |
30 * ConvertUTF8ToJavaString(env, ...).Release() | |
31 * | |
32 * please use ConvertUTF8ToJavaString(env, ...).obj() or ScopedJavaLocalFrame. | |
33 */ | |
34 class PeerConnectionDelegateImpl | |
35 : public AbstractPeerConnection::Delegate { | |
36 public: | |
37 PeerConnectionDelegateImpl(JNIEnv* env, jobject java_object) { | |
38 java_object_.Reset(env, java_object); | |
39 connected_ = false; | |
40 } | |
41 | |
42 void OnIceConnectionChange(bool connected) override { | |
43 JNIEnv* env = AttachCurrentThread(); | |
44 Java_SessionDependencyFactoryNative_notifyIceConnectionChange( | |
45 env, java_object_.obj(), connected); | |
46 } | |
47 | |
48 void OnIceCandidate(const std::string& sdp_mid, | |
49 int sdp_mline_index, | |
50 const std::string& sdp) override { | |
51 JNIEnv* env = AttachCurrentThread(); | |
52 Java_SessionDependencyFactoryNative_notifyIceCandidate( | |
53 env, java_object_.obj(), | |
54 ConvertUTF8ToJavaString(env, sdp_mid).obj(), | |
55 sdp_mline_index, ConvertUTF8ToJavaString(env, sdp).obj()); | |
56 } | |
57 | |
58 void NotifyLocalOfferCreatedAndSetSet(const std::string& description) { | |
59 JNIEnv* env = AttachCurrentThread(); | |
60 Java_SessionDependencyFactoryNative_notifyLocalOfferCreatedAndSetSet( | |
61 env, java_object_.obj(), | |
62 ConvertUTF8ToJavaString(env, description).obj()); | |
63 } | |
64 | |
65 void OnLocalOfferCreatedAndSetSet(const std::string& description) override { | |
66 JNIEnv* env = AttachCurrentThread(); | |
67 Java_SessionDependencyFactoryNative_notifyLocalOfferCreatedAndSetSet( | |
68 env, java_object_.obj(), | |
69 ConvertUTF8ToJavaString(env, description).obj()); | |
70 } | |
71 | |
72 void OnLocalAnswerCreatedAndSetSet(const std::string& description) override { | |
73 JNIEnv* env = AttachCurrentThread(); | |
74 Java_SessionDependencyFactoryNative_notifyLocalAnswerCreatedAndSetSet( | |
75 env, java_object_.obj(), | |
76 ConvertUTF8ToJavaString(env, description).obj()); | |
77 } | |
78 | |
79 void OnRemoteDescriptionSet() override { | |
80 JNIEnv* env = AttachCurrentThread(); | |
81 Java_SessionDependencyFactoryNative_notifyRemoteDescriptionSet( | |
82 env, java_object_.obj()); | |
83 } | |
84 | |
85 void OnFailure(const std::string& description) override { | |
86 JNIEnv* env = AttachCurrentThread(); | |
87 Java_SessionDependencyFactoryNative_notifyConnectionFailure( | |
88 env, java_object_.obj(), | |
89 ConvertUTF8ToJavaString(env, description).obj()); | |
90 } | |
91 | |
92 private: | |
93 base::android::ScopedJavaGlobalRef<jobject> java_object_; | |
94 bool connected_; | |
95 }; | |
96 | |
97 class DataChannelObserverImpl : public AbstractDataChannel::Observer { | |
98 public: | |
99 DataChannelObserverImpl(JNIEnv* env, jobject java_object) { | |
100 java_object_.Reset(env, java_object); | |
101 } | |
102 | |
103 void OnOpen() override { | |
104 JNIEnv* env = AttachCurrentThread(); | |
105 Java_SessionDependencyFactoryNative_notifyChannelOpen( | |
106 env, java_object_.obj()); | |
107 } | |
108 | |
109 void OnClose() override { | |
110 JNIEnv* env = AttachCurrentThread(); | |
111 Java_SessionDependencyFactoryNative_notifyChannelClose( | |
112 env, java_object_.obj()); | |
113 } | |
114 | |
115 void OnMessage(const void* data, size_t length) override { | |
116 JNIEnv* env = AttachCurrentThread(); | |
117 | |
118 ScopedJavaLocalRef<jobject> byte_buffer( | |
119 env, env->NewDirectByteBuffer(const_cast<void*>(data), length)); | |
120 | |
121 Java_SessionDependencyFactoryNative_notifyMessage( | |
122 env, java_object_.obj(), byte_buffer.obj()); | |
123 } | |
124 | |
125 private: | |
126 base::android::ScopedJavaGlobalRef<jobject> java_object_; | |
127 }; | |
128 | |
129 static void CleanupOnSignalingThread() { | |
130 // Called on signaling thread when SessionDependencyFactory is destroying. | |
131 base::android::DetachFromVM(); | |
132 } | |
133 | |
134 } // namespace | |
135 | |
136 // SessionDependencyFactoryNative | |
137 | |
138 SessionDependencyFactoryAndroid::SessionDependencyFactoryAndroid() | |
139 : impl_(SessionDependencyFactory::CreateInstance( | |
140 base::Bind(&CleanupOnSignalingThread))) { | |
141 } | |
142 | |
143 SessionDependencyFactoryAndroid::~SessionDependencyFactoryAndroid() { | |
144 } | |
145 | |
146 // static | |
147 bool SessionDependencyFactoryAndroid::RegisterNatives(JNIEnv* env) { | |
148 return RegisterNativesImpl(env); | |
149 } | |
150 | |
151 scoped_ptr<AbstractPeerConnection> | |
152 SessionDependencyFactoryAndroid::CreatePeerConnection( | |
153 scoped_ptr<RTCConfiguration> config, | |
154 scoped_ptr<AbstractPeerConnection::Delegate> delegate) { | |
155 return impl_->CreatePeerConnection(config.Pass(), delegate.Pass()); | |
156 } | |
157 | |
158 scoped_refptr<base::TaskRunner> | |
159 SessionDependencyFactoryAndroid::signaling_thread_task_runner() { | |
160 return impl_->signaling_thread_task_runner(); | |
161 } | |
162 | |
163 scoped_refptr<base::TaskRunner> | |
164 SessionDependencyFactoryAndroid::io_thread_task_runner() { | |
165 return impl_->io_thread_task_runner(); | |
166 } | |
167 | |
168 // JNI generated methods | |
169 | |
170 static jlong CreateFactory(JNIEnv* env, jclass jcaller) { | |
171 return reinterpret_cast<jlong>(new SessionDependencyFactoryAndroid()); | |
172 } | |
173 | |
174 static void DestroyFactory(JNIEnv* env, jclass jcaller, jlong factory_ptr) { | |
175 delete reinterpret_cast<SessionDependencyFactoryAndroid*>(factory_ptr); | |
176 } | |
177 | |
178 static jlong CreateConfig(JNIEnv* env, jclass jcaller) { | |
179 return reinterpret_cast<jlong>( | |
180 RTCConfiguration::CreateInstance().release()); | |
181 } | |
182 | |
183 static void AddIceServer( | |
184 JNIEnv* env, jclass jcaller, jlong config_ptr, | |
185 jstring uri, jstring username, jstring credential) { | |
186 reinterpret_cast<RTCConfiguration*>(config_ptr)->AddIceServer( | |
187 ConvertJavaStringToUTF8(env, uri), | |
188 ConvertJavaStringToUTF8(env, username), | |
189 ConvertJavaStringToUTF8(env, credential)); | |
190 } | |
191 | |
192 static jlong CreatePeerConnection( | |
193 JNIEnv* env, jclass jcaller, | |
194 jlong factory_ptr, jlong config_ptr, jobject observer) { | |
195 auto factory = | |
196 reinterpret_cast<SessionDependencyFactoryAndroid*>(factory_ptr); | |
197 auto config = reinterpret_cast<RTCConfiguration*>(config_ptr); | |
198 | |
199 auto delegate = new PeerConnectionDelegateImpl(env, observer); | |
200 | |
201 return reinterpret_cast<jlong>(factory->CreatePeerConnection( | |
202 make_scoped_ptr(config), make_scoped_ptr(delegate)).release()); | |
203 } | |
204 | |
205 static void DestroyPeerConnection( | |
206 JNIEnv* env, jclass jcaller, jlong connection_ptr) { | |
207 delete reinterpret_cast<AbstractPeerConnection*>(connection_ptr); | |
208 } | |
209 | |
210 static void CreateAndSetLocalOffer( | |
211 JNIEnv* env, jclass jcaller, jlong connection_ptr) { | |
212 reinterpret_cast<AbstractPeerConnection*>( | |
213 connection_ptr)->CreateAndSetLocalOffer(); | |
214 } | |
215 | |
216 static void CreateAndSetLocalAnswer( | |
217 JNIEnv* env, jclass jcaller, jlong connection_ptr) { | |
218 reinterpret_cast<AbstractPeerConnection*>( | |
219 connection_ptr)->CreateAndSetLocalAnswer(); | |
220 } | |
221 | |
222 static void SetRemoteOffer( | |
223 JNIEnv* env, jclass jcaller, jlong connection_ptr, jstring description) { | |
224 reinterpret_cast<AbstractPeerConnection*>(connection_ptr)->SetRemoteOffer( | |
225 ConvertJavaStringToUTF8(env, description)); | |
226 } | |
227 | |
228 static void SetRemoteAnswer( | |
229 JNIEnv* env, jclass jcaller, jlong connection_ptr, jstring description) { | |
230 reinterpret_cast<AbstractPeerConnection*>(connection_ptr)->SetRemoteAnswer( | |
231 ConvertJavaStringToUTF8(env, description)); | |
232 } | |
233 | |
234 static void AddIceCandidate( | |
235 JNIEnv* env, jclass jcaller, | |
236 jlong connection_ptr, jstring sdp_mid, jint sdp_mline_index, jstring sdp) { | |
237 reinterpret_cast<AbstractPeerConnection*>(connection_ptr)->AddIceCandidate( | |
238 ConvertJavaStringToUTF8(env, sdp_mid), | |
239 sdp_mline_index, | |
240 ConvertJavaStringToUTF8(env, sdp)); | |
241 } | |
242 | |
243 static jlong CreateDataChannel( | |
244 JNIEnv* env, jclass jcaller, jlong connection_ptr, jint channel_id) { | |
245 return reinterpret_cast<jlong>( | |
246 reinterpret_cast<AbstractPeerConnection*>( | |
247 connection_ptr)->CreateDataChannel(channel_id).release()); | |
248 } | |
249 | |
250 static void DestroyDataChannel( | |
251 JNIEnv* env, jclass jcaller, jlong channel_ptr) { | |
252 delete reinterpret_cast<AbstractDataChannel*>(channel_ptr); | |
253 } | |
254 | |
255 static void RegisterDataChannelObserver( | |
256 JNIEnv* env, jclass jcaller, jlong channel_ptr, jobject observer) { | |
257 reinterpret_cast<AbstractDataChannel*>(channel_ptr)->RegisterObserver( | |
258 make_scoped_ptr(new DataChannelObserverImpl(env, observer))); | |
259 } | |
260 | |
261 static void UnregisterDataChannelObserver( | |
262 JNIEnv* env, jclass jcaller, jlong channel_ptr) { | |
263 reinterpret_cast<AbstractDataChannel*>(channel_ptr)->UnregisterObserver(); | |
264 } | |
265 | |
266 static void SendBinaryMessage( | |
267 JNIEnv* env, jclass jcaller, jlong channel_ptr, jobject message, | |
268 jint size) { | |
269 DCHECK(size > 0); | |
270 reinterpret_cast<AbstractDataChannel*>(channel_ptr)->SendBinaryMessage( | |
271 env->GetDirectBufferAddress(message), size); | |
272 } | |
273 | |
274 static void SendTextMessage( | |
275 JNIEnv* env, jclass jcaller, jlong channel_ptr, jobject message, | |
276 jint size) { | |
277 DCHECK(size > 0); | |
278 reinterpret_cast<AbstractDataChannel*>(channel_ptr)->SendTextMessage( | |
279 env->GetDirectBufferAddress(message), size); | |
280 } | |
281 | |
282 static void CloseDataChannel(JNIEnv* env, jclass jcaller, jlong channel_ptr) { | |
283 reinterpret_cast<AbstractDataChannel*>(channel_ptr)->Close(); | |
284 } | |
285 | |
286 static jlong CreateSocketTunnelServer( | |
287 JNIEnv* env, jclass jcaller, jlong factory_ptr, jlong channel_ptr, | |
288 jstring socket_name) { | |
289 return reinterpret_cast<jlong>( | |
290 new SocketTunnelServer( | |
291 reinterpret_cast<SessionDependencyFactory*>(factory_ptr), | |
292 reinterpret_cast<AbstractDataChannel*>(channel_ptr), | |
293 ConvertJavaStringToUTF8(env, socket_name))); | |
294 } | |
295 | |
296 static void DestroySocketTunnelServer( | |
297 JNIEnv* env, jclass jcaller, jlong tunnel_ptr) { | |
298 delete reinterpret_cast<SocketTunnelServer*>(tunnel_ptr); | |
299 } | |
300 | |
301 } // namespace android | |
302 } // namespace devtools_bridge | |
OLD | NEW |