xref: /aosp_15_r20/external/cronet/net/cert/cert_database.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/cert/cert_database.h"
6 
7 #include "base/metrics/histogram_functions.h"
8 #include "base/observer_list_threadsafe.h"
9 #include "build/build_config.h"
10 #include "net/log/net_log.h"
11 #include "net/log/net_log_values.h"
12 
13 namespace net {
14 
15 namespace {
16 
RecordNotificationHistogram(CertDatabase::HistogramNotificationType type)17 void RecordNotificationHistogram(CertDatabase::HistogramNotificationType type) {
18   base::UmaHistogramEnumeration("Net.Certificate.ChangeNotification", type);
19 }
20 
21 }  // namespace
22 
23 // static
GetInstance()24 CertDatabase* CertDatabase::GetInstance() {
25   static base::NoDestructor<CertDatabase> cert_database;
26   return cert_database.get();
27 }
28 
AddObserver(Observer * observer)29 void CertDatabase::AddObserver(Observer* observer) {
30   observer_list_->AddObserver(observer);
31 }
32 
RemoveObserver(Observer * observer)33 void CertDatabase::RemoveObserver(Observer* observer) {
34   observer_list_->RemoveObserver(observer);
35 }
36 
NotifyObserversTrustStoreChanged()37 void CertDatabase::NotifyObserversTrustStoreChanged() {
38   // Log to NetLog as it may help debug issues like https://crbug.com/915463
39   // This isn't guarded with net::NetLog::Get()->IsCapturing()) because an
40   // AddGlobalEntry() call without much computation is really cheap.
41   net::NetLog::Get()->AddGlobalEntry(
42       NetLogEventType::CERTIFICATE_DATABASE_TRUST_STORE_CHANGED);
43 
44   RecordNotificationHistogram(HistogramNotificationType::kTrust);
45 
46   observer_list_->Notify(FROM_HERE, &Observer::OnTrustStoreChanged);
47 }
48 
NotifyObserversClientCertStoreChanged()49 void CertDatabase::NotifyObserversClientCertStoreChanged() {
50   // Log to NetLog as it may help debug issues like https://crbug.com/915463
51   // This isn't guarded with net::NetLog::Get()->IsCapturing()) because an
52   // AddGlobalEntry() call without much computation is really cheap.
53   net::NetLog::Get()->AddGlobalEntry(
54       NetLogEventType::CERTIFICATE_DATABASE_CLIENT_CERT_STORE_CHANGED);
55 
56   RecordNotificationHistogram(HistogramNotificationType::kClientCert);
57 
58   observer_list_->Notify(FROM_HERE, &Observer::OnClientCertStoreChanged);
59 }
60 
CertDatabase()61 CertDatabase::CertDatabase()
62     : observer_list_(
63           base::MakeRefCounted<base::ObserverListThreadSafe<Observer>>()) {}
64 
65 }  // namespace net
66