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 3 // found in the LICENSE file. 4 5 #include "chromeos/network/network_ui_data.h" 6 7 #include "base/logging.h" 8 #include "base/values.h" 9 #include "components/onc/onc_constants.h" 10 11 namespace chromeos { 12 13 // Top-level UI data dictionary keys. 14 const char NetworkUIData::kKeyONCSource[] = "onc_source"; 15 const char NetworkUIData::kKeyUserSettings[] = "user_settings"; 16 const char NetworkUIData::kONCSourceUserImport[] = "user_import"; 17 const char NetworkUIData::kONCSourceDevicePolicy[] = "device_policy"; 18 const char NetworkUIData::kONCSourceUserPolicy[] = "user_policy"; 19 20 namespace { 21 22 template <typename Enum> 23 struct StringEnumEntry { 24 const char* string; 25 Enum enum_value; 26 }; 27 28 const StringEnumEntry< ::onc::ONCSource> kONCSourceTable[] = { 29 { NetworkUIData::kONCSourceUserImport, ::onc::ONC_SOURCE_USER_IMPORT }, 30 { NetworkUIData::kONCSourceDevicePolicy, ::onc::ONC_SOURCE_DEVICE_POLICY }, 31 { NetworkUIData::kONCSourceUserPolicy, ::onc::ONC_SOURCE_USER_POLICY } 32 }; 33 34 // Converts |enum_value| to the corresponding string according to |table|. If no 35 // enum value of the table matches (which can only occur if incorrect casting 36 // was used to obtain |enum_value|), returns an empty string instead. 37 template <typename Enum, int N> 38 std::string EnumToString(const StringEnumEntry<Enum>(& table)[N], 39 Enum enum_value) { 40 for (int i = 0; i < N; ++i) { 41 if (table[i].enum_value == enum_value) 42 return table[i].string; 43 } 44 return std::string(); 45 } 46 47 // Converts |str| to the corresponding enum value according to |table|. If no 48 // string of the table matches, returns |fallback| instead. 49 template<typename Enum, int N> 50 Enum StringToEnum(const StringEnumEntry<Enum>(& table)[N], 51 const std::string& str, 52 Enum fallback) { 53 for (int i = 0; i < N; ++i) { 54 if (table[i].string == str) 55 return table[i].enum_value; 56 } 57 return fallback; 58 } 59 60 } // namespace 61 62 NetworkUIData::NetworkUIData() : onc_source_(::onc::ONC_SOURCE_NONE) { 63 } 64 65 NetworkUIData::NetworkUIData(const NetworkUIData& other) { 66 *this = other; 67 } 68 69 NetworkUIData& NetworkUIData::operator=(const NetworkUIData& other) { 70 onc_source_ = other.onc_source_; 71 if (other.user_settings_) 72 user_settings_.reset(other.user_settings_->DeepCopy()); 73 return *this; 74 } 75 76 NetworkUIData::NetworkUIData(const base::DictionaryValue& dict) { 77 std::string source; 78 dict.GetString(kKeyONCSource, &source); 79 onc_source_ = StringToEnum(kONCSourceTable, source, ::onc::ONC_SOURCE_NONE); 80 81 std::string type_string; 82 83 const base::DictionaryValue* user_settings = NULL; 84 if (dict.GetDictionary(kKeyUserSettings, &user_settings)) 85 user_settings_.reset(user_settings->DeepCopy()); 86 } 87 88 NetworkUIData::~NetworkUIData() { 89 } 90 91 void NetworkUIData::set_user_settings(scoped_ptr<base::DictionaryValue> dict) { 92 user_settings_ = dict.Pass(); 93 } 94 95 std::string NetworkUIData::GetONCSourceAsString() const { 96 return EnumToString(kONCSourceTable, onc_source_); 97 } 98 99 void NetworkUIData::FillDictionary(base::DictionaryValue* dict) const { 100 dict->Clear(); 101 102 std::string source_string = GetONCSourceAsString(); 103 if (!source_string.empty()) 104 dict->SetString(kKeyONCSource, source_string); 105 106 if (user_settings_) 107 dict->SetWithoutPathExpansion(kKeyUserSettings, 108 user_settings_->DeepCopy()); 109 } 110 111 // static 112 scoped_ptr<NetworkUIData> NetworkUIData::CreateFromONC( 113 ::onc::ONCSource onc_source) { 114 scoped_ptr<NetworkUIData> ui_data(new NetworkUIData()); 115 116 ui_data->onc_source_ = onc_source; 117 118 return ui_data.Pass(); 119 } 120 121 } // namespace chromeos 122