| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/cros/cert_library.h" | 5 #include "chrome/browser/chromeos/cros/cert_library.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/observer_list_threadsafe.h" | 10 #include "base/observer_list_threadsafe.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 } | 92 } |
| 93 | 93 |
| 94 } // namespace | 94 } // namespace |
| 95 | 95 |
| 96 ////////////////////////////////////////////////////////////////////////////// | 96 ////////////////////////////////////////////////////////////////////////////// |
| 97 | 97 |
| 98 namespace chromeos { | 98 namespace chromeos { |
| 99 | 99 |
| 100 ////////////////////////////////////////////////////////////////////////////// | 100 ////////////////////////////////////////////////////////////////////////////// |
| 101 | 101 |
| 102 // base::Unretained(this) in the class is safe. By the time this object is |
| 103 // deleted as part of CrosLibrary, the DB thread and the UI message loop |
| 104 // are already terminated. |
| 102 class CertLibraryImpl | 105 class CertLibraryImpl |
| 103 : public CertLibrary, | 106 : public CertLibrary, |
| 104 public net::CertDatabase::Observer { | 107 public net::CertDatabase::Observer { |
| 105 public: | 108 public: |
| 106 typedef ObserverListThreadSafe<CertLibrary::Observer> CertLibraryObserverList; | 109 typedef ObserverListThreadSafe<CertLibrary::Observer> CertLibraryObserverList; |
| 107 | 110 |
| 108 CertLibraryImpl() : | 111 CertLibraryImpl() : |
| 109 observer_list_(new CertLibraryObserverList), | 112 observer_list_(new CertLibraryObserverList), |
| 110 user_logged_in_(false), | 113 user_logged_in_(false), |
| 111 certificates_requested_(false), | 114 certificates_requested_(false), |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 233 } | 236 } |
| 234 | 237 |
| 235 virtual void OnUserCertAdded(const net::X509Certificate* cert) OVERRIDE { | 238 virtual void OnUserCertAdded(const net::X509Certificate* cert) OVERRIDE { |
| 236 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 237 // Only load certificates if we have completed an initial request. | 240 // Only load certificates if we have completed an initial request. |
| 238 if (certificates_loaded_) { | 241 if (certificates_loaded_) { |
| 239 BrowserThread::PostTask( | 242 BrowserThread::PostTask( |
| 240 BrowserThread::DB, FROM_HERE, | 243 BrowserThread::DB, FROM_HERE, |
| 241 base::Bind(&CertLibraryImpl::LoadCertificates, | 244 base::Bind(&CertLibraryImpl::LoadCertificates, |
| 242 weak_ptr_factory_.GetWeakPtr())); | 245 base::Unretained(this))); |
| 243 } | 246 } |
| 244 } | 247 } |
| 245 | 248 |
| 246 virtual void OnUserCertRemoved(const net::X509Certificate* cert) OVERRIDE { | 249 virtual void OnUserCertRemoved(const net::X509Certificate* cert) OVERRIDE { |
| 247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 248 // Only load certificates if we have completed an initial request. | 251 // Only load certificates if we have completed an initial request. |
| 249 if (certificates_loaded_) { | 252 if (certificates_loaded_) { |
| 250 BrowserThread::PostTask( | 253 BrowserThread::PostTask( |
| 251 BrowserThread::DB, FROM_HERE, | 254 BrowserThread::DB, FROM_HERE, |
| 252 base::Bind(&CertLibraryImpl::LoadCertificates, | 255 base::Bind(&CertLibraryImpl::LoadCertificates, |
| 253 weak_ptr_factory_.GetWeakPtr())); | 256 base::Unretained(this))); |
| 254 } | 257 } |
| 255 } | 258 } |
| 256 | 259 |
| 257 virtual const std::string& GetTpmTokenName() const OVERRIDE { | 260 virtual const std::string& GetTpmTokenName() const OVERRIDE { |
| 258 return tpm_token_name_; | 261 return tpm_token_name_; |
| 259 } | 262 } |
| 260 | 263 |
| 261 private: | 264 private: |
| 262 void LoadCertificates() { | 265 void LoadCertificates() { |
| 263 VLOG(1) << " Loading Certificates."; | 266 VLOG(1) << " Loading Certificates."; |
| 264 // Certificate fetch occurs on the DB thread. | 267 // Certificate fetch occurs on the DB thread. |
| 265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 266 net::CertDatabase cert_db; | 269 net::CertDatabase cert_db; |
| 267 net::CertificateList* cert_list = new net::CertificateList(); | 270 net::CertificateList* cert_list = new net::CertificateList(); |
| 268 cert_db.ListCerts(cert_list); | 271 cert_db.ListCerts(cert_list); |
| 269 // Pass the list to the UI thread to safely update the local lists. | 272 // Pass the list to the UI thread to safely update the local lists. |
| 270 BrowserThread::PostTask( | 273 BrowserThread::PostTask( |
| 271 BrowserThread::UI, FROM_HERE, | 274 BrowserThread::UI, FROM_HERE, |
| 272 base::Bind(&CertLibraryImpl::UpdateCertificates, | 275 base::Bind(&CertLibraryImpl::UpdateCertificates, |
| 273 weak_ptr_factory_.GetWeakPtr(), cert_list)); | 276 base::Unretained(this), cert_list)); |
| 274 } | 277 } |
| 275 | 278 |
| 276 // Comparison functor for locale-sensitive sorting of certificates by name. | 279 // Comparison functor for locale-sensitive sorting of certificates by name. |
| 277 class CertNameComparator { | 280 class CertNameComparator { |
| 278 public: | 281 public: |
| 279 explicit CertNameComparator(icu::Collator* collator) | 282 explicit CertNameComparator(icu::Collator* collator) |
| 280 : collator_(collator) { } | 283 : collator_(collator) { } |
| 281 | 284 |
| 282 bool operator()(const scoped_refptr<net::X509Certificate>& lhs, | 285 bool operator()(const scoped_refptr<net::X509Certificate>& lhs, |
| 283 const scoped_refptr<net::X509Certificate>& rhs) const { | 286 const scoped_refptr<net::X509Certificate>& rhs) const { |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 } | 406 } |
| 404 return; | 407 return; |
| 405 } | 408 } |
| 406 // TPM is not enabled, so proceed with empty tpm token name. | 409 // TPM is not enabled, so proceed with empty tpm token name. |
| 407 VLOG(1) << "TPM not available."; | 410 VLOG(1) << "TPM not available."; |
| 408 } | 411 } |
| 409 | 412 |
| 410 // tpm_token_name_ is set, load the certificates on the DB thread. | 413 // tpm_token_name_ is set, load the certificates on the DB thread. |
| 411 BrowserThread::PostTask( | 414 BrowserThread::PostTask( |
| 412 BrowserThread::DB, FROM_HERE, | 415 BrowserThread::DB, FROM_HERE, |
| 413 base::Bind(&CertLibraryImpl::LoadCertificates, | 416 base::Bind(&CertLibraryImpl::LoadCertificates, base::Unretained(this))); |
| 414 weak_ptr_factory_.GetWeakPtr())); | |
| 415 } | 417 } |
| 416 | 418 |
| 417 // Observers. | 419 // Observers. |
| 418 const scoped_refptr<CertLibraryObserverList> observer_list_; | 420 const scoped_refptr<CertLibraryObserverList> observer_list_; |
| 419 | 421 |
| 420 // Active request task for re-requests while waiting for TPM init. | 422 // Active request task for re-requests while waiting for TPM init. |
| 421 base::Closure request_task_; | 423 base::Closure request_task_; |
| 422 | 424 |
| 423 // Cached TPM token name. | 425 // Cached TPM token name. |
| 424 std::string tpm_token_name_; | 426 std::string tpm_token_name_; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 net::X509Certificate* cert = GetCertificateAt(index); | 506 net::X509Certificate* cert = GetCertificateAt(index); |
| 505 net::X509Certificate::OSCertHandle cert_handle = cert->os_cert_handle(); | 507 net::X509Certificate::OSCertHandle cert_handle = cert->os_cert_handle(); |
| 506 std::string id = x509_certificate_model::GetPkcs11Id(cert_handle); | 508 std::string id = x509_certificate_model::GetPkcs11Id(cert_handle); |
| 507 if (id == pkcs11_id) | 509 if (id == pkcs11_id) |
| 508 return index; | 510 return index; |
| 509 } | 511 } |
| 510 return -1; // Not found. | 512 return -1; // Not found. |
| 511 } | 513 } |
| 512 | 514 |
| 513 } // chromeos | 515 } // chromeos |
| OLD | NEW |