Clean up ShareTargetInfo.

PiperOrigin-RevId: 617397808
This commit is contained in:
Francis Tsui
2024-03-19 22:23:40 -07:00
committed by Copybara-Service
parent ed61f4a143
commit 6bd24c14a4
7 changed files with 73 additions and 57 deletions
+9 -1
View File
@@ -14,10 +14,18 @@
#include "sharing/incoming_share_target_info.h"
#include <string>
#include <utility>
#include "sharing/share_target.h"
#include "sharing/share_target_info.h"
namespace nearby {
namespace sharing {
IncomingShareTargetInfo::IncomingShareTargetInfo() = default;
IncomingShareTargetInfo::IncomingShareTargetInfo(
std::string endpoint_id, const ShareTarget& share_target)
: ShareTargetInfo(std::move(endpoint_id), share_target) {}
IncomingShareTargetInfo::IncomingShareTargetInfo(IncomingShareTargetInfo&&) =
default;
+4 -1
View File
@@ -15,6 +15,8 @@
#ifndef THIRD_PARTY_NEARBY_SHARING_INCOMING_SHARE_TARGET_INFO_H_
#define THIRD_PARTY_NEARBY_SHARING_INCOMING_SHARE_TARGET_INFO_H_
#include <string>
#include "sharing/share_target.h"
#include "sharing/share_target_info.h"
namespace nearby {
@@ -22,7 +24,8 @@ namespace sharing {
class IncomingShareTargetInfo : public ShareTargetInfo {
public:
IncomingShareTargetInfo();
IncomingShareTargetInfo(std::string endpoint_id,
const ShareTarget& share_target);
IncomingShareTargetInfo(IncomingShareTargetInfo&&);
IncomingShareTargetInfo& operator=(IncomingShareTargetInfo&&);
~IncomingShareTargetInfo() override;
+30 -38
View File
@@ -687,7 +687,7 @@ void NearbySharingServiceImpl::SendAttachments(
NL_DCHECK(!is_transferring_);
ShareTargetInfo* info = GetShareTargetInfo(share_target.id);
if (!info || !info->endpoint_id()) {
if (!info) {
NL_LOG(WARNING)
<< __func__
<< ": Failed to send attachments. Unknown ShareTarget.";
@@ -697,6 +697,7 @@ void NearbySharingServiceImpl::SendAttachments(
// Set session ID.
info->set_session_id(analytics_recorder_->GenerateNextId());
info->set_share_target(share_target_copy);
if (!share_target_copy.has_attachments()) {
NL_LOG(WARNING) << __func__ << ": No attachments to send.";
@@ -879,7 +880,7 @@ void NearbySharingServiceImpl::DoCancel(
bool is_initiator_of_cancellation) {
int64_t share_target_id = share_target.id;
ShareTargetInfo* info = GetShareTargetInfo(share_target_id);
if (!info || !info->endpoint_id()) {
if (!info) {
NL_LOG(ERROR) << __func__
<< ": Cancel invoked for unknown share target, returning "
"kOutOfOrderApiCall";
@@ -931,7 +932,7 @@ void NearbySharingServiceImpl::DoCancel(
// info->connection() will be null.
if (info->connection()) {
NL_LOG(INFO) << "Disconnect fully established endpoint id:"
<< *info->endpoint_id();
<< info->endpoint_id();
if (is_initiator_of_cancellation) {
info->connection()->SetDisconnectionListener(
[&, share_target_id, info]() {
@@ -958,8 +959,8 @@ void NearbySharingServiceImpl::DoCancel(
info->connection()->Close();
}
} else {
NL_LOG(INFO) << "Disconnect endpoint id:" << *info->endpoint_id();
nearby_connections_manager_->Disconnect(*info->endpoint_id());
NL_LOG(INFO) << "Disconnect endpoint id:" << info->endpoint_id();
nearby_connections_manager_->Disconnect(info->endpoint_id());
UnregisterShareTarget(info->IsIncoming(), share_target_id);
}
@@ -2599,14 +2600,6 @@ NearbySharingService::StatusCodes NearbySharingServiceImpl::SendPayloads(
.set_status(TransferMetadata::Status::kAwaitingRemoteAcceptance)
.build());
if (!info->endpoint_id()) {
NL_LOG(WARNING) << __func__
<< ": Failed to send payload due to missing endpoint id.";
AbortAndCloseConnectionIfNecessary(
TransferMetadata::Status::kMissingEndpointId, share_target);
return StatusCodes::kOutOfOrderApiCall;
}
ReceiveConnectionResponse(share_target);
return StatusCodes::kOk;
}
@@ -2734,7 +2727,7 @@ void NearbySharingServiceImpl::OnOutgoingConnection(
NearbyConnection* connection) {
int64_t share_target_id = share_target.id;
OutgoingShareTargetInfo* info = GetOutgoingShareTargetInfo(share_target_id);
bool success = info && info->endpoint_id() && connection;
bool success = info && connection;
if (!success) {
NL_LOG(WARNING) << __func__
@@ -2774,10 +2767,10 @@ void NearbySharingServiceImpl::OnOutgoingConnection(
std::optional<std::string> four_digit_token = TokenToFourDigitString(
nearby_connections_manager_->GetRawAuthenticationToken(
*info->endpoint_id()));
info->endpoint_id()));
RunPairedKeyVerification(
share_target, *info->endpoint_id(),
share_target, info->endpoint_id(),
[&, share_target, four_digit_token = std::move(four_digit_token)](
PairedKeyVerificationRunner::PairedKeyVerificationResult result,
OSType remote_os_type) {
@@ -2971,7 +2964,7 @@ void NearbySharingServiceImpl::OnCreatePayloads(
OutgoingShareTargetInfo* info = GetOutgoingShareTargetInfo(share_target.id);
bool has_payloads = info && (!info->text_payloads().empty() ||
!info->file_payloads().empty());
if (!success || !has_payloads || !info->endpoint_id()) {
if (!success || !has_payloads) {
NL_LOG(WARNING) << __func__
<< ": Failed to send file to remote ShareTarget. Failed to "
"create payloads.";
@@ -2994,7 +2987,7 @@ void NearbySharingServiceImpl::OnCreatePayloads(
info->set_connection_start_time(context_->GetClock()->Now());
nearby_connections_manager_->Connect(
std::move(endpoint_info), *info->endpoint_id(),
std::move(endpoint_info), info->endpoint_id(),
std::move(bluetooth_mac_address), settings_->GetDataUsage(),
GetTransportType(share_target),
[&, share_target, info](NearbyConnection* connection, Status status) {
@@ -3343,7 +3336,7 @@ void NearbySharingServiceImpl::OnOutgoingTransferUpdate(
std::optional<Payload> payload = info->ExtractNextPayload();
if (payload.has_value()) {
NL_LOG(INFO) << __func__ << ": Send payload " << payload->id;
nearby_connections_manager_->Send(*info->endpoint_id(),
nearby_connections_manager_->Send(info->endpoint_id(),
std::make_unique<Payload>(*payload),
info->payload_tracker());
} else {
@@ -3481,7 +3474,7 @@ void NearbySharingServiceImpl::OnIncomingConnectionKeyVerificationDone(
PairedKeyVerificationRunner::PairedKeyVerificationResult result,
OSType share_target_os_type) {
ShareTargetInfo* info = GetShareTargetInfo(share_target.id);
if (!info || !info->connection() || !info->endpoint_id()) {
if (!info || !info->connection()) {
NL_VLOG(1) << __func__ << ": Invalid connection or endpoint id";
return;
}
@@ -3725,6 +3718,7 @@ void NearbySharingServiceImpl::OnReceivedIntroduction(
<< share_target.id;
return;
}
info->set_share_target(share_target);
// Log analytics event of receiving introduction.
analytics_recorder_->NewReceiveIntroduction(
@@ -3740,13 +3734,12 @@ void NearbySharingServiceImpl::OnReceivedIntroduction(
if (introduction_frame.has_start_transfer() &&
introduction_frame.start_transfer()) {
if (info->endpoint_id().has_value() &&
share_target.GetTotalAttachmentsSize() >=
if (share_target.GetTotalAttachmentsSize() >=
kAttachmentsSizeThresholdOverHighQualityMedium) {
NL_LOG(INFO)
<< __func__
<< ": Upgrade bandwidth when receiving an introduction frame.";
nearby_connections_manager_->UpgradeBandwidth(*info->endpoint_id());
nearby_connections_manager_->UpgradeBandwidth(info->endpoint_id());
}
}
@@ -3841,7 +3834,7 @@ void NearbySharingServiceImpl::OnReceiveConnectionResponse(
if (payload.has_value()) {
NL_LOG(INFO) << __func__ << ": Send payload " << payload->id;
nearby_connections_manager_->Send(*info->endpoint_id(),
nearby_connections_manager_->Send(info->endpoint_id(),
std::make_unique<Payload>(*payload),
info->payload_tracker());
} else {
@@ -3849,12 +3842,12 @@ void NearbySharingServiceImpl::OnReceiveConnectionResponse(
}
} else {
for (auto& payload : info->ExtractTextPayloads()) {
nearby_connections_manager_->Send(*info->endpoint_id(),
nearby_connections_manager_->Send(info->endpoint_id(),
std::make_unique<Payload>(payload),
info->payload_tracker());
}
for (auto& payload : info->ExtractFilePayloads()) {
nearby_connections_manager_->Send(*info->endpoint_id(),
nearby_connections_manager_->Send(info->endpoint_id(),
std::make_unique<Payload>(payload),
info->payload_tracker());
}
@@ -4061,15 +4054,14 @@ void NearbySharingServiceImpl::HandleProgressUpdateFrame(
progress_update_frame.start_transfer()) {
ShareTargetInfo* info = GetShareTargetInfo(share_target.id);
if (info != nullptr && info->endpoint_id().has_value() &&
share_target.GetTotalAttachmentsSize() >=
kAttachmentsSizeThresholdOverHighQualityMedium) {
if (info != nullptr && share_target.GetTotalAttachmentsSize() >=
kAttachmentsSizeThresholdOverHighQualityMedium) {
NL_LOG(INFO)
<< __func__
<< ": Upgrade bandwidth when receiving progress update frame "
"for endpoint "
<< (*info->endpoint_id());
nearby_connections_manager_->UpgradeBandwidth(*info->endpoint_id());
<< info->endpoint_id();
nearby_connections_manager_->UpgradeBandwidth(info->endpoint_id());
}
}
@@ -4254,6 +4246,7 @@ void NearbySharingServiceImpl::OnPayloadTransferUpdate(
// transfer updates in the receive case due to the Disconnect call cleaning up
// share targets.
ShareTargetInfo* info = GetShareTargetInfo(share_target.id);
info->set_share_target(share_target);
if (info && info->transfer_update_callback())
info->transfer_update_callback()->OnTransferUpdate(share_target, metadata);
@@ -4514,10 +4507,9 @@ void NearbySharingServiceImpl::OnDisconnectingConnectionDisconnected(
ShareTargetInfo& NearbySharingServiceImpl::GetOrCreateShareTargetInfo(
const ShareTarget& share_target, absl::string_view endpoint_id) {
if (share_target.is_incoming) {
auto& info = incoming_share_target_info_map_[share_target.id];
info.set_endpoint_id(std::string(endpoint_id));
info.set_self_share(share_target.for_self_share);
return info;
auto [it, inserted] = incoming_share_target_info_map_.try_emplace(
share_target.id, std::string(endpoint_id), share_target);
return it->second;
} else {
// We need to explicitly remove any previous share target for
// |endpoint_id| if one exists, notifying observers that a share target is
@@ -4532,10 +4524,10 @@ ShareTargetInfo& NearbySharingServiceImpl::GetOrCreateShareTargetInfo(
<< ", share_target_id=" << share_target.id
<< ") to outgoing share target map";
outgoing_share_target_map_.insert_or_assign(endpoint_id, share_target);
auto& info = outgoing_share_target_info_map_[share_target.id];
info.set_endpoint_id(std::string(endpoint_id));
auto [it_out, inserted] = outgoing_share_target_info_map_.try_emplace(
share_target.id, std::string(endpoint_id), share_target);
auto& info = it_out->second;
info.set_connection_layer_status(Status::kUnknown);
info.set_self_share(share_target.for_self_share);
return info;
}
}
+5 -1
View File
@@ -20,11 +20,15 @@
#include <vector>
#include "sharing/nearby_connections_types.h"
#include "sharing/share_target.h"
#include "sharing/share_target_info.h"
namespace nearby {
namespace sharing {
OutgoingShareTargetInfo::OutgoingShareTargetInfo() = default;
OutgoingShareTargetInfo::OutgoingShareTargetInfo(
std::string endpoint_id, const ShareTarget& share_target)
: ShareTargetInfo(std::move(endpoint_id), share_target) {}
OutgoingShareTargetInfo::OutgoingShareTargetInfo(OutgoingShareTargetInfo&&) =
default;
+2 -1
View File
@@ -29,7 +29,8 @@ namespace sharing {
// A description of the outgoing connection to a remote device.
class OutgoingShareTargetInfo : public ShareTargetInfo {
public:
OutgoingShareTargetInfo();
OutgoingShareTargetInfo(std::string endpoint_id,
const ShareTarget& share_target);
OutgoingShareTargetInfo(OutgoingShareTargetInfo&&);
OutgoingShareTargetInfo& operator=(OutgoingShareTargetInfo&&);
~OutgoingShareTargetInfo() override;
+14 -6
View File
@@ -15,17 +15,19 @@
#include "sharing/share_target_info.h"
#include <string>
#include <utility>
#include "absl/time/time.h"
#include "sharing/certificates/nearby_share_decrypted_public_certificate.h"
#include "sharing/incoming_frames_reader.h"
#include "sharing/paired_key_verification_runner.h"
#include "sharing/transfer_update_callback.h"
#include "sharing/internal/public/logging.h"
#include "sharing/share_target.h"
namespace nearby {
namespace sharing {
ShareTargetInfo::ShareTargetInfo() = default;
ShareTargetInfo::ShareTargetInfo(std::string endpoint_id,
const ShareTarget& share_target)
: endpoint_id_(std::move(endpoint_id)),
self_share_(share_target.for_self_share),
share_target_(share_target) {}
ShareTargetInfo::ShareTargetInfo(ShareTargetInfo&&) = default;
@@ -33,5 +35,11 @@ ShareTargetInfo& ShareTargetInfo::operator=(ShareTargetInfo&&) = default;
ShareTargetInfo::~ShareTargetInfo() = default;
void ShareTargetInfo::set_share_target(const ShareTarget& share_target) {
NL_DCHECK(share_target.id == share_target_.id);
NL_DCHECK(share_target.for_self_share == share_target_.for_self_share);
share_target_ = share_target;
}
} // namespace sharing
} // namespace nearby
+9 -9
View File
@@ -29,6 +29,7 @@
#include "sharing/nearby_connections_manager.h"
#include "sharing/paired_key_verification_runner.h"
#include "sharing/payload_tracker.h"
#include "sharing/share_target.h"
#include "sharing/transfer_update_callback.h"
namespace nearby {
@@ -37,17 +38,13 @@ namespace sharing {
// Additional information about the connection to a remote device.
class ShareTargetInfo {
public:
ShareTargetInfo();
ShareTargetInfo(std::string endpoint_id, const ShareTarget& share_target);
ShareTargetInfo(ShareTargetInfo&&);
ShareTargetInfo& operator=(ShareTargetInfo&&);
virtual ~ShareTargetInfo();
virtual bool IsIncoming() const = 0;
const std::optional<std::string>& endpoint_id() const { return endpoint_id_; }
void set_endpoint_id(std::string endpoint_id) {
endpoint_id_ = std::move(endpoint_id);
}
std::string endpoint_id() const { return endpoint_id_; }
const std::optional<NearbyShareDecryptedPublicCertificate>& certificate()
const {
@@ -120,12 +117,14 @@ class ShareTargetInfo {
os_type_ = os_type;
}
void set_self_share(bool self_share) { self_share_ = self_share; }
bool self_share() const { return self_share_; }
void set_share_target(const ShareTarget& share_target);
ShareTarget share_target() const { return share_target_; }
private:
std::optional<std::string> endpoint_id_;
std::string endpoint_id_;
std::optional<NearbyShareDecryptedPublicCertificate> certificate_;
NearbyConnection* connection_ = nullptr;
std::unique_ptr<TransferUpdateCallback> transfer_update_callback_;
@@ -138,6 +137,7 @@ class ShareTargetInfo {
::location::nearby::proto::sharing::OSType os_type_ =
::location::nearby::proto::sharing::OSType::UNKNOWN_OS_TYPE;
bool self_share_ = false;
ShareTarget share_target_;
};
} // namespace sharing