OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 "chrome/browser/chromeos/cros/network_ui_data.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "chrome/browser/chromeos/cros/network_library.h" | |
9 #include "chrome/browser/chromeos/cros/network_parser.h" | |
10 | |
11 namespace chromeos { | |
12 | |
13 namespace { | |
14 | |
15 const EnumMapper<NetworkUIData::ONCSource>::Pair kONCSourceTable[] = { | |
16 { "user_import", NetworkUIData::ONC_SOURCE_USER_IMPORT }, | |
17 { "device_policy", NetworkUIData::ONC_SOURCE_DEVICE_POLICY }, | |
18 { "user_policy", NetworkUIData::ONC_SOURCE_USER_POLICY }, | |
19 }; | |
20 | |
21 const EnumMapper<NetworkPropertyUIData::Controller>::Pair | |
22 kControllerTable[] = { | |
23 { "user", NetworkPropertyUIData::CONTROLLER_USER }, | |
24 { "policy", NetworkPropertyUIData::CONTROLLER_POLICY }, | |
25 }; | |
26 | |
27 } | |
28 | |
29 const char NetworkUIData::kKeyONCSource[] = "onc_source"; | |
30 const char NetworkUIData::kKeyProperties[] = "properties"; | |
31 | |
32 // Property names for per-property data stored under |kKeyProperties|. | |
33 const char NetworkUIData::kPropertyAutoConnect[] = "auto_connect"; | |
34 const char NetworkUIData::kPropertyPreferred[] = "preferred"; | |
35 const char NetworkUIData::kPropertyPassphrase[] = "passphrase"; | |
36 const char NetworkUIData::kPropertySaveCredentials[] = "save_credentials"; | |
37 | |
38 const char NetworkUIData::kPropertyVPNCaCertNss[] = "VPN.ca_cert_nss"; | |
39 const char NetworkUIData::kPropertyVPNPskPassphrase[] = "VPN.psk_passphrase"; | |
40 const char NetworkUIData::kPropertyVPNClientCertId[] = "VPN.client_cert_id"; | |
41 const char NetworkUIData::kPropertyVPNUsername[] = "VPN.username"; | |
42 const char NetworkUIData::kPropertyVPNUserPassphrase[] = "VPN.user_passphrase"; | |
43 const char NetworkUIData::kPropertyVPNGroupName[] = "VPN.group_name"; | |
44 | |
45 const char NetworkUIData::kPropertyEAPMethod[] = "EAP.method"; | |
46 const char NetworkUIData::kPropertyEAPPhase2Auth[] = "EAP.phase_2_auth"; | |
47 const char NetworkUIData::kPropertyEAPServerCaCertNssNickname[] = | |
48 "EAP.server_ca_cert_nss_nickname"; | |
49 const char NetworkUIData::kPropertyEAPClientCertPkcs11Id[] = | |
50 "EAP.client_cert_pkcs11_id"; | |
51 const char NetworkUIData::kPropertyEAPUseSystemCAs[] = "EAP.use_system_cas"; | |
52 const char NetworkUIData::kPropertyEAPIdentity[] = "EAP.identity"; | |
53 const char NetworkUIData::kPropertyEAPAnonymousIdentity[] = | |
54 "EAP.anonymous_identity"; | |
55 const char NetworkUIData::kPropertyEAPPassphrase[] = "EAP.passphrase"; | |
56 | |
57 // Property names for the per-property dictionary. | |
58 const char NetworkPropertyUIData::kKeyController[] = "controller"; | |
59 const char NetworkPropertyUIData::kKeyDefaultValue[] = "default_value"; | |
60 | |
61 NetworkUIData::NetworkUIData() | |
62 : source_(ONC_SOURCE_NONE) {} | |
63 | |
64 void NetworkUIData::SetProperty(const char* property_key, | |
65 const NetworkPropertyUIData& ui_data) { | |
66 properties_.Set(property_key, ui_data.BuildDictionary()); | |
67 } | |
68 | |
69 void NetworkUIData::FillDictionary(base::DictionaryValue* dict) const { | |
70 std::string source_string = EncodeONCSource(source_); | |
71 if (!source_string.empty()) | |
72 dict->SetString(kKeyONCSource, source_string); | |
73 dict->Set(kKeyProperties, properties_.DeepCopy()); | |
74 } | |
75 | |
76 // static | |
77 NetworkUIData::ONCSource NetworkUIData::GetONCSource(const Network* network) { | |
78 std::string source; | |
79 if (network->ui_data()->GetString(kKeyONCSource, &source)) | |
80 return DecodeONCSource(source); | |
81 return ONC_SOURCE_NONE; | |
82 } | |
83 | |
84 // static | |
85 bool NetworkUIData::IsManaged(const Network* network) { | |
86 ONCSource source = GetONCSource(network); | |
87 return source == ONC_SOURCE_DEVICE_POLICY || source == ONC_SOURCE_USER_POLICY; | |
88 } | |
89 | |
90 // static | |
91 std::string NetworkUIData::EncodeONCSource(ONCSource source) { | |
92 EnumMapper<ONCSource> mapper(kONCSourceTable, arraysize(kONCSourceTable), | |
93 ONC_SOURCE_NONE); | |
stevenjb
2011/11/29 17:35:16
Use CR_DEFINE_STATIC_LOCAL to make this static so
Mattias Nissler (ping if slow)
2011/11/29 20:48:47
Done.
| |
94 std::string source_string; | |
95 if (!mapper.GetKey(source, &source_string)) | |
96 NOTREACHED() << "Invalid ONC source value " << source; | |
97 return source_string; | |
98 } | |
99 | |
100 // static | |
101 NetworkUIData::ONCSource NetworkUIData::DecodeONCSource( | |
102 const std::string& source) { | |
103 EnumMapper<ONCSource> mapper(kONCSourceTable, arraysize(kONCSourceTable), | |
104 ONC_SOURCE_NONE); | |
105 return mapper.Get(source); | |
106 } | |
107 | |
108 NetworkPropertyUIData::NetworkPropertyUIData() | |
109 : controller_(CONTROLLER_USER) {} | |
stevenjb
2011/11/29 17:35:16
nit: } on a separate line is easier to read outsid
Mattias Nissler (ping if slow)
2011/11/29 20:48:47
Some people prefer one style, some people the othe
| |
110 | |
111 NetworkPropertyUIData::NetworkPropertyUIData(Controller controller, | |
112 base::Value* default_value) | |
113 : controller_(controller), | |
114 default_value_(default_value) {} | |
115 | |
116 NetworkPropertyUIData::NetworkPropertyUIData(const Network* network, | |
117 const char* property_key) { | |
118 UpdateFromNetwork(network, property_key); | |
119 } | |
120 | |
121 void NetworkPropertyUIData::UpdateFromNetwork(const Network* network, | |
122 const char* property_key) { | |
123 // If there is no per-property information available, the property inherits | |
124 // the controlled state of the network. | |
125 controller_ = | |
126 NetworkUIData::IsManaged(network) ? CONTROLLER_POLICY : CONTROLLER_USER; | |
127 | |
128 if (!property_key) | |
129 return; | |
130 | |
131 const base::DictionaryValue* ui_data = network->ui_data(); | |
132 if (!ui_data) | |
133 return; | |
134 | |
135 base::DictionaryValue* property_map = NULL; | |
136 if (!ui_data->GetDictionary(NetworkUIData::kKeyProperties, &property_map)) | |
137 return; | |
138 | |
139 base::DictionaryValue* property = NULL; | |
140 if (!property_map->GetDictionary(property_key, &property)) | |
141 return; | |
142 | |
143 std::string controller; | |
144 if (property->GetString(kKeyController, &controller)) | |
145 controller_ = DecodeController(controller); | |
146 | |
147 base::Value* default_value = NULL; | |
148 property->Get(kKeyDefaultValue, &default_value); | |
149 default_value_.reset(default_value); | |
stevenjb
2011/11/29 17:35:16
Do we always want to reset default_value, even if
Mattias Nissler (ping if slow)
2011/11/29 20:48:47
Done.
| |
150 } | |
151 | |
152 base::DictionaryValue* NetworkPropertyUIData::BuildDictionary() const { | |
153 base::DictionaryValue* dict = new base::DictionaryValue(); | |
154 dict->SetString(kKeyController, | |
155 EncodeController(controller_)); | |
156 if (default_value_.get()) | |
157 dict->Set(kKeyDefaultValue, default_value_->DeepCopy()); | |
158 return dict; | |
159 } | |
160 | |
161 // static | |
162 std::string NetworkPropertyUIData::EncodeController(Controller controller) { | |
stevenjb
2011/11/29 17:35:16
Are we likely to need these outside of this class?
Mattias Nissler (ping if slow)
2011/11/29 20:48:47
Done.
stevenjb
2011/11/29 23:39:05
What I meant to suggest was that, rather than expo
Mattias Nissler (ping if slow)
2011/11/30 10:16:47
Ah, the reason I decided to leave as is is that pr
| |
163 EnumMapper<Controller> mapper(kControllerTable, arraysize(kControllerTable), | |
164 CONTROLLER_USER); | |
165 std::string controller_string; | |
166 if (!mapper.GetKey(controller, &controller_string)) | |
167 NOTREACHED() << "Invalid property controller value " << controller; | |
168 return controller_string; | |
169 } | |
170 | |
171 // static | |
172 NetworkPropertyUIData::Controller NetworkPropertyUIData::DecodeController( | |
173 const std::string& controller) { | |
174 EnumMapper<Controller> mapper(kControllerTable, arraysize(kControllerTable), | |
175 CONTROLLER_USER); | |
176 return mapper.Get(controller); | |
177 } | |
178 | |
179 } // namespace chromeos | |
OLD | NEW |