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/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/network/network_change_notifier_chromeos.h"
13 #include "chromeos/network/network_event_log.h"
14 #include "chromeos/network/network_state.h"
15 #include "chromeos/network/network_state_handler.h"
16 #include "net/dns/dns_config_service_posix.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
21 // DNS config services on Chrome OS are signalled by the network state handler
22 // rather than relying on watching files in /etc.
23 class NetworkChangeNotifierChromeos::DnsConfigService
24 : public net::internal::DnsConfigServicePosix
{
27 ~DnsConfigService() override
;
29 // net::internal::DnsConfigService() overrides.
30 bool StartWatching() override
;
32 virtual void OnNetworkChange();
35 NetworkChangeNotifierChromeos::DnsConfigService::DnsConfigService() {
38 NetworkChangeNotifierChromeos::DnsConfigService::~DnsConfigService() {
41 bool NetworkChangeNotifierChromeos::DnsConfigService::StartWatching() {
42 // DNS config changes are handled and notified by the network state handlers.
46 void NetworkChangeNotifierChromeos::DnsConfigService::OnNetworkChange() {
52 NetworkChangeNotifierChromeos::NetworkChangeNotifierChromeos()
53 : NetworkChangeNotifier(NetworkChangeCalculatorParamsChromeos()),
54 connection_type_(CONNECTION_NONE
),
56 NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
58 message_loop_(base::MessageLoopProxy::current()),
59 weak_ptr_factory_(this) {
60 poll_callback_
= base::Bind(&NetworkChangeNotifierChromeos::PollForState
,
61 weak_ptr_factory_
.GetWeakPtr());
64 NetworkChangeNotifierChromeos::~NetworkChangeNotifierChromeos() {
67 void NetworkChangeNotifierChromeos::Initialize() {
68 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
69 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE
);
71 dns_config_service_
.reset(new DnsConfigService());
72 dns_config_service_
->WatchConfig(
73 base::Bind(net::NetworkChangeNotifier::SetDnsConfig
));
78 void NetworkChangeNotifierChromeos::PollForState() {
79 // Update initial connection state.
80 DefaultNetworkChanged(
81 NetworkHandler::Get()->network_state_handler()->DefaultNetwork());
84 void NetworkChangeNotifierChromeos::Shutdown() {
85 dns_config_service_
.reset();
86 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
88 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
91 net::NetworkChangeNotifier::ConnectionType
92 NetworkChangeNotifierChromeos::GetCurrentConnectionType() const {
93 // Re-evaluate connection state if we are offline since there is little
94 // cost to doing so. Since we are in the context of a const method,
95 // this is done through a closure that holds a non-const reference to
96 // |this|, to allow PollForState() to modify our cached state.
97 // TODO(gauravsh): Figure out why we would have missed this notification.
98 if (connection_type_
== CONNECTION_NONE
)
99 message_loop_
->PostTask(FROM_HERE
, poll_callback_
);
100 return connection_type_
;
103 double NetworkChangeNotifierChromeos::GetCurrentMaxBandwidth() const {
104 return max_bandwidth_mbps_
;
107 void NetworkChangeNotifierChromeos::SuspendDone(
108 const base::TimeDelta
& sleep_duration
) {
109 // Force invalidation of network resources on resume.
110 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
114 void NetworkChangeNotifierChromeos::DefaultNetworkChanged(
115 const chromeos::NetworkState
* default_network
) {
116 bool connection_type_changed
= false;
117 bool ip_address_changed
= false;
118 bool dns_changed
= false;
119 bool max_bandwidth_changed
= false;
121 UpdateState(default_network
, &connection_type_changed
, &ip_address_changed
,
122 &dns_changed
, &max_bandwidth_changed
);
124 if (connection_type_changed
)
125 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
126 if (ip_address_changed
)
127 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
129 dns_config_service_
->OnNetworkChange();
130 if (max_bandwidth_changed
)
131 NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange(
132 max_bandwidth_mbps_
);
135 void NetworkChangeNotifierChromeos::UpdateState(
136 const chromeos::NetworkState
* default_network
,
137 bool* connection_type_changed
,
138 bool* ip_address_changed
,
140 bool* max_bandwidth_changed
) {
141 *connection_type_changed
= false;
142 *ip_address_changed
= false;
143 *dns_changed
= false;
144 *max_bandwidth_changed
= false;
146 if (!default_network
|| !default_network
->IsConnectedState()) {
147 // If we lost a default network, we must update our state and notify
148 // observers, otherwise we have nothing to do.
149 if (connection_type_
!= CONNECTION_NONE
) {
150 NET_LOG_EVENT("NCNDefaultNetworkLost", service_path_
);
151 *ip_address_changed
= true;
153 *connection_type_changed
= true;
154 *max_bandwidth_changed
= true;
155 connection_type_
= CONNECTION_NONE
;
156 max_bandwidth_mbps_
= GetMaxBandwidthForConnectionSubtype(SUBTYPE_NONE
);
157 service_path_
.clear();
159 dns_servers_
.clear();
164 // We do have a default network and it is connected.
165 net::NetworkChangeNotifier::ConnectionType new_connection_type
=
166 ConnectionTypeFromShill(default_network
->type(),
167 default_network
->network_technology());
168 if (new_connection_type
!= connection_type_
) {
170 "NCNDefaultConnectionTypeChanged",
171 base::StringPrintf("%s -> %s",
172 ConnectionTypeToString(connection_type_
),
173 ConnectionTypeToString(new_connection_type
)));
174 *connection_type_changed
= true;
176 if (default_network
->path() != service_path_
) {
178 "NCNDefaultNetworkServicePathChanged",
179 base::StringPrintf("%s -> %s",
180 service_path_
.c_str(),
181 default_network
->path().c_str()));
183 // If we had a default network service change, network resources
184 // must always be invalidated.
185 *ip_address_changed
= true;
188 if (default_network
->ip_address() != ip_address_
) {
189 // Is this a state update with an online->online transition?
190 bool stayed_online
= (!*connection_type_changed
&&
191 connection_type_
!= CONNECTION_NONE
);
193 bool is_suppressed
= true;
194 // Suppress IP address change signalling on online->online transitions
195 // when getting an IP address update for the first time.
196 if (!(stayed_online
&& ip_address_
.empty())) {
197 is_suppressed
= false;
198 *ip_address_changed
= true;
201 base::StringPrintf("%s%s",
202 "NCNDefaultIPAddressChanged",
203 is_suppressed
? " (Suppressed)" : "" ),
204 base::StringPrintf("%s -> %s",
206 default_network
->ip_address().c_str()));
208 if (default_network
->dns_servers() != dns_servers_
) {
210 "NCNDefaultDNSServerChanged",
213 JoinString(dns_servers_
, ",").c_str(),
214 JoinString(default_network
->dns_servers(), ",").c_str()));
218 connection_type_
= new_connection_type
;
219 service_path_
= default_network
->path();
220 ip_address_
= default_network
->ip_address();
221 dns_servers_
= default_network
->dns_servers();
222 double old_max_bandwidth
= max_bandwidth_mbps_
;
223 max_bandwidth_mbps_
=
224 GetMaxBandwidthForConnectionSubtype(GetConnectionSubtype(
225 default_network
->type(), default_network
->network_technology()));
226 if (max_bandwidth_mbps_
!= old_max_bandwidth
)
227 *max_bandwidth_changed
= true;
231 net::NetworkChangeNotifier::ConnectionType
232 NetworkChangeNotifierChromeos::ConnectionTypeFromShill(
233 const std::string
& type
, const std::string
& technology
) {
234 if (NetworkTypePattern::Ethernet().MatchesType(type
))
235 return CONNECTION_ETHERNET
;
236 else if (type
== shill::kTypeWifi
)
237 return CONNECTION_WIFI
;
238 else if (type
== shill::kTypeWimax
)
239 return CONNECTION_4G
;
240 else if (type
== shill::kTypeBluetooth
)
241 return CONNECTION_BLUETOOTH
;
243 if (type
!= shill::kTypeCellular
)
244 return CONNECTION_UNKNOWN
;
246 // For cellular types, mapping depends on the technology.
247 if (technology
== shill::kNetworkTechnologyEvdo
||
248 technology
== shill::kNetworkTechnologyGsm
||
249 technology
== shill::kNetworkTechnologyUmts
||
250 technology
== shill::kNetworkTechnologyHspa
) {
251 return CONNECTION_3G
;
252 } else if (technology
== shill::kNetworkTechnologyHspaPlus
||
253 technology
== shill::kNetworkTechnologyLte
||
254 technology
== shill::kNetworkTechnologyLteAdvanced
) {
255 return CONNECTION_4G
;
257 return CONNECTION_2G
; // Default cellular type is 2G.
262 net::NetworkChangeNotifier::ConnectionSubtype
263 NetworkChangeNotifierChromeos::GetConnectionSubtype(
264 const std::string
& type
,
265 const std::string
& technology
) {
266 if (type
!= shill::kTypeCellular
)
267 return SUBTYPE_UNKNOWN
;
269 if (technology
== shill::kNetworkTechnology1Xrtt
)
270 return SUBTYPE_1XRTT
;
271 if (technology
== shill::kNetworkTechnologyEvdo
)
272 return SUBTYPE_EVDO_REV_0
;
273 if (technology
== shill::kNetworkTechnologyGsm
)
275 if (technology
== shill::kNetworkTechnologyGprs
)
277 if (technology
== shill::kNetworkTechnologyEdge
)
279 if (technology
== shill::kNetworkTechnologyUmts
)
281 if (technology
== shill::kNetworkTechnologyHspa
)
283 if (technology
== shill::kNetworkTechnologyHspaPlus
)
284 return SUBTYPE_HSPAP
;
285 if (technology
== shill::kNetworkTechnologyLte
)
287 if (technology
== shill::kNetworkTechnologyLteAdvanced
)
288 return SUBTYPE_LTE_ADVANCED
;
290 return SUBTYPE_UNKNOWN
;
294 net::NetworkChangeNotifier::NetworkChangeCalculatorParams
295 NetworkChangeNotifierChromeos::NetworkChangeCalculatorParamsChromeos() {
296 NetworkChangeCalculatorParams params
;
297 // Delay values arrived at by simple experimentation and adjusted so as to
298 // produce a single signal when switching between network connections.
299 params
.ip_address_offline_delay_
= base::TimeDelta::FromMilliseconds(4000);
300 params
.ip_address_online_delay_
= base::TimeDelta::FromMilliseconds(1000);
301 params
.connection_type_offline_delay_
=
302 base::TimeDelta::FromMilliseconds(500);
303 params
.connection_type_online_delay_
= base::TimeDelta::FromMilliseconds(500);
307 } // namespace chromeos