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. 4 5#include "chrome/browser/chromeos/policy/network_configuration_updater.h" 6 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" 14 15namespace policy { 16 17NetworkConfigurationUpdater::~NetworkConfigurationUpdater() { 18 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this); 19} 20 21void 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. 25} 26 27void NetworkConfigurationUpdater::OnPolicyServiceInitialized( 28 PolicyDomain domain) { 29 if (domain != POLICY_DOMAIN_CHROME) 30 return; 31 32 if (policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)) { 33 VLOG(1) << LogHeader() << " initialized."; 34 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this); 35 ApplyPolicy(); 36 } 37} 38 39NetworkConfigurationUpdater::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) { 51} 52 53void NetworkConfigurationUpdater::Init() { 54 policy_change_registrar_.Observe( 55 policy_key_, 56 base::Bind(&NetworkConfigurationUpdater::OnPolicyChanged, 57 base::Unretained(this))); 58 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); 64 } 65} 66 67void NetworkConfigurationUpdater::OnPolicyChanged( 68 const base::Value* previous, 69 const base::Value* current) { 70 VLOG(1) << LogHeader() << " changed."; 71 ApplyPolicy(); 72} 73 74void 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_); 78 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."; 84 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); 94 95 ImportCertificates(certificates); 96 ApplyNetworkPolicy(&network_configs, &global_network_config); 97} 98 99std::string NetworkConfigurationUpdater::LogHeader() const { 100 return chromeos::onc::GetSourceAsString(onc_source_); 101} 102 103} // namespace policy 104