OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 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 "chrome/browser/chromeos/policy/policy_cert_service.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/prefs/pref_service.h" |
| 11 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h" |
| 12 #include "chrome/common/pref_names.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "net/cert/x509_certificate.h" |
| 15 |
| 16 namespace policy { |
| 17 |
| 18 PolicyCertService::~PolicyCertService() { |
| 19 DCHECK(cert_verifier_) |
| 20 << "CreatePolicyCertVerifier() must be called after construction."; |
| 21 } |
| 22 |
| 23 PolicyCertService::PolicyCertService( |
| 24 UserNetworkConfigurationUpdater* net_conf_updater, |
| 25 PrefService* user_prefs) |
| 26 : cert_verifier_(NULL), |
| 27 net_conf_updater_(net_conf_updater), |
| 28 user_prefs_(user_prefs), |
| 29 weak_ptr_factory_(this) { |
| 30 DCHECK(net_conf_updater_); |
| 31 DCHECK(user_prefs_); |
| 32 } |
| 33 |
| 34 scoped_ptr<PolicyCertVerifier> PolicyCertService::CreatePolicyCertVerifier() { |
| 35 base::Closure callback = |
| 36 base::Bind(&PolicyCertService::SetUsedPolicyCertificatesOnce, |
| 37 weak_ptr_factory_.GetWeakPtr()); |
| 38 cert_verifier_ = new PolicyCertVerifier( |
| 39 base::Bind(base::IgnoreResult(&content::BrowserThread::PostTask), |
| 40 content::BrowserThread::UI, |
| 41 FROM_HERE, |
| 42 callback)); |
| 43 // Certs are forwarded to |cert_verifier_|, thus register here after |
| 44 // |cert_verifier_| is created. |
| 45 net_conf_updater_->AddTrustedCertsObserver(this); |
| 46 |
| 47 // Set the current list of trust anchors. |
| 48 net::CertificateList trust_anchors; |
| 49 net_conf_updater_->GetWebTrustedCertificates(&trust_anchors); |
| 50 OnTrustAnchorsChanged(trust_anchors); |
| 51 |
| 52 return make_scoped_ptr(cert_verifier_); |
| 53 } |
| 54 |
| 55 void PolicyCertService::OnTrustAnchorsChanged( |
| 56 const net::CertificateList& trust_anchors) { |
| 57 DCHECK(cert_verifier_); |
| 58 // It's safe to use base::Unretained here, because it's guaranteed that |
| 59 // |cert_verifier_| outlives this object (see description of |
| 60 // CreatePolicyCertVerifier). |
| 61 // Note: ProfileIOData, which owns the CertVerifier is deleted by a |
| 62 // DeleteSoon on IO, i.e. after all pending tasks on IO are finished. |
| 63 content::BrowserThread::PostTask( |
| 64 content::BrowserThread::IO, |
| 65 FROM_HERE, |
| 66 base::Bind(&PolicyCertVerifier::SetTrustAnchors, |
| 67 base::Unretained(cert_verifier_), |
| 68 trust_anchors)); |
| 69 } |
| 70 |
| 71 bool PolicyCertService::UsedPolicyCertificates() const { |
| 72 return user_prefs_->GetBoolean(prefs::kUsedPolicyCertificatesOnce); |
| 73 } |
| 74 |
| 75 void PolicyCertService::Shutdown() { |
| 76 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 77 net_conf_updater_->RemoveTrustedCertsObserver(this); |
| 78 OnTrustAnchorsChanged(net::CertificateList()); |
| 79 net_conf_updater_ = NULL; |
| 80 user_prefs_ = NULL; |
| 81 } |
| 82 |
| 83 void PolicyCertService::SetUsedPolicyCertificatesOnce() { |
| 84 user_prefs_->SetBoolean(prefs::kUsedPolicyCertificatesOnce, true); |
| 85 } |
| 86 |
| 87 } // namespace policy |
OLD | NEW |