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