Add kInvalidArgument status.

PiperOrigin-RevId: 638322753
This commit is contained in:
Francis Tsui
2024-05-29 10:03:04 -07:00
committed by Copybara-Service
parent 25493f4241
commit f15f7ecc24
6 changed files with 63 additions and 41 deletions
+2 -1
View File
@@ -14,7 +14,6 @@
#include "sharing/nearby_sharing_service.h"
#include <ostream>
#include <string>
#include "sharing/internal/public/logging.h"
@@ -46,6 +45,8 @@ std::string NearbySharingService::StatusCodeToString(StatusCodes status_code) {
return "kNoAvailableConnectionMedium";
case StatusCodes::kIrrecoverableHardwareError:
return "kIrrecoverableHardwareError";
case StatusCodes::kInvalidArgument:
return "kInvalidArgument";
}
NL_LOG(ERROR) << "Unexpected value for StatusCodes: "
<< static_cast<int>(status_code);
+5 -1
View File
@@ -70,7 +70,11 @@ class NearbySharingService {
// Bluetooth or WiFi hardware ran into an irrecoverable state. User PC needs
// to be restarted.
kIrrecoverableHardwareError = 6,
kMaxValue = kIrrecoverableHardwareError
// Method argument is invalid.
// TODO(b/341292610): update dart side to use kInvalidArgument.
// https://source.corp.google.com/piper///depot/google3/location/nearby/cpp/sharing/clients/dart/platform/lib/types/models.dart;rcl=637648200;l=21
kInvalidArgument = 7,
kMaxValue = kInvalidArgument
};
enum class ReceiveSurfaceState {
+46 -30
View File
@@ -376,7 +376,7 @@ void NearbySharingServiceImpl::RegisterSendSurface(
<< __func__
<< ": RegisterSendSurface failed. Already registered for a "
"different state.";
std::move(status_codes_callback)(StatusCodes::kError);
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
@@ -534,7 +534,7 @@ void NearbySharingServiceImpl::RegisterReceiveSurface(
NL_LOG(ERROR) << __func__
<< ": transfer callback already registered but for a "
"different state.";
std::move(status_codes_callback)(StatusCodes::kError);
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
@@ -659,7 +659,7 @@ void NearbySharingServiceImpl::SendAttachments(
if (attachments.empty()) {
NL_LOG(WARNING) << __func__ << ": No attachments to send.";
std::move(status_codes_callback)(StatusCodes::kError);
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
// Outgoing connections always announces with contacts visibility.
@@ -673,20 +673,33 @@ void NearbySharingServiceImpl::SendAttachments(
return;
}
ShareTargetInfo* info = GetShareTargetInfo(share_target_id);
OutgoingShareTargetInfo* info =
GetOutgoingShareTargetInfo(share_target_id);
if (!info) {
NL_LOG(WARNING)
<< __func__
<< ": Failed to send attachments. Unknown ShareTarget.";
std::move(status_codes_callback)(StatusCodes::kError);
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
app_info_->SetActiveFlag();
ShareTarget share_target = info->share_target();
for (std::unique_ptr<Attachment>& attachment : attachments) {
attachment->MoveToShareTarget(share_target);
}
if (!share_target.has_attachments()) {
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
for (const FileAttachment& attachment : share_target.file_attachments) {
if (!attachment.file_path()) {
NL_LOG(WARNING) << __func__ << ": Got file attachment without path";
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
}
app_info_->SetActiveFlag();
// Set session ID.
info->set_session_id(analytics_recorder_->GenerateNextId());
info->set_share_target(share_target);
@@ -710,7 +723,7 @@ void NearbySharingServiceImpl::SendAttachments(
.set_status(TransferMetadata::Status::kConnecting)
.build());
CreatePayloads(std::move(share_target),
CreatePayloads(*info,
[this, endpoint_info = std::move(*endpoint_info)](
ShareTarget share_target, bool success) {
// Log analytics event of describing attachments.
@@ -737,9 +750,15 @@ void NearbySharingServiceImpl::Accept(
ResponseToIntroduction::ACCEPT_INTRODUCTION, receiving_session_id_);
ShareTargetInfo* info = GetShareTargetInfo(share_target_id);
if (!info || !info->connection()) {
if (info == nullptr) {
NL_LOG(WARNING) << __func__
<< ": Accept invoked for unknown share target";
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
if (!info->connection()) {
NL_LOG(WARNING) << __func__
<< ": Accept invoked for unconnected share target";
std::move(status_codes_callback)(StatusCodes::kOutOfOrderApiCall);
return;
}
@@ -780,9 +799,15 @@ void NearbySharingServiceImpl::Reject(
ResponseToIntroduction::REJECT_INTRODUCTION, receiving_session_id_);
ShareTargetInfo* info = GetShareTargetInfo(share_target_id);
if (!info || !info->connection()) {
if (info == nullptr) {
NL_LOG(WARNING) << __func__
<< ": Reject invoked for unknown share target";
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
if (!info->connection()) {
NL_LOG(WARNING) << __func__
<< ": Reject invoked for unconnected share target";
std::move(status_codes_callback)(StatusCodes::kOutOfOrderApiCall);
return;
}
@@ -835,11 +860,10 @@ void NearbySharingServiceImpl::DoCancel(
std::function<void(StatusCodes status_codes)> status_codes_callback,
bool is_initiator_of_cancellation) {
ShareTargetInfo* info = GetShareTargetInfo(share_target_id);
if (!info) {
NL_LOG(ERROR) << __func__
<< ": Cancel invoked for unknown share target, returning "
"kOutOfOrderApiCall";
std::move(status_codes_callback)(StatusCodes::kOutOfOrderApiCall);
if (info == nullptr) {
NL_LOG(WARNING) << __func__
<< ": Cancel invoked for unknown share target";
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
@@ -2752,23 +2776,19 @@ void NearbySharingServiceImpl::SendIntroduction(
}
void NearbySharingServiceImpl::CreatePayloads(
ShareTarget share_target, std::function<void(ShareTarget, bool)> callback) {
OutgoingShareTargetInfo* info = GetOutgoingShareTargetInfo(share_target.id);
if (!info || !share_target.has_attachments()) {
std::move(callback)(std::move(share_target), /*success=*/false);
return;
}
if (!info->file_payloads().empty() || !info->text_payloads().empty() ||
!info->wifi_credentials_payloads().empty()) {
OutgoingShareTargetInfo& info,
std::function<void(ShareTarget, bool)> callback) {
ShareTarget share_target = info.share_target();
if (!info.file_payloads().empty() || !info.text_payloads().empty() ||
!info.wifi_credentials_payloads().empty()) {
// We may have already created the payloads in the case of retry, so we can
// skip this step.
std::move(callback)(std::move(share_target), /*success=*/false);
return;
}
info->set_text_payloads(CreateTextPayloads(share_target.text_attachments));
info->set_wifi_credentials_payloads(
info.set_text_payloads(CreateTextPayloads(share_target.text_attachments));
info.set_wifi_credentials_payloads(
CreateWifiCredentialsPayloads(share_target.wifi_credentials_attachments));
if (share_target.file_attachments.empty()) {
std::move(callback)(std::move(share_target), /*success=*/true);
@@ -2776,12 +2796,8 @@ void NearbySharingServiceImpl::CreatePayloads(
}
std::vector<std::filesystem::path> file_paths;
file_paths.reserve(share_target.file_attachments.size());
for (const FileAttachment& attachment : share_target.file_attachments) {
if (!attachment.file_path()) {
NL_LOG(WARNING) << __func__ << ": Got file attachment without path";
std::move(callback)(std::move(share_target), /*success=*/false);
return;
}
file_paths.push_back(*attachment.file_path());
}
+1 -1
View File
@@ -319,7 +319,7 @@ class NearbySharingServiceImpl
void SendIntroduction(const ShareTarget& share_target,
std::optional<std::string> four_digit_token);
void CreatePayloads(ShareTarget share_target,
void CreatePayloads(OutgoingShareTargetInfo& info,
std::function<void(ShareTarget, bool)> callback);
void OnCreatePayloads(std::vector<uint8_t> endpoint_info,
ShareTarget share_target, bool success);
+7 -7
View File
@@ -1386,7 +1386,7 @@ TEST_F(NearbySharingServiceImplTest, StartFastInitiationAdvertising) {
// not called again.
EXPECT_EQ(RegisterSendSurface(&transfer_callback, &discovery_callback,
SendSurfaceState::kForeground),
NearbySharingService::StatusCodes::kError);
NearbySharingService::StatusCodes::kInvalidArgument);
EXPECT_EQ(fast_initiation->StartAdvertisingCount(), 1);
}
@@ -1627,7 +1627,7 @@ TEST_F(NearbySharingServiceImplTest,
EXPECT_EQ(RegisterSendSurface(&transfer_callback, &discovery_callback,
SendSurfaceState::kForeground),
NearbySharingService::StatusCodes::kError);
NearbySharingService::StatusCodes::kInvalidArgument);
EXPECT_TRUE(fake_nearby_connections_manager_->IsDiscovering());
}
@@ -1673,7 +1673,7 @@ TEST_F(NearbySharingServiceImplTest,
EXPECT_EQ(RegisterSendSurface(&transfer_callback, &discovery_callback,
SendSurfaceState::kBackground),
NearbySharingService::StatusCodes::kError);
NearbySharingService::StatusCodes::kInvalidArgument);
EXPECT_TRUE(fake_nearby_connections_manager_->IsDiscovering());
}
@@ -2779,7 +2779,7 @@ TEST_F(NearbySharingServiceImplTest, AcceptInvalidShareTarget) {
service_->Accept(
share_target.id, [&](NearbySharingServiceImpl::StatusCodes status_code) {
EXPECT_EQ(status_code,
NearbySharingServiceImpl::StatusCodes::kOutOfOrderApiCall);
NearbySharingServiceImpl::StatusCodes::kInvalidArgument);
notification.Notify();
});
@@ -3065,7 +3065,7 @@ TEST_F(NearbySharingServiceImplTest, RejectInvalidShareTarget) {
service_->Reject(
share_target.id, [&](NearbySharingServiceImpl::StatusCodes status_code) {
EXPECT_EQ(status_code,
NearbySharingServiceImpl::StatusCodes::kOutOfOrderApiCall);
NearbySharingServiceImpl::StatusCodes::kInvalidArgument);
notification.Notify();
});
@@ -3319,7 +3319,7 @@ TEST_F(NearbySharingServiceImplTest, SendAttachmentsWithoutAttachments) {
DiscoverShareTarget(transfer_callback, discovery_callback);
EXPECT_EQ(SendAttachments(target, /*attachments=*/{}),
NearbySharingServiceImpl::StatusCodes::kError);
NearbySharingServiceImpl::StatusCodes::kInvalidArgument);
UnregisterSendSurface(&transfer_callback, &discovery_callback);
}
@@ -3386,7 +3386,7 @@ TEST_F(NearbySharingServiceImplTest, SendTextUnknownTarget) {
ShareTarget target;
EXPECT_EQ(SendAttachments(target, CreateTextAttachments({kTextPayload})),
NearbySharingServiceImpl::StatusCodes::kError);
NearbySharingServiceImpl::StatusCodes::kInvalidArgument);
UnregisterSendSurface(&transfer_callback, &discovery_callback);
}
+2 -1
View File
@@ -44,9 +44,10 @@ std::vector<StatusCodeToStringData> GetTestData() {
"kNoAvailableConnectionMedium"},
{StatusCodes::kIrrecoverableHardwareError,
"kIrrecoverableHardwareError"},
{StatusCodes::kInvalidArgument, "kInvalidArgument"},
// If entries are added, kMaxValue and
// NearbySharingService::StatusCodeToString should be updated.
{StatusCodes::kMaxValue, "kIrrecoverableHardwareError"},
{StatusCodes::kMaxValue, "kInvalidArgument"},
});
return *kStatusCodeToStringData;