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

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: added bug # Created 6 years, 10 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/message_loop/message_loop_proxy.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
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
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(base::Bind(&CertLoader::UpdateCertificates,
165 if (!task_runner) 149 weak_factory_.GetWeakPtr()));
stevenjb 2014/02/04 22:28:42 nit: align
tbarzic 2014/02/04 23:12:38 Done.
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,
180 weak_factory_.GetWeakPtr(),
181 base::Owned(cert_list)));
182 } 150 }
183 151
184 void CertLoader::UpdateCertificates(net::CertificateList* cert_list) { 152 void CertLoader::UpdateCertificates(
153 scoped_ptr<net::CertificateList> cert_list) {
185 CHECK(thread_checker_.CalledOnValidThread()); 154 CHECK(thread_checker_.CalledOnValidThread());
186 DCHECK(certificates_update_running_); 155 DCHECK(certificates_update_running_);
187 VLOG(1) << "UpdateCertificates: " << cert_list->size(); 156 VLOG(1) << "UpdateCertificates: " << cert_list->size();
188 157
189 // Ignore any existing certificates. 158 // Ignore any existing certificates.
190 cert_list_.swap(*cert_list); 159 cert_list_.swap(*cert_list);
stevenjb 2014/02/04 22:28:42 cert_list.release() to make the ownership transfer
tbarzic 2014/02/04 23:12:38 It's not that bigger.. Done.
191 160
192 bool initial_load = !certificates_loaded_; 161 bool initial_load = !certificates_loaded_;
193 certificates_loaded_ = true; 162 certificates_loaded_ = true;
194 NotifyCertificatesLoaded(initial_load); 163 NotifyCertificatesLoaded(initial_load);
195 164
196 certificates_update_running_ = false; 165 certificates_update_running_ = false;
197 if (certificates_update_required_) 166 if (certificates_update_required_)
198 LoadCertificates(); 167 LoadCertificates();
199 } 168 }
200 169
(...skipping 13 matching lines...) Expand all
214 VLOG(1) << "OnCertAdded"; 183 VLOG(1) << "OnCertAdded";
215 LoadCertificates(); 184 LoadCertificates();
216 } 185 }
217 186
218 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) { 187 void CertLoader::OnCertRemoved(const net::X509Certificate* cert) {
219 VLOG(1) << "OnCertRemoved"; 188 VLOG(1) << "OnCertRemoved";
220 LoadCertificates(); 189 LoadCertificates();
221 } 190 }
222 191
223 } // namespace chromeos 192 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698