Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / network_configuration_updater.cc
blob45fb921e7214de3b8c19ffb73fbcdf46d9e25aa7
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_utils.h"
12 #include "components/policy/core/common/policy_map.h"
13 #include "policy/policy_constants.h"
15 namespace policy {
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)
30 return;
32 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) {
33 VLOG(1) << LogHeader() << " initialized.";
34 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this);
35 ApplyPolicy();
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,
49 std::string())),
50 policy_service_(policy_service) {
53 void NetworkConfigurationUpdater::Init() {
54 policy_change_registrar_.Observe(
55 policy_key_,
56 base::Bind(&NetworkConfigurationUpdater::OnPolicyChanged,
57 base::Unretained(this)));
59 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) {
60 VLOG(1) << LogHeader() << " is already initialized.";
61 ApplyPolicy();
62 } else {
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.";
71 ApplyPolicy();
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_);
79 std::string onc_blob;
80 if (!policy_value)
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,
89 onc_source_,
90 "" /* no passphrase */,
91 &network_configs,
92 &global_network_config,
93 &certificates);
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