Cleanup Attachments and AttachmentContainer.

PiperOrigin-RevId: 839803045
This commit is contained in:
Francis Tsui
2025-12-03 10:06:33 -08:00
committed by Copybara-Service
parent d4ecf8a23c
commit 75d260dce4
20 changed files with 380 additions and 285 deletions
+2
View File
@@ -61,6 +61,8 @@ cc_library(
"//sharing/common:enum",
"//sharing/internal/base",
"//sharing/proto:wire_format_cc_proto",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/random",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
+28 -28
View File
@@ -16,6 +16,7 @@
#include <stdint.h>
#include <memory>
#include <optional>
#include <string>
@@ -38,9 +39,7 @@
#include "sharing/share_target.h"
#include "sharing/text_attachment.h"
namespace nearby {
namespace sharing {
namespace analytics {
namespace nearby::sharing::analytics {
namespace {
using ::location::nearby::proto::sharing::EventCategory;
@@ -280,29 +279,32 @@ TEST_F(AnalyticsRecorderTest, NewDescribeAttachments) {
SharingLog::FileAttachment::DOCUMENT);
});
AttachmentContainer attachments(
{TextAttachment(5, service::proto::TextMetadata::TEXT,
std::string(kTextBody), kTextBody.size()),
TextAttachment(6, service::proto::TextMetadata::PHONE_NUMBER,
std::string(kTextBody), kTextBody.size()),
TextAttachment(7, service::proto::TextMetadata::URL,
std::string(kTextBody), kTextBody.size()),
TextAttachment(8, service::proto::TextMetadata::ADDRESS,
std::string(kTextBody), kTextBody.size()),
TextAttachment(9, service::proto::TextMetadata::UNKNOWN,
std::string(kTextBody), kTextBody.size())},
{FileAttachment(1, 2, std::string(kFileName), "",
service::proto::FileMetadata::IMAGE),
FileAttachment(2, 3, std::string(kFileDocumentName),
std::string(kFileMimeType),
service::proto::FileMetadata::DOCUMENT),
FileAttachment(3, 4, std::string(kFileName), "",
service::proto::FileMetadata::AUDIO),
FileAttachment(4, 5, std::string(kFileName), std::string(kTextMimeType),
service::proto::FileMetadata::DOCUMENT)},
{});
std::unique_ptr<AttachmentContainer> attachments =
AttachmentContainer::Builder(
{TextAttachment(5, service::proto::TextMetadata::TEXT,
std::string(kTextBody), kTextBody.size()),
TextAttachment(6, service::proto::TextMetadata::PHONE_NUMBER,
std::string(kTextBody), kTextBody.size()),
TextAttachment(7, service::proto::TextMetadata::URL,
std::string(kTextBody), kTextBody.size()),
TextAttachment(8, service::proto::TextMetadata::ADDRESS,
std::string(kTextBody), kTextBody.size()),
TextAttachment(9, service::proto::TextMetadata::UNKNOWN,
std::string(kTextBody), kTextBody.size())},
{FileAttachment(1, 2, std::string(kFileName), "",
service::proto::FileMetadata::IMAGE),
FileAttachment(2, 3, std::string(kFileDocumentName),
std::string(kFileMimeType),
service::proto::FileMetadata::DOCUMENT),
FileAttachment(3, 4, std::string(kFileName), "",
service::proto::FileMetadata::AUDIO),
FileAttachment(4, 5, std::string(kFileName),
std::string(kTextMimeType),
service::proto::FileMetadata::DOCUMENT)},
{})
.Build();
analytics_recoder().NewDescribeAttachments(attachments);
analytics_recoder().NewDescribeAttachments(*attachments);
}
TEST_F(AnalyticsRecorderTest, EmptyDescribeAttachments) {
@@ -896,6 +898,4 @@ TEST_F(AnalyticsRecorderTest, GenerateID) {
}
} // namespace
} // namespace analytics
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing::analytics
+5 -22
View File
@@ -20,8 +20,7 @@
#include "absl/random/random.h"
#include "proto/sharing_enums.pb.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
namespace {
using ::location::nearby::proto::sharing::AttachmentSourceType;
@@ -33,32 +32,16 @@ int64_t CreateRandomId() {
} // namespace
// TODO(b/258690183): Add unit tests for Attachment with same and different ids
Attachment::Attachment(Attachment::Family family, int64_t size,
int32_t batch_id, AttachmentSourceType source_type)
: id_(CreateRandomId()),
family_(family),
size_(size),
batch_id_(batch_id),
source_type_(source_type) {}
: Attachment(/*id=*/0, family, size, batch_id, source_type) {}
Attachment::Attachment(int64_t id, Attachment::Family family, int64_t size,
int32_t batch_id, AttachmentSourceType source_type)
: id_(id == 0 ? CreateRandomId() : id),
family_(family),
size_(size),
batch_id_(batch_id),
source_type_(source_type) {}
source_type_(source_type),
size_(size) {}
Attachment::Attachment(const Attachment&) = default;
Attachment::Attachment(Attachment&&) = default;
Attachment& Attachment::operator=(const Attachment&) = default;
Attachment& Attachment::operator=(Attachment&&) = default;
Attachment::~Attachment() = default;
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
+7 -13
View File
@@ -21,8 +21,7 @@
#include "proto/sharing_enums.pb.h"
#include "sharing/common/nearby_share_enums.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
// A single attachment to be sent by / received from a ShareTarget, can be
// either a file or text.
@@ -41,11 +40,7 @@ class Attachment {
Attachment(
int64_t id, Family family, int64_t size, int32_t batch_id,
location::nearby::proto::sharing::AttachmentSourceType source_type);
Attachment(const Attachment&);
Attachment(Attachment&&);
Attachment& operator=(const Attachment&);
Attachment& operator=(Attachment&&);
virtual ~Attachment();
virtual ~Attachment() = default;
int64_t id() const { return id_; }
Family family() const { return family_; }
@@ -60,14 +55,13 @@ class Attachment {
virtual ShareType GetShareType() const = 0;
private:
int64_t id_;
Family family_;
const int64_t id_;
const Family family_;
const int32_t batch_id_;
const location::nearby::proto::sharing::AttachmentSourceType source_type_;
int64_t size_;
int32_t batch_id_;
location::nearby::proto::sharing::AttachmentSourceType source_type_;
};
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
#endif // THIRD_PARTY_NEARBY_SHARING_ATTACHMENT_H_
+69 -5
View File
@@ -14,23 +14,81 @@
#include "sharing/attachment_container.h"
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "absl/memory/memory.h"
#include "sharing/file_attachment.h"
#include "sharing/text_attachment.h"
#include "sharing/wifi_credentials_attachment.h"
namespace nearby::sharing {
AttachmentContainer::Builder&
AttachmentContainer::Builder::ReserveAttachmentsCount(
int text_attachments_count, int file_attachments_count,
int wifi_credentials_attachments_count) {
text_attachments_.reserve(text_attachments_count);
file_attachments_.reserve(file_attachments_count);
wifi_credentials_attachments_.reserve(wifi_credentials_attachments_count);
return *this;
}
AttachmentContainer::Builder& AttachmentContainer::Builder::AddTextAttachment(
TextAttachment text_attachment) {
text_attachments_.push_back(std::move(text_attachment));
return *this;
}
AttachmentContainer::Builder& AttachmentContainer::Builder::AddFileAttachment(
FileAttachment file_attachment) {
file_attachments_.push_back(std::move(file_attachment));
return *this;
}
AttachmentContainer::Builder&
AttachmentContainer::Builder::AddWifiCredentialsAttachment(
WifiCredentialsAttachment wifi_credentials_attachment) {
wifi_credentials_attachments_.push_back(
std::move(wifi_credentials_attachment));
return *this;
}
std::unique_ptr<AttachmentContainer> AttachmentContainer::Builder::Build() {
return absl::WrapUnique(new AttachmentContainer(
std::move(text_attachments_), std::move(file_attachments_),
std::move(wifi_credentials_attachments_)));
}
AttachmentContainer::AttachmentContainer(
std::vector<TextAttachment> text_attachments,
std::vector<FileAttachment> file_attachments,
std::vector<WifiCredentialsAttachment> wifi_credentials_attachments)
: text_attachments_(std::move(text_attachments)),
file_attachments_(std::move(file_attachments)),
wifi_credentials_attachments_(std::move(wifi_credentials_attachments)) {}
wifi_credentials_attachments_(std::move(wifi_credentials_attachments)) {
BuildIndex();
}
AttachmentContainer::AttachmentContainer(AttachmentContainer&& other) {
text_attachments_ = std::move(other.text_attachments_);
file_attachments_ = std::move(other.file_attachments_);
wifi_credentials_attachments_ =
std::move(other.wifi_credentials_attachments_);
BuildIndex();
}
AttachmentContainer& AttachmentContainer::operator=(
AttachmentContainer&& other) {
text_attachments_ = std::move(other.text_attachments_);
file_attachments_ = std::move(other.file_attachments_);
wifi_credentials_attachments_ =
std::move(other.wifi_credentials_attachments_);
BuildIndex();
return *this;
}
int64_t AttachmentContainer::GetTotalAttachmentsSize() const {
int64_t size_in_bytes = 0;
@@ -77,10 +135,16 @@ void AttachmentContainer::ClearAttachments() {
}
}
void AttachmentContainer::Clear() {
file_attachments_.clear();
text_attachments_.clear();
wifi_credentials_attachments_.clear();
void AttachmentContainer::BuildIndex() {
for (const auto& text : text_attachments_) {
attachment_id_map_[text.id()] = &text;
}
for (const auto& file : file_attachments_) {
attachment_id_map_[file.id()] = &file;
}
for (const auto& wifi_credentials : wifi_credentials_attachments_) {
attachment_id_map_[wifi_credentials.id()] = &wifi_credentials;
}
}
} // namespace nearby::sharing
+52 -23
View File
@@ -16,8 +16,11 @@
#define THIRD_PARTY_NEARBY_SHARING_ATTACHMENT_CONTAINER_H_
#include <cstdint>
#include <memory>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "sharing/attachment.h"
#include "sharing/file_attachment.h"
#include "sharing/text_attachment.h"
#include "sharing/wifi_credentials_attachment.h"
@@ -28,15 +31,40 @@ namespace nearby::sharing {
// This class is thread-compatible (go/thread-compatible).
class AttachmentContainer {
public:
AttachmentContainer(
std::vector<TextAttachment> text_attachments,
std::vector<FileAttachment> file_attachments,
std::vector<WifiCredentialsAttachment> wifi_credentials_attachments);
class Builder {
public:
Builder() = default;
Builder(std::vector<TextAttachment> text_attachments,
std::vector<FileAttachment> file_attachments,
std::vector<WifiCredentialsAttachment> wifi_credentials_attachments)
: text_attachments_(std::move(text_attachments)),
file_attachments_(std::move(file_attachments)),
wifi_credentials_attachments_(
std::move(wifi_credentials_attachments)) {}
Builder& ReserveAttachmentsCount(int text_attachments_count,
int file_attachments_count,
int wifi_credentials_attachments_count);
Builder& AddTextAttachment(TextAttachment text_attachment);
Builder& AddFileAttachment(FileAttachment file_attachment);
Builder& AddWifiCredentialsAttachment(
WifiCredentialsAttachment wifi_credentials_attachment);
bool Empty() const {
return text_attachments_.empty() && file_attachments_.empty() &&
wifi_credentials_attachments_.empty();
}
std::unique_ptr<AttachmentContainer> Build();
private:
std::vector<TextAttachment> text_attachments_;
std::vector<FileAttachment> file_attachments_;
std::vector<WifiCredentialsAttachment> wifi_credentials_attachments_;
};
AttachmentContainer() = default;
AttachmentContainer(const AttachmentContainer&) = default;
AttachmentContainer(AttachmentContainer&&) = default;
AttachmentContainer& operator=(const AttachmentContainer&) = default;
AttachmentContainer& operator=(AttachmentContainer&&) = default;
AttachmentContainer(AttachmentContainer&&);
AttachmentContainer& operator=(AttachmentContainer&&);
~AttachmentContainer() = default;
const std::vector<TextAttachment>& GetTextAttachments() const {
@@ -50,18 +78,6 @@ class AttachmentContainer {
return wifi_credentials_attachments_;
}
void AddTextAttachment(TextAttachment text_attachment) {
text_attachments_.push_back(std::move(text_attachment));
}
void AddFileAttachment(FileAttachment file_attachment) {
file_attachments_.push_back(std::move(file_attachment));
}
void AddWifiCredentialsAttachment(
WifiCredentialsAttachment wifi_credentials_attachment) {
wifi_credentials_attachments_.push_back(
std::move(wifi_credentials_attachment));
}
TextAttachment& GetMutableTextAttachment(int index) {
return text_attachments_[index];
}
@@ -74,6 +90,14 @@ class AttachmentContainer {
return wifi_credentials_attachments_[index];
}
const Attachment* GetAttachment(int64_t id) const {
const auto it = attachment_id_map_.find(id);
if (it == attachment_id_map_.end()) {
return nullptr;
}
return it->second;
}
// Returns the total number of attachments of all types.
int GetAttachmentCount() const {
return text_attachments_.size() + file_attachments_.size() +
@@ -96,13 +120,18 @@ class AttachmentContainer {
// place.
void ClearAttachments();
// Delete all attachments.
void Clear();
private:
AttachmentContainer(
std::vector<TextAttachment> text_attachments,
std::vector<FileAttachment> file_attachments,
std::vector<WifiCredentialsAttachment> wifi_credentials_attachments);
// Build id to attachment index.
void BuildIndex();
std::vector<TextAttachment> text_attachments_;
std::vector<FileAttachment> file_attachments_;
std::vector<WifiCredentialsAttachment> wifi_credentials_attachments_;
absl::flat_hash_map<int64_t, const Attachment*> attachment_id_map_;
};
} // namespace nearby::sharing
+83 -72
View File
@@ -15,6 +15,7 @@
#include "sharing/attachment_container.h"
#include <cstdint>
#include <memory>
#include <optional>
#include <vector>
@@ -72,132 +73,142 @@ class AttachmentContainerTest : public ::testing::Test {
};
TEST_F(AttachmentContainerTest, Constructor) {
AttachmentContainer container(std::vector<TextAttachment>{text1_, text2_},
std::vector<FileAttachment>{file1_},
std::vector<WifiCredentialsAttachment>{wifi1_});
std::unique_ptr<AttachmentContainer> container =
AttachmentContainer::Builder()
.AddTextAttachment(text1_)
.AddTextAttachment(text2_)
.AddFileAttachment(file1_)
.AddWifiCredentialsAttachment(wifi1_)
.Build();
EXPECT_THAT(container.GetTextAttachments(),
EXPECT_THAT(container->GetTextAttachments(),
UnorderedElementsAre(text1_, text2_));
EXPECT_THAT(container.GetFileAttachments(), UnorderedElementsAre(file1_));
EXPECT_THAT(container.GetWifiCredentialsAttachments(),
EXPECT_THAT(container->GetFileAttachments(), UnorderedElementsAre(file1_));
EXPECT_THAT(container->GetWifiCredentialsAttachments(),
UnorderedElementsAre(wifi1_));
}
TEST_F(AttachmentContainerTest, AddTextAttachment) {
AttachmentContainer container;
std::unique_ptr<AttachmentContainer> container =
AttachmentContainer::Builder()
.AddTextAttachment(text1_)
.AddTextAttachment(text2_)
.Build();
container.AddTextAttachment(text1_);
container.AddTextAttachment(text2_);
EXPECT_THAT(container.GetTextAttachments(),
EXPECT_THAT(container->GetTextAttachments(),
UnorderedElementsAre(text1_, text2_));
}
TEST_F(AttachmentContainerTest, AddFileAttachment) {
AttachmentContainer container;
std::unique_ptr<AttachmentContainer> container =
AttachmentContainer::Builder().AddFileAttachment(file1_).Build();
container.AddFileAttachment(file1_);
EXPECT_THAT(container.GetFileAttachments(), UnorderedElementsAre(file1_));
EXPECT_THAT(container->GetFileAttachments(), UnorderedElementsAre(file1_));
}
TEST_F(AttachmentContainerTest, AddWifiCredentialsAttachment) {
AttachmentContainer container;
std::unique_ptr<AttachmentContainer> container =
AttachmentContainer::Builder()
.AddWifiCredentialsAttachment(wifi1_)
.Build();
container.AddWifiCredentialsAttachment(wifi1_);
EXPECT_THAT(container.GetWifiCredentialsAttachments(),
EXPECT_THAT(container->GetWifiCredentialsAttachments(),
UnorderedElementsAre(wifi1_));
}
TEST_F(AttachmentContainerTest, GetMutableTextAttachment) {
AttachmentContainer container;
std::unique_ptr<AttachmentContainer> container =
AttachmentContainer::Builder()
.AddTextAttachment(text1_)
.AddTextAttachment(text2_)
.Build();
container.AddTextAttachment(text1_);
container.AddTextAttachment(text2_);
EXPECT_THAT(container.GetMutableTextAttachment(0), Eq(text1_));
EXPECT_THAT(container.GetMutableTextAttachment(1), Eq(text2_));
EXPECT_THAT(container->GetMutableTextAttachment(0), Eq(text1_));
EXPECT_THAT(container->GetMutableTextAttachment(1), Eq(text2_));
}
TEST_F(AttachmentContainerTest, GetMutableFileAttachment) {
AttachmentContainer container;
std::unique_ptr<AttachmentContainer> container =
AttachmentContainer::Builder().AddFileAttachment(file1_).Build();
container.AddFileAttachment(file1_);
EXPECT_THAT(container.GetMutableFileAttachment(0), Eq(file1_));
EXPECT_THAT(container->GetMutableFileAttachment(0), Eq(file1_));
}
TEST_F(AttachmentContainerTest, GetMutableWifiCredentialsAttachment) {
AttachmentContainer container;
std::unique_ptr<AttachmentContainer> container =
AttachmentContainer::Builder()
.AddWifiCredentialsAttachment(wifi1_)
.Build();
container.AddWifiCredentialsAttachment(wifi1_);
EXPECT_THAT(container.GetMutableWifiCredentialsAttachment(0), Eq(wifi1_));
EXPECT_THAT(container->GetMutableWifiCredentialsAttachment(0), Eq(wifi1_));
}
TEST_F(AttachmentContainerTest, AttachmentCount) {
AttachmentContainer container(std::vector<TextAttachment>{text1_, text2_},
std::vector<FileAttachment>{file1_},
std::vector<WifiCredentialsAttachment>{wifi1_});
std::unique_ptr<AttachmentContainer> container =
AttachmentContainer::Builder()
.AddTextAttachment(text1_)
.AddTextAttachment(text2_)
.AddFileAttachment(file1_)
.AddWifiCredentialsAttachment(wifi1_)
.Build();
EXPECT_THAT(container.GetAttachmentCount(), Eq(4));
EXPECT_THAT(container->GetAttachmentCount(), Eq(4));
}
TEST_F(AttachmentContainerTest, GetTotalAttachmentsSize) {
AttachmentContainer container(std::vector<TextAttachment>{text1_, text2_},
std::vector<FileAttachment>{file1_},
std::vector<WifiCredentialsAttachment>{});
std::unique_ptr<AttachmentContainer> container =
AttachmentContainer::Builder()
.AddTextAttachment(text1_)
.AddTextAttachment(text2_)
.AddFileAttachment(file1_)
.Build();
EXPECT_THAT(container.GetTotalAttachmentsSize(), Eq(18 + 20 + 100000));
EXPECT_THAT(container->GetTotalAttachmentsSize(), Eq(18 + 20 + 100000));
}
TEST_F(AttachmentContainerTest, HasAttachments) {
AttachmentContainer container;
AttachmentContainer::Builder builder = AttachmentContainer::Builder();
EXPECT_THAT(container.HasAttachments(), IsFalse());
EXPECT_THAT(builder.Empty(), IsTrue());
container.AddWifiCredentialsAttachment(wifi1_);
builder.AddWifiCredentialsAttachment(wifi1_);
EXPECT_THAT(container.HasAttachments(), IsTrue());
EXPECT_THAT(builder.Empty(), IsFalse());
}
TEST_F(AttachmentContainerTest, ClearAttachments) {
AttachmentContainer container(std::vector<TextAttachment>{text1_, text2_},
std::vector<FileAttachment>{file1_},
std::vector<WifiCredentialsAttachment>{wifi1_});
std::unique_ptr<AttachmentContainer> container =
AttachmentContainer::Builder()
.AddTextAttachment(text1_)
.AddTextAttachment(text2_)
.AddFileAttachment(file1_)
.AddWifiCredentialsAttachment(wifi1_)
.Build();
container.ClearAttachments();
container->ClearAttachments();
ASSERT_THAT(container.GetTextAttachments(), SizeIs(2));
EXPECT_THAT(container.GetTextAttachments()[0].text_body(), IsEmpty());
EXPECT_THAT(container.GetTextAttachments()[1].text_body(), IsEmpty());
ASSERT_THAT(container.GetFileAttachments(), SizeIs(1));
EXPECT_THAT(container.GetFileAttachments()[0].file_path(), Eq(std::nullopt));
ASSERT_THAT(container.GetWifiCredentialsAttachments(), SizeIs(1));
EXPECT_THAT(container.GetWifiCredentialsAttachments()[0].password(),
ASSERT_THAT(container->GetTextAttachments(), SizeIs(2));
EXPECT_THAT(container->GetTextAttachments()[0].text_body(), IsEmpty());
EXPECT_THAT(container->GetTextAttachments()[1].text_body(), IsEmpty());
ASSERT_THAT(container->GetFileAttachments(), SizeIs(1));
EXPECT_THAT(container->GetFileAttachments()[0].file_path(), Eq(std::nullopt));
ASSERT_THAT(container->GetWifiCredentialsAttachments(), SizeIs(1));
EXPECT_THAT(container->GetWifiCredentialsAttachments()[0].password(),
IsEmpty());
EXPECT_THAT(container.GetWifiCredentialsAttachments()[0].is_hidden(),
EXPECT_THAT(container->GetWifiCredentialsAttachments()[0].is_hidden(),
IsFalse());
}
TEST_F(AttachmentContainerTest, Clear) {
AttachmentContainer container(std::vector<TextAttachment>{text1_, text2_},
std::vector<FileAttachment>{file1_},
std::vector<WifiCredentialsAttachment>{wifi1_});
EXPECT_THAT(container.HasAttachments(), IsTrue());
container.Clear();
EXPECT_THAT(container.HasAttachments(), IsFalse());
}
TEST_F(AttachmentContainerTest, GetStorageSize) {
AttachmentContainer container(std::vector<TextAttachment>{text1_, text2_},
std::vector<FileAttachment>{file1_},
std::vector<WifiCredentialsAttachment>{wifi1_});
std::unique_ptr<AttachmentContainer> container =
AttachmentContainer::Builder()
.AddTextAttachment(text1_)
.AddTextAttachment(text2_)
.AddFileAttachment(file1_)
.AddWifiCredentialsAttachment(wifi1_)
.Build();
int64_t storage_size = container.GetStorageSize();
int64_t storage_size = container->GetStorageSize();
EXPECT_THAT(storage_size, Eq(file1_.size()));
}
+5 -8
View File
@@ -28,8 +28,7 @@
#include "sharing/internal/base/mime.h"
#include "sharing/proto/wire_format.pb.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
namespace {
using ::location::nearby::proto::sharing::AttachmentSourceType;
@@ -61,12 +60,11 @@ FileAttachment::FileAttachment(FilePath file_path, absl::string_view mime_type,
std::string parent_folder, int32_t batch_id,
AttachmentSourceType source_type)
: Attachment(Attachment::Family::kFile, /*size=*/0, batch_id, source_type),
file_name_(file_path.GetFileName().ToString()),
mime_type_(mime_type.empty() ? MimeTypeFromPath(file_path) : mime_type),
type_(FileAttachmentTypeFromMimeType(mime_type_)),
parent_folder_(std::move(parent_folder)) {
file_name_ = file_path.GetFileName().ToString();
file_path_ = std::move(file_path);
}
parent_folder_(std::move(parent_folder)),
file_path_(std::move(file_path)) {}
FileAttachment::FileAttachment(int64_t id, int64_t size, std::string file_name,
std::string mime_type, Type type,
@@ -108,5 +106,4 @@ ShareType FileAttachment::GetShareType() const {
}
}
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
+6 -12
View File
@@ -39,8 +39,7 @@
#include "sharing/common/nearby_share_enums.h"
#include "sharing/proto/wire_format.pb.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
// A single attachment to be sent by / received from a |ShareTarget|, can be
// either a file or text.
@@ -58,10 +57,6 @@ class FileAttachment : public Attachment {
Type type, std::string parent_folder = "", int32_t batch_id = 0,
location::nearby::proto::sharing::AttachmentSourceType source_type =
location::nearby::proto::sharing::ATTACHMENT_SOURCE_UNKNOWN);
FileAttachment(const FileAttachment&) = default;
FileAttachment(FileAttachment&&) = default;
FileAttachment& operator=(const FileAttachment&) = default;
FileAttachment& operator=(FileAttachment&&) = default;
~FileAttachment() override = default;
absl::string_view file_name() const { return file_name_; }
@@ -82,14 +77,13 @@ class FileAttachment : public Attachment {
private:
// File name should be in UTF8 format.
std::string file_name_;
std::string mime_type_;
Type type_;
const std::string file_name_;
const std::string mime_type_;
const Type type_;
const std::string parent_folder_;
std::optional<FilePath> file_path_;
std::string parent_folder_;
};
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
#endif // THIRD_PARTY_NEARBY_SHARING_FILE_ATTACHMENT_H_
+15 -8
View File
@@ -85,7 +85,15 @@ std::optional<TransferMetadata::Status>
IncomingShareSession::ProcessIntroduction(
const IntroductionFrame& introduction_frame) {
int64_t file_size_sum = 0;
AttachmentContainer& container = mutable_attachment_container();
int app_file_count = 0;
for (const AppMetadata& apk : introduction_frame.app_metadata()) {
app_file_count += apk.file_name_size();
}
AttachmentContainer::Builder builder;
builder.ReserveAttachmentsCount(
introduction_frame.text_metadata_size() + app_file_count,
introduction_frame.file_metadata_size(),
introduction_frame.wifi_credentials_metadata_size());
for (const auto& file : introduction_frame.file_metadata()) {
if (file.size() <= 0) {
LOG(WARNING) << "Ignore introduction, due to invalid attachment size";
@@ -97,7 +105,7 @@ IncomingShareSession::ProcessIntroduction(
<< ", payload_id=" << file.payload_id()
<< ", parent_folder=" << file.parent_folder()
<< ", mime_type=" << file.mime_type();
container.AddFileAttachment(
builder.AddFileAttachment(
FileAttachment(file.id(), file.size(), file.name(), file.mime_type(),
file.type(), file.parent_folder()));
SetAttachmentPayloadId(file.id(), file.payload_id());
@@ -105,7 +113,6 @@ IncomingShareSession::ProcessIntroduction(
if (std::numeric_limits<int64_t>::max() - file.size() < file_size_sum) {
LOG(WARNING) << "Ignoring introduction, total file size overflowed 64 "
"bit integer.";
container.Clear();
return TransferMetadata::Status::kNotEnoughSpace;
}
file_size_sum += file.size();
@@ -126,7 +133,6 @@ IncomingShareSession::ProcessIntroduction(
LOG(WARNING) << __func__
<< ": Ignoring introduction, total file size overflowed "
"64 bit integer.";
container.Clear();
return TransferMetadata::Status::kNotEnoughSpace;
}
// Map each apk file to a file attachment.
@@ -141,7 +147,7 @@ IncomingShareSession::ProcessIntroduction(
<< ", attachment id=" << apk_file_id
<< ", file size=" << apk.file_size(index)
<< ", payload_id=" << apk.payload_id(index);
container.AddFileAttachment(std::move(apk_file));
builder.AddFileAttachment(std::move(apk_file));
SetAttachmentPayloadId(apk_file_id, apk.payload_id(index));
}
file_size_sum += apk.size();
@@ -156,7 +162,7 @@ IncomingShareSession::ProcessIntroduction(
VLOG(1) << "Found text attachment: id=" << text.id()
<< ", type= " << text.type() << ", size=" << text.size()
<< ", payload_id=" << text.payload_id();
container.AddTextAttachment(
builder.AddTextAttachment(
TextAttachment(text.id(), text.type(), text.text_title(), text.size()));
SetAttachmentPayloadId(text.id(), text.payload_id());
}
@@ -167,7 +173,7 @@ IncomingShareSession::ProcessIntroduction(
VLOG(1) << "Found WiFi credentials attachment: id="
<< wifi_credentials.id() << ", ssid= " << wifi_credentials.ssid()
<< ", payload_id=" << wifi_credentials.payload_id();
container.AddWifiCredentialsAttachment(WifiCredentialsAttachment(
builder.AddWifiCredentialsAttachment(WifiCredentialsAttachment(
wifi_credentials.id(), wifi_credentials.ssid(),
wifi_credentials.security_type()));
SetAttachmentPayloadId(wifi_credentials.id(),
@@ -175,12 +181,13 @@ IncomingShareSession::ProcessIntroduction(
}
}
if (!container.HasAttachments()) {
if (builder.Empty()) {
LOG(WARNING) << __func__
<< ": No attachment is found for this share target. It can "
"be result of unrecognizable attachment type";
return TransferMetadata::Status::kUnsupportedAttachmentType;
}
mutable_attachment_container() = std::move(*builder.Build());
return std::nullopt;
}
+24 -14
View File
@@ -484,7 +484,7 @@ void NearbySharingServiceImpl::RegisterSendSurface(
// request comes from a surface with the blocked vendor ID.
wrapped_callback.OnShareTargetDiscovered(share_target);
transfer_callback->OnTransferUpdate(
share_target, attachment_container, transfer_metadata);
share_target, *attachment_container, transfer_metadata);
}
// Sync down data from Nearby server when the sending flow starts,
@@ -596,7 +596,7 @@ void NearbySharingServiceImpl::RegisterReceiveSurface(
auto& [share_target, attachment_container, transfer_metadata] =
*last_incoming_metadata_;
transfer_callback->OnTransferUpdate(
share_target, attachment_container, transfer_metadata);
share_target, *attachment_container, transfer_metadata);
}
GetReceiveCallbacksMapFromState(state).insert(
@@ -1042,7 +1042,7 @@ NearbySharingServiceImpl::InternalUnregisterSendSurface(
*last_outgoing_metadata_;
for (auto& background_transfer_callback : background_send_surface_map_) {
background_transfer_callback.first->OnTransferUpdate(
share_target, attachment_container, transfer_metadata);
share_target, *attachment_container, transfer_metadata);
}
}
if (foreground_send_surface_map_.empty()) {
@@ -1108,7 +1108,7 @@ NearbySharingServiceImpl::InternalUnregisterReceiveSurface(
*last_incoming_metadata_;
for (auto& background_callback : background_receive_callbacks_map_) {
background_callback.first->OnTransferUpdate(
share_target, attachment_container, transfer_metadata);
share_target, *attachment_container, transfer_metadata);
}
}
@@ -2398,11 +2398,16 @@ void NearbySharingServiceImpl::OnIncomingTransferUpdate(
}
if (metadata.status() != TransferMetadata::Status::kCancelled &&
metadata.status() != TransferMetadata::Status::kRejected) {
last_incoming_metadata_ =
std::make_tuple(session.share_target(), session.attachment_container(),
TransferMetadataBuilder::Clone(metadata)
.set_is_original(false)
.build());
last_incoming_metadata_ = std::make_tuple(
session.share_target(),
AttachmentContainer::Builder(
session.attachment_container().GetTextAttachments(),
session.attachment_container().GetFileAttachments(),
session.attachment_container().GetWifiCredentialsAttachments())
.Build(),
TransferMetadataBuilder::Clone(metadata)
.set_is_original(false)
.build());
} else {
last_incoming_metadata_ = std::nullopt;
}
@@ -2490,11 +2495,16 @@ void NearbySharingServiceImpl::OnOutgoingTransferUpdate(
if (has_foreground_send_surface && metadata.is_final_status()) {
last_outgoing_metadata_ = std::nullopt;
} else {
last_outgoing_metadata_ =
std::make_tuple(session.share_target(), session.attachment_container(),
TransferMetadataBuilder::Clone(metadata)
.set_is_original(false)
.build());
last_outgoing_metadata_ = std::make_tuple(
session.share_target(),
AttachmentContainer::Builder(
session.attachment_container().GetTextAttachments(),
session.attachment_container().GetFileAttachments(),
session.attachment_container().GetWifiCredentialsAttachments())
.Build(),
TransferMetadataBuilder::Clone(metadata)
.set_is_original(false)
.build());
}
}
+4 -2
View File
@@ -456,10 +456,12 @@ class NearbySharingServiceImpl
// Registers the most recent TransferMetadata and ShareTarget used for
// transitioning notifications between foreground surfaces and background
// surfaces. Empty if no metadata is available.
std::optional<std::tuple<ShareTarget, AttachmentContainer, TransferMetadata>>
std::optional<std::tuple<ShareTarget, std::unique_ptr<AttachmentContainer>,
TransferMetadata>>
last_incoming_metadata_;
// The most recent outgoing TransferMetadata and ShareTarget.
std::optional<std::tuple<ShareTarget, AttachmentContainer, TransferMetadata>>
std::optional<std::tuple<ShareTarget, std::unique_ptr<AttachmentContainer>,
TransferMetadata>>
last_outgoing_metadata_;
// A map of ShareTarget id to IncomingShareSession. This lets us know which
// Nearby Connections endpoint and public certificate are related to the
+20 -13
View File
@@ -353,33 +353,40 @@ std::unique_ptr<Frame> GetCancelFrame() {
std::unique_ptr<AttachmentContainer> CreateTextAttachments(
std::vector<std::string> texts) {
auto attachment_container = std::make_unique<AttachmentContainer>();
AttachmentContainer::Builder builder;
builder.ReserveAttachmentsCount(texts.size(), /*file_attachments_count=*/0,
/*wifi_credentials_attachments_count=*/0);
for (auto& text : texts) {
attachment_container->AddTextAttachment(
TextAttachment(service::proto::TextMetadata::TEXT, std::move(text),
/*text_title=*/std::nullopt,
/*mime_type=*/std::nullopt));
builder.AddTextAttachment(TextAttachment(service::proto::TextMetadata::TEXT,
std::move(text),
/*text_title=*/std::nullopt,
/*mime_type=*/std::nullopt));
}
return attachment_container;
return builder.Build();
}
std::unique_ptr<AttachmentContainer> CreateFileAttachments(
std::vector<FilePath> file_paths) {
auto attachment_container = std::make_unique<AttachmentContainer>();
AttachmentContainer::Builder builder;
builder.ReserveAttachmentsCount(/*text_attachments_count=*/0,
file_paths.size(),
/*wifi_credentials_attachments_count=*/0);
for (auto& file_path : file_paths) {
attachment_container->AddFileAttachment(
FileAttachment(std::move(file_path)));
builder.AddFileAttachment(FileAttachment(std::move(file_path)));
}
return attachment_container;
return builder.Build();
}
std::unique_ptr<AttachmentContainer> CreateWifiCredentialAttachments(
std::string ssid, std::string password) {
auto attachment_container = std::make_unique<AttachmentContainer>();
attachment_container->AddWifiCredentialsAttachment(WifiCredentialsAttachment(
AttachmentContainer::Builder builder;
builder.ReserveAttachmentsCount(/*text_attachments_count=*/0,
/*file_attachments_count=*/0,
/*wifi_credentials_attachments_count=*/1);
builder.AddWifiCredentialsAttachment(WifiCredentialsAttachment(
std::move(ssid), service::proto::WifiCredentialsMetadata::WPA_PSK,
std::move(password)));
return attachment_container;
return builder.Build();
}
class NearbySharingServiceImplTest : public testing::Test {
+2 -2
View File
@@ -173,7 +173,7 @@ std::vector<FilePath> OutgoingShareSession::GetFilePaths() const {
}
void OutgoingShareSession::CreateTextPayloads() {
const std::vector<TextAttachment> attachments =
const std::vector<TextAttachment>& attachments =
attachment_container().GetTextAttachments();
if (attachments.empty()) {
return;
@@ -189,7 +189,7 @@ void OutgoingShareSession::CreateTextPayloads() {
}
void OutgoingShareSession::CreateWifiCredentialsPayloads() {
const std::vector<WifiCredentialsAttachment> attachments =
const std::vector<WifiCredentialsAttachment>& attachments =
attachment_container().GetWifiCredentialsAttachments();
if (attachments.empty()) {
return;
+25 -17
View File
@@ -104,10 +104,11 @@ class OutgoingShareSessionTest : public ::testing::Test {
"somepassword", /*is_hidden=*/true) {}
std::unique_ptr<AttachmentContainer> CreateDefaultAttachmentContainer() {
return std::make_unique<AttachmentContainer>(
std::vector<TextAttachment>{text1_, text2_},
std::vector<FileAttachment>{file1_},
std::vector<WifiCredentialsAttachment>{wifi1_});
return AttachmentContainer::Builder(
std::vector<TextAttachment>{text1_, text2_},
std::vector<FileAttachment>{file1_},
std::vector<WifiCredentialsAttachment>{wifi1_})
.Build();
}
void InitSendAttachments(
@@ -157,10 +158,11 @@ TEST_F(OutgoingShareSessionTest, GetFilePaths) {
&fake_clock_, fake_task_runner_, &connections_manager_,
analytics_recorder_, std::string(kEndpointId), share_target_,
[](OutgoingShareSession&, const TransferMetadata&) {});
auto container = std::make_unique<AttachmentContainer>(
std::vector<TextAttachment>{},
std::vector<FileAttachment>{file1_, file2_},
std::vector<WifiCredentialsAttachment>{});
auto container =
AttachmentContainer::Builder(std::vector<TextAttachment>{},
std::vector<FileAttachment>{file1_, file2_},
std::vector<WifiCredentialsAttachment>{})
.Build();
session.InitiateSendAttachments(std::move(container));
auto file_paths = session.GetFilePaths();
@@ -485,9 +487,11 @@ TEST_F(OutgoingShareSessionTest, SendIntroductionSuccess) {
}
TEST_F(OutgoingShareSessionTest, SendIntroductionTimeout) {
auto container = std::make_unique<AttachmentContainer>(
std::vector<TextAttachment>{text1_}, std::vector<FileAttachment>{},
std::vector<WifiCredentialsAttachment>{});
auto container =
AttachmentContainer::Builder(std::vector<TextAttachment>{text1_},
std::vector<FileAttachment>{},
std::vector<WifiCredentialsAttachment>{})
.Build();
InitSendAttachments(std::move(container));
session_.set_session_id(1234);
NearbyConnectionImpl connection(device_info_);
@@ -511,9 +515,11 @@ TEST_F(OutgoingShareSessionTest, SendIntroductionTimeout) {
}
TEST_F(OutgoingShareSessionTest, SendIntroductionTimeoutCancelled) {
auto container = std::make_unique<AttachmentContainer>(
std::vector<TextAttachment>{text1_}, std::vector<FileAttachment>{},
std::vector<WifiCredentialsAttachment>{});
auto container =
AttachmentContainer::Builder(std::vector<TextAttachment>{text1_},
std::vector<FileAttachment>{},
std::vector<WifiCredentialsAttachment>{})
.Build();
InitSendAttachments(std::move(container));
session_.set_session_id(1234);
NearbyConnectionImpl connection(device_info_);
@@ -561,9 +567,11 @@ TEST_F(OutgoingShareSessionTest, AcceptTransferNotReady) {
}
TEST_F(OutgoingShareSessionTest, AcceptTransferSuccess) {
auto container = std::make_unique<AttachmentContainer>(
std::vector<TextAttachment>{text1_}, std::vector<FileAttachment>{},
std::vector<WifiCredentialsAttachment>{});
auto container =
AttachmentContainer::Builder(std::vector<TextAttachment>{text1_},
std::vector<FileAttachment>{},
std::vector<WifiCredentialsAttachment>{})
.Build();
InitSendAttachments(std::move(container));
session_.set_session_id(1234);
NearbyConnectionImpl connection(device_info_);
+11 -10
View File
@@ -33,8 +33,7 @@
#include "sharing/proto/wire_format.pb.h"
#include "sharing/transfer_metadata.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
namespace {
constexpr int64_t kShareTargetId = 123456789L;
@@ -46,16 +45,19 @@ constexpr absl::string_view kMimeType = "image/jpg";
class PayloadTrackerTest : public ::testing::Test {
public:
void SetUp() override {
container_.AddFileAttachment(FileAttachment(
kFileId, kFileSize, std::string(kFileName), std::string(kMimeType),
service::proto::FileMetadata::IMAGE));
container_ =
AttachmentContainer::Builder()
.AddFileAttachment(FileAttachment(
kFileId, kFileSize, std::string(kFileName),
std::string(kMimeType), service::proto::FileMetadata::IMAGE))
.Build();
attachment_payload_map_.clear();
attachment_payload_map_.emplace(container_.GetFileAttachments()[0].id(),
attachment_payload_map_.emplace(container_->GetFileAttachments()[0].id(),
kFileId);
auto payload_updates_queue =
std::make_unique<PayloadTracker::PayloadUpdateQueue>(&task_runner_);
payload_tracker_ = std::make_unique<PayloadTracker>(
&fake_clock_, kShareTargetId, container_, attachment_payload_map_,
&fake_clock_, kShareTargetId, *container_, attachment_payload_map_,
std::move(payload_updates_queue));
}
@@ -74,7 +76,7 @@ class PayloadTrackerTest : public ::testing::Test {
FakeClock fake_clock_;
FakeTaskRunner task_runner_{&fake_clock_, 1};
std::unique_ptr<PayloadTracker> payload_tracker_ = nullptr;
AttachmentContainer container_;
std::unique_ptr<AttachmentContainer> container_;
absl::flat_hash_map<int64_t, int64_t> attachment_payload_map_;
};
@@ -102,5 +104,4 @@ TEST_F(PayloadTrackerTest, StatusUpdateWithTimeUpdate) {
}
} // namespace
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
+6 -8
View File
@@ -31,8 +31,7 @@
#include "sharing/common/nearby_share_enums.h"
#include "sharing/proto/wire_format.pb.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
namespace {
using ::location::nearby::proto::sharing::AttachmentSourceType;
@@ -128,8 +127,8 @@ TextAttachment::TextAttachment(Type type, std::string text_body,
text_title_(text_title.has_value() && !text_title->empty()
? *text_title
: GetTextTitle(text_body, type)),
text_body_(std::move(text_body)),
mime_type_(mime_type ? *mime_type : std::string()) {}
mime_type_(mime_type.value_or("")),
text_body_(std::move(text_body)) {}
TextAttachment::TextAttachment(int64_t id, Type type, std::string text_title,
int64_t size, int32_t batch_id,
@@ -145,8 +144,8 @@ TextAttachment::TextAttachment(int64_t id, Type type, std::string text_body,
: Attachment(id, Attachment::Family::kText, size, batch_id, source_type),
type_(type),
text_title_(std::move(text_title)),
text_body_(std::move(text_body)),
mime_type_(std::move(mime_type)) {}
mime_type_(std::move(mime_type)),
text_body_(std::move(text_body)) {}
absl::string_view TextAttachment::GetDescription() const { return text_title_; }
@@ -176,5 +175,4 @@ void TextAttachment::set_text_body(std::string text_body) {
text_title_ = GetTextTitle(text_body_, type_);
}
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
+5 -8
View File
@@ -25,8 +25,7 @@
#include "sharing/common/nearby_share_enums.h"
#include "sharing/proto/wire_format.pb.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
// Represents a text attachment.
class TextAttachment : public Attachment {
@@ -49,8 +48,7 @@ class TextAttachment : public Attachment {
location::nearby::proto::sharing::AttachmentSourceType source_type);
TextAttachment(const TextAttachment&) = default;
TextAttachment(TextAttachment&&) = default;
TextAttachment& operator=(const TextAttachment&) = default;
TextAttachment& operator=(TextAttachment&&) = default;
TextAttachment& operator=(TextAttachment&&) = delete;
~TextAttachment() override = default;
absl::string_view text_body() const { return text_body_; }
@@ -66,13 +64,12 @@ class TextAttachment : public Attachment {
std::string mime_type() const { return mime_type_; }
private:
Type type_ = service::proto::TextMetadata::UNKNOWN;
const Type type_;
std::string text_title_;
const std::string mime_type_;
std::string text_body_;
std::string mime_type_;
};
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
#endif // THIRD_PARTY_NEARBY_SHARING_TEXT_ATTACHMENT_H_
+5 -10
View File
@@ -23,20 +23,16 @@
#include "sharing/attachment.h"
#include "sharing/common/nearby_share_enums.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
using ::location::nearby::proto::sharing::AttachmentSourceType;
WifiCredentialsAttachment::WifiCredentialsAttachment(
std::string ssid, SecurityType security_type, std::string password,
bool is_hidden, int32_t batch_id, AttachmentSourceType source_type)
: Attachment(Attachment::Family::kWifiCredentials, ssid.size(), batch_id,
source_type),
ssid_(ssid),
security_type_(security_type),
password_(std::move(password)),
is_hidden_(is_hidden) {}
: WifiCredentialsAttachment(/*id=*/0, ssid, security_type,
std::move(password), is_hidden, batch_id,
source_type) {}
WifiCredentialsAttachment::WifiCredentialsAttachment(
int64_t id, std::string ssid, SecurityType security_type,
@@ -65,5 +61,4 @@ void WifiCredentialsAttachment::set_is_hidden(bool is_hidden) {
is_hidden_ = is_hidden;
}
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
+6 -10
View File
@@ -24,9 +24,8 @@
#include "sharing/common/nearby_share_enums.h"
#include "sharing/proto/wire_format.pb.h"
namespace nearby {
namespace sharing {
namespace nearby::sharing {
\
// Represents a WiFi credentials attachment.
class WifiCredentialsAttachment : public Attachment {
public:
@@ -45,9 +44,7 @@ class WifiCredentialsAttachment : public Attachment {
location::nearby::proto::sharing::ATTACHMENT_SOURCE_UNKNOWN);
WifiCredentialsAttachment(const WifiCredentialsAttachment&) = default;
WifiCredentialsAttachment(WifiCredentialsAttachment&&) = default;
WifiCredentialsAttachment& operator=(const WifiCredentialsAttachment&) =
default;
WifiCredentialsAttachment& operator=(WifiCredentialsAttachment&&) = default;
WifiCredentialsAttachment& operator=(WifiCredentialsAttachment&&) = delete;
~WifiCredentialsAttachment() override = default;
absl::string_view ssid() const { return ssid_; }
@@ -63,13 +60,12 @@ class WifiCredentialsAttachment : public Attachment {
void set_is_hidden(bool is_hidden);
private:
std::string ssid_;
SecurityType security_type_;
const std::string ssid_;
const SecurityType security_type_;
std::string password_;
bool is_hidden_;
};
} // namespace sharing
} // namespace nearby
} // namespace nearby::sharing
#endif // THIRD_PARTY_NEARBY_SHARING_WIFI_CREDENTIALS_ATTACHMENT_H_