Skip to content

Commit 781454c

Browse files
authored
Heartbeat logging passthrough for Auth on Mobile Platforms (#846)
When GetAuth is called, guarantee that the underlying native getter for Auth is also called so that any usage logging related to the native SDK is triggered. * Minor changes based on review feedback * Minor syntax fixes for android and ios LogHeartbeat * Fix related to visibility of auth_data_ in LogHeartbeat
1 parent d100e4f commit 781454c

File tree

6 files changed

+28
-0
lines changed

6 files changed

+28
-0
lines changed

auth/src/android/auth_android.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,17 @@ void Auth::DestroyPlatformAuth(AuthData* auth_data) {
309309
}
310310
}
311311

312+
void LogHeartbeat(Auth* auth) {
313+
// Calling the native getter is sufficient to cause a Heartbeat to be logged.
314+
JNIEnv* env = Env(auth->auth_data_);
315+
jobject platform_app = auth->app().GetPlatformApp();
316+
jobject j_auth_impl = env->CallStaticObjectMethod(
317+
auth::GetClass(), auth::GetMethodId(auth::kGetInstance), platform_app);
318+
util::CheckAndClearJniExceptions(env);
319+
env->DeleteLocalRef(j_auth_impl);
320+
env->DeleteLocalRef(platform_app);
321+
}
322+
312323
JNIEXPORT void JNICALL JniAuthStateListener_nativeOnAuthStateChanged(
313324
JNIEnv* env, jobject clazz, jlong callback_data) {
314325
AuthData* auth_data = reinterpret_cast<AuthData*>(callback_data);

auth/src/auth.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ Auth* Auth::GetAuth(App* app, InitResult* init_result_out) {
6868
Auth* existing_auth = FindAuth(app);
6969
if (existing_auth) {
7070
if (init_result_out != nullptr) *init_result_out = kInitResultSuccess;
71+
// Log heartbeat data when instance getters are called.
72+
// See go/firebase-platform-logging-design for more information.
73+
LogHeartbeat(existing_auth);
7174
return existing_auth;
7275
}
7376

auth/src/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ void InitPlatformAuth(AuthData* auth_data);
3939
// Platform-specific method to destroy the wrapped Auth class.
4040
void DestroyPlatformAuth(AuthData* auth_data);
4141

42+
// Platform-specific method that causes a heartbeat to be logged.
43+
// See go/firebase-platform-logging-design for more information.
44+
void LogHeartbeat(Auth* auth);
45+
4246
// All the result functions are similar.
4347
// Just return the local Future, cast to the proper result type.
4448
#define AUTH_RESULT_FN(class_name, fn_name, result_type) \

auth/src/desktop/auth_desktop.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ void* CreatePlatformAuth(App* const app) {
9494
void InitializeFunctionRegistryListener(AuthData* auth_data);
9595
void DestroyFunctionRegistryListener(AuthData* auth_data);
9696

97+
// TODO(b/211006737): This is a stub until desktop implementation supports
98+
// heartbeat logging.
99+
void LogHeartbeat(Auth* const auth) {}
100+
97101
IdTokenRefreshListener::IdTokenRefreshListener() : token_timestamp_(0) {}
98102

99103
IdTokenRefreshListener::~IdTokenRefreshListener() {}

auth/src/include/firebase/auth.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ class Auth {
532532
friend void EnableTokenAutoRefresh(AuthData* authData);
533533
friend void DisableTokenAutoRefresh(AuthData* authData);
534534
friend void ResetTokenRefreshCounter(AuthData* authData);
535+
friend void LogHeartbeat(Auth* auth);
535536
/// @endcond
536537

537538
// Find Auth instance using App. Return null if the instance does not exist.

auth/src/ios/auth_ios.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ void UpdateCurrentUser(AuthData *auth_data) {
223223
auth_data->auth_impl = nullptr;
224224
}
225225

226+
void LogHeartbeat(Auth *auth) {
227+
// Calling the native getter is sufficient to cause a Heartbeat to be logged.
228+
[FIRAuth authWithApp:auth->app().GetPlatformApp()];
229+
}
230+
226231
Future<Auth::FetchProvidersResult> Auth::FetchProvidersForEmail(const char *email) {
227232
// Create data structure to hold asynchronous results.
228233
FetchProvidersResult initial_data;

0 commit comments

Comments
 (0)