diff --git a/app/src/util.cc b/app/src/util.cc index d74fb83d08..5116907c97 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; @@ -241,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 f25817a209..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); } @@ -117,7 +118,41 @@ 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) { + if (callback != nullptr) { + callback->SetLibrary(name, version); + } + } + }; // Called by firebase::App when an instance is created. static void NotifyAllAppCreated( @@ -135,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 { @@ -146,6 +186,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. @@ -158,8 +202,16 @@ 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. + 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 +256,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..84effd30c1 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,53 @@ 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)); +} + +struct ScopedCallbackReset { + ~ScopedCallbackReset() { AppCallback::ResetAllToDefaults(); } +}; + +TEST(AppDesktopTest, TestModuleLibraryRegisteredWhenAppCreated) { + EXPECT_FALSE(AppCallback::GetEnabledByName(kLibraryTestModule)); + std::unique_ptr app = CreateAppForLibraryTest(); + ASSERT_NE(app, nullptr); + + EXPECT_THAT(std::string(App::GetUserAgent()), + ::testing::HasSubstr(kLibraryTestUserAgent)); +} + +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(); + ASSERT_NE(app, nullptr); + + 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); + 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/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 {