mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Move payload tracker callback to service thread.
PiperOrigin-RevId: 694644790
This commit is contained in:
committed by
Copybara-Service
parent
63c8486a41
commit
13d5f702d6
@@ -46,8 +46,8 @@ FakeNearbyConnectionsManager::FakeNearbyConnectionsManager() = default;
|
||||
FakeNearbyConnectionsManager::~FakeNearbyConnectionsManager() = default;
|
||||
|
||||
void FakeNearbyConnectionsManager::Shutdown() {
|
||||
NL_DCHECK(!IsAdvertising());
|
||||
NL_DCHECK(!IsDiscovering());
|
||||
DCHECK(!IsAdvertising());
|
||||
DCHECK(!IsDiscovering());
|
||||
is_shutdown_ = true;
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ void FakeNearbyConnectionsManager::StartAdvertising(
|
||||
std::vector<uint8_t> endpoint_info, IncomingConnectionListener* listener,
|
||||
PowerLevel power_level, DataUsage data_usage, bool use_stable_endpoint_id,
|
||||
ConnectionsCallback callback) {
|
||||
NL_DCHECK(!IsAdvertising());
|
||||
DCHECK(!IsAdvertising());
|
||||
is_shutdown_ = false;
|
||||
{
|
||||
absl::MutexLock lock(&listener_mutex_);
|
||||
@@ -74,8 +74,8 @@ void FakeNearbyConnectionsManager::StartAdvertising(
|
||||
|
||||
void FakeNearbyConnectionsManager::StopAdvertising(
|
||||
ConnectionsCallback callback) {
|
||||
NL_DCHECK(IsAdvertising());
|
||||
NL_DCHECK(!is_shutdown());
|
||||
DCHECK(IsAdvertising());
|
||||
DCHECK(!is_shutdown());
|
||||
{
|
||||
absl::MutexLock lock(&listener_mutex_);
|
||||
advertising_listener_ = nullptr;
|
||||
@@ -101,8 +101,8 @@ void FakeNearbyConnectionsManager::StartDiscovery(
|
||||
}
|
||||
|
||||
void FakeNearbyConnectionsManager::StopDiscovery() {
|
||||
NL_DCHECK(IsDiscovering());
|
||||
NL_DCHECK(!is_shutdown());
|
||||
DCHECK(IsDiscovering());
|
||||
DCHECK(!is_shutdown());
|
||||
absl::MutexLock lock(&listener_mutex_);
|
||||
discovery_listener_ = nullptr;
|
||||
}
|
||||
@@ -112,7 +112,7 @@ void FakeNearbyConnectionsManager::Connect(
|
||||
std::optional<std::vector<uint8_t>> bluetooth_mac_address,
|
||||
DataUsage data_usage, TransportType transport_type,
|
||||
NearbyConnectionCallback callback) {
|
||||
NL_DCHECK(!is_shutdown());
|
||||
DCHECK(!is_shutdown());
|
||||
connected_data_usage_ = data_usage;
|
||||
transport_type_ = transport_type;
|
||||
connection_endpoint_infos_.emplace(endpoint_id, std::move(endpoint_info));
|
||||
@@ -120,28 +120,28 @@ void FakeNearbyConnectionsManager::Connect(
|
||||
}
|
||||
|
||||
void FakeNearbyConnectionsManager::Disconnect(absl::string_view endpoint_id) {
|
||||
NL_DCHECK(!is_shutdown());
|
||||
DCHECK(!is_shutdown());
|
||||
connection_endpoint_infos_.erase(std::string(endpoint_id));
|
||||
}
|
||||
|
||||
void FakeNearbyConnectionsManager::Send(
|
||||
absl::string_view endpoint_id, std::unique_ptr<Payload> payload,
|
||||
std::weak_ptr<PayloadStatusListener> listener) {
|
||||
NL_DCHECK(!is_shutdown());
|
||||
DCHECK(!is_shutdown());
|
||||
if (send_payload_callback_)
|
||||
send_payload_callback_(std::move(payload), listener);
|
||||
}
|
||||
|
||||
void FakeNearbyConnectionsManager::RegisterPayloadStatusListener(
|
||||
int64_t payload_id, std::weak_ptr<PayloadStatusListener> listener) {
|
||||
NL_DCHECK(!is_shutdown());
|
||||
DCHECK(!is_shutdown());
|
||||
|
||||
payload_status_listeners_[payload_id] = listener;
|
||||
}
|
||||
|
||||
const Payload* FakeNearbyConnectionsManager::GetIncomingPayload(
|
||||
int64_t payload_id) const {
|
||||
NL_DCHECK(!is_shutdown());
|
||||
DCHECK(!is_shutdown());
|
||||
absl::MutexLock lock(&incoming_payloads_mutex_);
|
||||
auto it = incoming_payloads_.find(payload_id);
|
||||
if (it == incoming_payloads_.end()) return nullptr;
|
||||
@@ -150,7 +150,7 @@ const Payload* FakeNearbyConnectionsManager::GetIncomingPayload(
|
||||
}
|
||||
|
||||
void FakeNearbyConnectionsManager::Cancel(int64_t payload_id) {
|
||||
NL_DCHECK(!is_shutdown());
|
||||
DCHECK(!is_shutdown());
|
||||
std::weak_ptr<PayloadStatusListener> listener =
|
||||
GetRegisteredPayloadStatusListener(payload_id);
|
||||
if (auto weak_listener = listener.lock()) {
|
||||
@@ -159,8 +159,7 @@ void FakeNearbyConnectionsManager::Cancel(int64_t payload_id) {
|
||||
status_update->status = PayloadStatus::kCanceled;
|
||||
status_update->total_bytes = 0;
|
||||
status_update->bytes_transferred = 0;
|
||||
weak_listener->OnStatusUpdate(std::move(status_update),
|
||||
/*upgraded_medium=*/std::nullopt);
|
||||
weak_listener->OnStatusUpdate(std::move(status_update));
|
||||
payload_status_listeners_.erase(payload_id);
|
||||
}
|
||||
|
||||
@@ -176,7 +175,7 @@ void FakeNearbyConnectionsManager::ClearIncomingPayloads() {
|
||||
std::optional<std::vector<uint8_t>>
|
||||
FakeNearbyConnectionsManager::GetRawAuthenticationToken(
|
||||
absl::string_view endpoint_id) {
|
||||
NL_DCHECK(!is_shutdown());
|
||||
DCHECK(!is_shutdown());
|
||||
|
||||
auto iter = endpoint_auth_tokens_.find(std::string(endpoint_id));
|
||||
if (iter != endpoint_auth_tokens_.end()) return iter->second;
|
||||
|
||||
@@ -84,11 +84,8 @@ class NearbyConnectionsManager {
|
||||
return weak_from_this();
|
||||
}
|
||||
|
||||
// Note: `upgraded_medium` is passed in for use in metrics, and it is
|
||||
// absl::nullopt if the bandwidth has not upgraded yet or if the upgrade
|
||||
// status is not known.
|
||||
virtual void OnStatusUpdate(std::unique_ptr<PayloadTransferUpdate> update,
|
||||
std::optional<Medium> upgraded_medium) = 0;
|
||||
virtual void OnStatusUpdate(
|
||||
std::unique_ptr<PayloadTransferUpdate> update) = 0;
|
||||
};
|
||||
|
||||
// Converts the status to a logging-friendly string.
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
#include "absl/functional/bind_front.h"
|
||||
#include "absl/meta/type_traits.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/time/time.h"
|
||||
@@ -78,7 +79,7 @@ bool ShouldUseInternet(ConnectivityManager& connectivity_manager,
|
||||
|
||||
// Verify that this network has an internet connection.
|
||||
if (connection_type == ConnectivityManager::ConnectionType::kNone) {
|
||||
NL_VLOG(1) << __func__ << ": No internet connection.";
|
||||
VLOG(1) << __func__ << ": No internet connection.";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -168,8 +169,8 @@ void NearbyConnectionsManagerImpl::StartAdvertising(
|
||||
std::vector<uint8_t> endpoint_info, IncomingConnectionListener* listener,
|
||||
PowerLevel power_level, DataUsage data_usage, bool use_stable_endpoint_id,
|
||||
ConnectionsCallback callback) {
|
||||
NL_DCHECK(listener);
|
||||
NL_DCHECK(!incoming_connection_listener_);
|
||||
DCHECK(listener);
|
||||
DCHECK(!incoming_connection_listener_);
|
||||
|
||||
if (!nearby_connections_service_) {
|
||||
std::move(callback)(ConnectionsStatus::kError);
|
||||
@@ -189,11 +190,10 @@ void NearbyConnectionsManagerImpl::StartAdvertising(
|
||||
/*wifi_lan=*/
|
||||
ShouldEnableWifiLan(connectivity_manager_),
|
||||
/*wifi_hotspot=*/true);
|
||||
NL_VLOG(1) << __func__ << ": "
|
||||
<< "is_high_power=" << (is_high_power ? "yes" : "no")
|
||||
<< ", data_usage=" << static_cast<int>(data_usage)
|
||||
<< ", allowed_mediums="
|
||||
<< MediumSelectionToString(allowed_mediums);
|
||||
VLOG(1) << __func__ << ": "
|
||||
<< "is_high_power=" << (is_high_power ? "yes" : "no")
|
||||
<< ", data_usage=" << static_cast<int>(data_usage)
|
||||
<< ", allowed_mediums=" << MediumSelectionToString(allowed_mediums);
|
||||
|
||||
// Nearby Sharing manually controls Wi-Fi/Bluetooth upgrade. Frequent
|
||||
// Bluetooth connection drops were observed during upgrades for Bluetooth
|
||||
@@ -271,7 +271,7 @@ void NearbyConnectionsManagerImpl::StopAdvertising(
|
||||
void NearbyConnectionsManagerImpl::StartDiscovery(
|
||||
DiscoveryListener* listener, DataUsage data_usage,
|
||||
ConnectionsCallback callback) {
|
||||
NL_DCHECK(listener);
|
||||
DCHECK(listener);
|
||||
|
||||
if (!nearby_connections_service_) {
|
||||
std::move(callback)(ConnectionsStatus::kError);
|
||||
@@ -287,10 +287,9 @@ void NearbyConnectionsManagerImpl::StartDiscovery(
|
||||
/*wifi_lan=*/
|
||||
ShouldEnableWifiLan(connectivity_manager_),
|
||||
/*wifi_hotspot=*/true);
|
||||
NL_VLOG(1) << __func__ << ": "
|
||||
<< "data_usage=" << static_cast<int>(data_usage)
|
||||
<< ", allowed_mediums="
|
||||
<< MediumSelectionToString(allowed_mediums);
|
||||
VLOG(1) << __func__ << ": "
|
||||
<< "data_usage=" << static_cast<int>(data_usage)
|
||||
<< ", allowed_mediums=" << MediumSelectionToString(allowed_mediums);
|
||||
|
||||
discovery_listener_ = listener;
|
||||
|
||||
@@ -321,10 +320,10 @@ void NearbyConnectionsManagerImpl::StopDiscovery() {
|
||||
|
||||
nearby_connections_service_->StopDiscovery(
|
||||
kServiceId, [&](ConnectionsStatus status) {
|
||||
NL_VLOG(1) << __func__
|
||||
<< ": Stop discovery attempted over Nearby "
|
||||
"Connections with result: "
|
||||
<< ConnectionsStatusToString(status);
|
||||
VLOG(1) << __func__
|
||||
<< ": Stop discovery attempted over Nearby "
|
||||
"Connections with result: "
|
||||
<< ConnectionsStatusToString(status);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -352,13 +351,12 @@ void NearbyConnectionsManagerImpl::Connect(
|
||||
ShouldEnableWifiLan(connectivity_manager_),
|
||||
/*wifi_hotspot=*/
|
||||
IsTransportTypeFlagsSet(transport_type, TransportType::kHighQuality));
|
||||
NL_VLOG(1) << __func__ << ": "
|
||||
<< "data_usage=" << static_cast<int>(data_usage)
|
||||
<< ", allowed_mediums="
|
||||
<< MediumSelectionToString(allowed_mediums);
|
||||
VLOG(1) << __func__ << ": "
|
||||
<< "data_usage=" << static_cast<int>(data_usage)
|
||||
<< ", allowed_mediums=" << MediumSelectionToString(allowed_mediums);
|
||||
[[maybe_unused]] auto result =
|
||||
pending_outgoing_connections_.emplace(endpoint_id, std::move(callback));
|
||||
NL_DCHECK(result.second);
|
||||
DCHECK(result.second);
|
||||
|
||||
auto timeout_timer = context_->CreateTimer();
|
||||
timeout_timer->Start(
|
||||
@@ -468,10 +466,9 @@ void NearbyConnectionsManagerImpl::Disconnect(absl::string_view endpoint_id) {
|
||||
nearby_connections_service_->DisconnectFromEndpoint(
|
||||
kServiceId, endpoint_id,
|
||||
[&, endpoint_id = std::string(endpoint_id)](ConnectionsStatus status) {
|
||||
NL_VLOG(1) << __func__ << ": Disconnecting from endpoint "
|
||||
<< endpoint_id
|
||||
<< " attempted over Nearby Connections with result: "
|
||||
<< ConnectionsStatusToString(status);
|
||||
VLOG(1) << __func__ << ": Disconnecting from endpoint " << endpoint_id
|
||||
<< " attempted over Nearby Connections with result: "
|
||||
<< ConnectionsStatusToString(status);
|
||||
|
||||
connections_callback_task_runner_->PostTask([this, endpoint_id]() {
|
||||
OnDisconnected(endpoint_id);
|
||||
@@ -551,18 +548,17 @@ void NearbyConnectionsManagerImpl::Cancel(int64_t payload_id) {
|
||||
// with another payload in the same transfer.
|
||||
if (auto status_listener = listener.lock()) {
|
||||
status_listener->OnStatusUpdate(std::make_unique<PayloadTransferUpdate>(
|
||||
payload_id, PayloadStatus::kCanceled,
|
||||
/*total_bytes=*/0,
|
||||
/*bytes_transferred=*/0),
|
||||
/*upgraded_medium=*/std::nullopt);
|
||||
payload_id, PayloadStatus::kCanceled,
|
||||
/*total_bytes=*/0,
|
||||
/*bytes_transferred=*/0));
|
||||
}
|
||||
}
|
||||
|
||||
nearby_connections_service_->CancelPayload(
|
||||
kServiceId, payload_id, [&, payload_id](ConnectionsStatus status) {
|
||||
NL_VLOG(1) << __func__ << ": Cancelling payload to id " << payload_id
|
||||
<< " attempted over Nearby Connections with result: "
|
||||
<< ConnectionsStatusToString(status);
|
||||
VLOG(1) << __func__ << ": Cancelling payload to id " << payload_id
|
||||
<< " attempted over Nearby Connections with result: "
|
||||
<< ConnectionsStatusToString(status);
|
||||
});
|
||||
|
||||
LOG(INFO) << "Cancelling payload: " << payload_id;
|
||||
@@ -605,9 +601,9 @@ void NearbyConnectionsManagerImpl::UpgradeBandwidth(
|
||||
nearby_connections_service_->InitiateBandwidthUpgrade(
|
||||
kServiceId, endpoint_id,
|
||||
[&, endpoint_id = std::string(endpoint_id)](ConnectionsStatus status) {
|
||||
NL_VLOG(1) << __func__ << ": Bandwidth upgrade attempted to endpoint "
|
||||
<< endpoint_id << "over Nearby Connections with result: "
|
||||
<< ConnectionsStatusToString(status);
|
||||
VLOG(1) << __func__ << ": Bandwidth upgrade attempted to endpoint "
|
||||
<< endpoint_id << "over Nearby Connections with result: "
|
||||
<< ConnectionsStatusToString(status);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -660,7 +656,7 @@ void NearbyConnectionsManagerImpl::OnConnectionInitiated(
|
||||
MutexLock lock(&mutex_);
|
||||
[[maybe_unused]] auto result =
|
||||
connection_info_map_.emplace(endpoint_id, std::move(info));
|
||||
NL_DCHECK(result.second);
|
||||
DCHECK(result.second);
|
||||
|
||||
NearbyConnectionsService::PayloadListener payload_listener;
|
||||
|
||||
@@ -669,21 +665,15 @@ void NearbyConnectionsManagerImpl::OnConnectionInitiated(
|
||||
OnPayloadReceived(endpoint_id, payload);
|
||||
};
|
||||
|
||||
payload_listener.payload_progress_cb =
|
||||
[this](absl::string_view endpoint_id,
|
||||
const PayloadTransferUpdate& update) {
|
||||
connections_callback_task_runner_->PostTask(
|
||||
[this, endpoint_id = std::string(endpoint_id), update = update]() {
|
||||
OnPayloadTransferUpdate(endpoint_id, update);
|
||||
});
|
||||
};
|
||||
payload_listener.payload_progress_cb = absl::bind_front(
|
||||
&NearbyConnectionsManagerImpl::OnPayloadTransferUpdate, this);
|
||||
|
||||
nearby_connections_service_->AcceptConnection(
|
||||
kServiceId, endpoint_id, std::move(payload_listener),
|
||||
[endpoint_id = std::string(endpoint_id)](ConnectionsStatus status) {
|
||||
NL_VLOG(1) << __func__ << ": Accept connection attempted to endpoint "
|
||||
<< endpoint_id << " over Nearby Connections with result: "
|
||||
<< ConnectionsStatusToString(status);
|
||||
VLOG(1) << __func__ << ": Accept connection attempted to endpoint "
|
||||
<< endpoint_id << " over Nearby Connections with result: "
|
||||
<< ConnectionsStatusToString(status);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -703,7 +693,7 @@ void NearbyConnectionsManagerImpl::OnConnectionAccepted(
|
||||
auto result = connections_.emplace(std::string(endpoint_id),
|
||||
std::make_unique<NearbyConnectionImpl>(
|
||||
device_info_, this, endpoint_id));
|
||||
NL_DCHECK(result.second);
|
||||
DCHECK(result.second);
|
||||
incoming_connection_listener_->OnIncomingConnection(
|
||||
endpoint_id, it->second.endpoint_info, result.first->second.get());
|
||||
} else {
|
||||
@@ -716,7 +706,7 @@ void NearbyConnectionsManagerImpl::OnConnectionAccepted(
|
||||
auto result = connections_.emplace(
|
||||
endpoint_id, std::make_unique<NearbyConnectionImpl>(device_info_, this,
|
||||
endpoint_id));
|
||||
NL_DCHECK(result.second);
|
||||
DCHECK(result.second);
|
||||
std::move(it->second)(result.first->second.get(), Status::kSuccess);
|
||||
pending_outgoing_connections_.erase(it);
|
||||
connect_timeout_timers_.erase(endpoint_id);
|
||||
@@ -768,9 +758,9 @@ void NearbyConnectionsManagerImpl::OnDisconnected(
|
||||
void NearbyConnectionsManagerImpl::OnBandwidthChanged(
|
||||
absl::string_view endpoint_id, Medium medium) {
|
||||
MutexLock lock(&mutex_);
|
||||
NL_VLOG(1) << __func__
|
||||
<< ": Bandwidth changed to medium=" << static_cast<int>(medium)
|
||||
<< "; endpoint_id=" << endpoint_id;
|
||||
VLOG(1) << __func__
|
||||
<< ": Bandwidth changed to medium=" << static_cast<int>(medium)
|
||||
<< "; endpoint_id=" << endpoint_id;
|
||||
|
||||
if (transfer_managers_.contains(endpoint_id)) {
|
||||
transfer_managers_[endpoint_id]->OnMediumQualityChanged(medium);
|
||||
@@ -789,8 +779,7 @@ void NearbyConnectionsManagerImpl::OnPayloadReceived(
|
||||
kDeleteUnexpectedReceivedFileFix)) {
|
||||
if (payload.content.type != PayloadContent::Type::kBytes &&
|
||||
!payload_status_listeners_.contains(payload.id)) {
|
||||
LOG(WARNING) << __func__
|
||||
<< ": Received unknown payload. Canceling.";
|
||||
LOG(WARNING) << __func__ << ": Received unknown payload. Canceling.";
|
||||
DeleteUnknownFilePayloadAndCancel(payload);
|
||||
return;
|
||||
}
|
||||
@@ -803,7 +792,7 @@ void NearbyConnectionsManagerImpl::OnPayloadReceived(
|
||||
} else {
|
||||
[[maybe_unused]] auto result =
|
||||
incoming_payloads_.emplace(payload.id, std::move(payload));
|
||||
NL_DCHECK(result.second);
|
||||
DCHECK(result.second);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -882,11 +871,12 @@ void NearbyConnectionsManagerImpl::OnPayloadTransferUpdate(
|
||||
|
||||
// Note: The listener might be invalidated, for example, if it is shared
|
||||
// with another payload in the same transfer.
|
||||
if (auto status_listener = listener->lock()) {
|
||||
status_listener->OnStatusUpdate(
|
||||
std::make_unique<PayloadTransferUpdate>(update),
|
||||
GetUpgradedMedium(endpoint_id));
|
||||
}
|
||||
connections_callback_task_runner_->PostTask([listener, update = update]() {
|
||||
if (auto status_listener = listener->lock()) {
|
||||
status_listener->OnStatusUpdate(
|
||||
std::make_unique<PayloadTransferUpdate>(update));
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -921,10 +911,10 @@ void NearbyConnectionsManagerImpl::OnPayloadTransferUpdate(
|
||||
void NearbyConnectionsManagerImpl::Reset() {
|
||||
MutexLock lock(&mutex_);
|
||||
nearby_connections_service_->StopAllEndpoints([](ConnectionsStatus status) {
|
||||
NL_VLOG(1) << __func__
|
||||
<< ": Stop all endpoints attempted over Nearby "
|
||||
"Connections with result: "
|
||||
<< ConnectionsStatusToString(status);
|
||||
VLOG(1) << __func__
|
||||
<< ": Stop all endpoints attempted over Nearby "
|
||||
"Connections with result: "
|
||||
<< ConnectionsStatusToString(status);
|
||||
});
|
||||
|
||||
discovered_endpoints_.clear();
|
||||
@@ -963,10 +953,10 @@ void NearbyConnectionsManagerImpl::SetCustomSavePath(
|
||||
MutexLock lock(&mutex_);
|
||||
nearby_connections_service_->SetCustomSavePath(
|
||||
custom_save_path, [&](Status status) {
|
||||
NL_VLOG(1) << __func__
|
||||
<< ": SetCustomSavePath attempted over Nearby "
|
||||
"Connections with result: "
|
||||
<< static_cast<int>(status);
|
||||
VLOG(1) << __func__
|
||||
<< ": SetCustomSavePath attempted over Nearby "
|
||||
"Connections with result: "
|
||||
<< static_cast<int>(status);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -124,8 +124,7 @@ class MockPayloadStatusListener
|
||||
: public NearbyConnectionsManager::PayloadStatusListener {
|
||||
public:
|
||||
MOCK_METHOD(void, OnStatusUpdate,
|
||||
(std::unique_ptr<PayloadTransferUpdate> update,
|
||||
std::optional<Medium> upgraded_medium),
|
||||
(std::unique_ptr<PayloadTransferUpdate> update),
|
||||
(override));
|
||||
};
|
||||
|
||||
@@ -1085,13 +1084,11 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectSendPayload) {
|
||||
kTotalSize, kBytesTransferred);
|
||||
absl::Notification payload_notification;
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate)
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update,
|
||||
std::optional<Medium> upgraded_medium) {
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update) {
|
||||
EXPECT_EQ(update->payload_id, expected_update.payload_id);
|
||||
EXPECT_EQ(update->bytes_transferred, expected_update.bytes_transferred);
|
||||
EXPECT_EQ(update->total_bytes, expected_update.total_bytes);
|
||||
EXPECT_EQ(update->status, expected_update.status);
|
||||
EXPECT_FALSE(upgraded_medium.has_value());
|
||||
payload_notification.Notify();
|
||||
});
|
||||
|
||||
@@ -1130,13 +1127,11 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectCancelPayload) {
|
||||
|
||||
absl::Notification payload_notification;
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate)
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update,
|
||||
std::optional<Medium> upgraded_medium) {
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update) {
|
||||
EXPECT_EQ(update->payload_id, kPayloadId);
|
||||
EXPECT_EQ(update->status, PayloadStatus::kCanceled);
|
||||
EXPECT_EQ(update->total_bytes, 0u);
|
||||
EXPECT_EQ(update->bytes_transferred, 0u);
|
||||
EXPECT_FALSE(upgraded_medium.has_value());
|
||||
payload_notification.Notify();
|
||||
});
|
||||
|
||||
@@ -1194,13 +1189,11 @@ TEST_F(NearbyConnectionsManagerImplTest,
|
||||
absl::Notification payload_notification;
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate)
|
||||
.Times(1)
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update,
|
||||
std::optional<Medium> upgraded_medium) {
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update) {
|
||||
EXPECT_EQ(update->payload_id, kPayloadId);
|
||||
EXPECT_EQ(update->status, PayloadStatus::kCanceled);
|
||||
EXPECT_EQ(update->total_bytes, 0u);
|
||||
EXPECT_EQ(update->bytes_transferred, 0u);
|
||||
EXPECT_FALSE(upgraded_medium.has_value());
|
||||
|
||||
// Destroy the PayloadStatusListener after the first payload is
|
||||
// cancelled.
|
||||
@@ -1309,11 +1302,9 @@ TEST_F(NearbyConnectionsManagerImplTest, IncomingPayloadStatusListener) {
|
||||
kTotalSize, kBytesTransferred);
|
||||
absl::Notification payload_notification;
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate)
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update,
|
||||
std::optional<Medium> upgraded_medium) {
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update) {
|
||||
EXPECT_THAT(*update, FieldsAre(kPayloadId, PayloadStatus::kInProgress,
|
||||
kTotalSize, kBytesTransferred));
|
||||
EXPECT_FALSE(upgraded_medium.has_value());
|
||||
payload_notification.Notify();
|
||||
});
|
||||
|
||||
@@ -1325,8 +1316,7 @@ TEST_F(NearbyConnectionsManagerImplTest, IncomingPayloadStatusListener) {
|
||||
// After success status, send another progress update.
|
||||
absl::Notification payload_notification_2;
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate)
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update,
|
||||
std::optional<Medium> upgraded_medium) {
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update) {
|
||||
payload_notification_2.Notify();
|
||||
});
|
||||
|
||||
@@ -1403,13 +1393,11 @@ TEST_F(NearbyConnectionsManagerImplTest,
|
||||
absl::Notification payload_notification;
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate)
|
||||
.Times(1)
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update,
|
||||
std::optional<Medium> upgraded_medium) {
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update) {
|
||||
EXPECT_EQ(update->payload_id, kPayloadId);
|
||||
EXPECT_EQ(update->status, PayloadStatus::kFailure);
|
||||
EXPECT_EQ(update->total_bytes, kTotalSize);
|
||||
EXPECT_EQ(update->bytes_transferred, 0u);
|
||||
EXPECT_FALSE(upgraded_medium.has_value());
|
||||
|
||||
// Destroy the PayloadStatusListener after the first payload fails.
|
||||
payload_listener.reset();
|
||||
@@ -1458,7 +1446,7 @@ TEST_F(NearbyConnectionsManagerImplTest, IncomingBytesPayload) {
|
||||
Payload(kPayloadId, expected_payload));
|
||||
|
||||
absl::Notification payload_notification;
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate(::testing::_, ::testing::_))
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate(::testing::_))
|
||||
.WillOnce([&]() { payload_notification.Notify(); });
|
||||
|
||||
payload_listener_remote.payload_progress_cb(
|
||||
@@ -1503,7 +1491,7 @@ TEST_F(NearbyConnectionsManagerImplTest, IncomingFilePayload) {
|
||||
Payload(kPayloadId, InputFile(file)));
|
||||
|
||||
absl::Notification payload_notification;
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate(::testing::_, ::testing::_))
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate(::testing::_))
|
||||
.WillOnce([&]() { payload_notification.Notify(); });
|
||||
|
||||
payload_listener_remote.payload_progress_cb(
|
||||
@@ -1553,7 +1541,7 @@ TEST_F(NearbyConnectionsManagerImplTest, ClearIncomingPayloads) {
|
||||
Payload(kPayloadId, InputFile(file)));
|
||||
|
||||
absl::Notification payload_notification;
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate(::testing::_, ::testing::_))
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate(::testing::_))
|
||||
.WillOnce([&]() { payload_notification.Notify(); });
|
||||
|
||||
payload_listener_remote.payload_progress_cb(
|
||||
@@ -1943,13 +1931,11 @@ TEST_F(NearbyConnectionsManagerImplTest,
|
||||
|
||||
absl::Notification payload_notification;
|
||||
EXPECT_CALL(*payload_listener, OnStatusUpdate)
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update,
|
||||
std::optional<Medium> upgraded_medium) {
|
||||
.WillOnce([&](std::unique_ptr<PayloadTransferUpdate> update) {
|
||||
EXPECT_EQ(update->payload_id, kPayloadId);
|
||||
EXPECT_EQ(update->status, PayloadStatus::kCanceled);
|
||||
EXPECT_EQ(update->total_bytes, 0u);
|
||||
EXPECT_EQ(update->bytes_transferred, 0u);
|
||||
EXPECT_FALSE(upgraded_medium.has_value());
|
||||
payload_notification.Notify();
|
||||
});
|
||||
|
||||
|
||||
@@ -1000,8 +1000,7 @@ class NearbySharingServiceImplTest : public testing::Test {
|
||||
/*total_bytes=*/strlen(kTextPayload),
|
||||
/*bytes_transferred=*/strlen(kTextPayload));
|
||||
if (auto listener = info.listener.lock()) {
|
||||
listener->OnStatusUpdate(std::move(payload_transfer_update),
|
||||
/*upgraded_medium=*/std::nullopt);
|
||||
listener->OnStatusUpdate(std::move(payload_transfer_update));
|
||||
}
|
||||
});
|
||||
EXPECT_TRUE(sharing_service_task_runner_->SyncWithTimeout(kWaitTimeout));
|
||||
@@ -1096,8 +1095,7 @@ class NearbySharingServiceImplTest : public testing::Test {
|
||||
/*bytes_transferred=*/kPayloadSize);
|
||||
if (auto locked_listener = listener.lock()) {
|
||||
locked_listener->OnStatusUpdate(
|
||||
std::make_unique<PayloadTransferUpdate>(payload),
|
||||
/*upgraded_medium=*/std::nullopt);
|
||||
std::make_unique<PayloadTransferUpdate>(payload));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1141,8 +1139,7 @@ class NearbySharingServiceImplTest : public testing::Test {
|
||||
/*bytes_transferred=*/kPayloadSize);
|
||||
if (auto locked_listener = listener.lock()) {
|
||||
locked_listener->OnStatusUpdate(
|
||||
std::make_unique<PayloadTransferUpdate>(payload),
|
||||
/*upgraded_medium=*/std::nullopt);
|
||||
std::make_unique<PayloadTransferUpdate>(payload));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -2774,8 +2771,7 @@ TEST_F(NearbySharingServiceImplTest,
|
||||
/*total_bytes=*/kPayloadSize,
|
||||
/*bytes_transferred=*/kPayloadSize);
|
||||
if (auto locked_listener = listener.lock()) {
|
||||
locked_listener->OnStatusUpdate(std::move(payload),
|
||||
/*upgraded_medium=*/std::nullopt);
|
||||
locked_listener->OnStatusUpdate(std::move(payload));
|
||||
}
|
||||
});
|
||||
EXPECT_TRUE(
|
||||
@@ -2809,8 +2805,7 @@ TEST_F(NearbySharingServiceImplTest,
|
||||
/*total_bytes=*/kPayloadSize,
|
||||
/*bytes_transferred=*/kPayloadSize);
|
||||
if (auto locked_listener = listener.lock()) {
|
||||
locked_listener->OnStatusUpdate(std::move(payload),
|
||||
/*upgraded_medium=*/std::nullopt);
|
||||
locked_listener->OnStatusUpdate(std::move(payload));
|
||||
}
|
||||
});
|
||||
EXPECT_TRUE(
|
||||
@@ -2859,8 +2854,7 @@ TEST_F(NearbySharingServiceImplTest, AcceptValidShareTargetPayloadFailed) {
|
||||
/*total_bytes=*/kPayloadSize,
|
||||
/*bytes_transferred=*/kPayloadSize);
|
||||
if (auto locked_listener = listener.lock()) {
|
||||
locked_listener->OnStatusUpdate(std::move(payload),
|
||||
/*upgraded_medium=*/std::nullopt);
|
||||
locked_listener->OnStatusUpdate(std::move(payload));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -2910,8 +2904,7 @@ TEST_F(NearbySharingServiceImplTest, AcceptValidShareTargetPayloadCancelled) {
|
||||
/*total_bytes=*/kPayloadSize,
|
||||
/*bytes_transferred=*/kPayloadSize);
|
||||
if (auto locked_listener = listener.lock()) {
|
||||
locked_listener->OnStatusUpdate(std::move(payload),
|
||||
/*upgraded_medium=*/std::nullopt);
|
||||
locked_listener->OnStatusUpdate(std::move(payload));
|
||||
}
|
||||
});
|
||||
EXPECT_TRUE(
|
||||
|
||||
+20
-28
@@ -40,8 +40,7 @@ namespace nearby {
|
||||
namespace sharing {
|
||||
|
||||
PayloadTracker::PayloadTracker(
|
||||
Clock* clock, int64_t share_target_id,
|
||||
const AttachmentContainer& container,
|
||||
Clock* clock, int64_t share_target_id, const AttachmentContainer& container,
|
||||
const absl::flat_hash_map<int64_t, int64_t>& attachment_payload_map,
|
||||
std::function<void(int64_t, TransferMetadata)> update_callback)
|
||||
: clock_(clock),
|
||||
@@ -53,10 +52,9 @@ PayloadTracker::PayloadTracker(
|
||||
for (const auto& file : container.GetFileAttachments()) {
|
||||
auto it = attachment_payload_map.find(file.id());
|
||||
if (it == attachment_payload_map.end()) {
|
||||
NL_LOG(WARNING)
|
||||
<< __func__
|
||||
<< ": Failed to retrieve payload for file attachment id - "
|
||||
<< file.id();
|
||||
LOG(WARNING) << __func__
|
||||
<< ": Failed to retrieve payload for file attachment id - "
|
||||
<< file.id();
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -68,10 +66,9 @@ PayloadTracker::PayloadTracker(
|
||||
for (const auto& text : container.GetTextAttachments()) {
|
||||
auto it = attachment_payload_map.find(text.id());
|
||||
if (it == attachment_payload_map.end()) {
|
||||
NL_LOG(WARNING)
|
||||
<< __func__
|
||||
<< ": Failed to retrieve payload for text attachment id - "
|
||||
<< text.id();
|
||||
LOG(WARNING) << __func__
|
||||
<< ": Failed to retrieve payload for text attachment id - "
|
||||
<< text.id();
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -84,10 +81,10 @@ PayloadTracker::PayloadTracker(
|
||||
container.GetWifiCredentialsAttachments()) {
|
||||
auto it = attachment_payload_map.find(wifi_credentials.id());
|
||||
if (it == attachment_payload_map.end()) {
|
||||
NL_LOG(WARNING) << __func__
|
||||
<< ": Failed to retrieve payload for WiFi credentials "
|
||||
"attachment id - "
|
||||
<< wifi_credentials.id();
|
||||
LOG(WARNING) << __func__
|
||||
<< ": Failed to retrieve payload for WiFi credentials "
|
||||
"attachment id - "
|
||||
<< wifi_credentials.id();
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -101,8 +98,7 @@ PayloadTracker::PayloadTracker(
|
||||
PayloadTracker::~PayloadTracker() = default;
|
||||
|
||||
void PayloadTracker::OnStatusUpdate(
|
||||
std::unique_ptr<PayloadTransferUpdate> update,
|
||||
std::optional<Medium> upgraded_medium) {
|
||||
std::unique_ptr<PayloadTransferUpdate> update) {
|
||||
auto it = payload_state_.find(update->payload_id);
|
||||
if (it == payload_state_.end()) return;
|
||||
|
||||
@@ -111,20 +107,16 @@ void PayloadTracker::OnStatusUpdate(
|
||||
first_update_timestamp_ = absl::Now();
|
||||
num_first_update_bytes_ = update->bytes_transferred;
|
||||
}
|
||||
if (upgraded_medium.has_value()) {
|
||||
last_upgraded_medium_ = upgraded_medium;
|
||||
}
|
||||
|
||||
if (it->second.status != update->status) {
|
||||
it->second.status = update->status;
|
||||
|
||||
NL_VLOG(1) << __func__ << ": Payload id " << update->payload_id
|
||||
<< " had status change: " << update->status;
|
||||
VLOG(1) << __func__ << ": Payload id " << update->payload_id
|
||||
<< " had status change: " << update->status;
|
||||
}
|
||||
|
||||
if (it->second.status == PayloadStatus::kSuccess) {
|
||||
NL_LOG(INFO) << __func__ << ": Completed transfer of payload " << it->first
|
||||
<< " with attachment id " << it->second.attachment_id;
|
||||
LOG(INFO) << __func__ << ": Completed transfer of payload " << it->first
|
||||
<< " with attachment id " << it->second.attachment_id;
|
||||
transferred_attachments_count_++;
|
||||
confirmed_transfer_size_ += update->bytes_transferred;
|
||||
}
|
||||
@@ -147,7 +139,7 @@ void PayloadTracker::OnStatusUpdate(
|
||||
|
||||
void PayloadTracker::OnTransferUpdate(const State& state) {
|
||||
if (IsComplete()) {
|
||||
NL_VLOG(1) << __func__ << ": All payloads are complete.";
|
||||
VLOG(1) << __func__ << ": All payloads are complete.";
|
||||
update_callback_(
|
||||
share_target_id_,
|
||||
TransferMetadataBuilder()
|
||||
@@ -160,7 +152,7 @@ void PayloadTracker::OnTransferUpdate(const State& state) {
|
||||
}
|
||||
|
||||
if (IsCancelled(state)) {
|
||||
NL_VLOG(1) << __func__ << ": Payloads cancelled.";
|
||||
VLOG(1) << __func__ << ": Payloads cancelled.";
|
||||
update_callback_(
|
||||
share_target_id_,
|
||||
TransferMetadataBuilder()
|
||||
@@ -172,7 +164,7 @@ void PayloadTracker::OnTransferUpdate(const State& state) {
|
||||
}
|
||||
|
||||
if (HasFailed(state)) {
|
||||
NL_VLOG(1) << __func__ << ": Payloads failed.";
|
||||
VLOG(1) << __func__ << ": Payloads failed.";
|
||||
update_callback_(
|
||||
share_target_id_,
|
||||
TransferMetadataBuilder()
|
||||
@@ -275,7 +267,7 @@ uint64_t PayloadTracker::GetTotalTransferred(const State& state) const {
|
||||
|
||||
double PayloadTracker::CalculateProgressPercent(const State& state) const {
|
||||
if (!total_transfer_size_) {
|
||||
NL_LOG(WARNING) << __func__ << ": Total attachment size is 0";
|
||||
LOG(WARNING) << __func__ << ": Total attachment size is 0";
|
||||
return 100.0;
|
||||
}
|
||||
|
||||
|
||||
@@ -46,8 +46,7 @@ class PayloadTracker : public NearbyConnectionsManager::PayloadStatusListener {
|
||||
~PayloadTracker() override;
|
||||
|
||||
// NearbyConnectionsManager::PayloadStatusListener:
|
||||
void OnStatusUpdate(std::unique_ptr<PayloadTransferUpdate> update,
|
||||
std::optional<Medium> upgraded_medium) override;
|
||||
void OnStatusUpdate(std::unique_ptr<PayloadTransferUpdate> update) override;
|
||||
|
||||
private:
|
||||
struct State {
|
||||
@@ -103,7 +102,6 @@ class PayloadTracker : public NearbyConnectionsManager::PayloadStatusListener {
|
||||
size_t num_wifi_credentials_attachments_ = 0;
|
||||
uint64_t num_first_update_bytes_ = 0;
|
||||
std::optional<absl::Time> first_update_timestamp_;
|
||||
std::optional<Medium> last_upgraded_medium_;
|
||||
};
|
||||
|
||||
} // namespace sharing
|
||||
|
||||
@@ -68,7 +68,7 @@ class PayloadTrackerTest : public ::testing::Test {
|
||||
auto transfer_update = std::make_unique<PayloadTransferUpdate>(
|
||||
/*payload_id=*/kFileId, PayloadStatus::kInProgress,
|
||||
/*total_bytes=*/kFileSize, /*bytes_transferred=*/bytes_transferred);
|
||||
payload_tracker_->OnStatusUpdate(std::move(transfer_update), std::nullopt);
|
||||
payload_tracker_->OnStatusUpdate(std::move(transfer_update));
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user