@@ -190,7 +190,7 @@ struct AppData {
190190// Tracks library registrations.
191191class LibraryRegistry {
192192 private:
193- LibraryRegistry () {}
193+ LibraryRegistry () : is_common_library_registered( false ) {}
194194
195195 public:
196196 // Register a library, returns true if the library version changed.
@@ -252,16 +252,30 @@ class LibraryRegistry {
252252 }
253253 }
254254
255+ static bool IsCommonLibraryRegistered () {
256+ if (library_registry_) {
257+ return library_registry_->is_common_library_registered ;
258+ }
259+ return false ;
260+ }
261+
262+ static void SetCommonLibraryRegistered () {
263+ if (library_registry_) {
264+ library_registry_->is_common_library_registered = true ;
265+ }
266+ }
267+
255268 private:
256269 std::map<std::string, std::string> library_to_version_;
257270 std::string user_agent_;
271+ bool is_common_library_registered;
258272
259273 static LibraryRegistry* library_registry_;
260274};
261275
262276// Guards g_apps and g_default_app.
263277static Mutex* g_app_mutex = new Mutex();
264- static std::map<std::string, UniquePtr<AppData>>* g_apps;
278+ static std::map<std::string, UniquePtr<AppData>>* g_apps = nullptr ;
265279static App* g_default_app = nullptr ;
266280LibraryRegistry* LibraryRegistry::library_registry_ = nullptr ;
267281
@@ -294,21 +308,6 @@ App* AddApp(App* app, std::map<std::string, InitResult>* results) {
294308 app_options.storage_bucket (), app_options.project_id (),
295309 static_cast <int >(reinterpret_cast <intptr_t >(app)));
296310 }
297- LibraryRegistry::Initialize ();
298- if (created_first_app) {
299- // This calls the platform specific method to propagate the registration to
300- // any SDKs in use by this library.
301- App::RegisterLibrary (FIREBASE_CPP_USER_AGENT_PREFIX,
302- FIREBASE_VERSION_NUMBER_STRING);
303- App::RegisterLibrary (FIREBASE_CPP_USER_AGENT_PREFIX " -os" ,
304- kOperatingSystem );
305- App::RegisterLibrary (FIREBASE_CPP_USER_AGENT_PREFIX " -arch" ,
306- kCpuArchitecture );
307- App::RegisterLibrary (FIREBASE_CPP_USER_AGENT_PREFIX " -stl" ,
308- kCppRuntimeOrStl );
309- App::RegisterLibrary (FIREBASE_CPP_USER_AGENT_PREFIX " -buildsrc" ,
310- kBuildSource );
311- }
312311 callback::Initialize ();
313312 AppCallback::NotifyAllAppCreated (app, results);
314313 return app;
@@ -433,6 +432,28 @@ void RegisterLibrariesFromUserAgent(const char* user_agent) {
433432 if (changed) registry->UpdateUserAgent ();
434433}
435434
435+ void RegisterSdkUsage (void * platform_resource) {
436+ MutexLock lock (*g_app_mutex);
437+
438+ // Only register libraries when no C++ apps was created before.
439+ if (!LibraryRegistry::IsCommonLibraryRegistered ()) {
440+ LibraryRegistry::Initialize ();
441+ // This calls the platform specific method to propagate the registration to
442+ // any SDKs in use by this library.
443+ App::RegisterLibrary (FIREBASE_CPP_USER_AGENT_PREFIX,
444+ FIREBASE_VERSION_NUMBER_STRING, platform_resource);
445+ App::RegisterLibrary (FIREBASE_CPP_USER_AGENT_PREFIX " -os" , kOperatingSystem ,
446+ platform_resource);
447+ App::RegisterLibrary (FIREBASE_CPP_USER_AGENT_PREFIX " -arch" ,
448+ kCpuArchitecture , platform_resource);
449+ App::RegisterLibrary (FIREBASE_CPP_USER_AGENT_PREFIX " -stl" ,
450+ kCppRuntimeOrStl , platform_resource);
451+ App::RegisterLibrary (FIREBASE_CPP_USER_AGENT_PREFIX " -buildsrc" ,
452+ kBuildSource , platform_resource);
453+ LibraryRegistry::SetCommonLibraryRegistered ();
454+ }
455+ }
456+
436457const char * GetUserAgent () {
437458 MutexLock lock (*g_app_mutex);
438459 return LibraryRegistry::Initialize ()->GetUserAgent ();
0 commit comments