OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/cros/network_parser.h" | 5 #include "chrome/browser/chromeos/cros/network_parser.h" |
6 | 6 |
7 #include "base/json/json_writer.h" // for debug output only. | 7 #include "base/json/json_writer.h" // for debug output only. |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 // Needed only for debug output (ConnectionTypeToString). | 9 // Needed only for debug output (ConnectionTypeToString). |
10 #include "chrome/browser/chromeos/cros/native_network_constants.h" | 10 #include "chrome/browser/chromeos/cros/native_network_constants.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 | 63 |
64 bool NetworkDeviceParser::UpdateDeviceFromInfo(const DictionaryValue& info, | 64 bool NetworkDeviceParser::UpdateDeviceFromInfo(const DictionaryValue& info, |
65 NetworkDevice* device) { | 65 NetworkDevice* device) { |
66 for (DictionaryValue::key_iterator iter = info.begin_keys(); | 66 for (DictionaryValue::key_iterator iter = info.begin_keys(); |
67 iter != info.end_keys(); ++iter) { | 67 iter != info.end_keys(); ++iter) { |
68 const std::string& key = *iter; | 68 const std::string& key = *iter; |
69 Value* value; | 69 Value* value; |
70 bool result = info.GetWithoutPathExpansion(key, &value); | 70 bool result = info.GetWithoutPathExpansion(key, &value); |
71 DCHECK(result); | 71 DCHECK(result); |
72 if (result) | 72 if (result) |
73 UpdateStatus(key, value, device, NULL); | 73 UpdateStatus(key, *value, device, NULL); |
74 } | 74 } |
75 if (VLOG_IS_ON(2)) { | 75 if (VLOG_IS_ON(2)) { |
76 std::string json; | 76 std::string json; |
77 base::JSONWriter::Write(&info, true, &json); | 77 base::JSONWriter::Write(&info, true, &json); |
78 VLOG(2) << "Updated device for path " | 78 VLOG(2) << "Updated device for path " |
79 << device->device_path() << ": " << json; | 79 << device->device_path() << ": " << json; |
80 } | 80 } |
81 return true; | 81 return true; |
82 } | 82 } |
83 | 83 |
84 bool NetworkDeviceParser::UpdateStatus(const std::string& key, | 84 bool NetworkDeviceParser::UpdateStatus(const std::string& key, |
85 Value* value, | 85 const Value& value, |
86 NetworkDevice* device, | 86 NetworkDevice* device, |
87 PropertyIndex* index) { | 87 PropertyIndex* index) { |
88 PropertyIndex found_index = mapper().Get(key); | 88 PropertyIndex found_index = mapper().Get(key); |
89 if (index) | 89 if (index) |
90 *index = found_index; | 90 *index = found_index; |
91 if (!ParseValue(found_index, value, device)) { | 91 if (!ParseValue(found_index, value, device)) { |
92 VLOG(1) << "NetworkDeviceParser: Unhandled key: " << key; | 92 VLOG(1) << "NetworkDeviceParser: Unhandled key: " << key; |
93 return false; | 93 return false; |
94 } | 94 } |
95 if (VLOG_IS_ON(2)) { | 95 if (VLOG_IS_ON(2)) { |
96 std::string value_json; | 96 std::string value_json; |
97 base::JSONWriter::Write(value, true, &value_json); | 97 base::JSONWriter::Write(&value, true, &value_json); |
98 VLOG(2) << "Updated value on device: " | 98 VLOG(2) << "Updated value on device: " |
99 << device->device_path() << "[" << key << "] = " << value_json; | 99 << device->device_path() << "[" << key << "] = " << value_json; |
100 } | 100 } |
101 return true; | 101 return true; |
102 } | 102 } |
103 | 103 |
104 //----------- Network Parser ----------------- | 104 //----------- Network Parser ----------------- |
105 | 105 |
106 NetworkParser::NetworkParser(const EnumMapper<PropertyIndex>* mapper) | 106 NetworkParser::NetworkParser(const EnumMapper<PropertyIndex>* mapper) |
107 : mapper_(mapper) { | 107 : mapper_(mapper) { |
(...skipping 19 matching lines...) Expand all Loading... |
127 bool NetworkParser::UpdateNetworkFromInfo(const DictionaryValue& info, | 127 bool NetworkParser::UpdateNetworkFromInfo(const DictionaryValue& info, |
128 Network* network) { | 128 Network* network) { |
129 network->set_unique_id(""); | 129 network->set_unique_id(""); |
130 for (DictionaryValue::key_iterator iter = info.begin_keys(); | 130 for (DictionaryValue::key_iterator iter = info.begin_keys(); |
131 iter != info.end_keys(); ++iter) { | 131 iter != info.end_keys(); ++iter) { |
132 const std::string& key = *iter; | 132 const std::string& key = *iter; |
133 Value* value; | 133 Value* value; |
134 bool res = info.GetWithoutPathExpansion(key, &value); | 134 bool res = info.GetWithoutPathExpansion(key, &value); |
135 DCHECK(res); | 135 DCHECK(res); |
136 if (res) | 136 if (res) |
137 network->UpdateStatus(key, value, NULL); | 137 network->UpdateStatus(key, *value, NULL); |
138 } | 138 } |
139 if (network->unique_id().empty()) | 139 if (network->unique_id().empty()) |
140 network->CalculateUniqueId(); | 140 network->CalculateUniqueId(); |
141 VLOG(2) << "Updated network '" << network->name() | 141 VLOG(2) << "Updated network '" << network->name() |
142 << "' Path:" << network->service_path() << " Type:" | 142 << "' Path:" << network->service_path() << " Type:" |
143 << ConnectionTypeToString(network->type()); | 143 << ConnectionTypeToString(network->type()); |
144 return true; | 144 return true; |
145 } | 145 } |
146 | 146 |
147 bool NetworkParser::UpdateStatus(const std::string& key, | 147 bool NetworkParser::UpdateStatus(const std::string& key, |
148 Value* value, | 148 const Value& value, |
149 Network* network, | 149 Network* network, |
150 PropertyIndex* index) { | 150 PropertyIndex* index) { |
151 PropertyIndex found_index = mapper().Get(key); | 151 PropertyIndex found_index = mapper().Get(key); |
152 if (index) | 152 if (index) |
153 *index = found_index; | 153 *index = found_index; |
154 if (!ParseValue(found_index, value, network)) { | 154 if (!ParseValue(found_index, value, network)) { |
155 VLOG(1) << "Unhandled key '" << key << "' in Network: " << network->name() | 155 VLOG(1) << "Unhandled key '" << key << "' in Network: " << network->name() |
156 << " ID: " << network->unique_id() | 156 << " ID: " << network->unique_id() |
157 << " Type: " << ConnectionTypeToString(network->type()); | 157 << " Type: " << ConnectionTypeToString(network->type()); |
158 return false; | 158 return false; |
159 } | 159 } |
160 if (VLOG_IS_ON(2)) { | 160 if (VLOG_IS_ON(2)) { |
161 std::string value_json; | 161 std::string value_json; |
162 base::JSONWriter::Write(value, true, &value_json); | 162 base::JSONWriter::Write(&value, true, &value_json); |
163 VLOG(2) << "Updated value on network: " | 163 VLOG(2) << "Updated value on network: " |
164 << network->unique_id() << "[" << key << "] = " << value_json; | 164 << network->unique_id() << "[" << key << "] = " << value_json; |
165 } | 165 } |
166 return true; | 166 return true; |
167 } | 167 } |
168 | 168 |
169 bool NetworkParser::ParseValue(PropertyIndex index, | 169 bool NetworkParser::ParseValue(PropertyIndex index, |
170 Value* value, | 170 const Value& value, |
171 Network* network) { | 171 Network* network) { |
172 switch (index) { | 172 switch (index) { |
173 case PROPERTY_INDEX_TYPE: { | 173 case PROPERTY_INDEX_TYPE: { |
174 std::string type_string; | 174 std::string type_string; |
175 if (value->GetAsString(&type_string)) { | 175 if (value.GetAsString(&type_string)) { |
176 ConnectionType type = ParseType(type_string); | 176 ConnectionType type = ParseType(type_string); |
177 LOG_IF(ERROR, type != network->type()) | 177 LOG_IF(ERROR, type != network->type()) |
178 << "Network with mismatched type: " << network->service_path() | 178 << "Network with mismatched type: " << network->service_path() |
179 << " " << type << " != " << network->type(); | 179 << " " << type << " != " << network->type(); |
180 return true; | 180 return true; |
181 } | 181 } |
182 break; | 182 break; |
183 } | 183 } |
184 case PROPERTY_INDEX_DEVICE: { | 184 case PROPERTY_INDEX_DEVICE: { |
185 std::string device_path; | 185 std::string device_path; |
186 if (!value->GetAsString(&device_path)) | 186 if (!value.GetAsString(&device_path)) |
187 break; | 187 break; |
188 network->set_device_path(device_path); | 188 network->set_device_path(device_path); |
189 return true; | 189 return true; |
190 } | 190 } |
191 case PROPERTY_INDEX_NAME: { | 191 case PROPERTY_INDEX_NAME: { |
192 std::string name; | 192 std::string name; |
193 if (value->GetAsString(&name)) { | 193 if (value.GetAsString(&name)) { |
194 network->SetName(name); | 194 network->SetName(name); |
195 return true; | 195 return true; |
196 } | 196 } |
197 break; | 197 break; |
198 } | 198 } |
199 case PROPERTY_INDEX_GUID: { | 199 case PROPERTY_INDEX_GUID: { |
200 std::string unique_id; | 200 std::string unique_id; |
201 if (!value->GetAsString(&unique_id)) | 201 if (!value.GetAsString(&unique_id)) |
202 break; | 202 break; |
203 network->set_unique_id(unique_id); | 203 network->set_unique_id(unique_id); |
204 return true; | 204 return true; |
205 } | 205 } |
206 case PROPERTY_INDEX_PROFILE: { | 206 case PROPERTY_INDEX_PROFILE: { |
207 // Note: currently this is only provided for non remembered networks. | 207 // Note: currently this is only provided for non remembered networks. |
208 std::string profile_path; | 208 std::string profile_path; |
209 if (!value->GetAsString(&profile_path)) | 209 if (!value.GetAsString(&profile_path)) |
210 break; | 210 break; |
211 network->set_profile_path(profile_path); | 211 network->set_profile_path(profile_path); |
212 return true; | 212 return true; |
213 } | 213 } |
214 case PROPERTY_INDEX_STATE: { | 214 case PROPERTY_INDEX_STATE: { |
215 std::string state_string; | 215 std::string state_string; |
216 if (value->GetAsString(&state_string)) { | 216 if (value.GetAsString(&state_string)) { |
217 network->SetState(ParseState(state_string)); | 217 network->SetState(ParseState(state_string)); |
218 return true; | 218 return true; |
219 } | 219 } |
220 break; | 220 break; |
221 } | 221 } |
222 case PROPERTY_INDEX_MODE: { | 222 case PROPERTY_INDEX_MODE: { |
223 std::string mode_string; | 223 std::string mode_string; |
224 if (value->GetAsString(&mode_string)) { | 224 if (value.GetAsString(&mode_string)) { |
225 network->mode_ = ParseMode(mode_string); | 225 network->mode_ = ParseMode(mode_string); |
226 return true; | 226 return true; |
227 } | 227 } |
228 break; | 228 break; |
229 } | 229 } |
230 case PROPERTY_INDEX_ERROR: { | 230 case PROPERTY_INDEX_ERROR: { |
231 std::string error_string; | 231 std::string error_string; |
232 if (value->GetAsString(&error_string)) { | 232 if (value.GetAsString(&error_string)) { |
233 network->error_ = ParseError(error_string); | 233 network->error_ = ParseError(error_string); |
234 return true; | 234 return true; |
235 } | 235 } |
236 break; | 236 break; |
237 } | 237 } |
238 case PROPERTY_INDEX_CONNECTABLE: { | 238 case PROPERTY_INDEX_CONNECTABLE: { |
239 bool connectable; | 239 bool connectable; |
240 if (!value->GetAsBoolean(&connectable)) | 240 if (!value.GetAsBoolean(&connectable)) |
241 break; | 241 break; |
242 network->set_connectable(connectable); | 242 network->set_connectable(connectable); |
243 return true; | 243 return true; |
244 } | 244 } |
245 case PROPERTY_INDEX_IS_ACTIVE: { | 245 case PROPERTY_INDEX_IS_ACTIVE: { |
246 bool is_active; | 246 bool is_active; |
247 if (!value->GetAsBoolean(&is_active)) | 247 if (!value.GetAsBoolean(&is_active)) |
248 break; | 248 break; |
249 network->set_is_active(is_active); | 249 network->set_is_active(is_active); |
250 return true; | 250 return true; |
251 } | 251 } |
252 case PROPERTY_INDEX_AUTO_CONNECT: { | 252 case PROPERTY_INDEX_AUTO_CONNECT: { |
253 bool auto_connect; | 253 bool auto_connect; |
254 if (!value->GetAsBoolean(&auto_connect)) | 254 if (!value.GetAsBoolean(&auto_connect)) |
255 break; | 255 break; |
256 network->set_auto_connect(auto_connect); | 256 network->set_auto_connect(auto_connect); |
257 return true; | 257 return true; |
258 } | 258 } |
259 case PROPERTY_INDEX_SAVE_CREDENTIALS: { | 259 case PROPERTY_INDEX_SAVE_CREDENTIALS: { |
260 bool save_credentials; | 260 bool save_credentials; |
261 if (!value->GetAsBoolean(&save_credentials)) | 261 if (!value.GetAsBoolean(&save_credentials)) |
262 break; | 262 break; |
263 network->set_save_credentials(save_credentials); | 263 network->set_save_credentials(save_credentials); |
264 return true; | 264 return true; |
265 } | 265 } |
266 default: | 266 default: |
267 break; | 267 break; |
268 } | 268 } |
269 return false; | 269 return false; |
270 } | 270 } |
271 | 271 |
272 } // namespace chromeos | 272 } // namespace chromeos |
OLD | NEW |