Service workers: Allow HTTPS pages arrived at via HTTP redirect to use SW
[chromium-blink-merge.git] / chromeos / network / network_ui_data.cc
blob614dc4005bc27621de2bbba5b8b0b78c350ae4fc
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"
10 namespace chromeos {
12 // Top-level UI data dictionary keys.
13 const char NetworkUIData::kKeyONCSource[] = "onc_source";
14 const char NetworkUIData::kKeyUserSettings[] = "user_settings";
15 const char NetworkUIData::kONCSourceUserImport[] = "user_import";
16 const char NetworkUIData::kONCSourceDevicePolicy[] = "device_policy";
17 const char NetworkUIData::kONCSourceUserPolicy[] = "user_policy";
19 namespace {
21 template <typename Enum>
22 struct StringEnumEntry {
23 const char* string;
24 Enum enum_value;
27 const StringEnumEntry< ::onc::ONCSource> kONCSourceTable[] = {
28 { NetworkUIData::kONCSourceUserImport, ::onc::ONC_SOURCE_USER_IMPORT },
29 { NetworkUIData::kONCSourceDevicePolicy, ::onc::ONC_SOURCE_DEVICE_POLICY },
30 { NetworkUIData::kONCSourceUserPolicy, ::onc::ONC_SOURCE_USER_POLICY }
33 // Converts |enum_value| to the corresponding string according to |table|. If no
34 // enum value of the table matches (which can only occur if incorrect casting
35 // was used to obtain |enum_value|), returns an empty string instead.
36 template <typename Enum, int N>
37 std::string EnumToString(const StringEnumEntry<Enum>(& table)[N],
38 Enum enum_value) {
39 for (int i = 0; i < N; ++i) {
40 if (table[i].enum_value == enum_value)
41 return table[i].string;
43 return std::string();
46 // Converts |str| to the corresponding enum value according to |table|. If no
47 // string of the table matches, returns |fallback| instead.
48 template<typename Enum, int N>
49 Enum StringToEnum(const StringEnumEntry<Enum>(& table)[N],
50 const std::string& str,
51 Enum fallback) {
52 for (int i = 0; i < N; ++i) {
53 if (table[i].string == str)
54 return table[i].enum_value;
56 return fallback;
59 } // namespace
61 NetworkUIData::NetworkUIData() : onc_source_(::onc::ONC_SOURCE_NONE) {
64 NetworkUIData::NetworkUIData(const NetworkUIData& other) {
65 *this = other;
68 NetworkUIData& NetworkUIData::operator=(const NetworkUIData& other) {
69 onc_source_ = other.onc_source_;
70 if (other.user_settings_)
71 user_settings_.reset(other.user_settings_->DeepCopy());
72 return *this;
75 NetworkUIData::NetworkUIData(const base::DictionaryValue& dict) {
76 std::string source;
77 dict.GetString(kKeyONCSource, &source);
78 onc_source_ = StringToEnum(kONCSourceTable, source, ::onc::ONC_SOURCE_NONE);
80 std::string type_string;
82 const base::DictionaryValue* user_settings = NULL;
83 if (dict.GetDictionary(kKeyUserSettings, &user_settings))
84 user_settings_.reset(user_settings->DeepCopy());
87 NetworkUIData::~NetworkUIData() {
90 void NetworkUIData::set_user_settings(scoped_ptr<base::DictionaryValue> dict) {
91 user_settings_ = dict.Pass();
94 std::string NetworkUIData::GetONCSourceAsString() const {
95 return EnumToString(kONCSourceTable, onc_source_);
98 void NetworkUIData::FillDictionary(base::DictionaryValue* dict) const {
99 dict->Clear();
101 std::string source_string = GetONCSourceAsString();
102 if (!source_string.empty())
103 dict->SetString(kKeyONCSource, source_string);
105 if (user_settings_)
106 dict->SetWithoutPathExpansion(kKeyUserSettings,
107 user_settings_->DeepCopy());
110 // static
111 scoped_ptr<NetworkUIData> NetworkUIData::CreateFromONC(
112 ::onc::ONCSource onc_source) {
113 scoped_ptr<NetworkUIData> ui_data(new NetworkUIData());
115 ui_data->onc_source_ = onc_source;
117 return ui_data.Pass();
120 } // namespace chromeos