Skip to content

Commit 5410f26

Browse files
cynthiajianga-maurice
authored andcommitted
[RemoteConfig] Android impl
PiperOrigin-RevId: 290107855
1 parent 60e675a commit 5410f26

File tree

10 files changed

+559
-117
lines changed

10 files changed

+559
-117
lines changed

remote_config/src/android/remote_config_android.cc

Lines changed: 498 additions & 74 deletions
Large diffs are not rendered by default.

remote_config/src/android/remote_config_android.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
#define FIREBASE_REMOTE_CONFIG_CLIENT_CPP_SRC_ANDROID_REMOTE_CONFIG_ANDROID_H_
1717

1818
#include "firebase/app.h"
19+
#include "app/src/mutex.h"
20+
#include "app/src/reference_count.h"
1921
#include "app/src/reference_counted_future_impl.h"
22+
#include "app/src/util_android.h"
2023
#include "firebase/future.h"
2124
#include "remote_config/src/include/firebase/remote_config.h"
2225

@@ -76,20 +79,38 @@ class RemoteConfigInternal {
7679

7780
std::map<std::string, Variant> GetAll();
7881

79-
const ConfigInfo& GetInfo() const;
82+
const ConfigInfo GetInfo() const;
8083

8184
bool Initialized() const;
8285

8386
void Cleanup();
8487

88+
void set_throttled_end_time(int64_t end_time) {
89+
throttled_end_time_ = end_time;
90+
}
91+
92+
void SaveTmpKeysToDefault(std::vector<std::string> tmp_default_keys) {
93+
MutexLock lock(default_key_mutex_);
94+
default_keys_ = std::move(tmp_default_keys);
95+
}
96+
8597
private:
98+
static firebase::internal::ReferenceCount initializer_;
8699
// app
87100
const firebase::App& app_;
88101

89102
/// Handle calls from Futures that the API returns.
90103
ReferenceCountedFutureImpl future_impl_;
91104

92-
jobject* internal_obj_;
105+
jobject internal_obj_;
106+
107+
Mutex default_key_mutex_;
108+
std::vector<std::string> tmp_default_keys_;
109+
std::vector<std::string> default_keys_;
110+
111+
// If a fetch was throttled, this is set to the time when throttling is
112+
// finished, in milliseconds since epoch.
113+
int64_t throttled_end_time_ = 0;
93114
};
94115

95116
} // namespace internal

remote_config/src/common.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ enum RemoteConfigFn {
3030
kRemoteConfigFnCount
3131
};
3232

33-
/// @brief Describes the error codes returned by fetch futures.
34-
enum FetchFutureStatus {
33+
/// @brief Describes the error codes returned by futures.
34+
enum FutureStatus {
3535
// The future returned successfully.
3636
// This should always evaluate to zero, to ensure that the future returns
3737
// a zero result on success.
38-
kFetchFutureStatusSuccess = 0,
38+
kFutureStatusSuccess = 0,
3939
// The future returned unsuccessfully. Check GetInfo() for further details.
40-
kFetchFutureStatusFailure,
40+
kFutureStatusFailure,
4141
};
4242

4343
// Data structure which holds the Future API implementation with the only

remote_config/src/desktop/remote_config_desktop.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ bool RemoteConfigInternal::ActivateFetched() {
443443
return true;
444444
}
445445

446-
const ConfigInfo& RemoteConfigInternal::GetInfo() const {
446+
const ConfigInfo RemoteConfigInternal::GetInfo() const {
447447
std::unique_lock<std::mutex> lock(mutex_);
448448
return configs_.metadata.info();
449449
}
@@ -480,9 +480,10 @@ void RemoteConfigInternal::AsyncFetch() {
480480

481481
is_fetch_process_have_task_ = false;
482482
}
483-
FetchFutureStatus futureResult =
483+
FutureStatus futureResult =
484484
(GetInfo().last_fetch_status == kLastFetchStatusSuccess)
485-
? kFetchFutureStatusSuccess : kFetchFutureStatusFailure;
485+
? kFutureStatusSuccess
486+
: kFutureStatusFailure;
486487

487488
FutureData* future_data = FutureData::Get();
488489
future_data->api()->Complete(handle, futureResult);

remote_config/src/desktop/remote_config_desktop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class RemoteConfigInternal {
116116

117117
bool ActivateFetched();
118118

119-
const ConfigInfo& GetInfo() const;
119+
const ConfigInfo GetInfo() const;
120120

121121
static bool IsBoolTrue(const std::string& str);
122122
static bool IsBoolFalse(const std::string& str);

remote_config/src/desktop/remote_config_legacy.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ bool ActivateFetched() {
150150
const ConfigInfo& GetInfo() {
151151
static ConfigInfo config_info;
152152
FIREBASE_ASSERT_RETURN(config_info, internal::IsInitialized());
153-
return g_remote_config_desktop_instance->GetInfo();
153+
config_info = g_remote_config_desktop_instance->GetInfo();
154+
return config_info;
154155
}
155156

156157
Future<void> Fetch() { return Fetch(kDefaultCacheExpiration); }

remote_config/src/include/firebase/remote_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ class RemoteConfig {
726726
///
727727
/// @return A ConfigInfo struct, containing fields reflecting the state
728728
/// of the most recent fetch request.
729-
const ConfigInfo& GetInfo();
729+
const ConfigInfo GetInfo();
730730

731731
/// Gets the App this remote config object is connected to.
732732
App* app() { return app_; }
@@ -751,7 +751,7 @@ class RemoteConfig {
751751
static RemoteConfig* FindRemoteConfig(App* app);
752752

753753
// Clean up RemoteConfig instance.
754-
static void DeleteInternal(RemoteConfig* rc);
754+
void DeleteInternal();
755755

756756
/// The Firebase App this remote config is connected to.
757757
App* app_;

remote_config/src/ios/remote_config_ios.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class RemoteConfigInternal {
7373

7474
std::map<std::string, Variant> GetAll();
7575

76-
const ConfigInfo& GetInfo() const;
76+
const ConfigInfo GetInfo() const;
7777

7878
bool Initialized() const;
7979

remote_config/src/ios/remote_config_ios.mm

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,13 @@ static void CheckDoubleConversion(FIRRemoteConfigValue *value, ValueInfo *info)
321321
((NSNumber *)error.userInfo[FIRRemoteConfigThrottledEndTimeInSecondsKey]);
322322
}
323323
// If we got an error code back, return that, with the associated string.
324-
api->Complete(handle, kFetchFutureStatusFailure,
324+
api->Complete(handle, kFutureStatusFailure,
325325
util::NSStringToString(error.localizedDescription).c_str());
326326
} else if (status != FIRRemoteConfigFetchStatusSuccess) {
327-
api->Complete(handle, kFetchFutureStatusFailure,
328-
"Fetch encountered an error.");
327+
api->Complete(handle, kFutureStatusFailure, "Fetch encountered an error.");
329328
} else {
330329
// Everything worked!
331-
api->Complete(handle, kFetchFutureStatusSuccess);
330+
api->Complete(handle, kFutureStatusSuccess);
332331
}
333332
};
334333
[g_remote_config_instance fetchWithExpirationDuration:cache_expiration_in_seconds
@@ -522,8 +521,8 @@ bool ActivateFetched() {
522521
return value;
523522
}
524523

525-
const ConfigInfo& RemoteConfigInternal::GetInfo() const {
526-
static ConfigInfo config_info;
524+
const ConfigInfo RemoteConfigInternal::GetInfo() const {
525+
ConfigInfo config_info;
527526
// TODO(cynthiajiang) implement
528527
return config_info;
529528
}

remote_config/src/remote_config.cc

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ RemoteConfig* RemoteConfig::GetInstance(App* app) {
5858
"it depends upon.",
5959
static_cast<int>(reinterpret_cast<intptr_t>(rc)),
6060
static_cast<int>(reinterpret_cast<intptr_t>(rc->app())));
61-
DeleteInternal(rc);
61+
rc->DeleteInternal();
6262
});
63+
64+
// Stick it in the global map so we remember it, and can delete it on
65+
// shutdown.
66+
g_rcs[app] = rc;
67+
return rc;
6368
}
6469

65-
// Stick it in the global map so we remember it, and can delete it on
66-
// shutdown.
67-
g_rcs[app] = rc;
68-
return rc;
70+
return nullptr;
6971
}
7072

7173
RemoteConfig* RemoteConfig::FindRemoteConfig(App* app) {
@@ -78,18 +80,20 @@ RemoteConfig* RemoteConfig::FindRemoteConfig(App* app) {
7880
return nullptr;
7981
}
8082

81-
void RemoteConfig::DeleteInternal(RemoteConfig* rc) {
83+
void RemoteConfig::DeleteInternal() {
8284
MutexLock lock(g_rc_mutex);
85+
if (!internal_) return;
8386

84-
CleanupNotifier* notifier = CleanupNotifier::FindByOwner(rc->app());
87+
CleanupNotifier* notifier = CleanupNotifier::FindByOwner(app());
8588
assert(notifier);
86-
notifier->UnregisterObject(rc);
89+
notifier->UnregisterObject(this);
8790

88-
// Remove rc from the g_rcs map.
89-
g_rcs.erase(rc->app());
91+
internal_->Cleanup();
92+
delete internal_;
93+
internal_ = nullptr;
9094

91-
delete rc;
92-
rc = nullptr;
95+
// Remove rc from the g_rcs map.
96+
g_rcs.erase(app());
9397
}
9498

9599
RemoteConfig::RemoteConfig(App* app) {
@@ -98,11 +102,7 @@ RemoteConfig::RemoteConfig(App* app) {
98102
internal_ = new internal::RemoteConfigInternal(*app);
99103
}
100104

101-
RemoteConfig::~RemoteConfig() {
102-
internal_->Cleanup();
103-
delete internal_;
104-
internal_ = nullptr;
105-
}
105+
RemoteConfig::~RemoteConfig() { DeleteInternal(); }
106106

107107
bool RemoteConfig::InitInternal() { return internal_->Initialized(); }
108108

@@ -199,9 +199,7 @@ std::string RemoteConfig::GetString(const char* key, ValueInfo* info) {
199199
}
200200

201201
std::vector<unsigned char> RemoteConfig::GetData(const char* key) {
202-
std::vector<unsigned char> value;
203-
// TODO(cynthiajiang) implement
204-
return value;
202+
return GetData(key, static_cast<ValueInfo*>(nullptr));
205203
}
206204

207205
std::vector<unsigned char> RemoteConfig::GetData(const char* key,
@@ -222,9 +220,7 @@ std::map<std::string, Variant> RemoteConfig::GetAll() {
222220
}
223221

224222
// TODO(b/147143718): Change to a more descriptive name.
225-
const ConfigInfo& RemoteConfig::GetInfo() {
226-
return internal_->GetInfo();
227-
}
223+
const ConfigInfo RemoteConfig::GetInfo() { return internal_->GetInfo(); }
228224
#endif // FIREBASE_EARLY_ACCESS_PREVIEW
229225

230226
} // namespace remote_config

0 commit comments

Comments
 (0)