-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathapp_common.cc
More file actions
522 lines (466 loc) · 16 KB
/
Copy pathapp_common.cc
File metadata and controls
522 lines (466 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* Copyright 2016 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "app/src/app_common.h"
#include <assert.h>
#include <string.h>
#include <cstdint>
#include <map>
#include <memory>
#include <string>
#include <utility> // Used to detect STL variant.
#include <vector>
#include "app/src/assert.h"
#include "app/src/callback.h"
#include "app/src/cleanup_notifier.h"
#include "app/src/include/firebase/app.h"
#include "app/src/include/firebase/internal/common.h"
#include "app/src/include/firebase/internal/mutex.h"
#include "app/src/include/firebase/internal/platform.h"
#include "app/src/include/firebase/version.h"
#include "app/src/util.h"
// strtok_r is strtok_s on Windows.
#if FIREBASE_PLATFORM_WINDOWS
#define strtok_r strtok_s
#endif // FIREBASE_PLATFORM_WINDOWS
namespace firebase {
#ifdef FIREBASE_LINUX_BUILD_CONFIG_STRING
void CheckCompilerString(const char* input) {
FIREBASE_ASSERT_MESSAGE(
strcmp(FIREBASE_LINUX_BUILD_CONFIG_STRING, input) == 0,
"The compiler or stdlib library Firebase was compiled with does not "
"match what is being used to compile this application."
" [Lib: '%s' != Bin: '%s']",
FIREBASE_LINUX_BUILD_CONFIG_STRING, input);
}
#endif // FIREBASE_LINUX_BUILD_CONFIG_STRING
// Default app name.
const char* const kDefaultAppName = "__FIRAPP_DEFAULT";
namespace app_common {
// clang-format=off
// Detect operating system and architecture.
#if FIREBASE_PLATFORM_WINDOWS
const char* kOperatingSystem = "windows";
#if defined(_DLL) && _DLL == 1
const char* kCppRuntimeOrStl = "MD";
#else
const char* kCppRuntimeOrStl = "MT";
#endif // defined(_MT) && _MT == 1
#if ((defined(_M_X64) && _M_X64 == 100) || \
(defined(_M_AMD64) && _M_AMD64 == 100))
const char* kCpuArchitecture = "x86_64";
#elif defined(_M_IX86) && _M_IX86 == 600
const char* kCpuArchitecture = "x86";
#elif defined(_M_ARM64) && _M_ARM64 == 1
const char* kCpuArchitecture = "arm64";
#elif defined(_M_ARM) && _M_ARM == 7
const char* kCpuArchitecture = "arm32";
#else
#error Unknown Windows architecture.
#endif // Architecture
#elif defined(__APPLE__)
const char* kCppRuntimeOrStl = "libcpp";
#if FIREBASE_PLATFORM_IOS
const char* kOperatingSystem = "ios";
#elif FIREBASE_PLATFORM_TVOS
const char* kOperatingSystem = "tvos";
#elif FIREBASE_PLATFORM_OSX
const char* kOperatingSystem = "darwin";
#else
#error Unknown Apple operating system.
#endif // OS
#if __i386__
const char* kCpuArchitecture = "x86";
#elif __amd64__
const char* kCpuArchitecture = "x86_64";
#elif __aarch64__
const char* kCpuArchitecture = "arm64";
#elif __arm__
const char* kCpuArchitecture = "arm32";
#else
#error Unknown Apple architecture.
#endif // Architecture
#elif FIREBASE_PLATFORM_ANDROID
const char* kOperatingSystem = "android";
#if __i386__
const char* kCpuArchitecture = "x86";
#elif __amd64__
const char* kCpuArchitecture = "x86_64";
#elif __aarch64__
const char* kCpuArchitecture = "arm64";
#elif __ARM_EABI__ || __arm__
const char* kCpuArchitecture = "armeabi-v7a";
#elif __mips__ || __mips
#if __mips__ == 32 || __mips == 32
const char* kCpuArchitecture = "mips";
#elif __mips__ == 64 || __mips == 64
const char* kCpuArchitecture = "mips64";
#else
#error Unknown MIPS version. __mips__
#endif // __mips__
#else
#error Unknown Android architecture.
#endif // Architecture
#if defined(_STLPORT_VERSION)
const char* kCppRuntimeOrStl = "stlport";
#elif defined(_GLIBCXX_UTILITY)
const char* kCppRuntimeOrStl = "gnustl";
#elif defined(_LIBCPP_STD_VER)
const char* kCppRuntimeOrStl = "libcpp";
#else
#error Unknown Android STL.
#endif // STL
#elif FIREBASE_PLATFORM_LINUX
const char* kOperatingSystem = "linux";
#if defined(_GLIBCXX_UTILITY)
const char* kCppRuntimeOrStl = "gnustl";
#elif defined(_LIBCPP_STD_VER)
const char* kCppRuntimeOrStl = "libcpp";
#else
#error Unknown Linux STL.
#endif // STL
#if __amd64__
const char* kCpuArchitecture = "x86_64";
#elif __i386__
const char* kCpuArchitecture = "x86";
#else
#error Unknown Linux architecture.
#endif // Architecture
#else
#error Unknown operating system.
#endif // Operating system
#if FIREBASE_GITHUB_ACTION_BUILD
const char* kBuildSource = "github_action_built";
#else
const char* kBuildSource = "custom_built";
#endif
// clang-format=on
const char* kApiClientHeader = "x-firebase-client";
const char* kXFirebaseGmpIdHeader = "X-Firebase-GMPID";
SystemLogger g_system_logger; // NOLINT
// Private cross platform data associated with an app.
struct AppData {
// TODO(b/140528778): Remove kLogLevelVerbose here and add [GS]etLogLevel
// member functions to app.
AppData() : logger(&g_system_logger, kLogLevelVerbose) {}
// App associated with this data.
App* app;
// Notifies subscribers when the app is about to be destroyed.
CleanupNotifier cleanup_notifier;
// A per-app logger.
Logger logger;
};
// Tracks library registrations.
class LibraryRegistry {
private:
LibraryRegistry() : is_common_library_registered(false) {}
public:
// Register a library, returns true if the library version changed.
bool RegisterLibrary(const char* library, const char* version) {
std::string library_string(library);
std::string version_string(version);
bool changed = true;
std::string existing_version = GetLibraryVersion(library_string);
if (!existing_version.empty()) {
changed = existing_version != version_string;
if (changed) {
LogWarning(
"Library %s is already registered with version %s. "
"This will be overridden with version %s.",
library, existing_version.c_str(), version);
}
}
library_to_version_[library_string] = version_string;
return changed;
}
// Get the version of a previously registered library.
std::string GetLibraryVersion(const std::string& library) const {
auto existing = library_to_version_.find(library);
if (existing != library_to_version_.end()) {
return existing->second;
}
return std::string();
}
// Regenerate the user agent string.
void UpdateUserAgent() {
user_agent_.clear();
for (auto it = library_to_version_.begin(); it != library_to_version_.end();
++it) {
user_agent_ += it->first + "/" + it->second + " ";
}
// Removing the trailing space.
if (!user_agent_.empty()) {
user_agent_ = user_agent_.substr(0, user_agent_.length() - 1);
}
}
// Get the cached user agent string.
const char* GetUserAgent() const { return user_agent_.c_str(); }
public:
// Create the library registry singleton and get the instance.
static LibraryRegistry* Initialize() {
if (!library_registry_) library_registry_ = new LibraryRegistry();
return library_registry_;
}
// Destroy the library registry singleton if there is one.
static void Terminate() {
if (library_registry_) {
delete library_registry_;
library_registry_ = nullptr;
}
}
static bool IsCommonLibraryRegistered() {
if (library_registry_) {
return library_registry_->is_common_library_registered;
}
return false;
}
static void SetCommonLibraryRegistered() {
if (library_registry_) {
library_registry_->is_common_library_registered = true;
}
}
private:
std::map<std::string, std::string> library_to_version_;
std::string user_agent_;
bool is_common_library_registered;
static LibraryRegistry* library_registry_;
};
// Guards g_apps and g_default_app.
static Mutex* g_app_mutex = new Mutex();
static std::map<std::string, std::unique_ptr<AppData>>* g_apps = nullptr;
static App* g_default_app = nullptr;
LibraryRegistry* LibraryRegistry::library_registry_ = nullptr;
App* AddApp(App* app, std::map<std::string, InitResult>* results) {
bool created_first_app = false;
assert(app);
App* existing_app = FindAppByName(app->name());
FIREBASE_ASSERT_RETURN(nullptr, !existing_app);
MutexLock lock(*g_app_mutex);
if (IsDefaultAppName(app->name())) {
assert(!g_default_app);
g_default_app = app;
}
std::unique_ptr<AppData> app_data = std::make_unique<AppData>();
app_data->app = app;
app_data->cleanup_notifier.RegisterOwner(app);
if (!g_apps) {
g_apps = new std::map<std::string, std::unique_ptr<AppData>>();
created_first_app = true;
}
(*g_apps)[std::string(app->name())] = std::move(app_data);
// Create a cleanup notifier for the app.
{
const AppOptions& app_options = app->options();
LogDebug(
"Added app name=%s: options, api_key=%s, app_id=%s, database_url=%s, "
"messaging_sender_id=%s, storage_bucket=%s, project_id=%s (0x%08x)",
app->name(), app_options.api_key(), app_options.app_id(),
app_options.database_url(), app_options.messaging_sender_id(),
app_options.storage_bucket(), app_options.project_id(),
static_cast<int>(reinterpret_cast<intptr_t>(app)));
}
callback::Initialize();
AppCallback::NotifyAllAppCreated(app, results);
return app;
}
App* FindAppByName(const char* name) {
assert(name);
MutexLock lock(*g_app_mutex);
if (g_apps) {
auto it = g_apps->find(std::string(name));
if (it == g_apps->end()) return nullptr;
return it->second->app;
}
return nullptr;
}
App* GetDefaultApp() { return g_default_app; }
App* GetAnyApp() {
if (g_default_app) {
return g_default_app;
}
MutexLock lock(*g_app_mutex);
if (g_apps && !g_apps->empty()) {
return g_apps->begin()->second->app;
}
return nullptr;
}
std::vector<App*> GetAllApps() {
std::vector<App*> result;
App* const default_app = GetDefaultApp();
MutexLock lock(*g_app_mutex);
if (g_apps) {
for (auto it = g_apps->begin(); it != g_apps->end(); ++it) {
if (it->second->app != default_app) result.push_back(it->second->app);
}
if (default_app) result.push_back(default_app);
}
return result;
}
void RemoveApp(App* app) {
assert(app);
MutexLock lock(*g_app_mutex);
if (g_apps) {
auto it = g_apps->find(std::string(app->name()));
bool last_app = false;
// If app initialization failed AddApp() will not be called.
if (it != g_apps->end()) {
LogDebug("Deleting app %s (0x%08x)", app->name(),
static_cast<int>(reinterpret_cast<intptr_t>(app)));
// Notify app callbacks before removing the app from the g_apps
// dictionary, this makes it possible to look up apps by name on
// destruction.
it->second->cleanup_notifier.CleanupAll();
AppCallback::NotifyAllAppDestroyed(app);
// Remove the app entry from the map, from this point it is no longer
// accessible to other components.
g_apps->erase(it);
if (app == g_default_app) {
g_default_app = nullptr;
}
if (g_apps->empty()) {
last_app = true;
delete g_apps;
g_apps = nullptr;
}
}
callback::Terminate(last_app);
if (last_app) {
LibraryRegistry::Terminate();
}
}
}
void DestroyAllApps() {
std::vector<App*> apps_to_delete;
App* const default_app = GetDefaultApp();
MutexLock lock(*g_app_mutex);
if (g_apps) {
for (auto it = g_apps->begin(); it != g_apps->end(); ++it) {
if (it->second->app != default_app)
apps_to_delete.push_back(it->second->app);
}
if (default_app) apps_to_delete.push_back(default_app);
for (auto it = apps_to_delete.begin(); it != apps_to_delete.end(); ++it) {
// As each App is deleted, RemoveApp() is called by the destructor which
// removes the app from the global list of apps (g_apps). When g_apps
// is empty it is deleted by RemoveApp().
delete *it;
}
}
}
// Determine whether the specified app name refers to the default app.
bool IsDefaultAppName(const char* name) {
return strcmp(kDefaultAppName, name) == 0;
}
void RegisterLibrary(const char* library, const char* version) {
MutexLock lock(*g_app_mutex);
LibraryRegistry* registry = LibraryRegistry::Initialize();
if (registry->RegisterLibrary(library, version)) {
registry->UpdateUserAgent();
}
}
void RegisterLibrariesFromUserAgent(const char* user_agent) {
MutexLock lock(*g_app_mutex);
LibraryRegistry* registry = LibraryRegistry::Initialize();
// Copy the string into a vector so that we can safely mutate the string.
std::vector<char> user_agent_vector(user_agent,
user_agent + strlen(user_agent) + 1);
if (user_agent_vector.empty()) return;
char* token = &user_agent_vector[0];
char* next_token = nullptr;
bool changed = false;
do {
// Split fields in the string.
static const char kFieldSeparator[] = " ";
token = strtok_r(token, kFieldSeparator, &next_token);
if (token) {
// Split library / version components and register each tuple.
static const char kLibraryVersionSeparator[] = "/";
char* version;
char* library = strtok_r(token, kLibraryVersionSeparator, &version);
if (library && version) {
changed |= registry->RegisterLibrary(library, version);
}
}
token = next_token;
} while (token && next_token && next_token[0] != '\0');
if (changed) registry->UpdateUserAgent();
}
void RegisterSdkUsage(void* platform_resource) {
MutexLock lock(*g_app_mutex);
// Only register libraries when no C++ apps was created before.
if (!LibraryRegistry::IsCommonLibraryRegistered()) {
LibraryRegistry::Initialize();
// This calls the platform specific method to propagate the registration to
// any SDKs in use by this library.
App::RegisterLibrary(FIREBASE_CPP_USER_AGENT_PREFIX,
FIREBASE_VERSION_NUMBER_STRING, platform_resource);
App::RegisterLibrary(FIREBASE_CPP_USER_AGENT_PREFIX "-os", kOperatingSystem,
platform_resource);
App::RegisterLibrary(FIREBASE_CPP_USER_AGENT_PREFIX "-arch",
kCpuArchitecture, platform_resource);
App::RegisterLibrary(FIREBASE_CPP_USER_AGENT_PREFIX "-stl",
kCppRuntimeOrStl, platform_resource);
App::RegisterLibrary(FIREBASE_CPP_USER_AGENT_PREFIX "-buildsrc",
kBuildSource, platform_resource);
LibraryRegistry::SetCommonLibraryRegistered();
}
}
const char* GetUserAgent() {
MutexLock lock(*g_app_mutex);
return LibraryRegistry::Initialize()->GetUserAgent();
}
std::string GetLibraryVersion(const char* library) {
MutexLock lock(*g_app_mutex);
return LibraryRegistry::Initialize()->GetLibraryVersion(library);
}
void GetOuterMostSdkAndVersion(std::string* sdk, std::string* version) {
assert(sdk);
assert(version);
sdk->clear();
version->clear();
MutexLock lock(*g_app_mutex);
// Set of library versions to query in order of outer wrapper to inner.
// We're only retrieving the outer-most SDK version here as we can only send
// one component as part of the user agent string to the storage backend.
static const char* kLibraryVersions[] = {
FIREBASE_USER_AGENT_PREFIX "unity",
FIREBASE_USER_AGENT_PREFIX "mono",
FIREBASE_CPP_USER_AGENT_PREFIX,
};
LibraryRegistry* registry = LibraryRegistry::Initialize();
for (size_t i = 0; i < FIREBASE_ARRAYSIZE(kLibraryVersions); ++i) {
std::string library(kLibraryVersions[i]);
std::string library_version = registry->GetLibraryVersion(library);
if (!library_version.empty()) {
*sdk = library;
*version = library_version;
break;
}
}
}
// Find a logger associated with an app by app name.
Logger* FindAppLoggerByName(const char* name) {
assert(name);
MutexLock lock(*g_app_mutex);
if (g_apps) {
auto it = g_apps->find(std::string(name));
if (it == g_apps->end()) return nullptr;
return &it->second->logger;
}
return nullptr;
}
} // namespace app_common
// NOLINTNEXTLINE - allow namespace overridden
} // namespace firebase