Skip to content

Commit 93bd9be

Browse files
Internal Updates (#1595)
Internal changes only - Integration to enable QueryInfo generation.
1 parent df09d59 commit 93bd9be

23 files changed

+1478
-2
lines changed

app/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ if (IOS)
546546
${FIREBASE_SOURCE_DIR}/gma/src/include/firebase/gma/ad_view.h
547547
${FIREBASE_SOURCE_DIR}/gma/src/include/firebase/gma/interstitial_ad.h
548548
${FIREBASE_SOURCE_DIR}/gma/src/include/firebase/gma/internal/native_ad.h
549+
${FIREBASE_SOURCE_DIR}/gma/src/include/firebase/gma/internal/query_info.h
549550
${FIREBASE_SOURCE_DIR}/gma/src/include/firebase/gma/rewarded_ad.h
550551
${FIREBASE_SOURCE_DIR}/gma/src/include/firebase/gma/types.h
551552
${FIREBASE_SOURCE_DIR}/gma/src/include/firebase/gma/ump.h

gma/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ set(common_SRCS
2626
src/common/full_screen_ad_event_listener.cc
2727
src/common/native_ad.cc
2828
src/common/native_ad_internal.cc
29+
src/common/query_info.cc
30+
src/common/query_info_internal.cc
2931
src/common/rewarded_ad.cc
3032
src/common/rewarded_ad_internal.cc)
3133

@@ -49,6 +51,7 @@ set(android_SRCS
4951
src/android/interstitial_ad_internal_android.cc
5052
src/android/native_ad_image_android.cc
5153
src/android/native_ad_internal_android.cc
54+
src/android/query_info_internal_android.cc
5255
src/android/response_info_android.cc
5356
src/android/rewarded_ad_internal_android.cc)
5457

@@ -68,6 +71,7 @@ set(ios_SRCS
6871
src/ios/interstitial_ad_internal_ios.mm
6972
src/ios/native_ad_image_ios.mm
7073
src/ios/native_ad_internal_ios.mm
74+
src/ios/query_info_internal_ios.mm
7175
src/ios/response_info_ios.mm
7276
src/ios/rewarded_ad_internal_ios.mm)
7377

gma/integration_test/src/integration_test.cc

+66
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,72 @@ TEST_F(FirebaseGmaTest, TestNativeAdLoad) {
993993
delete native_ad;
994994
}
995995

996+
TEST_F(FirebaseGmaTest, TestCreateQueryInfo) {
997+
SKIP_TEST_ON_DESKTOP;
998+
SKIP_TEST_ON_SIMULATOR;
999+
1000+
firebase::gma::QueryInfo* query_info = new firebase::gma::QueryInfo();
1001+
1002+
WaitForCompletion(query_info->Initialize(app_framework::GetWindowContext()),
1003+
"Initialize");
1004+
1005+
firebase::gma::AdRequest request = GetAdRequest();
1006+
// Set the requester type to 8. QueryInfo gets generated without a
1007+
// query_info_type set, but throws a warning that it is missing.
1008+
request.add_extra(kAdNetworkExtrasClassName, "query_info_type",
1009+
"requester_type_8");
1010+
// When the QueryInfo is initialized, generate a query info string.
1011+
firebase::Future<firebase::gma::QueryInfoResult> create_query_info_future =
1012+
query_info->CreateQueryInfo(firebase::gma::kAdFormatNative, request);
1013+
1014+
WaitForCompletion(create_query_info_future, "CreateQueryInfo");
1015+
1016+
if (create_query_info_future.error() == firebase::gma::kAdErrorCodeNone) {
1017+
const firebase::gma::QueryInfoResult* result_ptr =
1018+
create_query_info_future.result();
1019+
ASSERT_NE(result_ptr, nullptr);
1020+
EXPECT_TRUE(result_ptr->is_successful());
1021+
EXPECT_FALSE(result_ptr->query_info().empty());
1022+
}
1023+
1024+
create_query_info_future.Release();
1025+
delete query_info;
1026+
}
1027+
1028+
TEST_F(FirebaseGmaTest, TestCreateQueryInfoWithAdUnit) {
1029+
SKIP_TEST_ON_DESKTOP;
1030+
SKIP_TEST_ON_SIMULATOR;
1031+
1032+
firebase::gma::QueryInfo* query_info = new firebase::gma::QueryInfo();
1033+
1034+
WaitForCompletion(query_info->Initialize(app_framework::GetWindowContext()),
1035+
"Initialize");
1036+
1037+
firebase::gma::AdRequest request = GetAdRequest();
1038+
// Set the requester type to 8. QueryInfo gets generated without a
1039+
// query_info_type set, but throws a warning that it is missing.
1040+
request.add_extra(kAdNetworkExtrasClassName, "query_info_type",
1041+
"requester_type_8");
1042+
// When the QueryInfo is initialized, generate a query info string.
1043+
// Providing a bad/empty ad unit does not affect the query info generation.
1044+
firebase::Future<firebase::gma::QueryInfoResult> create_query_info_future =
1045+
query_info->CreateQueryInfoWithAdUnit(firebase::gma::kAdFormatNative,
1046+
request, kNativeAdUnit);
1047+
1048+
WaitForCompletion(create_query_info_future, "CreateQueryInfoWithAdUnit");
1049+
1050+
if (create_query_info_future.error() == firebase::gma::kAdErrorCodeNone) {
1051+
const firebase::gma::QueryInfoResult* result_ptr =
1052+
create_query_info_future.result();
1053+
ASSERT_NE(result_ptr, nullptr);
1054+
EXPECT_TRUE(result_ptr->is_successful());
1055+
EXPECT_FALSE(result_ptr->query_info().empty());
1056+
}
1057+
1058+
create_query_info_future.Release();
1059+
delete query_info;
1060+
}
1061+
9961062
// Interactive test section. These have been placed up front so that the
9971063
// tester doesn't get bored waiting for them.
9981064
TEST_F(FirebaseGmaUITest, TestAdViewAdOpenedAdClosed) {

gma/src/android/gma_android.cc

+45
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "gma/src/android/interstitial_ad_internal_android.h"
4141
#include "gma/src/android/native_ad_image_android.h"
4242
#include "gma/src/android/native_ad_internal_android.h"
43+
#include "gma/src/android/query_info_internal_android.h"
4344
#include "gma/src/android/response_info_android.h"
4445
#include "gma/src/android/rewarded_ad_internal_android.h"
4546
#include "gma/src/common/gma_common.h"
@@ -363,6 +364,9 @@ Future<AdapterInitializationStatus> Initialize(JNIEnv* env, jobject activity,
363364
download_helper::CacheClassFromFiles(env, activity, &embedded_files) !=
364365
nullptr &&
365366
download_helper::CacheMethodIds(env, activity) &&
367+
query_info_helper::CacheClassFromFiles(env, activity,
368+
&embedded_files) != nullptr &&
369+
query_info_helper::CacheMethodIds(env, activity) &&
366370
rewarded_ad_helper::CacheClassFromFiles(env, activity,
367371
&embedded_files) != nullptr &&
368372
rewarded_ad_helper::CacheMethodIds(env, activity) &&
@@ -705,6 +709,7 @@ void ReleaseClasses(JNIEnv* env) {
705709
native_ad_helper::ReleaseClass(env);
706710
native_image::ReleaseClass(env);
707711
download_helper::ReleaseClass(env);
712+
query_info_helper::ReleaseClass(env);
708713
rewarded_ad_helper::ReleaseClass(env);
709714
load_ad_error::ReleaseClass(env);
710715
}
@@ -917,6 +922,35 @@ void JNI_completeLoadedAd(JNIEnv* env, jclass clazz, jlong data_ptr,
917922
env->DeleteLocalRef(j_response_info);
918923
}
919924

925+
void JNI_completeCreateQueryInfoSuccess(JNIEnv* env, jclass clazz,
926+
jlong data_ptr, jstring j_query_info) {
927+
FIREBASE_ASSERT(env);
928+
FIREBASE_ASSERT(data_ptr);
929+
FIREBASE_ASSERT(j_query_info);
930+
931+
std::string query_info = util::JStringToString(env, j_query_info);
932+
FutureCallbackData<QueryInfoResult>* callback_data =
933+
reinterpret_cast<FutureCallbackData<QueryInfoResult>*>(data_ptr);
934+
GmaInternal::CompleteCreateQueryInfoFutureSuccess(callback_data, query_info);
935+
env->DeleteLocalRef(j_query_info);
936+
}
937+
938+
void JNI_completeQueryInfoError(JNIEnv* env, jclass clazz, jlong data_ptr,
939+
jint j_error_code, jstring j_error_message) {
940+
FIREBASE_ASSERT(env);
941+
FIREBASE_ASSERT(data_ptr);
942+
FIREBASE_ASSERT(j_error_message);
943+
944+
// QueryInfo errors return only internal AdErrorCode values.
945+
const AdErrorCode error_code = static_cast<AdErrorCode>(j_error_code);
946+
std::string error_message = util::JStringToString(env, j_error_message);
947+
948+
FutureCallbackData<QueryInfoResult>* callback_data =
949+
reinterpret_cast<FutureCallbackData<QueryInfoResult>*>(data_ptr);
950+
GmaInternal::CompleteCreateQueryInfoFutureFailure(callback_data, error_code,
951+
error_message);
952+
}
953+
920954
void JNI_NativeAd_completeLoadedAd(JNIEnv* env, jclass clazz, jlong data_ptr,
921955
jlong native_internal_data_ptr,
922956
jobject j_icon, jobjectArray j_images,
@@ -1307,6 +1341,15 @@ bool RegisterNatives() {
13071341
reinterpret_cast<void*>(&JNI_NativeAd_notifyAdOpened)},
13081342
};
13091343

1344+
static const JNINativeMethod kQueryInfoMethods[] = {
1345+
{"completeQueryInfoFutureCallback", "(JILjava/lang/String;)V",
1346+
reinterpret_cast<void*>(&JNI_completeAdFutureCallback)},
1347+
{"completeCreateQueryInfoSuccess", "(JLjava/lang/String;)V",
1348+
reinterpret_cast<void*>(&JNI_completeCreateQueryInfoSuccess)},
1349+
{"completeCreateQueryInfoError", "(JILjava/lang/String;)V",
1350+
reinterpret_cast<void*>(&JNI_completeQueryInfoError)},
1351+
};
1352+
13101353
static const JNINativeMethod kNativeImageMethods[] = {
13111354
{"completeNativeImageFutureCallback", "(JILjava/lang/String;)V",
13121355
reinterpret_cast<void*>(&JNI_completeAdFutureCallback)},
@@ -1370,6 +1413,8 @@ bool RegisterNatives() {
13701413
download_helper::RegisterNatives(
13711414
env, kNativeImageMethods,
13721415
FIREBASE_ARRAYSIZE(kNativeImageMethods)) &&
1416+
query_info_helper::RegisterNatives(
1417+
env, kQueryInfoMethods, FIREBASE_ARRAYSIZE(kQueryInfoMethods)) &&
13731418
rewarded_ad_helper::RegisterNatives(
13741419
env, kRewardedAdMethods, FIREBASE_ARRAYSIZE(kRewardedAdMethods)) &&
13751420
gma_initialization_helper::RegisterNatives(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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

Comments
 (0)