[MD settings] moving attached() code
[chromium-blink-merge.git] / chromeos / network / network_change_notifier_chromeos.cc
bloba232997a4d4d4b83b05b2a64097bc168cc5c5fb2
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.
5 #include <string>
7 #include "base/basictypes.h"
8 #include "base/bind.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"
21 namespace chromeos {
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 {
27 public:
28 DnsConfigService();
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.
45 return true;
48 void NetworkChangeNotifierChromeos::DnsConfigService::OnNetworkChange() {
49 InvalidateConfig();
50 InvalidateHosts();
51 ReadNow();
54 NetworkChangeNotifierChromeos::NetworkChangeNotifierChromeos()
55 : NetworkChangeNotifier(NetworkChangeCalculatorParamsChromeos()),
56 connection_type_(CONNECTION_NONE),
57 max_bandwidth_mbps_(
58 NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
59 SUBTYPE_NONE)),
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));
77 PollForState();
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(
89 this, FROM_HERE);
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 void NetworkChangeNotifierChromeos::GetCurrentMaxBandwidthAndConnectionType(
106 double* max_bandwidth_mbps,
107 ConnectionType* connection_type) const {
108 *connection_type = connection_type_;
109 *max_bandwidth_mbps = max_bandwidth_mbps_;
112 void NetworkChangeNotifierChromeos::SuspendDone(
113 const base::TimeDelta& sleep_duration) {
114 // Force invalidation of network resources on resume.
115 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
119 void NetworkChangeNotifierChromeos::DefaultNetworkChanged(
120 const chromeos::NetworkState* default_network) {
121 bool connection_type_changed = false;
122 bool ip_address_changed = false;
123 bool dns_changed = false;
124 bool max_bandwidth_changed = false;
126 UpdateState(default_network, &connection_type_changed, &ip_address_changed,
127 &dns_changed, &max_bandwidth_changed);
129 if (connection_type_changed)
130 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
131 if (ip_address_changed)
132 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
133 if (dns_changed)
134 dns_config_service_->OnNetworkChange();
135 if (max_bandwidth_changed || connection_type_changed) {
136 NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange(
137 max_bandwidth_mbps_, connection_type_);
141 void NetworkChangeNotifierChromeos::UpdateState(
142 const chromeos::NetworkState* default_network,
143 bool* connection_type_changed,
144 bool* ip_address_changed,
145 bool* dns_changed,
146 bool* max_bandwidth_changed) {
147 *connection_type_changed = false;
148 *ip_address_changed = false;
149 *dns_changed = false;
150 *max_bandwidth_changed = false;
152 if (!default_network || !default_network->IsConnectedState()) {
153 // If we lost a default network, we must update our state and notify
154 // observers, otherwise we have nothing to do.
155 if (connection_type_ != CONNECTION_NONE) {
156 NET_LOG_EVENT("NCNDefaultNetworkLost", service_path_);
157 *ip_address_changed = true;
158 *dns_changed = true;
159 *connection_type_changed = true;
160 *max_bandwidth_changed = true;
161 connection_type_ = CONNECTION_NONE;
162 max_bandwidth_mbps_ = GetMaxBandwidthForConnectionSubtype(SUBTYPE_NONE);
163 service_path_.clear();
164 ip_address_.clear();
165 dns_servers_.clear();
167 return;
170 // We do have a default network and it is connected.
171 net::NetworkChangeNotifier::ConnectionType new_connection_type =
172 ConnectionTypeFromShill(default_network->type(),
173 default_network->network_technology());
174 if (new_connection_type != connection_type_) {
175 NET_LOG_EVENT(
176 "NCNDefaultConnectionTypeChanged",
177 base::StringPrintf("%s -> %s",
178 ConnectionTypeToString(connection_type_),
179 ConnectionTypeToString(new_connection_type)));
180 *connection_type_changed = true;
182 if (default_network->path() != service_path_) {
183 NET_LOG_EVENT(
184 "NCNDefaultNetworkServicePathChanged",
185 base::StringPrintf("%s -> %s",
186 service_path_.c_str(),
187 default_network->path().c_str()));
189 // If we had a default network service change, network resources
190 // must always be invalidated.
191 *ip_address_changed = true;
192 *dns_changed = true;
194 if (default_network->ip_address() != ip_address_) {
195 // Is this a state update with an online->online transition?
196 bool stayed_online = (!*connection_type_changed &&
197 connection_type_ != CONNECTION_NONE);
199 bool is_suppressed = true;
200 // Suppress IP address change signalling on online->online transitions
201 // when getting an IP address update for the first time.
202 if (!(stayed_online && ip_address_.empty())) {
203 is_suppressed = false;
204 *ip_address_changed = true;
206 NET_LOG_EVENT(
207 base::StringPrintf("%s%s",
208 "NCNDefaultIPAddressChanged",
209 is_suppressed ? " (Suppressed)" : "" ),
210 base::StringPrintf("%s -> %s",
211 ip_address_.c_str(),
212 default_network->ip_address().c_str()));
214 if (default_network->dns_servers() != dns_servers_) {
215 NET_LOG_EVENT(
216 "NCNDefaultDNSServerChanged",
217 base::StringPrintf(
218 "%s -> %s", base::JoinString(dns_servers_, ",").c_str(),
219 base::JoinString(default_network->dns_servers(), ",").c_str()));
220 *dns_changed = true;
223 connection_type_ = new_connection_type;
224 service_path_ = default_network->path();
225 ip_address_ = default_network->ip_address();
226 dns_servers_ = default_network->dns_servers();
227 double old_max_bandwidth = max_bandwidth_mbps_;
228 max_bandwidth_mbps_ =
229 GetMaxBandwidthForConnectionSubtype(GetConnectionSubtype(
230 default_network->type(), default_network->network_technology()));
231 if (max_bandwidth_mbps_ != old_max_bandwidth)
232 *max_bandwidth_changed = true;
235 // static
236 net::NetworkChangeNotifier::ConnectionType
237 NetworkChangeNotifierChromeos::ConnectionTypeFromShill(
238 const std::string& type, const std::string& technology) {
239 if (NetworkTypePattern::Ethernet().MatchesType(type))
240 return CONNECTION_ETHERNET;
241 else if (type == shill::kTypeWifi)
242 return CONNECTION_WIFI;
243 else if (type == shill::kTypeWimax)
244 return CONNECTION_4G;
245 else if (type == shill::kTypeBluetooth)
246 return CONNECTION_BLUETOOTH;
248 if (type != shill::kTypeCellular)
249 return CONNECTION_UNKNOWN;
251 // For cellular types, mapping depends on the technology.
252 if (technology == shill::kNetworkTechnologyEvdo ||
253 technology == shill::kNetworkTechnologyGsm ||
254 technology == shill::kNetworkTechnologyUmts ||
255 technology == shill::kNetworkTechnologyHspa) {
256 return CONNECTION_3G;
257 } else if (technology == shill::kNetworkTechnologyHspaPlus ||
258 technology == shill::kNetworkTechnologyLte ||
259 technology == shill::kNetworkTechnologyLteAdvanced) {
260 return CONNECTION_4G;
261 } else {
262 return CONNECTION_2G; // Default cellular type is 2G.
266 // static
267 net::NetworkChangeNotifier::ConnectionSubtype
268 NetworkChangeNotifierChromeos::GetConnectionSubtype(
269 const std::string& type,
270 const std::string& technology) {
271 if (type != shill::kTypeCellular)
272 return SUBTYPE_UNKNOWN;
274 if (technology == shill::kNetworkTechnology1Xrtt)
275 return SUBTYPE_1XRTT;
276 if (technology == shill::kNetworkTechnologyEvdo)
277 return SUBTYPE_EVDO_REV_0;
278 if (technology == shill::kNetworkTechnologyGsm)
279 return SUBTYPE_GSM;
280 if (technology == shill::kNetworkTechnologyGprs)
281 return SUBTYPE_GPRS;
282 if (technology == shill::kNetworkTechnologyEdge)
283 return SUBTYPE_EDGE;
284 if (technology == shill::kNetworkTechnologyUmts)
285 return SUBTYPE_UMTS;
286 if (technology == shill::kNetworkTechnologyHspa)
287 return SUBTYPE_HSPA;
288 if (technology == shill::kNetworkTechnologyHspaPlus)
289 return SUBTYPE_HSPAP;
290 if (technology == shill::kNetworkTechnologyLte)
291 return SUBTYPE_LTE;
292 if (technology == shill::kNetworkTechnologyLteAdvanced)
293 return SUBTYPE_LTE_ADVANCED;
295 return SUBTYPE_UNKNOWN;
298 // static
299 net::NetworkChangeNotifier::NetworkChangeCalculatorParams
300 NetworkChangeNotifierChromeos::NetworkChangeCalculatorParamsChromeos() {
301 NetworkChangeCalculatorParams params;
302 // Delay values arrived at by simple experimentation and adjusted so as to
303 // produce a single signal when switching between network connections.
304 params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(4000);
305 params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(1000);
306 params.connection_type_offline_delay_ =
307 base::TimeDelta::FromMilliseconds(500);
308 params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500);
309 return params;
312 } // namespace chromeos