From 6a92e0ebba6dbd39e1f88784d1e5f8bd7a8767e4 Mon Sep 17 00:00:00 2001 From: Magnus Jedvert Date: Wed, 29 Apr 2020 14:47:12 +0200 Subject: [PATCH] Android: Allow for re-assigning ScopedJavaGlobalRef MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, ScopedJavaGlobalRef can only be set at creation and never changed. This CL makes it possible to re-set these. Bug: b/153389044 Change-Id: I6be92dae83a9f5f3d87aa44dde226b874f4ca0a5 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/174041 Reviewed-by: Sami Kalliomäki Commit-Queue: Magnus Jedvert Cr-Commit-Position: refs/heads/master@{#31145} --- sdk/android/native_api/jni/scoped_java_ref.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sdk/android/native_api/jni/scoped_java_ref.h b/sdk/android/native_api/jni/scoped_java_ref.h index e37a992445..ac2c4f4c88 100644 --- a/sdk/android/native_api/jni/scoped_java_ref.h +++ b/sdk/android/native_api/jni/scoped_java_ref.h @@ -172,6 +172,7 @@ class ScopedJavaGlobalRef : public JavaRef { public: using JavaRef::obj_; + ScopedJavaGlobalRef() = default; explicit constexpr ScopedJavaGlobalRef(std::nullptr_t) {} ScopedJavaGlobalRef(JNIEnv* env, const JavaRef& other) : JavaRef(static_cast(env->NewGlobalRef(other.obj()))) {} @@ -185,6 +186,21 @@ class ScopedJavaGlobalRef : public JavaRef { AttachCurrentThreadIfNeeded()->DeleteGlobalRef(obj_); } + void operator=(const JavaRef& other) { + JNIEnv* env = AttachCurrentThreadIfNeeded(); + if (obj_ != nullptr) { + env->DeleteGlobalRef(obj_); + } + obj_ = other.is_null() ? nullptr : env->NewGlobalRef(other.obj()); + } + + void operator=(std::nullptr_t) { + if (obj_ != nullptr) { + AttachCurrentThreadIfNeeded()->DeleteGlobalRef(obj_); + } + obj_ = nullptr; + } + // Releases the reference to the caller. The caller *must* delete the // reference when it is done with it. Note that calling a Java method // is *not* a transfer of ownership and Release() should not be used.