Skip to content

Commit c03d1cd

Browse files
authored
Fix user-agent register timing in Unity SDK (#635)
* Fix user-agent register timing in Unity SDK Make sure the user-agents are registered before C++ App is created.
1 parent eca1869 commit c03d1cd

File tree

1 file changed

+60
-19
lines changed

1 file changed

+60
-19
lines changed

app/src/swig/app.i

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
#if defined(__ANDROID__)
5353
#include "app/src/google_play_services/availability_android.h"
54+
#include "app/src/app_android.h"
5455
#endif // defined(__ANDROID__)
5556

5657
namespace firebase {
@@ -167,6 +168,35 @@ static void LogHeartbeatForDesktop(App* app) {
167168
#endif // FIREBASE_PLATFORM_DESKTOP
168169
}
169170

171+
static void RegisterLibrariesHelper(
172+
const std::map<std::string, std::string>& libraries) {
173+
#if FIREBASE_PLATFORM_ANDROID
174+
JNIEnv* jni_env;
175+
jobject activity_local_ref = UnityGetActivity(&jni_env);
176+
firebase::CallAfterEnsureMethodsCached(
177+
jni_env, activity_local_ref, [&libraries, &jni_env](){
178+
for (std::map<std::string, std::string>::const_iterator it =
179+
libraries.begin(); it != libraries.end(); ++it) {
180+
const std::string& library = it->first;
181+
const std::string& version = it->second;
182+
firebase::App::RegisterLibrary(library.c_str(),
183+
version.c_str(),
184+
jni_env);
185+
}
186+
});
187+
jni_env->DeleteLocalRef(activity_local_ref);
188+
#else
189+
for (std::map<std::string, std::string>::const_iterator it =
190+
libraries.begin(); it != libraries.end(); ++it) {
191+
const std::string& library = it->first;
192+
const std::string& version = it->second;
193+
firebase::App::RegisterLibrary(library.c_str(),
194+
version.c_str(),
195+
nullptr);
196+
}
197+
#endif
198+
}
199+
170200
// Decrease the reference count for the app. When the reference count reaches
171201
// 0, the App will be deleted.
172202
static void AppReleaseReference(App* app) {
@@ -821,6 +851,8 @@ static firebase::AppOptions* AppOptionsLoadFromJsonConfig(const char* config) {
821851
// Whether Crashlytics initialization has been attempted.
822852
private static bool crashlyticsInitializationAttempted = false;
823853

854+
private static bool userAgentRegistered = false;
855+
824856
// Instantiates a C++ App and returns a reference to the C# proxy.
825857
// existingProxy is required here to prevent the finalizer being executed
826858
// while we're creating a new FirebaseApp proxy.
@@ -832,7 +864,28 @@ static firebase::AppOptions* AppOptionsLoadFromJsonConfig(const char* config) {
832864
Firebase.Platform.FirebaseHandler.Create(
833865
Firebase.Platform.FirebaseAppUtils.Instance);
834866
FirebaseApp newProxy;
867+
835868
lock (nameToProxy) {
869+
// If this is the first App, register library information.
870+
if (!userAgentRegistered) {
871+
userAgentRegistered = true;
872+
var userAgentMap = new StringStringMap();
873+
874+
// fire-(unity|mono)/<sdk_version>
875+
var libraryPrefix = "fire-" +
876+
Firebase.Platform.PlatformInformation.RuntimeName;
877+
userAgentMap[libraryPrefix] =
878+
Firebase.VersionInfo.SdkVersion;
879+
// fire-(unity|mono)-ver/<unity|mono_version>
880+
userAgentMap[libraryPrefix + "-ver"] =
881+
Firebase.Platform.PlatformInformation.RuntimeVersion;
882+
// fire-(unity|mono)/<github-action-built|custom_built>
883+
userAgentMap[libraryPrefix + "-buildsrc"] =
884+
Firebase.VersionInfo.BuildSource;
885+
886+
RegisterLibrariesInternal(userAgentMap);
887+
}
888+
836889
InitializeAppUtilCallbacks();
837890
var cPtrHandleRef = new System.Runtime.InteropServices.HandleRef(
838891
null, System.IntPtr.Zero);
@@ -903,22 +956,10 @@ static firebase::AppOptions* AppOptionsLoadFromJsonConfig(const char* config) {
903956
return existingProxyForNewApp;
904957
}
905958
}
906-
// If this is the first App, register library information.
907-
if (cPtrToProxy.Count == 0) {
908-
// fire-(unity|mono)/<sdk_version>
909-
var libraryPrefix = "fire-" +
910-
Firebase.Platform.PlatformInformation.RuntimeName;
911-
RegisterLibraryInternal(libraryPrefix, Firebase.VersionInfo.SdkVersion);
912-
// fire-(unity|mono)-ver/<unity|mono_version>
913-
RegisterLibraryInternal(
914-
libraryPrefix + "-ver",
915-
Firebase.Platform.PlatformInformation.RuntimeVersion);
916-
// fire-(unity|mono)/<github-action-built|custom_built>
917-
RegisterLibraryInternal(
918-
libraryPrefix + "-buildsrc", Firebase.VersionInfo.BuildSource);
919-
// Log a heartbeat after all Unity user agents have been registered.
920-
LogHeartbeatInternal(newProxy);
921-
}
959+
960+
// Log a heartbeat after all Unity user agents have been registered.
961+
LogHeartbeatInternal(newProxy);
962+
922963
// Cache the name so that it can be accessed after the app is disposed.
923964
newProxy.name = newProxy.NameInternal;
924965
// By default the newly created proxy doesn't own the C++ app, take
@@ -1320,9 +1361,9 @@ namespace callback {
13201361
return firebase::GetLogLevel();
13211362
}
13221363

1323-
%csmethodmodifiers RegisterLibraryInternal() "internal";
1324-
static void RegisterLibraryInternal(const char* library, const char* version) {
1325-
firebase::App::RegisterLibrary(library, version);
1364+
%csmethodmodifiers RegisterLibrariesInternal() "internal";
1365+
static void RegisterLibrariesInternal(std::map<std::string, std::string> libraries) {
1366+
firebase::RegisterLibrariesHelper(libraries);
13261367
}
13271368

13281369
%csmethodmodifiers LogHeartbeatInternal(App* app) "internal";

0 commit comments

Comments
 (0)