Update V8 to version 4.6.54.
[chromium-blink-merge.git] / chromeos / network / network_change_notifier_chromeos.cc
blob52ee616a184938c032d185b6cbc694b5e43e2b66
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 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();
130 if (dns_changed)
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,
141 bool* dns_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;
154 *dns_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();
160 ip_address_.clear();
161 dns_servers_.clear();
163 return;
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_) {
171 NET_LOG_EVENT(
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_) {
179 NET_LOG_EVENT(
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;
188 *dns_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;
202 NET_LOG_EVENT(
203 base::StringPrintf("%s%s",
204 "NCNDefaultIPAddressChanged",
205 is_suppressed ? " (Suppressed)" : "" ),
206 base::StringPrintf("%s -> %s",
207 ip_address_.c_str(),
208 default_network->ip_address().c_str()));
210 if (default_network->dns_servers() != dns_servers_) {
211 NET_LOG_EVENT(
212 "NCNDefaultDNSServerChanged",
213 base::StringPrintf(
214 "%s -> %s", base::JoinString(dns_servers_, ",").c_str(),
215 base::JoinString(default_network->dns_servers(), ",").c_str()));
216 *dns_changed = true;
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;
231 // static
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;
257 } else {
258 return CONNECTION_2G; // Default cellular type is 2G.
262 // static
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)
275 return SUBTYPE_GSM;
276 if (technology == shill::kNetworkTechnologyGprs)
277 return SUBTYPE_GPRS;
278 if (technology == shill::kNetworkTechnologyEdge)
279 return SUBTYPE_EDGE;
280 if (technology == shill::kNetworkTechnologyUmts)
281 return SUBTYPE_UMTS;
282 if (technology == shill::kNetworkTechnologyHspa)
283 return SUBTYPE_HSPA;
284 if (technology == shill::kNetworkTechnologyHspaPlus)
285 return SUBTYPE_HSPAP;
286 if (technology == shill::kNetworkTechnologyLte)
287 return SUBTYPE_LTE;
288 if (technology == shill::kNetworkTechnologyLteAdvanced)
289 return SUBTYPE_LTE_ADVANCED;
291 return SUBTYPE_UNKNOWN;
294 // static
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);
305 return params;
308 } // namespace chromeos