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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/network_configuration_updater.h"
8 #include "base/bind_helpers.h"
9 #include "base/logging.h"
10 #include "base/values.h"
11 #include "chromeos/network/onc/onc_utils.h"
12 #include "components/policy/core/common/policy_map.h"
13 #include "policy/policy_constants.h"
17 NetworkConfigurationUpdater::~NetworkConfigurationUpdater() {
18 policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, this);
21 void NetworkConfigurationUpdater::OnPolicyUpdated(const PolicyNamespace
& ns
,
22 const PolicyMap
& previous
,
23 const PolicyMap
& current
) {
24 // Ignore this call. Policy changes are already observed by the registrar.
27 void NetworkConfigurationUpdater::OnPolicyServiceInitialized(
28 PolicyDomain domain
) {
29 if (domain
!= POLICY_DOMAIN_CHROME
)
32 if (policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
)) {
33 VLOG(1) << LogHeader() << " initialized.";
34 policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, this);
39 NetworkConfigurationUpdater::NetworkConfigurationUpdater(
40 onc::ONCSource onc_source
,
41 std::string policy_key
,
42 PolicyService
* policy_service
,
43 chromeos::ManagedNetworkConfigurationHandler
* network_config_handler
)
44 : onc_source_(onc_source
),
45 network_config_handler_(network_config_handler
),
46 policy_key_(policy_key
),
47 policy_change_registrar_(policy_service
,
48 PolicyNamespace(POLICY_DOMAIN_CHROME
,
50 policy_service_(policy_service
) {
53 void NetworkConfigurationUpdater::Init() {
54 policy_change_registrar_
.Observe(
56 base::Bind(&NetworkConfigurationUpdater::OnPolicyChanged
,
57 base::Unretained(this)));
59 if (policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
)) {
60 VLOG(1) << LogHeader() << " is already initialized.";
63 policy_service_
->AddObserver(POLICY_DOMAIN_CHROME
, this);
67 void NetworkConfigurationUpdater::OnPolicyChanged(
68 const base::Value
* previous
,
69 const base::Value
* current
) {
70 VLOG(1) << LogHeader() << " changed.";
74 void NetworkConfigurationUpdater::ApplyPolicy() {
75 const PolicyMap
& policies
= policy_service_
->GetPolicies(
76 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()));
77 const base::Value
* policy_value
= policies
.GetValue(policy_key_
);
81 VLOG(2) << LogHeader() << " is not set.";
82 else if (!policy_value
->GetAsString(&onc_blob
))
83 LOG(ERROR
) << LogHeader() << " is not a string value.";
85 base::ListValue network_configs
;
86 base::DictionaryValue global_network_config
;
87 base::ListValue certificates
;
88 chromeos::onc::ParseAndValidateOncForImport(onc_blob
,
90 "" /* no passphrase */,
92 &global_network_config
,
95 ImportCertificates(certificates
);
96 ApplyNetworkPolicy(&network_configs
, &global_network_config
);
99 std::string
NetworkConfigurationUpdater::LogHeader() const {
100 return chromeos::onc::GetSourceAsString(onc_source_
);
103 } // namespace policy