Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / network_configuration_updater.cc
blobce72dd8bd4a5e9ce111c63bfd9886135aa5f5b79
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"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/logging.h"
10 #include "base/values.h"
11 #include "chromeos/network/onc/onc_certificate_importer.h"
12 #include "chromeos/network/onc/onc_utils.h"
13 #include "components/policy/core/common/policy_map.h"
14 #include "policy/policy_constants.h"
16 namespace policy {
18 NetworkConfigurationUpdater::~NetworkConfigurationUpdater() {
19 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this);
22 void NetworkConfigurationUpdater::OnPolicyUpdated(const PolicyNamespace& ns,
23 const PolicyMap& previous,
24 const PolicyMap& current) {
25 // Ignore this call. Policy changes are already observed by the registrar.
28 void NetworkConfigurationUpdater::OnPolicyServiceInitialized(
29 PolicyDomain domain) {
30 if (domain != POLICY_DOMAIN_CHROME)
31 return;
33 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) {
34 VLOG(1) << LogHeader() << " initialized.";
35 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this);
36 ApplyPolicy();
40 NetworkConfigurationUpdater::NetworkConfigurationUpdater(
41 onc::ONCSource onc_source,
42 std::string policy_key,
43 scoped_ptr<chromeos::onc::CertificateImporter> certificate_importer,
44 PolicyService* policy_service,
45 chromeos::ManagedNetworkConfigurationHandler* network_config_handler)
46 : onc_source_(onc_source),
47 network_config_handler_(network_config_handler),
48 certificate_importer_(certificate_importer.Pass()),
49 policy_key_(policy_key),
50 policy_change_registrar_(policy_service,
51 PolicyNamespace(POLICY_DOMAIN_CHROME,
52 std::string())),
53 policy_service_(policy_service) {
56 void NetworkConfigurationUpdater::Init() {
57 policy_change_registrar_.Observe(
58 policy_key_,
59 base::Bind(&NetworkConfigurationUpdater::OnPolicyChanged,
60 base::Unretained(this)));
62 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) {
63 VLOG(1) << LogHeader() << " is already initialized.";
64 ApplyPolicy();
65 } else {
66 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, this);
70 void NetworkConfigurationUpdater::OnPolicyChanged(
71 const base::Value* previous,
72 const base::Value* current) {
73 VLOG(1) << LogHeader() << " changed.";
74 ApplyPolicy();
77 void NetworkConfigurationUpdater::ApplyPolicy() {
78 const PolicyMap& policies = policy_service_->GetPolicies(
79 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
80 const base::Value* policy_value = policies.GetValue(policy_key_);
82 std::string onc_blob;
83 if (!policy_value)
84 VLOG(2) << LogHeader() << " is not set.";
85 else if (!policy_value->GetAsString(&onc_blob))
86 LOG(ERROR) << LogHeader() << " is not a string value.";
88 base::ListValue network_configs;
89 base::DictionaryValue global_network_config;
90 base::ListValue certificates;
91 chromeos::onc::ParseAndValidateOncForImport(onc_blob,
92 onc_source_,
93 "" /* no passphrase */,
94 &network_configs,
95 &global_network_config,
96 &certificates);
98 ImportCertificates(certificates);
99 ApplyNetworkPolicy(&network_configs, &global_network_config);
102 std::string NetworkConfigurationUpdater::LogHeader() const {
103 return chromeos::onc::GetSourceAsString(onc_source_);
106 } // namespace policy