OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/policy/user_network_configuration_updater.h" | 5 #include "chrome/browser/chromeos/policy/user_network_configuration_updater.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/chromeos/login/user.h" | 12 #include "chrome/browser/chromeos/login/user.h" |
11 #include "chrome/browser/chromeos/net/onc_utils.h" | 13 #include "chrome/browser/chromeos/net/onc_utils.h" |
| 14 #include "chrome/browser/net/nss_context.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
12 #include "chromeos/network/managed_network_configuration_handler.h" | 16 #include "chromeos/network/managed_network_configuration_handler.h" |
13 #include "chromeos/network/onc/onc_certificate_importer.h" | 17 #include "chromeos/network/onc/onc_certificate_importer_impl.h" |
14 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/notification_source.h" |
15 #include "net/cert/x509_certificate.h" | 20 #include "net/cert/x509_certificate.h" |
16 #include "policy/policy_constants.h" | 21 #include "policy/policy_constants.h" |
17 | 22 |
| 23 namespace { |
| 24 |
| 25 bool skip_certificate_importer_creation_for_test = false; |
| 26 |
| 27 } // namespace |
| 28 |
18 namespace policy { | 29 namespace policy { |
19 | 30 |
20 UserNetworkConfigurationUpdater::~UserNetworkConfigurationUpdater() {} | 31 UserNetworkConfigurationUpdater::~UserNetworkConfigurationUpdater() {} |
21 | 32 |
22 // static | 33 // static |
23 scoped_ptr<UserNetworkConfigurationUpdater> | 34 scoped_ptr<UserNetworkConfigurationUpdater> |
24 UserNetworkConfigurationUpdater::CreateForUserPolicy( | 35 UserNetworkConfigurationUpdater::CreateForUserPolicy( |
| 36 Profile* profile, |
25 bool allow_trusted_certs_from_policy, | 37 bool allow_trusted_certs_from_policy, |
26 const chromeos::User& user, | 38 const chromeos::User& user, |
27 scoped_ptr<chromeos::onc::CertificateImporter> certificate_importer, | |
28 PolicyService* policy_service, | 39 PolicyService* policy_service, |
29 chromeos::ManagedNetworkConfigurationHandler* network_config_handler) { | 40 chromeos::ManagedNetworkConfigurationHandler* network_config_handler) { |
30 scoped_ptr<UserNetworkConfigurationUpdater> updater( | 41 scoped_ptr<UserNetworkConfigurationUpdater> updater( |
31 new UserNetworkConfigurationUpdater(allow_trusted_certs_from_policy, | 42 new UserNetworkConfigurationUpdater(profile, |
| 43 allow_trusted_certs_from_policy, |
32 user, | 44 user, |
33 certificate_importer.Pass(), | |
34 policy_service, | 45 policy_service, |
35 network_config_handler)); | 46 network_config_handler)); |
36 updater->Init(); | 47 updater->Init(); |
37 return updater.Pass(); | 48 return updater.Pass(); |
38 } | 49 } |
39 | 50 |
40 void UserNetworkConfigurationUpdater::AddTrustedCertsObserver( | 51 void UserNetworkConfigurationUpdater::AddTrustedCertsObserver( |
41 WebTrustedCertsObserver* observer) { | 52 WebTrustedCertsObserver* observer) { |
42 observer_list_.AddObserver(observer); | 53 observer_list_.AddObserver(observer); |
43 } | 54 } |
44 | 55 |
45 void UserNetworkConfigurationUpdater::RemoveTrustedCertsObserver( | 56 void UserNetworkConfigurationUpdater::RemoveTrustedCertsObserver( |
46 WebTrustedCertsObserver* observer) { | 57 WebTrustedCertsObserver* observer) { |
47 observer_list_.RemoveObserver(observer); | 58 observer_list_.RemoveObserver(observer); |
48 } | 59 } |
49 | 60 |
50 UserNetworkConfigurationUpdater::UserNetworkConfigurationUpdater( | 61 UserNetworkConfigurationUpdater::UserNetworkConfigurationUpdater( |
| 62 Profile* profile, |
51 bool allow_trusted_certs_from_policy, | 63 bool allow_trusted_certs_from_policy, |
52 const chromeos::User& user, | 64 const chromeos::User& user, |
53 scoped_ptr<chromeos::onc::CertificateImporter> certificate_importer, | |
54 PolicyService* policy_service, | 65 PolicyService* policy_service, |
55 chromeos::ManagedNetworkConfigurationHandler* network_config_handler) | 66 chromeos::ManagedNetworkConfigurationHandler* network_config_handler) |
56 : NetworkConfigurationUpdater(onc::ONC_SOURCE_USER_POLICY, | 67 : NetworkConfigurationUpdater(onc::ONC_SOURCE_USER_POLICY, |
57 key::kOpenNetworkConfiguration, | 68 key::kOpenNetworkConfiguration, |
58 certificate_importer.Pass(), | |
59 policy_service, | 69 policy_service, |
60 network_config_handler), | 70 network_config_handler), |
61 allow_trusted_certificates_from_policy_(allow_trusted_certs_from_policy), | 71 allow_trusted_certificates_from_policy_(allow_trusted_certs_from_policy), |
62 user_(&user) {} | 72 user_(&user), |
| 73 weak_factory_(this) { |
| 74 // The updater is created with |certificate_importer_| unset and is |
| 75 // responsible for creating it. This requires |GetNSSCertDatabaseForProfile| |
| 76 // call, which is not safe before the profile initialization is finalized. |
| 77 // Thus, listen for PROFILE_ADDED notification, on which |cert_importer_| |
| 78 // creation should start. This behaviour can be disabled in tests. |
| 79 if (!skip_certificate_importer_creation_for_test) { |
| 80 registrar_.Add(this, |
| 81 chrome::NOTIFICATION_PROFILE_ADDED, |
| 82 content::Source<Profile>(profile)); |
| 83 } |
| 84 } |
| 85 |
| 86 void UserNetworkConfigurationUpdater::SetCertificateImporterForTest( |
| 87 scoped_ptr<chromeos::onc::CertificateImporter> certificate_importer) { |
| 88 SetCertificateImporter(certificate_importer.Pass()); |
| 89 } |
| 90 |
| 91 // static |
| 92 void UserNetworkConfigurationUpdater:: |
| 93 SetSkipCertificateImporterCreationForTest(bool skip) { |
| 94 skip_certificate_importer_creation_for_test = skip; |
| 95 } |
63 | 96 |
64 void UserNetworkConfigurationUpdater::GetWebTrustedCertificates( | 97 void UserNetworkConfigurationUpdater::GetWebTrustedCertificates( |
65 net::CertificateList* certs) const { | 98 net::CertificateList* certs) const { |
66 *certs = web_trust_certs_; | 99 *certs = web_trust_certs_; |
67 } | 100 } |
68 | 101 |
69 void UserNetworkConfigurationUpdater::ImportCertificates( | 102 void UserNetworkConfigurationUpdater::ImportCertificates( |
70 const base::ListValue& certificates_onc) { | 103 const base::ListValue& certificates_onc) { |
| 104 // If certificate importer is not yet set, cache the certificate onc. It will |
| 105 // be imported when the certificate importer gets set. |
| 106 if (!certificate_importer_) { |
| 107 pending_certificates_onc_.reset(certificates_onc.DeepCopy()); |
| 108 return; |
| 109 } |
| 110 |
71 web_trust_certs_.clear(); | 111 web_trust_certs_.clear(); |
72 certificate_importer_->ImportCertificates( | 112 certificate_importer_->ImportCertificates( |
73 certificates_onc, | 113 certificates_onc, |
74 onc_source_, | 114 onc_source_, |
75 allow_trusted_certificates_from_policy_ ? &web_trust_certs_ : NULL); | 115 allow_trusted_certificates_from_policy_ ? &web_trust_certs_ : NULL); |
76 | 116 |
77 NotifyTrustAnchorsChanged(); | 117 NotifyTrustAnchorsChanged(); |
78 } | 118 } |
79 | 119 |
80 void UserNetworkConfigurationUpdater::ApplyNetworkPolicy( | 120 void UserNetworkConfigurationUpdater::ApplyNetworkPolicy( |
81 base::ListValue* network_configs_onc, | 121 base::ListValue* network_configs_onc, |
82 base::DictionaryValue* global_network_config) { | 122 base::DictionaryValue* global_network_config) { |
83 DCHECK(user_); | 123 DCHECK(user_); |
84 chromeos::onc::ExpandStringPlaceholdersInNetworksForUser(user_, | 124 chromeos::onc::ExpandStringPlaceholdersInNetworksForUser(user_, |
85 network_configs_onc); | 125 network_configs_onc); |
86 network_config_handler_->SetPolicy(onc_source_, | 126 network_config_handler_->SetPolicy(onc_source_, |
87 user_->username_hash(), | 127 user_->username_hash(), |
88 *network_configs_onc, | 128 *network_configs_onc, |
89 *global_network_config); | 129 *global_network_config); |
90 } | 130 } |
91 | 131 |
| 132 void UserNetworkConfigurationUpdater::Observe( |
| 133 int type, |
| 134 const content::NotificationSource& source, |
| 135 const content::NotificationDetails& details) { |
| 136 DCHECK_EQ(type, chrome::NOTIFICATION_PROFILE_ADDED); |
| 137 Profile* profile = content::Source<Profile>(source).ptr(); |
| 138 |
| 139 if (skip_certificate_importer_creation_for_test) |
| 140 return; |
| 141 |
| 142 GetNSSCertDatabaseForProfile( |
| 143 profile, |
| 144 base::Bind( |
| 145 &UserNetworkConfigurationUpdater::CreateAndSetCertificateImporter, |
| 146 weak_factory_.GetWeakPtr())); |
| 147 } |
| 148 |
| 149 void UserNetworkConfigurationUpdater::CreateAndSetCertificateImporter( |
| 150 net::NSSCertDatabase* database) { |
| 151 DCHECK(database); |
| 152 SetCertificateImporter(scoped_ptr<chromeos::onc::CertificateImporter>( |
| 153 new chromeos::onc::CertificateImporterImpl(database))); |
| 154 } |
| 155 |
| 156 void UserNetworkConfigurationUpdater::SetCertificateImporter( |
| 157 scoped_ptr<chromeos::onc::CertificateImporter> certificate_importer) { |
| 158 certificate_importer_ = certificate_importer.Pass(); |
| 159 |
| 160 if (pending_certificates_onc_) |
| 161 ImportCertificates(*pending_certificates_onc_); |
| 162 pending_certificates_onc_.reset(); |
| 163 } |
| 164 |
92 void UserNetworkConfigurationUpdater::NotifyTrustAnchorsChanged() { | 165 void UserNetworkConfigurationUpdater::NotifyTrustAnchorsChanged() { |
93 FOR_EACH_OBSERVER(WebTrustedCertsObserver, | 166 FOR_EACH_OBSERVER(WebTrustedCertsObserver, |
94 observer_list_, | 167 observer_list_, |
95 OnTrustAnchorsChanged(web_trust_certs_)); | 168 OnTrustAnchorsChanged(web_trust_certs_)); |
96 } | 169 } |
97 | 170 |
98 } // namespace policy | 171 } // namespace policy |
OLD | NEW |