23
23
24
24
#include " admob/src/android/ad_request_converter.h"
25
25
#include " admob/src/android/admob_android.h"
26
+ #include " admob/src/android/response_info_android.h"
26
27
#include " admob/src/common/admob_common.h"
27
28
#include " admob/src/include/firebase/admob.h"
28
29
@@ -34,6 +35,11 @@ METHOD_LOOKUP_DEFINITION(ad_error,
34
35
" com/google/android/gms/ads/AdError" ,
35
36
ADERROR_METHODS);
36
37
38
+ METHOD_LOOKUP_DEFINITION (load_ad_error,
39
+ PROGUARD_KEEP_CLASS
40
+ " com/google/android/gms/ads/LoadAdError" ,
41
+ LOADADERROR_METHODS);
42
+
37
43
const char * const AdResult::kUndefinedDomain = " undefined" ;
38
44
39
45
AdResult::AdResult () {
@@ -43,11 +49,17 @@ AdResult::AdResult() {
43
49
internal_ = new AdResultInternal ();
44
50
internal_->is_successful = false ;
45
51
internal_->is_wrapper_error = true ;
52
+ internal_->is_load_ad_error = false ;
46
53
internal_->code = kAdMobErrorUninitialized ;
47
54
internal_->domain = " SDK" ;
48
55
internal_->message = " This AdResult has not be initialized." ;
49
56
internal_->to_string = internal_->message ;
50
57
internal_->j_ad_error = nullptr ;
58
+
59
+ // While most data is passed into this object through the AdResultInternal
60
+ // structure (above), the response_info_ is constructed when parsing
61
+ // the j_ad_error itself.
62
+ response_info_ = new ResponseInfo ();
51
63
}
52
64
53
65
AdResult::AdResult (const AdResultInternal& ad_result_internal) {
@@ -57,7 +69,9 @@ AdResult::AdResult(const AdResultInternal& ad_result_internal) {
57
69
internal_ = new AdResultInternal ();
58
70
internal_->is_successful = ad_result_internal.is_successful ;
59
71
internal_->is_wrapper_error = ad_result_internal.is_wrapper_error ;
72
+ internal_->is_load_ad_error = ad_result_internal.is_load_ad_error ;
60
73
internal_->j_ad_error = nullptr ;
74
+ response_info_ = new ResponseInfo ();
61
75
62
76
// AdResults can be returned on success, or for errors encountered in the C++
63
77
// SDK wrapper, or in the Android AdMob SDK. The structure is populated
@@ -104,17 +118,44 @@ AdResult::AdResult(const AdResultInternal& ad_result_internal) {
104
118
internal_->message = util::JStringToString (env, j_message);
105
119
env->DeleteLocalRef (j_message);
106
120
107
- // To string.
108
- jobject j_to_string = env->CallObjectMethod (
109
- internal_->j_ad_error , ad_error::GetMethodId (ad_error::kToString ));
110
- FIREBASE_ASSERT (j_to_string);
111
- internal_->to_string = util::JStringToString (env, j_to_string);
112
- env->DeleteLocalRef (j_to_string);
121
+ // Differentiate between a com.google.android.gms.ads.AdError or its
122
+ // com.google.android.gms.ads.LoadAdError subclass.
123
+ if (!internal_->is_load_ad_error ) {
124
+ // AdError.
125
+ jobject j_to_string = env->CallObjectMethod (
126
+ internal_->j_ad_error , ad_error::GetMethodId (ad_error::kToString ));
127
+ FIREBASE_ASSERT (j_to_string);
128
+ internal_->to_string = util::JStringToString (env, j_to_string);
129
+ env->DeleteLocalRef (j_to_string);
130
+ } else {
131
+ // LoadAdError.
132
+ jobject j_response_info = env->CallObjectMethod (
133
+ internal_->j_ad_error ,
134
+ load_ad_error::GetMethodId (load_ad_error::kGetResponseInfo ));
135
+
136
+ if (j_response_info != nullptr ) {
137
+ ResponseInfoInternal response_info_internal;
138
+ response_info_internal.j_response_info = j_response_info;
139
+ *response_info_ = ResponseInfo (response_info_internal);
140
+ env->DeleteLocalRef (j_response_info);
141
+ }
142
+
143
+ // A to_string value of this LoadAdError. Invoke the set_to_string
144
+ // protected method of the AdResult parent class to overwrite whatever
145
+ // it parsed.
146
+ jobject j_to_string = env->CallObjectMethod (
147
+ internal_->j_ad_error ,
148
+ load_ad_error::GetMethodId (load_ad_error::kToString ));
149
+ internal_->to_string = util::JStringToString (env, j_to_string);
150
+ env->DeleteLocalRef (j_to_string);
151
+ }
113
152
}
114
153
}
115
154
116
155
AdResult::AdResult (const AdResult& ad_result) : AdResult() {
156
+ FIREBASE_ASSERT (ad_result.response_info_ != nullptr );
117
157
// Reuse the assignment operator.
158
+ this ->response_info_ = new ResponseInfo ();
118
159
*this = ad_result;
119
160
}
120
161
@@ -127,46 +168,57 @@ AdResult& AdResult::operator=(const AdResult& ad_result) {
127
168
FIREBASE_ASSERT (env);
128
169
FIREBASE_ASSERT (internal_);
129
170
FIREBASE_ASSERT (ad_result.internal_ );
171
+ FIREBASE_ASSERT (response_info_);
172
+ FIREBASE_ASSERT (ad_result.response_info_ );
130
173
131
174
AdResultInternal* preexisting_internal = internal_;
132
175
{
133
176
// Lock the parties so they're not deleted while the copying takes place.
134
- MutexLock (ad_result.internal_ ->mutex );
135
- MutexLock (internal_->mutex );
177
+ MutexLock ad_result_lock (ad_result.internal_ ->mutex );
178
+ MutexLock lock (internal_->mutex );
136
179
internal_ = new AdResultInternal ();
137
180
138
181
internal_->is_successful = ad_result.internal_ ->is_successful ;
139
182
internal_->is_wrapper_error = ad_result.internal_ ->is_wrapper_error ;
183
+ internal_->is_load_ad_error = ad_result.internal_ ->is_load_ad_error ;
140
184
internal_->code = ad_result.internal_ ->code ;
141
185
internal_->domain = ad_result.internal_ ->domain ;
142
186
internal_->message = ad_result.internal_ ->message ;
143
187
if (ad_result.internal_ ->j_ad_error != nullptr ) {
144
188
internal_->j_ad_error =
145
189
env->NewGlobalRef (ad_result.internal_ ->j_ad_error );
146
190
}
147
-
148
191
if (preexisting_internal->j_ad_error ) {
149
192
env->DeleteGlobalRef (preexisting_internal->j_ad_error );
150
193
preexisting_internal->j_ad_error = nullptr ;
151
194
}
195
+
196
+ *response_info_ = *ad_result.response_info_ ;
152
197
}
153
198
154
199
// Deleting the internal_ deletes the mutex within it, so we wait for
155
200
// complete deletion until after the mutex lock leaves scope.
156
201
delete preexisting_internal;
202
+
157
203
return *this ;
158
204
}
159
205
160
206
AdResult::~AdResult () {
161
207
FIREBASE_ASSERT (internal_);
208
+ FIREBASE_ASSERT (response_info_);
209
+
162
210
if (internal_->j_ad_error != nullptr ) {
163
211
JNIEnv* env = GetJNI ();
164
212
FIREBASE_ASSERT (env);
165
213
env->DeleteGlobalRef (internal_->j_ad_error );
166
214
internal_->j_ad_error = nullptr ;
167
215
}
216
+
168
217
delete internal_;
169
218
internal_ = nullptr ;
219
+
220
+ delete response_info_;
221
+ response_info_ = nullptr ;
170
222
}
171
223
172
224
bool AdResult::is_successful () const {
@@ -193,7 +245,9 @@ std::unique_ptr<AdResult> AdResult::GetCause() const {
193
245
194
246
AdResultInternal ad_result_internal;
195
247
ad_result_internal.is_wrapper_error = false ;
248
+ ad_result_internal.is_load_ad_error = false ;
196
249
ad_result_internal.j_ad_error = j_ad_error;
250
+
197
251
std::unique_ptr<AdResult> ad_result =
198
252
std::unique_ptr<AdResult>(new AdResult (ad_result_internal));
199
253
env->DeleteLocalRef (j_ad_error);
@@ -219,17 +273,16 @@ const std::string& AdResult::message() const {
219
273
return internal_->message ;
220
274
}
221
275
276
+ const ResponseInfo& AdResult::response_info () const {
277
+ FIREBASE_ASSERT (response_info_ != nullptr );
278
+ return *response_info_;
279
+ }
280
+
222
281
// / Returns a log friendly string version of this object.
223
282
const std::string& AdResult::ToString () const {
224
283
FIREBASE_ASSERT (internal_);
225
284
return internal_->to_string ;
226
285
}
227
286
228
- // / Protected method used by LoadAdResult, a subclass which constructs its
229
- // / to_string representation programatically after the AdResult construction.
230
- void AdResult::set_to_string (std::string to_string) {
231
- FIREBASE_ASSERT (internal_);
232
- internal_->to_string = to_string;
233
- }
234
287
} // namespace admob
235
288
} // namespace firebase
0 commit comments