Chromium Code Reviews| Index: components/devtools_bridge/android/session_dependency_factory_android.cc |
| diff --git a/components/devtools_bridge/android/session_dependency_factory_android.cc b/components/devtools_bridge/android/session_dependency_factory_android.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..30b8ad91973b5992042cf270a4f4b8d43d5246c0 |
| --- /dev/null |
| +++ b/components/devtools_bridge/android/session_dependency_factory_android.cc |
| @@ -0,0 +1,249 @@ |
| +// Copyright 2014 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 "components/devtools_bridge/android/session_dependency_factory_android.h" |
| + |
| +#include "base/android/jni_string.h" |
| +#include "base/android/scoped_java_ref.h" |
| +#include "base/bind.h" |
| +#include "components/devtools_bridge/abstract_data_channel.h" |
| +#include "components/devtools_bridge/abstract_peer_connection.h" |
| +#include "components/devtools_bridge/rtc_configuration.h" |
| +#include "jni/SessionDependencyFactoryNative_jni.h" |
| + |
| +namespace devtools_bridge { |
| +namespace android { |
| + |
| +namespace { |
| + |
| +class JNIInvokeUtil { |
|
mnaganov (inactive)
2014/11/10 18:10:26
We don't usually use such helper classes: this one
SeRya
2014/11/11 08:08:19
Done.
|
| + public: |
| + JNIInvokeUtil() { |
| + env_ = base::android::AttachCurrentThread(); |
| + } |
| + |
| + JNIEnv* env() { return env_; } |
| + |
| + jstring convert(std::string const& str) { |
| + return base::android::ConvertUTF8ToJavaString(env_, str).Release(); |
| + } |
| + |
| + private: |
| + JNIEnv* env_; |
| +}; |
| + |
| +} // namespace |
| + |
| +class PeerConnectionDelegateImpl |
| + : public AbstractPeerConnection::Delegate { |
| + public: |
| + PeerConnectionDelegateImpl(JNIEnv* env, jobject java_object) { |
| + java_object_.Reset(env, java_object); |
| + connected_ = false; |
| + } |
| + |
| + virtual void OnIceConnectionChange(bool connected) override { |
| + JNIInvokeUtil util; |
| + Java_SessionDependencyFactoryNative_notifyIceConnectionChange( |
| + util.env(), java_object_.obj(), connected); |
| + } |
| + |
| + virtual void OnIceCandidate( |
| + std::string const& sdp_mid, int sdp_mline_index, std::string const& sdp) |
| + override { |
| + JNIInvokeUtil util; |
| + Java_SessionDependencyFactoryNative_notifyIceCandidate( |
| + util.env(), java_object_.obj(), util.convert(sdp_mid), |
| + sdp_mline_index, util.convert(sdp)); |
| + } |
| + |
| + void NotifyLocalOfferCreatedAndSetSet(std::string const& description) { |
| + JNIInvokeUtil util; |
| + Java_SessionDependencyFactoryNative_notifyLocalOfferCreatedAndSetSet( |
| + util.env(), java_object_.obj(), util.convert(description)); |
| + } |
| + |
| + virtual void OnLocalOfferCreatedAndSetSet(std::string const& description) |
| + override { |
| + JNIInvokeUtil util; |
| + Java_SessionDependencyFactoryNative_notifyLocalOfferCreatedAndSetSet( |
| + util.env(), java_object_.obj(), util.convert(description)); |
| + } |
| + |
| + virtual void OnLocalAnswerCreatedAndSetSet(std::string const& description) |
| + override { |
| + JNIInvokeUtil util; |
| + Java_SessionDependencyFactoryNative_notifyLocalAnswerCreatedAndSetSet( |
| + util.env(), java_object_.obj(), util.convert(description)); |
| + } |
| + |
| + virtual void OnRemoteDescriptionSet() override { |
| + JNIInvokeUtil util; |
| + Java_SessionDependencyFactoryNative_notifyRemoteDescriptionSet( |
| + util.env(), java_object_.obj()); |
| + } |
| + |
| + virtual void OnFailure(std::string const& description) override { |
| + JNIInvokeUtil util; |
| + Java_SessionDependencyFactoryNative_notifyConnectionFailure( |
| + util.env(), java_object_.obj(), util.convert(description)); |
| + } |
| + |
| + private: |
| + base::android::ScopedJavaGlobalRef<jobject> java_object_; |
| + bool connected_; |
| +}; |
| + |
| +namespace { |
| + |
| +class JNIHandlingUtil { |
| + public: |
| + JNIHandlingUtil(JNIEnv* env) : env_(env) { |
| + } |
| + |
| + static jlong cast(void* obj) { |
| + return reinterpret_cast<jlong>(obj); |
| + } |
| + |
| + static RTCConfiguration* toConfig(jlong ptr) { |
| + return reinterpret_cast<RTCConfiguration*>(ptr); |
| + } |
| + |
| + static SessionDependencyFactoryAndroid* toFactory( |
| + jlong ptr) { |
| + return reinterpret_cast<SessionDependencyFactoryAndroid*>(ptr); |
| + } |
| + |
| + static AbstractPeerConnection* toConnection(jlong ptr) { |
| + return reinterpret_cast<AbstractPeerConnection*>(ptr); |
| + } |
| + |
| + static AbstractDataChannel* toDataChannel(jlong ptr) { |
| + return reinterpret_cast<AbstractDataChannel*>(ptr); |
| + } |
| + |
| + std::string convert(jstring str) { |
| + return base::android::ConvertJavaStringToUTF8(env_, str); |
| + } |
| + |
| + private: |
| + JNIEnv* const env_; |
| +}; |
| + |
| +static void CleanupOnSignalingThread() { |
| + // Called on signaling thread when SessionDependencyFactory is destroying. |
| + base::android::DetachFromVM(); |
| +} |
| + |
| +} // namespace |
| + |
| +// SessionDependencyFactoryNative |
| + |
| +SessionDependencyFactoryAndroid::SessionDependencyFactoryAndroid() |
| + : impl_(SessionDependencyFactory::CreateInstance( |
| + base::Bind(&CleanupOnSignalingThread))) { |
| +} |
| + |
| +SessionDependencyFactoryAndroid::~SessionDependencyFactoryAndroid() { |
| +} |
| + |
| +// static |
| +void SessionDependencyFactoryAndroid::RegisterNatives(JNIEnv* env) { |
| + RegisterNativesImpl(env); |
| +} |
| + |
| +scoped_ptr<AbstractPeerConnection> |
| +SessionDependencyFactoryAndroid::CreatePeerConnection( |
| + scoped_ptr<RTCConfiguration> config, |
| + scoped_ptr<AbstractPeerConnection::Delegate> delegate) { |
| + return impl_->CreatePeerConnection(config.Pass(), delegate.Pass()); |
| +} |
| + |
| +// JNI generated methods |
| + |
| +static jlong CreateFactory(JNIEnv* env, jclass jcaller) { |
| + return JNIHandlingUtil::cast(new SessionDependencyFactoryAndroid()); |
| +} |
| + |
| +static void DestroyFactory(JNIEnv* env, jclass jcaller, jlong ptr) { |
| + delete JNIHandlingUtil::toFactory(ptr); |
| +} |
| + |
| +static jlong CreateConfig(JNIEnv* env, jclass jcaller) { |
| + return JNIHandlingUtil::cast( |
| + RTCConfiguration::CreateInstance().release()); |
| +} |
| + |
| +static void AddIceServer( |
| + JNIEnv* env, jclass jcaller, jlong configPtr, |
| + jstring uri, jstring username, jstring credential) { |
| + JNIHandlingUtil util(env); |
| + |
| + JNIHandlingUtil::toConfig(configPtr)->AddIceServer( |
| + util.convert(uri), util.convert(username), util.convert(credential)); |
| +} |
| + |
| +static jlong CreatePeerConnection( |
| + JNIEnv* env, jclass jcaller, |
| + jlong factoryPtr, jlong configPtr, jobject observer) { |
| + auto factory = JNIHandlingUtil::toFactory(factoryPtr); |
| + auto config = JNIHandlingUtil::toConfig(configPtr); |
| + |
| + auto delegate = new PeerConnectionDelegateImpl(env, observer); |
| + |
| + return JNIHandlingUtil::cast(factory->CreatePeerConnection( |
| + make_scoped_ptr(config), make_scoped_ptr(delegate)).release()); |
| +} |
| + |
| +static void DestroyPeerConnection( |
| + JNIEnv* env, jclass jcaller, jlong peerConnectionPtr) { |
| + delete JNIHandlingUtil::toConnection(peerConnectionPtr); |
| +} |
| + |
| +static void CreateAndSetLocalOffer( |
| + JNIEnv* env, jclass jcaller, jlong peerConnectionPtr) { |
| + JNIHandlingUtil::toConnection(peerConnectionPtr)->CreateAndSetLocalOffer(); |
| +} |
| + |
| +static void CreateAndSetLocalAnswer( |
| + JNIEnv* env, jclass jcaller, jlong peerConnectionPtr) { |
| + JNIHandlingUtil::toConnection(peerConnectionPtr)->CreateAndSetLocalAnswer(); |
| +} |
| + |
| +static void SetRemoteOffer( |
| + JNIEnv* env, jclass jcaller, jlong peerConnectionPtr, jstring description) { |
| + JNIHandlingUtil util(env); |
| + JNIHandlingUtil::toConnection(peerConnectionPtr)->SetRemoteOffer( |
| + util.convert(description)); |
| +} |
| + |
| +static void SetRemoteAnswer( |
| + JNIEnv* env, jclass jcaller, jlong peerConnectionPtr, jstring description) { |
| + JNIHandlingUtil util(env); |
| + JNIHandlingUtil::toConnection(peerConnectionPtr)->SetRemoteAnswer( |
| + util.convert(description)); |
| +} |
| + |
| +static void AddIceCandidate( |
| + JNIEnv* env, jclass jcaller, |
| + jlong peerConnectionPtr, jstring sdpMid, jint sdpMLineIndex, jstring sdp) { |
| + JNIHandlingUtil util(env); |
| + util.toConnection(peerConnectionPtr)->AddIceCandidate( |
| + util.convert(sdpMid), sdpMLineIndex, util.convert(sdp)); |
| +} |
| + |
| +static jlong CreateDataChannel( |
| + JNIEnv* env, jclass jcaller, jlong peerConnectionPtr, jint channelId) { |
| + return JNIHandlingUtil::cast( |
| + JNIHandlingUtil::toConnection( |
| + peerConnectionPtr)->CreateDataChannel(channelId).release()); |
| +} |
| + |
| +static void DestroyDataChannel( |
| + JNIEnv* env, jclass jcaller, jlong dataChannelPtr) { |
| + delete JNIHandlingUtil::toDataChannel(dataChannelPtr); |
| +} |
| + |
| +} // namespace android |
| +} // namespace devtools_bridge |