Skip to content

Commit

Permalink
Report cell connection type
Browse files Browse the repository at this point in the history
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: Eugene But <eugenebut@chromium.org>
Reviewed-by: Paul Jensen <pauljensen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808423}
  • Loading branch information
robinolivier authored and Commit Bot committed Sep 18, 2020
1 parent 27ad5b7 commit f7f1657
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 22 deletions.
1 change: 1 addition & 0 deletions components/cronet/ios/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ config("cronet_include_config") {
config("cronet_static_config") {
frameworks = [
"Cronet.framework",
"CoreTelephony.framework",
"UIKit.framework",
"CFNetwork.framework",
"MobileCoreServices.framework",
Expand Down
3 changes: 2 additions & 1 deletion net/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1288,8 +1288,8 @@ component("net") {
sources += [
"base/mac/url_conversions.h",
"base/mac/url_conversions.mm",
"base/network_change_notifier_mac.cc",
"base/network_change_notifier_mac.h",
"base/network_change_notifier_mac.mm",
"base/network_config_watcher_mac.cc",
"base/network_config_watcher_mac.h",
"base/platform_mime_util_mac.mm",
Expand Down Expand Up @@ -1510,6 +1510,7 @@ component("net") {
libs = [ "resolv" ]
frameworks = [
"CFNetwork.framework",
"CoreTelephony.framework",
"MobileCoreServices.framework",
"Security.framework",
"SystemConfiguration.framework",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include "base/task/task_traits.h"
#include "net/dns/dns_config_service.h"

#if defined(OS_IOS)
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#endif

namespace net {

static bool CalculateReachability(SCNetworkConnectionFlags flags) {
Expand Down Expand Up @@ -43,9 +47,8 @@ NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {
// Now that StartReachabilityNotifications() has either run to completion or
// never run at all, unschedule reachability_ if it was previously scheduled.
if (reachability_.get() && run_loop_.get()) {
SCNetworkReachabilityUnscheduleFromRunLoop(reachability_.get(),
run_loop_.get(),
kCFRunLoopCommonModes);
SCNetworkReachabilityUnscheduleFromRunLoop(
reachability_.get(), run_loop_.get(), kCFRunLoopCommonModes);
}
}

Expand Down Expand Up @@ -75,7 +78,7 @@ NetworkChangeNotifierMac::GetCurrentConnectionType() const {
return connection_type_;
}

void NetworkChangeNotifierMac::Forwarder::Init() {
void NetworkChangeNotifierMac::Forwarder::Init() {
net_config_watcher_->SetInitialConnectionType();
}

Expand All @@ -88,8 +91,68 @@ NetworkChangeNotifierMac::CalculateConnectionType(
return CONNECTION_NONE;

#if defined(OS_IOS)
return (flags & kSCNetworkReachabilityFlagsIsWWAN) ? CONNECTION_3G
: CONNECTION_WIFI;
if (!(flags & kSCNetworkReachabilityFlagsIsWWAN)) {
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
return ConnectionTypeFromInterfaces();
#endif
Expand All @@ -100,12 +163,12 @@ void NetworkChangeNotifierMac::Forwarder::StartReachabilityNotifications() {
}

void NetworkChangeNotifierMac::Forwarder::SetDynamicStoreNotificationKeys(
SCDynamicStoreRef store) {
SCDynamicStoreRef store) {
net_config_watcher_->SetDynamicStoreNotificationKeys(store);
}

void NetworkChangeNotifierMac::Forwarder::OnNetworkConfigChange(
CFArrayRef changed_keys) {
CFArrayRef changed_keys) {
net_config_watcher_->OnNetworkConfigChange(changed_keys);
}

Expand Down Expand Up @@ -147,20 +210,18 @@ void NetworkChangeNotifierMac::StartReachabilityNotifications() {

DCHECK(reachability_);
SCNetworkReachabilityContext reachability_context = {
0, // version
this, // user data
NULL, // retain
NULL, // release
NULL // description
0, // version
this, // user data
NULL, // retain
NULL, // release
NULL // description
};
if (!SCNetworkReachabilitySetCallback(
reachability_,
&NetworkChangeNotifierMac::ReachabilityCallback,
reachability_, &NetworkChangeNotifierMac::ReachabilityCallback,
&reachability_context)) {
LOG(DFATAL) << "Could not set network reachability callback";
reachability_.reset();
} else if (!SCNetworkReachabilityScheduleWithRunLoop(reachability_,
run_loop_,
} else if (!SCNetworkReachabilityScheduleWithRunLoop(reachability_, run_loop_,
kCFRunLoopCommonModes)) {
LOG(DFATAL) << "Could not schedule network reachability on run loop";
reachability_.reset();
Expand All @@ -187,8 +248,8 @@ void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys(
CFArrayAppendValue(notification_keys.get(), key.get());

// Set the notification keys. This starts us receiving notifications.
bool ret = SCDynamicStoreSetNotificationKeys(
store, notification_keys.get(), NULL);
bool ret =
SCDynamicStoreSetNotificationKeys(store, notification_keys.get(), NULL);
// TODO(willchan): Figure out a proper way to handle this rather than crash.
CHECK(ret);
#endif // defined(OS_IOS)
Expand All @@ -202,8 +263,8 @@ void NetworkChangeNotifierMac::OnNetworkConfigChange(CFArrayRef changed_keys) {
DCHECK_EQ(run_loop_.get(), CFRunLoopGetCurrent());

for (CFIndex i = 0; i < CFArrayGetCount(changed_keys); ++i) {
CFStringRef key = static_cast<CFStringRef>(
CFArrayGetValueAtIndex(changed_keys, i));
CFStringRef key =
static_cast<CFStringRef>(CFArrayGetValueAtIndex(changed_keys, i));
if (CFStringHasSuffix(key, kSCEntNetIPv4) ||
CFStringHasSuffix(key, kSCEntNetIPv6)) {
NotifyObserversOfIPAddressChange();
Expand Down

0 comments on commit f7f1657

Please sign in to comment.