13
13
#include " app/src/cleanup_notifier.h"
14
14
#include " app/src/future_manager.h"
15
15
#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"
18
17
#include " firestore/src/include/firebase/firestore/collection_reference.h"
19
18
#include " firestore/src/include/firebase/firestore/document_reference.h"
20
19
#include " firestore/src/include/firebase/firestore/settings.h"
20
+ #include " firestore/src/jni/env.h"
21
21
#include " firestore/src/jni/jni_fwd.h"
22
+ #include " firestore/src/jni/object.h"
23
+ #include " firestore/src/jni/ownership.h"
22
24
23
25
namespace firebase {
24
26
namespace firestore {
@@ -29,31 +31,34 @@ class Transaction;
29
31
class TransactionFunction ;
30
32
class WriteBatch ;
31
33
34
+ template <typename PublicType, typename InternalType, typename FnEnumType>
35
+ class Promise ;
36
+
32
37
// Used for registering global callbacks. See
33
38
// firebase::util::RegisterCallbackOnTask for context.
34
39
extern const char kApiIdentifier [];
35
40
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
-
50
41
// This is the Android implementation of Firestore. Cannot inherit WrapperFuture
51
42
// as a valid FirestoreInternal is required to construct a WrapperFuture. So we
52
43
// will implement the Future APIs on the fly.
53
44
class FirestoreInternal {
54
45
public:
55
46
using ApiType = Firestore;
56
47
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
+
57
62
// Note: call `set_firestore_public` immediately after construction.
58
63
explicit FirestoreInternal (App* app);
59
64
~FirestoreInternal ();
@@ -120,18 +125,15 @@ class FirestoreInternal {
120
125
void UnregisterListenerRegistration (
121
126
ListenerRegistrationInternal* registration);
122
127
123
- jni::Env GetEnv () const ;
128
+ static jni::Env GetEnv ();
124
129
125
- CollectionReference NewCollectionReference (jni::Env& env,
126
- const jni::Object& reference);
130
+ CollectionReference NewCollectionReference (
131
+ jni::Env& env, const jni::Object& reference) const ;
127
132
DocumentReference NewDocumentReference (jni::Env& env,
128
- const jni::Object& reference);
133
+ const jni::Object& reference) const ;
129
134
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 ;
135
137
136
138
// The constructor explicit Foo(FooInternal*) is protected in public API.
137
139
// But we want it to be public-usable in internal implementation code
@@ -159,7 +161,9 @@ class FirestoreInternal {
159
161
Firestore* firestore_public () { return firestore_public_; }
160
162
const Firestore* firestore_public () const { return firestore_public_; }
161
163
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
+ }
163
167
164
168
static void SetClientLanguage (const std::string& language_token);
165
169
@@ -170,25 +174,40 @@ class FirestoreInternal {
170
174
return future_manager_.GetFutureApi (this );
171
175
}
172
176
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);
174
193
175
194
static bool Initialize (App* app);
176
195
static void ReleaseClasses (App* app);
177
196
static void Terminate (App* app);
178
197
179
198
// Initialize classes loaded from embedded files.
180
- static bool InitializeEmbeddedClasses (App* app);
199
+ static bool InitializeEmbeddedClasses (App* app, jni::Loader& loader );
181
200
182
201
static Mutex init_mutex_;
183
202
static int initialize_count_;
184
203
static jni::Loader* loader_;
185
204
186
- jobject user_callback_executor_;
205
+ jni::Global<jni::Object> user_callback_executor_;
187
206
188
207
App* app_ = nullptr ;
189
208
Firestore* firestore_public_ = nullptr ;
190
209
// Java Firestore global ref.
191
- jobject obj_;
210
+ jni::Global<jni::Object> obj_;
192
211
193
212
Mutex listener_registration_mutex_; // For registering listener-registrations
194
213
#if defined(_STLPORT_VERSION)
0 commit comments