Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / policy_cert_service.cc
blobd966fdc6b3c986b7f7f3c3ce3699c283ff7438ce
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.
5 #include "chrome/browser/chromeos/policy/policy_cert_service.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/logging.h"
10 #include "chrome/browser/chromeos/login/user_manager.h"
11 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
12 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "net/cert/x509_certificate.h"
16 namespace policy {
18 PolicyCertService::~PolicyCertService() {
19 DCHECK(cert_verifier_)
20 << "CreatePolicyCertVerifier() must be called after construction.";
23 PolicyCertService::PolicyCertService(
24 const std::string& user_id,
25 UserNetworkConfigurationUpdater* net_conf_updater,
26 chromeos::UserManager* user_manager)
27 : cert_verifier_(NULL),
28 user_id_(user_id),
29 net_conf_updater_(net_conf_updater),
30 user_manager_(user_manager),
31 has_trust_anchors_(false),
32 weak_ptr_factory_(this) {
33 DCHECK(net_conf_updater_);
34 DCHECK(user_manager_);
37 PolicyCertService::PolicyCertService(const std::string& user_id,
38 PolicyCertVerifier* verifier,
39 chromeos::UserManager* user_manager)
40 : cert_verifier_(verifier),
41 user_id_(user_id),
42 net_conf_updater_(NULL),
43 user_manager_(user_manager),
44 has_trust_anchors_(false),
45 weak_ptr_factory_(this) {}
47 scoped_ptr<PolicyCertVerifier> PolicyCertService::CreatePolicyCertVerifier() {
48 base::Closure callback = base::Bind(
49 &PolicyCertServiceFactory::SetUsedPolicyCertificates, user_id_);
50 cert_verifier_ = new PolicyCertVerifier(
51 base::Bind(base::IgnoreResult(&content::BrowserThread::PostTask),
52 content::BrowserThread::UI,
53 FROM_HERE,
54 callback));
55 // Certs are forwarded to |cert_verifier_|, thus register here after
56 // |cert_verifier_| is created.
57 net_conf_updater_->AddTrustedCertsObserver(this);
59 // Set the current list of trust anchors.
60 net::CertificateList trust_anchors;
61 net_conf_updater_->GetWebTrustedCertificates(&trust_anchors);
62 OnTrustAnchorsChanged(trust_anchors);
64 return make_scoped_ptr(cert_verifier_);
67 void PolicyCertService::OnTrustAnchorsChanged(
68 const net::CertificateList& trust_anchors) {
69 DCHECK(cert_verifier_);
71 // Do not use certificates installed via ONC policy if the current session has
72 // multiple profiles. This is important to make sure that any possibly tainted
73 // data is absolutely confined to the managed profile and never, ever leaks to
74 // any other profile.
75 if (!trust_anchors.empty() && user_manager_->GetLoggedInUsers().size() > 1u) {
76 LOG(ERROR) << "Ignoring ONC-pushed certificates update because multiple "
77 << "users are logged in.";
78 return;
81 has_trust_anchors_ = !trust_anchors.empty();
83 // It's safe to use base::Unretained here, because it's guaranteed that
84 // |cert_verifier_| outlives this object (see description of
85 // CreatePolicyCertVerifier).
86 // Note: ProfileIOData, which owns the CertVerifier is deleted by a
87 // DeleteSoon on IO, i.e. after all pending tasks on IO are finished.
88 content::BrowserThread::PostTask(
89 content::BrowserThread::IO,
90 FROM_HERE,
91 base::Bind(&PolicyCertVerifier::SetTrustAnchors,
92 base::Unretained(cert_verifier_),
93 trust_anchors));
96 bool PolicyCertService::UsedPolicyCertificates() const {
97 return PolicyCertServiceFactory::UsedPolicyCertificates(user_id_);
100 void PolicyCertService::Shutdown() {
101 weak_ptr_factory_.InvalidateWeakPtrs();
102 if (net_conf_updater_)
103 net_conf_updater_->RemoveTrustedCertsObserver(this);
104 OnTrustAnchorsChanged(net::CertificateList());
105 net_conf_updater_ = NULL;
108 // static
109 scoped_ptr<PolicyCertService> PolicyCertService::CreateForTesting(
110 const std::string& user_id,
111 PolicyCertVerifier* verifier,
112 chromeos::UserManager* user_manager) {
113 return make_scoped_ptr(
114 new PolicyCertService(user_id, verifier, user_manager));
117 } // namespace policy