diff options
author | mnissler@chromium.org <mnissler@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-01 16:20:00 +0000 |
---|---|---|
committer | mnissler@chromium.org <mnissler@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-01 16:20:00 +0000 |
commit | 5581458c8b7ac9e7117d96606d74e574c41df8d6 (patch) | |
tree | dfb05f2475363f82e8ce09aaad52aaa112fd0ec7 | |
parent | 45132c563e520bd734f2e4bb4f627017de2d2775 (diff) | |
download | chromium_src-5581458c8b7ac9e7117d96606d74e574c41df8d6.zip chromium_src-5581458c8b7ac9e7117d96606d74e574c41df8d6.tar.gz chromium_src-5581458c8b7ac9e7117d96606d74e574c41df8d6.tar.bz2 |
Infrastructure for accessing and manipulating network UI data.
BUG=chromium-os:23124
TEST=compiles and passes tests, no observable changes.
Review URL: http://codereview.chromium.org/8728030
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@112472 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/chromeos/cros/network_library.h | 4 | ||||
-rw-r--r-- | chrome/browser/chromeos/cros/network_ui_data.cc | 168 | ||||
-rw-r--r-- | chrome/browser/chromeos/cros/network_ui_data.h | 165 | ||||
-rw-r--r-- | chrome/browser/chromeos/cros/network_ui_data_unittest.cc | 163 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 2 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 |
6 files changed, 502 insertions, 1 deletions
diff --git a/chrome/browser/chromeos/cros/network_library.h b/chrome/browser/chromeos/cros/network_library.h index 28e33f7..07de4df 100644 --- a/chrome/browser/chromeos/cros/network_library.h +++ b/chrome/browser/chromeos/cros/network_library.h @@ -621,6 +621,9 @@ class Network { const std::string& proxy_config() const { return proxy_config_; } + const DictionaryValue* ui_data() const { return &ui_data_; } + DictionaryValue* ui_data() { return &ui_data_; } + void set_notify_failure(bool state) { notify_failure_ = state; } void SetPreferred(bool preferred); @@ -704,7 +707,6 @@ class Network { std::string* dest); void set_unique_id(const std::string& unique_id) { unique_id_ = unique_id; } - DictionaryValue* ui_data() { return &ui_data_; } private: typedef std::map<PropertyIndex, base::Value*> PropertyMap; diff --git a/chrome/browser/chromeos/cros/network_ui_data.cc b/chrome/browser/chromeos/cros/network_ui_data.cc new file mode 100644 index 0000000..9fae463 --- /dev/null +++ b/chrome/browser/chromeos/cros/network_ui_data.cc @@ -0,0 +1,168 @@ +// Copyright (c) 2011 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/cros/network_ui_data.h" + +#include "base/logging.h" +#include "chrome/browser/chromeos/cros/network_library.h" + +namespace chromeos { + +// Top-level UI data dictionary keys. +const char NetworkUIData::kKeyONCSource[] = "onc_source"; +const char NetworkUIData::kKeyProperties[] = "properties"; + +// Property names for per-property data stored under |kKeyProperties|. +const char NetworkUIData::kPropertyAutoConnect[] = "auto_connect"; +const char NetworkUIData::kPropertyPreferred[] = "preferred"; +const char NetworkUIData::kPropertyPassphrase[] = "passphrase"; +const char NetworkUIData::kPropertySaveCredentials[] = "save_credentials"; + +const char NetworkUIData::kPropertyVPNCaCertNss[] = "VPN.ca_cert_nss"; +const char NetworkUIData::kPropertyVPNPskPassphrase[] = "VPN.psk_passphrase"; +const char NetworkUIData::kPropertyVPNClientCertId[] = "VPN.client_cert_id"; +const char NetworkUIData::kPropertyVPNUsername[] = "VPN.username"; +const char NetworkUIData::kPropertyVPNUserPassphrase[] = "VPN.user_passphrase"; +const char NetworkUIData::kPropertyVPNGroupName[] = "VPN.group_name"; + +const char NetworkUIData::kPropertyEAPMethod[] = "EAP.method"; +const char NetworkUIData::kPropertyEAPPhase2Auth[] = "EAP.phase_2_auth"; +const char NetworkUIData::kPropertyEAPServerCaCertNssNickname[] = + "EAP.server_ca_cert_nss_nickname"; +const char NetworkUIData::kPropertyEAPClientCertPkcs11Id[] = + "EAP.client_cert_pkcs11_id"; +const char NetworkUIData::kPropertyEAPUseSystemCAs[] = "EAP.use_system_cas"; +const char NetworkUIData::kPropertyEAPIdentity[] = "EAP.identity"; +const char NetworkUIData::kPropertyEAPAnonymousIdentity[] = + "EAP.anonymous_identity"; +const char NetworkUIData::kPropertyEAPPassphrase[] = "EAP.passphrase"; + +const EnumMapper<NetworkUIData::ONCSource>::Pair + NetworkUIData::kONCSourceTable[] = { + { "user_import", NetworkUIData::ONC_SOURCE_USER_IMPORT }, + { "device_policy", NetworkUIData::ONC_SOURCE_DEVICE_POLICY }, + { "user_policy", NetworkUIData::ONC_SOURCE_USER_POLICY }, +}; + +// Property names for the per-property dictionary. +const char NetworkPropertyUIData::kKeyController[] = "controller"; +const char NetworkPropertyUIData::kKeyDefaultValue[] = "default_value"; + +const EnumMapper<NetworkPropertyUIData::Controller>::Pair + NetworkPropertyUIData::kControllerTable[] = { + { "user", NetworkPropertyUIData::CONTROLLER_USER }, + { "policy", NetworkPropertyUIData::CONTROLLER_POLICY }, +}; + +NetworkUIData::NetworkUIData() + : onc_source_(ONC_SOURCE_NONE) { +} + +NetworkUIData::~NetworkUIData() { +} + +void NetworkUIData::SetProperty(const char* property_key, + const NetworkPropertyUIData& ui_data) { + properties_.Set(property_key, ui_data.BuildDictionary()); +} + +void NetworkUIData::FillDictionary(base::DictionaryValue* dict) const { + std::string source_string(GetONCSourceMapper().GetKey(onc_source_)); + if (!source_string.empty()) + dict->SetString(kKeyONCSource, source_string); + dict->Set(kKeyProperties, properties_.DeepCopy()); +} + +// static +NetworkUIData::ONCSource NetworkUIData::GetONCSource(const Network* network) { + std::string source; + if (network->ui_data()->GetString(kKeyONCSource, &source)) + return GetONCSourceMapper().Get(source); + return ONC_SOURCE_NONE; +} + +// static +bool NetworkUIData::IsManaged(const Network* network) { + ONCSource source = GetONCSource(network); + return source == ONC_SOURCE_DEVICE_POLICY || source == ONC_SOURCE_USER_POLICY; +} + +// static +EnumMapper<NetworkUIData::ONCSource>& NetworkUIData::GetONCSourceMapper() { + CR_DEFINE_STATIC_LOCAL(EnumMapper<ONCSource>, mapper, + (kONCSourceTable, arraysize(kONCSourceTable), + ONC_SOURCE_NONE)); + return mapper; +} + +NetworkPropertyUIData::NetworkPropertyUIData() + : controller_(CONTROLLER_USER) { +} + +NetworkPropertyUIData::~NetworkPropertyUIData() { +} + +NetworkPropertyUIData::NetworkPropertyUIData(Controller controller, + base::Value* default_value) + : controller_(controller), + default_value_(default_value) { +} + +NetworkPropertyUIData::NetworkPropertyUIData(const Network* network, + const char* property_key) { + UpdateFromNetwork(network, property_key); +} + +void NetworkPropertyUIData::UpdateFromNetwork(const Network* network, + const char* property_key) { + // If there is no per-property information available, the property inherits + // the controlled state of the network. + controller_ = + NetworkUIData::IsManaged(network) ? CONTROLLER_POLICY : CONTROLLER_USER; + default_value_.reset(); + + if (!property_key) + return; + + const base::DictionaryValue* ui_data = network->ui_data(); + if (!ui_data) + return; + + base::DictionaryValue* property_map = NULL; + if (!ui_data->GetDictionary(NetworkUIData::kKeyProperties, &property_map)) + return; + + base::DictionaryValue* property = NULL; + if (!property_map->GetDictionary(property_key, &property)) + return; + + std::string controller; + if (property->GetString(kKeyController, &controller)) + controller_ = GetControllerMapper().Get(controller); + + base::Value* default_value = NULL; + if (property->Get(kKeyDefaultValue, &default_value) && default_value) + default_value_.reset(default_value->DeepCopy()); +} + +base::DictionaryValue* NetworkPropertyUIData::BuildDictionary() const { + base::DictionaryValue* dict = new base::DictionaryValue(); + std::string controller_string(GetControllerMapper().GetKey(controller_)); + if (!controller_string.empty()) + dict->SetString(kKeyController, controller_string); + if (default_value_.get()) + dict->Set(kKeyDefaultValue, default_value_->DeepCopy()); + return dict; +} + +// static +EnumMapper<NetworkPropertyUIData::Controller>& + NetworkPropertyUIData::GetControllerMapper() { + CR_DEFINE_STATIC_LOCAL(EnumMapper<Controller>, mapper, + (kControllerTable, arraysize(kControllerTable), + CONTROLLER_USER)); + return mapper; +} + +} // namespace chromeos diff --git a/chrome/browser/chromeos/cros/network_ui_data.h b/chrome/browser/chromeos/cros/network_ui_data.h new file mode 100644 index 0000000..7b3d24f --- /dev/null +++ b/chrome/browser/chromeos/cros/network_ui_data.h @@ -0,0 +1,165 @@ +// Copyright (c) 2011 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. + +#ifndef CHROME_BROWSER_CHROMEOS_CROS_NETWORK_UI_DATA_H_ +#define CHROME_BROWSER_CHROMEOS_CROS_NETWORK_UI_DATA_H_ +#pragma once + +#include <string> + +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" +#include "base/values.h" +#include "chrome/browser/chromeos/cros/network_parser.h" + +namespace chromeos { + +class Network; +class NetworkPropertyUIData; + +// Helper for accessing and setting values in the network's UI data dictionary. +// Accessing values is done via static members that take the network as an +// argument. In order to fill a UI data dictionary, construct an instance, set +// up your data members, and call FillDictionary(). For example, if you have a +// |network|: +// +// NetworkUIData ui_data; +// ui_data.set_onc_source(NetworkUIData::ONC_SOURCE_USER_IMPORT); +// NetworkPropertyUIData auto_connect_property( +// NetworkPropertyUIData::CONTROLLER_USER, +// base::Value::CreateBooleanValue(true)); +// ui_data.SetProperty(NetworkUIData::kPropertyAutoConnect, +// auto_connect_property); +// ui_data.FillDictionary(network->ui_data()); +class NetworkUIData { + public: + // Indicates from which source an ONC blob comes from. + enum ONCSource { + ONC_SOURCE_NONE, + ONC_SOURCE_USER_IMPORT, + ONC_SOURCE_DEVICE_POLICY, + ONC_SOURCE_USER_POLICY, + }; + + // Constructs a new dictionary builder. + NetworkUIData(); + ~NetworkUIData(); + + // Sets the ONC source. + void set_onc_source(ONCSource onc_source) { onc_source_ = onc_source; } + + // Fills in metadata for a property. + void SetProperty(const char* property_key, + const NetworkPropertyUIData& ui_data); + + // Fills in |dict| with the currently configured values. This will write the + // keys appropriate for Network::ui_data() as defined below (kKeyXXX). + void FillDictionary(DictionaryValue* dict) const; + + // Get the ONC source for a network. + static ONCSource GetONCSource(const Network* network); + + // Check whether a network is managed by policy. + static bool IsManaged(const Network* network); + + // Source of the ONC network. This is an integer according to enum ONCSource. + static const char kKeyONCSource[]; + + // Per-property meta data. This is handled by NetworkPropertyUIData. + static const char kKeyProperties[]; + + // Property names for per-property dat + static const char kPropertyAutoConnect[]; + static const char kPropertyPreferred[]; + static const char kPropertyPassphrase[]; + static const char kPropertySaveCredentials[]; + + static const char kPropertyVPNCaCertNss[]; + static const char kPropertyVPNPskPassphrase[]; + static const char kPropertyVPNClientCertId[]; + static const char kPropertyVPNUsername[]; + static const char kPropertyVPNUserPassphrase[]; + static const char kPropertyVPNGroupName[]; + + static const char kPropertyEAPMethod[]; + static const char kPropertyEAPPhase2Auth[]; + static const char kPropertyEAPServerCaCertNssNickname[]; + static const char kPropertyEAPClientCertPkcs11Id[]; + static const char kPropertyEAPUseSystemCAs[]; + static const char kPropertyEAPIdentity[]; + static const char kPropertyEAPAnonymousIdentity[]; + static const char kPropertyEAPPassphrase[]; + + private: + static EnumMapper<ONCSource>& GetONCSourceMapper(); + + ONCSource onc_source_; + DictionaryValue properties_; + + static const EnumMapper<NetworkUIData::ONCSource>::Pair kONCSourceTable[]; + + DISALLOW_COPY_AND_ASSIGN(NetworkUIData); +}; + +// Holds meta information for a network property: Whether the property is under +// policy control, if it is user-editable, and whether the policy-provided +// default value, if applicable. +class NetworkPropertyUIData { + public: + // Enum values indicating the entity controlling the property. + enum Controller { + // Property is managed by policy. + CONTROLLER_POLICY, + // The user controls the policy. + CONTROLLER_USER, + }; + + // Initializes the object with CONTROLLER_USER and no default value. + NetworkPropertyUIData(); + ~NetworkPropertyUIData(); + + // Initializes the object with the given values. |default_value| may be NULL + // to specify no default value is present. Takes ownership of |default_value|. + NetworkPropertyUIData(Controller controller, base::Value* default_value); + + // Initializes the object and calls UpdateFromNetwork. + NetworkPropertyUIData(const Network* network, const char* property_key); + + // Updates the object from the network-level UI data dictionary. + // |property_key| may be null, in which case the there'll be no default value + // and |controller| will be set to CONTROLLER_POLICY iff the network is + // managed. + void UpdateFromNetwork(const Network* network, const char* property_key); + // Builds a dictionary for storing in the network-level UI data dictionary. + // Ownership is transferred to the caller. + DictionaryValue* BuildDictionary() const; + + const base::Value* default_value() const { return default_value_.get(); } + bool managed() const { return controller_ == CONTROLLER_POLICY; } + bool recommended() const { + return controller_ == CONTROLLER_USER && default_value_.get(); + } + bool editable() const { return controller_ == CONTROLLER_USER; } + + private: + static EnumMapper<Controller>& GetControllerMapper(); + + Controller controller_; + scoped_ptr<base::Value> default_value_; + + static const char kKeyController[]; + static const char kKeyDefaultValue[]; + + static const EnumMapper<NetworkPropertyUIData::Controller>::Pair + kControllerTable[]; + + // So it can access the kKeyXYZ constants. + friend class NetworkUIDataTest; + + DISALLOW_COPY_AND_ASSIGN(NetworkPropertyUIData); +}; + +} // namespace chromeos + +#endif // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_UI_DATA_H_ diff --git a/chrome/browser/chromeos/cros/network_ui_data_unittest.cc b/chrome/browser/chromeos/cros/network_ui_data_unittest.cc new file mode 100644 index 0000000..53f56f4 --- /dev/null +++ b/chrome/browser/chromeos/cros/network_ui_data_unittest.cc @@ -0,0 +1,163 @@ +// Copyright (c) 2011 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 "base/stringprintf.h" +#include "chrome/browser/chromeos/cros/network_library.h" +#include "chrome/browser/chromeos/cros/network_ui_data.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace chromeos { + +namespace { + +// A mock network for testing. We really only need the ui_data() member. +class TestNetwork : public Network { + public: + TestNetwork() + : Network("wifi-network", TYPE_WIFI) {} +}; + +} // namespace + +class NetworkUIDataTest : public testing::Test { + protected: + NetworkUIDataTest() {} + + static void SetProperty(DictionaryValue* dict, + const char* property_key, + const char* controller, + base::Value* default_value) { + DictionaryValue* property_dict = new DictionaryValue(); + if (controller) { + property_dict->SetString(NetworkPropertyUIData::kKeyController, + controller); + } + if (default_value) { + property_dict->Set(NetworkPropertyUIData::kKeyDefaultValue, + default_value); + } + dict->Set(base::StringPrintf("%s.%s", + NetworkUIData::kKeyProperties, + property_key), + property_dict); + } + + static void CheckProperty(const DictionaryValue* dict, + const char* property_key, + const char* controller, + base::Value* default_value) { + DictionaryValue* property_dict; + std::string key = base::StringPrintf("%s.%s", + NetworkUIData::kKeyProperties, + property_key); + EXPECT_TRUE(dict->GetDictionary(key, &property_dict)); + ASSERT_TRUE(property_dict); + std::string actual_controller; + EXPECT_TRUE(property_dict->GetString(NetworkPropertyUIData::kKeyController, + &actual_controller)); + EXPECT_EQ(controller, actual_controller); + if (default_value) { + base::Value* actual_value = NULL; + EXPECT_TRUE(property_dict->Get(NetworkPropertyUIData::kKeyDefaultValue, + &actual_value)); + EXPECT_TRUE(base::Value::Equals(default_value, actual_value)); + } + } + + TestNetwork network_; + + DISALLOW_COPY_AND_ASSIGN(NetworkUIDataTest); +}; + +TEST_F(NetworkUIDataTest, ONCSource) { + network_.ui_data()->SetString(NetworkUIData::kKeyONCSource, "user_import"); + EXPECT_EQ(NetworkUIData::ONC_SOURCE_USER_IMPORT, + NetworkUIData::GetONCSource(&network_)); + EXPECT_FALSE(NetworkUIData::IsManaged(&network_)); + + network_.ui_data()->SetString(NetworkUIData::kKeyONCSource, "device_policy"); + EXPECT_EQ(NetworkUIData::ONC_SOURCE_DEVICE_POLICY, + NetworkUIData::GetONCSource(&network_)); + EXPECT_TRUE(NetworkUIData::IsManaged(&network_)); + + network_.ui_data()->SetString(NetworkUIData::kKeyONCSource, "user_policy"); + EXPECT_EQ(NetworkUIData::ONC_SOURCE_USER_POLICY, + NetworkUIData::GetONCSource(&network_)); + EXPECT_TRUE(NetworkUIData::IsManaged(&network_)); +} + +TEST_F(NetworkUIDataTest, ReadProperties) { + SetProperty(network_.ui_data(), NetworkUIData::kPropertyAutoConnect, + "policy", NULL); + SetProperty(network_.ui_data(), NetworkUIData::kPropertyPreferred, + "user", Value::CreateBooleanValue(true)); + SetProperty(network_.ui_data(), NetworkUIData::kPropertyPassphrase, + NULL, Value::CreateIntegerValue(42)); + + NetworkPropertyUIData property_ui_data(&network_, + NetworkUIData::kPropertyAutoConnect); + EXPECT_TRUE(property_ui_data.managed()); + EXPECT_FALSE(property_ui_data.recommended()); + EXPECT_FALSE(property_ui_data.editable()); + EXPECT_FALSE(property_ui_data.default_value()); + + property_ui_data.UpdateFromNetwork(&network_, + NetworkUIData::kPropertyPreferred); + EXPECT_FALSE(property_ui_data.managed()); + EXPECT_TRUE(property_ui_data.recommended()); + EXPECT_TRUE(property_ui_data.editable()); + base::FundamentalValue expected_preferred(true); + EXPECT_TRUE(base::Value::Equals(&expected_preferred, + property_ui_data.default_value())); + + property_ui_data.UpdateFromNetwork(&network_, + NetworkUIData::kPropertyPassphrase); + EXPECT_FALSE(property_ui_data.managed()); + EXPECT_TRUE(property_ui_data.recommended()); + EXPECT_TRUE(property_ui_data.editable()); + base::FundamentalValue expected_passphrase(42); + EXPECT_TRUE(base::Value::Equals(&expected_passphrase, + property_ui_data.default_value())); + + property_ui_data.UpdateFromNetwork(&network_, + NetworkUIData::kPropertySaveCredentials); + EXPECT_FALSE(property_ui_data.managed()); + EXPECT_FALSE(property_ui_data.recommended()); + EXPECT_TRUE(property_ui_data.editable()); + EXPECT_FALSE(property_ui_data.default_value()); +} + +TEST_F(NetworkUIDataTest, WriteProperties) { + NetworkUIData ui_data_builder; + ui_data_builder.set_onc_source(NetworkUIData::ONC_SOURCE_USER_POLICY); + NetworkPropertyUIData auto_connect_ui_data( + NetworkPropertyUIData::CONTROLLER_USER, + base::Value::CreateBooleanValue(true)); + ui_data_builder.SetProperty(NetworkUIData::kPropertyAutoConnect, + auto_connect_ui_data); + NetworkPropertyUIData preferred_ui_data( + NetworkPropertyUIData::CONTROLLER_POLICY, + base::Value::CreateBooleanValue(42)); + ui_data_builder.SetProperty(NetworkUIData::kPropertyPreferred, + preferred_ui_data); + NetworkPropertyUIData passphrase_ui_data( + NetworkPropertyUIData::CONTROLLER_USER, NULL); + ui_data_builder.SetProperty(NetworkUIData::kPropertyPassphrase, + passphrase_ui_data); + + DictionaryValue dict; + ui_data_builder.FillDictionary(&dict); + + std::string onc_source; + EXPECT_TRUE(dict.GetString(NetworkUIData::kKeyONCSource, &onc_source)); + EXPECT_EQ("user_policy", onc_source); + CheckProperty(&dict, NetworkUIData::kPropertyAutoConnect, + "user", base::Value::CreateBooleanValue(true)); + CheckProperty(&dict, NetworkUIData::kPropertyPreferred, + "policy", base::Value::CreateBooleanValue(42)); + CheckProperty(&dict, NetworkUIData::kPropertyPassphrase, + "user", NULL); +} + +} // namespace chromeos diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index ae20a62..039e3ec 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -422,6 +422,8 @@ 'browser/chromeos/cros/network_library.h', 'browser/chromeos/cros/network_parser.cc', 'browser/chromeos/cros/network_parser.h', + 'browser/chromeos/cros/network_ui_data.cc', + 'browser/chromeos/cros/network_ui_data.h', 'browser/chromeos/cros/onc_network_parser.cc', 'browser/chromeos/cros/onc_network_parser.h', 'browser/chromeos/cros/screen_lock_library.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 23eab00..55ac654 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1279,6 +1279,7 @@ 'browser/chromeos/cros/network_library.cc', 'browser/chromeos/cros/network_library.h', 'browser/chromeos/cros/network_library_unittest.cc', + 'browser/chromeos/cros/network_ui_data_unittest.cc', 'browser/chromeos/cros/onc_network_parser_unittest.cc', 'browser/chromeos/customization_document_unittest.cc', 'browser/chromeos/dbus/cros_dbus_service_unittest.cc', |