-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathremote_config.cc
More file actions
245 lines (194 loc) · 7.31 KB
/
remote_config.cc
File metadata and controls
245 lines (194 loc) · 7.31 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
// Copyright 2019 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 "remote_config/src/include/firebase/remote_config.h"
#include <cstdint>
#include "app/src/cleanup_notifier.h"
#include "app/src/include/firebase/internal/mutex.h"
#include "app/src/include/firebase/internal/platform.h"
#include "app/src/semaphore.h"
#include "app/src/time.h"
#include "include/firebase/remote_config.h"
#include "remote_config/src/common.h"
// RemoteConfigInternal is defined in these 3 files, one implementation for each
// OS.
#if FIREBASE_PLATFORM_ANDROID
#include "remote_config/src/android/remote_config_android.h"
#elif FIREBASE_PLATFORM_IOS || FIREBASE_PLATFORM_TVOS
#include "remote_config/src/ios/remote_config_ios.h"
#else
#include "remote_config/src/desktop/remote_config_desktop.h"
#endif // FIREBASE_PLATFORM_ANDROID, FIREBASE_PLATFORM_IOS,
// FIREBASE_PLATFORM_TVOS
namespace firebase {
namespace remote_config {
std::map<App*, RemoteConfig*> g_rcs;
Mutex g_rc_mutex; // NOLINT
RemoteConfig* RemoteConfig::GetInstance(App* app) {
MutexLock lock(g_rc_mutex);
// Return the RemoteConfig if it already exists.
RemoteConfig* existing_rc = FindRemoteConfig(app);
if (existing_rc) {
return existing_rc;
}
// Create a new RemoteConfig and initialize.
RemoteConfig* rc = new RemoteConfig(app);
LogDebug("Creating RemoteConfig %p for App %s", rc, app->name());
if (rc->InitInternal()) {
// Cleanup this object if app is destroyed.
CleanupNotifier* notifier = CleanupNotifier::FindByOwner(app);
assert(notifier);
notifier->RegisterObject(rc, [](void* object) {
RemoteConfig* rc = reinterpret_cast<RemoteConfig*>(object);
LogWarning(
"Remote Config object 0x%08x should be deleted before the App 0x%08x "
"it depends upon.",
static_cast<int>(reinterpret_cast<intptr_t>(rc)),
static_cast<int>(reinterpret_cast<intptr_t>(rc->app())));
rc->DeleteInternal();
});
// Stick it in the global map so we remember it, and can delete it on
// shutdown.
g_rcs[app] = rc;
return rc;
}
return nullptr;
}
RemoteConfig* RemoteConfig::FindRemoteConfig(App* app) {
MutexLock lock(g_rc_mutex);
// Return the RemoteConfig if it already exists.
std::map<App*, RemoteConfig*>::iterator it = g_rcs.find(app);
if (it != g_rcs.end()) {
return it->second;
}
return nullptr;
}
void RemoteConfig::DeleteInternal() {
MutexLock lock(g_rc_mutex);
if (!internal_) return;
CleanupNotifier* notifier = CleanupNotifier::FindByOwner(app());
assert(notifier);
notifier->UnregisterObject(this);
internal_->Cleanup();
delete internal_;
internal_ = nullptr;
// Remove rc from the g_rcs map.
g_rcs.erase(app());
}
RemoteConfig::RemoteConfig(App* app) {
FIREBASE_ASSERT(app != nullptr);
app_ = app;
internal_ = new internal::RemoteConfigInternal(*app);
}
RemoteConfig::~RemoteConfig() { DeleteInternal(); }
bool RemoteConfig::InitInternal() { return internal_->Initialized(); }
Future<ConfigInfo> RemoteConfig::EnsureInitialized() {
return internal_->EnsureInitialized();
}
Future<ConfigInfo> RemoteConfig::EnsureInitializedLastResult() {
return internal_->EnsureInitializedLastResult();
}
Future<bool> RemoteConfig::Activate() { return internal_->Activate(); }
Future<bool> RemoteConfig::ActivateLastResult() {
return internal_->ActivateLastResult();
}
Future<bool> RemoteConfig::FetchAndActivate() {
return internal_->FetchAndActivate();
}
Future<bool> RemoteConfig::FetchAndActivateLastResult() {
return internal_->FetchAndActivateLastResult();
}
Future<void> RemoteConfig::Fetch() {
return Fetch(GetConfigSettings().minimum_fetch_interval_in_milliseconds /
firebase::internal::kMillisecondsPerSecond);
}
Future<void> RemoteConfig::Fetch(uint64_t cache_expiration_in_seconds) {
return internal_->Fetch(cache_expiration_in_seconds);
}
Future<void> RemoteConfig::FetchLastResult() {
return internal_->FetchLastResult();
}
#if defined(__ANDROID__)
Future<void> RemoteConfig::SetDefaults(int defaults_resource_id) {
return internal_->SetDefaults(defaults_resource_id);
}
#endif // __ANDROID__
Future<void> RemoteConfig::SetDefaults(const ConfigKeyValueVariant* defaults,
size_t number_of_defaults) {
return internal_->SetDefaults(defaults, number_of_defaults);
}
Future<void> RemoteConfig::SetDefaults(const ConfigKeyValue* defaults,
size_t number_of_defaults) {
return internal_->SetDefaults(defaults, number_of_defaults);
}
Future<void> RemoteConfig::SetDefaultsLastResult() {
return internal_->SetDefaultsLastResult();
}
Future<void> RemoteConfig::SetConfigSettings(ConfigSettings settings) {
return internal_->SetConfigSettings(settings);
}
ConfigSettings RemoteConfig::GetConfigSettings() {
return internal_->GetConfigSettings();
}
Future<void> RemoteConfig::SetConfigSettingsLastResult() {
return internal_->SetConfigSettingsLastResult();
}
bool RemoteConfig::GetBoolean(const char* key) {
return GetBoolean(key, static_cast<ValueInfo*>(nullptr));
}
bool RemoteConfig::GetBoolean(const char* key, ValueInfo* info) {
return internal_->GetBoolean(key, info);
}
int64_t RemoteConfig::GetLong(const char* key) {
return GetLong(key, static_cast<ValueInfo*>(nullptr));
}
int64_t RemoteConfig::GetLong(const char* key, ValueInfo* info) {
return internal_->GetLong(key, info);
}
double RemoteConfig::GetDouble(const char* key) {
return GetDouble(key, static_cast<ValueInfo*>(nullptr));
}
double RemoteConfig::GetDouble(const char* key, ValueInfo* info) {
return internal_->GetDouble(key, info);
}
std::string RemoteConfig::GetString(const char* key) {
return GetString(key, static_cast<ValueInfo*>(nullptr));
}
std::string RemoteConfig::GetString(const char* key, ValueInfo* info) {
return internal_->GetString(key, info);
}
std::vector<unsigned char> RemoteConfig::GetData(const char* key) {
return GetData(key, static_cast<ValueInfo*>(nullptr));
}
std::vector<unsigned char> RemoteConfig::GetData(const char* key,
ValueInfo* info) {
return internal_->GetData(key, info);
}
std::vector<std::string> RemoteConfig::GetKeysByPrefix(const char* prefix) {
return internal_->GetKeysByPrefix(prefix);
}
std::vector<std::string> RemoteConfig::GetKeys() {
return internal_->GetKeys();
}
std::map<std::string, Variant> RemoteConfig::GetAll() {
return internal_->GetAll();
}
// TODO(b/147143718): Change to a more descriptive name.
const ConfigInfo RemoteConfig::GetInfo() { return internal_->GetInfo(); }
ConfigUpdateListenerRegistration RemoteConfig::AddOnConfigUpdateListener(
std::function<void(ConfigUpdate&&, RemoteConfigError)>
config_update_listener) {
return internal_->AddOnConfigUpdateListener(config_update_listener);
}
} // namespace remote_config
} // namespace firebase