OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #include "chrome/browser/chromeos/policy/network_configuration_updater_impl.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/debug/stack_trace.h" |
| 12 #include "chrome/browser/policy/policy_map.h" |
| 13 #include "chromeos/network/managed_network_configuration_handler.h" |
| 14 #include "chromeos/network/onc/onc_utils.h" |
| 15 #include "policy/policy_constants.h" |
| 16 |
| 17 namespace policy { |
| 18 |
| 19 NetworkConfigurationUpdaterImpl::NetworkConfigurationUpdaterImpl( |
| 20 PolicyService* policy_service) |
| 21 : user_policy_initialized_(false), |
| 22 policy_change_registrar_( |
| 23 policy_service, PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())), |
| 24 policy_service_(policy_service) { |
| 25 policy_change_registrar_.Observe( |
| 26 key::kDeviceOpenNetworkConfiguration, |
| 27 base::Bind(&NetworkConfigurationUpdaterImpl::OnPolicyChanged, |
| 28 base::Unretained(this), |
| 29 chromeos::onc::ONC_SOURCE_DEVICE_POLICY)); |
| 30 policy_change_registrar_.Observe( |
| 31 key::kOpenNetworkConfiguration, |
| 32 base::Bind(&NetworkConfigurationUpdaterImpl::OnPolicyChanged, |
| 33 base::Unretained(this), |
| 34 chromeos::onc::ONC_SOURCE_USER_POLICY)); |
| 35 } |
| 36 |
| 37 NetworkConfigurationUpdaterImpl::~NetworkConfigurationUpdaterImpl() { |
| 38 } |
| 39 |
| 40 void NetworkConfigurationUpdaterImpl::OnUserPolicyInitialized() { |
| 41 VLOG(1) << "User policy initialized."; |
| 42 user_policy_initialized_ = true; |
| 43 ApplyNetworkConfiguration(chromeos::onc::ONC_SOURCE_USER_POLICY); |
| 44 } |
| 45 |
| 46 net::CertTrustAnchorProvider* |
| 47 NetworkConfigurationUpdaterImpl::GetCertTrustAnchorProvider() { |
| 48 return NULL; |
| 49 } |
| 50 |
| 51 void NetworkConfigurationUpdaterImpl::OnPolicyChanged( |
| 52 chromeos::onc::ONCSource onc_source, |
| 53 const base::Value* previous, |
| 54 const base::Value* current) { |
| 55 VLOG(1) << "Policy for ONC source " |
| 56 << chromeos::onc::GetSourceAsString(onc_source) << " changed."; |
| 57 VLOG(2) << "User policy is " << (user_policy_initialized_ ? "" : "not ") |
| 58 << "initialized."; |
| 59 ApplyNetworkConfiguration(onc_source); |
| 60 } |
| 61 |
| 62 void NetworkConfigurationUpdaterImpl::ApplyNetworkConfiguration( |
| 63 chromeos::onc::ONCSource onc_source) { |
| 64 VLOG(1) << "Apply policy for ONC source " |
| 65 << chromeos::onc::GetSourceAsString(onc_source); |
| 66 |
| 67 std::string policy_key; |
| 68 if (onc_source == chromeos::onc::ONC_SOURCE_USER_POLICY) |
| 69 policy_key = key::kOpenNetworkConfiguration; |
| 70 else |
| 71 policy_key = key::kDeviceOpenNetworkConfiguration; |
| 72 |
| 73 const PolicyMap& policies = policy_service_->GetPolicies( |
| 74 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); |
| 75 const base::Value* policy_value = policies.GetValue(policy_key); |
| 76 |
| 77 if (!policy_value) { |
| 78 VLOG(1) << "The policy value is NULL. Aborting."; |
| 79 return; |
| 80 } |
| 81 |
| 82 std::string onc_blob; |
| 83 if (!policy_value->GetAsString(&onc_blob)) { |
| 84 LOG(ERROR) << "ONC policy " << policy_key << " is not a string value."; |
| 85 return; |
| 86 } |
| 87 VLOG(2) << "The policy contains this ONC: " << onc_blob; |
| 88 |
| 89 if (onc_blob.empty()) |
| 90 onc_blob = chromeos::onc::kEmptyUnencryptedConfiguration; |
| 91 |
| 92 scoped_ptr<base::DictionaryValue> onc_dict = |
| 93 chromeos::onc::ReadDictionaryFromJson(onc_blob); |
| 94 if (!onc_dict) { |
| 95 LOG(ERROR) << "ONC loaded from policy " << policy_key |
| 96 << " is not a valid JSON dictionary."; |
| 97 return; |
| 98 } |
| 99 |
| 100 chromeos::ManagedNetworkConfigurationHandler::Get()->SetPolicy(onc_source, |
| 101 *onc_dict); |
| 102 } |
| 103 |
| 104 } // namespace policy |
OLD | NEW |