Skip to content

Commit ea86890

Browse files
chkuang-ga-maurice
authored andcommitted
Integrate IID to Remote Config
Whenever RC try to fetch the latest configuration from the server, it would check with InstanceIdDesktopImpl to see if instance id and token is available. If so, the id and token will be included in REST call as part of the PackageData PiperOrigin-RevId: 248613772
1 parent 03641d6 commit ea86890

File tree

6 files changed

+84
-8
lines changed

6 files changed

+84
-8
lines changed

remote_config/src/desktop/remote_config_desktop.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ void RemoteConfigDesktop::AsyncFetch() {
376376
}
377377

378378
// Fetch fresh config from server.
379-
rest->Fetch();
379+
rest->Fetch(app_);
380380

381381
{
382382
std::unique_lock<std::mutex> lock(mutex_);

remote_config/src/desktop/rest.cc

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <sstream>
2121

2222
#include "firebase/app.h"
23+
#include "app/instance_id/instance_id_desktop_impl.h"
2324
#include "app/meta/move.h"
2425
#include "app/rest/request_binary.h"
2526
#include "app/rest/response_binary.h"
@@ -53,7 +54,8 @@ RemoteConfigREST::RemoteConfigREST(const firebase::AppOptions& app_options,
5354
: app_package_name_(app_options.package_name()),
5455
app_gmp_project_id_(app_options.app_id()),
5556
configs_(configs),
56-
cache_expiration_in_seconds_(cache_expiration_in_seconds) {
57+
cache_expiration_in_seconds_(cache_expiration_in_seconds),
58+
fetch_future_sem_(0) {
5759
rest::util::Initialize();
5860
firebase::rest::InitTransportCurl();
5961
}
@@ -63,12 +65,57 @@ RemoteConfigREST::~RemoteConfigREST() {
6365
rest::util::Terminate();
6466
}
6567

66-
void RemoteConfigREST::Fetch() {
68+
void RemoteConfigREST::Fetch(const App& app) {
69+
TryGetInstanceIdAndToken(app);
6770
SetupRestRequest();
6871
firebase::rest::CreateTransport()->Perform(rest_request_, &rest_response_);
6972
ParseRestResponse();
7073
}
7174

75+
void WaitForFuture(const Future<std::string>& future, Semaphore* future_sem,
76+
std::string* result, const char* action_name) {
77+
// Block and wait until Future is complete.
78+
future.OnCompletion(
79+
[](const firebase::Future<std::string>& result, void* data) {
80+
Semaphore* sem = static_cast<Semaphore*>(data);
81+
sem->Post();
82+
},
83+
future_sem);
84+
future_sem->Wait();
85+
86+
if (future.status() == firebase::kFutureStatusComplete &&
87+
future.error() ==
88+
firebase::instance_id::internal::InstanceIdDesktopImpl::kErrorNone) {
89+
*result = *future.result();
90+
} else if (future.status() != firebase::kFutureStatusComplete) {
91+
// It is fine if timeout
92+
LogWarning("Remote Config Fetch: %s timeout", action_name);
93+
} else {
94+
// It is fine if failed
95+
LogWarning("Remote Config Fetch: Failed to %s. Error %d: %s", action_name,
96+
future.error(), future.error_message());
97+
}
98+
}
99+
100+
void RemoteConfigREST::TryGetInstanceIdAndToken(const App& app) {
101+
// Convert the app reference stored in RemoteConfigDesktop
102+
// pointer for InstanceIdDesktopImpl.
103+
App* non_const_app = const_cast<App*>(&app);
104+
auto* iid_impl =
105+
firebase::instance_id::internal::InstanceIdDesktopImpl::GetInstance(
106+
non_const_app);
107+
assert(iid_impl);
108+
109+
WaitForFuture(iid_impl->GetId(), &fetch_future_sem_, &app_instance_id_,
110+
"Get Instance Id");
111+
112+
// Only get token if instance id is retrieved.
113+
if (!app_instance_id_.empty()) {
114+
WaitForFuture(iid_impl->GetToken(), &fetch_future_sem_,
115+
&app_instance_id_token_, "Get Instance Id Token");
116+
}
117+
}
118+
72119
void RemoteConfigREST::SetupRestRequest() {
73120
ConfigFetchRequest config_fetch_request = GetFetchRequestData();
74121

@@ -113,6 +160,9 @@ void RemoteConfigREST::GetPackageData(PackageData* package_data) {
113160
package_data->custom_variable[kDeveloperModeKey] = "1";
114161
}
115162

163+
package_data->app_instance_id = app_instance_id_;
164+
package_data->app_instance_id_token = app_instance_id_token_;
165+
116166
package_data->requested_cache_expiration_seconds =
117167
static_cast<int32_t>(cache_expiration_in_seconds_);
118168

remote_config/src/desktop/rest.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cstdint>
1919

2020
#include "firebase/app.h"
21+
#include "app/src/semaphore.h"
2122
#ifndef REST_STUB_IMPL // These pull in unnecessary deps for the stub
2223
#include "app/rest/request_binary_gzip.h"
2324
#include "app/rest/response_binary.h"
@@ -73,10 +74,12 @@ class RemoteConfigREST {
7374

7475
~RemoteConfigREST();
7576

76-
// 1. Setup REST request;
77-
// 2. Make REST request;
78-
// 3. Parse REST response.
79-
void Fetch();
77+
// 1. Attempt to Fetch Instance Id and token. App is required to get an
78+
// instance of InstanceIdDesktopImpl.
79+
// 2. Setup REST request;
80+
// 3. Make REST request;
81+
// 4. Parse REST response.
82+
void Fetch(const App& app);
8083

8184
// After Fetch() will return updated fetched holder. Otherwise will return not
8285
// updated fetched holder.
@@ -87,6 +90,10 @@ class RemoteConfigREST {
8790
const RemoteConfigMetadata& metadata() const { return configs_.metadata; }
8891

8992
private:
93+
// Attempt to get Instance Id and token from app synchronously. This will
94+
// block the current thread and wait until the futures are complete.
95+
void TryGetInstanceIdAndToken(const App& app);
96+
9097
// Setup all values to make REST request. Call `SetupProtoRequest` to setup
9198
// post fields.
9299
void SetupRestRequest();
@@ -123,6 +130,13 @@ class RemoteConfigREST {
123130
// cache expiration
124131
uint64_t cache_expiration_in_seconds_;
125132

133+
// Instance Id data
134+
std::string app_instance_id_;
135+
std::string app_instance_id_token_;
136+
137+
// The semaphore to block the thread and wait for.
138+
Semaphore fetch_future_sem_;
139+
126140
#ifndef REST_STUB_IMPL
127141
// HTTP request/response
128142
firebase::rest::RequestBinaryGzip rest_request_;

remote_config/src/desktop/rest_nanopb_encode.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ pb_callback_t EncodePackageDataCB(const PackageData& source) {
113113

114114
npb_package.namespace_digest = EncodeNamedValuesCB(source.namespace_digest);
115115
npb_package.custom_variable = EncodeNamedValuesCB(source.custom_variable);
116-
116+
npb_package.app_instance_id = EncodeStringCB(source.app_instance_id);
117+
npb_package.app_instance_id_token =
118+
EncodeStringCB(source.app_instance_id_token);
117119
npb_package.sdk_version = source.sdk_version;
118120
npb_package.has_sdk_version = (source.sdk_version != 0);
119121
npb_package.requested_cache_expiration_seconds =

remote_config/src/desktop/rest_nanopb_encode.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ struct PackageData {
3939
// custom variables as defined by the client app.
4040
NamedValues custom_variable;
4141

42+
// optional
43+
// The instance id of the app.
44+
std::string app_instance_id;
45+
46+
// optional
47+
// The instance id token of the app.
48+
std::string app_instance_id_token;
49+
4250
// version of the firebase remote config sdk. constructed by a major version,
4351
// a minor version, and a patch version, using the formula:
4452
// (major * 10000) + (minor * 100) + patch

remote_config/src_protos/config.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ message PackageData {
2424
optional string gmp_project_id = 6;
2525
repeated NamedValue namespace_digest = 8;
2626
repeated NamedValue custom_variable = 9;
27+
optional string app_instance_id = 12;
28+
optional string app_instance_id_token = 14;
2729
optional int32 sdk_version = 16;
2830
optional int32 requested_cache_expiration_seconds = 18;
2931
optional int32 fetched_config_age_seconds = 19;

0 commit comments

Comments
 (0)