Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(718)

Side by Side Diff: net/base/network_change_notifier_mac.cc

Issue 2896203003: [NetworkChangeNotifier] Return more specific network connection types (ethernet/wifi) on OSX (Closed)
Patch Set: Fixes Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/network_change_notifier_mac.h" 5 #include "net/base/network_change_notifier_mac.h"
6 6
7 #include <netinet/in.h> 7 #include <netinet/in.h>
8 #include <resolv.h> 8 #include <resolv.h>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/task_runner_util.h"
12 #include "base/threading/thread.h" 13 #include "base/threading/thread.h"
13 #include "base/threading/thread_restrictions.h" 14 #include "base/threading/thread_restrictions.h"
14 #include "net/dns/dns_config_service.h" 15 #include "net/dns/dns_config_service.h"
15 16
16 namespace net { 17 namespace net {
17 18
18 static bool CalculateReachability(SCNetworkConnectionFlags flags) { 19 static bool CalculateReachability(SCNetworkConnectionFlags flags) {
19 bool reachable = flags & kSCNetworkFlagsReachable; 20 bool reachable = flags & kSCNetworkFlagsReachable;
20 bool connection_required = flags & kSCNetworkFlagsConnectionRequired; 21 bool connection_required = flags & kSCNetworkFlagsConnectionRequired;
21 return reachable && !connection_required; 22 return reachable && !connection_required;
22 } 23 }
23 24
24 NetworkChangeNotifier::ConnectionType CalculateConnectionType(
25 SCNetworkConnectionFlags flags) {
26 bool reachable = CalculateReachability(flags);
27 if (reachable) {
28 #if defined(OS_IOS)
29 return (flags & kSCNetworkReachabilityFlagsIsWWAN) ?
30 NetworkChangeNotifier::CONNECTION_3G :
31 NetworkChangeNotifier::CONNECTION_WIFI;
32 #else
33 // TODO(droger): Get something more detailed than CONNECTION_UNKNOWN.
34 // http://crbug.com/112937
35 return NetworkChangeNotifier::CONNECTION_UNKNOWN;
36 #endif // defined(OS_IOS)
37 } else {
38 return NetworkChangeNotifier::CONNECTION_NONE;
39 }
40 }
41
42 // Thread on which we can run DnsConfigService, which requires a TYPE_IO 25 // Thread on which we can run DnsConfigService, which requires a TYPE_IO
43 // message loop. 26 // message loop.
44 class NetworkChangeNotifierMac::DnsConfigServiceThread : public base::Thread { 27 class NetworkChangeNotifierMac::DnsConfigServiceThread : public base::Thread {
45 public: 28 public:
46 DnsConfigServiceThread() : base::Thread("DnsConfigService") {} 29 DnsConfigServiceThread() : base::Thread("DnsConfigService") {}
47 30
48 ~DnsConfigServiceThread() override { Stop(); } 31 ~DnsConfigServiceThread() override { Stop(); }
49 32
50 void Init() override { 33 void Init() override {
51 service_ = DnsConfigService::CreateSystemService(); 34 service_ = DnsConfigService::CreateSystemService();
52 service_->WatchConfig(base::Bind(&NetworkChangeNotifier::SetDnsConfig)); 35 service_->WatchConfig(base::Bind(&NetworkChangeNotifier::SetDnsConfig));
53 } 36 }
54 37
55 void CleanUp() override { service_.reset(); } 38 void CleanUp() override { service_.reset(); }
56 39
57 private: 40 private:
58 std::unique_ptr<DnsConfigService> service_; 41 std::unique_ptr<DnsConfigService> service_;
59 42
60 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread); 43 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread);
61 }; 44 };
62 45
63 NetworkChangeNotifierMac::NetworkChangeNotifierMac() 46 NetworkChangeNotifierMac::NetworkChangeNotifierMac()
64 : NetworkChangeNotifier(NetworkChangeCalculatorParamsMac()), 47 : NetworkChangeNotifier(NetworkChangeCalculatorParamsMac()),
65 connection_type_(CONNECTION_UNKNOWN), 48 connection_type_(CONNECTION_UNKNOWN),
66 connection_type_initialized_(false), 49 connection_type_initialized_(false),
67 initial_connection_type_cv_(&connection_type_lock_), 50 initial_connection_type_cv_(&connection_type_lock_),
68 forwarder_(this), 51 forwarder_(this),
69 dns_config_service_thread_(new DnsConfigServiceThread()) { 52 dns_config_service_thread_(new DnsConfigServiceThread()),
53 weak_ptr_factory_(this) {
70 // Must be initialized after the rest of this object, as it may call back into 54 // Must be initialized after the rest of this object, as it may call back into
71 // SetInitialConnectionType(). 55 // SetInitialConnectionType().
72 config_watcher_.reset(new NetworkConfigWatcherMac(&forwarder_)); 56 config_watcher_.reset(new NetworkConfigWatcherMac(&forwarder_));
73 dns_config_service_thread_->StartWithOptions( 57 dns_config_service_thread_->StartWithOptions(
74 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); 58 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
75 } 59 }
76 60
77 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() { 61 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {
78 // Delete the ConfigWatcher to join the notifier thread, ensuring that 62 // Delete the ConfigWatcher to join the notifier thread, ensuring that
79 // StartReachabilityNotifications() has an opportunity to run to completion. 63 // StartReachabilityNotifications() has an opportunity to run to completion.
(...skipping 15 matching lines...) Expand all
95 // Delay values arrived at by simple experimentation and adjusted so as to 79 // Delay values arrived at by simple experimentation and adjusted so as to
96 // produce a single signal when switching between network connections. 80 // produce a single signal when switching between network connections.
97 params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(500); 81 params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(500);
98 params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(500); 82 params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(500);
99 params.connection_type_offline_delay_ = 83 params.connection_type_offline_delay_ =
100 base::TimeDelta::FromMilliseconds(1000); 84 base::TimeDelta::FromMilliseconds(1000);
101 params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500); 85 params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500);
102 return params; 86 return params;
103 } 87 }
104 88
89 // static
90 NetworkChangeNotifier::ConnectionType
91 NetworkChangeNotifierMac::CalculateConnectionType(
92 SCNetworkConnectionFlags flags) {
93 // Called on notifier thread during forwarder initialization, otherwise on
94 // the dns thread.
95 bool reachable = CalculateReachability(flags);
96 if (reachable) {
97 #if defined(OS_IOS)
98 return (flags & kSCNetworkReachabilityFlagsIsWWAN)
99 ? NetworkChangeNotifier::CONNECTION_3G
100 : NetworkChangeNotifier::CONNECTION_WIFI;
101 #else
102 // TODO(droger): Get something more detailed than CONNECTION_UNKNOWN.
103 // http://crbug.com/112937
pauljensen 2017/05/23 15:10:30 I think we can remove this comment now. Can you a
jkarlin 2017/05/27 01:06:19 Done.
104 return ConnectionTypeFromInterfaces();
105 #endif // defined(OS_IOS)
106 } else {
107 return CONNECTION_NONE;
108 }
109 }
110
105 NetworkChangeNotifier::ConnectionType 111 NetworkChangeNotifier::ConnectionType
106 NetworkChangeNotifierMac::GetCurrentConnectionType() const { 112 NetworkChangeNotifierMac::GetCurrentConnectionType() const {
107 base::ThreadRestrictions::ScopedAllowWait allow_wait; 113 base::ThreadRestrictions::ScopedAllowWait allow_wait;
108 base::AutoLock lock(connection_type_lock_); 114 base::AutoLock lock(connection_type_lock_);
109 // Make sure the initial connection type is set before returning. 115 // Make sure the initial connection type is set before returning.
110 while (!connection_type_initialized_) { 116 while (!connection_type_initialized_) {
111 initial_connection_type_cv_.Wait(); 117 initial_connection_type_cv_.Wait();
112 } 118 }
113 return connection_type_; 119 return connection_type_;
114 } 120 }
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 #endif // defined(OS_IOS) 247 #endif // defined(OS_IOS)
242 } 248 }
243 249
244 // static 250 // static
245 void NetworkChangeNotifierMac::ReachabilityCallback( 251 void NetworkChangeNotifierMac::ReachabilityCallback(
246 SCNetworkReachabilityRef target, 252 SCNetworkReachabilityRef target,
247 SCNetworkConnectionFlags flags, 253 SCNetworkConnectionFlags flags,
248 void* notifier) { 254 void* notifier) {
249 NetworkChangeNotifierMac* notifier_mac = 255 NetworkChangeNotifierMac* notifier_mac =
250 static_cast<NetworkChangeNotifierMac*>(notifier); 256 static_cast<NetworkChangeNotifierMac*>(notifier);
251
pauljensen 2017/05/23 15:10:30 whitespace change?
jkarlin 2017/05/27 01:06:19 Done.
252 DCHECK_EQ(notifier_mac->run_loop_.get(), CFRunLoopGetCurrent()); 257 DCHECK_EQ(notifier_mac->run_loop_.get(), CFRunLoopGetCurrent());
253 258
254 ConnectionType new_type = CalculateConnectionType(flags); 259 base::PostTaskAndReplyWithResult(
260 notifier_mac->dns_config_service_thread_->message_loop()
261 ->task_runner()
pauljensen 2017/05/23 15:18:15 hmm so I think this may not be the thread we want
jkarlin 2017/05/27 01:06:19 After F2F, moved to notifier thread and enabled bl
262 .get(),
263 FROM_HERE,
264 base::Bind(&NetworkChangeNotifierMac::CalculateConnectionType, flags),
265 base::Bind(&NetworkChangeNotifierMac::ReachabilityCallbackImpl,
266 notifier_mac->weak_ptr_factory_.GetWeakPtr()));
267 }
268
269 void NetworkChangeNotifierMac::ReachabilityCallbackImpl(
270 ConnectionType new_type) {
255 ConnectionType old_type; 271 ConnectionType old_type;
256 { 272 {
257 base::AutoLock lock(notifier_mac->connection_type_lock_); 273 base::AutoLock lock(connection_type_lock_);
258 old_type = notifier_mac->connection_type_; 274 old_type = connection_type_;
259 notifier_mac->connection_type_ = new_type; 275 connection_type_ = new_type;
260 } 276 }
261 if (old_type != new_type) { 277 if (old_type != new_type) {
262 NotifyObserversOfConnectionTypeChange(); 278 NotifyObserversOfConnectionTypeChange();
263 double max_bandwidth_mbps = 279 double max_bandwidth_mbps = GetMaxBandwidthForConnectionSubtype(
264 NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype( 280 new_type == CONNECTION_NONE ? SUBTYPE_NONE : SUBTYPE_UNKNOWN);
265 new_type == CONNECTION_NONE ? SUBTYPE_NONE : SUBTYPE_UNKNOWN);
266 NotifyObserversOfMaxBandwidthChange(max_bandwidth_mbps, new_type); 281 NotifyObserversOfMaxBandwidthChange(max_bandwidth_mbps, new_type);
267 } 282 }
268 283
269 #if defined(OS_IOS) 284 #if defined(OS_IOS)
270 // On iOS, the SCDynamicStore API does not exist, and we use the reachability 285 // On iOS, the SCDynamicStore API does not exist, and we use the reachability
271 // API to detect IP address changes instead. 286 // API to detect IP address changes instead.
272 NotifyObserversOfIPAddressChange(); 287 NotifyObserversOfIPAddressChange();
273 #endif // defined(OS_IOS) 288 #endif // defined(OS_IOS)
274 } 289 }
275 290
276 } // namespace net 291 } // namespace net
OLDNEW
« net/base/network_change_notifier_mac.h ('K') | « net/base/network_change_notifier_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698