|
18 | 18 |
|
19 | 19 | #include "app/instance_id/iid_data_generated.h"
|
20 | 20 | #include "app/src/app_identifier.h"
|
| 21 | +#include "app/src/base64.h" |
21 | 22 | #include "app/src/cleanup_notifier.h"
|
22 | 23 | #include "app/src/time.h"
|
| 24 | +#include "app/src/uuid.h" |
23 | 25 | #include "flatbuffers/flexbuffers.h"
|
24 | 26 |
|
25 | 27 | namespace firebase {
|
@@ -190,9 +192,9 @@ bool InstanceIdDesktopImpl::SaveToStorage() {
|
190 | 192 |
|
191 | 193 | std::vector<flatbuffers::Offset<Token>> token_offsets;
|
192 | 194 | 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 */)); |
| 195 | + token_offsets.push_back(CreateTokenDirect(builder, |
| 196 | + it->first.c_str() /* scope */, |
| 197 | + it->second.c_str() /* token */)); |
196 | 198 | }
|
197 | 199 | auto iid_data_table = CreateInstanceIdDesktopDataDirect(
|
198 | 200 | builder, instance_id_.c_str(), checkin_data_.device_id.c_str(),
|
@@ -301,6 +303,11 @@ bool InstanceIdDesktopImpl::InitialOrRefreshCheckin() {
|
301 | 303 | LoadFromStorage();
|
302 | 304 | }
|
303 | 305 |
|
| 306 | + // If we don't have an instance_id_ yet, generate one now. |
| 307 | + if (instance_id_.empty()) { |
| 308 | + instance_id_ = GenerateAppId(); |
| 309 | + } |
| 310 | + |
304 | 311 | // If we've already checked in.
|
305 | 312 | if (checkin_data_.last_checkin_time_ms > 0) {
|
306 | 313 | FIREBASE_ASSERT(!checkin_data_.device_id.empty() &&
|
@@ -433,6 +440,29 @@ bool InstanceIdDesktopImpl::InitialOrRefreshCheckin() {
|
433 | 440 | return true;
|
434 | 441 | }
|
435 | 442 |
|
| 443 | +std::string InstanceIdDesktopImpl::GenerateAppId() { |
| 444 | + firebase::internal::Uuid uuid; |
| 445 | + uuid.Generate(); |
| 446 | + // Collapse into 8 bytes, forcing the top 4 bits to be 0x70. |
| 447 | + uint8_t buffer[8]; |
| 448 | + buffer[0] = ((uuid.data[0] ^ uuid.data[8]) & 0x0f) | 0x70; |
| 449 | + buffer[1] = uuid.data[1] ^ uuid.data[9]; |
| 450 | + buffer[2] = uuid.data[2] ^ uuid.data[10]; |
| 451 | + buffer[3] = uuid.data[3] ^ uuid.data[11]; |
| 452 | + buffer[4] = uuid.data[4] ^ uuid.data[12]; |
| 453 | + buffer[5] = uuid.data[5] ^ uuid.data[13]; |
| 454 | + buffer[6] = uuid.data[6] ^ uuid.data[14]; |
| 455 | + buffer[7] = uuid.data[7] ^ uuid.data[15]; |
| 456 | + |
| 457 | + std::string input(reinterpret_cast<char*>(buffer), sizeof(buffer)); |
| 458 | + std::string output; |
| 459 | + |
| 460 | + if (firebase::internal::Base64Encode(input, &output)) { |
| 461 | + return output; |
| 462 | + } |
| 463 | + return ""; // Error encoding. |
| 464 | +} |
| 465 | + |
436 | 466 | } // namespace internal
|
437 | 467 | } // namespace instance_id
|
438 | 468 | } // namespace firebase
|
0 commit comments