| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_library_impl_cros.h" | 5 #include "chrome/browser/chromeos/cros/network_library_impl_cros.h" |
| 6 | 6 |
| 7 #include <dbus/dbus-glib.h> | 7 #include <dbus/dbus-glib.h> |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/json/json_writer.h" // for debug output only. | 10 #include "base/json/json_writer.h" // for debug output only. |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/chromeos/cros/cros_library.h" | 13 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 14 #include "chrome/browser/chromeos/cros/native_network_constants.h" | 14 #include "chrome/browser/chromeos/cros/native_network_constants.h" |
| 15 #include "chrome/browser/chromeos/cros/native_network_parser.h" | 15 #include "chrome/browser/chromeos/cros/native_network_parser.h" |
| 16 #include "chrome/browser/chromeos/settings/cros_settings.h" | 16 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "third_party/cros_system_api/dbus/service_constants.h" | 19 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 20 | 20 |
| 21 using content::BrowserThread; | 21 using content::BrowserThread; |
| 22 | 22 |
| 23 namespace chromeos { | 23 namespace chromeos { |
| 24 | 24 |
| 25 // Structure used to pass IP parameter info to a DoSetIPParameters callback, |
| 26 // since Bind only takes up to six parameters. |
| 27 struct NetworkLibraryImplCros::IPParameterInfo { |
| 28 std::string address; |
| 29 std::string netmask; |
| 30 std::string gateway; |
| 31 std::string name_servers; |
| 32 int dhcp_usage_mask; |
| 33 }; |
| 34 |
| 25 namespace { | 35 namespace { |
| 26 | 36 |
| 27 // List of cellular operators names that should have data roaming always enabled | 37 // List of cellular operators names that should have data roaming always enabled |
| 28 // to be able to connect to any network. | 38 // to be able to connect to any network. |
| 29 const char* kAlwaysInRoamingOperators[] = { | 39 const char* kAlwaysInRoamingOperators[] = { |
| 30 "CUBIC" | 40 "CUBIC" |
| 31 }; | 41 }; |
| 32 | 42 |
| 33 // List of interfaces that have portal check enabled by default. | 43 // List of interfaces that have portal check enabled by default. |
| 34 const char kDefaultCheckPortalList[] = "ethernet,wifi,cellular"; | 44 const char kDefaultCheckPortalList[] = "ethernet,wifi,cellular"; |
| (...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 output.push_back((*hardware_address)[i]); | 536 output.push_back((*hardware_address)[i]); |
| 527 } | 537 } |
| 528 *hardware_address = output; | 538 *hardware_address = output; |
| 529 } | 539 } |
| 530 } else { | 540 } else { |
| 531 DCHECK_EQ(format, FORMAT_RAW_HEX); | 541 DCHECK_EQ(format, FORMAT_RAW_HEX); |
| 532 } | 542 } |
| 533 return ipconfig_vector; | 543 return ipconfig_vector; |
| 534 } | 544 } |
| 535 | 545 |
| 536 void NetworkLibraryImplCros::SetIPConfig(const NetworkIPConfig& ipconfig) { | 546 void NetworkLibraryImplCros::SetIPParameters(const std::string& service_path, |
| 537 if (ipconfig.device_path.empty()) | 547 const std::string& address, |
| 548 const std::string& netmask, |
| 549 const std::string& gateway, |
| 550 const std::string& name_servers, |
| 551 int dhcp_usage_mask) { |
| 552 if (service_path.empty()) |
| 538 return; | 553 return; |
| 539 | 554 |
| 540 VLOG(1) << "Setting IPConfig: " << ipconfig.ToString(); | 555 VLOG(1) << "Setting IP parameters: " |
| 556 << "address: " << address |
| 557 << (dhcp_usage_mask & USE_DHCP_ADDRESS ? |
| 558 " (ignored)" : " (in use)") |
| 559 << "netmask: " << netmask |
| 560 << (dhcp_usage_mask & USE_DHCP_NETMASK ? |
| 561 " (ignored)" : " (in use)") |
| 562 << "gateway: " << gateway |
| 563 << (dhcp_usage_mask & USE_DHCP_GATEWAY ? |
| 564 " (ignored)" : " (in use)") |
| 565 << "name_servers: " << name_servers |
| 566 << (dhcp_usage_mask & USE_DHCP_NAME_SERVERS ? |
| 567 " (ignored)" : " (in use)"); |
| 541 | 568 |
| 542 NetworkIPConfig* ipconfig_dhcp = NULL; | 569 // Have to pass these in a structure, since Bind only takes up to six |
| 543 std::string ipconfig_dhcp_path; | 570 // parameters. |
| 544 NetworkIPConfig* ipconfig_static = NULL; | 571 IPParameterInfo info; |
| 545 std::string ipconfig_static_path; | 572 info.address = address; |
| 573 info.netmask = netmask; |
| 574 info.gateway = gateway; |
| 575 info.name_servers = name_servers; |
| 576 info.dhcp_usage_mask = dhcp_usage_mask; |
| 577 chromeos::NetworkPropertiesCallback callback = |
| 578 base::Bind(&NetworkLibraryImplCros::SetIPParametersCallback, |
| 579 weak_ptr_factory_.GetWeakPtr(), info); |
| 546 | 580 |
| 547 NetworkIPConfigVector ipconfigs; | 581 CrosRequestNetworkServiceProperties(service_path, callback); |
| 548 std::vector<std::string> ipconfig_paths; | |
| 549 CrosListIPConfigs(ipconfig.device_path, &ipconfigs, &ipconfig_paths, NULL); | |
| 550 for (size_t i = 0; i < ipconfigs.size(); ++i) { | |
| 551 if (ipconfigs[i].type == chromeos::IPCONFIG_TYPE_DHCP) { | |
| 552 ipconfig_dhcp = &ipconfigs[i]; | |
| 553 ipconfig_dhcp_path = ipconfig_paths[i]; | |
| 554 } else if (ipconfigs[i].type == chromeos::IPCONFIG_TYPE_IPV4) { | |
| 555 ipconfig_static = &ipconfigs[i]; | |
| 556 ipconfig_static_path = ipconfig_paths[i]; | |
| 557 } | |
| 558 } | |
| 559 | |
| 560 NetworkIPConfigVector ipconfigs2; | |
| 561 std::vector<std::string> ipconfig_paths2; | |
| 562 if (ipconfig.type == chromeos::IPCONFIG_TYPE_DHCP) { | |
| 563 // If switching from static to DHCP, create new DHCP IPConfig. | |
| 564 if (!ipconfig_dhcp && | |
| 565 !CrosAddIPConfig(ipconfig.device_path, chromeos::IPCONFIG_TYPE_DHCP)) { | |
| 566 LOG(ERROR) << "Unable to add new DHCP IPConfig"; | |
| 567 return; | |
| 568 } | |
| 569 // User wants DHCP now. So delete the static IPConfig. | |
| 570 if (ipconfig_static) | |
| 571 CrosRemoveIPConfig(ipconfig_static_path); | |
| 572 } else if (ipconfig.type == chromeos::IPCONFIG_TYPE_IPV4) { | |
| 573 // If switching from DHCP to static, create new static IPConfig. | |
| 574 if (!ipconfig_static) { | |
| 575 if (!CrosAddIPConfig(ipconfig.device_path, | |
| 576 chromeos::IPCONFIG_TYPE_IPV4)) { | |
| 577 LOG(ERROR) << "Unable to add new static IPConfig"; | |
| 578 return; | |
| 579 } | |
| 580 // Now find the newly created IPConfig. | |
| 581 if (CrosListIPConfigs(ipconfig.device_path, &ipconfigs2, | |
| 582 &ipconfig_paths2, NULL)) { | |
| 583 for (size_t i = 0; i < ipconfigs2.size(); ++i) { | |
| 584 if (ipconfigs2[i].type == chromeos::IPCONFIG_TYPE_IPV4) { | |
| 585 ipconfig_static = &ipconfigs2[i]; | |
| 586 ipconfig_static_path = ipconfig_paths2[i]; | |
| 587 } | |
| 588 } | |
| 589 } | |
| 590 if (!ipconfig_static) { | |
| 591 LOG(ERROR) << "Unable to find newly added static IPConfig"; | |
| 592 return; | |
| 593 } | |
| 594 } | |
| 595 | |
| 596 // Save any changed details. | |
| 597 if (ipconfig.address != ipconfig_static->address) { | |
| 598 base::StringValue value(ipconfig.address); | |
| 599 CrosSetNetworkIPConfigProperty(ipconfig_static_path, | |
| 600 flimflam::kAddressProperty, value); | |
| 601 } | |
| 602 if (ipconfig.netmask != ipconfig_static->netmask) { | |
| 603 int prefixlen = ipconfig.GetPrefixLength(); | |
| 604 if (prefixlen == -1) { | |
| 605 VLOG(1) << "IPConfig prefix length is invalid for netmask " | |
| 606 << ipconfig.netmask; | |
| 607 } else { | |
| 608 base::FundamentalValue value(prefixlen); | |
| 609 CrosSetNetworkIPConfigProperty(ipconfig_static_path, | |
| 610 flimflam::kPrefixlenProperty, value); | |
| 611 } | |
| 612 } | |
| 613 if (ipconfig.gateway != ipconfig_static->gateway) { | |
| 614 base::StringValue value(ipconfig.gateway); | |
| 615 CrosSetNetworkIPConfigProperty(ipconfig_static_path, | |
| 616 flimflam::kGatewayProperty, value); | |
| 617 } | |
| 618 if (ipconfig.name_servers != ipconfig_static->name_servers) { | |
| 619 base::StringValue value(ipconfig.name_servers); | |
| 620 CrosSetNetworkIPConfigProperty(ipconfig_static_path, | |
| 621 flimflam::kNameServersProperty, value); | |
| 622 } | |
| 623 // Remove DHCP IPConfig if there is one. | |
| 624 if (ipconfig_dhcp) | |
| 625 CrosRemoveIPConfig(ipconfig_dhcp_path); | |
| 626 } | |
| 627 } | 582 } |
| 628 | 583 |
| 629 ///////////////////////////////////////////////////////////////////////////// | 584 ///////////////////////////////////////////////////////////////////////////// |
| 630 // Network Manager functions. | 585 // Network Manager functions. |
| 631 | 586 |
| 632 void NetworkLibraryImplCros::NetworkManagerStatusChangedHandler( | 587 void NetworkLibraryImplCros::NetworkManagerStatusChangedHandler( |
| 633 const std::string& path, | 588 const std::string& path, |
| 634 const std::string& key, | 589 const std::string& key, |
| 635 const Value& value) { | 590 const Value& value) { |
| 636 if (!NetworkManagerStatusChanged(key, &value)) { | 591 if (!NetworkManagerStatusChanged(key, &value)) { |
| (...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1211 device->data_roaming_allowed() != settings_value) { | 1166 device->data_roaming_allowed() != settings_value) { |
| 1212 // Switch back to signed settings value. | 1167 // Switch back to signed settings value. |
| 1213 SetCellularDataRoamingAllowed(settings_value); | 1168 SetCellularDataRoamingAllowed(settings_value); |
| 1214 } | 1169 } |
| 1215 } | 1170 } |
| 1216 } | 1171 } |
| 1217 NotifyNetworkManagerChanged(false); // Not forced. | 1172 NotifyNetworkManagerChanged(false); // Not forced. |
| 1218 AddNetworkDeviceObserver(device_path, network_device_observer_.get()); | 1173 AddNetworkDeviceObserver(device_path, network_device_observer_.get()); |
| 1219 } | 1174 } |
| 1220 | 1175 |
| 1176 void NetworkLibraryImplCros::SetIPParametersCallback( |
| 1177 const IPParameterInfo& info, |
| 1178 const std::string& service_path, |
| 1179 const base::DictionaryValue* properties) { |
| 1180 // Find the properties we're going to set, and minimize the DBus calls below |
| 1181 // by not clearing if it's already cleared, and not setting if it's already |
| 1182 // set to the same value. Also, don't reconnect at the end if nothing changed. |
| 1183 bool something_changed = false; |
| 1184 std::string current_address; |
| 1185 int32 current_prefixlen = -1; |
| 1186 std::string current_gateway; |
| 1187 std::string current_name_servers; |
| 1188 bool address_exists = properties->GetStringWithoutPathExpansion( |
| 1189 shill::kStaticIPAddressProperty, |
| 1190 ¤t_address); |
| 1191 VLOG_IF(2, address_exists) << shill::kStaticIPAddressProperty |
| 1192 << "=" << current_address; |
| 1193 bool prefixlen_exists = properties->GetIntegerWithoutPathExpansion( |
| 1194 shill::kStaticIPPrefixlenProperty, |
| 1195 ¤t_prefixlen); |
| 1196 VLOG_IF(2, prefixlen_exists) << shill::kStaticIPPrefixlenProperty |
| 1197 << "=" << current_prefixlen; |
| 1198 bool gateway_exists = properties->GetStringWithoutPathExpansion( |
| 1199 shill::kStaticIPGatewayProperty, |
| 1200 ¤t_gateway); |
| 1201 VLOG_IF(2, gateway_exists) << shill::kStaticIPGatewayProperty |
| 1202 << "=" << current_gateway; |
| 1203 bool name_servers_exist = properties->GetStringWithoutPathExpansion( |
| 1204 shill::kStaticIPNameServersProperty, |
| 1205 ¤t_name_servers); |
| 1206 VLOG_IF(2, name_servers_exist) << shill::kStaticIPNameServersProperty |
| 1207 << "=" << current_name_servers; |
| 1208 |
| 1209 if (info.dhcp_usage_mask & USE_DHCP_ADDRESS) { |
| 1210 if (address_exists) { |
| 1211 something_changed = true; |
| 1212 CrosClearNetworkServiceProperty(service_path, |
| 1213 shill::kStaticIPAddressProperty); |
| 1214 VLOG(2) << "Clearing " << shill::kStaticIPAddressProperty; |
| 1215 } |
| 1216 } else if (current_address != info.address) { |
| 1217 base::StringValue value(info.address); |
| 1218 VLOG(2) << "Setting " << shill::kStaticIPAddressProperty |
| 1219 << " to " << info.address; |
| 1220 something_changed = true; |
| 1221 CrosSetNetworkServiceProperty(service_path, |
| 1222 shill::kStaticIPAddressProperty, |
| 1223 value); |
| 1224 } |
| 1225 |
| 1226 if (info.dhcp_usage_mask & USE_DHCP_NETMASK) { |
| 1227 if (prefixlen_exists) { |
| 1228 something_changed = true; |
| 1229 CrosClearNetworkServiceProperty(service_path, |
| 1230 shill::kStaticIPPrefixlenProperty); |
| 1231 VLOG(2) << "Clearing " << shill::kStaticIPPrefixlenProperty; |
| 1232 } |
| 1233 } else { |
| 1234 int prefixlen = CrosNetmaskToPrefixLength(info.netmask); |
| 1235 if (prefixlen == -1) { |
| 1236 VLOG(1) << "IPConfig prefix length is invalid for netmask " |
| 1237 << info.netmask; |
| 1238 } else if (current_prefixlen != prefixlen) { |
| 1239 base::FundamentalValue value(prefixlen); |
| 1240 VLOG(2) << "Setting " << shill::kStaticIPPrefixlenProperty |
| 1241 << " to " << prefixlen; |
| 1242 something_changed = true; |
| 1243 CrosSetNetworkServiceProperty(service_path, |
| 1244 shill::kStaticIPPrefixlenProperty, |
| 1245 value); |
| 1246 } |
| 1247 } |
| 1248 |
| 1249 if (info.dhcp_usage_mask & USE_DHCP_GATEWAY) { |
| 1250 if (gateway_exists) { |
| 1251 something_changed = true; |
| 1252 CrosClearNetworkServiceProperty(service_path, |
| 1253 shill::kStaticIPGatewayProperty); |
| 1254 VLOG(2) << "Clearing " << shill::kStaticIPGatewayProperty; |
| 1255 } |
| 1256 } else if (current_gateway != info.gateway){ |
| 1257 base::StringValue value(info.gateway); |
| 1258 VLOG(2) << "Setting " << shill::kStaticIPGatewayProperty |
| 1259 << " to " << info.gateway; |
| 1260 something_changed = true; |
| 1261 CrosSetNetworkServiceProperty(service_path, |
| 1262 shill::kStaticIPGatewayProperty, |
| 1263 value); |
| 1264 } |
| 1265 |
| 1266 if (info.dhcp_usage_mask & USE_DHCP_NAME_SERVERS) { |
| 1267 if (name_servers_exist) { |
| 1268 something_changed = true; |
| 1269 CrosClearNetworkServiceProperty(service_path, |
| 1270 shill::kStaticIPNameServersProperty); |
| 1271 VLOG(2) << "Clearing " << shill::kStaticIPNameServersProperty; |
| 1272 } |
| 1273 } else if (current_name_servers != info.name_servers){ |
| 1274 base::StringValue value(info.name_servers); |
| 1275 VLOG(2) << "Setting " << shill::kStaticIPNameServersProperty |
| 1276 << " to " << info.name_servers; |
| 1277 something_changed = true; |
| 1278 CrosSetNetworkServiceProperty(service_path, |
| 1279 shill::kStaticIPNameServersProperty, |
| 1280 value); |
| 1281 } |
| 1282 |
| 1283 if (!something_changed) |
| 1284 return; |
| 1285 |
| 1286 // Find the network associated with this service path, and attempt to refresh |
| 1287 // its IP parameters, so that the changes to the service properties can take |
| 1288 // effect. |
| 1289 Network* network = FindNetworkByPath(service_path); |
| 1290 |
| 1291 if (network && network->connecting_or_connected()) |
| 1292 RefreshIPConfig(network); |
| 1293 } |
| 1294 |
| 1221 } // namespace chromeos | 1295 } // namespace chromeos |
| OLD | NEW |