Skip to content

Commit f2105db

Browse files
wilhuffa-maurice
authored andcommitted
Convert FirestoreInternal to the new JNI framework
This converts all method invocations but does not necessarily convert all uses of older APIs because those are still needed to support classes that have not yet converted. PiperOrigin-RevId: 333429091
1 parent e7363ce commit f2105db

12 files changed

+263
-351
lines changed

firestore/src/android/document_reference_android.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ ListenerRegistration DocumentReferenceInternal::AddSnapshotListener(
174174
env.Call(obj_, kAddSnapshotListener, firestore_->user_callback_executor(),
175175
java_metadata, java_listener);
176176

177-
return firestore_->NewListenerRegistration(
178-
env, listener, passing_listener_ownership, java_registration);
177+
if (!env.ok() || !java_registration) return {};
178+
return ListenerRegistration(new ListenerRegistrationInternal(
179+
firestore_, listener, passing_listener_ownership,
180+
java_registration.get()));
179181
}
180182

181183
Class DocumentReferenceInternal::GetClass() { return Class(clazz); }

firestore/src/android/event_listener_android.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ jobject EventListenerInternal::EventListenerToJavaEventListener(
155155
return result;
156156
}
157157

158+
Local<Object> EventListenerInternal::Create(Env& env,
159+
EventListener<void>* listener) {
160+
return Local<Object>(env.get(),
161+
EventListenerToJavaRunnable(env.get(), listener));
162+
}
163+
158164
/* static */
159165
jobject EventListenerInternal::EventListenerToJavaRunnable(
160166
JNIEnv* env, EventListener<void>* listener) {

firestore/src/android/event_listener_android.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class EventListenerInternal {
4141
JNIEnv* env, FirestoreInternal* firestore,
4242
EventListener<QuerySnapshot>* listener);
4343

44+
static jni::Local<jni::Object> Create(jni::Env& env,
45+
EventListener<void>* listener);
46+
4447
static jobject EventListenerToJavaRunnable(JNIEnv* env,
4548
EventListener<void>* listener);
4649

firestore/src/android/firestore_android.cc

Lines changed: 181 additions & 315 deletions
Large diffs are not rendered by default.

firestore/src/android/firestore_android.h

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
#include "app/src/cleanup_notifier.h"
1414
#include "app/src/future_manager.h"
1515
#include "app/src/include/firebase/app.h"
16-
#include "app/src/util_android.h"
17-
#include "firestore/src/android/listener_registration_android.h"
16+
#include "firestore/src/common/type_mapping.h"
1817
#include "firestore/src/include/firebase/firestore/collection_reference.h"
1918
#include "firestore/src/include/firebase/firestore/document_reference.h"
2019
#include "firestore/src/include/firebase/firestore/settings.h"
20+
#include "firestore/src/jni/env.h"
2121
#include "firestore/src/jni/jni_fwd.h"
22+
#include "firestore/src/jni/object.h"
23+
#include "firestore/src/jni/ownership.h"
2224

2325
namespace firebase {
2426
namespace firestore {
@@ -29,31 +31,34 @@ class Transaction;
2931
class TransactionFunction;
3032
class WriteBatch;
3133

34+
template <typename PublicType, typename InternalType, typename FnEnumType>
35+
class Promise;
36+
3237
// Used for registering global callbacks. See
3338
// firebase::util::RegisterCallbackOnTask for context.
3439
extern const char kApiIdentifier[];
3540

36-
// Each API of Firestore that returns a Future needs to define an enum
37-
// value here. For example, a Future-returning method Foo() relies on the enum
38-
// value kFoo. The enum values are used to identify and manage Future in the
39-
// Firestore Future manager.
40-
enum class FirestoreFn {
41-
kEnableNetwork = 0,
42-
kDisableNetwork,
43-
kRunTransaction,
44-
kTerminate,
45-
kWaitForPendingWrites,
46-
kClearPersistence,
47-
kCount, // Must be the last enum value.
48-
};
49-
5041
// This is the Android implementation of Firestore. Cannot inherit WrapperFuture
5142
// as a valid FirestoreInternal is required to construct a WrapperFuture. So we
5243
// will implement the Future APIs on the fly.
5344
class FirestoreInternal {
5445
public:
5546
using ApiType = Firestore;
5647

48+
// Each API of Firestore that returns a Future needs to define an enum
49+
// value here. For example, a Future-returning method Foo() relies on the enum
50+
// value kFoo. The enum values are used to identify and manage Future in the
51+
// Firestore Future manager.
52+
enum class AsyncFn {
53+
kEnableNetwork = 0,
54+
kDisableNetwork,
55+
kRunTransaction,
56+
kTerminate,
57+
kWaitForPendingWrites,
58+
kClearPersistence,
59+
kCount, // Must be the last enum value.
60+
};
61+
5762
// Note: call `set_firestore_public` immediately after construction.
5863
explicit FirestoreInternal(App* app);
5964
~FirestoreInternal();
@@ -120,18 +125,15 @@ class FirestoreInternal {
120125
void UnregisterListenerRegistration(
121126
ListenerRegistrationInternal* registration);
122127

123-
jni::Env GetEnv() const;
128+
static jni::Env GetEnv();
124129

125-
CollectionReference NewCollectionReference(jni::Env& env,
126-
const jni::Object& reference);
130+
CollectionReference NewCollectionReference(
131+
jni::Env& env, const jni::Object& reference) const;
127132
DocumentReference NewDocumentReference(jni::Env& env,
128-
const jni::Object& reference);
133+
const jni::Object& reference) const;
129134
DocumentSnapshot NewDocumentSnapshot(jni::Env& env,
130-
const jni::Object& snapshot);
131-
ListenerRegistration NewListenerRegistration(
132-
jni::Env& env, EventListener<DocumentSnapshot>* listener,
133-
bool passing_listener_ownership, const jni::Object& registration);
134-
Query NewQuery(jni::Env& env, const jni::Object& query);
135+
const jni::Object& snapshot) const;
136+
Query NewQuery(jni::Env& env, const jni::Object& query) const;
135137

136138
// The constructor explicit Foo(FooInternal*) is protected in public API.
137139
// But we want it to be public-usable in internal implementation code
@@ -159,7 +161,9 @@ class FirestoreInternal {
159161
Firestore* firestore_public() { return firestore_public_; }
160162
const Firestore* firestore_public() const { return firestore_public_; }
161163

162-
jobject user_callback_executor() const { return user_callback_executor_; }
164+
const jni::Global<jni::Object>& user_callback_executor() const {
165+
return user_callback_executor_;
166+
}
163167

164168
static void SetClientLanguage(const std::string& language_token);
165169

@@ -170,25 +174,40 @@ class FirestoreInternal {
170174
return future_manager_.GetFutureApi(this);
171175
}
172176

173-
void ShutdownUserCallbackExecutor();
177+
FirestoreInternal* mutable_this() const {
178+
return const_cast<FirestoreInternal*>(this);
179+
}
180+
181+
template <typename PublicT = void, typename InternalT = InternalType<PublicT>>
182+
Future<PublicT> NewFuture(jni::Env& env, AsyncFn op,
183+
const jni::Object& task) const {
184+
if (!env.ok()) return {};
185+
186+
FirestoreInternal* self = mutable_this();
187+
Promise<PublicT, InternalT, AsyncFn> promise(self->ref_future(), self);
188+
promise.RegisterForTask(env, op, task);
189+
return promise.GetFuture();
190+
}
191+
192+
void ShutdownUserCallbackExecutor(jni::Env& env);
174193

175194
static bool Initialize(App* app);
176195
static void ReleaseClasses(App* app);
177196
static void Terminate(App* app);
178197

179198
// Initialize classes loaded from embedded files.
180-
static bool InitializeEmbeddedClasses(App* app);
199+
static bool InitializeEmbeddedClasses(App* app, jni::Loader& loader);
181200

182201
static Mutex init_mutex_;
183202
static int initialize_count_;
184203
static jni::Loader* loader_;
185204

186-
jobject user_callback_executor_;
205+
jni::Global<jni::Object> user_callback_executor_;
187206

188207
App* app_ = nullptr;
189208
Firestore* firestore_public_ = nullptr;
190209
// Java Firestore global ref.
191-
jobject obj_;
210+
jni::Global<jni::Object> obj_;
192211

193212
Mutex listener_registration_mutex_; // For registering listener-registrations
194213
#if defined(_STLPORT_VERSION)

firestore/src/android/lambda_transaction_function.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace firestore {
2525
*/
2626
class LambdaTransactionFunction
2727
: public TransactionFunction,
28-
public Promise<void, void, FirestoreFn>::Completion<void> {
28+
public Promise<void, void, FirestoreInternal::AsyncFn>::Completion<void> {
2929
public:
3030
LambdaTransactionFunction(
3131
std::function<Error(Transaction&, std::string&)> update)

firestore/src/android/query_android.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "firestore/src/android/field_value_android.h"
1010
#include "firestore/src/android/firestore_android.h"
1111
#include "firestore/src/android/lambda_event_listener.h"
12+
#include "firestore/src/android/listener_registration_android.h"
1213
#include "firestore/src/android/metadata_changes_android.h"
1314
#include "firestore/src/android/promise_android.h"
1415
#include "firestore/src/android/source_android.h"

firestore/src/android/transaction_android.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ bool TransactionInternal::CheckAndClearJniExceptions() {
192192
return result;
193193
}
194194

195+
Local<Object> TransactionInternal::Create(Env& env,
196+
FirestoreInternal* firestore,
197+
TransactionFunction* function) {
198+
return Local<Object>(env.get(), ToJavaObject(env.get(), firestore, function));
199+
}
200+
195201
/* static */
196202
jobject TransactionInternal::ToJavaObject(JNIEnv* env,
197203
FirestoreInternal* firestore,

firestore/src/android/transaction_android.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class TransactionInternal : public Wrapper {
3939
DocumentSnapshot Get(const DocumentReference& document, Error* error_code,
4040
std::string* error_message);
4141

42+
static jni::Local<jni::Object> Create(jni::Env& env,
43+
FirestoreInternal* firestore,
44+
TransactionFunction* function);
45+
4246
static jobject ToJavaObject(JNIEnv* env, FirestoreInternal* firestore,
4347
TransactionFunction* function);
4448

firestore/src/common/wrapper_assertions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#if defined(__ANDROID__)
88
#include <jni.h>
99

10+
#include "app/src/util_android.h"
1011
#include "firestore/src/android/firestore_android.h"
1112
#elif defined(FIRESTORE_STUB_BUILD)
1213
#include "firestore/src/stub/firestore_stub.h"

0 commit comments

Comments
 (0)