Skip to content

Commit 841d9b7

Browse files
smilesa-maurice
authored andcommitted
Remove casting of firebase::App platform specific data.
Replaced friend class access to App with GetPlatformApp() that allows access to the platform specific app implementation on iOS and Android. PiperOrigin-RevId: 265533440
1 parent e9ed94b commit 841d9b7

File tree

17 files changed

+92
-72
lines changed

17 files changed

+92
-72
lines changed

app/src/app_android.cc

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ namespace firebase {
3232

3333
DEFINE_FIREBASE_VERSION_STRING(Firebase);
3434

35+
namespace internal {
36+
37+
JOBJECT_REFERENCE(AppInternal);
38+
39+
} // namespace internal
40+
3541
// Namespace and class for FirebaseApp.
3642
// java/com/google/android/gmscore/*/client/firebase-common/src/com/google/\
3743
// firebase/FirebaseApp.java
@@ -413,15 +419,13 @@ AppOptions* AppOptions::LoadDefault(AppOptions* app_options,
413419
return app_options;
414420
}
415421

416-
App::App() : activity_(nullptr), data_(nullptr) {}
422+
App::App() : activity_(nullptr), internal_(nullptr) {}
417423

418424
App::~App() {
419425
app_common::RemoveApp(this);
420426
JNIEnv* env = GetJNIEnv();
421-
if (data_) {
422-
env->DeleteGlobalRef(reinterpret_cast<jobject>(data_));
423-
data_ = nullptr;
424-
}
427+
delete internal_;
428+
internal_ = nullptr;
425429
if (activity_) {
426430
env->DeleteGlobalRef(reinterpret_cast<jobject>(activity_));
427431
activity_ = nullptr;
@@ -466,11 +470,9 @@ App* App::Create(const AppOptions& options, const char* name, JNIEnv* jni_env,
466470
app = new App();
467471
app->name_ = name;
468472
app->activity_ = jni_env->NewGlobalRef(activity);
469-
jint result = jni_env->GetJavaVM(&app->java_vm_);
470-
FIREBASE_ASSERT(result == JNI_OK);
471473
GetAppOptionsFromPlatformApp(jni_env, platform_app, &app->options_);
472-
app->data_ = static_cast<void*>(jni_env->NewGlobalRef(platform_app));
473-
jni_env->DeleteLocalRef(platform_app);
474+
app->internal_ = new internal::AppInternal(
475+
internal::AppInternal::FromLocalReference(jni_env, platform_app));
474476
app = app_common::AddApp(app, &app->init_results_);
475477
} else {
476478
ReleaseClasses(jni_env);
@@ -526,8 +528,7 @@ void App::SetDataCollectionDefaultEnabled(bool enabled) {
526528
return;
527529
}
528530
JNIEnv* env = GetJNIEnv();
529-
jobject obj = reinterpret_cast<jobject>(data_);
530-
env->CallVoidMethod(obj,
531+
env->CallVoidMethod(**internal_,
531532
app::GetMethodId(app::kSetDataCollectionDefaultEnabled),
532533
enabled ? JNI_TRUE : JNI_FALSE);
533534
util::CheckAndClearJniExceptions(env);
@@ -540,13 +541,16 @@ bool App::IsDataCollectionDefaultEnabled() const {
540541
return true;
541542
}
542543
JNIEnv* env = GetJNIEnv();
543-
jobject obj = reinterpret_cast<jobject>(data_);
544544
jboolean result = env->CallBooleanMethod(
545-
obj, app::GetMethodId(app::kIsDataCollectionDefaultEnabled));
545+
**internal_, app::GetMethodId(app::kIsDataCollectionDefaultEnabled));
546546
util::CheckAndClearJniExceptions(env);
547547
return result != JNI_FALSE;
548548
}
549549

550550
const char* App::GetUserAgent() { return app_common::GetUserAgent(); }
551551

552+
JavaVM* App::java_vm() const { return internal_->java_vm(); }
553+
554+
jobject App::GetPlatformApp() const { return internal_->GetLocalRef(); }
555+
552556
} // namespace firebase

app/src/app_desktop.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ namespace firebase {
2929
DEFINE_FIREBASE_VERSION_STRING(Firebase);
3030

3131
namespace internal {
32-
struct PrivateAppData {
32+
33+
class AppInternal {
34+
public:
3335
// A registry that modules can use to expose functions to each other, without
3436
// requiring a linkage dependency.
3537
// todo - make all the implementations use something like this, for internal
@@ -115,14 +117,13 @@ AppOptions* AppOptions::LoadDefault(AppOptions* options) {
115117
}
116118

117119
App::App() {
118-
data_ = new internal::PrivateAppData();
120+
internal_ = new internal::AppInternal();
119121
}
120122

121123
App::~App() {
122124
app_common::RemoveApp(this);
123-
// Once we use data_, we should delete it here.
124-
delete static_cast<internal::PrivateAppData*>(data_);
125-
data_ = nullptr;
125+
delete internal_;
126+
internal_ = nullptr;
126127
}
127128

128129
// On desktop, if you create an app with no arguments, it will try to
@@ -164,7 +165,7 @@ App* App::GetInstance(const char* name) { // NOLINT
164165

165166
#ifdef INTERNAL_EXPERIMENTAL
166167
internal::FunctionRegistry* App::function_registry() {
167-
return &(static_cast<internal::PrivateAppData*>(data_)->function_registry);
168+
return &internal_->function_registry;
168169
}
169170
#endif
170171

app/src/app_ios.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ namespace internal {
2626
OBJ_C_PTR_WRAPPER_NAMED(AppInternal, FIRApp);
2727
} // namespace internal
2828

29-
typedef internal::AppInternal FIRAppPointer;
30-
3129
} // namespace firebase
3230

3331
#endif // FIREBASE_APP_CLIENT_CPP_SRC_APP_IOS_H_

app/src/app_ios.mm

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,12 @@ static void PlatformOptionsToAppOptions(FIROptions* platform_options,
220220
return app_options;
221221
}
222222

223-
App::App() : data_(nullptr) {}
223+
App::App() : internal_(nullptr) {}
224224

225225
App::~App() {
226226
app_common::RemoveApp(this);
227-
if (data_) {
228-
delete static_cast<internal::AppInternal*>(data_);
229-
data_ = nullptr;
230-
}
227+
delete internal_;
228+
internal_ = nullptr;
231229
}
232230

233231
App* App::Create() {
@@ -249,7 +247,7 @@ static void PlatformOptionsToAppOptions(FIROptions* platform_options,
249247
app = new App();
250248
app->options_ = options;
251249
app->name_ = name;
252-
app->data_ = new internal::AppInternal(platform_app);
250+
app->internal_ = new internal::AppInternal(platform_app);
253251
app_common::AddApp(app, &app->init_results_);
254252
}
255253
return app;
@@ -272,13 +270,15 @@ static void PlatformOptionsToAppOptions(FIROptions* platform_options,
272270
void App::SetDefaultConfigPath(const char* path) { }
273271

274272
void App::SetDataCollectionDefaultEnabled(bool enabled) {
275-
(**static_cast<internal::AppInternal*>(data_)).dataCollectionDefaultEnabled =
276-
(enabled ? YES : NO);
273+
GetPlatformApp().dataCollectionDefaultEnabled = (enabled ? YES : NO);
277274
}
278275

279276
bool App::IsDataCollectionDefaultEnabled() const {
280-
return (**static_cast<internal::AppInternal*>(data_)).isDataCollectionDefaultEnabled ?
281-
true : false;
277+
return GetPlatformApp().isDataCollectionDefaultEnabled ? true : false;
278+
}
279+
280+
FIRApp* App::GetPlatformApp() const {
281+
return internal_->get();
282282
}
283283

284284
} // namespace firebase

app/src/include/firebase/app.h

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@
2222
#if FIREBASE_PLATFORM_ANDROID
2323
#include <jni.h>
2424
#endif // FIREBASE_PLATFORM_ANDROID
25+
2526
#include <map>
2627
#include <string>
2728

29+
#if FIREBASE_PLATFORM_IOS
30+
#ifdef __OBJC__
31+
@class FIRApp;
32+
#endif // __OBJC__
33+
#endif // FIREBASE_PLATFORM_IOS
34+
2835
/// @brief Namespace that encompasses all Firebase APIs.
2936
#if !defined(FIREBASE_NAMESPACE)
3037
#define FIREBASE_NAMESPACE firebase
@@ -36,6 +43,7 @@ namespace FIREBASE_NAMESPACE {
3643
#ifdef INTERNAL_EXPERIMENTAL
3744
namespace internal {
3845
class FunctionRegistry;
46+
class AppInternal;
3947
} // namespace internal
4048
#endif // INTERNAL_EXPERIMENTAL
4149

@@ -594,7 +602,7 @@ class App {
594602
/// @note This method is specific to the Android implementation.
595603
///
596604
/// @return JNI Java virtual machine object.
597-
JavaVM* java_vm() const { return java_vm_; }
605+
JavaVM* java_vm() const;
598606
/// Get JNI environment, needed for performing JNI calls, set on creation.
599607
/// This is not trivial as the correct environment needs to retrieved per
600608
/// thread.
@@ -725,19 +733,24 @@ class App {
725733
static void SetDefaultConfigPath(const char* path);
726734
#endif // INTERNAL_EXPERIMENTAL
727735

728-
private:
729-
/// @cond FIREBASE_APP_INTERNAL
730-
friend class auth::Auth;
731-
friend class crashlytics::internal::CrashlyticsInternal;
732-
friend class database::internal::DatabaseInternal;
733736
#ifdef INTERNAL_EXPERIMENTAL
734-
friend class firestore::FirestoreInternal;
737+
#if FIREBASE_PLATFORM_ANDROID
738+
/// Get the platform specific app implementation referenced by this object.
739+
///
740+
/// @return Global reference to the FirebaseApp. The returned reference
741+
/// most be deleted after use.
742+
jobject GetPlatformApp() const;
743+
#elif FIREBASE_PLATFORM_IOS
744+
#ifdef __OBJC__
745+
/// Get the platform specific app implementation referenced by this object.
746+
///
747+
/// @return Reference to the FIRApp object owned by this app.
748+
FIRApp* GetPlatformApp() const;
749+
#endif // __OBJC__
750+
#endif // FIREBASE_PLATFORM_ANDROID, FIREBASE_PLATFORM_IOS
735751
#endif // INTERNAL_EXPERIMENTAL
736-
friend class functions::internal::FunctionsInternal;
737-
friend class instance_id::InstanceId;
738-
friend class internal::InstanceId;
739-
friend class storage::internal::StorageInternal;
740752

753+
private:
741754
/// Construct the object.
742755
App();
743756

@@ -746,10 +759,6 @@ class App {
746759
// Unity doesn't need the JNI from here, it has its method to access JNI.
747760
// </SWIG>
748761
#if FIREBASE_PLATFORM_ANDROID || defined(DOXYGEN)
749-
/// JNI reference to the Java virtual machine associated with the App's
750-
/// process.
751-
/// @note This is specific to Android.
752-
JavaVM* java_vm_;
753762
/// Android activity.
754763
/// @note This is specific to Android.
755764
jobject activity_;
@@ -763,7 +772,7 @@ class App {
763772
/// Module initialization results.
764773
std::map<std::string, InitResult> init_results_;
765774
/// Pointer to other internal data used by this instance.
766-
void* data_;
775+
internal::AppInternal* internal_;
767776

768777
/// @endcond
769778
};

auth/src/android/auth_android.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,10 @@ static void ReleaseClasses(JNIEnv* env) {
186186
ReleaseCommonClasses(env);
187187
}
188188

189-
void* CreatePlatformAuth(App* app, void* app_impl) {
190-
FIREBASE_ASSERT(app_impl != nullptr);
191-
189+
void* CreatePlatformAuth(App* app) {
192190
// Grab varous java objects from the app.
193191
JNIEnv* env = app->GetJNIEnv();
194192
jobject activity = app->activity();
195-
jobject j_app = reinterpret_cast<jobject>(app_impl);
196193

197194
// Cache the JNI method ids so we only have to look them up by name once.
198195
if (!g_initialized_count) {
@@ -218,9 +215,11 @@ void* CreatePlatformAuth(App* app, void* app_impl) {
218215
g_initialized_count++;
219216

220217
// Create the FirebaseAuth class in Java.
218+
jobject platform_app = app->GetPlatformApp();
221219
jobject j_auth_impl = env->CallStaticObjectMethod(
222-
auth::GetClass(), auth::GetMethodId(auth::kGetInstance), j_app);
223-
assert(env->ExceptionCheck() == false);
220+
auth::GetClass(), auth::GetMethodId(auth::kGetInstance), platform_app);
221+
FIREBASE_ASSERT(!util::CheckAndClearJniExceptions(env));
222+
env->DeleteLocalRef(platform_app);
224223

225224
// Ensure the reference hangs around.
226225
void* auth_impl = nullptr;

auth/src/auth.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Auth* Auth::GetAuth(App* app, InitResult* init_result_out) {
7878
FIREBASE_UTIL_RETURN_NULL_IF_GOOGLE_PLAY_UNAVAILABLE(*app, init_result_out);
7979

8080
// Create the platform dependent version of Auth.
81-
void* auth_impl = CreatePlatformAuth(app, app->data_);
81+
void* auth_impl = CreatePlatformAuth(app);
8282
if (!auth_impl) return nullptr;
8383

8484
// Create a new Auth and initialize.

auth/src/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum CredentialApiFunction {
3131
};
3232

3333
// Platform-specific method to create the wrapped Auth class.
34-
void* CreatePlatformAuth(App* app, void* app_impl);
34+
void* CreatePlatformAuth(App* app);
3535

3636
// Platform-specific method to initialize AuthData.
3737
void InitPlatformAuth(AuthData* auth_data);

auth/src/desktop/auth_desktop.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Future<ResultT> DoSignInWithCredential(Promise<ResultT> promise,
8282

8383
} // namespace
8484

85-
void* CreatePlatformAuth(App* const app, void* const /*app_impl*/) {
85+
void* CreatePlatformAuth(App* const app) {
8686
FIREBASE_ASSERT_RETURN(nullptr, app);
8787

8888
AuthImpl* const auth = new AuthImpl();

auth/src/ios/auth_ios.mm

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,9 @@ explicit ListenerHandleHolder(T handle) : handle(handle) {}
124124
};
125125

126126
// Platform-specific method to create the wrapped Auth class.
127-
void *CreatePlatformAuth(App *app, void *app_impl) {
128-
FIRApp *fir_app = static_cast<FIRAppPointer *>(app_impl)->get();
129-
127+
void *CreatePlatformAuth(App *app) {
130128
// Grab the auth for our app.
131-
FIRAuth *auth = [FIRAuth authWithApp:fir_app];
129+
FIRAuth *auth = [FIRAuth authWithApp:app->GetPlatformApp()];
132130
FIREBASE_ASSERT(auth != nullptr);
133131

134132
// Create a FIRAuth* that uses Objective-C's automatic reference counting.

0 commit comments

Comments
 (0)