@@ -34,79 +34,9 @@ namespace firebase {
34
34
namespace analytics {
35
35
36
36
#if defined(_WIN32)
37
- #define ANALYTICS_DLL_DEFAULT_FILENAME L" analytics_win.dll"
38
- std::wstring g_analytics_dll_filename = ANALYTICS_DLL_DEFAULT_FILENAME;
39
- static HMODULE g_analytics_dll = 0 ;
40
-
41
- // Function to convert a UTF-8 string to a wide character (UTF-16) string.
42
- std::wstring Utf8ToWide (const std::string& utf8String) {
43
- if (utf8String.empty ()) {
44
- return std::wstring ();
45
- }
46
-
47
- // First, determine the required buffer size.
48
- int wideCharCount = MultiByteToWideChar (
49
- CP_UTF8, // Source code page (UTF-8)
50
- 0 , // Flags
51
- utf8String.c_str (), // Source UTF-8 string
52
- -1 , // -1 indicates the string is null-terminated
53
- nullptr , // No buffer provided, we're calculating the size
54
- 0 // Requesting the buffer size
55
- );
56
-
57
- if (wideCharCount == 0 ) {
58
- // Handle error: GetLastError() can provide more details.
59
- LogError (
60
- " Error determining buffer size for UTF-8 to wide char conversion." );
61
- return std::wstring ();
62
- }
63
-
64
- // Allocate the wide character string.
65
- std::wstring wideString (wideCharCount, 0 );
66
-
67
- // Second, perform the actual conversion.
68
- int result =
69
- MultiByteToWideChar (CP_UTF8, // Source code page (UTF-8)
70
- 0 , // Flags
71
- utf8String.c_str (), // Source UTF-8 string
72
- -1 , // -1 indicates the string is null-terminated
73
- &wideString[0 ], // Pointer to the destination buffer
74
- wideCharCount // The size of the destination buffer
75
- );
76
-
77
- if (result == 0 ) {
78
- // Handle error: GetLastError() can provide more details.
79
- LogError (" Error converting UTF-8 to wide char." );
80
- return std::wstring ();
81
- }
82
-
83
- // The returned wideString from MultiByteToWideChar will be null-terminated,
84
- // but std::wstring handles its own length. We might need to resize it
85
- // to remove the extra null character included in the count if we passed -1.
86
- size_t pos = wideString.find (L' \0 ' );
87
- if (pos != std::wstring::npos) {
88
- wideString.resize (pos);
89
- }
90
-
91
- return wideString;
92
- }
93
-
94
- void SetAnalyticsLibraryPath (const char * path) {
95
- if (path) {
96
- g_analytics_dll_filename = Utf8ToWide (path);
97
- } else {
98
- g_analytics_dll_filename = ANALYTICS_DLL_DEFAULT_FILENAME;
99
- }
100
- }
37
+ #define ANALYTICS_DLL_FILENAME L" analytics_win.dll"
101
38
102
- void SetAnalyticsLibraryPath (const wchar_t * path) {
103
- if (path) {
104
- g_analytics_dll_filename = path;
105
- } else {
106
- g_analytics_dll_filename = ANALYTICS_DLL_DEFAULT_FILENAME;
107
- }
108
- }
109
- #endif
39
+ static HMODULE g_analytics_module = 0 ;
110
40
111
41
// Future data for analytics.
112
42
// This is initialized in `Initialize()` and cleaned up in `Terminate()`.
@@ -127,14 +57,18 @@ void Initialize(const App& app) {
127
57
g_fake_instance_id = 0 ;
128
58
129
59
#if defined(_WIN32)
130
- if (!g_analytics_dll) {
131
- g_analytics_dll = LoadLibraryW (g_analytics_dll_filename.c_str ());
132
- if (g_analytics_dll) {
133
- LogInfo (" Loaded Google Analytics DLL" );
60
+ if (!g_analytics_module) {
61
+ // Only allow the DLL to be loaded from the application directory.
62
+ g_analytics_module = LoadLibraryExW (ANALYTICS_DLL_FILENAME, NULL ,
63
+ LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
64
+ if (g_analytics_module) {
65
+ LogInfo (" Loaded Google Analytics module" );
134
66
int num_loaded = FirebaseAnalytics_LoadDynamicFunctions (g_analytics_dll);
135
67
if (num_loaded < FIREBASE_ANALYTICS_DYNAMIC_FUNCTION_COUNT) {
136
- LogWarning (" Only loaded %d out of %d expected functions from DLL." ,
137
- num_loaded, FIREBASE_ANALYTICS_DYNAMIC_FUNCTION_COUNT);
68
+ LogWarning (
69
+ " Only loaded %d out of %d expected functions from the Google "
70
+ " Analytics module." ,
71
+ num_loaded, FIREBASE_ANALYTICS_DYNAMIC_FUNCTION_COUNT);
138
72
}
139
73
} else {
140
74
// Silently fail and continue in stub mode.
@@ -155,9 +89,9 @@ bool IsInitialized() { return g_initialized; }
155
89
void Terminate () {
156
90
#if defined(_WIN32)
157
91
FirebaseAnalytics_UnloadDynamicFunctions ();
158
- if (g_analytics_dll ) {
159
- FreeLibrary (g_analytics_dll );
160
- g_analytics_dll = 0 ;
92
+ if (g_analytics_module ) {
93
+ FreeLibrary (g_analytics_module );
94
+ g_analytics_module = 0 ;
161
95
}
162
96
#endif
163
97
0 commit comments