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 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_USER_NETWORK_CONFIGURATION_UPDATER_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_USER_NETWORK_CONFIGURATION_UPDATER_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "chrome/browser/chromeos/policy/network_configuration_updater.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
27 namespace user_manager
{
34 class CertificateImporter
;
39 class NSSCertDatabase
;
40 class X509Certificate
;
41 typedef std::vector
<scoped_refptr
<X509Certificate
> > CertificateList
;
48 // Implements additional special handling of ONC user policies. Namely string
49 // expansion with the user's name (or email address, etc.) and handling of "Web"
50 // trust of certificates.
51 class UserNetworkConfigurationUpdater
: public NetworkConfigurationUpdater
,
53 public content::NotificationObserver
{
55 class WebTrustedCertsObserver
{
57 // Is called everytime the list of imported certificates with Web trust is
59 virtual void OnTrustAnchorsChanged(
60 const net::CertificateList
& trust_anchors
) = 0;
63 ~UserNetworkConfigurationUpdater() override
;
65 // Creates an updater that applies the ONC user policy from |policy_service|
66 // for user |user| once the policy service is completely initialized and on
67 // each policy change. Imported certificates, that request it, are only
68 // granted Web trust if |allow_trusted_certs_from_policy| is true. A reference
69 // to |user| is stored. It must outlive the returned updater.
70 static scoped_ptr
<UserNetworkConfigurationUpdater
> CreateForUserPolicy(
72 bool allow_trusted_certs_from_policy
,
73 const user_manager::User
& user
,
74 PolicyService
* policy_service
,
75 chromeos::ManagedNetworkConfigurationHandler
* network_config_handler
);
77 void AddTrustedCertsObserver(WebTrustedCertsObserver
* observer
);
78 void RemoveTrustedCertsObserver(WebTrustedCertsObserver
* observer
);
80 // Sets |certs| to the list of Web trusted server and CA certificates from the
81 // last received policy.
82 void GetWebTrustedCertificates(net::CertificateList
* certs
) const;
84 // Helper method to expose |SetCertificateImporter| for usage in tests.
85 void SetCertificateImporterForTest(
86 scoped_ptr
<chromeos::onc::CertificateImporter
> certificate_importer
);
89 class CrosTrustAnchorProvider
;
91 UserNetworkConfigurationUpdater(
93 bool allow_trusted_certs_from_policy
,
94 const user_manager::User
& user
,
95 PolicyService
* policy_service
,
96 chromeos::ManagedNetworkConfigurationHandler
* network_config_handler
);
98 // Called by the CertificateImporter when an import finished.
99 void OnCertificatesImported(
101 const net::CertificateList
& onc_trusted_certificates
);
103 // NetworkConfigurationUpdater:
104 void ImportCertificates(const base::ListValue
& certificates_onc
) override
;
105 void ApplyNetworkPolicy(
106 base::ListValue
* network_configs_onc
,
107 base::DictionaryValue
* global_network_config
) override
;
109 // content::NotificationObserver implementation. Observes the profile to which
110 // |this| belongs to for PROFILE_ADDED notification.
111 void Observe(int type
,
112 const content::NotificationSource
& source
,
113 const content::NotificationDetails
& details
) override
;
115 // Creates onc::CertImporter with |database| and passes it to
116 // |SetCertificateImporter|.
117 void CreateAndSetCertificateImporter(net::NSSCertDatabase
* database
);
119 // Sets the certificate importer that should be used to import certificate
120 // policies. If there is |pending_certificates_onc_|, it gets imported.
121 void SetCertificateImporter(
122 scoped_ptr
<chromeos::onc::CertificateImporter
> certificate_importer
);
124 void NotifyTrustAnchorsChanged();
126 // Whether Web trust is allowed or not.
127 bool allow_trusted_certificates_from_policy_
;
129 // The user for whom the user policy will be applied.
130 const user_manager::User
* user_
;
132 ObserverList
<WebTrustedCertsObserver
, true> observer_list_
;
134 // Contains the certificates of the last import that requested web trust. Must
135 // be empty if Web trust from policy is not allowed.
136 net::CertificateList web_trust_certs_
;
138 // If |ImportCertificates| is called before |SetCertificateImporter|, gets set
139 // to a copy of the policy for which the import was requested.
140 // The policy will be processed when the certificate importer is set.
141 scoped_ptr
<base::ListValue
> pending_certificates_onc_
;
143 // Certificate importer to be used for importing policy defined certificates.
144 // Set by |SetCertificateImporter|.
145 scoped_ptr
<chromeos::onc::CertificateImporter
> certificate_importer_
;
147 content::NotificationRegistrar registrar_
;
149 base::WeakPtrFactory
<UserNetworkConfigurationUpdater
> weak_factory_
;
151 DISALLOW_COPY_AND_ASSIGN(UserNetworkConfigurationUpdater
);
154 } // namespace policy
156 #endif // CHROME_BROWSER_CHROMEOS_POLICY_USER_NETWORK_CONFIGURATION_UPDATER_H_