Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/cryptauth/background_eid_generator.h" | 5 #include "components/cryptauth/background_eid_generator.h" |
| 6 | 6 |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 const std::vector<BeaconSeed>& beacon_seeds) { | 33 const std::vector<BeaconSeed>& beacon_seeds) { |
| 34 for (const BeaconSeed& seed : beacon_seeds) { | 34 for (const BeaconSeed& seed : beacon_seeds) { |
| 35 if (timestamp_ms >= seed.start_time_millis() && | 35 if (timestamp_ms >= seed.start_time_millis() && |
| 36 timestamp_ms <= seed.end_time_millis()) { | 36 timestamp_ms <= seed.end_time_millis()) { |
| 37 return &seed; | 37 return &seed; |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 return nullptr; | 40 return nullptr; |
| 41 } | 41 } |
| 42 | 42 |
| 43 // Given a list of |eids| and their respective |timestamps|, return a human | |
| 44 // readable string for debugging. | |
| 45 std::string EidsToDebugString(const std::vector<EidDataWithTimestamp>& eids) { | |
| 46 std::stringstream ss; | |
| 47 ss << "["; | |
| 48 for (const EidDataWithTimestamp& eid : eids) { | |
| 49 ss << "\n " << eid.start_timestamp_ms << ": " << eid.DataInHex() << ","; | |
|
Kyle Horimoto
2017/04/28 22:19:16
nit: Can you add parentheses around each EidDataWi
Tim Song
2017/04/29 01:07:50
Done.
| |
| 50 } | |
| 51 ss << "\n]"; | |
|
Kyle Horimoto
2017/04/28 22:19:16
nit: Remove the final comma.
Tim Song
2017/04/29 01:07:50
Done.
| |
| 52 return ss.str(); | |
| 53 } | |
| 54 | |
| 43 } // namespace | 55 } // namespace |
| 44 | 56 |
| 45 BackgroundEidGenerator::BackgroundEidGenerator() | 57 BackgroundEidGenerator::BackgroundEidGenerator() |
| 46 : BackgroundEidGenerator(base::MakeUnique<RawEidGeneratorImpl>(), | 58 : BackgroundEidGenerator(base::MakeUnique<RawEidGeneratorImpl>(), |
| 47 base::MakeUnique<base::DefaultClock>()) {} | 59 base::MakeUnique<base::DefaultClock>()) {} |
| 48 | 60 |
| 49 BackgroundEidGenerator::~BackgroundEidGenerator() {} | 61 BackgroundEidGenerator::~BackgroundEidGenerator() {} |
| 50 | 62 |
| 51 BackgroundEidGenerator::BackgroundEidGenerator( | 63 BackgroundEidGenerator::BackgroundEidGenerator( |
| 52 std::unique_ptr<RawEidGenerator> raw_eid_generator, | 64 std::unique_ptr<RawEidGenerator> raw_eid_generator, |
| 53 std::unique_ptr<base::Clock> clock) | 65 std::unique_ptr<base::Clock> clock) |
| 54 : raw_eid_generator_(std::move(raw_eid_generator)), | 66 : raw_eid_generator_(std::move(raw_eid_generator)), |
| 55 clock_(std::move(clock)) {} | 67 clock_(std::move(clock)) {} |
| 56 | 68 |
| 57 std::vector<std::string> BackgroundEidGenerator::GenerateNearestEids( | 69 std::vector<EidDataWithTimestamp> BackgroundEidGenerator::GenerateNearestEids( |
| 58 const std::vector<BeaconSeed>& beacon_seeds) const { | 70 const std::vector<BeaconSeed>& beacon_seeds) const { |
| 59 int64_t now_timestamp_ms = clock_->Now().ToJavaTime(); | 71 int64_t now_timestamp_ms = clock_->Now().ToJavaTime(); |
| 60 std::vector<std::string> eids; | 72 std::vector<EidDataWithTimestamp> eids; |
| 61 | 73 |
| 62 PA_LOG(INFO) << "Generating EIDs:"; | |
| 63 for (int i = -kEidLookAhead; i <= kEidLookAhead; ++i) { | 74 for (int i = -kEidLookAhead; i <= kEidLookAhead; ++i) { |
| 64 int64_t timestamp_ms = now_timestamp_ms + i * kEidPeriodMs; | 75 int64_t timestamp_ms = now_timestamp_ms + i * kEidPeriodMs; |
| 65 std::unique_ptr<std::string> eid = GenerateEid(timestamp_ms, beacon_seeds); | 76 std::unique_ptr<EidDataWithTimestamp> eid = |
| 77 GenerateEid(timestamp_ms, beacon_seeds); | |
| 66 if (eid) | 78 if (eid) |
| 67 eids.push_back(*eid); | 79 eids.push_back(*eid); |
| 68 } | 80 } |
| 69 | 81 |
| 82 PA_LOG(INFO) << "Generated EIDs: " << EidsToDebugString(eids); | |
| 70 return eids; | 83 return eids; |
| 71 } | 84 } |
| 72 | 85 |
| 73 std::unique_ptr<std::string> BackgroundEidGenerator::GenerateEid( | 86 std::unique_ptr<EidDataWithTimestamp> BackgroundEidGenerator::GenerateEid( |
| 74 int64_t timestamp_ms, | 87 int64_t timestamp_ms, |
| 75 const std::vector<BeaconSeed>& beacon_seeds) const { | 88 const std::vector<BeaconSeed>& beacon_seeds) const { |
| 76 const BeaconSeed* beacon_seed = | 89 const BeaconSeed* beacon_seed = |
| 77 GetBeaconSeedForTimestamp(timestamp_ms, beacon_seeds); | 90 GetBeaconSeedForTimestamp(timestamp_ms, beacon_seeds); |
| 78 if (!beacon_seed) { | 91 if (!beacon_seed) { |
| 79 PA_LOG(INFO) << " " << timestamp_ms << ": outside of BeaconSeed range."; | 92 PA_LOG(INFO) << " " << timestamp_ms << ": outside of BeaconSeed range."; |
| 80 return nullptr; | 93 return nullptr; |
| 81 } | 94 } |
| 82 | 95 |
| 83 int64_t seed_start_time_ms = beacon_seed->start_time_millis(); | 96 int64_t seed_start_time_ms = beacon_seed->start_time_millis(); |
| 84 int64_t offset_time_ms = timestamp_ms - seed_start_time_ms; | 97 int64_t offset_time_ms = timestamp_ms - seed_start_time_ms; |
| 85 int64_t start_of_period_ms = | 98 int64_t start_of_period_ms = |
| 86 seed_start_time_ms + (offset_time_ms / kEidPeriodMs) * kEidPeriodMs; | 99 seed_start_time_ms + (offset_time_ms / kEidPeriodMs) * kEidPeriodMs; |
| 87 | 100 |
| 88 std::string eid = raw_eid_generator_->GenerateEid( | 101 std::string eid = raw_eid_generator_->GenerateEid( |
| 89 beacon_seed->data(), start_of_period_ms, nullptr); | 102 beacon_seed->data(), start_of_period_ms, nullptr); |
| 90 | 103 |
| 91 PA_LOG(INFO) << " " << start_of_period_ms << ": " << eid; | 104 return base::MakeUnique<EidDataWithTimestamp>( |
| 92 return base::MakeUnique<std::string>(eid); | 105 eid, start_of_period_ms, start_of_period_ms + kEidPeriodMs); |
| 93 } | 106 } |
| 94 | 107 |
| 95 } // cryptauth | 108 } // cryptauth |
| OLD | NEW |