Fix style on iOS for distilled content.
[chromium-blink-merge.git] / chromeos / network / network_change_notifier_chromeos.cc
blob8be92f7b0014be306e82eb95598c34e038e1f784
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 message_loop_(base::MessageLoopProxy::current()),
56 weak_ptr_factory_(this) {
57 poll_callback_ = base::Bind(&NetworkChangeNotifierChromeos::PollForState,
58 weak_ptr_factory_.GetWeakPtr());
61 NetworkChangeNotifierChromeos::~NetworkChangeNotifierChromeos() {
64 void NetworkChangeNotifierChromeos::Initialize() {
65 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
66 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE);
68 dns_config_service_.reset(new DnsConfigService());
69 dns_config_service_->WatchConfig(
70 base::Bind(net::NetworkChangeNotifier::SetDnsConfig));
72 PollForState();
75 void NetworkChangeNotifierChromeos::PollForState() {
76 // Update initial connection state.
77 DefaultNetworkChanged(
78 NetworkHandler::Get()->network_state_handler()->DefaultNetwork());
81 void NetworkChangeNotifierChromeos::Shutdown() {
82 dns_config_service_.reset();
83 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
84 this, FROM_HERE);
85 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
88 net::NetworkChangeNotifier::ConnectionType
89 NetworkChangeNotifierChromeos::GetCurrentConnectionType() const {
90 // Re-evaluate connection state if we are offline since there is little
91 // cost to doing so. Since we are in the context of a const method,
92 // this is done through a closure that holds a non-const reference to
93 // |this|, to allow PollForState() to modify our cached state.
94 // TODO(gauravsh): Figure out why we would have missed this notification.
95 if (connection_type_ == CONNECTION_NONE)
96 message_loop_->PostTask(FROM_HERE, poll_callback_);
97 return connection_type_;
100 void NetworkChangeNotifierChromeos::SuspendDone(
101 const base::TimeDelta& sleep_duration) {
102 // Force invalidation of network resources on resume.
103 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
107 void NetworkChangeNotifierChromeos::DefaultNetworkChanged(
108 const chromeos::NetworkState* default_network) {
109 bool connection_type_changed = false;
110 bool ip_address_changed = false;
111 bool dns_changed = false;
113 UpdateState(default_network, &connection_type_changed,
114 &ip_address_changed, &dns_changed);
116 if (connection_type_changed)
117 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
118 if (ip_address_changed)
119 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
120 if (dns_changed)
121 dns_config_service_->OnNetworkChange();
124 void NetworkChangeNotifierChromeos::UpdateState(
125 const chromeos::NetworkState* default_network,
126 bool* connection_type_changed,
127 bool* ip_address_changed,
128 bool* dns_changed) {
129 *connection_type_changed = false;
130 *ip_address_changed = false;
131 *dns_changed = false;
132 if (!default_network || !default_network->IsConnectedState()) {
133 // If we lost a default network, we must update our state and notify
134 // observers, otherwise we have nothing to do.
135 if (connection_type_ != CONNECTION_NONE) {
136 NET_LOG_EVENT("NCNDefaultNetworkLost", service_path_);
137 *ip_address_changed = true;
138 *dns_changed = true;
139 *connection_type_changed = true;
140 connection_type_ = CONNECTION_NONE;
141 service_path_.clear();
142 ip_address_.clear();
143 dns_servers_.clear();
145 return;
148 // We do have a default network and it is connected.
149 net::NetworkChangeNotifier::ConnectionType new_connection_type =
150 ConnectionTypeFromShill(default_network->type(),
151 default_network->network_technology());
152 if (new_connection_type != connection_type_) {
153 NET_LOG_EVENT(
154 "NCNDefaultConnectionTypeChanged",
155 base::StringPrintf("%s -> %s",
156 ConnectionTypeToString(connection_type_),
157 ConnectionTypeToString(new_connection_type)));
158 *connection_type_changed = true;
160 if (default_network->path() != service_path_) {
161 NET_LOG_EVENT(
162 "NCNDefaultNetworkServicePathChanged",
163 base::StringPrintf("%s -> %s",
164 service_path_.c_str(),
165 default_network->path().c_str()));
167 // If we had a default network service change, network resources
168 // must always be invalidated.
169 *ip_address_changed = true;
170 *dns_changed = true;
172 if (default_network->ip_address() != ip_address_) {
173 // Is this a state update with an online->online transition?
174 bool stayed_online = (!*connection_type_changed &&
175 connection_type_ != CONNECTION_NONE);
177 bool is_suppressed = true;
178 // Suppress IP address change signalling on online->online transitions
179 // when getting an IP address update for the first time.
180 if (!(stayed_online && ip_address_.empty())) {
181 is_suppressed = false;
182 *ip_address_changed = true;
184 NET_LOG_EVENT(
185 base::StringPrintf("%s%s",
186 "NCNDefaultIPAddressChanged",
187 is_suppressed ? " (Suppressed)" : "" ),
188 base::StringPrintf("%s -> %s",
189 ip_address_.c_str(),
190 default_network->ip_address().c_str()));
192 if (default_network->dns_servers() != dns_servers_) {
193 NET_LOG_EVENT(
194 "NCNDefaultDNSServerChanged",
195 base::StringPrintf(
196 "%s -> %s",
197 JoinString(dns_servers_, ",").c_str(),
198 JoinString(default_network->dns_servers(), ",").c_str()));
199 *dns_changed = true;
202 connection_type_ = new_connection_type;
203 service_path_ = default_network->path();
204 ip_address_ = default_network->ip_address();
205 dns_servers_ = default_network->dns_servers();
208 // static
209 net::NetworkChangeNotifier::ConnectionType
210 NetworkChangeNotifierChromeos::ConnectionTypeFromShill(
211 const std::string& type, const std::string& technology) {
212 if (NetworkTypePattern::Ethernet().MatchesType(type))
213 return CONNECTION_ETHERNET;
214 else if (type == shill::kTypeWifi)
215 return CONNECTION_WIFI;
216 else if (type == shill::kTypeWimax)
217 return CONNECTION_4G;
218 else if (type == shill::kTypeBluetooth)
219 return CONNECTION_BLUETOOTH;
221 if (type != shill::kTypeCellular)
222 return CONNECTION_UNKNOWN;
224 // For cellular types, mapping depends on the technology.
225 if (technology == shill::kNetworkTechnologyEvdo ||
226 technology == shill::kNetworkTechnologyGsm ||
227 technology == shill::kNetworkTechnologyUmts ||
228 technology == shill::kNetworkTechnologyHspa) {
229 return CONNECTION_3G;
230 } else if (technology == shill::kNetworkTechnologyHspaPlus ||
231 technology == shill::kNetworkTechnologyLte ||
232 technology == shill::kNetworkTechnologyLteAdvanced) {
233 return CONNECTION_4G;
234 } else {
235 return CONNECTION_2G; // Default cellular type is 2G.
239 // static
240 net::NetworkChangeNotifier::NetworkChangeCalculatorParams
241 NetworkChangeNotifierChromeos::NetworkChangeCalculatorParamsChromeos() {
242 NetworkChangeCalculatorParams params;
243 // Delay values arrived at by simple experimentation and adjusted so as to
244 // produce a single signal when switching between network connections.
245 params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(4000);
246 params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(1000);
247 params.connection_type_offline_delay_ =
248 base::TimeDelta::FromMilliseconds(500);
249 params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500);
250 return params;
253 } // namespace chromeos