Don't preload rarely seen large images
[chromium-blink-merge.git] / chromeos / network / network_change_notifier_chromeos.cc
blob4561091d5214e172a6f53d28e8fac3a998696d6b
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",
215 JoinString(dns_servers_, ",").c_str(),
216 JoinString(default_network->dns_servers(), ",").c_str()));
217 *dns_changed = true;
220 connection_type_ = new_connection_type;
221 service_path_ = default_network->path();
222 ip_address_ = default_network->ip_address();
223 dns_servers_ = default_network->dns_servers();
224 double old_max_bandwidth = max_bandwidth_mbps_;
225 max_bandwidth_mbps_ =
226 GetMaxBandwidthForConnectionSubtype(GetConnectionSubtype(
227 default_network->type(), default_network->network_technology()));
228 if (max_bandwidth_mbps_ != old_max_bandwidth)
229 *max_bandwidth_changed = true;
232 // static
233 net::NetworkChangeNotifier::ConnectionType
234 NetworkChangeNotifierChromeos::ConnectionTypeFromShill(
235 const std::string& type, const std::string& technology) {
236 if (NetworkTypePattern::Ethernet().MatchesType(type))
237 return CONNECTION_ETHERNET;
238 else if (type == shill::kTypeWifi)
239 return CONNECTION_WIFI;
240 else if (type == shill::kTypeWimax)
241 return CONNECTION_4G;
242 else if (type == shill::kTypeBluetooth)
243 return CONNECTION_BLUETOOTH;
245 if (type != shill::kTypeCellular)
246 return CONNECTION_UNKNOWN;
248 // For cellular types, mapping depends on the technology.
249 if (technology == shill::kNetworkTechnologyEvdo ||
250 technology == shill::kNetworkTechnologyGsm ||
251 technology == shill::kNetworkTechnologyUmts ||
252 technology == shill::kNetworkTechnologyHspa) {
253 return CONNECTION_3G;
254 } else if (technology == shill::kNetworkTechnologyHspaPlus ||
255 technology == shill::kNetworkTechnologyLte ||
256 technology == shill::kNetworkTechnologyLteAdvanced) {
257 return CONNECTION_4G;
258 } else {
259 return CONNECTION_2G; // Default cellular type is 2G.
263 // static
264 net::NetworkChangeNotifier::ConnectionSubtype
265 NetworkChangeNotifierChromeos::GetConnectionSubtype(
266 const std::string& type,
267 const std::string& technology) {
268 if (type != shill::kTypeCellular)
269 return SUBTYPE_UNKNOWN;
271 if (technology == shill::kNetworkTechnology1Xrtt)
272 return SUBTYPE_1XRTT;
273 if (technology == shill::kNetworkTechnologyEvdo)
274 return SUBTYPE_EVDO_REV_0;
275 if (technology == shill::kNetworkTechnologyGsm)
276 return SUBTYPE_GSM;
277 if (technology == shill::kNetworkTechnologyGprs)
278 return SUBTYPE_GPRS;
279 if (technology == shill::kNetworkTechnologyEdge)
280 return SUBTYPE_EDGE;
281 if (technology == shill::kNetworkTechnologyUmts)
282 return SUBTYPE_UMTS;
283 if (technology == shill::kNetworkTechnologyHspa)
284 return SUBTYPE_HSPA;
285 if (technology == shill::kNetworkTechnologyHspaPlus)
286 return SUBTYPE_HSPAP;
287 if (technology == shill::kNetworkTechnologyLte)
288 return SUBTYPE_LTE;
289 if (technology == shill::kNetworkTechnologyLteAdvanced)
290 return SUBTYPE_LTE_ADVANCED;
292 return SUBTYPE_UNKNOWN;
295 // static
296 net::NetworkChangeNotifier::NetworkChangeCalculatorParams
297 NetworkChangeNotifierChromeos::NetworkChangeCalculatorParamsChromeos() {
298 NetworkChangeCalculatorParams params;
299 // Delay values arrived at by simple experimentation and adjusted so as to
300 // produce a single signal when switching between network connections.
301 params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(4000);
302 params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(1000);
303 params.connection_type_offline_delay_ =
304 base::TimeDelta::FromMilliseconds(500);
305 params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500);
306 return params;
309 } // namespace chromeos