Skip to content

Commit 0cd8b7c

Browse files
cynthiajoanCynthia Jiang
and
Cynthia Jiang
authored
add api to get allApps (#1327)
* add api to get allApps * add more test cases * format code * review comments * format code --------- Co-authored-by: Cynthia Jiang <[email protected]>
1 parent 3f32415 commit 0cd8b7c

File tree

10 files changed

+68
-2
lines changed

10 files changed

+68
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ __pycache__/
1010
**/build/
1111
**/.externalNativeBuild/
1212

13+
### IDE generated files
14+
.vscode/
15+
1316
# Unencrypted secret files
1417
google-services.json
1518
GoogleService-Info.plist

app/integration_test/src/integration_test.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "app_framework.h" // NOLINT
2424
#include "firebase_test_framework.h" // NOLINT
2525

26+
using ::testing::UnorderedElementsAre;
2627
// The TO_STRING macro is useful for command line defined strings as the quotes
2728
// get stripped.
2829
#define TO_STRING_EXPAND(X) #X
@@ -65,4 +66,34 @@ TEST_F(FirebaseAppTest, TestDefaultAppWithDefaultOptions) {
6566
default_app = nullptr;
6667
}
6768

69+
TEST_F(FirebaseAppTest, TestGetAll) {
70+
EXPECT_EQ(firebase::App::GetApps().size(), 0);
71+
72+
firebase::App* default_app = firebase::App::Create(APP_CREATE_PARAMS);
73+
EXPECT_THAT(firebase::App::GetApps(), UnorderedElementsAre(default_app));
74+
75+
firebase::App* second_app =
76+
firebase::App::Create(firebase::AppOptions(), "2ndApp");
77+
EXPECT_THAT(firebase::App::GetApps(),
78+
UnorderedElementsAre(default_app, second_app));
79+
80+
firebase::App* third_app =
81+
firebase::App::Create(firebase::AppOptions(), "3rdApp");
82+
EXPECT_THAT(firebase::App::GetApps(),
83+
UnorderedElementsAre(default_app, second_app, third_app));
84+
85+
delete third_app;
86+
third_app = nullptr;
87+
EXPECT_THAT(firebase::App::GetApps(),
88+
UnorderedElementsAre(default_app, second_app));
89+
90+
delete default_app;
91+
default_app = nullptr;
92+
EXPECT_THAT(firebase::App::GetApps(), UnorderedElementsAre(second_app));
93+
94+
delete second_app;
95+
second_app = nullptr;
96+
EXPECT_EQ(firebase::App::GetApps().size(), 0);
97+
}
98+
6899
} // namespace firebase_testapp_automated

app/src/app_android.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <string.h>
2121

2222
#include <string>
23+
#include <vector>
2324

2425
#include "app/src/app_common.h"
2526
#include "app/src/assert.h"
@@ -509,6 +510,8 @@ App* App::GetInstance(const char* name) {
509510
return app_common::FindAppByName(name);
510511
}
511512

513+
std::vector<App*> App::GetApps() { return app_common::GetAllApps(); }
514+
512515
JNIEnv* App::GetJNIEnv() const { return util::GetThreadsafeJNIEnv(java_vm()); }
513516

514517
static void RegisterLibraryWithVersionRegistrar(JNIEnv* env,

app/src/app_common.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,19 @@ App* GetAnyApp() {
338338
return nullptr;
339339
}
340340

341+
std::vector<App*> GetAllApps() {
342+
std::vector<App*> result;
343+
App* const default_app = GetDefaultApp();
344+
MutexLock lock(*g_app_mutex);
345+
if (g_apps) {
346+
for (auto it = g_apps->begin(); it != g_apps->end(); ++it) {
347+
if (it->second->app != default_app) result.push_back(it->second->app);
348+
}
349+
if (default_app) result.push_back(default_app);
350+
}
351+
return result;
352+
}
353+
341354
void RemoveApp(App* app) {
342355
assert(app);
343356
MutexLock lock(*g_app_mutex);

app/src/app_common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <map>
2323
#include <string>
24+
#include <vector>
2425

2526
#include "app/src/include/firebase/app.h"
2627
#include "app/src/logger.h"
@@ -60,6 +61,9 @@ App* GetDefaultApp();
6061
// will be returned.
6162
App* GetAnyApp();
6263

64+
// Get all existing apps
65+
std::vector<App*> GetAllApps();
66+
6367
// Remove an app from the set of apps.
6468
// Call this method before destroying an app.
6569
void RemoveApp(App* app);

app/src/app_desktop.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <fstream>
2222
#include <memory>
2323
#include <string>
24+
#include <vector>
2425

2526
#include "app/src/app_common.h"
2627
#include "app/src/function_registry.h"
@@ -167,6 +168,8 @@ App* App::GetInstance(const char* name) { // NOLINT
167168
return app_common::FindAppByName(name);
168169
}
169170

171+
std::vector<App*> App::GetApps() { return app_common::GetAllApps(); }
172+
170173
#ifdef INTERNAL_EXPERIMENTAL
171174
internal::FunctionRegistry* App::function_registry() {
172175
return &internal_->function_registry;

app/src/app_ios.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ static void PlatformOptionsToAppOptions(FIROptions* platform_options, AppOptions
281281

282282
App* App::GetInstance(const char* name) { return app_common::FindAppByName(name); }
283283

284+
std::vector<App*> App::GetApps() { return app_common::GetAllApps(); }
285+
284286
void App::RegisterLibrary(const char* library, const char* version, void* /* platform_resource */) {
285287
[FIRApp registerLibrary:@(library) withVersion:@(version)];
286288
app_common::RegisterLibrariesFromUserAgent(

app/src/app_stub.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ App* App::GetInstance(const char* name) {
6666
return app_common::FindAppByName(name);
6767
}
6868

69+
std::vector<App*> App::GetApps() { return app_common::GetAllApps(); }
70+
6971
#ifdef INTERNAL_EXPERIMENTAL
7072
internal::FunctionRegistry* App::function_registry() {
7173
return static_cast<internal::FunctionRegistry*>(data_);

app/src/include/firebase/app.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <map>
2727
#include <memory>
2828
#include <string>
29+
#include <vector>
2930

3031
#if FIREBASE_PLATFORM_IOS || FIREBASE_PLATFORM_TVOS
3132
#ifdef __OBJC__
@@ -570,6 +571,11 @@ class App {
570571
/// Get the App with the given name, or nullptr if none have been created.
571572
static App* GetInstance(const char* name);
572573

574+
#if !defined(DOXYGEN)
575+
/// Get all the apps, including the default one.
576+
static std::vector<App*> GetApps();
577+
#endif //! defined(DOXYGEN), to hide the api from public documentation.
578+
573579
#ifndef SWIG
574580
// <SWIG>
575581
// Unity doesn't need the JNI from here, it has its method to access JNI.

app/src/util_android.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,7 @@ METHOD_LOOKUP_DECLARATION(double_class, DOUBLE_METHODS);
614614
X(Equals, "equals", \
615615
"(Ljava/lang/Object;)Z"), \
616616
X(Name, "name", \
617-
"()Ljava/lang/String;") \
618-
// clang-format on
617+
"()Ljava/lang/String;") // clang-format on
619618
METHOD_LOOKUP_DECLARATION(enum_class, ENUM_METHODS);
620619

621620
// clang-format off

0 commit comments

Comments
 (0)