Chromium Code Reviews| 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> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 7 #include "chrome/browser/chromeos/cros/network_library.h" | 11 #include "chrome/browser/chromeos/cros/network_library.h" |
| 8 #include "chrome/browser/policy/policy_map.h" | 12 #include "chrome/browser/policy/policy_map.h" |
| 9 #include "policy/policy_constants.h" | 13 #include "policy/policy_constants.h" |
| 10 | 14 |
| 11 namespace policy { | 15 namespace policy { |
| 12 | 16 |
| 13 const char NetworkConfigurationUpdater::kEmptyConfiguration[] = | 17 const char NetworkConfigurationUpdater::kEmptyConfiguration[] = |
| 14 "{\"NetworkConfigurations\":[],\"Certificates\":[]}"; | 18 "{\"NetworkConfigurations\":[],\"Certificates\":[]}"; |
| 15 | 19 |
| 16 NetworkConfigurationUpdater::NetworkConfigurationUpdater( | 20 NetworkConfigurationUpdater::NetworkConfigurationUpdater( |
| 17 ConfigurationPolicyProvider* provider, | 21 PolicyService* policy_service, |
| 18 chromeos::NetworkLibrary* network_library) | 22 chromeos::NetworkLibrary* network_library) |
| 19 : network_library_(network_library) { | 23 : policy_change_registrar_( |
| 24 policy_service, POLICY_DOMAIN_CHROME, std::string()), | |
| 25 network_library_(network_library) { | |
| 20 DCHECK(network_library_); | 26 DCHECK(network_library_); |
| 21 provider_registrar_.Init(provider, this); | 27 PolicyChangeRegistrar::UpdateCallback device_update_callback = |
| 22 Update(); | 28 base::Bind(&NetworkConfigurationUpdater::ApplyNetworkConfiguration, |
| 29 base::Unretained(this), | |
| 30 chromeos::NetworkUIData::ONC_SOURCE_DEVICE_POLICY, | |
| 31 &device_network_config_); | |
| 32 PolicyChangeRegistrar::UpdateCallback user_update_callback = | |
| 33 base::Bind(&NetworkConfigurationUpdater::ApplyNetworkConfiguration, | |
| 34 base::Unretained(this), | |
| 35 chromeos::NetworkUIData::ONC_SOURCE_USER_POLICY, | |
| 36 &user_network_config_); | |
| 37 policy_change_registrar_.Observe(key::kDeviceOpenNetworkConfiguration, | |
| 38 device_update_callback); | |
|
Mattias Nissler (ping if slow)
2012/05/14 08:14:25
no need for locals for the callbacks?
Joao da Silva
2012/05/16 09:36:03
Done.
| |
| 39 policy_change_registrar_.Observe(key::kOpenNetworkConfiguration, | |
| 40 user_update_callback); | |
| 41 | |
| 42 // Apply the current values immediately. | |
| 43 const PolicyMap& policies = policy_service->GetPolicies(POLICY_DOMAIN_CHROME, | |
| 44 std::string()); | |
| 45 device_update_callback.Run( | |
| 46 NULL, policies.GetValue(key::kDeviceOpenNetworkConfiguration)); | |
| 47 user_update_callback.Run( | |
| 48 NULL, policies.GetValue(key::kOpenNetworkConfiguration)); | |
| 23 } | 49 } |
| 24 | 50 |
| 25 NetworkConfigurationUpdater::~NetworkConfigurationUpdater() {} | 51 NetworkConfigurationUpdater::~NetworkConfigurationUpdater() {} |
| 26 | 52 |
| 27 void NetworkConfigurationUpdater::OnUpdatePolicy( | |
| 28 ConfigurationPolicyProvider* provider) { | |
| 29 Update(); | |
| 30 } | |
| 31 | |
| 32 void NetworkConfigurationUpdater::Update() { | |
| 33 ConfigurationPolicyProvider* provider = provider_registrar_.provider(); | |
| 34 | |
| 35 PolicyMap policy; | |
| 36 if (!provider->Provide(&policy)) { | |
| 37 LOG(WARNING) << "Failed to read policy from policy provider."; | |
| 38 return; | |
| 39 } | |
| 40 | |
| 41 ApplyNetworkConfiguration(policy, key::kDeviceOpenNetworkConfiguration, | |
| 42 chromeos::NetworkUIData::ONC_SOURCE_DEVICE_POLICY, | |
| 43 &device_network_config_); | |
| 44 ApplyNetworkConfiguration(policy, key::kOpenNetworkConfiguration, | |
| 45 chromeos::NetworkUIData::ONC_SOURCE_USER_POLICY, | |
| 46 &user_network_config_); | |
| 47 } | |
| 48 | |
| 49 void NetworkConfigurationUpdater::ApplyNetworkConfiguration( | 53 void NetworkConfigurationUpdater::ApplyNetworkConfiguration( |
| 50 const PolicyMap& policy_map, | |
| 51 const char* policy_name, | |
| 52 chromeos::NetworkUIData::ONCSource onc_source, | 54 chromeos::NetworkUIData::ONCSource onc_source, |
| 53 std::string* cached_value) { | 55 std::string* cached_value, |
| 56 const base::Value* previous, | |
| 57 const base::Value* current) { | |
| 54 std::string new_network_config; | 58 std::string new_network_config; |
| 55 const base::Value* value = policy_map.GetValue(policy_name); | 59 if (current != NULL) { |
| 56 if (value != NULL) { | |
| 57 // If the policy is not a string, we issue a warning, but still clear the | 60 // If the policy is not a string, we issue a warning, but still clear the |
| 58 // network configuration. | 61 // network configuration. |
| 59 if (!value->GetAsString(&new_network_config)) | 62 if (!current->GetAsString(&new_network_config)) |
| 60 LOG(WARNING) << "Invalid network configuration."; | 63 LOG(WARNING) << "Invalid network configuration."; |
| 61 } | 64 } |
| 62 | 65 |
| 63 // We need to load an empty configuration to get rid of any configuration | 66 // We need to load an empty configuration to get rid of any configuration |
| 64 // that has been installed previously. An empty string also works, but | 67 // that has been installed previously. An empty string also works, but |
| 65 // generates warnings and errors, which we'd like to avoid. | 68 // generates warnings and errors, which we'd like to avoid. |
| 66 if (new_network_config.empty()) | 69 if (new_network_config.empty()) |
| 67 new_network_config = kEmptyConfiguration; | 70 new_network_config = kEmptyConfiguration; |
| 68 | 71 |
| 69 if (*cached_value != new_network_config) { | 72 if (*cached_value != new_network_config) { |
| 70 *cached_value = new_network_config; | 73 *cached_value = new_network_config; |
| 71 std::string error; | 74 std::string error; |
| 72 if (!network_library_->LoadOncNetworks(new_network_config, "", onc_source, | 75 if (!network_library_->LoadOncNetworks(new_network_config, "", onc_source, |
| 73 &error)) { | 76 &error)) { |
| 74 LOG(WARNING) << "Network library failed to load ONC configuration:" | 77 LOG(WARNING) << "Network library failed to load ONC configuration:" |
| 75 << error; | 78 << error; |
| 76 } | 79 } |
| 77 } | 80 } |
| 78 } | 81 } |
| 79 | 82 |
| 80 } // namespace policy | 83 } // namespace policy |
| OLD | NEW |