| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/policy/network_configuration_updater.h" | 5 #include "chrome/browser/policy/network_configuration_updater.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "chrome/browser/chromeos/cros/network_library.h" | 11 #include "chrome/browser/chromeos/cros/network_library.h" |
| 12 #include "chrome/browser/policy/policy_map.h" | 12 #include "chrome/browser/policy/policy_map.h" |
| 13 #include "chromeos/network/onc/onc_constants.h" |
| 13 #include "policy/policy_constants.h" | 14 #include "policy/policy_constants.h" |
| 14 | 15 |
| 15 namespace policy { | 16 namespace policy { |
| 16 | 17 |
| 17 const char NetworkConfigurationUpdater::kEmptyConfiguration[] = | 18 const char NetworkConfigurationUpdater::kEmptyConfiguration[] = |
| 18 "{\"NetworkConfigurations\":[],\"Certificates\":[]}"; | 19 "{\"NetworkConfigurations\":[],\"Certificates\":[]}"; |
| 19 | 20 |
| 20 NetworkConfigurationUpdater::NetworkConfigurationUpdater( | 21 NetworkConfigurationUpdater::NetworkConfigurationUpdater( |
| 21 PolicyService* policy_service, | 22 PolicyService* policy_service, |
| 22 chromeos::NetworkLibrary* network_library) | 23 chromeos::NetworkLibrary* network_library) |
| 23 : policy_change_registrar_( | 24 : policy_change_registrar_( |
| 24 policy_service, POLICY_DOMAIN_CHROME, std::string()), | 25 policy_service, POLICY_DOMAIN_CHROME, std::string()), |
| 25 network_library_(network_library), | 26 network_library_(network_library), |
| 26 user_policy_initialized_(false), | 27 user_policy_initialized_(false), |
| 27 allow_web_trust_(false), | 28 allow_web_trust_(false), |
| 28 policy_service_(policy_service) { | 29 policy_service_(policy_service) { |
| 29 DCHECK(network_library_); | 30 DCHECK(network_library_); |
| 30 policy_change_registrar_.Observe( | 31 policy_change_registrar_.Observe( |
| 31 key::kDeviceOpenNetworkConfiguration, | 32 key::kDeviceOpenNetworkConfiguration, |
| 32 base::Bind(&NetworkConfigurationUpdater::OnPolicyChanged, | 33 base::Bind(&NetworkConfigurationUpdater::OnPolicyChanged, |
| 33 base::Unretained(this), | 34 base::Unretained(this), |
| 34 chromeos::NetworkUIData::ONC_SOURCE_DEVICE_POLICY)); | 35 chromeos::onc::ONC_SOURCE_DEVICE_POLICY)); |
| 35 policy_change_registrar_.Observe( | 36 policy_change_registrar_.Observe( |
| 36 key::kOpenNetworkConfiguration, | 37 key::kOpenNetworkConfiguration, |
| 37 base::Bind(&NetworkConfigurationUpdater::OnPolicyChanged, | 38 base::Bind(&NetworkConfigurationUpdater::OnPolicyChanged, |
| 38 base::Unretained(this), | 39 base::Unretained(this), |
| 39 chromeos::NetworkUIData::ONC_SOURCE_USER_POLICY)); | 40 chromeos::onc::ONC_SOURCE_USER_POLICY)); |
| 40 | 41 |
| 41 network_library_->AddNetworkProfileObserver(this); | 42 network_library_->AddNetworkProfileObserver(this); |
| 42 | 43 |
| 43 // Apply the current policies immediately. | 44 // Apply the current policies immediately. |
| 44 ApplyNetworkConfigurations(); | 45 ApplyNetworkConfigurations(); |
| 45 } | 46 } |
| 46 | 47 |
| 47 NetworkConfigurationUpdater::~NetworkConfigurationUpdater() { | 48 NetworkConfigurationUpdater::~NetworkConfigurationUpdater() { |
| 48 network_library_->RemoveNetworkProfileObserver(this); | 49 network_library_->RemoveNetworkProfileObserver(this); |
| 49 } | 50 } |
| 50 | 51 |
| 51 void NetworkConfigurationUpdater::OnProfileListChanged() { | 52 void NetworkConfigurationUpdater::OnProfileListChanged() { |
| 52 VLOG(1) << "Network profile list changed, applying policies."; | 53 VLOG(1) << "Network profile list changed, applying policies."; |
| 53 ApplyNetworkConfigurations(); | 54 ApplyNetworkConfigurations(); |
| 54 } | 55 } |
| 55 | 56 |
| 56 void NetworkConfigurationUpdater::OnUserPolicyInitialized() { | 57 void NetworkConfigurationUpdater::OnUserPolicyInitialized() { |
| 57 VLOG(1) << "User policy initialized, applying policies. Ignoring."; | 58 VLOG(1) << "User policy initialized, applying policies. Ignoring."; |
| 58 user_policy_initialized_ = true; | 59 user_policy_initialized_ = true; |
| 59 ApplyNetworkConfigurations(); | 60 ApplyNetworkConfigurations(); |
| 60 } | 61 } |
| 61 | 62 |
| 62 void NetworkConfigurationUpdater::OnPolicyChanged( | 63 void NetworkConfigurationUpdater::OnPolicyChanged( |
| 63 chromeos::NetworkUIData::ONCSource onc_source, | 64 chromeos::onc::ONCSource onc_source, |
| 64 const base::Value* previous, | 65 const base::Value* previous, |
| 65 const base::Value* current) { | 66 const base::Value* current) { |
| 66 VLOG(1) << "Policy for ONC source " << onc_source << " changed."; | 67 VLOG(1) << "Policy for ONC source " << onc_source << " changed."; |
| 67 ApplyNetworkConfigurations(); | 68 ApplyNetworkConfigurations(); |
| 68 } | 69 } |
| 69 | 70 |
| 70 void NetworkConfigurationUpdater::ApplyNetworkConfigurations() { | 71 void NetworkConfigurationUpdater::ApplyNetworkConfigurations() { |
| 71 ApplyNetworkConfiguration(key::kDeviceOpenNetworkConfiguration, | 72 ApplyNetworkConfiguration(key::kDeviceOpenNetworkConfiguration, |
| 72 chromeos::NetworkUIData::ONC_SOURCE_DEVICE_POLICY); | 73 chromeos::onc::ONC_SOURCE_DEVICE_POLICY); |
| 73 if (user_policy_initialized_) { | 74 if (user_policy_initialized_) { |
| 74 ApplyNetworkConfiguration(key::kOpenNetworkConfiguration, | 75 ApplyNetworkConfiguration(key::kOpenNetworkConfiguration, |
| 75 chromeos::NetworkUIData::ONC_SOURCE_USER_POLICY); | 76 chromeos::onc::ONC_SOURCE_USER_POLICY); |
| 76 } | 77 } |
| 77 } | 78 } |
| 78 | 79 |
| 79 void NetworkConfigurationUpdater::ApplyNetworkConfiguration( | 80 void NetworkConfigurationUpdater::ApplyNetworkConfiguration( |
| 80 const std::string& policy_key, | 81 const std::string& policy_key, |
| 81 chromeos::NetworkUIData::ONCSource onc_source) { | 82 chromeos::onc::ONCSource onc_source) { |
| 82 VLOG(1) << "Apply policy for ONC source " << onc_source; | 83 VLOG(1) << "Apply policy for ONC source " << onc_source; |
| 83 const PolicyMap& policies = policy_service_->GetPolicies(POLICY_DOMAIN_CHROME, | 84 const PolicyMap& policies = policy_service_->GetPolicies(POLICY_DOMAIN_CHROME, |
| 84 std::string()); | 85 std::string()); |
| 85 const base::Value* policy_value = policies.GetValue(policy_key); | 86 const base::Value* policy_value = policies.GetValue(policy_key); |
| 86 | 87 |
| 87 std::string new_network_config; | 88 std::string new_network_config; |
| 88 if (policy_value != NULL) { | 89 if (policy_value != NULL) { |
| 89 // If the policy is not a string, we issue a warning, but still clear the | 90 // If the policy is not a string, we issue a warning, but still clear the |
| 90 // network configuration. | 91 // network configuration. |
| 91 if (!policy_value->GetAsString(&new_network_config)) | 92 if (!policy_value->GetAsString(&new_network_config)) |
| 92 LOG(WARNING) << "ONC policy is not a string value."; | 93 LOG(WARNING) << "ONC policy is not a string value."; |
| 93 } | 94 } |
| 94 | 95 |
| 95 // An empty string is not a valid ONC and generates warnings and | 96 // An empty string is not a valid ONC and generates warnings and |
| 96 // errors. Replace by a valid empty configuration. | 97 // errors. Replace by a valid empty configuration. |
| 97 if (new_network_config.empty()) | 98 if (new_network_config.empty()) |
| 98 new_network_config = kEmptyConfiguration; | 99 new_network_config = kEmptyConfiguration; |
| 99 | 100 |
| 100 std::string unused_error; | |
| 101 network_library_->LoadOncNetworks(new_network_config, "", onc_source, | 101 network_library_->LoadOncNetworks(new_network_config, "", onc_source, |
| 102 allow_web_trust_, &unused_error); | 102 allow_web_trust_); |
| 103 } | 103 } |
| 104 | 104 |
| 105 } // namespace policy | 105 } // namespace policy |
| OLD | NEW |