55#include < utility>
66
77#include " app/meta/move.h"
8- #include " app/src/embedded_file.h"
98#include " app/src/include/firebase/internal/common.h"
10- #include " app/src/util_android.h"
119#include " firestore/src/android/document_reference_android.h"
1210#include " firestore/src/android/exception_android.h"
1311#include " firestore/src/android/field_path_android.h"
1412#include " firestore/src/android/field_value_android.h"
1513#include " firestore/src/android/set_options_android.h"
1614#include " firestore/src/jni/env.h"
1715#include " firestore/src/jni/hash_map.h"
16+ #include " firestore/src/jni/loader.h"
1817
1918namespace firebase {
2019namespace firestore {
2120namespace {
2221
22+ using jni::Constructor;
2323using jni::Env;
2424using jni::HashMap;
2525using jni::Local;
26+ using jni::Method;
2627using jni::Object;
2728using jni::Throwable;
2829
30+ constexpr char kTransactionClassName [] =
31+ PROGUARD_KEEP_CLASS " com/google/firebase/firestore/Transaction" ;
32+ Method<Object> kSet (
33+ " set" ,
34+ " (Lcom/google/firebase/firestore/DocumentReference;Ljava/lang/Object;"
35+ " Lcom/google/firebase/firestore/SetOptions;)"
36+ " Lcom/google/firebase/firestore/Transaction;" );
37+ Method<Object> kUpdate (
38+ " update" ,
39+ " (Lcom/google/firebase/firestore/DocumentReference;Ljava/util/Map;)"
40+ " Lcom/google/firebase/firestore/Transaction;" );
41+ Method<Object> kUpdateVarargs (
42+ " update" ,
43+ " (Lcom/google/firebase/firestore/DocumentReference;"
44+ " Lcom/google/firebase/firestore/FieldPath;Ljava/lang/Object;"
45+ " [Ljava/lang/Object;)Lcom/google/firebase/firestore/Transaction;" );
46+ Method<Object> kDelete (" delete" ,
47+ " (Lcom/google/firebase/firestore/DocumentReference;)"
48+ " Lcom/google/firebase/firestore/Transaction;" );
49+ Method<Object> kGet (" get" ,
50+ " (Lcom/google/firebase/firestore/DocumentReference;)"
51+ " Lcom/google/firebase/firestore/DocumentSnapshot;" );
52+
53+ constexpr char kTransactionFunctionClassName [] = PROGUARD_KEEP_CLASS
54+ " com/google/firebase/firestore/internal/cpp/TransactionFunction" ;
55+
56+ Constructor<Object> kNewTransactionFunction (" (JJ)V" );
57+
2958} // namespace
3059
31- // clang-format off
32- #define TRANSACTION_METHODS (X ) \
33- X (Set, " set" , \
34- " (Lcom/google/firebase/firestore/DocumentReference;Ljava/lang/Object;" \
35- " Lcom/google/firebase/firestore/SetOptions;)" \
36- " Lcom/google/firebase/firestore/Transaction;" ), \
37- X (Update, " update" , \
38- " (Lcom/google/firebase/firestore/DocumentReference;Ljava/util/Map;)" \
39- " Lcom/google/firebase/firestore/Transaction;" ), \
40- X (UpdateVarargs, " update" , \
41- " (Lcom/google/firebase/firestore/DocumentReference;" \
42- " Lcom/google/firebase/firestore/FieldPath;Ljava/lang/Object;" \
43- " [Ljava/lang/Object;)Lcom/google/firebase/firestore/Transaction;" ), \
44- X (Delete, " delete" , " (Lcom/google/firebase/firestore/DocumentReference;)" \
45- " Lcom/google/firebase/firestore/Transaction;" ), \
46- X (Get, " get" , " (Lcom/google/firebase/firestore/DocumentReference;)" \
47- " Lcom/google/firebase/firestore/DocumentSnapshot;" )
48- // clang-format on
49-
50- METHOD_LOOKUP_DECLARATION (transaction, TRANSACTION_METHODS)
51- METHOD_LOOKUP_DEFINITION (transaction,
52- PROGUARD_KEEP_CLASS
53- " com/google/firebase/firestore/Transaction" ,
54- TRANSACTION_METHODS)
55-
56- #define TRANSACTION_FUNCTION_METHODS (X ) X(Constructor, " <init>" , " (JJ)V" )
57- METHOD_LOOKUP_DECLARATION (transaction_function, TRANSACTION_FUNCTION_METHODS)
58- METHOD_LOOKUP_DEFINITION (
59- transaction_function,
60- PROGUARD_KEEP_CLASS
61- " com/google/firebase/firestore/internal/cpp/TransactionFunction" ,
62- TRANSACTION_FUNCTION_METHODS)
60+ void TransactionInternal::Initialize (jni::Loader& loader) {
61+ loader.LoadClass (kTransactionClassName , kSet , kUpdate , kUpdateVarargs ,
62+ kDelete , kGet );
63+
64+ static const JNINativeMethod kTransactionFunctionNatives [] = {
65+ {" nativeApply" ,
66+ " (JJLcom/google/firebase/firestore/Transaction;)Ljava/lang/Exception;" ,
67+ reinterpret_cast <void *>(
68+ &TransactionInternal::TransactionFunctionNativeApply)}};
69+ loader.LoadClass (kTransactionFunctionClassName , kNewTransactionFunction );
70+ loader.RegisterNatives (kTransactionFunctionNatives ,
71+ FIREBASE_ARRAYSIZE (kTransactionFunctionNatives ));
72+ }
6373
6474void TransactionInternal::Set (const DocumentReference& document,
6575 const MapFieldValue& data,
6676 const SetOptions& options) {
6777 Env env = GetEnv ();
6878 Local<HashMap> java_data = MakeJavaMap (env, data);
6979 Local<Object> java_options = SetOptionsInternal::Create (env, options);
70- env.Call <Object>(obj_, transaction::GetMethodId (transaction::kSet ),
71- document.internal_ ->ToJava (), java_data, java_options);
80+ env.Call (obj_, kSet , document.internal_ ->ToJava (), java_data, java_options);
7281}
7382
7483void TransactionInternal::Update (const DocumentReference& document,
7584 const MapFieldValue& data) {
7685 Env env = GetEnv ();
7786 Local<HashMap> java_data = MakeJavaMap (env, data);
78- env.Call <Object>(obj_, transaction::GetMethodId (transaction::kUpdate ),
79- document.internal_ ->ToJava (), java_data);
87+ env.Call (obj_, kUpdate , document.internal_ ->ToJava (), java_data);
8088}
8189
8290void TransactionInternal::Update (const DocumentReference& document,
@@ -88,25 +96,21 @@ void TransactionInternal::Update(const DocumentReference& document,
8896
8997 Env env = GetEnv ();
9098 UpdateFieldPathArgs args = MakeUpdateFieldPathArgs (env, data);
91- env.Call <Object>(obj_, transaction::GetMethodId (transaction::kUpdateVarargs ),
92- document.internal_ ->ToJava (), args.first_field ,
93- args.first_value , args.varargs );
99+ env.Call (obj_, kUpdateVarargs , document.internal_ ->ToJava (), args.first_field ,
100+ args.first_value , args.varargs );
94101}
95102
96103void TransactionInternal::Delete (const DocumentReference& document) {
97104 Env env = GetEnv ();
98- env.Call <Object>(obj_, transaction::GetMethodId (transaction::kDelete ),
99- document.internal_ ->ToJava ());
105+ env.Call (obj_, kDelete , document.internal_ ->ToJava ());
100106}
101107
102108DocumentSnapshot TransactionInternal::Get (const DocumentReference& document,
103109 Error* error_code,
104110 std::string* error_message) {
105111 Env env = GetEnv ();
106112
107- Local<Object> snapshot =
108- env.Call <Object>(obj_, transaction::GetMethodId (transaction::kGet ),
109- document.internal_ ->ToJava ());
113+ Local<Object> snapshot = env.Call (obj_, kGet , document.internal_ ->ToJava ());
110114 Local<Throwable> exception = env.ClearExceptionOccurred ();
111115
112116 if (exception) {
@@ -172,22 +176,10 @@ Local<Throwable> TransactionInternal::ClearExceptionOccurred() {
172176Local<Object> TransactionInternal::Create (Env& env,
173177 FirestoreInternal* firestore,
174178 TransactionFunction* function) {
175- return Local<Object>(env.get (), ToJavaObject (env.get (), firestore, function));
176- }
177-
178- /* static */
179- jobject TransactionInternal::ToJavaObject (JNIEnv* env,
180- FirestoreInternal* firestore,
181- TransactionFunction* function) {
182- jobject result = env->NewObject (
183- transaction_function::GetClass (),
184- transaction_function::GetMethodId (transaction_function::kConstructor ),
185- reinterpret_cast <jlong>(firestore), reinterpret_cast <jlong>(function));
186- util::CheckAndClearJniExceptions (env);
187- return result;
179+ return env.New (kNewTransactionFunction , reinterpret_cast <jlong>(firestore),
180+ reinterpret_cast <jlong>(function));
188181}
189182
190- /* static */
191183jobject TransactionInternal::TransactionFunctionNativeApply (
192184 JNIEnv* raw_env, jclass clazz, jlong firestore_ptr,
193185 jlong transaction_function_ptr, jobject java_transaction) {
@@ -200,7 +192,7 @@ jobject TransactionInternal::TransactionFunctionNativeApply(
200192 TransactionFunction* transaction_function =
201193 reinterpret_cast <TransactionFunction*>(transaction_function_ptr);
202194
203- Transaction transaction (new TransactionInternal{ firestore, java_transaction} );
195+ Transaction transaction (new TransactionInternal ( firestore, java_transaction) );
204196
205197 std::string message;
206198 Error code = transaction_function->Apply (transaction, message);
@@ -216,42 +208,5 @@ jobject TransactionInternal::TransactionFunctionNativeApply(
216208 }
217209}
218210
219- /* static */
220- bool TransactionInternal::Initialize (App* app) {
221- JNIEnv* env = app->GetJNIEnv ();
222- jobject activity = app->activity ();
223- bool result = transaction::CacheMethodIds (env, activity);
224- util::CheckAndClearJniExceptions (env);
225- return result;
226- }
227-
228- /* static */
229- bool TransactionInternal::InitializeEmbeddedClasses (
230- App* app, const std::vector<internal::EmbeddedFile>* embedded_files) {
231- static const JNINativeMethod kTransactionFunctionNatives [] = {
232- {" nativeApply" ,
233- " (JJLcom/google/firebase/firestore/Transaction;)Ljava/lang/Exception;" ,
234- reinterpret_cast <void *>(
235- &TransactionInternal::TransactionFunctionNativeApply)}};
236- JNIEnv* env = app->GetJNIEnv ();
237- jobject activity = app->activity ();
238- bool result = transaction_function::CacheClassFromFiles (env, activity,
239- embedded_files) &&
240- transaction_function::CacheMethodIds (env, activity) &&
241- transaction_function::RegisterNatives (
242- env, kTransactionFunctionNatives ,
243- FIREBASE_ARRAYSIZE (kTransactionFunctionNatives ));
244- util::CheckAndClearJniExceptions (env);
245- return result;
246- }
247-
248- /* static */
249- void TransactionInternal::Terminate (App* app) {
250- JNIEnv* env = app->GetJNIEnv ();
251- transaction::ReleaseClass (env);
252- transaction_function::ReleaseClass (env);
253- util::CheckAndClearJniExceptions (env);
254- }
255-
256211} // namespace firestore
257212} // namespace firebase
0 commit comments