Skip to content

Commit a88486e

Browse files
wilhuffa-maurice
authored andcommitted
Convert Settings to the new JNI framework
PiperOrigin-RevId: 329734038
1 parent 502a386 commit a88486e

11 files changed

+191
-249
lines changed

firestore/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ set(android_SRCS
7979
src/android/field_value_android.h
8080
src/android/firebase_firestore_exception_android.cc
8181
src/android/firebase_firestore_exception_android.h
82-
src/android/firebase_firestore_settings_android.cc
83-
src/android/firebase_firestore_settings_android.h
8482
src/android/firestore_android.cc
8583
src/android/firestore_android.h
8684
src/android/geo_point_android.cc
@@ -101,6 +99,8 @@ set(android_SRCS
10199
src/android/server_timestamp_behavior_android.h
102100
src/android/set_options_android.cc
103101
src/android/set_options_android.h
102+
src/android/settings_android.cc
103+
src/android/settings_android.h
104104
src/android/snapshot_metadata_android.cc
105105
src/android/snapshot_metadata_android.h
106106
src/android/source_android.cc

firestore/src/android/firebase_firestore_settings_android.cc

Lines changed: 0 additions & 135 deletions
This file was deleted.

firestore/src/android/firebase_firestore_settings_android.h

Lines changed: 0 additions & 31 deletions
This file was deleted.

firestore/src/android/firestore_android.cc

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "firestore/src/android/field_path_android.h"
2121
#include "firestore/src/android/field_value_android.h"
2222
#include "firestore/src/android/firebase_firestore_exception_android.h"
23-
#include "firestore/src/android/firebase_firestore_settings_android.h"
2423
#include "firestore/src/android/geo_point_android.h"
2524
#include "firestore/src/android/lambda_event_listener.h"
2625
#include "firestore/src/android/lambda_transaction_function.h"
@@ -30,6 +29,7 @@
3029
#include "firestore/src/android/query_snapshot_android.h"
3130
#include "firestore/src/android/server_timestamp_behavior_android.h"
3231
#include "firestore/src/android/set_options_android.h"
32+
#include "firestore/src/android/settings_android.h"
3333
#include "firestore/src/android/snapshot_metadata_android.h"
3434
#include "firestore/src/android/source_android.h"
3535
#include "firestore/src/android/timestamp_android.h"
@@ -52,6 +52,9 @@
5252
namespace firebase {
5353
namespace firestore {
5454

55+
using jni::Env;
56+
using jni::Local;
57+
5558
const char kApiIdentifier[] = "Firestore";
5659

5760
// clang-format off
@@ -163,7 +166,6 @@ bool FirestoreInternal::Initialize(App* app) {
163166
// Call Initialize on each Firestore internal class.
164167
FieldValueInternal::Initialize(app) &&
165168
FirebaseFirestoreExceptionInternal::Initialize(app) &&
166-
FirebaseFirestoreSettingsInternal::Initialize(app) &&
167169
GeoPointInternal::Initialize(app) &&
168170
ListenerRegistrationInternal::Initialize(app) &&
169171
QueryInternal::Initialize(app) &&
@@ -200,6 +202,7 @@ bool FirestoreInternal::Initialize(App* app) {
200202
MetadataChangesInternal::Initialize(loader);
201203
ServerTimestampBehaviorInternal::Initialize(loader);
202204
SetOptionsInternal::Initialize(loader);
205+
SettingsInternal::Initialize(loader);
203206
SnapshotMetadataInternal::Initialize(loader);
204207
SourceInternal::Initialize(loader);
205208
if (!loader.ok()) {
@@ -251,7 +254,6 @@ void FirestoreInternal::ReleaseClasses(App* app) {
251254
EventListenerInternal::Terminate(app);
252255
FieldValueInternal::Terminate(app);
253256
FirebaseFirestoreExceptionInternal::Terminate(app);
254-
FirebaseFirestoreSettingsInternal::Terminate(app);
255257
GeoPointInternal::Terminate(app);
256258
ListenerRegistrationInternal::Terminate(app);
257259
QueryInternal::Terminate(app);
@@ -363,27 +365,20 @@ Query FirestoreInternal::CollectionGroup(const char* collection_id) const {
363365
}
364366

365367
Settings FirestoreInternal::settings() const {
366-
JNIEnv* env = app_->GetJNIEnv();
367-
jobject settings = env->CallObjectMethod(
368+
Env env = GetEnv();
369+
Local<SettingsInternal> settings = env.Call<SettingsInternal>(
368370
obj_, firebase_firestore::GetMethodId(firebase_firestore::kGetSettings));
369-
FIREBASE_ASSERT(settings != nullptr);
371+
FIREBASE_ASSERT(settings);
370372

371-
Settings result =
372-
FirebaseFirestoreSettingsInternal::JavaSettingToSetting(env, settings);
373-
env->DeleteLocalRef(settings);
374-
CheckAndClearJniExceptions(env);
375-
return result;
373+
return settings.ToPublic(env);
376374
}
377375

378376
void FirestoreInternal::set_settings(Settings settings) {
379-
JNIEnv* env = app_->GetJNIEnv();
380-
jobject settings_jobj =
381-
FirebaseFirestoreSettingsInternal::SettingToJavaSetting(env, settings);
382-
env->CallVoidMethod(
377+
Env env = GetEnv();
378+
auto java_settings = SettingsInternal::Create(env, settings);
379+
env.Call<void>(
383380
obj_, firebase_firestore::GetMethodId(firebase_firestore::kSetSettings),
384-
settings_jobj);
385-
env->DeleteLocalRef(settings_jobj);
386-
CheckAndClearJniExceptions(env);
381+
java_settings);
387382
}
388383

389384
WriteBatch FirestoreInternal::batch() const {
@@ -556,6 +551,15 @@ void FirestoreInternal::UnregisterListenerRegistration(
556551
}
557552
}
558553

554+
jni::Env FirestoreInternal::GetEnv() const {
555+
// TODO(mcg): Investigate whether or not this method can be made static.
556+
// This will depend on whether or not we need to flag the current Firestore
557+
// instance as somehow broken in response to an exception.
558+
jni::Env env;
559+
env.SetUnhandledExceptionHandler(GlobalUnhandledExceptionHandler, nullptr);
560+
return env;
561+
}
562+
559563
CollectionReference FirestoreInternal::NewCollectionReference(
560564
jni::Env& env, const jni::Object& reference) {
561565
if (!env.ok() || !reference) return {};

firestore/src/android/firestore_android.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ class FirestoreInternal {
120120
void UnregisterListenerRegistration(
121121
ListenerRegistrationInternal* registration);
122122

123+
jni::Env GetEnv() const;
124+
123125
CollectionReference NewCollectionReference(jni::Env& env,
124126
const jni::Object& reference);
125127
DocumentReference NewDocumentReference(jni::Env& env,
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "firestore/src/android/settings_android.h"
2+
3+
#include "firestore/src/jni/env.h"
4+
#include "firestore/src/jni/loader.h"
5+
6+
namespace firebase {
7+
namespace firestore {
8+
namespace {
9+
10+
using jni::Constructor;
11+
using jni::Env;
12+
using jni::Local;
13+
using jni::Method;
14+
using jni::Object;
15+
using jni::String;
16+
17+
// class FirebaseFirestoreSettings.Builder
18+
constexpr char kSettingsBuilderClass[] = PROGUARD_KEEP_CLASS
19+
"com/google/firebase/firestore/FirebaseFirestoreSettings$Builder";
20+
Constructor<Object> kNewBuilder("()V");
21+
Method<Object> kSetHost(
22+
"setHost",
23+
"(Ljava/lang/String;)"
24+
"Lcom/google/firebase/firestore/FirebaseFirestoreSettings$Builder;");
25+
Method<Object> kSetSslEnabled(
26+
"setSslEnabled",
27+
"(Z)"
28+
"Lcom/google/firebase/firestore/FirebaseFirestoreSettings$Builder;");
29+
Method<Object> kSetPersistenceEnabled(
30+
"setPersistenceEnabled",
31+
"(Z)"
32+
"Lcom/google/firebase/firestore/FirebaseFirestoreSettings$Builder;");
33+
Method<SettingsInternal> kBuild(
34+
"build", "()Lcom/google/firebase/firestore/FirebaseFirestoreSettings;");
35+
36+
// class FirebaseFirestoreSettings
37+
constexpr char kSettingsClass[] = PROGUARD_KEEP_CLASS
38+
"com/google/firebase/firestore/FirebaseFirestoreSettings";
39+
Method<String> kGetHost("getHost", "()Ljava/lang/String;");
40+
Method<bool> kIsSslEnabled("isSslEnabled", "()Z");
41+
Method<bool> kIsPersistenceEnabled("isPersistenceEnabled", "()Z");
42+
43+
} // namespace
44+
45+
void SettingsInternal::Initialize(jni::Loader& loader) {
46+
loader.LoadClass(kSettingsBuilderClass, kNewBuilder, kSetHost, kSetSslEnabled,
47+
kSetPersistenceEnabled, kBuild);
48+
49+
loader.LoadClass(kSettingsClass, kGetHost, kIsSslEnabled,
50+
kIsPersistenceEnabled);
51+
}
52+
53+
Local<SettingsInternal> SettingsInternal::Create(Env& env,
54+
const Settings& settings) {
55+
Local<Object> builder = env.New(kNewBuilder);
56+
57+
Local<String> host = env.NewStringUtf(settings.host());
58+
builder = env.Call(builder, kSetHost, host);
59+
60+
builder = env.Call(builder, kSetSslEnabled, settings.is_ssl_enabled());
61+
62+
builder = env.Call(builder, kSetPersistenceEnabled,
63+
settings.is_persistence_enabled());
64+
65+
return env.Call(builder, kBuild);
66+
}
67+
68+
Settings SettingsInternal::ToPublic(Env& env) const {
69+
Settings result;
70+
71+
// Set host
72+
Local<String> host = env.Call(*this, kGetHost);
73+
result.set_host(host.ToString(env));
74+
75+
// Set SSL enabled
76+
bool ssl_enabled = env.Call(*this, kIsSslEnabled);
77+
result.set_ssl_enabled(ssl_enabled);
78+
79+
// Set Persistence enabled
80+
bool persistence_enabled = env.Call(*this, kIsPersistenceEnabled);
81+
result.set_persistence_enabled(persistence_enabled);
82+
83+
return result;
84+
}
85+
86+
} // namespace firestore
87+
} // namespace firebase

0 commit comments

Comments
 (0)