Change SendAttachements to accept an AttachmentContainer.

PiperOrigin-RevId: 639874033
This commit is contained in:
Francis Tsui
2024-06-03 12:28:34 -07:00
committed by Copybara-Service
parent d073073593
commit 911d55ff63
13 changed files with 39 additions and 78 deletions
-4
View File
@@ -23,8 +23,6 @@
namespace nearby {
namespace sharing {
class AttachmentContainer;
// A single attachment to be sent by / received from a ShareTarget, can be
// either a file or text.
class Attachment {
@@ -65,8 +63,6 @@ class Attachment {
int32_t batch_id() const { return batch_id_; }
SourceType source_type() const { return source_type_; }
// Move the attachment to a container
virtual void MoveToContainer(AttachmentContainer& container) = 0;
virtual absl::string_view GetDescription() const = 0;
virtual ShareType GetShareType() const = 0;
+1 -3
View File
@@ -18,12 +18,10 @@
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "absl/strings/string_view.h"
#include "internal/base/observer_list.h"
#include "sharing/advertisement.h"
#include "sharing/attachment.h"
#include "sharing/attachment_container.h"
#include "sharing/internal/api/sharing_rpc_notifier.h"
#include "sharing/local_device_data/nearby_share_local_device_data_manager.h"
@@ -137,7 +135,7 @@ bool FakeNearbySharingService::IsScanning() const { return false; }
// Sends |attachments| to the remote |share_target|.
void FakeNearbySharingService::SendAttachments(
int64_t share_target_id,
std::vector<std::unique_ptr<Attachment>> attachments,
std::unique_ptr<AttachmentContainer> attachment_container,
std::function<void(StatusCodes)> status_codes_callback) {
status_codes_callback(StatusCodes::kOk);
}
+1 -3
View File
@@ -19,11 +19,9 @@
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "absl/strings/string_view.h"
#include "internal/base/observer_list.h"
#include "sharing/attachment.h"
#include "sharing/attachment_container.h"
#include "sharing/internal/api/sharing_rpc_notifier.h"
#include "sharing/local_device_data/nearby_share_local_device_data_manager.h"
@@ -99,7 +97,7 @@ class FakeNearbySharingService : public NearbySharingService {
// Sends |attachments| to the remote |share_target|.
void SendAttachments(
int64_t share_target_id,
std::vector<std::unique_ptr<Attachment>> attachments,
std::unique_ptr<AttachmentContainer> attachment_container,
std::function<void(StatusCodes)> status_codes_callback) override;
// Accepts incoming share from the remote |share_target|.
-5
View File
@@ -23,7 +23,6 @@
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "sharing/attachment.h"
#include "sharing/attachment_container.h"
#include "sharing/common/compatible_u8_string.h"
#include "sharing/common/nearby_share_enums.h"
#include "sharing/internal/base/mime.h"
@@ -78,10 +77,6 @@ FileAttachment::FileAttachment(int64_t id, int64_t size, std::string file_name,
type_(type),
parent_folder_(std::move(parent_folder)) {}
void FileAttachment::MoveToContainer(AttachmentContainer& container) {
container.AddFileAttachment(std::move(*this));
}
absl::string_view FileAttachment::GetDescription() const { return file_name_; }
ShareType FileAttachment::GetShareType() const {
-3
View File
@@ -31,8 +31,6 @@ namespace sharing {
// A single attachment to be sent by / received from a |ShareTarget|, can be
// either a file or text.
class AttachmentContainer;
class FileAttachment : public Attachment {
public:
using Type = nearby::sharing::service::proto::FileMetadata::Type;
@@ -59,7 +57,6 @@ class FileAttachment : public Attachment {
}
// Attachment:
void MoveToContainer(AttachmentContainer& container) override;
absl::string_view GetDescription() const override;
ShareType GetShareType() const override;
+2 -3
View File
@@ -19,12 +19,11 @@
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "absl/strings/string_view.h"
#include "internal/network/url.h"
#include "sharing/advertisement.h"
#include "sharing/attachment.h"
#include "sharing/attachment_container.h"
#include "sharing/internal/api/sharing_rpc_notifier.h"
#include "sharing/local_device_data/nearby_share_local_device_data_manager.h"
#include "sharing/nearby_sharing_settings.h"
@@ -203,7 +202,7 @@ class NearbySharingService {
// Sends |attachments| to the remote |share_target|.
virtual void SendAttachments(
int64_t share_target_id,
std::vector<std::unique_ptr<Attachment>> attachments,
std::unique_ptr<AttachmentContainer> attachment_container,
std::function<void(StatusCodes)> status_codes_callback) = 0;
// Accepts incoming share from the remote |share_target|.
+13 -19
View File
@@ -665,11 +665,12 @@ std::string NearbySharingServiceImpl::GetQrCodeUrl() const {
void NearbySharingServiceImpl::SendAttachments(
int64_t share_target_id,
std::vector<std::unique_ptr<Attachment>> attachments,
std::unique_ptr<AttachmentContainer> attachment_container,
std::function<void(StatusCodes)> status_codes_callback) {
RunOnNearbySharingServiceThread(
"api_send_attachments",
[this, share_target_id, attachments = std::move(attachments),
[this, share_target_id,
attachment_container = std::move(attachment_container),
status_codes_callback = std::move(status_codes_callback)]() mutable {
if (!is_scanning_) {
NL_LOG(WARNING) << __func__
@@ -684,11 +685,19 @@ void NearbySharingServiceImpl::SendAttachments(
// |is_scanning_| and |is_transferring_| are mutually exclusive.
NL_DCHECK(!is_transferring_);
if (attachments.empty()) {
if (!attachment_container || !attachment_container->HasAttachments()) {
NL_LOG(WARNING) << __func__ << ": No attachments to send.";
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
for (const FileAttachment& attachment :
attachment_container->GetFileAttachments()) {
if (!attachment.file_path()) {
NL_LOG(WARNING) << __func__ << ": Got file attachment without path";
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
}
// Outgoing connections always announces with contacts visibility.
std::optional<std::vector<uint8_t>> endpoint_info =
CreateEndpointInfo(DeviceVisibility::DEVICE_VISIBILITY_ALL_CONTACTS,
@@ -711,22 +720,7 @@ void NearbySharingServiceImpl::SendAttachments(
}
ShareTarget share_target = info->share_target();
AttachmentContainer& container = share_target.attachment_container;
for (std::unique_ptr<Attachment>& attachment : attachments) {
attachment->MoveToContainer(container);
}
if (!container.HasAttachments()) {
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
for (const FileAttachment& attachment :
container.GetFileAttachments()) {
if (!attachment.file_path()) {
NL_LOG(WARNING) << __func__ << ": Got file attachment without path";
std::move(status_codes_callback)(StatusCodes::kInvalidArgument);
return;
}
}
share_target.attachment_container = std::move(*attachment_container);
app_info_->SetActiveFlag();
// Set session ID.
+2 -1
View File
@@ -44,6 +44,7 @@
#include "sharing/advertisement.h"
#include "sharing/analytics/analytics_recorder.h"
#include "sharing/attachment.h"
#include "sharing/attachment_container.h"
#include "sharing/attachment_info.h"
#include "sharing/certificates/nearby_share_certificate_manager.h"
#include "sharing/certificates/nearby_share_decrypted_public_certificate.h"
@@ -155,7 +156,7 @@ class NearbySharingServiceImpl
std::string GetQrCodeUrl() const override;
void SendAttachments(
int64_t share_target_id,
std::vector<std::unique_ptr<Attachment>> attachments,
std::unique_ptr<AttachmentContainer> attachment_container,
std::function<void(StatusCodes)> status_codes_callback) override;
void Accept(int64_t share_target_id,
std::function<void(StatusCodes status_codes)>
+20 -20
View File
@@ -46,7 +46,7 @@
#include "internal/test/fake_device_info.h"
#include "internal/test/fake_task_runner.h"
#include "sharing/advertisement.h"
#include "sharing/attachment.h"
#include "sharing/attachment_container.h"
#include "sharing/certificates/fake_nearby_share_certificate_manager.h"
#include "sharing/certificates/nearby_share_certificate_manager_impl.h"
#include "sharing/certificates/nearby_share_decrypted_public_certificate.h"
@@ -333,35 +333,35 @@ std::unique_ptr<Frame> GetCancelFrame() {
return std::unique_ptr<Frame>(frame);
}
std::vector<std::unique_ptr<Attachment>> CreateTextAttachments(
std::unique_ptr<AttachmentContainer> CreateTextAttachments(
std::vector<std::string> texts) {
std::vector<std::unique_ptr<Attachment>> attachments;
auto attachment_container = std::make_unique<AttachmentContainer>();
for (auto& text : texts) {
attachments.push_back(std::make_unique<TextAttachment>(
service::proto::TextMetadata::TEXT, std::move(text),
/*text_title=*/std::nullopt,
/*mime_type=*/std::nullopt));
attachment_container->AddTextAttachment(
TextAttachment(service::proto::TextMetadata::TEXT, std::move(text),
/*text_title=*/std::nullopt,
/*mime_type=*/std::nullopt));
}
return attachments;
return attachment_container;
}
std::vector<std::unique_ptr<Attachment>> CreateFileAttachments(
std::unique_ptr<AttachmentContainer> CreateFileAttachments(
std::vector<std::filesystem::path> file_paths) {
std::vector<std::unique_ptr<Attachment>> attachments;
auto attachment_container = std::make_unique<AttachmentContainer>();
for (auto& file_path : file_paths) {
attachments.push_back(
std::make_unique<FileAttachment>(std::move(file_path)));
attachment_container->AddFileAttachment(
FileAttachment(std::move(file_path)));
}
return attachments;
return attachment_container;
}
std::vector<std::unique_ptr<Attachment>> CreateWifiCredentialAttachments(
std::unique_ptr<AttachmentContainer> CreateWifiCredentialAttachments(
std::string ssid, std::string password) {
std::vector<std::unique_ptr<Attachment>> attachments;
attachments.push_back(std::make_unique<WifiCredentialsAttachment>(
auto attachment_container = std::make_unique<AttachmentContainer>();
attachment_container->AddWifiCredentialsAttachment(WifiCredentialsAttachment(
std::move(ssid), service::proto::WifiCredentialsMetadata::WPA_PSK,
std::move(password)));
return attachments;
return attachment_container;
}
class NearbySharingServiceImplTest : public testing::Test {
@@ -562,12 +562,12 @@ class NearbySharingServiceImplTest : public testing::Test {
NearbySharingService::StatusCodes SendAttachments(
const ShareTarget& share_target,
std::vector<std::unique_ptr<Attachment>> attachments) {
std::unique_ptr<AttachmentContainer> attachment_container) {
NearbySharingService::StatusCodes result =
NearbySharingService::StatusCodes::kError;
absl::Notification notification;
service_->SendAttachments(
share_target.id, std::move(attachments),
share_target.id, std::move(attachment_container),
[&](NearbySharingService::StatusCodes status_codes) {
result = status_codes;
notification.Notify();
@@ -3431,7 +3431,7 @@ TEST_F(NearbySharingServiceImplTest, SendAttachmentsWithoutAttachments) {
ShareTarget target =
DiscoverShareTarget(transfer_callback, discovery_callback);
EXPECT_EQ(SendAttachments(target, /*attachments=*/{}),
EXPECT_EQ(SendAttachments(target, /*attachment_container=*/nullptr),
NearbySharingServiceImpl::StatusCodes::kInvalidArgument);
UnregisterSendSurface(&transfer_callback, &discovery_callback);
-5
View File
@@ -27,7 +27,6 @@
#include "absl/strings/string_view.h"
#include "internal/network/url.h"
#include "sharing/attachment.h"
#include "sharing/attachment_container.h"
#include "sharing/common/nearby_share_enums.h"
#include "sharing/proto/wire_format.pb.h"
@@ -146,10 +145,6 @@ TextAttachment::TextAttachment(int64_t id, Type type, std::string text_body,
text_body_(std::move(text_body)),
mime_type_(std::move(mime_type)) {}
void TextAttachment::MoveToContainer(AttachmentContainer& container) {
container.AddTextAttachment(std::move(*this));
}
absl::string_view TextAttachment::GetDescription() const { return text_title_; }
ShareType TextAttachment::GetShareType() const {
-3
View File
@@ -28,8 +28,6 @@ namespace nearby {
namespace sharing {
// Represents a text attachment.
class AttachmentContainer;
class TextAttachment : public Attachment {
public:
using Type = nearby::sharing::service::proto::TextMetadata::Type;
@@ -55,7 +53,6 @@ class TextAttachment : public Attachment {
Type type() const { return type_; }
// Attachment:
void MoveToContainer(AttachmentContainer& container) override;
absl::string_view GetDescription() const override;
ShareType GetShareType() const override;
-6
View File
@@ -20,7 +20,6 @@
#include "absl/strings/string_view.h"
#include "sharing/attachment.h"
#include "sharing/attachment_container.h"
#include "sharing/common/nearby_share_enums.h"
namespace nearby {
@@ -47,11 +46,6 @@ WifiCredentialsAttachment::WifiCredentialsAttachment(
password_(std::move(password)),
is_hidden_(is_hidden) {}
void WifiCredentialsAttachment::MoveToContainer(
AttachmentContainer& container) {
container.AddWifiCredentialsAttachment(std::move(*this));
}
absl::string_view WifiCredentialsAttachment::GetDescription() const {
return ssid_;
}
-3
View File
@@ -27,8 +27,6 @@ namespace nearby {
namespace sharing {
// Represents a WiFi credentials attachment.
class AttachmentContainer;
class WifiCredentialsAttachment : public Attachment {
public:
using SecurityType =
@@ -56,7 +54,6 @@ class WifiCredentialsAttachment : public Attachment {
bool is_hidden() const { return is_hidden_; }
// Attachment:
void MoveToContainer(AttachmentContainer& container) override;
absl::string_view GetDescription() const override;
ShareType GetShareType() const override;