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

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

Issue 10827283: This updates the StaticIP configuration UI to match new mocks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup for review Created 8 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
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.
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 output.push_back((*hardware_address)[i]); 526 output.push_back((*hardware_address)[i]);
527 } 527 }
528 *hardware_address = output; 528 *hardware_address = output;
529 } 529 }
530 } else { 530 } else {
531 DCHECK_EQ(format, FORMAT_RAW_HEX); 531 DCHECK_EQ(format, FORMAT_RAW_HEX);
532 } 532 }
533 return ipconfig_vector; 533 return ipconfig_vector;
534 } 534 }
535 535
536 void NetworkLibraryImplCros::SetIPConfig(const NetworkIPConfig& ipconfig) { 536 void NetworkLibraryImplCros::SetIPParameters(const std::string& service_path,
537 if (ipconfig.device_path.empty()) 537 const std::string& address,
538 const std::string& netmask,
539 const std::string& gateway,
540 const std::string& name_servers,
541 int dhcp_usage_mask) {
542 if (service_path.empty())
538 return; 543 return;
539 544
540 VLOG(1) << "Setting IPConfig: " << ipconfig.ToString(); 545 VLOG(1) << "Setting IP parameters: "
546 << "address: " << address
547 << (dhcp_usage_mask & USE_DHCP_ADDRESS ?
548 " (ignored)" : " (in use)")
549 << "netmask: " << netmask
550 << (dhcp_usage_mask & USE_DHCP_NETMASK ?
551 " (ignored)" : " (in use)")
552 << "gateway: " << gateway
553 << (dhcp_usage_mask & USE_DHCP_GATEWAY ?
554 " (ignored)" : " (in use)")
555 << "name_servers: " << name_servers
556 << (dhcp_usage_mask & USE_DHCP_NAME_SERVERS ?
557 " (ignored)" : " (in use)");
541 558
542 NetworkIPConfig* ipconfig_dhcp = NULL; 559 // Have to pass these in a structure, since Bind only takes up to six
543 std::string ipconfig_dhcp_path; 560 // parameters.
544 NetworkIPConfig* ipconfig_static = NULL; 561 IPParameterInfo info;
545 std::string ipconfig_static_path; 562 info.address = address;
563 info.netmask = netmask;
564 info.gateway = gateway;
565 info.name_servers = name_servers;
566 info.dhcp_usage_mask = dhcp_usage_mask;
567 chromeos::NetworkPropertiesCallback callback =
568 base::Bind(&NetworkLibraryImplCros::SetIPParametersCallback,
569 weak_ptr_factory_.GetWeakPtr(), info);
546 570
547 NetworkIPConfigVector ipconfigs; 571 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 } 572 }
628 573
629 ///////////////////////////////////////////////////////////////////////////// 574 /////////////////////////////////////////////////////////////////////////////
630 // Network Manager functions. 575 // Network Manager functions.
631 576
632 void NetworkLibraryImplCros::NetworkManagerStatusChangedHandler( 577 void NetworkLibraryImplCros::NetworkManagerStatusChangedHandler(
633 const std::string& path, 578 const std::string& path,
634 const std::string& key, 579 const std::string& key,
635 const Value& value) { 580 const Value& value) {
636 if (!NetworkManagerStatusChanged(key, &value)) { 581 if (!NetworkManagerStatusChanged(key, &value)) {
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after
1211 device->data_roaming_allowed() != settings_value) { 1156 device->data_roaming_allowed() != settings_value) {
1212 // Switch back to signed settings value. 1157 // Switch back to signed settings value.
1213 SetCellularDataRoamingAllowed(settings_value); 1158 SetCellularDataRoamingAllowed(settings_value);
1214 } 1159 }
1215 } 1160 }
1216 } 1161 }
1217 NotifyNetworkManagerChanged(false); // Not forced. 1162 NotifyNetworkManagerChanged(false); // Not forced.
1218 AddNetworkDeviceObserver(device_path, network_device_observer_.get()); 1163 AddNetworkDeviceObserver(device_path, network_device_observer_.get());
1219 } 1164 }
1220 1165
1166 void NetworkLibraryImplCros::SetIPParametersCallback(
1167 const IPParameterInfo& info,
1168 const std::string& service_path,
1169 const base::DictionaryValue* properties) {
1170 // Find the properties we're going to set, and minimize the DBus calls below
1171 // by not clearing if it's already cleared, and not setting if it's already
1172 // set to the same value. Also, don't reconnect at the end if nothing changed.
1173 bool something_changed = false;
1174 std::string current_address;
1175 int32 current_prefixlen = -1;
1176 std::string current_gateway;
1177 std::string current_name_servers;
1178 bool address_exists = properties->GetStringWithoutPathExpansion(
1179 shill::kStaticIPAddressProperty,
1180 &current_address);
1181 if (address_exists)
1182 VLOG(2) << shill::kStaticIPAddressProperty << "=" << current_address;
stevenjb 2012/08/13 17:43:44 I think these could all be VLOG_IF(2, foo_exists)
Greg Spencer (Chromium) 2012/08/13 23:53:44 Done.
1183 bool prefixlen_exists = properties->GetIntegerWithoutPathExpansion(
1184 shill::kStaticIPPrefixlenProperty,
1185 &current_prefixlen);
1186 if (prefixlen_exists)
1187 VLOG(2) << shill::kStaticIPPrefixlenProperty << "=" << current_prefixlen;
1188 bool gateway_exists = properties->GetStringWithoutPathExpansion(
1189 shill::kStaticIPGatewayProperty,
1190 &current_gateway);
1191 if (gateway_exists)
1192 VLOG(2) << shill::kStaticIPGatewayProperty << "=" << current_gateway;
1193 bool name_servers_exist = properties->GetStringWithoutPathExpansion(
1194 shill::kStaticIPNameServersProperty,
1195 &current_name_servers);
1196 if (name_servers_exist)
1197 VLOG(2) << shill::kStaticIPNameServersProperty
1198 << "=" << current_name_servers;
stevenjb 2012/08/13 17:43:44 nit: align << with << here and elsewhere.
Greg Spencer (Chromium) 2012/08/13 23:53:44 I blame Eclipse. :-) Done.
1199
1200 if (info.dhcp_usage_mask & USE_DHCP_ADDRESS) {
1201 if (address_exists) {
1202 something_changed = true;
1203 CrosClearNetworkServiceProperty(service_path,
1204 shill::kStaticIPAddressProperty);
1205 VLOG(2) << "Clearing " << shill::kStaticIPAddressProperty;
1206 }
1207 } else if (current_address != info.address) {
1208 base::StringValue value(info.address);
1209 VLOG(2) << "Setting " << shill::kStaticIPAddressProperty
1210 << " to " << info.address;
1211 something_changed = true;
1212 CrosSetNetworkServiceProperty(service_path,
1213 shill::kStaticIPAddressProperty,
1214 value);
1215 }
1216
1217 if (info.dhcp_usage_mask & USE_DHCP_NETMASK) {
1218 if (prefixlen_exists) {
1219 something_changed = true;
1220 CrosClearNetworkServiceProperty(service_path,
1221 shill::kStaticIPPrefixlenProperty);
1222 VLOG(2) << "Clearing " << shill::kStaticIPPrefixlenProperty;
1223 }
1224 } else {
1225 int prefixlen = CrosNetmaskToPrefixLength(info.netmask);
1226 if (prefixlen == -1) {
1227 VLOG(1) << "IPConfig prefix length is invalid for netmask "
1228 << info.netmask;
1229 } else if (current_prefixlen != prefixlen) {
1230 base::FundamentalValue value(prefixlen);
1231 VLOG(2) << "Setting " << shill::kStaticIPPrefixlenProperty
1232 << " to " << prefixlen;
1233 something_changed = true;
1234 CrosSetNetworkServiceProperty(service_path,
1235 shill::kStaticIPPrefixlenProperty,
1236 value);
1237 }
1238 }
1239
1240 if (info.dhcp_usage_mask & USE_DHCP_GATEWAY) {
1241 if (gateway_exists) {
1242 something_changed = true;
1243 CrosClearNetworkServiceProperty(service_path,
1244 shill::kStaticIPGatewayProperty);
1245 VLOG(2) << "Clearing " << shill::kStaticIPGatewayProperty;
1246 }
1247 } else if (current_gateway != info.gateway){
1248 base::StringValue value(info.gateway);
1249 VLOG(2) << "Setting " << shill::kStaticIPGatewayProperty
1250 << " to " << info.gateway;
1251 something_changed = true;
1252 CrosSetNetworkServiceProperty(service_path,
1253 shill::kStaticIPGatewayProperty,
1254 value);
1255 }
1256
1257 if (info.dhcp_usage_mask & USE_DHCP_NAME_SERVERS) {
1258 if (name_servers_exist) {
1259 something_changed = true;
1260 CrosClearNetworkServiceProperty(service_path,
1261 shill::kStaticIPNameServersProperty);
1262 VLOG(2) << "Clearing " << shill::kStaticIPNameServersProperty;
1263 }
1264 } else if (current_name_servers != info.name_servers){
1265 base::StringValue value(info.name_servers);
1266 VLOG(2) << "Setting " << shill::kStaticIPNameServersProperty
1267 << " to " << info.name_servers;
1268 something_changed = true;
1269 CrosSetNetworkServiceProperty(service_path,
1270 shill::kStaticIPNameServersProperty,
1271 value);
1272 }
1273
stevenjb 2012/08/13 17:43:44 nit: 'if (!something_changed) return;' early exit
Greg Spencer (Chromium) 2012/08/13 23:53:44 Done.
1274 // Find the network associated with this service path, and attempt to refresh
1275 // its IP parameters, so that the changes to the service properties can take
1276 // effect.
1277 Network* network = FindNetworkByPath(service_path);
1278
1279 if (something_changed && network && network->connecting_or_connected())
1280 RefreshIPConfig(network);
1281 }
stevenjb 2012/08/13 17:43:44 nit: blank line
Greg Spencer (Chromium) 2012/08/13 23:53:44 Done.
1221 } // namespace chromeos 1282 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698