Skip to content

Commit 1ff5a40

Browse files
wilhuffa-maurice
authored andcommitted
Convert QuerySnapshot to the new JNI framework
PiperOrigin-RevId: 330111277
1 parent a13be5b commit 1ff5a40

File tree

4 files changed

+35
-64
lines changed

4 files changed

+35
-64
lines changed

firestore/src/android/firestore_android.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ bool FirestoreInternal::Initialize(App* app) {
169169
GeoPointInternal::Initialize(app) &&
170170
ListenerRegistrationInternal::Initialize(app) &&
171171
QueryInternal::Initialize(app) &&
172-
QuerySnapshotInternal::Initialize(app) &&
173172
TransactionInternal::Initialize(app) && Wrapper::Initialize(app) &&
174173
// Initialize those embedded Firestore internal classes.
175174
InitializeEmbeddedClasses(app))) {
@@ -198,6 +197,7 @@ bool FirestoreInternal::Initialize(App* app) {
198197
DocumentSnapshotInternal::Initialize(loader);
199198
FieldPathConverter::Initialize(loader);
200199
MetadataChangesInternal::Initialize(loader);
200+
QuerySnapshotInternal::Initialize(loader);
201201
ServerTimestampBehaviorInternal::Initialize(loader);
202202
SetOptionsInternal::Initialize(loader);
203203
SettingsInternal::Initialize(loader);
@@ -257,7 +257,6 @@ void FirestoreInternal::ReleaseClasses(App* app) {
257257
GeoPointInternal::Terminate(app);
258258
ListenerRegistrationInternal::Terminate(app);
259259
QueryInternal::Terminate(app);
260-
QuerySnapshotInternal::Terminate(app);
261260
TransactionInternal::Terminate(app);
262261
Wrapper::Terminate(app);
263262
}
@@ -589,6 +588,11 @@ ListenerRegistration FirestoreInternal::NewListenerRegistration(
589588
this, listener, passing_listener_ownership, registration.get()));
590589
}
591590

591+
Query FirestoreInternal::NewQuery(jni::Env& env, const jni::Object& query) {
592+
if (!env.ok() || !query) return {};
593+
return Query(new QueryInternal(this, query.get()));
594+
}
595+
592596
/* static */
593597
void Firestore::set_log_level(LogLevel log_level) {
594598
// "Verbose" and "debug" map to logging enabled.

firestore/src/android/firestore_android.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class FirestoreInternal {
131131
ListenerRegistration NewListenerRegistration(
132132
jni::Env& env, EventListener<DocumentSnapshot>* listener,
133133
bool passing_listener_ownership, const jni::Object& registration);
134+
Query NewQuery(jni::Env& env, const jni::Object& query);
134135

135136
// The constructor explicit Foo(FooInternal*) is protected in public API.
136137
// But we want it to be public-usable in internal implementation code

firestore/src/android/query_snapshot_android.cc

Lines changed: 25 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
#include <jni.h>
44

55
#include "app/src/assert.h"
6-
#include "app/src/util_android.h"
76
#include "firestore/src/android/document_change_android.h"
87
#include "firestore/src/android/document_snapshot_android.h"
98
#include "firestore/src/android/metadata_changes_android.h"
109
#include "firestore/src/android/query_android.h"
1110
#include "firestore/src/android/snapshot_metadata_android.h"
12-
#include "firestore/src/android/util_android.h"
1311
#include "firestore/src/jni/env.h"
12+
#include "firestore/src/jni/loader.h"
1413

1514
namespace firebase {
1615
namespace firestore {
@@ -19,86 +18,56 @@ namespace {
1918
using jni::Env;
2019
using jni::List;
2120
using jni::Local;
21+
using jni::Method;
2222
using jni::Object;
2323

24-
} // namespace
24+
constexpr char kClassName[] =
25+
PROGUARD_KEEP_CLASS "com/google/firebase/firestore/QuerySnapshot";
26+
Method<Object> kGetQuery("getQuery", "()Lcom/google/firebase/firestore/Query;");
27+
Method<SnapshotMetadataInternal> kGetMetadata(
28+
"getMetadata", "()Lcom/google/firebase/firestore/SnapshotMetadata;");
29+
Method<List> kGetDocumentChanges(
30+
"getDocumentChanges",
31+
"(Lcom/google/firebase/firestore/MetadataChanges;)Ljava/util/List;");
32+
Method<List> kGetDocuments("getDocuments", "()Ljava/util/List;");
33+
Method<size_t> kSize("size", "()I");
2534

26-
// clang-format off
27-
#define QUERY_SNAPSHOT_METHODS(X) \
28-
X(Query, "getQuery", "()Lcom/google/firebase/firestore/Query;"), \
29-
X(Metadata, "getMetadata", \
30-
"()Lcom/google/firebase/firestore/SnapshotMetadata;"), \
31-
X(DocumentChanges, "getDocumentChanges", \
32-
"(Lcom/google/firebase/firestore/MetadataChanges;)Ljava/util/List;"), \
33-
X(Documents, "getDocuments", "()Ljava/util/List;"), \
34-
X(Size, "size", "()I")
35-
// clang-format on
35+
} // namespace
3636

37-
METHOD_LOOKUP_DECLARATION(query_snapshot, QUERY_SNAPSHOT_METHODS)
38-
METHOD_LOOKUP_DEFINITION(query_snapshot,
39-
PROGUARD_KEEP_CLASS
40-
"com/google/firebase/firestore/QuerySnapshot",
41-
QUERY_SNAPSHOT_METHODS)
37+
void QuerySnapshotInternal::Initialize(jni::Loader& loader) {
38+
loader.LoadClass(kClassName, kGetQuery, kGetMetadata, kGetDocumentChanges,
39+
kGetDocuments, kSize);
40+
}
4241

4342
Query QuerySnapshotInternal::query() const {
44-
JNIEnv* env = firestore_->app()->GetJNIEnv();
45-
jobject query = env->CallObjectMethod(
46-
obj_, query_snapshot::GetMethodId(query_snapshot::kQuery));
47-
QueryInternal* internal = new QueryInternal{firestore_, query};
48-
env->DeleteLocalRef(query);
49-
50-
CheckAndClearJniExceptions(env);
51-
return Query{internal};
43+
Env env = GetEnv();
44+
Local<Object> query = env.Call(obj_, kGetQuery);
45+
return firestore_->NewQuery(env, query);
5246
}
5347

5448
SnapshotMetadata QuerySnapshotInternal::metadata() const {
5549
Env env = GetEnv();
56-
auto java_metadata = env.Call<SnapshotMetadataInternal>(
57-
obj_, query_snapshot::GetMethodId(query_snapshot::kMetadata));
58-
return java_metadata.ToPublic(env);
50+
return env.Call(obj_, kGetMetadata).ToPublic(env);
5951
}
6052

6153
std::vector<DocumentChange> QuerySnapshotInternal::DocumentChanges(
6254
MetadataChanges metadata_changes) const {
6355
Env env = GetEnv();
6456
auto java_metadata = MetadataChangesInternal::Create(env, metadata_changes);
6557

66-
Local<List> change_list = env.Call<List>(
67-
obj_, query_snapshot::GetMethodId(query_snapshot::kDocumentChanges),
68-
java_metadata);
58+
Local<List> change_list = env.Call(obj_, kGetDocumentChanges, java_metadata);
6959
return MakeVector<DocumentChange>(env, change_list);
7060
}
7161

7262
std::vector<DocumentSnapshot> QuerySnapshotInternal::documents() const {
7363
Env env = GetEnv();
74-
Local<List> document_list = env.Call<List>(
75-
obj_, query_snapshot::GetMethodId(query_snapshot::kDocuments));
64+
Local<List> document_list = env.Call(obj_, kGetDocuments);
7665
return MakeVector<DocumentSnapshot>(env, document_list);
7766
}
7867

7968
std::size_t QuerySnapshotInternal::size() const {
80-
JNIEnv* env = firestore_->app()->GetJNIEnv();
81-
jint result = env->CallIntMethod(
82-
obj_, query_snapshot::GetMethodId(query_snapshot::kSize));
83-
84-
CheckAndClearJniExceptions(env);
85-
return static_cast<std::size_t>(result);
86-
}
87-
88-
/* static */
89-
bool QuerySnapshotInternal::Initialize(App* app) {
90-
JNIEnv* env = app->GetJNIEnv();
91-
jobject activity = app->activity();
92-
bool result = query_snapshot::CacheMethodIds(env, activity);
93-
util::CheckAndClearJniExceptions(env);
94-
return result;
95-
}
96-
97-
/* static */
98-
void QuerySnapshotInternal::Terminate(App* app) {
99-
JNIEnv* env = app->GetJNIEnv();
100-
query_snapshot::ReleaseClass(env);
101-
util::CheckAndClearJniExceptions(env);
69+
Env env = GetEnv();
70+
return env.Call(obj_, kSize);
10271
}
10372

10473
} // namespace firestore

firestore/src/android/query_snapshot_android.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "firestore/src/include/firebase/firestore/query.h"
1111
#include "firestore/src/include/firebase/firestore/query_snapshot.h"
1212
#include "firestore/src/include/firebase/firestore/snapshot_metadata.h"
13+
#include "firestore/src/jni/jni_fwd.h"
1314

1415
namespace firebase {
1516
namespace firestore {
@@ -19,6 +20,8 @@ class QuerySnapshotInternal : public Wrapper {
1920
using ApiType = QuerySnapshot;
2021
using Wrapper::Wrapper;
2122

23+
static void Initialize(jni::Loader& loader);
24+
2225
/**
2326
* @brief The query from which you get this QuerySnapshot.
2427
*/
@@ -59,12 +62,6 @@ class QuerySnapshotInternal : public Wrapper {
5962
* @return The number of documents in the QuerySnapshot.
6063
*/
6164
std::size_t size() const;
62-
63-
private:
64-
friend class FirestoreInternal;
65-
66-
static bool Initialize(App* app);
67-
static void Terminate(App* app);
6865
};
6966

7067
} // namespace firestore

0 commit comments

Comments
 (0)