| 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 "net/cert/sth_distributor.h" | 5 #include "net/cert/sth_distributor.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "net/cert/signed_tree_head.h" | 9 #include "net/cert/signed_tree_head.h" |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 const uint8_t kPilotLogID[] = {0xa4, 0xb9, 0x09, 0x90, 0xb4, 0x18, 0x58, 0x14, | 12 const uint8_t kPilotLogID[] = {0xa4, 0xb9, 0x09, 0x90, 0xb4, 0x18, 0x58, 0x14, |
| 13 0x87, 0xbb, 0x13, 0xa2, 0xcc, 0x67, 0x70, 0x0a, | 13 0x87, 0xbb, 0x13, 0xa2, 0xcc, 0x67, 0x70, 0x0a, |
| 14 0x3c, 0x35, 0x98, 0x04, 0xf9, 0x1b, 0xdf, 0xb8, | 14 0x3c, 0x35, 0x98, 0x04, 0xf9, 0x1b, 0xdf, 0xb8, |
| 15 0xe3, 0x77, 0xcd, 0x0e, 0xc8, 0x0d, 0xdc, 0x10}; | 15 0xe3, 0x77, 0xcd, 0x0e, 0xc8, 0x0d, 0xdc, 0x10}; |
| 16 } | 16 } |
| 17 | 17 |
| 18 namespace net { | 18 namespace net { |
| 19 | 19 |
| 20 namespace ct { | 20 namespace ct { |
| 21 | 21 |
| 22 STHDistributor::STHDistributor() | 22 STHDistributor::STHDistributor() |
| 23 : observer_list_(base::ObserverList<STHObserver>::NOTIFY_EXISTING_ONLY) {} | 23 : observer_list_(base::ObserverList<STHObserver>::NOTIFY_EXISTING_ONLY) {} |
| 24 | 24 |
| 25 STHDistributor::~STHDistributor() {} | 25 STHDistributor::~STHDistributor() {} |
| 26 | 26 |
| 27 void STHDistributor::NewSTHObserved(const SignedTreeHead& sth) { | 27 void STHDistributor::NewSTHObserved(const SignedTreeHead& sth) { |
| 28 auto it = std::find_if(observed_sths_.begin(), observed_sths_.end(), |
| 29 [&sth](const SignedTreeHead& other) { |
| 30 return sth.log_id == other.log_id; |
| 31 }); |
| 32 |
| 33 if (it == observed_sths_.end()) |
| 34 observed_sths_.push_back(sth); |
| 35 else |
| 36 *it = sth; |
| 37 |
| 28 FOR_EACH_OBSERVER(STHObserver, observer_list_, NewSTHObserved(sth)); | 38 FOR_EACH_OBSERVER(STHObserver, observer_list_, NewSTHObserved(sth)); |
| 29 | 39 |
| 30 if (sth.log_id.compare(0, sth.log_id.size(), | 40 if (sth.log_id.compare(0, sth.log_id.size(), |
| 31 reinterpret_cast<const char*>(kPilotLogID), | 41 reinterpret_cast<const char*>(kPilotLogID), |
| 32 sizeof(kPilotLogID)) != 0) | 42 sizeof(kPilotLogID)) != 0) |
| 33 return; | 43 return; |
| 34 | 44 |
| 35 const base::TimeDelta sth_age = base::Time::Now() - sth.timestamp; | 45 const base::TimeDelta sth_age = base::Time::Now() - sth.timestamp; |
| 36 UMA_HISTOGRAM_CUSTOM_TIMES("Net.CertificateTransparency.PilotSTHAge", sth_age, | 46 UMA_HISTOGRAM_CUSTOM_TIMES("Net.CertificateTransparency.PilotSTHAge", sth_age, |
| 37 base::TimeDelta::FromHours(1), | 47 base::TimeDelta::FromHours(1), |
| 38 base::TimeDelta::FromDays(4), 100); | 48 base::TimeDelta::FromDays(4), 100); |
| 39 } | 49 } |
| 40 | 50 |
| 41 void STHDistributor::RegisterObserver(STHObserver* observer) { | 51 void STHDistributor::RegisterObserver(STHObserver* observer) { |
| 42 observer_list_.AddObserver(observer); | 52 observer_list_.AddObserver(observer); |
| 53 // Make a local copy, because notifying the |observer| of a |
| 54 // new STH may result in this class being notified of a |
| 55 // (different) new STH, thus invalidating the iterator. |
| 56 std::vector<SignedTreeHead> local_sths(observed_sths_); |
| 57 |
| 58 for (const auto& sth : local_sths) |
| 59 observer->NewSTHObserved(sth); |
| 43 } | 60 } |
| 44 | 61 |
| 45 void STHDistributor::UnregisterObserver(STHObserver* observer) { | 62 void STHDistributor::UnregisterObserver(STHObserver* observer) { |
| 46 observer_list_.RemoveObserver(observer); | 63 observer_list_.RemoveObserver(observer); |
| 47 } | 64 } |
| 48 | 65 |
| 49 } // namespace ct | 66 } // namespace ct |
| 50 | 67 |
| 51 } // namespace net | 68 } // namespace net |
| OLD | NEW |