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 policy_change_registrar_.Observe( |
22 Update(); | 28 key::kDeviceOpenNetworkConfiguration, |
| 29 base::Bind(&NetworkConfigurationUpdater::ApplyNetworkConfiguration, |
| 30 base::Unretained(this), |
| 31 chromeos::NetworkUIData::ONC_SOURCE_DEVICE_POLICY, |
| 32 &device_network_config_)); |
| 33 policy_change_registrar_.Observe( |
| 34 key::kOpenNetworkConfiguration, |
| 35 base::Bind(&NetworkConfigurationUpdater::ApplyNetworkConfiguration, |
| 36 base::Unretained(this), |
| 37 chromeos::NetworkUIData::ONC_SOURCE_USER_POLICY, |
| 38 &user_network_config_)); |
| 39 |
| 40 // Apply the current values immediately. |
| 41 const PolicyMap& policies = policy_service->GetPolicies(POLICY_DOMAIN_CHROME, |
| 42 std::string()); |
| 43 ApplyNetworkConfiguration( |
| 44 chromeos::NetworkUIData::ONC_SOURCE_DEVICE_POLICY, |
| 45 &device_network_config_, |
| 46 NULL, |
| 47 policies.GetValue(key::kDeviceOpenNetworkConfiguration)); |
| 48 ApplyNetworkConfiguration( |
| 49 chromeos::NetworkUIData::ONC_SOURCE_USER_POLICY, |
| 50 &user_network_config_, |
| 51 NULL, |
| 52 policies.GetValue(key::kOpenNetworkConfiguration)); |
23 } | 53 } |
24 | 54 |
25 NetworkConfigurationUpdater::~NetworkConfigurationUpdater() {} | 55 NetworkConfigurationUpdater::~NetworkConfigurationUpdater() {} |
26 | 56 |
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( | 57 void NetworkConfigurationUpdater::ApplyNetworkConfiguration( |
50 const PolicyMap& policy_map, | |
51 const char* policy_name, | |
52 chromeos::NetworkUIData::ONCSource onc_source, | 58 chromeos::NetworkUIData::ONCSource onc_source, |
53 std::string* cached_value) { | 59 std::string* cached_value, |
| 60 const base::Value* previous, |
| 61 const base::Value* current) { |
54 std::string new_network_config; | 62 std::string new_network_config; |
55 const base::Value* value = policy_map.GetValue(policy_name); | 63 if (current != NULL) { |
56 if (value != NULL) { | |
57 // If the policy is not a string, we issue a warning, but still clear the | 64 // If the policy is not a string, we issue a warning, but still clear the |
58 // network configuration. | 65 // network configuration. |
59 if (!value->GetAsString(&new_network_config)) | 66 if (!current->GetAsString(&new_network_config)) |
60 LOG(WARNING) << "Invalid network configuration."; | 67 LOG(WARNING) << "Invalid network configuration."; |
61 } | 68 } |
62 | 69 |
63 // We need to load an empty configuration to get rid of any configuration | 70 // 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 | 71 // that has been installed previously. An empty string also works, but |
65 // generates warnings and errors, which we'd like to avoid. | 72 // generates warnings and errors, which we'd like to avoid. |
66 if (new_network_config.empty()) | 73 if (new_network_config.empty()) |
67 new_network_config = kEmptyConfiguration; | 74 new_network_config = kEmptyConfiguration; |
68 | 75 |
69 if (*cached_value != new_network_config) { | 76 if (*cached_value != new_network_config) { |
70 *cached_value = new_network_config; | 77 *cached_value = new_network_config; |
71 std::string error; | 78 std::string error; |
72 if (!network_library_->LoadOncNetworks(new_network_config, "", onc_source, | 79 if (!network_library_->LoadOncNetworks(new_network_config, "", onc_source, |
73 &error)) { | 80 &error)) { |
74 LOG(WARNING) << "Network library failed to load ONC configuration:" | 81 LOG(WARNING) << "Network library failed to load ONC configuration:" |
75 << error; | 82 << error; |
76 } | 83 } |
77 } | 84 } |
78 } | 85 } |
79 | 86 |
80 } // namespace policy | 87 } // namespace policy |
OLD | NEW |