Commit c01cf1b9 authored by Josh Nohle's avatar Josh Nohle Committed by Chromium LUCI CQ

[Nearby] Do not disable WebRTC if attachments exceed 25MB

Currently, if the sum of attachment sizes exceeds 25MB, WebRTC will be
disabled regardless of the user's data-usage setting. Because we already
provide the user an option to only use unmetered connections, we make
changes in this CL to respect the user's setting regardless of
attachment size.

We also add additional logging of supported mediums.

Manually tested that the correct mediums are sent to Nearby Connections;
however, there is an outstanding bug in Nearby Connections (b/176432167)
that supported medium choices are not respected.

Fixed: 1164688
Change-Id: I749ab72aaf48feac7e799561f1ad1bbb961f5e31
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2619319
Auto-Submit: Josh Nohle <nohle@chromium.org>
Commit-Queue: James Vecore <vecore@google.com>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Cr-Commit-Position: refs/heads/master@{#842092}
parent 18f4dfca
......@@ -45,10 +45,6 @@ constexpr base::TimeDelta kInvalidateDelay =
constexpr base::TimeDelta kMinProgressUpdateFrequency =
base::TimeDelta::FromMilliseconds(100);
// If total size of all attachments is larger than this limit, online share will
// be disabled even if it would be allowed by the user.
constexpr int64_t kOnlineFileSizeLimitBytes = 25 * 1024 * 1024; // 25MB
// TODO(crbug.com/1129069): Set this to true when WiFi LAN is supported to
// enable logic that checks for an internet connection for managing surfaces and
// the utility process lifecycle.
......
......@@ -41,17 +41,41 @@ bool ShouldEnableWebRtc(DataUsage data_usage, PowerLevel power_level) {
net::NetworkChangeNotifier::GetConnectionType();
// Verify that this network has an internet connection.
if (connection_type == net::NetworkChangeNotifier::CONNECTION_NONE)
if (connection_type == net::NetworkChangeNotifier::CONNECTION_NONE) {
NS_LOG(VERBOSE) << __func__
<< ": Do not use WebRTC; no internet connection.";
return false;
}
// If the user wants to limit WebRTC, then only use it on unmetered networks.
if (data_usage == DataUsage::kWifiOnly)
return !net::NetworkChangeNotifier::IsConnectionCellular(connection_type);
if (data_usage == DataUsage::kWifiOnly &&
net::NetworkChangeNotifier::IsConnectionCellular(connection_type)) {
NS_LOG(VERBOSE) << __func__ << ": Do not use WebRTC with " << data_usage
<< " and a cellular conneciton.";
return false;
}
// We're online, the user hasn't disabled WebRTC, let's use it!
return true;
}
std::string MediumSelectionToString(
const location::nearby::connections::mojom::MediumSelection& mediums) {
std::stringstream ss;
ss << "{";
if (mediums.bluetooth)
ss << "bluetooth ";
if (mediums.ble)
ss << "ble ";
if (mediums.web_rtc)
ss << "webrtc ";
if (mediums.wifi_lan)
ss << "wifilan ";
ss << "}";
return ss.str();
}
} // namespace
NearbyConnectionsManagerImpl::NearbyConnectionsManagerImpl(
......@@ -92,6 +116,10 @@ void NearbyConnectionsManagerImpl::StartAdvertising(
/*bluetooth=*/is_high_power, /*ble=*/use_ble,
ShouldEnableWebRtc(data_usage, power_level),
/*wifi_lan=*/is_high_power && kIsWifiLanSupported);
NS_LOG(VERBOSE) << __func__ << ": "
<< "is_high_power=" << (is_high_power ? "yes" : "no")
<< ", data_usage=" << data_usage << ", allowed_mediums="
<< MediumSelectionToString(*allowed_mediums);
mojo::PendingRemote<ConnectionLifecycleListener> lifecycle_listener;
connection_lifecycle_listeners_.Add(
......@@ -142,6 +170,9 @@ void NearbyConnectionsManagerImpl::StartDiscovery(
/*ble=*/true,
/*webrtc=*/ShouldEnableWebRtc(data_usage, PowerLevel::kHighPower),
/*wifi_lan=*/kIsWifiLanSupported);
NS_LOG(VERBOSE) << __func__ << ": "
<< "data_usage=" << data_usage << ", allowed_mediums="
<< MediumSelectionToString(*allowed_mediums);
discovery_listener_ = listener;
nearby_connections_->StartDiscovery(
......@@ -188,6 +219,9 @@ void NearbyConnectionsManagerImpl::Connect(
/*bluetooth=*/true,
/*ble=*/false, ShouldEnableWebRtc(data_usage, PowerLevel::kHighPower),
/*wifi_lan=*/kIsWifiLanSupported);
NS_LOG(VERBOSE) << __func__ << ": "
<< "data_usage=" << data_usage << ", allowed_mediums="
<< MediumSelectionToString(*allowed_mediums);
mojo::PendingRemote<ConnectionLifecycleListener> lifecycle_listener;
connection_lifecycle_listeners_.Add(
......
......@@ -153,36 +153,6 @@ bool IsOutOfStorage(base::FilePath file_path,
return free_space < storage_required;
}
bool DoAttachmentsExceedThreshold(const ShareTarget& share_target,
int64_t threshold) {
for (const auto& attachment : share_target.text_attachments) {
if (attachment.size() > threshold)
return false;
threshold -= attachment.size();
}
for (const auto& attachment : share_target.file_attachments) {
if (attachment.size() > threshold)
return false;
threshold -= attachment.size();
}
return true;
}
DataUsage CheckFileSizeForDataUsagePreference(DataUsage client_preference,
const ShareTarget& share_target) {
if (client_preference == DataUsage::kOffline)
return client_preference;
if (DoAttachmentsExceedThreshold(share_target, kOnlineFileSizeLimitBytes))
return DataUsage::kOffline;
return client_preference;
}
int64_t GeneratePayloadId() {
int64_t payload_id = 0;
crypto::RandBytes(&payload_id, sizeof(payload_id));
......@@ -2266,16 +2236,13 @@ void NearbySharingServiceImpl::OnCreatePayloads(
base::Optional<std::vector<uint8_t>> bluetooth_mac_address =
GetBluetoothMacAddress(share_target);
DataUsage adjusted_data_usage = CheckFileSizeForDataUsagePreference(
settings_.GetDataUsage(), share_target);
// For metrics.
cancelled_share_target_ids_.clear();
// TODO(crbug.com/1111458): Add preferred transfer type.
nearby_connections_manager_->Connect(
std::move(endpoint_info), *info->endpoint_id(),
std::move(bluetooth_mac_address), adjusted_data_usage,
std::move(bluetooth_mac_address), settings_.GetDataUsage(),
base::BindOnce(&NearbySharingServiceImpl::OnOutgoingConnection,
weak_ptr_factory_.GetWeakPtr(), share_target,
base::TimeTicks::Now()));
......
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