Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -185,13 +188,24 @@ 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;
}
}
}

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;
Expand Down Expand Up @@ -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<std::string, AppCallback*>();
Expand Down
77 changes: 76 additions & 1 deletion app/src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
}
Comment on lines +149 to +154

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent potential null pointer dereferences, it is safer to perform a null check on callback before calling SetLibrary on it.

    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(
Expand All @@ -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 {
Expand All @@ -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.
Expand All @@ -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<std::string, AppCallback*>* callbacks_;
Expand Down Expand Up @@ -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 {
Expand Down
62 changes: 62 additions & 0 deletions app/tests/app_desktop_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
#include <codecvt>
#include <cstdio>
#include <fstream>
#include <memory>
#include <string>

#include "app/src/app_common.h"
#include "app/src/app_identifier.h"
#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"

Expand All @@ -37,6 +39,17 @@
#include <unistd.h>
#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) {
Expand Down Expand Up @@ -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<App> 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>(App::Create(options));
}

struct ScopedCallbackReset {
~ScopedCallbackReset() { AppCallback::ResetAllToDefaults(); }
};

TEST(AppDesktopTest, TestModuleLibraryRegisteredWhenAppCreated) {
EXPECT_FALSE(AppCallback::GetEnabledByName(kLibraryTestModule));
std::unique_ptr<App> 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> 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> app = CreateAppForLibraryTest();
ASSERT_NE(app, nullptr);

EXPECT_THAT(std::string(App::GetUserAgent()),
::testing::HasSubstr(kLibraryTestUserAgent));
}
Comment on lines +314 to +316

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test disables all callbacks using AppCallback::SetEnabledAll(false) but does not restore them at the end of the test. This can leave the global state modified and potentially affect other tests running in the same process. It is recommended to restore the default state of the callbacks by calling AppCallback::SetEnabledAll(true) at the end of the test.

  EXPECT_THAT(std::string(App::GetUserAgent()),
              ::testing::HasSubstr(kLibraryTestUserAgent));
  AppCallback::SetEnabledAll(true);
}

} // namespace firebase
2 changes: 2 additions & 0 deletions app_check/src/common/app_check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions auth/src/auth.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions database/src/common/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions firestore/src/common/firestore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <utility>

#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"
Expand All @@ -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 {

Expand Down
2 changes: 2 additions & 0 deletions functions/src/common/functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions remote_config/src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <assert.h>

#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"

Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions storage/src/common/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading