Skip to content

Commit 2303169

Browse files
ftsuicopybara-github
authored andcommitted
Move IntroductionFrame handing to ShareTargetInfo.
PiperOrigin-RevId: 644192822
1 parent 2fccc4a commit 2303169

14 files changed

+588
-198
lines changed

sharing/BUILD

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ cc_library(
199199
"//sharing/common:compatible_u8_string",
200200
"//sharing/internal/public:logging",
201201
"//sharing/internal/public:types",
202+
"//sharing/proto:wire_format_cc_proto",
202203
"@com_google_absl//absl/container:flat_hash_map",
204+
"@com_google_absl//absl/strings",
203205
"@com_google_absl//absl/strings:string_view",
204206
"@com_google_absl//absl/time",
205207
"@com_google_absl//absl/types:span",
@@ -350,6 +352,17 @@ cc_library(
350352
],
351353
)
352354

355+
cc_library(
356+
name = "attachment_compare",
357+
testonly = True,
358+
srcs = ["attachment_compare.cc"],
359+
hdrs = ["attachment_compare.h"],
360+
deps = [
361+
":attachments",
362+
"@com_google_absl//absl/strings",
363+
],
364+
)
365+
353366
cc_test(
354367
name = "advertisement_test",
355368
srcs = ["advertisement_test.cc"],
@@ -721,6 +734,7 @@ cc_test(
721734
name = "attachment_container_test",
722735
srcs = ["attachment_container_test.cc"],
723736
deps = [
737+
":attachment_compare",
724738
":attachments",
725739
"@com_github_protobuf_matchers//protobuf-matchers",
726740
"@com_google_googletest//:gtest_main",
@@ -755,3 +769,22 @@ cc_test(
755769
"@com_google_googletest//:gtest_main",
756770
],
757771
)
772+
773+
cc_test(
774+
name = "incoming_share_target_info_test",
775+
srcs = ["incoming_share_target_info_test.cc"],
776+
deps = [
777+
":attachment_compare",
778+
":attachments",
779+
":share_target_info",
780+
":transfer_metadata",
781+
":types",
782+
"//internal/platform/implementation/g3", # fixdeps: keep
783+
"//sharing/internal/public:logging",
784+
"//sharing/proto:wire_format_cc_proto",
785+
"//third_party/protobuf",
786+
"@com_github_protobuf_matchers//protobuf-matchers",
787+
"@com_google_absl//absl/strings:string_view",
788+
"@com_google_googletest//:gtest_main",
789+
],
790+
)

sharing/attachment_compare.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "sharing/attachment_compare.h"
16+
#include "sharing/file_attachment.h"
17+
#include "sharing/text_attachment.h"
18+
#include "sharing/wifi_credentials_attachment.h"
19+
20+
namespace nearby::sharing {
21+
22+
bool operator==(const TextAttachment& lhs, const TextAttachment& rhs) {
23+
return lhs.id() == rhs.id() && lhs.family() == rhs.family() &&
24+
lhs.size() == rhs.size() && lhs.batch_id() == rhs.batch_id() &&
25+
lhs.source_type() == rhs.source_type() && lhs.type() == rhs.type() &&
26+
lhs.text_title() == rhs.text_title() &&
27+
lhs.text_body() == rhs.text_body() &&
28+
lhs.mime_type() == rhs.mime_type();
29+
}
30+
31+
bool operator==(const FileAttachment& lhs, const FileAttachment& rhs) {
32+
return lhs.id() == rhs.id() && lhs.family() == rhs.family() &&
33+
lhs.size() == rhs.size() && lhs.batch_id() == rhs.batch_id() &&
34+
lhs.source_type() == rhs.source_type() &&
35+
lhs.file_name() == rhs.file_name() &&
36+
lhs.mime_type() == rhs.mime_type() && lhs.type() == rhs.type() &&
37+
lhs.file_path() == rhs.file_path() &&
38+
lhs.parent_folder() == rhs.parent_folder();
39+
}
40+
41+
bool operator==(const WifiCredentialsAttachment& lhs,
42+
const WifiCredentialsAttachment& rhs) {
43+
return lhs.id() == rhs.id() && lhs.family() == rhs.family() &&
44+
lhs.size() == rhs.size() && lhs.batch_id() == rhs.batch_id() &&
45+
lhs.ssid() == rhs.ssid() &&
46+
lhs.security_type() == rhs.security_type() &&
47+
lhs.password() == rhs.password() && lhs.is_hidden() == rhs.is_hidden();
48+
}
49+
50+
} // namespace nearby::sharing

sharing/attachment_compare.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef THIRD_PARTY_NEARBY_SHARING_ATTACHMENT_COMPARE_H_
16+
#define THIRD_PARTY_NEARBY_SHARING_ATTACHMENT_COMPARE_H_
17+
18+
#include "sharing/file_attachment.h"
19+
#include "sharing/text_attachment.h"
20+
#include "sharing/wifi_credentials_attachment.h"
21+
22+
namespace nearby::sharing {
23+
24+
bool operator==(const TextAttachment& lhs, const TextAttachment& rhs);
25+
bool operator==(const FileAttachment& lhs, const FileAttachment& rhs);
26+
bool operator==(const WifiCredentialsAttachment& lhs,
27+
const WifiCredentialsAttachment& rhs);
28+
29+
} // namespace nearby::sharing
30+
31+
#endif // THIRD_PARTY_NEARBY_SHARING_ATTACHMENT_COMPARE_H_

sharing/attachment_container.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ int64_t AttachmentContainer::GetTotalAttachmentsSize() const {
5050
return size_in_bytes;
5151
}
5252

53+
int64_t AttachmentContainer::GetStorageSize() const {
54+
int64_t size_in_bytes = 0;
55+
56+
// Only files require disk storage.
57+
for (const auto& file : file_attachments_) {
58+
size_in_bytes += file.size();
59+
}
60+
61+
return size_in_bytes;
62+
}
63+
5364
void AttachmentContainer::ClearAttachments() {
5465
// Reset file paths for file attachments.
5566
for (auto& file : file_attachments_)
@@ -66,6 +77,12 @@ void AttachmentContainer::ClearAttachments() {
6677
}
6778
}
6879

80+
void AttachmentContainer::Clear() {
81+
file_attachments_.clear();
82+
text_attachments_.clear();
83+
wifi_credentials_attachments_.clear();
84+
}
85+
6986
std::vector<int64_t> AttachmentContainer::GetAttachmentIds() const {
7087
std::vector<int64_t> attachment_ids;
7188

sharing/attachment_container.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class AttachmentContainer {
8383
// Returns the total size of all attachments.
8484
int64_t GetTotalAttachmentsSize() const;
8585

86+
// Returns the total size of all attachments on disk.
87+
int64_t GetStorageSize() const;
88+
8689
// Returns true if there are any attachments.
8790
bool HasAttachments() const {
8891
return !text_attachments_.empty() || !file_attachments_.empty() ||
@@ -93,6 +96,9 @@ class AttachmentContainer {
9396
// place.
9497
void ClearAttachments();
9598

99+
// Delete all attachments.
100+
void Clear();
101+
96102
// Returns the list of attachment IDs of attachments in this container.
97103
std::vector<int64_t> GetAttachmentIds() const;
98104

sharing/attachment_container_test.cc

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,12 @@
2323
#include "protobuf-matchers/protocol-buffer-matchers.h"
2424
#include "gtest/gtest.h"
2525
#include "sharing/attachment.h"
26+
#include "sharing/attachment_compare.h" // IWYU pragma: keep
2627
#include "sharing/file_attachment.h"
2728
#include "sharing/text_attachment.h"
2829
#include "sharing/wifi_credentials_attachment.h"
2930

3031
namespace nearby::sharing {
31-
32-
bool operator==(const TextAttachment& lhs, const TextAttachment& rhs) {
33-
return lhs.id() == rhs.id() && lhs.family() == rhs.family() &&
34-
lhs.size() == rhs.size() && lhs.batch_id() == rhs.batch_id() &&
35-
lhs.source_type() == rhs.source_type() && lhs.type() == rhs.type() &&
36-
lhs.text_title() == rhs.text_title() &&
37-
lhs.text_body() == rhs.text_body() &&
38-
lhs.mime_type() == rhs.mime_type();
39-
}
40-
41-
bool operator==(const FileAttachment& lhs, const FileAttachment& rhs) {
42-
return lhs.id() == rhs.id() && lhs.family() == rhs.family() &&
43-
lhs.size() == rhs.size() && lhs.batch_id() == rhs.batch_id() &&
44-
lhs.source_type() == rhs.source_type() &&
45-
lhs.file_name() == rhs.file_name() &&
46-
lhs.mime_type() == rhs.mime_type() && lhs.type() == rhs.type() &&
47-
lhs.file_path() == rhs.file_path() &&
48-
lhs.parent_folder() == rhs.parent_folder();
49-
}
50-
51-
bool operator==(const WifiCredentialsAttachment& lhs,
52-
const WifiCredentialsAttachment& rhs) {
53-
return lhs.id() == rhs.id() && lhs.family() == rhs.family() &&
54-
lhs.size() == rhs.size() && lhs.batch_id() == rhs.batch_id() &&
55-
lhs.ssid() == rhs.ssid() &&
56-
lhs.security_type() == rhs.security_type() &&
57-
lhs.password() == rhs.password() && lhs.is_hidden() == rhs.is_hidden();
58-
}
59-
6032
namespace {
6133

6234
using testing::Eq;
@@ -209,6 +181,17 @@ TEST_F(AttachmentContainerTest, ClearAttachments) {
209181
IsFalse());
210182
}
211183

184+
TEST_F(AttachmentContainerTest, Clear) {
185+
AttachmentContainer container(std::vector<TextAttachment>{text1_, text2_},
186+
std::vector<FileAttachment>{file1_},
187+
std::vector<WifiCredentialsAttachment>{wifi1_});
188+
EXPECT_THAT(container.HasAttachments(), IsTrue());
189+
190+
container.Clear();
191+
192+
EXPECT_THAT(container.HasAttachments(), IsFalse());
193+
}
194+
212195
TEST_F(AttachmentContainerTest, GetAttachmentIds) {
213196
AttachmentContainer container(std::vector<TextAttachment>{text1_, text2_},
214197
std::vector<FileAttachment>{file1_},
@@ -222,6 +205,16 @@ TEST_F(AttachmentContainerTest, GetAttachmentIds) {
222205
wifi1_.id()));
223206
}
224207

208+
TEST_F(AttachmentContainerTest, GetStorageSize) {
209+
AttachmentContainer container(std::vector<TextAttachment>{text1_, text2_},
210+
std::vector<FileAttachment>{file1_},
211+
std::vector<WifiCredentialsAttachment>{wifi1_});
212+
213+
int64_t storage_size = container.GetStorageSize();
214+
215+
EXPECT_THAT(storage_size, Eq(file1_.size()));
216+
}
217+
225218
} // namespace
226219

227220
} // namespace nearby::sharing

sharing/incoming_share_target_info.cc

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,31 @@
1414

1515
#include "sharing/incoming_share_target_info.h"
1616

17+
#include <cstdint>
1718
#include <functional>
19+
#include <limits>
20+
#include <optional>
1821
#include <string>
1922
#include <utility>
2023

24+
#include "sharing/attachment_container.h"
25+
#include "sharing/constants.h"
26+
#include "sharing/file_attachment.h"
27+
#include "sharing/internal/public/logging.h"
2128
#include "sharing/nearby_connection.h"
29+
#include "sharing/proto/wire_format.pb.h"
2230
#include "sharing/share_target.h"
2331
#include "sharing/share_target_info.h"
32+
#include "sharing/text_attachment.h"
2433
#include "sharing/transfer_metadata.h"
34+
#include "sharing/wifi_credentials_attachment.h"
2535

26-
namespace nearby {
27-
namespace sharing {
36+
namespace nearby::sharing {
37+
namespace {
38+
39+
using ::nearby::sharing::service::proto::IntroductionFrame;
40+
41+
} // namespace
2842

2943
IncomingShareTargetInfo::IncomingShareTargetInfo(
3044
std::string endpoint_id, const ShareTarget& share_target,
@@ -52,5 +66,77 @@ bool IncomingShareTargetInfo::OnNewConnection(NearbyConnection* connection) {
5266
return true;
5367
}
5468

55-
} // namespace sharing
56-
} // namespace nearby
69+
std::optional<TransferMetadata::Status>
70+
IncomingShareTargetInfo::ProcessIntroduction(
71+
const IntroductionFrame& introduction_frame) {
72+
int64_t file_size_sum = 0;
73+
AttachmentContainer& container = mutable_attachment_container();
74+
for (const auto& file : introduction_frame.file_metadata()) {
75+
if (file.size() <= 0) {
76+
NL_LOG(WARNING)
77+
<< __func__
78+
<< ": Ignore introduction, due to invalid attachment size";
79+
return TransferMetadata::Status::kUnsupportedAttachmentType;
80+
}
81+
82+
NL_VLOG(1) << __func__ << ": Found file attachment: id=" << file.id()
83+
<< ", type= " << file.type() << ", size=" << file.size()
84+
<< ", payload_id=" << file.payload_id()
85+
<< ", parent_folder=" << file.parent_folder()
86+
<< ", mime_type=" << file.mime_type();
87+
container.AddFileAttachment(
88+
FileAttachment(file.id(), file.size(), file.name(), file.mime_type(),
89+
file.type(), file.parent_folder()));
90+
SetAttachmentPayloadId(file.id(), file.payload_id());
91+
92+
if (std::numeric_limits<int64_t>::max() - file.size() < file_size_sum) {
93+
NL_LOG(WARNING) << __func__
94+
<< ": Ignoring introduction, total file size overflowed "
95+
"64 bit integer.";
96+
container.Clear();
97+
return TransferMetadata::Status::kNotEnoughSpace;
98+
}
99+
file_size_sum += file.size();
100+
}
101+
102+
for (const auto& text : introduction_frame.text_metadata()) {
103+
if (text.size() <= 0) {
104+
NL_LOG(WARNING)
105+
<< __func__
106+
<< ": Ignore introduction, due to invalid attachment size";
107+
return TransferMetadata::Status::kUnsupportedAttachmentType;
108+
}
109+
110+
NL_VLOG(1) << __func__ << ": Found text attachment: id=" << text.id()
111+
<< ", type= " << text.type() << ", size=" << text.size()
112+
<< ", payload_id=" << text.payload_id();
113+
container.AddTextAttachment(
114+
TextAttachment(text.id(), text.type(), text.text_title(), text.size()));
115+
SetAttachmentPayloadId(text.id(), text.payload_id());
116+
}
117+
118+
if (kSupportReceivingWifiCredentials) {
119+
for (const auto& wifi_credentials :
120+
introduction_frame.wifi_credentials_metadata()) {
121+
NL_VLOG(1) << __func__ << ": Found WiFi credentials attachment: id="
122+
<< wifi_credentials.id()
123+
<< ", ssid= " << wifi_credentials.ssid()
124+
<< ", payload_id=" << wifi_credentials.payload_id();
125+
container.AddWifiCredentialsAttachment(WifiCredentialsAttachment(
126+
wifi_credentials.id(), wifi_credentials.ssid(),
127+
wifi_credentials.security_type()));
128+
SetAttachmentPayloadId(wifi_credentials.id(),
129+
wifi_credentials.payload_id());
130+
}
131+
}
132+
133+
if (!container.HasAttachments()) {
134+
NL_LOG(WARNING) << __func__
135+
<< ": No attachment is found for this share target. It can "
136+
"be result of unrecognizable attachment type";
137+
return TransferMetadata::Status::kUnsupportedAttachmentType;
138+
}
139+
return std::nullopt;
140+
}
141+
142+
} // namespace nearby::sharing

0 commit comments

Comments
 (0)