Index: chrome/browser/chromeos/policy/network_configuration_updater2.cc |
diff --git a/chrome/browser/chromeos/policy/network_configuration_updater2.cc b/chrome/browser/chromeos/policy/network_configuration_updater2.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3968c519ff859a470a147820b623aa710bf3a0e2 |
--- /dev/null |
+++ b/chrome/browser/chromeos/policy/network_configuration_updater2.cc |
@@ -0,0 +1,101 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/chromeos/policy/network_configuration_updater2.h" |
+ |
+#include <string> |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/debug/stack_trace.h" |
+#include "chrome/browser/policy/policy_map.h" |
+#include "chromeos/network/managed_network_configuration_handler.h" |
+#include "chromeos/network/onc/onc_utils.h" |
+#include "policy/policy_constants.h" |
+ |
+namespace policy { |
+ |
+NetworkConfigurationUpdater2::NetworkConfigurationUpdater2( |
+ PolicyService* policy_service) |
+ : user_policy_initialized_(false), |
+ policy_change_registrar_( |
+ policy_service, PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())), |
+ policy_service_(policy_service) { |
+ policy_change_registrar_.Observe( |
+ key::kDeviceOpenNetworkConfiguration, |
+ base::Bind(&NetworkConfigurationUpdater2::OnPolicyChanged, |
+ base::Unretained(this), |
+ chromeos::onc::ONC_SOURCE_DEVICE_POLICY)); |
+ policy_change_registrar_.Observe( |
+ key::kOpenNetworkConfiguration, |
+ base::Bind(&NetworkConfigurationUpdater2::OnPolicyChanged, |
+ base::Unretained(this), |
+ chromeos::onc::ONC_SOURCE_USER_POLICY)); |
+} |
+ |
+NetworkConfigurationUpdater2::~NetworkConfigurationUpdater2() { |
+} |
+ |
+void NetworkConfigurationUpdater2::OnUserPolicyInitialized() { |
+ VLOG(1) << "User policy initialized."; |
+ user_policy_initialized_ = true; |
+ ApplyNetworkConfiguration(chromeos::onc::ONC_SOURCE_USER_POLICY); |
+} |
+ |
+void NetworkConfigurationUpdater2::OnPolicyChanged( |
+ chromeos::onc::ONCSource onc_source, |
+ const base::Value* previous, |
+ const base::Value* current) { |
+ VLOG(1) << "Policy for ONC source " |
+ << chromeos::onc::GetSourceAsString(onc_source) << " changed."; |
+ VLOG(2) << "User policy is " << (user_policy_initialized_ ? "" : "not ") |
+ << "initialized."; |
+ ApplyNetworkConfiguration(onc_source); |
+} |
+ |
+void NetworkConfigurationUpdater2::ApplyNetworkConfiguration( |
+ chromeos::onc::ONCSource onc_source) { |
+ VLOG(1) << "Apply policy for ONC source " |
+ << chromeos::onc::GetSourceAsString(onc_source); |
pastarmovj
2013/04/11 14:46:29
Align.
pneubeck (no reviews)
2013/04/15 12:16:24
Done.
|
+ |
+ std::string policy_key; |
+ if (onc_source == chromeos::onc::ONC_SOURCE_USER_POLICY) { |
+ policy_key = key::kOpenNetworkConfiguration; |
+ } else { |
+ policy_key = key::kDeviceOpenNetworkConfiguration; |
+ } |
stevenjb
2013/04/11 18:20:08
nit: No {}
pneubeck (no reviews)
2013/04/15 12:16:24
Done.
|
+ |
+ const PolicyMap& policies = policy_service_->GetPolicies( |
+ PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); |
+ const base::Value* policy_value = policies.GetValue(policy_key); |
+ |
+ 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.
|
+ VLOG(1) << "The policy value is NULL. Aborting."; |
+ return; |
+ } |
+ |
+ std::string onc_blob; |
+ if (!policy_value->GetAsString(&onc_blob)) { |
+ LOG(ERROR) << "ONC policy " << policy_key << " is not a string value."; |
+ return; |
+ } else { |
stevenjb
2013/04/11 18:20:08
No else after return.
pneubeck (no reviews)
2013/04/15 12:16:24
Done.
|
+ VLOG(2) << "The policy contains this ONC: " << onc_blob; |
+ } |
+ |
+ if (onc_blob.empty()) |
+ onc_blob = chromeos::onc::kEmptyUnencryptedConfiguration; |
+ |
+ scoped_ptr<base::DictionaryValue> onc_dict = |
+ chromeos::onc::ReadDictionaryFromJson(onc_blob); |
+ 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.
|
+ LOG(ERROR) << "ONC loaded from policy " << policy_key |
+ << " is not a valid JSON dictionary."; |
+ return; |
+ } |
+ |
+ chromeos::ManagedNetworkConfigurationHandler::Get()->SetPolicy(onc_source, |
+ *onc_dict); |
+} |
+ |
+} // namespace policy |