network_change_notifier_chromeos.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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.
4
5#include <string>
6
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 "chromeos/network/shill_property_util.h"
17#include "net/base/network_change_notifier.h"
18#include "net/dns/dns_config_service_posix.h"
19#include "third_party/cros_system_api/dbus/service_constants.h"
20
21namespace chromeos {
22
23// DNS config services on Chrome OS are signalled by the network state handler
24// rather than relying on watching files in /etc.
25class NetworkChangeNotifierChromeos::DnsConfigService
26    : public net::internal::DnsConfigServicePosix {
27 public:
28  DnsConfigService();
29  virtual ~DnsConfigService();
30
31  // net::internal::DnsConfigService() overrides.
32  virtual bool StartWatching() OVERRIDE;
33
34  virtual void OnNetworkChange();
35};
36
37NetworkChangeNotifierChromeos::DnsConfigService::DnsConfigService() {
38}
39
40NetworkChangeNotifierChromeos::DnsConfigService::~DnsConfigService() {
41}
42
43bool NetworkChangeNotifierChromeos::DnsConfigService::StartWatching() {
44  // DNS config changes are handled and notified by the network state handlers.
45  return true;
46}
47
48void NetworkChangeNotifierChromeos::DnsConfigService::OnNetworkChange() {
49  InvalidateConfig();
50  InvalidateHosts();
51  ReadNow();
52}
53
54NetworkChangeNotifierChromeos::NetworkChangeNotifierChromeos()
55    : NetworkChangeNotifier(NetworkChangeCalculatorParamsChromeos()),
56      connection_type_(CONNECTION_NONE) {
57}
58
59NetworkChangeNotifierChromeos::~NetworkChangeNotifierChromeos() {
60}
61
62void NetworkChangeNotifierChromeos::Initialize() {
63  DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
64  NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE);
65
66  dns_config_service_.reset(new DnsConfigService());
67  dns_config_service_->WatchConfig(
68      base::Bind(net::NetworkChangeNotifier::SetDnsConfig));
69
70  // Update initial connection state.
71  DefaultNetworkChanged(
72      NetworkHandler::Get()->network_state_handler()->DefaultNetwork());
73}
74
75void NetworkChangeNotifierChromeos::Shutdown() {
76  dns_config_service_.reset();
77  NetworkHandler::Get()->network_state_handler()->RemoveObserver(
78      this, FROM_HERE);
79  DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
80}
81
82net::NetworkChangeNotifier::ConnectionType
83NetworkChangeNotifierChromeos::GetCurrentConnectionType() const {
84  return connection_type_;
85}
86
87void NetworkChangeNotifierChromeos::SystemResumed(
88    const base::TimeDelta& sleep_duration) {
89  // Force invalidation of network resources on resume.
90  NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
91}
92
93
94void NetworkChangeNotifierChromeos::DefaultNetworkChanged(
95    const chromeos::NetworkState* default_network) {
96  bool connection_type_changed = false;
97  bool ip_address_changed = false;
98  bool dns_changed = false;
99
100  UpdateState(default_network, &connection_type_changed,
101              &ip_address_changed, &dns_changed);
102
103  if (connection_type_changed)
104    NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
105  if (ip_address_changed)
106    NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
107  if (dns_changed)
108    dns_config_service_->OnNetworkChange();
109}
110
111void NetworkChangeNotifierChromeos::UpdateState(
112    const chromeos::NetworkState* default_network,
113    bool* connection_type_changed,
114    bool* ip_address_changed,
115    bool* dns_changed) {
116  *connection_type_changed = false;
117  *ip_address_changed = false;
118  *dns_changed = false;
119  if (!default_network || !default_network->IsConnectedState()) {
120    // If we lost a default network, we must update our state and notify
121    // observers, otherwise we have nothing to do. (Under normal circumstances,
122    // we should never get duplicate no default network notifications).
123    if (connection_type_ != CONNECTION_NONE) {
124      NET_LOG_EVENT("NCNDefaultNetworkLost", service_path_);
125      *ip_address_changed = true;
126      *dns_changed = true;
127      *connection_type_changed = true;
128      connection_type_ = CONNECTION_NONE;
129      service_path_.clear();
130      ip_address_.clear();
131      dns_servers_.clear();
132    }
133    return;
134  }
135
136  // We do have a default network and it is connected.
137  net::NetworkChangeNotifier::ConnectionType new_connection_type =
138      ConnectionTypeFromShill(default_network->type(),
139                              default_network->network_technology());
140  if (new_connection_type != connection_type_) {
141    NET_LOG_EVENT(
142        "NCNDefaultConnectionTypeChanged",
143        base::StringPrintf("%s -> %s",
144                           ConnectionTypeToString(connection_type_),
145                           ConnectionTypeToString(new_connection_type)));
146    *connection_type_changed = true;
147  }
148  if (default_network->path() != service_path_) {
149    NET_LOG_EVENT(
150        "NCNDefaultNetworkServicePathChanged",
151        base::StringPrintf("%s -> %s",
152                           service_path_.c_str(),
153                           default_network->path().c_str()));
154
155    // If we had a default network service change, network resources
156    // must always be invalidated.
157    *ip_address_changed = true;
158    *dns_changed = true;
159  }
160  if (default_network->ip_address() != ip_address_) {
161    // Is this a state update with an online->online transition?
162    bool stayed_online = (!*connection_type_changed &&
163                          connection_type_ != CONNECTION_NONE);
164
165    bool is_suppressed = true;
166    // Suppress IP address change signalling on online->online transitions
167    // when getting an IP address update for the first time.
168    if (!(stayed_online && ip_address_.empty())) {
169      is_suppressed = false;
170      *ip_address_changed = true;
171    }
172    NET_LOG_EVENT(
173        base::StringPrintf("%s%s",
174                           "NCNDefaultIPAddressChanged",
175                           is_suppressed ? " (Suppressed)" : "" ),
176        base::StringPrintf("%s -> %s",
177                           ip_address_.c_str(),
178                           default_network->ip_address().c_str()));
179  }
180  if (default_network->dns_servers() != dns_servers_) {
181    NET_LOG_EVENT(
182        "NCNDefaultDNSServerChanged",
183        base::StringPrintf(
184            "%s -> %s",
185            JoinString(dns_servers_, ",").c_str(),
186            JoinString(default_network->dns_servers(), ",").c_str()));
187    *dns_changed = true;
188  }
189
190  connection_type_ = new_connection_type;
191  service_path_ = default_network->path();
192  ip_address_ = default_network->ip_address();
193  dns_servers_ = default_network->dns_servers();
194}
195
196// static
197net::NetworkChangeNotifier::ConnectionType
198NetworkChangeNotifierChromeos::ConnectionTypeFromShill(
199    const std::string& type, const std::string& technology) {
200  if (NetworkTypePattern::Ethernet().MatchesType(type))
201    return CONNECTION_ETHERNET;
202  else if (type == shill::kTypeWifi)
203    return CONNECTION_WIFI;
204  else if (type == shill::kTypeWimax)
205    return CONNECTION_4G;
206
207  if (type != shill::kTypeCellular)
208    return CONNECTION_UNKNOWN;
209
210  // For cellular types, mapping depends on the technology.
211  if (technology == shill::kNetworkTechnologyEvdo ||
212      technology == shill::kNetworkTechnologyGsm ||
213      technology == shill::kNetworkTechnologyUmts ||
214      technology == shill::kNetworkTechnologyHspa) {
215    return CONNECTION_3G;
216  } else if (technology == shill::kNetworkTechnologyHspaPlus ||
217             technology == shill::kNetworkTechnologyLte ||
218             technology == shill::kNetworkTechnologyLteAdvanced) {
219    return CONNECTION_4G;
220  } else {
221    return CONNECTION_2G;  // Default cellular type is 2G.
222  }
223}
224
225// static
226net::NetworkChangeNotifier::NetworkChangeCalculatorParams
227NetworkChangeNotifierChromeos::NetworkChangeCalculatorParamsChromeos() {
228  NetworkChangeCalculatorParams params;
229  // Delay values arrived at by simple experimentation and adjusted so as to
230  // produce a single signal when switching between network connections.
231  params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(4000);
232  params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(1000);
233  params.connection_type_offline_delay_ =
234      base::TimeDelta::FromMilliseconds(500);
235  params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500);
236  return params;
237}
238
239}  // namespace chromeos
240