Android: Allow for re-assigning ScopedJavaGlobalRef

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 <sakal@webrtc.org>
Commit-Queue: Magnus Jedvert <magjed@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31145}
This commit is contained in:
Magnus Jedvert
2020-04-29 14:47:12 +02:00
committed by Commit Bot
parent b5a013815f
commit 6a92e0ebba

View File

@ -172,6 +172,7 @@ class ScopedJavaGlobalRef : public JavaRef<T> {
public:
using JavaRef<T>::obj_;
ScopedJavaGlobalRef() = default;
explicit constexpr ScopedJavaGlobalRef(std::nullptr_t) {}
ScopedJavaGlobalRef(JNIEnv* env, const JavaRef<T>& other)
: JavaRef<T>(static_cast<T>(env->NewGlobalRef(other.obj()))) {}
@ -185,6 +186,21 @@ class ScopedJavaGlobalRef : public JavaRef<T> {
AttachCurrentThreadIfNeeded()->DeleteGlobalRef(obj_);
}
void operator=(const JavaRef<T>& 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.