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 10, 2024
1 parent 45e297c commit e1fc9f5
Show file tree
Hide file tree
Showing 15 changed files with 484 additions and 433 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
36 changes: 15 additions & 21 deletions sharing/incoming_share_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,18 @@ 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()
VLOG(1) << "Found file attachment: id=" << file.id()
<< ", type= " << file.type() << ", size=" << file.size()
<< ", payload_id=" << file.payload_id()
<< ", parent_folder=" << file.parent_folder()
Expand All @@ -108,9 +101,8 @@ IncomingShareSession::ProcessIntroduction(
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,13 +111,11 @@ 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()
VLOG(1) << "Found text attachment: id=" << text.id()
<< ", type= " << text.type() << ", size=" << text.size()
<< ", payload_id=" << text.payload_id();
container.AddTextAttachment(
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,7 +138,7 @@ IncomingShareSession::ProcessIntroduction(
}

if (!container.HasAttachments()) {
NL_LOG(WARNING) << __func__
LOG(WARNING) << __func__
<< ": No attachment is found for this share target. It can "
"be result of unrecognizable attachment type";
return TransferMetadata::Status::kUnsupportedAttachmentType;
Expand Down Expand Up @@ -481,4 +470,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
54 changes: 27 additions & 27 deletions sharing/incoming_share_session_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class IncomingShareSessionTest : public ::testing::Test {
};

TEST_F(IncomingShareSessionTest, ProcessIntroductionNoSupportedPayload) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
IntroductionFrame frame;

EXPECT_THAT(session_.ProcessIntroduction(frame),
Expand All @@ -193,7 +193,7 @@ TEST_F(IncomingShareSessionTest, ProcessIntroductionNoSupportedPayload) {
}

TEST_F(IncomingShareSessionTest, ProcessIntroductionEmptyFile) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
IntroductionFrame frame;
frame.mutable_file_metadata();

Expand All @@ -203,7 +203,7 @@ TEST_F(IncomingShareSessionTest, ProcessIntroductionEmptyFile) {
}

TEST_F(IncomingShareSessionTest, ProcessIntroductionFilesTooLarge) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
IntroductionFrame frame;
FileMetadata file1;
FileMetadata file2;
Expand All @@ -218,7 +218,7 @@ TEST_F(IncomingShareSessionTest, ProcessIntroductionFilesTooLarge) {
}

TEST_F(IncomingShareSessionTest, ProcessIntroductionEmptyText) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
IntroductionFrame frame;
frame.mutable_text_metadata();

Expand All @@ -228,7 +228,7 @@ TEST_F(IncomingShareSessionTest, ProcessIntroductionEmptyText) {
}

TEST_F(IncomingShareSessionTest, ProcessIntroductionSuccess) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
FileMetadata filemeta1 = introduction_frame_.file_metadata(0);
FileAttachment file1(filemeta1.id(), filemeta1.size(), filemeta1.name(),
filemeta1.mime_type(), filemeta1.type(),
Expand Down Expand Up @@ -277,7 +277,7 @@ TEST_F(IncomingShareSessionTest, ProcessIntroductionSuccess) {

TEST_F(IncomingShareSessionTest,
PayloadTransferUpdateCompleteWithWrongPayloadType) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame_),
Eq(std::nullopt));
int64_t payload_id1 = introduction_frame_.file_metadata(0).payload_id();
Expand Down Expand Up @@ -333,7 +333,7 @@ TEST_F(IncomingShareSessionTest,

TEST_F(IncomingShareSessionTest,
PayloadTransferUpdateCompleteWithMissingFilePayloads) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame_),
Eq(std::nullopt));
std::filesystem::path file1_path = "/usr/tmp/file1";
Expand Down Expand Up @@ -406,7 +406,7 @@ TEST_F(IncomingShareSessionTest,

TEST_F(IncomingShareSessionTest,
PayloadTransferUpdateCompleteWithMissingTextPayloads) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame_),
Eq(std::nullopt));
std::filesystem::path file1_path = "/usr/tmp/file1";
Expand Down Expand Up @@ -476,7 +476,7 @@ TEST_F(IncomingShareSessionTest,

TEST_F(IncomingShareSessionTest,
PayloadTransferUpdateCompleteWithMissingWifiPayloads) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame_),
Eq(std::nullopt));
std::filesystem::path file1_path = "/usr/tmp/file1";
Expand Down Expand Up @@ -544,7 +544,7 @@ TEST_F(IncomingShareSessionTest,
}

TEST_F(IncomingShareSessionTest, GetPayloadFilePaths) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
IntroductionFrame introduction_frame;
FileMetadata file1;
FileMetadata file2;
Expand Down Expand Up @@ -583,7 +583,7 @@ TEST_F(IncomingShareSessionTest, GetPayloadFilePaths) {
}

TEST_F(IncomingShareSessionTest, PayloadTransferUpdateCompleteWithSuccess) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame_),
Eq(std::nullopt));
std::filesystem::path file1_path = "/usr/tmp/file1";
Expand Down Expand Up @@ -660,7 +660,7 @@ TEST_F(IncomingShareSessionTest, PayloadTransferUpdateCompleteWithSuccess) {
}

TEST_F(IncomingShareSessionTest, PayloadTransferUpdateCancelled) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame_),
Eq(std::nullopt));
std::filesystem::path file1_path = "/usr/tmp/file1";
Expand Down Expand Up @@ -715,7 +715,7 @@ TEST_F(IncomingShareSessionTest, PayloadTransferUpdateCancelled) {
}

TEST_F(IncomingShareSessionTest, PayloadTransferUpdateFailed) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame_),
Eq(std::nullopt));
std::filesystem::path file1_path = "/usr/tmp/file1";
Expand Down Expand Up @@ -763,7 +763,7 @@ TEST_F(IncomingShareSessionTest, PayloadTransferUpdateFailed) {
}

TEST_F(IncomingShareSessionTest, PayloadTransferUpdateInProgress) {
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame_),
Eq(std::nullopt));
std::filesystem::path file1_path = "/usr/tmp/file1";
Expand Down Expand Up @@ -821,7 +821,7 @@ TEST_F(IncomingShareSessionTest, ReadyForTransferNotConnected) {

TEST_F(IncomingShareSessionTest, ReadyForTransferNotSelfShare) {
session_.set_session_id(1234);
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_CALL(
transfer_metadata_callback_,
Call(_, HasStatus(TransferMetadata::Status::kAwaitingLocalConfirmation)));
Expand All @@ -839,7 +839,7 @@ TEST_F(IncomingShareSessionTest, ReadyForTransferSelfShare) {
share_target,
transfer_metadata_callback_.AsStdFunction());
session.set_session_id(1234);
EXPECT_TRUE(session.OnConnected(absl::Now(), &connection_));
session.OnConnected(&connection_);
EXPECT_CALL(
transfer_metadata_callback_,
Call(_, HasStatus(TransferMetadata::Status::kAwaitingLocalConfirmation)))
Expand All @@ -852,7 +852,7 @@ TEST_F(IncomingShareSessionTest, ReadyForTransferSelfShare) {

TEST_F(IncomingShareSessionTest, ReadyForTransferTimeout) {
session_.set_session_id(1234);
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_CALL(
transfer_metadata_callback_,
Call(_, HasStatus(TransferMetadata::Status::kAwaitingLocalConfirmation)));
Expand All @@ -870,7 +870,7 @@ TEST_F(IncomingShareSessionTest, ReadyForTransferTimeout) {

TEST_F(IncomingShareSessionTest, ReadyForTransferTimeoutCancelled) {
session_.set_session_id(1234);
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_CALL(
transfer_metadata_callback_,
Call(_, HasStatus(TransferMetadata::Status::kAwaitingLocalConfirmation)));
Expand Down Expand Up @@ -903,7 +903,7 @@ TEST_F(IncomingShareSessionTest, AcceptTransferNotConnected) {

TEST_F(IncomingShareSessionTest, AcceptTransferNotReady) {
session_.set_session_id(1234);
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame_),
Eq(std::nullopt));

Expand All @@ -913,7 +913,7 @@ TEST_F(IncomingShareSessionTest, AcceptTransferNotReady) {

TEST_F(IncomingShareSessionTest, AcceptTransferSuccess) {
session_.set_session_id(1234);
EXPECT_TRUE(session_.OnConnected(absl::Now(), &connection_));
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame_),
Eq(std::nullopt));
EXPECT_THAT(
Expand Down Expand Up @@ -956,7 +956,7 @@ TEST_F(IncomingShareSessionTest, AcceptTransferSuccess) {
}

TEST_F(IncomingShareSessionTest, ProcessKeyVerificationResultSuccess) {
session_.OnConnected(absl::Now(), &connection_);
session_.OnConnected(&connection_);
session_.SetTokenForTests("1234");

bool introduction_received = false;
Expand Down Expand Up @@ -990,7 +990,7 @@ TEST_F(IncomingShareSessionTest, ProcessKeyVerificationResultSuccess) {
}

TEST_F(IncomingShareSessionTest, ProcessKeyVerificationResultFail) {
session_.OnConnected(absl::Now(), &connection_);
session_.OnConnected(&connection_);
session_.SetTokenForTests("1234");

bool introduction_received = false;
Expand Down Expand Up @@ -1023,7 +1023,7 @@ TEST_F(IncomingShareSessionTest, ProcessKeyVerificationResultFail) {
}

TEST_F(IncomingShareSessionTest, ProcessKeyVerificationResultUnable) {
session_.OnConnected(absl::Now(), &connection_);
session_.OnConnected(&connection_);
session_.SetTokenForTests("1234");

bool introduction_received = false;
Expand Down Expand Up @@ -1056,7 +1056,7 @@ TEST_F(IncomingShareSessionTest, ProcessKeyVerificationResultUnable) {
}

TEST_F(IncomingShareSessionTest, ProcessKeyVerificationResultUnknown) {
session_.OnConnected(absl::Now(), &connection_);
session_.OnConnected(&connection_);
session_.SetTokenForTests("1234");

bool introduction_received = false;
Expand Down Expand Up @@ -1089,7 +1089,7 @@ TEST_F(IncomingShareSessionTest, ProcessKeyVerificationResultUnknown) {
}

TEST_F(IncomingShareSessionTest, TryUpgradeBandwidthNotNeeded) {
session_.OnConnected(absl::Now(), &connection_);
session_.OnConnected(&connection_);

EXPECT_THAT(session_.TryUpgradeBandwidth(), IsFalse());
EXPECT_THAT(connections_manager_.DidUpgradeBandwidth(kEndpointId), IsFalse());
Expand Down Expand Up @@ -1119,7 +1119,7 @@ TEST_F(IncomingShareSessionTest, TryUpgradeBandwidthNeeded) {
}
)pb",
&introduction_frame));
session_.OnConnected(absl::Now(), &connection_);
session_.OnConnected(&connection_);
EXPECT_THAT(session_.ProcessIntroduction(introduction_frame),
Eq(std::nullopt));

Expand All @@ -1135,7 +1135,7 @@ TEST_F(IncomingShareSessionTest, SendFailureResponseNotConnected) {
}

TEST_F(IncomingShareSessionTest, SendFailureResponseConnected) {
session_.OnConnected(absl::Now(), &connection_);
session_.OnConnected(&connection_);
EXPECT_CALL(transfer_metadata_callback_,
Call(_, HasStatus(TransferMetadata::Status::kNotEnoughSpace)));

Expand Down
Loading

0 comments on commit e1fc9f5

Please sign in to comment.