|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "gma/src/android/query_info_internal_android.h" |
| 18 | + |
| 19 | +#include <assert.h> |
| 20 | +#include <jni.h> |
| 21 | + |
| 22 | +#include <cstdarg> |
| 23 | +#include <cstddef> |
| 24 | + |
| 25 | +#include "app/src/assert.h" |
| 26 | +#include "app/src/util_android.h" |
| 27 | +#include "gma/src/android/ad_request_converter.h" |
| 28 | +#include "gma/src/android/gma_android.h" |
| 29 | +#include "gma/src/common/gma_common.h" |
| 30 | +#include "gma/src/include/firebase/gma.h" |
| 31 | +#include "gma/src/include/firebase/gma/internal/query_info.h" |
| 32 | +#include "gma/src/include/firebase/gma/types.h" |
| 33 | + |
| 34 | +namespace firebase { |
| 35 | +namespace gma { |
| 36 | + |
| 37 | +METHOD_LOOKUP_DEFINITION(query_info_helper, |
| 38 | + "com/google/firebase/gma/internal/cpp/QueryInfoHelper", |
| 39 | + QUERYINFOHELPER_METHODS); |
| 40 | + |
| 41 | +namespace internal { |
| 42 | + |
| 43 | +QueryInfoInternalAndroid::QueryInfoInternalAndroid(QueryInfo* base) |
| 44 | + : QueryInfoInternal(base), helper_(nullptr), initialized_(false) { |
| 45 | + firebase::MutexLock lock(mutex_); |
| 46 | + JNIEnv* env = ::firebase::gma::GetJNI(); |
| 47 | + jobject helper_ref = env->NewObject( |
| 48 | + query_info_helper::GetClass(), |
| 49 | + query_info_helper::GetMethodId(query_info_helper::kConstructor), |
| 50 | + reinterpret_cast<jlong>(this)); |
| 51 | + util::CheckAndClearJniExceptions(env); |
| 52 | + |
| 53 | + FIREBASE_ASSERT(helper_ref); |
| 54 | + helper_ = env->NewGlobalRef(helper_ref); |
| 55 | + FIREBASE_ASSERT(helper_); |
| 56 | + env->DeleteLocalRef(helper_ref); |
| 57 | +} |
| 58 | + |
| 59 | +QueryInfoInternalAndroid::~QueryInfoInternalAndroid() { |
| 60 | + firebase::MutexLock lock(mutex_); |
| 61 | + JNIEnv* env = ::firebase::gma::GetJNI(); |
| 62 | + |
| 63 | + env->CallVoidMethod( |
| 64 | + helper_, query_info_helper::GetMethodId(query_info_helper::kDisconnect)); |
| 65 | + util::CheckAndClearJniExceptions(env); |
| 66 | + env->DeleteGlobalRef(helper_); |
| 67 | + helper_ = nullptr; |
| 68 | +} |
| 69 | + |
| 70 | +Future<void> QueryInfoInternalAndroid::Initialize(AdParent parent) { |
| 71 | + firebase::MutexLock lock(mutex_); |
| 72 | + |
| 73 | + if (initialized_) { |
| 74 | + const SafeFutureHandle<void> future_handle = |
| 75 | + future_data_.future_impl.SafeAlloc<void>(kQueryInfoFnInitialize); |
| 76 | + Future<void> future = MakeFuture(&future_data_.future_impl, future_handle); |
| 77 | + CompleteFuture(kAdErrorCodeAlreadyInitialized, |
| 78 | + kAdAlreadyInitializedErrorMessage, future_handle, |
| 79 | + &future_data_); |
| 80 | + return future; |
| 81 | + } |
| 82 | + |
| 83 | + initialized_ = true; |
| 84 | + JNIEnv* env = ::firebase::gma::GetJNI(); |
| 85 | + FIREBASE_ASSERT(env); |
| 86 | + |
| 87 | + FutureCallbackData<void>* callback_data = |
| 88 | + CreateVoidFutureCallbackData(kQueryInfoFnInitialize, &future_data_); |
| 89 | + Future<void> future = |
| 90 | + MakeFuture(&future_data_.future_impl, callback_data->future_handle); |
| 91 | + env->CallVoidMethod( |
| 92 | + helper_, query_info_helper::GetMethodId(query_info_helper::kInitialize), |
| 93 | + reinterpret_cast<jlong>(callback_data), parent); |
| 94 | + util::CheckAndClearJniExceptions(env); |
| 95 | + return future; |
| 96 | +} |
| 97 | + |
| 98 | +Future<QueryInfoResult> QueryInfoInternalAndroid::CreateQueryInfo( |
| 99 | + AdFormat format, const AdRequest& request) { |
| 100 | + firebase::MutexLock lock(mutex_); |
| 101 | + |
| 102 | + if (!initialized_) { |
| 103 | + SafeFutureHandle<QueryInfoResult> future_handle = |
| 104 | + CreateFuture<QueryInfoResult>(kQueryInfoFnCreateQueryInfo, |
| 105 | + &future_data_); |
| 106 | + Future<QueryInfoResult> future = |
| 107 | + MakeFuture(&future_data_.future_impl, future_handle); |
| 108 | + CompleteFuture(kAdErrorCodeUninitialized, kAdUninitializedErrorMessage, |
| 109 | + future_handle, &future_data_, QueryInfoResult()); |
| 110 | + return future; |
| 111 | + } |
| 112 | + |
| 113 | + gma::AdErrorCode error = kAdErrorCodeNone; |
| 114 | + jobject j_request = GetJavaAdRequestFromCPPAdRequest(request, &error); |
| 115 | + if (j_request == nullptr) { |
| 116 | + if (error == kAdErrorCodeNone) { |
| 117 | + error = kAdErrorCodeInternalError; |
| 118 | + } |
| 119 | + return CreateAndCompleteFutureWithQueryInfoResult( |
| 120 | + kQueryInfoFnCreateQueryInfo, error, |
| 121 | + kAdCouldNotParseAdRequestErrorMessage, &future_data_, |
| 122 | + QueryInfoResult()); |
| 123 | + } |
| 124 | + JNIEnv* env = GetJNI(); |
| 125 | + FIREBASE_ASSERT(env); |
| 126 | + FutureCallbackData<QueryInfoResult>* callback_data = |
| 127 | + CreateQueryInfoResultFutureCallbackData(kQueryInfoFnCreateQueryInfo, |
| 128 | + &future_data_); |
| 129 | + Future<QueryInfoResult> future = |
| 130 | + MakeFuture(&future_data_.future_impl, callback_data->future_handle); |
| 131 | + |
| 132 | + jstring j_ad_unit_str = env->NewStringUTF(""); |
| 133 | + ::firebase::gma::GetJNI()->CallVoidMethod( |
| 134 | + helper_, |
| 135 | + query_info_helper::GetMethodId(query_info_helper::kCreateQueryInfo), |
| 136 | + reinterpret_cast<jlong>(callback_data), static_cast<int>(format), |
| 137 | + j_ad_unit_str, j_request); |
| 138 | + util::CheckAndClearJniExceptions(env); |
| 139 | + env->DeleteLocalRef(j_ad_unit_str); |
| 140 | + env->DeleteLocalRef(j_request); |
| 141 | + return future; |
| 142 | +} |
| 143 | + |
| 144 | +Future<QueryInfoResult> QueryInfoInternalAndroid::CreateQueryInfoWithAdUnit( |
| 145 | + AdFormat format, const AdRequest& request, const char* ad_unit_id) { |
| 146 | + firebase::MutexLock lock(mutex_); |
| 147 | + |
| 148 | + if (!initialized_) { |
| 149 | + SafeFutureHandle<QueryInfoResult> future_handle = |
| 150 | + CreateFuture<QueryInfoResult>(kQueryInfoFnCreateQueryInfoWithAdUnit, |
| 151 | + &future_data_); |
| 152 | + Future<QueryInfoResult> future = |
| 153 | + MakeFuture(&future_data_.future_impl, future_handle); |
| 154 | + CompleteFuture(kAdErrorCodeUninitialized, kAdUninitializedErrorMessage, |
| 155 | + future_handle, &future_data_, QueryInfoResult()); |
| 156 | + return future; |
| 157 | + } |
| 158 | + |
| 159 | + gma::AdErrorCode error = kAdErrorCodeNone; |
| 160 | + jobject j_request = GetJavaAdRequestFromCPPAdRequest(request, &error); |
| 161 | + if (j_request == nullptr) { |
| 162 | + if (error == kAdErrorCodeNone) { |
| 163 | + error = kAdErrorCodeInternalError; |
| 164 | + } |
| 165 | + return CreateAndCompleteFutureWithQueryInfoResult( |
| 166 | + kQueryInfoFnCreateQueryInfoWithAdUnit, error, |
| 167 | + kAdCouldNotParseAdRequestErrorMessage, &future_data_, |
| 168 | + QueryInfoResult()); |
| 169 | + } |
| 170 | + JNIEnv* env = GetJNI(); |
| 171 | + FIREBASE_ASSERT(env); |
| 172 | + FutureCallbackData<QueryInfoResult>* callback_data = |
| 173 | + CreateQueryInfoResultFutureCallbackData( |
| 174 | + kQueryInfoFnCreateQueryInfoWithAdUnit, &future_data_); |
| 175 | + Future<QueryInfoResult> future = |
| 176 | + MakeFuture(&future_data_.future_impl, callback_data->future_handle); |
| 177 | + |
| 178 | + jstring j_ad_unit_str = env->NewStringUTF(ad_unit_id); |
| 179 | + ::firebase::gma::GetJNI()->CallVoidMethod( |
| 180 | + helper_, |
| 181 | + query_info_helper::GetMethodId(query_info_helper::kCreateQueryInfo), |
| 182 | + reinterpret_cast<jlong>(callback_data), static_cast<int>(format), |
| 183 | + j_ad_unit_str, j_request); |
| 184 | + util::CheckAndClearJniExceptions(env); |
| 185 | + env->DeleteLocalRef(j_ad_unit_str); |
| 186 | + env->DeleteLocalRef(j_request); |
| 187 | + return future; |
| 188 | +} |
| 189 | + |
| 190 | +} // namespace internal |
| 191 | +} // namespace gma |
| 192 | +} // namespace firebase |
0 commit comments