Commit f7f16577 authored by Olivier Robin's avatar Olivier Robin Committed by Commit Bot

Report cell connection type

This CL adds a breakdown of the connection type as 2G/3G/4G.
Previously, only 3G was reported.

Change-Id: I4e80eb6d568e2ff56450ebd979e5461f5f4a035c
Bug: 1128867
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2412310
Commit-Queue: Olivier Robin <olivierrobin@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Reviewed-by: default avatarPaul Jensen <pauljensen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808423}
parent 27ad5b7b
...@@ -25,6 +25,7 @@ config("cronet_include_config") { ...@@ -25,6 +25,7 @@ config("cronet_include_config") {
config("cronet_static_config") { config("cronet_static_config") {
frameworks = [ frameworks = [
"Cronet.framework", "Cronet.framework",
"CoreTelephony.framework",
"UIKit.framework", "UIKit.framework",
"CFNetwork.framework", "CFNetwork.framework",
"MobileCoreServices.framework", "MobileCoreServices.framework",
......
...@@ -1288,8 +1288,8 @@ component("net") { ...@@ -1288,8 +1288,8 @@ component("net") {
sources += [ sources += [
"base/mac/url_conversions.h", "base/mac/url_conversions.h",
"base/mac/url_conversions.mm", "base/mac/url_conversions.mm",
"base/network_change_notifier_mac.cc",
"base/network_change_notifier_mac.h", "base/network_change_notifier_mac.h",
"base/network_change_notifier_mac.mm",
"base/network_config_watcher_mac.cc", "base/network_config_watcher_mac.cc",
"base/network_config_watcher_mac.h", "base/network_config_watcher_mac.h",
"base/platform_mime_util_mac.mm", "base/platform_mime_util_mac.mm",
...@@ -1510,6 +1510,7 @@ component("net") { ...@@ -1510,6 +1510,7 @@ component("net") {
libs = [ "resolv" ] libs = [ "resolv" ]
frameworks = [ frameworks = [
"CFNetwork.framework", "CFNetwork.framework",
"CoreTelephony.framework",
"MobileCoreServices.framework", "MobileCoreServices.framework",
"Security.framework", "Security.framework",
"SystemConfiguration.framework", "SystemConfiguration.framework",
......
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
#include "base/task/task_traits.h" #include "base/task/task_traits.h"
#include "net/dns/dns_config_service.h" #include "net/dns/dns_config_service.h"
#if defined(OS_IOS)
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#endif
namespace net { namespace net {
static bool CalculateReachability(SCNetworkConnectionFlags flags) { static bool CalculateReachability(SCNetworkConnectionFlags flags) {
...@@ -43,9 +47,8 @@ NetworkChangeNotifierMac::~NetworkChangeNotifierMac() { ...@@ -43,9 +47,8 @@ NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {
// Now that StartReachabilityNotifications() has either run to completion or // Now that StartReachabilityNotifications() has either run to completion or
// never run at all, unschedule reachability_ if it was previously scheduled. // never run at all, unschedule reachability_ if it was previously scheduled.
if (reachability_.get() && run_loop_.get()) { if (reachability_.get() && run_loop_.get()) {
SCNetworkReachabilityUnscheduleFromRunLoop(reachability_.get(), SCNetworkReachabilityUnscheduleFromRunLoop(
run_loop_.get(), reachability_.get(), run_loop_.get(), kCFRunLoopCommonModes);
kCFRunLoopCommonModes);
} }
} }
...@@ -75,7 +78,7 @@ NetworkChangeNotifierMac::GetCurrentConnectionType() const { ...@@ -75,7 +78,7 @@ NetworkChangeNotifierMac::GetCurrentConnectionType() const {
return connection_type_; return connection_type_;
} }
void NetworkChangeNotifierMac::Forwarder::Init() { void NetworkChangeNotifierMac::Forwarder::Init() {
net_config_watcher_->SetInitialConnectionType(); net_config_watcher_->SetInitialConnectionType();
} }
...@@ -88,8 +91,68 @@ NetworkChangeNotifierMac::CalculateConnectionType( ...@@ -88,8 +91,68 @@ NetworkChangeNotifierMac::CalculateConnectionType(
return CONNECTION_NONE; return CONNECTION_NONE;
#if defined(OS_IOS) #if defined(OS_IOS)
return (flags & kSCNetworkReachabilityFlagsIsWWAN) ? CONNECTION_3G if (!(flags & kSCNetworkReachabilityFlagsIsWWAN)) {
: CONNECTION_WIFI; return CONNECTION_WIFI;
}
if (@available(iOS 12, *)) {
CTTelephonyNetworkInfo* info =
[[[CTTelephonyNetworkInfo alloc] init] autorelease];
NSDictionary<NSString*, NSString*>*
service_current_radio_access_technology =
[info serviceCurrentRadioAccessTechnology];
NSSet<NSString*>* technologies_2g = [NSSet
setWithObjects:CTRadioAccessTechnologyGPRS, CTRadioAccessTechnologyGPRS,
CTRadioAccessTechnologyCDMA1x, nil];
NSSet<NSString*>* technologies_3g =
[NSSet setWithObjects:CTRadioAccessTechnologyWCDMA,
CTRadioAccessTechnologyHSDPA,
CTRadioAccessTechnologyHSUPA,
CTRadioAccessTechnologyCDMAEVDORev0,
CTRadioAccessTechnologyCDMAEVDORevA,
CTRadioAccessTechnologyCDMAEVDORevB,
CTRadioAccessTechnologyeHRPD, nil];
NSSet<NSString*>* technologies_4g =
[NSSet setWithObjects:CTRadioAccessTechnologyLTE, nil];
int best_network = 0;
for (NSString* service in service_current_radio_access_technology) {
if (!service_current_radio_access_technology[service]) {
continue;
}
int current_network = 0;
NSString* network_type = service_current_radio_access_technology[service];
if ([technologies_2g containsObject:network_type]) {
current_network = 2;
} else if ([technologies_3g containsObject:network_type]) {
current_network = 3;
} else if ([technologies_4g containsObject:network_type]) {
current_network = 4;
} else {
// New technology?
NOTREACHED();
return CONNECTION_UNKNOWN;
}
if (current_network > best_network) {
// iOS is supposed to use the best network available.
best_network = current_network;
}
}
switch (best_network) {
case 2:
return CONNECTION_2G;
case 3:
return CONNECTION_3G;
case 4:
return CONNECTION_4G;
default:
// Default to CONNECTION_3G to not change existing behavior.
return CONNECTION_3G;
}
} else {
return CONNECTION_3G;
}
#else #else
return ConnectionTypeFromInterfaces(); return ConnectionTypeFromInterfaces();
#endif #endif
...@@ -100,12 +163,12 @@ void NetworkChangeNotifierMac::Forwarder::StartReachabilityNotifications() { ...@@ -100,12 +163,12 @@ void NetworkChangeNotifierMac::Forwarder::StartReachabilityNotifications() {
} }
void NetworkChangeNotifierMac::Forwarder::SetDynamicStoreNotificationKeys( void NetworkChangeNotifierMac::Forwarder::SetDynamicStoreNotificationKeys(
SCDynamicStoreRef store) { SCDynamicStoreRef store) {
net_config_watcher_->SetDynamicStoreNotificationKeys(store); net_config_watcher_->SetDynamicStoreNotificationKeys(store);
} }
void NetworkChangeNotifierMac::Forwarder::OnNetworkConfigChange( void NetworkChangeNotifierMac::Forwarder::OnNetworkConfigChange(
CFArrayRef changed_keys) { CFArrayRef changed_keys) {
net_config_watcher_->OnNetworkConfigChange(changed_keys); net_config_watcher_->OnNetworkConfigChange(changed_keys);
} }
...@@ -147,20 +210,18 @@ void NetworkChangeNotifierMac::StartReachabilityNotifications() { ...@@ -147,20 +210,18 @@ void NetworkChangeNotifierMac::StartReachabilityNotifications() {
DCHECK(reachability_); DCHECK(reachability_);
SCNetworkReachabilityContext reachability_context = { SCNetworkReachabilityContext reachability_context = {
0, // version 0, // version
this, // user data this, // user data
NULL, // retain NULL, // retain
NULL, // release NULL, // release
NULL // description NULL // description
}; };
if (!SCNetworkReachabilitySetCallback( if (!SCNetworkReachabilitySetCallback(
reachability_, reachability_, &NetworkChangeNotifierMac::ReachabilityCallback,
&NetworkChangeNotifierMac::ReachabilityCallback,
&reachability_context)) { &reachability_context)) {
LOG(DFATAL) << "Could not set network reachability callback"; LOG(DFATAL) << "Could not set network reachability callback";
reachability_.reset(); reachability_.reset();
} else if (!SCNetworkReachabilityScheduleWithRunLoop(reachability_, } else if (!SCNetworkReachabilityScheduleWithRunLoop(reachability_, run_loop_,
run_loop_,
kCFRunLoopCommonModes)) { kCFRunLoopCommonModes)) {
LOG(DFATAL) << "Could not schedule network reachability on run loop"; LOG(DFATAL) << "Could not schedule network reachability on run loop";
reachability_.reset(); reachability_.reset();
...@@ -187,8 +248,8 @@ void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys( ...@@ -187,8 +248,8 @@ void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys(
CFArrayAppendValue(notification_keys.get(), key.get()); CFArrayAppendValue(notification_keys.get(), key.get());
// Set the notification keys. This starts us receiving notifications. // Set the notification keys. This starts us receiving notifications.
bool ret = SCDynamicStoreSetNotificationKeys( bool ret =
store, notification_keys.get(), NULL); SCDynamicStoreSetNotificationKeys(store, notification_keys.get(), NULL);
// TODO(willchan): Figure out a proper way to handle this rather than crash. // TODO(willchan): Figure out a proper way to handle this rather than crash.
CHECK(ret); CHECK(ret);
#endif // defined(OS_IOS) #endif // defined(OS_IOS)
...@@ -202,8 +263,8 @@ void NetworkChangeNotifierMac::OnNetworkConfigChange(CFArrayRef changed_keys) { ...@@ -202,8 +263,8 @@ void NetworkChangeNotifierMac::OnNetworkConfigChange(CFArrayRef changed_keys) {
DCHECK_EQ(run_loop_.get(), CFRunLoopGetCurrent()); DCHECK_EQ(run_loop_.get(), CFRunLoopGetCurrent());
for (CFIndex i = 0; i < CFArrayGetCount(changed_keys); ++i) { for (CFIndex i = 0; i < CFArrayGetCount(changed_keys); ++i) {
CFStringRef key = static_cast<CFStringRef>( CFStringRef key =
CFArrayGetValueAtIndex(changed_keys, i)); static_cast<CFStringRef>(CFArrayGetValueAtIndex(changed_keys, i));
if (CFStringHasSuffix(key, kSCEntNetIPv4) || if (CFStringHasSuffix(key, kSCEntNetIPv4) ||
CFStringHasSuffix(key, kSCEntNetIPv6)) { CFStringHasSuffix(key, kSCEntNetIPv6)) {
NotifyObserversOfIPAddressChange(); NotifyObserversOfIPAddressChange();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment