Roll src/third_party/skia 4cd6713:0edd965
[chromium-blink-merge.git] / chromeos / network / network_change_notifier_chromeos.cc
blobd901263e07274ad1f803208cf94926aa04528a78
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/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"
19 namespace chromeos {
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 {
25 public:
26 DnsConfigService();
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.
43 return true;
46 void NetworkChangeNotifierChromeos::DnsConfigService::OnNetworkChange() {
47 InvalidateConfig();
48 InvalidateHosts();
49 ReadNow();
52 NetworkChangeNotifierChromeos::NetworkChangeNotifierChromeos()
53 : NetworkChangeNotifier(NetworkChangeCalculatorParamsChromeos()),
54 connection_type_(CONNECTION_NONE),
55 max_bandwidth_mbps_(
56 NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
57 SUBTYPE_NONE)),
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));
75 PollForState();
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(
87 this, FROM_HERE);
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();
128 if (dns_changed)
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,
139 bool* dns_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;
152 *dns_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();
158 ip_address_.clear();
159 dns_servers_.clear();
161 return;
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_) {
169 NET_LOG_EVENT(
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_) {
177 NET_LOG_EVENT(
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;
186 *dns_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;
200 NET_LOG_EVENT(
201 base::StringPrintf("%s%s",
202 "NCNDefaultIPAddressChanged",
203 is_suppressed ? " (Suppressed)" : "" ),
204 base::StringPrintf("%s -> %s",
205 ip_address_.c_str(),
206 default_network->ip_address().c_str()));
208 if (default_network->dns_servers() != dns_servers_) {
209 NET_LOG_EVENT(
210 "NCNDefaultDNSServerChanged",
211 base::StringPrintf(
212 "%s -> %s",
213 JoinString(dns_servers_, ",").c_str(),
214 JoinString(default_network->dns_servers(), ",").c_str()));
215 *dns_changed = true;
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;
230 // static
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;
256 } else {
257 return CONNECTION_2G; // Default cellular type is 2G.
261 // static
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)
274 return SUBTYPE_GSM;
275 if (technology == shill::kNetworkTechnologyGprs)
276 return SUBTYPE_GPRS;
277 if (technology == shill::kNetworkTechnologyEdge)
278 return SUBTYPE_EDGE;
279 if (technology == shill::kNetworkTechnologyUmts)
280 return SUBTYPE_UMTS;
281 if (technology == shill::kNetworkTechnologyHspa)
282 return SUBTYPE_HSPA;
283 if (technology == shill::kNetworkTechnologyHspaPlus)
284 return SUBTYPE_HSPAP;
285 if (technology == shill::kNetworkTechnologyLte)
286 return SUBTYPE_LTE;
287 if (technology == shill::kNetworkTechnologyLteAdvanced)
288 return SUBTYPE_LTE_ADVANCED;
290 return SUBTYPE_UNKNOWN;
293 // static
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);
304 return params;
307 } // namespace chromeos