1215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor// Copyright (c) 2012 The Chromium Authors. All rights reserved. 28e8fb3be5bd78f0564444eca02b404566a5f3b5dAndy Gibbs// Use of this source code is governed by a BSD-style license that can be 3215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor// found in the LICENSE file. 4215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor 5215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor#include "chromeos/network/network_ui_data.h" 6215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor 7215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor#include "base/logging.h" 8215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor#include "base/values.h" 9215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor#include "components/onc/onc_constants.h" 10215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor 11215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregornamespace chromeos { 12215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor 13215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor// Top-level UI data dictionary keys. 14const char NetworkUIData::kKeyONCSource[] = "onc_source"; 15const char NetworkUIData::kKeyUserSettings[] = "user_settings"; 16const char NetworkUIData::kONCSourceUserImport[] = "user_import"; 17const char NetworkUIData::kONCSourceDevicePolicy[] = "device_policy"; 18const char NetworkUIData::kONCSourceUserPolicy[] = "user_policy"; 19 20namespace { 21 22template <typename Enum> 23struct StringEnumEntry { 24 const char* string; 25 Enum enum_value; 26}; 27 28const 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. 37template <typename Enum, int N> 38std::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. 49template<typename Enum, int N> 50Enum 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 62NetworkUIData::NetworkUIData() : onc_source_(::onc::ONC_SOURCE_NONE) { 63} 64 65NetworkUIData::NetworkUIData(const NetworkUIData& other) { 66 *this = other; 67} 68 69NetworkUIData& 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 76NetworkUIData::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 88NetworkUIData::~NetworkUIData() { 89} 90 91void NetworkUIData::set_user_settings(scoped_ptr<base::DictionaryValue> dict) { 92 user_settings_ = dict.Pass(); 93} 94 95std::string NetworkUIData::GetONCSourceAsString() const { 96 return EnumToString(kONCSourceTable, onc_source_); 97} 98 99void 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 112scoped_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