Skip to content

Commit

Permalink
Move outgoing connection management into OutgoingShareSession.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 684182202
  • Loading branch information
ftsui authored and copybara-github committed Oct 16, 2024
1 parent a094817 commit 0803bd5
Show file tree
Hide file tree
Showing 15 changed files with 572 additions and 496 deletions.
8 changes: 8 additions & 0 deletions internal/analytics/sharing_log_matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ MATCHER_P(HasSessionId, session_id, "has session id") {
return arg.session_id() == session_id;
}

MATCHER_P(HasDurationMillis, duration_millis, "has duration millis") {
return arg.duration_millis() == duration_millis;
}

MATCHER_P(SharingLogHasStatus, status, "has status") {
return arg.status() == status;
}

} // namespace nearby::analytics

#endif // THIRD_PARTY_NEARBY_INTERNAL_ANALYTICS_SHARING_LOG_MATCHERS_H_
1 change: 1 addition & 0 deletions sharing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ cc_library(
"//sharing/common:compatible_u8_string",
"//sharing/internal/api:platform",
"//sharing/internal/public:logging",
"//sharing/proto:enums_cc_proto",
"//sharing/proto:wire_format_cc_proto",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/functional:any_invocable",
Expand Down
110 changes: 46 additions & 64 deletions sharing/incoming_share_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,38 +79,30 @@ void IncomingShareSession::InvokeTransferUpdateCallback(
transfer_update_callback_(*this, metadata);
}

bool IncomingShareSession::OnNewConnection(NearbyConnection* connection) {
set_disconnect_status(TransferMetadata::Status::kFailed);
return true;
}

std::optional<TransferMetadata::Status>
IncomingShareSession::ProcessIntroduction(
const IntroductionFrame& introduction_frame) {
int64_t file_size_sum = 0;
AttachmentContainer& container = mutable_attachment_container();
for (const auto& file : introduction_frame.file_metadata()) {
if (file.size() <= 0) {
NL_LOG(WARNING)
<< __func__
<< ": Ignore introduction, due to invalid attachment size";
LOG(WARNING) << "Ignore introduction, due to invalid attachment size";
return TransferMetadata::Status::kUnsupportedAttachmentType;
}

NL_VLOG(1) << __func__ << ": Found file attachment: id=" << file.id()
<< ", type= " << file.type() << ", size=" << file.size()
<< ", payload_id=" << file.payload_id()
<< ", parent_folder=" << file.parent_folder()
<< ", mime_type=" << file.mime_type();
VLOG(1) << "Found file attachment: id=" << file.id()
<< ", type= " << file.type() << ", size=" << file.size()
<< ", payload_id=" << file.payload_id()
<< ", parent_folder=" << file.parent_folder()
<< ", mime_type=" << file.mime_type();
container.AddFileAttachment(
FileAttachment(file.id(), file.size(), file.name(), file.mime_type(),
file.type(), file.parent_folder()));
SetAttachmentPayloadId(file.id(), file.payload_id());

if (std::numeric_limits<int64_t>::max() - file.size() < file_size_sum) {
NL_LOG(WARNING) << __func__
<< ": Ignoring introduction, total file size overflowed "
"64 bit integer.";
LOG(WARNING) << "Ignoring introduction, total file size overflowed 64 "
"bit integer.";
container.Clear();
return TransferMetadata::Status::kNotEnoughSpace;
}
Expand All @@ -119,15 +111,13 @@ IncomingShareSession::ProcessIntroduction(

for (const auto& text : introduction_frame.text_metadata()) {
if (text.size() <= 0) {
NL_LOG(WARNING)
<< __func__
<< ": Ignore introduction, due to invalid attachment size";
LOG(WARNING) << "Ignore introduction, due to invalid attachment size";
return TransferMetadata::Status::kUnsupportedAttachmentType;
}

NL_VLOG(1) << __func__ << ": Found text attachment: id=" << text.id()
<< ", type= " << text.type() << ", size=" << text.size()
<< ", payload_id=" << text.payload_id();
VLOG(1) << "Found text attachment: id=" << text.id()
<< ", type= " << text.type() << ", size=" << text.size()
<< ", payload_id=" << text.payload_id();
container.AddTextAttachment(
TextAttachment(text.id(), text.type(), text.text_title(), text.size()));
SetAttachmentPayloadId(text.id(), text.payload_id());
Expand All @@ -136,10 +126,9 @@ IncomingShareSession::ProcessIntroduction(
if (kSupportReceivingWifiCredentials) {
for (const auto& wifi_credentials :
introduction_frame.wifi_credentials_metadata()) {
NL_VLOG(1) << __func__ << ": Found WiFi credentials attachment: id="
<< wifi_credentials.id()
<< ", ssid= " << wifi_credentials.ssid()
<< ", payload_id=" << wifi_credentials.payload_id();
VLOG(1) << "Found WiFi credentials attachment: id="
<< wifi_credentials.id() << ", ssid= " << wifi_credentials.ssid()
<< ", payload_id=" << wifi_credentials.payload_id();
container.AddWifiCredentialsAttachment(WifiCredentialsAttachment(
wifi_credentials.id(), wifi_credentials.ssid(),
wifi_credentials.security_type()));
Expand All @@ -149,9 +138,9 @@ IncomingShareSession::ProcessIntroduction(
}

if (!container.HasAttachments()) {
NL_LOG(WARNING) << __func__
<< ": No attachment is found for this share target. It can "
"be result of unrecognizable attachment type";
LOG(WARNING) << __func__
<< ": No attachment is found for this share target. It can "
"be result of unrecognizable attachment type";
return TransferMetadata::Status::kUnsupportedAttachmentType;
}
return std::nullopt;
Expand All @@ -165,8 +154,7 @@ bool IncomingShareSession::ProcessKeyVerificationResult(
if (!HandleKeyVerificationResult(result, share_target_os_type)) {
return false;
}
NL_LOG(INFO) << __func__ << ": Waiting for introduction from "
<< share_target().id;
LOG(INFO) << ":Waiting for introduction from " << share_target().id;

frames_reader()->ReadFrame(
V1Frame::INTRODUCTION,
Expand All @@ -186,7 +174,7 @@ bool IncomingShareSession::ReadyForTransfer(
std::function<void()> accept_timeout_callback,
std::function<void(std::optional<V1Frame> frame)> frame_read_callback) {
if (!IsConnected()) {
NL_LOG(WARNING) << __func__ << ": out of order API call.";
LOG(WARNING) << "ReadyForTransfer called when not connected";
return false;
}
ready_for_accept_ = true;
Expand All @@ -212,7 +200,7 @@ bool IncomingShareSession::ReadyForTransfer(
bool IncomingShareSession::AcceptTransfer(
std::function<void(int64_t, TransferMetadata)> update_callback) {
if (!ready_for_accept_ || !IsConnected()) {
NL_LOG(WARNING) << __func__ << ": out of order API call.";
LOG(WARNING) << "AcceptTransfer call not expected";
return false;
}
ready_for_accept_ = false;
Expand All @@ -224,12 +212,11 @@ bool IncomingShareSession::AcceptTransfer(

// Register status listener for all payloads.
for (auto it = payload_map.begin(); it != payload_map.end(); ++it) {
NL_VLOG(1) << __func__
<< ": Started listening for progress on payload: " << it->second
<< " for attachment: " << it->first;
VLOG(1) << "Started listening for progress on payload: " << it->second
<< " for attachment: " << it->first;

connections_manager().RegisterPayloadStatusListener(it->second,
payload_tracker());
payload_tracker());

NL_VLOG(1) << __func__ << ": Accepted incoming files from share target - "
<< share_target().id;
Expand All @@ -251,7 +238,7 @@ bool IncomingShareSession::AcceptTransfer(
// the system or the user has verified the sender's identity; the
// stable identifiers potentially exposed by performing a bandwidth
// upgrade are no longer a concern.
NL_LOG(INFO) << __func__ << ": Upgrade bandwidth when sending accept.";
LOG(INFO) << "Upgrade bandwidth when sending accept.";
}
// Log analytics event of starting to receive payloads.
analytics_recorder().NewReceiveAttachmentsStart(session_id(),
Expand All @@ -270,17 +257,15 @@ bool IncomingShareSession::UpdateFilePayloadPaths() {
}
const auto it = attachment_payload_map().find(file.id());
if (it == attachment_payload_map().end()) {
NL_LOG(WARNING) << __func__ << ": No payload id found for file - "
<< file.id();
LOG(WARNING) << "Payload id missing for file attachment: " << file.id();
result = false;
continue;
}

const Payload* incoming_payload =
connections_manager().GetIncomingPayload(it->second);
if (!incoming_payload || !incoming_payload->content.is_file()) {
NL_LOG(WARNING) << __func__ << ": No payload found for file - "
<< file.id();
LOG(WARNING) << "No payload found for file attachment: " << file.id();
result = false;
continue;
}
Expand All @@ -304,24 +289,20 @@ bool IncomingShareSession::UpdatePayloadContents() {
if (it == attachment_payload_map().end()) {
// This should never happen unless IntroductionFrame has not been
// processed.
NL_LOG(WARNING) << __func__ << ": No payload id found for text - "
<< text.id();
LOG(WARNING) << "Payload id missing for text attachment: " << text.id();
return false;
}
const Payload* incoming_payload =
connections_manager().GetIncomingPayload(it->second);
if (!incoming_payload || !incoming_payload->content.is_bytes()) {
NL_LOG(WARNING) << __func__ << ": No payload found for text - "
<< text.id();
LOG(WARNING) << "No payload found for text attachment: " << text.id();
return false;
}

std::vector<uint8_t> bytes = incoming_payload->content.bytes_payload.bytes;
if (bytes.empty()) {
NL_LOG(WARNING)
<< __func__
<< ": Incoming bytes is empty for text payload with payload_id - "
<< it->second;
LOG(WARNING) << "Incoming bytes is empty for text attachment: "
<< text.id() << " with payload_id: " << it->second;
return false;
}

Expand All @@ -337,36 +318,32 @@ bool IncomingShareSession::UpdatePayloadContents() {
if (it == attachment_payload_map().end()) {
// This should never happen unless IntroductionFrame has not been
// processed.
NL_LOG(WARNING) << __func__
<< ": No payload id found for WiFi credentials - "
<< wifi_credentials_attachment.id();
LOG(WARNING) << "Payload id missing for WiFi credentials: "
<< wifi_credentials_attachment.id();
return false;
}

const Payload* incoming_payload =
connections_manager().GetIncomingPayload(it->second);
if (!incoming_payload || !incoming_payload->content.is_bytes()) {
NL_LOG(WARNING) << __func__
<< ": No payload found for WiFi credentials - "
<< wifi_credentials_attachment.id();
LOG(WARNING) << "No payload found for WiFi credentials: "
<< wifi_credentials_attachment.id();
return false;
}

std::vector<uint8_t> bytes = incoming_payload->content.bytes_payload.bytes;
if (bytes.empty()) {
NL_LOG(WARNING) << __func__
<< ": Incoming bytes is empty for WiFi credentials "
"payload with payload_id - "
<< it->second;
LOG(WARNING) << "Incoming bytes is empty for WiFi credentials: "
<< wifi_credentials_attachment.id()
<< " with payload_id: " << it->second;
return false;
}

WifiCredentials wifi_credentials;
if (!wifi_credentials.ParseFromArray(bytes.data(), bytes.size())) {
NL_LOG(WARNING) << __func__
<< ": Incoming bytes is invalid for WiFi credentials "
"payload with payload_id - "
<< it->second;
LOG(WARNING) << "Incoming bytes is invalid for WiFi credentials: "
<< wifi_credentials_attachment.id()
<< " with payload_id: " << it->second;
return false;
}

Expand Down Expand Up @@ -481,4 +458,9 @@ std::pair<bool, bool> IncomingShareSession::PayloadTransferUpdate(
return std::make_pair(/*completed=*/false, /*success=*/false);
}

void IncomingShareSession::OnConnected(NearbyConnection* connection) {
set_disconnect_status(TransferMetadata::Status::kFailed);
SetConnection(connection);
}

} // namespace nearby::sharing
4 changes: 3 additions & 1 deletion sharing/incoming_share_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ class IncomingShareSession : public ShareSession {
std::pair<bool, bool> PayloadTransferUpdate(
bool update_file_paths_in_progress, const TransferMetadata& metadata);

// Called when an incoming connection is established.
void OnConnected(NearbyConnection* connection);

protected:
void InvokeTransferUpdateCallback(const TransferMetadata& metadata) override;
bool OnNewConnection(NearbyConnection* connection) override;

private:
// Update file attachment paths with payload paths.
Expand Down
Loading

0 comments on commit 0803bd5

Please sign in to comment.