Skip to content

Commit d0c961c

Browse files
smilesa-maurice
authored andcommitted
Added an instance ID token cache.
PiperOrigin-RevId: 248432139
1 parent 8869db3 commit d0c961c

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

app/instance_id/iid_data.fbs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ native_include "app/memory/unique_ptr.h";
1818

1919
namespace firebase.instance_id.internal;
2020

21+
table Token {
22+
// Scope of the token. This can be "FCM" for Cloud Messaging tokens or "*"
23+
// for tokens to use with services like Remote Config.
24+
scope:string;
25+
// Instance ID token for the scope.
26+
token:string;
27+
}
28+
2129
table InstanceIdDesktopData {
2230
// Instance ID, random number generated by the client which acts as a unique
2331
// identifier when requesting tokens from the IID backend.
@@ -33,6 +41,8 @@ table InstanceIdDesktopData {
3341
// Time of the last succeessful checkin request in UTC milliseconds since the
3442
// epoch.
3543
last_checkin_time_ms:uint64;
44+
// Token for each scope.
45+
tokens:[Token];
3646
}
3747

3848
root_type InstanceIdDesktopData;

app/instance_id/instance_id_desktop_impl.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,16 @@ bool InstanceIdDesktopImpl::SaveToStorage() {
188188
// Build up a serialized buffer algorithmically:
189189
flatbuffers::FlatBufferBuilder builder;
190190

191+
std::vector<flatbuffers::Offset<Token>> token_offsets;
192+
for (auto it = tokens_.begin(); it != tokens_.end(); ++it) {
193+
token_offsets.push_back(CreateTokenDirect(
194+
builder, it->first.c_str() /* scope */,
195+
it->second.c_str() /* token */));
196+
}
191197
auto iid_data_table = CreateInstanceIdDesktopDataDirect(
192198
builder, instance_id_.c_str(), checkin_data_.device_id.c_str(),
193199
checkin_data_.security_token.c_str(), checkin_data_.digest.c_str(),
194-
checkin_data_.last_checkin_time_ms);
200+
checkin_data_.last_checkin_time_ms, &token_offsets);
195201
builder.Finish(iid_data_table);
196202

197203
std::string save_string;
@@ -276,6 +282,11 @@ bool InstanceIdDesktopImpl::ReadStoredInstanceIdData(
276282
checkin_data_.security_token = iid_data_fb->security_token()->c_str();
277283
checkin_data_.digest = iid_data_fb->digest()->c_str();
278284
checkin_data_.last_checkin_time_ms = iid_data_fb->last_checkin_time_ms();
285+
tokens_.clear();
286+
auto fbtokens = iid_data_fb->tokens();
287+
for (auto it = fbtokens->begin(); it != fbtokens->end(); ++it) {
288+
tokens_[it->scope()->c_str()] = it->token()->c_str();
289+
}
279290
return true;
280291
}
281292

app/instance_id/instance_id_desktop_impl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ class InstanceIdDesktopImpl {
215215
UniquePtr<firebase::app::secure::UserSecureManager> user_secure_manager_;
216216

217217
CheckinData checkin_data_;
218+
// Cached instance ID.
218219
std::string instance_id_;
220+
// Tokens indexed by scope.
221+
std::map<std::string, std::string> tokens_;
219222

220223
// Locale used to check-in.
221224
std::string locale_;

0 commit comments

Comments
 (0)