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