Fix the bookmanager page display issue on MacOS.
[chromium-blink-merge.git] / chromeos / network / network_ui_data.cc
blob2a592715b712b038d2c55b0f7974845258da2081
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.
5 #include "chromeos/network/network_ui_data.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "components/onc/onc_constants.h"
11 namespace chromeos {
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";
20 namespace {
22 template <typename Enum>
23 struct StringEnumEntry {
24 const char* string;
25 Enum enum_value;
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 }
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;
44 return std::string();
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;
57 return fallback;
60 } // namespace
62 NetworkUIData::NetworkUIData() : onc_source_(::onc::ONC_SOURCE_NONE) {
65 NetworkUIData::NetworkUIData(const NetworkUIData& other) {
66 *this = other;
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;
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);
81 std::string type_string;
83 const base::DictionaryValue* user_settings = NULL;
84 if (dict.GetDictionary(kKeyUserSettings, &user_settings))
85 user_settings_.reset(user_settings->DeepCopy());
88 NetworkUIData::~NetworkUIData() {
91 void NetworkUIData::set_user_settings(scoped_ptr<base::DictionaryValue> dict) {
92 user_settings_ = dict.Pass();
95 std::string NetworkUIData::GetONCSourceAsString() const {
96 return EnumToString(kONCSourceTable, onc_source_);
99 void NetworkUIData::FillDictionary(base::DictionaryValue* dict) const {
100 dict->Clear();
102 std::string source_string = GetONCSourceAsString();
103 if (!source_string.empty())
104 dict->SetString(kKeyONCSource, source_string);
106 if (user_settings_)
107 dict->SetWithoutPathExpansion(kKeyUserSettings,
108 user_settings_->DeepCopy());
111 // static
112 scoped_ptr<NetworkUIData> NetworkUIData::CreateFromONC(
113 ::onc::ONCSource onc_source) {
114 scoped_ptr<NetworkUIData> ui_data(new NetworkUIData());
116 ui_data->onc_source_ = onc_source;
118 return ui_data.Pass();
121 } // namespace chromeos