@@ -190,7 +190,7 @@ struct AppData {
190
190
// Tracks library registrations.
191
191
class LibraryRegistry {
192
192
private:
193
- LibraryRegistry () {}
193
+ LibraryRegistry () : is_common_library_registered( false ) {}
194
194
195
195
public:
196
196
// Register a library, returns true if the library version changed.
@@ -252,16 +252,30 @@ class LibraryRegistry {
252
252
}
253
253
}
254
254
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
+
255
268
private:
256
269
std::map<std::string, std::string> library_to_version_;
257
270
std::string user_agent_;
271
+ bool is_common_library_registered;
258
272
259
273
static LibraryRegistry* library_registry_;
260
274
};
261
275
262
276
// Guards g_apps and g_default_app.
263
277
static 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 ;
265
279
static App* g_default_app = nullptr ;
266
280
LibraryRegistry* LibraryRegistry::library_registry_ = nullptr ;
267
281
@@ -294,21 +308,6 @@ App* AddApp(App* app, std::map<std::string, InitResult>* results) {
294
308
app_options.storage_bucket (), app_options.project_id (),
295
309
static_cast <int >(reinterpret_cast <intptr_t >(app)));
296
310
}
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
- }
312
311
callback::Initialize ();
313
312
AppCallback::NotifyAllAppCreated (app, results);
314
313
return app;
@@ -433,6 +432,28 @@ void RegisterLibrariesFromUserAgent(const char* user_agent) {
433
432
if (changed) registry->UpdateUserAgent ();
434
433
}
435
434
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
+
436
457
const char * GetUserAgent () {
437
458
MutexLock lock (*g_app_mutex);
438
459
return LibraryRegistry::Initialize ()->GetUserAgent ();
0 commit comments