From 017414a18c5f775bb07c2bd5c87197b101fb251a Mon Sep 17 00:00:00 2001 From: Gian-Carlo Pascutto Date: Wed, 25 Sep 2013 08:06:21 +0200 Subject: [PATCH] Bug 918372 - Allocate a single GlobalRef for the Android Context. r=blassey --- content/media/webrtc/MediaEngineWebRTC.cpp | 5 --- .../src/media-conduit/AudioConduit.cpp | 9 ----- .../src/media-conduit/VideoConduit.cpp | 9 ----- widget/android/AndroidBridge.cpp | 39 +++++++++++++++---- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/content/media/webrtc/MediaEngineWebRTC.cpp b/content/media/webrtc/MediaEngineWebRTC.cpp index 25711d775a21..1136d815477c 100644 --- a/content/media/webrtc/MediaEngineWebRTC.cpp +++ b/content/media/webrtc/MediaEngineWebRTC.cpp @@ -94,15 +94,10 @@ MediaEngineWebRTC::EnumerateVideoDevices(nsTArrayGetVM(); - JNIEnv *env; - jint res = jvm->AttachCurrentThread(&env, NULL); - if (webrtc::VideoEngine::SetAndroidObjects(jvm, (void*)context) != 0) { LOG(("VieCapture:SetAndroidObjects Failed")); return; } - - env->DeleteGlobalRef(context); #endif if (!mVideoEngine) { diff --git a/media/webrtc/signaling/src/media-conduit/AudioConduit.cpp b/media/webrtc/signaling/src/media-conduit/AudioConduit.cpp index 46628dcf8f72..94d597a32ea3 100644 --- a/media/webrtc/signaling/src/media-conduit/AudioConduit.cpp +++ b/media/webrtc/signaling/src/media-conduit/AudioConduit.cpp @@ -145,19 +145,10 @@ MediaConduitErrorCode WebrtcAudioConduit::Init(WebrtcAudioConduit *other) // get the JVM JavaVM *jvm = jsjni_GetVM(); - JNIEnv* env; - if (jvm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) { - CSFLogError(logTag, "%s: could not get Java environment", __FUNCTION__); - return kMediaConduitSessionNotInited; - } - jvm->AttachCurrentThread(&env, NULL); - if (webrtc::VoiceEngine::SetAndroidObjects(jvm, (void*)context) != 0) { CSFLogError(logTag, "%s Unable to set Android objects", __FUNCTION__); return kMediaConduitSessionNotInited; } - - env->DeleteGlobalRef(context); #endif //Per WebRTC APIs below function calls return NULL on failure diff --git a/media/webrtc/signaling/src/media-conduit/VideoConduit.cpp b/media/webrtc/signaling/src/media-conduit/VideoConduit.cpp index 4d90fdb1a586..58e80652c277 100644 --- a/media/webrtc/signaling/src/media-conduit/VideoConduit.cpp +++ b/media/webrtc/signaling/src/media-conduit/VideoConduit.cpp @@ -113,19 +113,10 @@ MediaConduitErrorCode WebrtcVideoConduit::Init() // get the JVM JavaVM *jvm = jsjni_GetVM(); - JNIEnv* env; - if (jvm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) { - CSFLogError(logTag, "%s: could not get Java environment", __FUNCTION__); - return kMediaConduitSessionNotInited; - } - jvm->AttachCurrentThread(&env, nullptr); - if (webrtc::VideoEngine::SetAndroidObjects(jvm, (void*)context) != 0) { CSFLogError(logTag, "%s: could not set Android objects", __FUNCTION__); return kMediaConduitSessionNotInited; } - - env->DeleteGlobalRef(context); #endif if( !(mVideoEngine = webrtc::VideoEngine::Create()) ) diff --git a/widget/android/AndroidBridge.cpp b/widget/android/AndroidBridge.cpp index e772c1259548..64bceeada53f 100644 --- a/widget/android/AndroidBridge.cpp +++ b/widget/android/AndroidBridge.cpp @@ -49,6 +49,7 @@ NS_IMPL_ISUPPORTS0(nsFilePickerCallback) StaticRefPtr AndroidBridge::sBridge; static unsigned sJavaEnvThreadIndex = 0; +static jobject sGlobalContext = nullptr; static void JavaThreadDetachFunc(void *arg); // This is a dummy class that can be used in the template for android::sp @@ -1434,18 +1435,40 @@ AndroidBridge::LockWindow(void *window, unsigned char **bits, int *width, int *h jobject AndroidBridge::GetGlobalContextRef() { - JNIEnv *env = GetJNIForThread(); - if (!env) - return 0; + if (sGlobalContext == nullptr) { + JNIEnv *env = GetJNIForThread(); + if (!env) + return 0; - AutoLocalJNIFrame jniFrame(env, 2); + AutoLocalJNIFrame jniFrame(env, 4); - jobject context = GetContext(); + jobject context = GetContext(); + if (!context) { + ALOG_BRIDGE("%s: Could not GetContext()", __FUNCTION__); + return 0; + } + jclass contextClass = env->FindClass("android/content/Context"); + if (!contextClass) { + ALOG_BRIDGE("%s: Could not find Context class.", __FUNCTION__); + return 0; + } + jmethodID mid = env->GetMethodID(contextClass, "getApplicationContext", + "()Landroid/content/Context;"); + if (!mid) { + ALOG_BRIDGE("%s: Could not find getApplicationContext.", __FUNCTION__); + return 0; + } + jobject appContext = env->CallObjectMethod(context, mid); + if (!appContext) { + ALOG_BRIDGE("%s: getApplicationContext failed.", __FUNCTION__); + return 0; + } - jobject globalRef = env->NewGlobalRef(context); - MOZ_ASSERT(globalRef); + sGlobalContext = env->NewGlobalRef(appContext); + MOZ_ASSERT(sGlobalContext); + } - return globalRef; + return sGlobalContext; } bool