Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: chromeos/cert_loader.cc

Issue 144423007: Make NSSCertDatabase::ListCerts work async on a worker thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: aa Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 std::string pkcs11_id; 83 std::string pkcs11_id;
94 if (sec_item) { 84 if (sec_item) {
95 pkcs11_id = base::HexEncode(sec_item->data, sec_item->len); 85 pkcs11_id = base::HexEncode(sec_item->data, sec_item->len);
96 SECITEM_FreeItem(sec_item, PR_TRUE); 86 SECITEM_FreeItem(sec_item, PR_TRUE);
97 } 87 }
98 SECKEY_DestroyPrivateKey(priv_key); 88 SECKEY_DestroyPrivateKey(priv_key);
99 89
100 return pkcs11_id; 90 return pkcs11_id;
101 } 91 }
102 92
103 void CertLoader::SetSlowTaskRunnerForTest(
104 const scoped_refptr<base::TaskRunner>& task_runner) {
105 slow_task_runner_for_test_ = task_runner;
106 }
107
108 void CertLoader::AddObserver(CertLoader::Observer* observer) { 93 void CertLoader::AddObserver(CertLoader::Observer* observer) {
109 observers_.AddObserver(observer); 94 observers_.AddObserver(observer);
110 } 95 }
111 96
112 void CertLoader::RemoveObserver(CertLoader::Observer* observer) { 97 void CertLoader::RemoveObserver(CertLoader::Observer* observer) {
113 observers_.RemoveObserver(observer); 98 observers_.RemoveObserver(observer);
114 } 99 }
115 100
116 bool CertLoader::IsCertificateHardwareBacked( 101 bool CertLoader::IsCertificateHardwareBacked(
117 const net::X509Certificate* cert) const { 102 const net::X509Certificate* cert) const {
(...skipping 29 matching lines...) Expand all
147 132
148 void CertLoader::LoadCertificates() { 133 void CertLoader::LoadCertificates() {
149 CHECK(thread_checker_.CalledOnValidThread()); 134 CHECK(thread_checker_.CalledOnValidThread());
150 VLOG(1) << "LoadCertificates: " << certificates_update_running_; 135 VLOG(1) << "LoadCertificates: " << certificates_update_running_;
151 136
152 if (certificates_update_running_) { 137 if (certificates_update_running_) {
153 certificates_update_required_ = true; 138 certificates_update_required_ = true;
154 return; 139 return;
155 } 140 }
156 141
157 net::CertificateList* cert_list = new net::CertificateList;
158 certificates_update_running_ = true; 142 certificates_update_running_ = true;
159 certificates_update_required_ = false; 143 certificates_update_required_ = false;
160 144
161 base::TaskRunner* task_runner = slow_task_runner_for_test_.get(); 145 database_->ListCerts(base::Bind(&CertLoader::UpdateCertificates,
162 if (!task_runner) 146 weak_factory_.GetWeakPtr()));
163 task_runner = base::WorkerPool::GetTaskRunner(true /* task is slow */);
164 task_runner->PostTaskAndReply(
165 FROM_HERE,
166 base::Bind(LoadNSSCertificates,
167 // Create a copy of the database so it can be used on the
168 // worker pool.
169 base::Owned(new net::NSSCertDatabaseChromeOS(
170 database_->GetPublicSlot(),
171 database_->GetPrivateSlot())),
172 cert_list),
173 base::Bind(&CertLoader::UpdateCertificates,
174 weak_factory_.GetWeakPtr(),
175 base::Owned(cert_list)));
176 } 147 }
177 148
178 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) { 149 void CertLoader::UpdateCertificates(
150 scoped_ptr<net::CertificateList> cert_list) {
179 CHECK(thread_checker_.CalledOnValidThread()); 151 CHECK(thread_checker_.CalledOnValidThread());
180 DCHECK(certificates_update_running_); 152 DCHECK(certificates_update_running_);
181 VLOG(1) << "UpdateCertificates: " << cert_list->size(); 153 VLOG(1) << "UpdateCertificates: " << cert_list->size();
182 154
183 // Ignore any existing certificates. 155 // Ignore any existing certificates.
184 cert_list_.swap(*cert_list); 156 cert_list_.swap(*cert_list);
185 157
186 bool initial_load = !certificates_loaded_; 158 bool initial_load = !certificates_loaded_;
187 certificates_loaded_ = true; 159 certificates_loaded_ = true;
188 NotifyCertificatesLoaded(initial_load); 160 NotifyCertificatesLoaded(initial_load);
(...skipping 19 matching lines...) Expand all
208 VLOG(1) << "OnCertAdded"; 180 VLOG(1) << "OnCertAdded";
209 LoadCertificates(); 181 LoadCertificates();
210 } 182 }
211 183
212 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) { 184 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) {
213 VLOG(1) << "OnCertRemoved"; 185 VLOG(1) << "OnCertRemoved";
214 LoadCertificates(); 186 LoadCertificates();
215 } 187 }
216 188
217 } // namespace chromeos 189 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698