1414 * limitations under the License.
1515 */
1616
17+ #include " admob/src/android/banner_view_internal_android.h"
18+
1719#include < assert.h>
1820#include < jni.h>
1921
2325#include " admob/admob_resources.h"
2426#include " admob/src/android/ad_request_converter.h"
2527#include " admob/src/android/admob_android.h"
26- #include " admob/src/android/banner_view_internal_android.h"
2728#include " admob/src/common/admob_common.h"
2829#include " admob/src/include/firebase/admob.h"
2930#include " admob/src/include/firebase/admob/banner_view.h"
3031#include " admob/src/include/firebase/admob/types.h"
3132#include " app/src/assert.h"
3233#include " app/src/mutex.h"
34+ #include " app/src/semaphore.h"
3335#include " app/src/util_android.h"
3436
3537namespace firebase {
@@ -56,24 +58,25 @@ BannerViewInternalAndroid::BannerViewInternalAndroid(BannerView* base)
5658 env->DeleteLocalRef (helper_ref);
5759}
5860
61+
62+ void DestroyOnDeleteCallback (const Future<void >& result,
63+ void * sem_data) {
64+ if (sem_data != nullptr ) {
65+ Semaphore* semaphore = static_cast <Semaphore*>(sem_data);
66+ semaphore->Post ();
67+ }
68+ }
69+
5970BannerViewInternalAndroid::~BannerViewInternalAndroid () {
6071 JNIEnv* env = ::firebase::admob::GetJNI ();
6172
62- // Destroy the banner view so all pending futures / callbacks complete.
63- {
64- Mutex mutex (Mutex::kModeNonRecursive );
65- mutex.Acquire ();
66- Destroy ().OnCompletion (
67- [](const Future<void >&, void * mutex) {
68- reinterpret_cast <Mutex*>(mutex)->Release ();
69- },
70- &mutex);
71- // Acquire a second Mutex lock to block until the Future for the last call
72- // to Destroy() completes at which point the lambda function in OnCompletion
73- // is called and the Mutex lock is released.
74- mutex.Acquire ();
75- mutex.Release ();
76- }
73+ DestroyInternalData ();
74+
75+ Semaphore semaphore (0 );
76+ InvokeNullary (kBannerViewFnDestroyOnDelete , banner_view_helper::kDestroy )
77+ .OnCompletion (DestroyOnDeleteCallback, &semaphore);
78+
79+ semaphore.Wait ();
7780 env->DeleteGlobalRef (helper_);
7881 helper_ = nullptr ;
7982}
@@ -131,8 +134,7 @@ Future<void> BannerViewInternalAndroid::Resume() {
131134}
132135
133136Future<void > BannerViewInternalAndroid::Destroy () {
134- // The bounding box is zeroed on destroy.
135- bounding_box_ = {};
137+ DestroyInternalData ();
136138 return InvokeNullary (kBannerViewFnDestroy , banner_view_helper::kDestroy );
137139}
138140
@@ -214,6 +216,11 @@ Future<void> BannerViewInternalAndroid::InvokeNullary(
214216 return GetLastResult (fn);
215217}
216218
219+ void BannerViewInternalAndroid::DestroyInternalData () {
220+ // The bounding box is zeroed on destroy.
221+ bounding_box_ = {};
222+ }
223+
217224} // namespace internal
218225} // namespace admob
219226} // namespace firebase
0 commit comments