From cc82d1a75bad4e1a019e77dd7511afb0a2e9a24c Mon Sep 17 00:00:00 2001 From: austinbenoit Date: Tue, 24 Nov 2026 04:52:36 +0000 Subject: [PATCH 1/3] App (Desktop): register product user agents when an App is created On desktop only Firestore registered a product library, and only when the first Firestore instance was created, after the App had logged the day's heartbeat. Heartbeats therefore did not list the products in use. Let each module's AppCallback carry a library name and version, and register them in AppCallback::NotifyAllAppCreated(), before the heartbeat is logged. Add libraries for App Check, Auth, Database, Firestore, Functions, Remote Config and Storage. A callback that has been disabled does not register its library. The Unity SDK links every module into one library and disables the modules an application does not use, so this keeps unused modules out of the user agent. --- app/src/util.cc | 14 +++++++ app/src/util.h | 67 ++++++++++++++++++++++++++++++- app/tests/app_desktop_test.cc | 59 +++++++++++++++++++++++++++ app_check/src/common/app_check.cc | 2 + auth/src/auth.cc | 2 + database/src/common/database.cc | 2 + firestore/src/common/firestore.cc | 20 +++++++++ functions/src/common/functions.cc | 2 + release_build_files/readme.md | 6 +++ remote_config/src/common.cc | 3 ++ storage/src/common/storage.cc | 2 + 11 files changed, 178 insertions(+), 1 deletion(-) diff --git a/app/src/util.cc b/app/src/util.cc index d74fb83d08..d30f62d31c 100644 --- a/app/src/util.cc +++ b/app/src/util.cc @@ -30,6 +30,9 @@ #include "app/src/include/google_play_services/availability.h" #include "app/src/util_android.h" #endif // FIREBASE_PLATFORM_ANDROID +#if FIREBASE_PLATFORM_DESKTOP +#include "app/src/include/firebase/app.h" +#endif // FIREBASE_PLATFORM_DESKTOP #include "app/src/include/firebase/internal/mutex.h" #include "app/src/log.h" #include "app/src/reference_counted_future_impl.h" @@ -185,6 +188,9 @@ void AppCallback::NotifyAllAppCreated( callbacks_->begin(); it != callbacks_->end(); ++it) { const AppCallback* callback = it->second; + // Register libraries whether or not the callback is enabled, since most + // modules are not auto-initialized but should still be reported. + callback->RegisterLibrary(); if (callback->enabled()) { InitResult result = callback->NotifyAppCreated(app); if (results) (*results)[it->first] = result; @@ -192,6 +198,14 @@ void AppCallback::NotifyAllAppCreated( } } +void AppCallback::RegisterLibrary() const { +#if FIREBASE_PLATFORM_DESKTOP + if (library_registration_enabled_ && library_name_ && library_version_) { + App::RegisterLibrary(library_name_, library_version_, nullptr); + } +#endif // FIREBASE_PLATFORM_DESKTOP +} + void AppCallback::NotifyAllAppDestroyed(firebase::App* app) { MutexLock lock(*callbacks_mutex_); if (!callbacks_) return; diff --git a/app/src/util.h b/app/src/util.h index f25817a209..b0eb113830 100644 --- a/app/src/util.h +++ b/app/src/util.h @@ -117,7 +117,39 @@ class AppCallback { // a callback *before* any App instances are created otherwise it's possible // to get into a state where a module is initialized and will never be torn // down. - void set_enabled(bool enable) { enabled_ = enable; } + // + // Disabling a callback also stops its library from being registered (see + // SetLibrary()). The Unity SDK links every module into one library and + // disables the modules that an application does not use, so a disabled + // module must not report itself as in use. + void set_enabled(bool enable) { + enabled_ = enable; + library_registration_enabled_ = enable; + } + + // Set the library name and version that this module reports in the Firebase + // user agent (see App::RegisterLibrary()). The library is registered each + // time an App is created, before the App logs a heartbeat, so that heartbeats + // list every module linked into the application. + // + // This only has an effect on desktop. On Android and iOS the underlying + // platform SDKs register their own libraries. + // + // Both strings must outlive this object. + void SetLibrary(const char* name, const char* version) { + library_name_ = name; + library_version_ = version; + } + + // Calls AppCallback::SetLibrary() when constructed, so that a library can be + // set from a static initializer. Use FIREBASE_APP_REGISTER_LIBRARY() instead + // of instantiating this directly. + struct LibraryRegistration { + LibraryRegistration(AppCallback* callback, const char* name, + const char* version) { + callback->SetLibrary(name, version); + } + }; // Called by firebase::App when an instance is created. static void NotifyAllAppCreated( @@ -146,6 +178,10 @@ class AppCallback { if (destroyed_) destroyed_(app); } + // Register this module's library in the Firebase user agent, if one was set + // with SetLibrary() and the module has not been disabled. + void RegisterLibrary() const; + // Add a callback class to the list. // NOTE: This is *not* synchronized as it's not possible to use Mutex // from static constructors on some platforms. @@ -160,6 +196,12 @@ class AppCallback { Destroyed destroyed_; // Whether this callback is enabled. bool enabled_; + // Library name and version reported in the Firebase user agent, if any. + const char* library_name_ = nullptr; + const char* library_version_ = nullptr; + // Whether the library is registered when an App is created. This is true + // until the callback is explicitly disabled with set_enabled(). + bool library_registration_enabled_ = true; // Global set of callbacks. static std::map* callbacks_; @@ -204,6 +246,29 @@ class AppCallback { FIREBASE_APP_KEEP_SYMBOL = &module_name##_app_callback; \ } /* namespace firebase */ +// Register the library that a module reports in the Firebase user agent on +// desktop. See AppCallback::SetLibrary(). +// +// This must follow FIREBASE_APP_REGISTER_CALLBACKS() for the same module in +// the same source file. +// +// For example: +// FIREBASE_APP_REGISTER_LIBRARY(auth, "fire-auth", +// FIREBASE_VERSION_NUMBER_STRING); +#if FIREBASE_PLATFORM_DESKTOP +#define FIREBASE_APP_REGISTER_LIBRARY(module_name, library_name, \ + library_version) \ + namespace firebase { \ + static const ::firebase::AppCallback::LibraryRegistration \ + module_name##_library_registration(&module_name##_app_callback, \ + library_name, library_version); \ + } /* namespace firebase */ +#else +// The Android and iOS SDKs register their own libraries. +#define FIREBASE_APP_REGISTER_LIBRARY(module_name, library_name, \ + library_version) +#endif // FIREBASE_PLATFORM_DESKTOP + // Helper class to provide easy management and static access of // ReferenceCountedFutureImpls for modules. class StaticFutureData { diff --git a/app/tests/app_desktop_test.cc b/app/tests/app_desktop_test.cc index d6ace2da68..52fda8c323 100644 --- a/app/tests/app_desktop_test.cc +++ b/app/tests/app_desktop_test.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include #include "app/src/app_common.h" @@ -28,6 +29,7 @@ #include "app/src/include/firebase/app.h" #include "app/src/include/firebase/internal/platform.h" #include "app/src/include/firebase/version.h" +#include "app/src/util.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -37,6 +39,17 @@ #include #endif +// A module that reports a library, used to test that libraries are registered +// when an App is created. +FIREBASE_APP_REGISTER_CALLBACKS( + library_test_module, { return ::firebase::kInitResultSuccess; }, + { + // Nothing to tear down. + }, + false); +FIREBASE_APP_REGISTER_LIBRARY(library_test_module, "fire-library-test", + "1.2.3"); + namespace firebase { static bool RemoveDirectoryUtf8(std::string dir_utf8) { @@ -252,4 +265,50 @@ TEST(AppDesktopTest, TestLoadAppOptionsFromJsonConfigFileInInternationalPath) { EXPECT_TRUE(ChangeDirectoryUtf8(previous_directory)); EXPECT_TRUE(RemoveDirectoryUtf8(new_dir)); } + +static const char kLibraryTestModule[] = "library_test_module"; +static const char kLibraryTestUserAgent[] = "fire-library-test/1.2.3"; + +static std::unique_ptr CreateAppForLibraryTest() { + AppOptions options; + options.set_app_id("1:1234567890:android:321abc456def7890"); + options.set_api_key("fake-api-key"); + options.set_project_id("fake-project-id"); + return std::unique_ptr(App::Create(options)); +} + +TEST(AppDesktopTest, TestModuleLibraryRegisteredWhenAppCreated) { + std::unique_ptr app = CreateAppForLibraryTest(); + ASSERT_NE(app, nullptr); + + EXPECT_THAT(std::string(App::GetUserAgent()), + ::testing::HasSubstr(kLibraryTestUserAgent)); +} + +TEST(AppDesktopTest, TestModuleLibraryNotRegisteredWhenModuleDisabled) { + // The Unity SDK links every module and disables the ones that an application + // does not use. A disabled module must not be reported. + AppCallback::SetEnabledByName(kLibraryTestModule, false); + { + std::unique_ptr app = CreateAppForLibraryTest(); + EXPECT_NE(app, nullptr); + + EXPECT_THAT(std::string(App::GetUserAgent()), + ::testing::Not(::testing::HasSubstr(kLibraryTestUserAgent))); + } + // Restore the module for the other tests. + AppCallback::SetEnabledByName(kLibraryTestModule, true); +} + +TEST(AppDesktopTest, TestModuleLibraryRegisteredWhenModuleReenabled) { + // This is the sequence the Unity SDK uses before it creates an App: disable + // every module, then enable the ones that are in use. + AppCallback::SetEnabledAll(false); + AppCallback::SetEnabledByName(kLibraryTestModule, true); + std::unique_ptr app = CreateAppForLibraryTest(); + ASSERT_NE(app, nullptr); + + EXPECT_THAT(std::string(App::GetUserAgent()), + ::testing::HasSubstr(kLibraryTestUserAgent)); +} } // namespace firebase diff --git a/app_check/src/common/app_check.cc b/app_check/src/common/app_check.cc index 4e3c065488..0256fd794c 100644 --- a/app_check/src/common/app_check.cc +++ b/app_check/src/common/app_check.cc @@ -55,6 +55,8 @@ FIREBASE_APP_REGISTER_CALLBACKS( }, // App Check wants to be turned on by default true); +FIREBASE_APP_REGISTER_LIBRARY(app_check, "fire-app-check", + FIREBASE_VERSION_NUMBER_STRING); namespace firebase { namespace app_check { diff --git a/auth/src/auth.cc b/auth/src/auth.cc index 23a483178f..9794629d52 100644 --- a/auth/src/auth.cc +++ b/auth/src/auth.cc @@ -43,6 +43,8 @@ FIREBASE_APP_REGISTER_CALLBACKS( // Nothing to tear down. }, false); +FIREBASE_APP_REGISTER_LIBRARY(auth, "fire-auth", + FIREBASE_VERSION_NUMBER_STRING); namespace firebase { namespace auth { diff --git a/database/src/common/database.cc b/database/src/common/database.cc index ac17b9382c..1ec70ea180 100644 --- a/database/src/common/database.cc +++ b/database/src/common/database.cc @@ -47,6 +47,8 @@ FIREBASE_APP_REGISTER_CALLBACKS( // Nothing to tear down. }, false); +FIREBASE_APP_REGISTER_LIBRARY(database, "fire-rtdb", + FIREBASE_VERSION_NUMBER_STRING); namespace firebase { namespace database { diff --git a/firestore/src/common/firestore.cc b/firestore/src/common/firestore.cc index 805ee38519..2a11fdbefe 100644 --- a/firestore/src/common/firestore.cc +++ b/firestore/src/common/firestore.cc @@ -23,6 +23,7 @@ #include #include "app/src/cleanup_notifier.h" +#include "app/src/include/firebase/internal/platform.h" #include "app/src/include/firebase/version.h" #include "app/src/log.h" #include "app/src/util.h" @@ -41,6 +42,25 @@ #include "TargetConditionals.h" #endif // __APPLE__ +#if FIREBASE_PLATFORM_DESKTOP +#include "firebase/firestore/firestore_version.h" + +// Register Firestore as a module so that its library is in the Firebase user +// agent as soon as an App is created. Otherwise the library is only registered +// when the first Firestore instance is created (see FirestoreInternal), which +// is after the App has logged the heartbeat for the day. +FIREBASE_APP_REGISTER_CALLBACKS( + firestore, + { return ::firebase::kInitResultSuccess; }, + { + // Nothing to tear down. + }, + false); +FIREBASE_APP_REGISTER_LIBRARY(firestore, + "fire-fst", + ::firebase::firestore::kFirestoreVersionString); +#endif // FIREBASE_PLATFORM_DESKTOP + namespace firebase { namespace firestore { diff --git a/functions/src/common/functions.cc b/functions/src/common/functions.cc index df262eeac5..13db7d6b89 100644 --- a/functions/src/common/functions.cc +++ b/functions/src/common/functions.cc @@ -46,6 +46,8 @@ FIREBASE_APP_REGISTER_CALLBACKS( // Nothing to tear down. }, false); +FIREBASE_APP_REGISTER_LIBRARY(functions, "fire-fn", + FIREBASE_VERSION_NUMBER_STRING); namespace firebase { namespace functions { diff --git a/release_build_files/readme.md b/release_build_files/readme.md index 46566c6817..4f06c88526 100644 --- a/release_build_files/readme.md +++ b/release_build_files/readme.md @@ -613,6 +613,12 @@ workflow use only during the development of your app, not for publicly shipping code. ## Release Notes +### Upcoming +- Changes + - General (Desktop): Firebase products now register themselves in the + Firebase user agent when an `App` is created, so that desktop usage + is attributed to the products an application uses. + ### 14.2.0 - Changes - General (Android): Update to Firebase Android BoM version 35.2.0. diff --git a/remote_config/src/common.cc b/remote_config/src/common.cc index 7608168e8f..559c2f3b4d 100644 --- a/remote_config/src/common.cc +++ b/remote_config/src/common.cc @@ -17,6 +17,7 @@ #include #include "app/src/cleanup_notifier.h" +#include "app/src/include/firebase/version.h" #include "app/src/util.h" #include "remote_config/src/include/firebase/remote_config.h" @@ -31,6 +32,8 @@ FIREBASE_APP_REGISTER_CALLBACKS( // Nothing to do. }, false); +FIREBASE_APP_REGISTER_LIBRARY(remote_config, "fire-rc", + FIREBASE_VERSION_NUMBER_STRING); namespace firebase { namespace remote_config { diff --git a/storage/src/common/storage.cc b/storage/src/common/storage.cc index 3c995042b1..a9d5aac0fd 100644 --- a/storage/src/common/storage.cc +++ b/storage/src/common/storage.cc @@ -46,6 +46,8 @@ FIREBASE_APP_REGISTER_CALLBACKS( // Nothing to tear down. }, false); +FIREBASE_APP_REGISTER_LIBRARY(storage, "fire-gcs", + FIREBASE_VERSION_NUMBER_STRING); namespace firebase { namespace storage { From b2c6d0f5ca3caf44d3933513e051843735505fe5 Mon Sep 17 00:00:00 2001 From: austinbenoit Date: Tue, 24 Nov 2026 18:48:22 +0000 Subject: [PATCH 2/3] Guard LibraryRegistration against null callback and reset callback state in tests --- app/src/util.cc | 9 +++++++++ app/src/util.h | 12 +++++++++++- app/tests/app_desktop_test.cc | 19 +++++++++++-------- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/src/util.cc b/app/src/util.cc index d30f62d31c..5116907c97 100644 --- a/app/src/util.cc +++ b/app/src/util.cc @@ -255,6 +255,15 @@ void AppCallback::SetEnabledAll(bool enable) { } } +void AppCallback::ResetAllToDefaults() { + MutexLock lock(*callbacks_mutex_); + if (!callbacks_) return; + for (auto& entry : *callbacks_) { + entry.second->enabled_ = entry.second->enabled_by_default_; + entry.second->library_registration_enabled_ = true; + } +} + void AppCallback::AddCallback(AppCallback* callback) { if (!callbacks_) { callbacks_ = new std::map(); diff --git a/app/src/util.h b/app/src/util.h index b0eb113830..bd8c1861b4 100644 --- a/app/src/util.h +++ b/app/src/util.h @@ -102,6 +102,7 @@ class AppCallback { : module_name_(module_name), created_(created), destroyed_(destroyed), + enabled_by_default_(enabled_by_default), enabled_(enabled_by_default) { AddCallback(this); } @@ -147,7 +148,9 @@ class AppCallback { struct LibraryRegistration { LibraryRegistration(AppCallback* callback, const char* name, const char* version) { - callback->SetLibrary(name, version); + if (callback != nullptr) { + callback->SetLibrary(name, version); + } } }; @@ -167,6 +170,11 @@ class AppCallback { // Enable / disable all callbacks. static void SetEnabledAll(bool enable); + // Restore every callback to its initial enabled_by_default state and + // re-enable library registration. Intended for tests that mutate callback + // state with SetEnabledByName() or SetEnabledAll(). + static void ResetAllToDefaults(); + private: // Called by App when an instance is created. InitResult NotifyAppCreated(firebase::App* app) const { @@ -194,6 +202,8 @@ class AppCallback { Created created_; // Called when an app is about to be destroyed. Destroyed destroyed_; + // Initial value of enabled_ passed to the constructor. + const bool enabled_by_default_; // Whether this callback is enabled. bool enabled_; // Library name and version reported in the Firebase user agent, if any. diff --git a/app/tests/app_desktop_test.cc b/app/tests/app_desktop_test.cc index 52fda8c323..84effd30c1 100644 --- a/app/tests/app_desktop_test.cc +++ b/app/tests/app_desktop_test.cc @@ -277,7 +277,12 @@ static std::unique_ptr CreateAppForLibraryTest() { return std::unique_ptr(App::Create(options)); } +struct ScopedCallbackReset { + ~ScopedCallbackReset() { AppCallback::ResetAllToDefaults(); } +}; + TEST(AppDesktopTest, TestModuleLibraryRegisteredWhenAppCreated) { + EXPECT_FALSE(AppCallback::GetEnabledByName(kLibraryTestModule)); std::unique_ptr app = CreateAppForLibraryTest(); ASSERT_NE(app, nullptr); @@ -286,21 +291,19 @@ TEST(AppDesktopTest, TestModuleLibraryRegisteredWhenAppCreated) { } TEST(AppDesktopTest, TestModuleLibraryNotRegisteredWhenModuleDisabled) { + ScopedCallbackReset reset_callbacks; // The Unity SDK links every module and disables the ones that an application // does not use. A disabled module must not be reported. AppCallback::SetEnabledByName(kLibraryTestModule, false); - { - std::unique_ptr app = CreateAppForLibraryTest(); - EXPECT_NE(app, nullptr); + std::unique_ptr app = CreateAppForLibraryTest(); + ASSERT_NE(app, nullptr); - EXPECT_THAT(std::string(App::GetUserAgent()), - ::testing::Not(::testing::HasSubstr(kLibraryTestUserAgent))); - } - // Restore the module for the other tests. - AppCallback::SetEnabledByName(kLibraryTestModule, true); + EXPECT_THAT(std::string(App::GetUserAgent()), + ::testing::Not(::testing::HasSubstr(kLibraryTestUserAgent))); } TEST(AppDesktopTest, TestModuleLibraryRegisteredWhenModuleReenabled) { + ScopedCallbackReset reset_callbacks; // This is the sequence the Unity SDK uses before it creates an App: disable // every module, then enable the ones that are in use. AppCallback::SetEnabledAll(false); From 3135cc3d456d3cbd6d4aa7b8321ebf6577dc3ac9 Mon Sep 17 00:00:00 2001 From: austinbenoit Date: Wed, 25 Nov 2026 03:10:19 +0000 Subject: [PATCH 3/3] Remove release notes entry from release_build_files/readme.md --- release_build_files/readme.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/release_build_files/readme.md b/release_build_files/readme.md index 4f06c88526..46566c6817 100644 --- a/release_build_files/readme.md +++ b/release_build_files/readme.md @@ -613,12 +613,6 @@ workflow use only during the development of your app, not for publicly shipping code. ## Release Notes -### Upcoming -- Changes - - General (Desktop): Firebase products now register themselves in the - Firebase user agent when an `App` is created, so that desktop usage - is attributed to the products an application uses. - ### 14.2.0 - Changes - General (Android): Update to Firebase Android BoM version 35.2.0.