diff --git a/sharing/analytics/BUILD b/sharing/analytics/BUILD index 05ec0a58..5ba7db00 100644 --- a/sharing/analytics/BUILD +++ b/sharing/analytics/BUILD @@ -31,11 +31,9 @@ cc_library( "//sharing:attachments", "//sharing:types", "//sharing/common:enum", - "//sharing/internal/public:logging", "//sharing/proto:enums_cc_proto", "//sharing/proto/analytics:sharing_log_cc_proto", "@com_google_absl//absl/random", - "@com_google_absl//absl/strings", "@com_google_protobuf//:protobuf", "@com_google_protobuf//:protobuf_lite", ], diff --git a/sharing/analytics/analytics_recorder.cc b/sharing/analytics/analytics_recorder.cc index 7cc0b719..a7aa2d79 100644 --- a/sharing/analytics/analytics_recorder.cc +++ b/sharing/analytics/analytics_recorder.cc @@ -18,18 +18,16 @@ #include #include #include -#include #include "google/protobuf/duration.pb.h" #include "absl/random/random.h" -#include "absl/strings/str_cat.h" #include "proto/sharing_enums.pb.h" #include "sharing/analytics/analytics_device_settings.h" #include "sharing/analytics/analytics_information.h" #include "sharing/attachment.h" +#include "sharing/attachment_container.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/file_attachment.h" -#include "sharing/internal/public/logging.h" #include "sharing/proto/analytics/nearby_sharing_log.pb.h" #include "sharing/proto/enums.pb.h" #include "sharing/share_target.h" @@ -172,116 +170,95 @@ analytics::proto::SharingLog::ShareTargetInfo* GetAllocatedShareTargetInfo( } analytics::proto::SharingLog::AttachmentsInfo* GenerateAllocatedAttachmentInfo( - const std::vector>& attachments) { + const AttachmentContainer& attachments) { auto attachments_info = analytics::proto::SharingLog::AttachmentsInfo::default_instance().New(); - for (auto& attachment : attachments) { - int64_t size = attachment->size(); - if (attachment->family() == Attachment::Family::kText) { - analytics::proto::SharingLog::TextAttachment::Type type = - analytics::proto::SharingLog::TextAttachment::UNKNOWN_TEXT_TYPE; - switch (attachment->GetShareType()) { - case ShareType::kPhone: - type = analytics::proto::SharingLog::TextAttachment::PHONE_NUMBER; - break; - case ShareType::kUrl: - type = analytics::proto::SharingLog::TextAttachment::URL; - break; - case ShareType::kAddress: - type = analytics::proto::SharingLog::TextAttachment::ADDRESS; - break; - case ShareType::kText: - // Apply UNKNOWN_TEXT_TYPE for it based on analytics design. - break; - default: - break; - } - - ::google::protobuf::RepeatedPtrField* - text_attachments = attachments_info->mutable_text_attachment(); - analytics::proto::SharingLog_TextAttachment* text_attachment = - analytics::proto::SharingLog::TextAttachment::default_instance() - .New(); - text_attachment->set_type(type); - text_attachment->set_size_bytes(size); - text_attachment->set_source_type( - GetLoggerAttachmentSourceType(attachment->source_type())); - text_attachment->set_batch_id(attachment->batch_id()); - text_attachments->AddAllocated(text_attachment); - } else if (attachment->family() == Attachment::Family::kFile) { - analytics::proto::SharingLog::FileAttachment::Type type = - analytics::proto::SharingLog::FileAttachment::UNKNOWN_FILE_TYPE; - switch (attachment->GetShareType()) { - case ShareType::kImageFile: - type = analytics::proto::SharingLog::FileAttachment::IMAGE; - break; - case ShareType::kVideoFile: - type = analytics::proto::SharingLog::FileAttachment::VIDEO; - break; - case ShareType::kAudioFile: - type = analytics::proto::SharingLog::FileAttachment::AUDIO; - break; - case ShareType::kPdfFile: - case ShareType::kTextFile: - case ShareType::kGoogleDocsFile: - case ShareType::kGoogleSheetsFile: - case ShareType::kGoogleSlidesFile: - type = analytics::proto::SharingLog::FileAttachment::DOCUMENT; - break; - case ShareType::kUnknownFile: - // The default type is set to type. - break; - default: - break; - } - - ::google::protobuf::RepeatedPtrField* - file_attachments = attachments_info->mutable_file_attachment(); - analytics::proto::SharingLog_FileAttachment* file_attachment = - analytics::proto::SharingLog::FileAttachment::default_instance() - .New(); - file_attachment->set_type(type); - file_attachment->set_size_bytes(size); - file_attachment->set_offset_bytes(0); - file_attachment->set_source_type( - GetLoggerAttachmentSourceType(attachment->source_type())); - file_attachment->set_batch_id(attachment->batch_id()); - file_attachments->AddAllocated(file_attachment); - } else if (attachment->family() == Attachment::Family::kWifiCredentials) { - ::google::protobuf::RepeatedPtrField< - analytics::proto::SharingLog_WifiCredentialsAttachment>* - wifi_credentials_attachments = - attachments_info->mutable_wifi_credentials_attachment(); - analytics::proto::SharingLog_WifiCredentialsAttachment* - wifi_credentials_attachment = - analytics::proto::SharingLog::WifiCredentialsAttachment:: - default_instance() - .New(); - wifi_credentials_attachment->set_source_type( - GetLoggerAttachmentSourceType(attachment->source_type())); - wifi_credentials_attachment->set_batch_id(attachment->batch_id()); - wifi_credentials_attachments->AddAllocated(wifi_credentials_attachment); - } else { - NL_LOG(WARNING) - << __func__ - << "Unable to create event for attachment info. Unknown attachment " - << attachment->id(); - continue; + for (const auto& attachment : attachments.GetTextAttachments()) { + analytics::proto::SharingLog::TextAttachment::Type type = + analytics::proto::SharingLog::TextAttachment::UNKNOWN_TEXT_TYPE; + switch (attachment.GetShareType()) { + case ShareType::kPhone: + type = analytics::proto::SharingLog::TextAttachment::PHONE_NUMBER; + break; + case ShareType::kUrl: + type = analytics::proto::SharingLog::TextAttachment::URL; + break; + case ShareType::kAddress: + type = analytics::proto::SharingLog::TextAttachment::ADDRESS; + break; + case ShareType::kText: + // Apply UNKNOWN_TEXT_TYPE for it based on analytics design. + break; + default: + break; } + ::google::protobuf::RepeatedPtrField* + text_attachments = attachments_info->mutable_text_attachment(); + analytics::proto::SharingLog_TextAttachment* text_attachment = + analytics::proto::SharingLog::TextAttachment::default_instance().New(); + text_attachment->set_type(type); + text_attachment->set_size_bytes(attachment.size()); + text_attachment->set_source_type( + GetLoggerAttachmentSourceType(attachment.source_type())); + text_attachment->set_batch_id(attachment.batch_id()); + text_attachments->AddAllocated(text_attachment); } - if (attachments_info->file_attachment_size() == 0 && - attachments_info->text_attachment_size() == 0 && - attachments_info->wifi_credentials_attachment_size() == 0) { - std::string type = - attachments.empty() - ? "NULL" - : absl::StrCat(static_cast(attachments[0]->GetShareType())); - NL_LOG(WARNING) << __func__ << "attachmentInfo is empty, attachment size=" - << attachments.size() << ", type=" << type; + for (const auto& attachment : attachments.GetFileAttachments()) { + analytics::proto::SharingLog::FileAttachment::Type type = + analytics::proto::SharingLog::FileAttachment::UNKNOWN_FILE_TYPE; + switch (attachment.GetShareType()) { + case ShareType::kImageFile: + type = analytics::proto::SharingLog::FileAttachment::IMAGE; + break; + case ShareType::kVideoFile: + type = analytics::proto::SharingLog::FileAttachment::VIDEO; + break; + case ShareType::kAudioFile: + type = analytics::proto::SharingLog::FileAttachment::AUDIO; + break; + case ShareType::kPdfFile: + case ShareType::kTextFile: + case ShareType::kGoogleDocsFile: + case ShareType::kGoogleSheetsFile: + case ShareType::kGoogleSlidesFile: + type = analytics::proto::SharingLog::FileAttachment::DOCUMENT; + break; + case ShareType::kUnknownFile: + // The default type is set to type. + break; + default: + break; + } + ::google::protobuf::RepeatedPtrField* + file_attachments = attachments_info->mutable_file_attachment(); + analytics::proto::SharingLog_FileAttachment* file_attachment = + analytics::proto::SharingLog::FileAttachment::default_instance().New(); + file_attachment->set_type(type); + file_attachment->set_size_bytes(attachment.size()); + file_attachment->set_offset_bytes(0); + file_attachment->set_source_type( + GetLoggerAttachmentSourceType(attachment.source_type())); + file_attachment->set_batch_id(attachment.batch_id()); + file_attachments->AddAllocated(file_attachment); } + for (const auto& attachment : attachments.GetWifiCredentialsAttachments()) { + ::google::protobuf::RepeatedPtrField< + analytics::proto::SharingLog_WifiCredentialsAttachment>* + wifi_credentials_attachments = + attachments_info->mutable_wifi_credentials_attachment(); + analytics::proto::SharingLog_WifiCredentialsAttachment* + wifi_credentials_attachment = + analytics::proto::SharingLog::WifiCredentialsAttachment:: + default_instance() + .New(); + wifi_credentials_attachment->set_source_type( + GetLoggerAttachmentSourceType(attachment.source_type())); + wifi_credentials_attachment->set_batch_id(attachment.batch_id()); + wifi_credentials_attachments->AddAllocated(wifi_credentials_attachment); + } return attachments_info; } @@ -437,7 +414,7 @@ void AnalyticsRecorder::NewAdvertiseDevicePresenceStart( } void AnalyticsRecorder::NewDescribeAttachments( - const std::vector>& attachments) { + const AttachmentContainer& attachments) { std::unique_ptr sharing_log = CreateSharingLog( EventCategory::SENDING_EVENT, EventType::DESCRIBE_ATTACHMENTS); @@ -496,8 +473,7 @@ void AnalyticsRecorder::NewEnableNearbySharing( } void AnalyticsRecorder::NewOpenReceivedAttachments( - const std::vector>& attachments, - int64_t session_id) { + const AttachmentContainer& attachments, int64_t session_id) { std::unique_ptr sharing_log = CreateSharingLog( EventCategory::RECEIVING_EVENT, EventType::OPEN_RECEIVED_ATTACHMENTS); @@ -555,8 +531,7 @@ void AnalyticsRecorder::NewReceiveAttachmentsEnd( } void AnalyticsRecorder::NewReceiveAttachmentsStart( - int64_t session_id, - const std::vector>& attachments) { + int64_t session_id, const AttachmentContainer& attachments) { std::unique_ptr sharing_log = CreateSharingLog( EventCategory::RECEIVING_EVENT, EventType::RECEIVE_ATTACHMENTS_START); @@ -752,8 +727,7 @@ void AnalyticsRecorder::NewSendAttachmentsEnd( } void AnalyticsRecorder::NewSendAttachmentsStart( - int64_t session_id, - const std::vector>& attachments, + int64_t session_id, const AttachmentContainer& attachments, int transfer_position, int concurrent_connections) { std::unique_ptr sharing_log = CreateSharingLog( EventCategory::SENDING_EVENT, EventType::SEND_ATTACHMENTS_START); diff --git a/sharing/analytics/analytics_recorder.h b/sharing/analytics/analytics_recorder.h index 4ec1077d..aab84bd2 100644 --- a/sharing/analytics/analytics_recorder.h +++ b/sharing/analytics/analytics_recorder.h @@ -19,13 +19,12 @@ #include #include #include -#include #include "internal/analytics/event_logger.h" #include "proto/sharing_enums.pb.h" #include "sharing/analytics/analytics_device_settings.h" #include "sharing/analytics/analytics_information.h" -#include "sharing/attachment.h" +#include "sharing/attachment_container.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/proto/analytics/nearby_sharing_log.pb.h" #include "sharing/proto/enums.pb.h" @@ -72,8 +71,7 @@ class AnalyticsRecorder { nearby::sharing::proto::DataUsage data_usage, std::optional referrer_package); - void NewDescribeAttachments( - const std::vector>& attachments); + void NewDescribeAttachments(const AttachmentContainer& attachments); void NewDiscoverShareTarget( ShareTarget share_target, int64_t session_id, @@ -84,9 +82,8 @@ class AnalyticsRecorder { void NewEnableNearbySharing( location::nearby::proto::sharing::NearbySharingStatus status); - void NewOpenReceivedAttachments( - const std::vector>& attachments, - int64_t session_id); + void NewOpenReceivedAttachments(const AttachmentContainer& attachments, + int64_t session_id); void NewProcessReceivedAttachmentsEnd( int64_t session_id, @@ -98,9 +95,8 @@ class AnalyticsRecorder { location::nearby::proto::sharing::AttachmentTransmissionStatus status, std::optional referrer_package); - void NewReceiveAttachmentsStart( - int64_t session_id, - const std::vector>& attachments); + void NewReceiveAttachmentsStart(int64_t session_id, + const AttachmentContainer& attachments); void NewReceiveFastInitialization(int64_t timeElapseSinceScreenUnlockMillis); @@ -138,10 +134,10 @@ class AnalyticsRecorder { connection_layer_status, location::nearby::proto::sharing::OSType share_target_os_type); - void NewSendAttachmentsStart( - int64_t session_id, - const std::vector>& attachments, - int transfer_position, int concurrent_connections); + void NewSendAttachmentsStart(int64_t session_id, + const AttachmentContainer& attachments, + int transfer_position, + int concurrent_connections); void NewSendFastInitialization(); diff --git a/sharing/analytics/analytics_recorder_test.cc b/sharing/analytics/analytics_recorder_test.cc index b45230c1..87b68c24 100644 --- a/sharing/analytics/analytics_recorder_test.cc +++ b/sharing/analytics/analytics_recorder_test.cc @@ -16,10 +16,8 @@ #include -#include #include #include -#include #include "google/protobuf/duration.pb.h" #include "gmock/gmock.h" @@ -30,7 +28,7 @@ #include "proto/sharing_enums.pb.h" #include "sharing/analytics/analytics_device_settings.h" #include "sharing/analytics/analytics_information.h" -#include "sharing/attachment.h" +#include "sharing/attachment_container.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/file_attachment.h" #include "sharing/proto/analytics/nearby_sharing_log.pb.h" @@ -38,7 +36,6 @@ #include "sharing/proto/wire_format.pb.h" #include "sharing/share_target.h" #include "sharing/text_attachment.h" -#include "google/protobuf/message_lite.h" namespace nearby { namespace sharing { @@ -282,32 +279,27 @@ TEST_F(AnalyticsRecorderTest, NewDescribeAttachments) { SharingLog::FileAttachment::DOCUMENT); }); - std::vector> attachments; - attachments.push_back(std::make_unique( - 1, 2, std::string(kFileName), "", service::proto::FileMetadata::IMAGE)); - attachments.push_back(std::make_unique( - 2, 3, std::string(kFileDocumentName), std::string(kFileMimeType), - service::proto::FileMetadata::DOCUMENT)); - attachments.push_back(std::make_unique( - 3, 4, std::string(kFileName), "", service::proto::FileMetadata::AUDIO)); - attachments.push_back(std::make_unique( - 4, 5, std::string(kFileName), std::string(kTextMimeType), - service::proto::FileMetadata::DOCUMENT)); - attachments.push_back(std::make_unique( - 5, service::proto::TextMetadata::TEXT, std::string(kTextBody), - kTextBody.size())); - attachments.push_back(std::make_unique( - 6, service::proto::TextMetadata::PHONE_NUMBER, std::string(kTextBody), - kTextBody.size())); - attachments.push_back(std::make_unique( - 7, service::proto::TextMetadata::URL, std::string(kTextBody), - kTextBody.size())); - attachments.push_back(std::make_unique( - 8, service::proto::TextMetadata::ADDRESS, std::string(kTextBody), - kTextBody.size())); - attachments.push_back(std::make_unique( - 9, service::proto::TextMetadata::UNKNOWN, std::string(kTextBody), - kTextBody.size())); + 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)}, + {}); analytics_recoder().NewDescribeAttachments(attachments); } @@ -327,8 +319,7 @@ TEST_F(AnalyticsRecorderTest, EmptyDescribeAttachments) { 0); }); - std::vector> attachments; - analytics_recoder().NewDescribeAttachments(attachments); + analytics_recoder().NewDescribeAttachments(AttachmentContainer()); } TEST_F(AnalyticsRecorderTest, NewDiscoverShareTarget) { @@ -397,8 +388,7 @@ TEST_F(AnalyticsRecorderTest, NewOpenReceivedAttachments) { EXPECT_EQ(log.open_received_attachments().session_id(), 1); }); - analytics_recoder().NewOpenReceivedAttachments( - std::vector>(), 1); + analytics_recoder().NewOpenReceivedAttachments(AttachmentContainer(), 1); } TEST_F(AnalyticsRecorderTest, NewProcessReceivedAttachmentsEnd) { @@ -453,8 +443,7 @@ TEST_F(AnalyticsRecorderTest, NewReceiveAttachmentsStart) { 0); }); - analytics_recoder().NewReceiveAttachmentsStart( - 1, std::vector>()); + analytics_recoder().NewReceiveAttachmentsStart(1, AttachmentContainer()); } TEST_F(AnalyticsRecorderTest, NewReceiveFastInitialization) { @@ -628,8 +617,8 @@ TEST_F(AnalyticsRecorderTest, NewSendAttachmentsStart) { EXPECT_EQ(log.send_attachments_start().concurrent_connections(), 200); }); - analytics_recoder().NewSendAttachmentsStart( - 1, std::vector>(), 100, 200); + analytics_recoder().NewSendAttachmentsStart(1, AttachmentContainer(), 100, + 200); } TEST_F(AnalyticsRecorderTest, NewSendFastInitialization) { diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index 7ca31a9a..a4d9b499 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -751,7 +751,7 @@ void NearbySharingServiceImpl::SendAttachments( ShareTarget share_target, bool success) { // Log analytics event of describing attachments. analytics_recorder_->NewDescribeAttachments( - share_target.GetAttachments()); + share_target.attachment_container); OnCreatePayloads(std::move(endpoint_info), share_target, success); @@ -971,7 +971,7 @@ void NearbySharingServiceImpl::Open( // Log analytics event of opening received attachments. ShareTargetInfo* info = GetShareTargetInfo(share_target.id); analytics_recorder_->NewOpenReceivedAttachments( - share_target.GetAttachments(), + share_target.attachment_container, info != nullptr ? info->session_id() : 0); status_codes_callback( @@ -2565,7 +2565,7 @@ NearbySharingService::StatusCodes NearbySharingServiceImpl::SendPayloads( ShareTarget cached_share_target = info->share_target(); // Log analytics event of sending attachment start. analytics_recorder_->NewSendAttachmentsStart( - info->session_id(), cached_share_target.GetAttachments(), + info->session_id(), cached_share_target.attachment_container, /*transfer_position=*/GetConnectedShareTargetPos(share_target), /*concurrent_connections=*/GetConnectedShareTargetCount()); @@ -2592,7 +2592,7 @@ void NearbySharingServiceImpl::OnPayloadPathsRegistered( // Log analytics event of starting to receive payloads. analytics_recorder_->NewReceiveAttachmentsStart( - receiving_session_id_, share_target.GetAttachments()); + receiving_session_id_, share_target.attachment_container); info->set_payload_tracker(std::make_shared( context_, share_target.id, share_target.attachment_container, diff --git a/sharing/share_target.cc b/sharing/share_target.cc index 9f3590e8..0612580d 100644 --- a/sharing/share_target.cc +++ b/sharing/share_target.cc @@ -91,26 +91,6 @@ std::vector ShareTarget::GetAttachmentIds() const { return attachment_ids; } -std::vector> ShareTarget::GetAttachments() const { - std::vector> attachments; - attachments.reserve(attachment_container.GetAttachmentCount()); - for (const auto& file : attachment_container.GetFileAttachments()) { - attachments.push_back(std::make_unique(file)); - } - - for (const auto& text : attachment_container.GetTextAttachments()) { - attachments.push_back(std::make_unique(text)); - } - - for (const auto& wifi_credentials : - attachment_container.GetWifiCredentialsAttachments()) { - attachments.push_back( - std::make_unique(wifi_credentials)); - } - - return attachments; -} - int64_t ShareTarget::GetTotalAttachmentsSize() const { return attachment_container.GetTotalAttachmentsSize(); } diff --git a/sharing/share_target.h b/sharing/share_target.h index 03eae357..c81475f9 100644 --- a/sharing/share_target.h +++ b/sharing/share_target.h @@ -16,13 +16,11 @@ #define THIRD_PARTY_NEARBY_SHARING_SHARE_TARGET_H_ #include -#include #include #include #include #include "internal/network/url.h" -#include "sharing/attachment.h" #include "sharing/attachment_container.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/file_attachment.h" @@ -50,7 +48,6 @@ struct ShareTarget { ~ShareTarget(); std::vector GetAttachmentIds() const; - std::vector> GetAttachments() const; int64_t GetTotalAttachmentsSize() const; std::string ToString() const;