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_updater2.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 NetworkConfigurationUpdater2::NetworkConfigurationUpdater2( | |
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(&NetworkConfigurationUpdater2::OnPolicyChanged, | |
28 base::Unretained(this), | |
29 chromeos::onc::ONC_SOURCE_DEVICE_POLICY)); | |
30 policy_change_registrar_.Observe( | |
31 key::kOpenNetworkConfiguration, | |
32 base::Bind(&NetworkConfigurationUpdater2::OnPolicyChanged, | |
33 base::Unretained(this), | |
34 chromeos::onc::ONC_SOURCE_USER_POLICY)); | |
35 } | |
36 | |
37 NetworkConfigurationUpdater2::~NetworkConfigurationUpdater2() { | |
38 } | |
39 | |
40 void NetworkConfigurationUpdater2::OnUserPolicyInitialized() { | |
41 VLOG(1) << "User policy initialized."; | |
42 user_policy_initialized_ = true; | |
43 ApplyNetworkConfiguration(chromeos::onc::ONC_SOURCE_USER_POLICY); | |
44 } | |
45 | |
46 void NetworkConfigurationUpdater2::OnPolicyChanged( | |
47 chromeos::onc::ONCSource onc_source, | |
48 const base::Value* previous, | |
49 const base::Value* current) { | |
50 VLOG(1) << "Policy for ONC source " | |
51 << chromeos::onc::GetSourceAsString(onc_source) << " changed."; | |
52 VLOG(2) << "User policy is " << (user_policy_initialized_ ? "" : "not ") | |
53 << "initialized."; | |
54 ApplyNetworkConfiguration(onc_source); | |
55 } | |
56 | |
57 void NetworkConfigurationUpdater2::ApplyNetworkConfiguration( | |
58 chromeos::onc::ONCSource onc_source) { | |
59 VLOG(1) << "Apply policy for ONC source " | |
60 << chromeos::onc::GetSourceAsString(onc_source); | |
pastarmovj
2013/04/11 14:46:29
Align.
pneubeck (no reviews)
2013/04/15 12:16:24
Done.
| |
61 | |
62 std::string policy_key; | |
63 if (onc_source == chromeos::onc::ONC_SOURCE_USER_POLICY) { | |
64 policy_key = key::kOpenNetworkConfiguration; | |
65 } else { | |
66 policy_key = key::kDeviceOpenNetworkConfiguration; | |
67 } | |
stevenjb
2013/04/11 18:20:08
nit: No {}
pneubeck (no reviews)
2013/04/15 12:16:24
Done.
| |
68 | |
69 const PolicyMap& policies = policy_service_->GetPolicies( | |
70 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); | |
71 const base::Value* policy_value = policies.GetValue(policy_key); | |
72 | |
73 if (policy_value == NULL) { | |
pastarmovj
2013/04/11 14:46:29
!policy_value is preferred.
pneubeck (no reviews)
2013/04/15 12:16:24
Done.
| |
74 VLOG(1) << "The policy value is NULL. Aborting."; | |
75 return; | |
76 } | |
77 | |
78 std::string onc_blob; | |
79 if (!policy_value->GetAsString(&onc_blob)) { | |
80 LOG(ERROR) << "ONC policy " << policy_key << " is not a string value."; | |
81 return; | |
82 } else { | |
stevenjb
2013/04/11 18:20:08
No else after return.
pneubeck (no reviews)
2013/04/15 12:16:24
Done.
| |
83 VLOG(2) << "The policy contains this ONC: " << onc_blob; | |
84 } | |
85 | |
86 if (onc_blob.empty()) | |
87 onc_blob = chromeos::onc::kEmptyUnencryptedConfiguration; | |
88 | |
89 scoped_ptr<base::DictionaryValue> onc_dict = | |
90 chromeos::onc::ReadDictionaryFromJson(onc_blob); | |
91 if (onc_dict.get() == NULL) { | |
pastarmovj
2013/04/11 14:46:29
!onc_dict works for scoped_ptr.
pneubeck (no reviews)
2013/04/15 12:16:24
Done.
| |
92 LOG(ERROR) << "ONC loaded from policy " << policy_key | |
93 << " is not a valid JSON dictionary."; | |
94 return; | |
95 } | |
96 | |
97 chromeos::ManagedNetworkConfigurationHandler::Get()->SetPolicy(onc_source, | |
98 *onc_dict); | |
99 } | |
100 | |
101 } // namespace policy | |
OLD | NEW |