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 "chromeos/network/shill_property_util.h"
17 #include "net/base/network_change_notifier.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 virtual ~DnsConfigService();
31 // net::internal::DnsConfigService() overrides.
32 virtual 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
) {
59 NetworkChangeNotifierChromeos::~NetworkChangeNotifierChromeos() {
62 void NetworkChangeNotifierChromeos::Initialize() {
63 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
64 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE
);
66 dns_config_service_
.reset(new DnsConfigService());
67 dns_config_service_
->WatchConfig(
68 base::Bind(net::NetworkChangeNotifier::SetDnsConfig
));
70 // Update initial connection state.
71 DefaultNetworkChanged(
72 NetworkHandler::Get()->network_state_handler()->DefaultNetwork());
75 void NetworkChangeNotifierChromeos::Shutdown() {
76 dns_config_service_
.reset();
77 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
79 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
82 net::NetworkChangeNotifier::ConnectionType
83 NetworkChangeNotifierChromeos::GetCurrentConnectionType() const {
84 return connection_type_
;
87 void NetworkChangeNotifierChromeos::SuspendDone(
88 const base::TimeDelta
& sleep_duration
) {
89 // Force invalidation of network resources on resume.
90 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
94 void NetworkChangeNotifierChromeos::DefaultNetworkChanged(
95 const chromeos::NetworkState
* default_network
) {
96 bool connection_type_changed
= false;
97 bool ip_address_changed
= false;
98 bool dns_changed
= false;
100 UpdateState(default_network
, &connection_type_changed
,
101 &ip_address_changed
, &dns_changed
);
103 if (connection_type_changed
)
104 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
105 if (ip_address_changed
)
106 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
108 dns_config_service_
->OnNetworkChange();
111 void NetworkChangeNotifierChromeos::UpdateState(
112 const chromeos::NetworkState
* default_network
,
113 bool* connection_type_changed
,
114 bool* ip_address_changed
,
116 *connection_type_changed
= false;
117 *ip_address_changed
= false;
118 *dns_changed
= false;
119 if (!default_network
|| !default_network
->IsConnectedState()) {
120 // If we lost a default network, we must update our state and notify
121 // observers, otherwise we have nothing to do. (Under normal circumstances,
122 // we should never get duplicate no default network notifications).
123 if (connection_type_
!= CONNECTION_NONE
) {
124 NET_LOG_EVENT("NCNDefaultNetworkLost", service_path_
);
125 *ip_address_changed
= true;
127 *connection_type_changed
= true;
128 connection_type_
= CONNECTION_NONE
;
129 service_path_
.clear();
131 dns_servers_
.clear();
136 // We do have a default network and it is connected.
137 net::NetworkChangeNotifier::ConnectionType new_connection_type
=
138 ConnectionTypeFromShill(default_network
->type(),
139 default_network
->network_technology());
140 if (new_connection_type
!= connection_type_
) {
142 "NCNDefaultConnectionTypeChanged",
143 base::StringPrintf("%s -> %s",
144 ConnectionTypeToString(connection_type_
),
145 ConnectionTypeToString(new_connection_type
)));
146 *connection_type_changed
= true;
148 if (default_network
->path() != service_path_
) {
150 "NCNDefaultNetworkServicePathChanged",
151 base::StringPrintf("%s -> %s",
152 service_path_
.c_str(),
153 default_network
->path().c_str()));
155 // If we had a default network service change, network resources
156 // must always be invalidated.
157 *ip_address_changed
= true;
160 if (default_network
->ip_address() != ip_address_
) {
161 // Is this a state update with an online->online transition?
162 bool stayed_online
= (!*connection_type_changed
&&
163 connection_type_
!= CONNECTION_NONE
);
165 bool is_suppressed
= true;
166 // Suppress IP address change signalling on online->online transitions
167 // when getting an IP address update for the first time.
168 if (!(stayed_online
&& ip_address_
.empty())) {
169 is_suppressed
= false;
170 *ip_address_changed
= true;
173 base::StringPrintf("%s%s",
174 "NCNDefaultIPAddressChanged",
175 is_suppressed
? " (Suppressed)" : "" ),
176 base::StringPrintf("%s -> %s",
178 default_network
->ip_address().c_str()));
180 if (default_network
->dns_servers() != dns_servers_
) {
182 "NCNDefaultDNSServerChanged",
185 JoinString(dns_servers_
, ",").c_str(),
186 JoinString(default_network
->dns_servers(), ",").c_str()));
190 connection_type_
= new_connection_type
;
191 service_path_
= default_network
->path();
192 ip_address_
= default_network
->ip_address();
193 dns_servers_
= default_network
->dns_servers();
197 net::NetworkChangeNotifier::ConnectionType
198 NetworkChangeNotifierChromeos::ConnectionTypeFromShill(
199 const std::string
& type
, const std::string
& technology
) {
200 if (NetworkTypePattern::Ethernet().MatchesType(type
))
201 return CONNECTION_ETHERNET
;
202 else if (type
== shill::kTypeWifi
)
203 return CONNECTION_WIFI
;
204 else if (type
== shill::kTypeWimax
)
205 return CONNECTION_4G
;
207 if (type
!= shill::kTypeCellular
)
208 return CONNECTION_UNKNOWN
;
210 // For cellular types, mapping depends on the technology.
211 if (technology
== shill::kNetworkTechnologyEvdo
||
212 technology
== shill::kNetworkTechnologyGsm
||
213 technology
== shill::kNetworkTechnologyUmts
||
214 technology
== shill::kNetworkTechnologyHspa
) {
215 return CONNECTION_3G
;
216 } else if (technology
== shill::kNetworkTechnologyHspaPlus
||
217 technology
== shill::kNetworkTechnologyLte
||
218 technology
== shill::kNetworkTechnologyLteAdvanced
) {
219 return CONNECTION_4G
;
221 return CONNECTION_2G
; // Default cellular type is 2G.
226 net::NetworkChangeNotifier::NetworkChangeCalculatorParams
227 NetworkChangeNotifierChromeos::NetworkChangeCalculatorParamsChromeos() {
228 NetworkChangeCalculatorParams params
;
229 // Delay values arrived at by simple experimentation and adjusted so as to
230 // produce a single signal when switching between network connections.
231 params
.ip_address_offline_delay_
= base::TimeDelta::FromMilliseconds(4000);
232 params
.ip_address_online_delay_
= base::TimeDelta::FromMilliseconds(1000);
233 params
.connection_type_offline_delay_
=
234 base::TimeDelta::FromMilliseconds(500);
235 params
.connection_type_online_delay_
= base::TimeDelta::FromMilliseconds(500);
239 } // namespace chromeos