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.
7 #include "base/basictypes.h"
9 #include "base/location.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/network/network_change_notifier_chromeos.h"
15 #include "chromeos/network/network_event_log.h"
16 #include "chromeos/network/network_state.h"
17 #include "chromeos/network/network_state_handler.h"
18 #include "net/dns/dns_config_service_posix.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h"
23 // DNS config services on Chrome OS are signalled by the network state handler
24 // rather than relying on watching files in /etc.
25 class NetworkChangeNotifierChromeos::DnsConfigService
26 : public net::internal::DnsConfigServicePosix
{
29 ~DnsConfigService() override
;
31 // net::internal::DnsConfigService() overrides.
32 bool StartWatching() override
;
34 virtual void OnNetworkChange();
37 NetworkChangeNotifierChromeos::DnsConfigService::DnsConfigService() {
40 NetworkChangeNotifierChromeos::DnsConfigService::~DnsConfigService() {
43 bool NetworkChangeNotifierChromeos::DnsConfigService::StartWatching() {
44 // DNS config changes are handled and notified by the network state handlers.
48 void NetworkChangeNotifierChromeos::DnsConfigService::OnNetworkChange() {
54 NetworkChangeNotifierChromeos::NetworkChangeNotifierChromeos()
55 : NetworkChangeNotifier(NetworkChangeCalculatorParamsChromeos()),
56 connection_type_(CONNECTION_NONE
),
58 NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
60 task_runner_(base::ThreadTaskRunnerHandle::Get()),
61 weak_ptr_factory_(this) {
62 poll_callback_
= base::Bind(&NetworkChangeNotifierChromeos::PollForState
,
63 weak_ptr_factory_
.GetWeakPtr());
66 NetworkChangeNotifierChromeos::~NetworkChangeNotifierChromeos() {
69 void NetworkChangeNotifierChromeos::Initialize() {
70 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
71 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE
);
73 dns_config_service_
.reset(new DnsConfigService());
74 dns_config_service_
->WatchConfig(
75 base::Bind(net::NetworkChangeNotifier::SetDnsConfig
));
80 void NetworkChangeNotifierChromeos::PollForState() {
81 // Update initial connection state.
82 DefaultNetworkChanged(
83 NetworkHandler::Get()->network_state_handler()->DefaultNetwork());
86 void NetworkChangeNotifierChromeos::Shutdown() {
87 dns_config_service_
.reset();
88 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
90 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
93 net::NetworkChangeNotifier::ConnectionType
94 NetworkChangeNotifierChromeos::GetCurrentConnectionType() const {
95 // Re-evaluate connection state if we are offline since there is little
96 // cost to doing so. Since we are in the context of a const method,
97 // this is done through a closure that holds a non-const reference to
98 // |this|, to allow PollForState() to modify our cached state.
99 // TODO(gauravsh): Figure out why we would have missed this notification.
100 if (connection_type_
== CONNECTION_NONE
)
101 task_runner_
->PostTask(FROM_HERE
, poll_callback_
);
102 return connection_type_
;
105 double NetworkChangeNotifierChromeos::GetCurrentMaxBandwidth() const {
106 return max_bandwidth_mbps_
;
109 void NetworkChangeNotifierChromeos::SuspendDone(
110 const base::TimeDelta
& sleep_duration
) {
111 // Force invalidation of network resources on resume.
112 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
116 void NetworkChangeNotifierChromeos::DefaultNetworkChanged(
117 const chromeos::NetworkState
* default_network
) {
118 bool connection_type_changed
= false;
119 bool ip_address_changed
= false;
120 bool dns_changed
= false;
121 bool max_bandwidth_changed
= false;
123 UpdateState(default_network
, &connection_type_changed
, &ip_address_changed
,
124 &dns_changed
, &max_bandwidth_changed
);
126 if (connection_type_changed
)
127 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
128 if (ip_address_changed
)
129 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
131 dns_config_service_
->OnNetworkChange();
132 if (max_bandwidth_changed
)
133 NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange(
134 max_bandwidth_mbps_
);
137 void NetworkChangeNotifierChromeos::UpdateState(
138 const chromeos::NetworkState
* default_network
,
139 bool* connection_type_changed
,
140 bool* ip_address_changed
,
142 bool* max_bandwidth_changed
) {
143 *connection_type_changed
= false;
144 *ip_address_changed
= false;
145 *dns_changed
= false;
146 *max_bandwidth_changed
= false;
148 if (!default_network
|| !default_network
->IsConnectedState()) {
149 // If we lost a default network, we must update our state and notify
150 // observers, otherwise we have nothing to do.
151 if (connection_type_
!= CONNECTION_NONE
) {
152 NET_LOG_EVENT("NCNDefaultNetworkLost", service_path_
);
153 *ip_address_changed
= true;
155 *connection_type_changed
= true;
156 *max_bandwidth_changed
= true;
157 connection_type_
= CONNECTION_NONE
;
158 max_bandwidth_mbps_
= GetMaxBandwidthForConnectionSubtype(SUBTYPE_NONE
);
159 service_path_
.clear();
161 dns_servers_
.clear();
166 // We do have a default network and it is connected.
167 net::NetworkChangeNotifier::ConnectionType new_connection_type
=
168 ConnectionTypeFromShill(default_network
->type(),
169 default_network
->network_technology());
170 if (new_connection_type
!= connection_type_
) {
172 "NCNDefaultConnectionTypeChanged",
173 base::StringPrintf("%s -> %s",
174 ConnectionTypeToString(connection_type_
),
175 ConnectionTypeToString(new_connection_type
)));
176 *connection_type_changed
= true;
178 if (default_network
->path() != service_path_
) {
180 "NCNDefaultNetworkServicePathChanged",
181 base::StringPrintf("%s -> %s",
182 service_path_
.c_str(),
183 default_network
->path().c_str()));
185 // If we had a default network service change, network resources
186 // must always be invalidated.
187 *ip_address_changed
= true;
190 if (default_network
->ip_address() != ip_address_
) {
191 // Is this a state update with an online->online transition?
192 bool stayed_online
= (!*connection_type_changed
&&
193 connection_type_
!= CONNECTION_NONE
);
195 bool is_suppressed
= true;
196 // Suppress IP address change signalling on online->online transitions
197 // when getting an IP address update for the first time.
198 if (!(stayed_online
&& ip_address_
.empty())) {
199 is_suppressed
= false;
200 *ip_address_changed
= true;
203 base::StringPrintf("%s%s",
204 "NCNDefaultIPAddressChanged",
205 is_suppressed
? " (Suppressed)" : "" ),
206 base::StringPrintf("%s -> %s",
208 default_network
->ip_address().c_str()));
210 if (default_network
->dns_servers() != dns_servers_
) {
212 "NCNDefaultDNSServerChanged",
214 "%s -> %s", base::JoinString(dns_servers_
, ",").c_str(),
215 base::JoinString(default_network
->dns_servers(), ",").c_str()));
219 connection_type_
= new_connection_type
;
220 service_path_
= default_network
->path();
221 ip_address_
= default_network
->ip_address();
222 dns_servers_
= default_network
->dns_servers();
223 double old_max_bandwidth
= max_bandwidth_mbps_
;
224 max_bandwidth_mbps_
=
225 GetMaxBandwidthForConnectionSubtype(GetConnectionSubtype(
226 default_network
->type(), default_network
->network_technology()));
227 if (max_bandwidth_mbps_
!= old_max_bandwidth
)
228 *max_bandwidth_changed
= true;
232 net::NetworkChangeNotifier::ConnectionType
233 NetworkChangeNotifierChromeos::ConnectionTypeFromShill(
234 const std::string
& type
, const std::string
& technology
) {
235 if (NetworkTypePattern::Ethernet().MatchesType(type
))
236 return CONNECTION_ETHERNET
;
237 else if (type
== shill::kTypeWifi
)
238 return CONNECTION_WIFI
;
239 else if (type
== shill::kTypeWimax
)
240 return CONNECTION_4G
;
241 else if (type
== shill::kTypeBluetooth
)
242 return CONNECTION_BLUETOOTH
;
244 if (type
!= shill::kTypeCellular
)
245 return CONNECTION_UNKNOWN
;
247 // For cellular types, mapping depends on the technology.
248 if (technology
== shill::kNetworkTechnologyEvdo
||
249 technology
== shill::kNetworkTechnologyGsm
||
250 technology
== shill::kNetworkTechnologyUmts
||
251 technology
== shill::kNetworkTechnologyHspa
) {
252 return CONNECTION_3G
;
253 } else if (technology
== shill::kNetworkTechnologyHspaPlus
||
254 technology
== shill::kNetworkTechnologyLte
||
255 technology
== shill::kNetworkTechnologyLteAdvanced
) {
256 return CONNECTION_4G
;
258 return CONNECTION_2G
; // Default cellular type is 2G.
263 net::NetworkChangeNotifier::ConnectionSubtype
264 NetworkChangeNotifierChromeos::GetConnectionSubtype(
265 const std::string
& type
,
266 const std::string
& technology
) {
267 if (type
!= shill::kTypeCellular
)
268 return SUBTYPE_UNKNOWN
;
270 if (technology
== shill::kNetworkTechnology1Xrtt
)
271 return SUBTYPE_1XRTT
;
272 if (technology
== shill::kNetworkTechnologyEvdo
)
273 return SUBTYPE_EVDO_REV_0
;
274 if (technology
== shill::kNetworkTechnologyGsm
)
276 if (technology
== shill::kNetworkTechnologyGprs
)
278 if (technology
== shill::kNetworkTechnologyEdge
)
280 if (technology
== shill::kNetworkTechnologyUmts
)
282 if (technology
== shill::kNetworkTechnologyHspa
)
284 if (technology
== shill::kNetworkTechnologyHspaPlus
)
285 return SUBTYPE_HSPAP
;
286 if (technology
== shill::kNetworkTechnologyLte
)
288 if (technology
== shill::kNetworkTechnologyLteAdvanced
)
289 return SUBTYPE_LTE_ADVANCED
;
291 return SUBTYPE_UNKNOWN
;
295 net::NetworkChangeNotifier::NetworkChangeCalculatorParams
296 NetworkChangeNotifierChromeos::NetworkChangeCalculatorParamsChromeos() {
297 NetworkChangeCalculatorParams params
;
298 // Delay values arrived at by simple experimentation and adjusted so as to
299 // produce a single signal when switching between network connections.
300 params
.ip_address_offline_delay_
= base::TimeDelta::FromMilliseconds(4000);
301 params
.ip_address_online_delay_
= base::TimeDelta::FromMilliseconds(1000);
302 params
.connection_type_offline_delay_
=
303 base::TimeDelta::FromMilliseconds(500);
304 params
.connection_type_online_delay_
= base::TimeDelta::FromMilliseconds(500);
308 } // namespace chromeos