Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: chrome/browser/chromeos/cros/network_parser.cc

Issue 7453051: This factors out all of the parsing code from the network library (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reducing friends to minimal set. Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_parser.h"
6
7 #include "base/json/json_writer.h" // for debug output only.
8 #include "base/stringprintf.h"
9 // Needed only for debug output (ConnectionTypeToString).
10 #include "chrome/browser/chromeos/cros/native_network_constants.h"
11
12 namespace chromeos {
13
14 namespace {
15 Network* CreateNewNetwork(
16 ConnectionType type, const std::string& service_path) {
17 switch (type) {
18 case TYPE_ETHERNET: {
19 EthernetNetwork* ethernet = new EthernetNetwork(service_path);
20 return ethernet;
21 }
22 case TYPE_WIFI: {
23 WifiNetwork* wifi = new WifiNetwork(service_path);
24 return wifi;
25 }
26 case TYPE_CELLULAR: {
27 CellularNetwork* cellular = new CellularNetwork(service_path);
28 return cellular;
29 }
30 case TYPE_VPN: {
31 VirtualNetwork* vpn = new VirtualNetwork(service_path);
32 return vpn;
33 }
34 default: {
35 // If we try and create a service for which we have an unknown
36 // type, then that's a bug, and we will crash.
37 LOG(FATAL) << "Unknown service type: " << type;
38 return NULL;
39 }
40 }
41 }
42 } // namespace
43
44 NetworkDeviceParser::NetworkDeviceParser(
45 const EnumMapper<PropertyIndex>* mapper) : mapper_(mapper) {
46 CHECK(mapper);
47 }
48
49 NetworkDeviceParser::~NetworkDeviceParser() {
50 }
51
52 NetworkDevice* NetworkDeviceParser::CreateDeviceFromInfo(
53 const std::string& device_path,
54 const DictionaryValue& info) {
55 scoped_ptr<NetworkDevice> device(new NetworkDevice(device_path));
56 if (!UpdateDeviceFromInfo(info, device.get())) {
57 NOTREACHED() << "Unable to create new device";
58 return NULL;
59 }
60 VLOG(2) << "Created device for path " << device_path;
61 return device.release();
62 }
63
64 bool NetworkDeviceParser::UpdateDeviceFromInfo(const DictionaryValue& info,
65 NetworkDevice* device) {
66 for (DictionaryValue::key_iterator iter = info.begin_keys();
67 iter != info.end_keys(); ++iter) {
68 const std::string& key = *iter;
69 Value* value;
70 bool result = info.GetWithoutPathExpansion(key, &value);
71 DCHECK(result);
72 if (result)
73 UpdateStatus(key, *value, device, NULL);
74 }
75 if (VLOG_IS_ON(2)) {
76 std::string json;
77 base::JSONWriter::Write(&info, true, &json);
78 VLOG(2) << "Updated device for path "
79 << device->device_path() << ": " << json;
80 }
81 return true;
82 }
83
84 bool NetworkDeviceParser::UpdateStatus(const std::string& key,
85 const Value& value,
86 NetworkDevice* device,
87 PropertyIndex* index) {
88 PropertyIndex found_index = mapper().Get(key);
89 if (index)
90 *index = found_index;
91 if (!ParseValue(found_index, value, device)) {
92 VLOG(1) << "NetworkDeviceParser: Unhandled key: " << key;
93 return false;
94 }
95 if (VLOG_IS_ON(2)) {
96 std::string value_json;
97 base::JSONWriter::Write(&value, true, &value_json);
98 VLOG(2) << "Updated value on device: "
99 << device->device_path() << "[" << key << "] = " << value_json;
100 }
101 return true;
102 }
103
104 //----------- Network Parser -----------------
105
106 NetworkParser::NetworkParser(const EnumMapper<PropertyIndex>* mapper)
107 : mapper_(mapper) {
108 CHECK(mapper);
109 }
110
111 NetworkParser::~NetworkParser() {
112 }
113
114 Network* NetworkParser::CreateNetworkFromInfo(
115 const std::string& service_path,
116 const DictionaryValue& info) {
117 ConnectionType type = ParseTypeFromDictionary(info);
118 scoped_ptr<Network> network(CreateNewNetwork(type, service_path));
119 if (network.get())
120 UpdateNetworkFromInfo(info, network.get());
121 VLOG(2) << "Created Network '" << network->name()
122 << "' from info. Path:" << service_path
123 << " Type:" << ConnectionTypeToString(type);
124 return network.release();
125 }
126
127 bool NetworkParser::UpdateNetworkFromInfo(const DictionaryValue& info,
128 Network* network) {
129 network->set_unique_id("");
130 for (DictionaryValue::key_iterator iter = info.begin_keys();
131 iter != info.end_keys(); ++iter) {
132 const std::string& key = *iter;
133 Value* value;
134 bool res = info.GetWithoutPathExpansion(key, &value);
135 DCHECK(res);
136 if (res)
137 UpdateStatus(key, *value, network, NULL);
138 }
139 if (network->unique_id().empty())
140 network->CalculateUniqueId();
141 VLOG(2) << "Updated network '" << network->name()
142 << "' Path:" << network->service_path() << " Type:"
143 << ConnectionTypeToString(network->type());
144 return true;
145 }
146
147 bool NetworkParser::UpdateStatus(const std::string& key,
148 const Value& value,
149 Network* network,
150 PropertyIndex* index) {
151 PropertyIndex found_index = mapper().Get(key);
152 if (index)
153 *index = found_index;
154 if (!ParseValue(found_index, value, network)) {
155 VLOG(1) << "Unhandled key '" << key << "' in Network: " << network->name()
156 << " ID: " << network->unique_id()
157 << " Type: " << ConnectionTypeToString(network->type());
158 return false;
159 }
160 if (VLOG_IS_ON(2)) {
161 std::string value_json;
162 base::JSONWriter::Write(&value, true, &value_json);
163 VLOG(2) << "Updated value on network: "
164 << network->unique_id() << "[" << key << "] = " << value_json;
165 }
166 return true;
167 }
168
169 bool NetworkParser::ParseValue(PropertyIndex index,
170 const Value& value,
171 Network* network) {
172 switch (index) {
173 case PROPERTY_INDEX_TYPE: {
174 std::string type_string;
175 if (value.GetAsString(&type_string)) {
176 ConnectionType type = ParseType(type_string);
177 LOG_IF(ERROR, type != network->type())
178 << "Network with mismatched type: " << network->service_path()
179 << " " << type << " != " << network->type();
180 return true;
181 }
182 break;
183 }
184 case PROPERTY_INDEX_DEVICE: {
185 std::string device_path;
186 if (!value.GetAsString(&device_path))
187 break;
188 network->set_device_path(device_path);
189 return true;
190 }
191 case PROPERTY_INDEX_NAME: {
192 std::string name;
193 if (value.GetAsString(&name)) {
194 network->SetName(name);
195 return true;
196 }
197 break;
198 }
199 case PROPERTY_INDEX_GUID: {
200 std::string unique_id;
201 if (!value.GetAsString(&unique_id))
202 break;
203 network->set_unique_id(unique_id);
204 return true;
205 }
206 case PROPERTY_INDEX_PROFILE: {
207 // Note: currently this is only provided for non remembered networks.
208 std::string profile_path;
209 if (!value.GetAsString(&profile_path))
210 break;
211 network->set_profile_path(profile_path);
212 return true;
213 }
214 case PROPERTY_INDEX_STATE: {
215 std::string state_string;
216 if (value.GetAsString(&state_string)) {
217 network->SetState(ParseState(state_string));
218 return true;
219 }
220 break;
221 }
222 case PROPERTY_INDEX_MODE: {
223 std::string mode_string;
224 if (value.GetAsString(&mode_string)) {
225 network->mode_ = ParseMode(mode_string);
226 return true;
227 }
228 break;
229 }
230 case PROPERTY_INDEX_ERROR: {
231 std::string error_string;
232 if (value.GetAsString(&error_string)) {
233 network->error_ = ParseError(error_string);
234 return true;
235 }
236 break;
237 }
238 case PROPERTY_INDEX_CONNECTABLE: {
239 bool connectable;
240 if (!value.GetAsBoolean(&connectable))
241 break;
242 network->set_connectable(connectable);
243 return true;
244 }
245 case PROPERTY_INDEX_IS_ACTIVE: {
246 bool is_active;
247 if (!value.GetAsBoolean(&is_active))
248 break;
249 network->set_is_active(is_active);
250 return true;
251 }
252 case PROPERTY_INDEX_AUTO_CONNECT: {
253 bool auto_connect;
254 if (!value.GetAsBoolean(&auto_connect))
255 break;
256 network->set_auto_connect(auto_connect);
257 return true;
258 }
259 case PROPERTY_INDEX_SAVE_CREDENTIALS: {
260 bool save_credentials;
261 if (!value.GetAsBoolean(&save_credentials))
262 break;
263 network->set_save_credentials(save_credentials);
264 return true;
265 }
266 default:
267 break;
268 }
269 return false;
270 }
271
272 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/cros/network_parser.h ('k') | chrome/browser/chromeos/options/vpn_config_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698