| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chromeos/cert_loader.h" | 5 #include "chromeos/cert_loader.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
| 14 #include "base/threading/worker_pool.h" | 14 #include "base/threading/worker_pool.h" |
| 15 #include "crypto/nss_util.h" | 15 #include "crypto/nss_util.h" |
| 16 #include "net/cert/nss_cert_database.h" | 16 #include "net/cert/nss_cert_database.h" |
| 17 #include "net/cert/nss_cert_database_chromeos.h" | 17 #include "net/cert/nss_cert_database_chromeos.h" |
| 18 #include "net/cert/x509_certificate.h" | 18 #include "net/cert/x509_certificate.h" |
| 19 | 19 |
| 20 namespace chromeos { | 20 namespace chromeos { |
| 21 | 21 |
| 22 namespace { | |
| 23 | |
| 24 // Loads certificates from |cert_database| into |cert_list|. | |
| 25 void LoadNSSCertificates(net::NSSCertDatabase* cert_database, | |
| 26 net::CertificateList* cert_list) { | |
| 27 cert_database->ListCerts(cert_list); | |
| 28 } | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 static CertLoader* g_cert_loader = NULL; | 22 static CertLoader* g_cert_loader = NULL; |
| 33 | 23 |
| 34 // static | 24 // static |
| 35 void CertLoader::Initialize() { | 25 void CertLoader::Initialize() { |
| 36 CHECK(!g_cert_loader); | 26 CHECK(!g_cert_loader); |
| 37 g_cert_loader = new CertLoader(); | 27 g_cert_loader = new CertLoader(); |
| 38 } | 28 } |
| 39 | 29 |
| 40 // static | 30 // static |
| 41 void CertLoader::Shutdown() { | 31 void CertLoader::Shutdown() { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 // directly, as |database_| observers receive only events generated directly | 67 // directly, as |database_| observers receive only events generated directly |
| 78 // by |database_|, so they may miss a few relevant ones. | 68 // by |database_|, so they may miss a few relevant ones. |
| 79 // TODO(tbarzic): Once singleton NSSCertDatabase is removed, investigate if | 69 // TODO(tbarzic): Once singleton NSSCertDatabase is removed, investigate if |
| 80 // it would be OK to observe |database_| directly; or change NSSCertDatabase | 70 // it would be OK to observe |database_| directly; or change NSSCertDatabase |
| 81 // to send notification on all relevant changes. | 71 // to send notification on all relevant changes. |
| 82 net::CertDatabase::GetInstance()->AddObserver(this); | 72 net::CertDatabase::GetInstance()->AddObserver(this); |
| 83 | 73 |
| 84 LoadCertificates(); | 74 LoadCertificates(); |
| 85 } | 75 } |
| 86 | 76 |
| 87 void CertLoader::SetSlowTaskRunnerForTest( | |
| 88 const scoped_refptr<base::TaskRunner>& task_runner) { | |
| 89 slow_task_runner_for_test_ = task_runner; | |
| 90 } | |
| 91 | |
| 92 void CertLoader::AddObserver(CertLoader::Observer* observer) { | 77 void CertLoader::AddObserver(CertLoader::Observer* observer) { |
| 93 observers_.AddObserver(observer); | 78 observers_.AddObserver(observer); |
| 94 } | 79 } |
| 95 | 80 |
| 96 void CertLoader::RemoveObserver(CertLoader::Observer* observer) { | 81 void CertLoader::RemoveObserver(CertLoader::Observer* observer) { |
| 97 observers_.RemoveObserver(observer); | 82 observers_.RemoveObserver(observer); |
| 98 } | 83 } |
| 99 | 84 |
| 100 int CertLoader::TPMTokenSlotID() const { | 85 int CertLoader::TPMTokenSlotID() const { |
| 101 if (!database_) | 86 if (!database_) |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 135 |
| 151 void CertLoader::LoadCertificates() { | 136 void CertLoader::LoadCertificates() { |
| 152 CHECK(thread_checker_.CalledOnValidThread()); | 137 CHECK(thread_checker_.CalledOnValidThread()); |
| 153 VLOG(1) << "LoadCertificates: " << certificates_update_running_; | 138 VLOG(1) << "LoadCertificates: " << certificates_update_running_; |
| 154 | 139 |
| 155 if (certificates_update_running_) { | 140 if (certificates_update_running_) { |
| 156 certificates_update_required_ = true; | 141 certificates_update_required_ = true; |
| 157 return; | 142 return; |
| 158 } | 143 } |
| 159 | 144 |
| 160 net::CertificateList* cert_list = new net::CertificateList; | |
| 161 certificates_update_running_ = true; | 145 certificates_update_running_ = true; |
| 162 certificates_update_required_ = false; | 146 certificates_update_required_ = false; |
| 163 | 147 |
| 164 base::TaskRunner* task_runner = slow_task_runner_for_test_.get(); | 148 database_->ListCerts( |
| 165 if (!task_runner) | |
| 166 task_runner = base::WorkerPool::GetTaskRunner(true /* task is slow */); | |
| 167 task_runner->PostTaskAndReply( | |
| 168 FROM_HERE, | |
| 169 base::Bind(LoadNSSCertificates, | |
| 170 // Create a copy of the database so it can be used on the | |
| 171 // worker pool. | |
| 172 // TODO(tbarzic): Make net::NSSCertDatabase::ListCerts async | |
| 173 // and change it to do the certificate listing on worker | |
| 174 // pool. | |
| 175 base::Owned(new net::NSSCertDatabaseChromeOS( | |
| 176 database_->GetPublicSlot(), | |
| 177 database_->GetPrivateSlot())), | |
| 178 cert_list), | |
| 179 base::Bind(&CertLoader::UpdateCertificates, | 149 base::Bind(&CertLoader::UpdateCertificates, |
| 180 weak_factory_.GetWeakPtr(), | 150 weak_factory_.GetWeakPtr())); |
| 181 base::Owned(cert_list))); | |
| 182 } | 151 } |
| 183 | 152 |
| 184 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) { | 153 void CertLoader::UpdateCertificates( |
| 154 scoped_ptr<net::CertificateList> cert_list) { |
| 185 CHECK(thread_checker_.CalledOnValidThread()); | 155 CHECK(thread_checker_.CalledOnValidThread()); |
| 186 DCHECK(certificates_update_running_); | 156 DCHECK(certificates_update_running_); |
| 187 VLOG(1) << "UpdateCertificates: " << cert_list->size(); | 157 VLOG(1) << "UpdateCertificates: " << cert_list->size(); |
| 188 | 158 |
| 189 // Ignore any existing certificates. | 159 // Ignore any existing certificates. |
| 190 cert_list_.swap(*cert_list); | 160 cert_list_.swap(*cert_list); |
| 191 | 161 |
| 192 bool initial_load = !certificates_loaded_; | 162 bool initial_load = !certificates_loaded_; |
| 193 certificates_loaded_ = true; | 163 certificates_loaded_ = true; |
| 194 NotifyCertificatesLoaded(initial_load); | 164 NotifyCertificatesLoaded(initial_load); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 214 VLOG(1) << "OnCertAdded"; | 184 VLOG(1) << "OnCertAdded"; |
| 215 LoadCertificates(); | 185 LoadCertificates(); |
| 216 } | 186 } |
| 217 | 187 |
| 218 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) { | 188 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) { |
| 219 VLOG(1) << "OnCertRemoved"; | 189 VLOG(1) << "OnCertRemoved"; |
| 220 LoadCertificates(); | 190 LoadCertificates(); |
| 221 } | 191 } |
| 222 | 192 |
| 223 } // namespace chromeos | 193 } // namespace chromeos |
| OLD | NEW |