commit d70d4d689c7820001dd6d55c6672ac0a608c51b0 parent c90c138fc659905dc90f030aad5af1ef9fa44da0 Author: Makoto Kato <m_kato@ga2.so-net.ne.jp> Date: Fri, 5 Dec 2025 06:30:16 +0000 Bug 2003417 - Remove AndroidBridge::GetGlobalContextRef. r=jolin,geckoview-reviewers,webrtc-reviewers,pehrsons,ohall `VideoCaptureAndroid.java` and `video_capture_android.cc` aren't a part of WebRTC SDK now. So these are maintained by us. Although `AndroidBridge::GetGlobalContextRef` seems to store the application context as global reference, it isn't good due to possible leak. We should simply use `GeckoAppShell.getApplicationContext` instead of JNI. Differential Revision: https://phabricator.services.mozilla.com/D274710 Diffstat:
4 files changed, 5 insertions(+), 29 deletions(-)
diff --git a/dom/media/systemservices/android_video_capture/java/src/org/webrtc/videoengine/VideoCaptureAndroid.java b/dom/media/systemservices/android_video_capture/java/src/org/webrtc/videoengine/VideoCaptureAndroid.java @@ -22,6 +22,7 @@ import androidx.annotation.NonNull; import java.util.concurrent.CountDownLatch; import org.mozilla.gecko.annotation.WebRTCJNITarget; +import org.mozilla.gecko.GeckoAppShell; import org.webrtc.CameraEnumerator; import org.webrtc.Camera1Enumerator; @@ -78,8 +79,9 @@ public class VideoCaptureAndroid implements CameraVideoCapturer.CameraEventsHand } // Return the global application context. - @WebRTCJNITarget - private static native Context GetContext(); + private static Context GetContext() { + return GeckoAppShell.getApplicationContext(); + } public boolean canCapture() { return cameraVideoCapturer != null; diff --git a/dom/media/systemservices/android_video_capture/video_capture_android.cc b/dom/media/systemservices/android_video_capture/video_capture_android.cc @@ -19,7 +19,6 @@ static JavaVM* g_jvm_capture = NULL; static jclass g_java_capturer_class = NULL; // VideoCaptureAndroid.class. -static jobject g_context = NULL; // Owned android.content.Context. namespace webrtc { @@ -34,12 +33,6 @@ void JniCommon_freeNativeByteBuffer(JNIEnv* env, jclass, jobject byte_buffer) { ::operator delete(data); } -// Called by Java to get the global application context. -jobject JNICALL GetContext(JNIEnv* env, jclass) { - assert(g_context); - return g_context; -} - // Called by Java when the camera has a new frame to deliver. void JNICALL ProvideCameraFrame(JNIEnv* env, jobject, jint width, jint height, jobject javaDataY, jint strideY, @@ -76,8 +69,6 @@ int32_t SetCaptureAndroidVM(JavaVM* javaVM) { g_jvm_capture = javaVM; AttachThreadScoped ats(g_jvm_capture); - g_context = mozilla::AndroidBridge::Bridge()->GetGlobalContextRef(); - videocapturemodule::DeviceInfoAndroid::Initialize(g_jvm_capture); { @@ -89,14 +80,12 @@ int32_t SetCaptureAndroidVM(JavaVM* javaVM) { assert(g_java_capturer_class); JNINativeMethod native_methods[] = { - {"GetContext", "()Landroid/content/Context;", - reinterpret_cast<void*>(&GetContext)}, {"ProvideCameraFrame", "(IILjava/nio/ByteBuffer;ILjava/nio/ByteBuffer;ILjava/nio/" "ByteBuffer;IIJJ)V", reinterpret_cast<void*>(&ProvideCameraFrame)}}; if (ats.env()->RegisterNatives(g_java_capturer_class, native_methods, - 2) != 0) + 1) != 0) assert(false); } @@ -118,7 +107,6 @@ int32_t SetCaptureAndroidVM(JavaVM* javaVM) { ats.env()->UnregisterNatives(g_java_capturer_class); ats.env()->DeleteGlobalRef(g_java_capturer_class); g_java_capturer_class = NULL; - g_context = NULL; videocapturemodule::DeviceInfoAndroid::DeInitialize(); g_jvm_capture = NULL; } diff --git a/widget/android/AndroidBridge.cpp b/widget/android/AndroidBridge.cpp @@ -41,7 +41,6 @@ using namespace mozilla; AndroidBridge* AndroidBridge::sBridge = nullptr; -static jobject sGlobalContext = nullptr; jmethodID AndroidBridge::GetMethodID(JNIEnv* env, jclass jClass, const char* methodName, @@ -273,14 +272,6 @@ void AndroidBridge::GetCurrentNetworkInformation( env->ReleaseDoubleArrayElements(arr.Get(), info, 0); } -jobject AndroidBridge::GetGlobalContextRef() { - // The context object can change, so get a fresh copy every time. - auto context = java::GeckoAppShell::GetApplicationContext(); - sGlobalContext = jni::Object::GlobalRef(context).Forget(); - MOZ_ASSERT(sGlobalContext); - return sGlobalContext; -} - /* Implementation file */ NS_IMPL_ISUPPORTS(nsAndroidBridge, nsIGeckoViewEventDispatcher, nsIGeckoViewBridge) diff --git a/widget/android/AndroidBridge.h b/widget/android/AndroidBridge.h @@ -48,11 +48,6 @@ class AndroidBridge final { void GetIconForExtension(const nsACString& aFileExt, uint32_t aIconSize, uint8_t* const aBuf); - // Returns a global reference to the Context for Fennec's Activity. The - // caller is responsible for ensuring this doesn't leak by calling - // DeleteGlobalRef() when the context is no longer needed. - jobject GetGlobalContextRef(void); - void GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo); void GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInfo);