Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "chromeos/network/network_state.h" | |
| 10 #include "chromeos/network/network_state_handler.h" | |
| 11 #include "net/base/network_change_notifier.h" | |
| 12 #include "net/base/network_change_notifier_chromeos.h" | |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 14 | |
| 15 namespace net { | |
| 16 | |
| 17 NetworkChangeNotifierChromeos::NetworkChangeNotifierChromeos() | |
| 18 : NetworkChangeNotifier(NetworkChangeCalculatorParamsChromeos()), | |
| 19 connection_type_(CONNECTION_NONE) { | |
| 20 } | |
| 21 | |
| 22 NetworkChangeNotifierChromeos::~NetworkChangeNotifierChromeos() { | |
| 23 } | |
| 24 | |
| 25 void NetworkChangeNotifierChromeos::Initialize() { | |
| 26 chromeos::NetworkStateHandler::Get()->AddObserver(this); | |
| 27 | |
| 28 dns_config_service_.reset(new DnsConfigService()); | |
| 29 dns_config_service_->WatchConfig( | |
| 30 base::Bind(NetworkChangeNotifier::SetDnsConfig)); | |
| 31 | |
| 32 // Update initial connection state. | |
| 33 ActiveNetworkChanged(chromeos::NetworkStateHandler::Get()->ActiveNetwork()); | |
| 34 } | |
| 35 | |
| 36 void NetworkChangeNotifierChromeos::Shutdown() { | |
| 37 dns_config_service_.reset(); | |
| 38 if(chromeos::NetworkStateHandler::Get()) | |
| 39 chromeos::NetworkStateHandler::Get()->RemoveObserver(this); | |
| 40 } | |
| 41 | |
| 42 NetworkChangeNotifier::ConnectionType | |
| 43 NetworkChangeNotifierChromeos::GetCurrentConnectionType() const { | |
| 44 return connection_type_; | |
| 45 } | |
| 46 | |
| 47 void NetworkChangeNotifierChromeos::ActiveNetworkChanged( | |
| 48 const chromeos::NetworkState* active_network) { | |
| 49 bool connection_type_changed = false, ip_address_changed = false, | |
| 50 dns_changed = false; | |
|
stevenjb
2012/12/13 19:28:22
Use a separate line for each of these.
gauravsh
2012/12/15 00:42:45
Done.
| |
| 51 | |
| 52 UpdateActiveNetwork(active_network, &connection_type_changed, | |
| 53 &ip_address_changed, &dns_changed); | |
| 54 | |
| 55 if (connection_type_changed) | |
| 56 NetworkChangeNotifierChromeos:: NotifyObserversOfConnectionTypeChange(); | |
| 57 if (ip_address_changed) | |
| 58 NetworkChangeNotifierChromeos::NotifyObserversOfIPAddressChange(); | |
| 59 if (dns_changed) | |
| 60 dns_config_service_->OnNetworkChange(); | |
| 61 } | |
| 62 | |
| 63 void NetworkChangeNotifierChromeos::ActiveNetworkStateChanged( | |
| 64 const chromeos::NetworkState* active_network) { | |
| 65 ActiveNetworkChanged(active_network); | |
| 66 } | |
| 67 | |
| 68 NetworkChangeNotifierChromeos::DnsConfigService::DnsConfigService() | |
| 69 {} | |
|
stevenjb
2012/12/13 19:28:22
{ on line 68
gauravsh
2012/12/15 00:42:45
Done.
| |
| 70 | |
| 71 NetworkChangeNotifierChromeos::DnsConfigService::~DnsConfigService() | |
| 72 {} | |
|
stevenjb
2012/12/13 19:28:22
{ on line 71
gauravsh
2012/12/15 00:42:45
Done.
| |
| 73 | |
| 74 bool NetworkChangeNotifierChromeos::DnsConfigService::StartWatching() { | |
| 75 // DNS config changes are handled and notified by the network state handlers. | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 void NetworkChangeNotifierChromeos::DnsConfigService::OnNetworkChange() { | |
| 80 InvalidateConfig(); | |
| 81 InvalidateHosts(); | |
| 82 ReadNow(); | |
| 83 } | |
| 84 | |
| 85 void NetworkChangeNotifierChromeos::UpdateActiveNetwork( | |
| 86 const chromeos::NetworkState* active_network, | |
| 87 bool* connection_type_changed, | |
| 88 bool* ip_address_changed, | |
| 89 bool* dns_changed) { | |
| 90 *connection_type_changed = *ip_address_changed = *dns_changed = false; | |
|
stevenjb
2012/12/13 19:28:22
Use separate lines for each, here and below.
gauravsh
2012/12/15 00:42:45
Done.
| |
| 91 // TODO(gauravsh): DNS changes will be detected once ip config | |
| 92 // support is hooked into NetworkStateHandler. For now, | |
|
stevenjb
2012/12/13 19:28:22
nit: No extra WS indentation in comment
gauravsh
2012/12/15 00:42:45
Done.
| |
| 93 // we report a DNS change on changes to the active network (including | |
| 94 // loss). | |
| 95 if (!active_network || !active_network->IsConnectedState()) { | |
| 96 // If we lost an active network, we must update our state and notify | |
| 97 // observers, otherwise we have nothing do. (Under normal circumstances, | |
| 98 // we should never ever get duplicate no active network notifications). | |
| 99 if (connection_type_ != CONNECTION_NONE) { | |
| 100 *ip_address_changed = *dns_changed = *connection_type_changed = true; | |
| 101 connection_type_ = CONNECTION_NONE; | |
| 102 service_path_.clear(); | |
| 103 ip_address_.clear(); | |
| 104 } | |
| 105 } else { | |
| 106 // We do have an active network and it is connected. | |
| 107 NetworkChangeNotifier::ConnectionType new_connection_type = | |
| 108 ConnectionTypeFromShill(active_network->type(), | |
| 109 active_network->technology()); | |
| 110 if (new_connection_type != connection_type_) { | |
| 111 VLOG(1) << "Connection type changed from " << connection_type_ << " -> " | |
| 112 << new_connection_type; | |
| 113 *connection_type_changed = *dns_changed = true; | |
| 114 } | |
| 115 if (active_network->path() != service_path_ || | |
| 116 active_network->ip_address() != ip_address_) { | |
| 117 VLOG(1) << "Service path changed from " << service_path_ << " -> " | |
| 118 << active_network->path(); | |
| 119 VLOG(1) << "IP Address changed from " << ip_address_ << " -> " | |
| 120 << active_network->ip_address(); | |
| 121 *ip_address_changed = *dns_changed = true; | |
| 122 } | |
| 123 connection_type_ = new_connection_type; | |
| 124 service_path_ = active_network->path(); | |
| 125 ip_address_ = active_network->ip_address(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 // static | |
| 130 NetworkChangeNotifier::ConnectionType | |
| 131 NetworkChangeNotifierChromeos::ConnectionTypeFromShill( | |
| 132 const std::string& type, const std::string& technology) { | |
| 133 if (type == flimflam::kTypeEthernet) | |
| 134 return CONNECTION_ETHERNET; | |
| 135 else if (type == flimflam::kTypeWifi) | |
| 136 return CONNECTION_WIFI; | |
| 137 else if (type == flimflam::kTypeWimax) | |
| 138 return CONNECTION_4G; | |
| 139 | |
| 140 if (type != flimflam::kTypeCellular) | |
| 141 return CONNECTION_UNKNOWN; | |
| 142 | |
| 143 // For cellular types, mapping depends on the technology. | |
| 144 if (technology == flimflam::kNetworkTechnologyEvdo || | |
| 145 technology == flimflam::kNetworkTechnologyGsm || | |
| 146 technology == flimflam::kNetworkTechnologyUmts || | |
| 147 technology == flimflam::kNetworkTechnologyHspa) | |
| 148 return CONNECTION_3G; | |
| 149 else if (technology == flimflam::kNetworkTechnologyHspaPlus || | |
| 150 technology == flimflam::kNetworkTechnologyLte || | |
| 151 technology == flimflam::kNetworkTechnologyLteAdvanced) | |
| 152 return CONNECTION_4G; | |
| 153 else | |
| 154 return CONNECTION_2G; // Default cellular type is 2G. | |
|
stevenjb
2012/12/13 19:28:22
{} around each of these clauses for multi-line if(
gauravsh
2012/12/15 00:42:45
Done.
| |
| 155 } | |
| 156 | |
| 157 // static | |
| 158 net::NetworkChangeNotifier::NetworkChangeCalculatorParams | |
| 159 NetworkChangeNotifierChromeos::NetworkChangeCalculatorParamsChromeos() { | |
| 160 NetworkChangeCalculatorParams params; | |
| 161 // Delay values arrived at by simple experimentation and adjusted so as to | |
| 162 // produce a single signal when switching between network connections. | |
| 163 params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(4000); | |
| 164 params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(1000); | |
| 165 params.connection_type_offline_delay_ = | |
| 166 base::TimeDelta::FromMilliseconds(500); | |
| 167 params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500); | |
| 168 return params; | |
| 169 } | |
| 170 | |
| 171 } // namespace chromeos | |
| 172 | |
| OLD | NEW |