diff --git a/sharing/fake_nearby_sharing_service.cc b/sharing/fake_nearby_sharing_service.cc index 4da5d5cb..074970e4 100644 --- a/sharing/fake_nearby_sharing_service.cc +++ b/sharing/fake_nearby_sharing_service.cc @@ -172,7 +172,8 @@ bool FakeNearbySharingService::DidLocalUserCancelTransfer( // Opens attachments from the remote |share_target|. void FakeNearbySharingService::Open( - const ShareTarget& share_target, + ShareTarget share_target, + std::unique_ptr attachment_container, std::function status_codes_callback) { status_codes_callback(StatusCodes::kOk); } diff --git a/sharing/fake_nearby_sharing_service.h b/sharing/fake_nearby_sharing_service.h index dc80a639..ff898865 100644 --- a/sharing/fake_nearby_sharing_service.h +++ b/sharing/fake_nearby_sharing_service.h @@ -120,7 +120,8 @@ class FakeNearbySharingService : public NearbySharingService { bool DidLocalUserCancelTransfer(int64_t share_target_id) override; // Opens attachments from the remote |share_target|. - void Open(const ShareTarget& share_target, + void Open(ShareTarget share_target, + std::unique_ptr attachment_container, std::function status_codes_callback) override; diff --git a/sharing/nearby_sharing_service.h b/sharing/nearby_sharing_service.h index 23447f34..56d99990 100644 --- a/sharing/nearby_sharing_service.h +++ b/sharing/nearby_sharing_service.h @@ -236,9 +236,12 @@ class NearbySharingService { // |share_target|. virtual bool DidLocalUserCancelTransfer(int64_t share_target_id) = 0; - // Opens attachments from the remote |share_target|. + // Opens attachments in |attachment_container| from the remote |share_target|. + // If |attachment_container| is null, or the container is empty, the status + // code will be set to kInvalidArgument. virtual void Open( - const ShareTarget& share_target, + ShareTarget share_target, + std::unique_ptr attachment_container, std::function status_codes_callback) = 0; // Opens an url target on a browser instance. diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index 3f706979..4f0946fd 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -971,22 +971,24 @@ bool NearbySharingServiceImpl::DidLocalUserCancelTransfer( } void NearbySharingServiceImpl::Open( - const ShareTarget& share_target, + ShareTarget share_target, + std::unique_ptr attachment_container, std::function status_codes_callback) { RunOnAnyThread( - "api_open", [this, share_target, + "api_open", [this, share_target = std::move(share_target), + attachment_container = std::move(attachment_container), status_codes_callback = std::move(status_codes_callback)]() { + if (!attachment_container || !attachment_container->HasAttachments()) { + status_codes_callback(StatusCodes::kInvalidArgument); + return; + } NL_LOG(INFO) << __func__ << ": Open is called for share_target: " << share_target.ToString(); - // Log analytics event of opening received attachments. ShareTargetInfo* info = GetShareTargetInfo(share_target.id); analytics_recorder_->NewOpenReceivedAttachments( - share_target.attachment_container, - info != nullptr ? info->session_id() : 0); - - status_codes_callback( - service_extension_->Open(share_target.attachment_container)); + *attachment_container, info != nullptr ? info->session_id() : 0); + status_codes_callback(service_extension_->Open(*attachment_container)); }); } diff --git a/sharing/nearby_sharing_service_impl.h b/sharing/nearby_sharing_service_impl.h index 7a3d4bc3..137b3cc5 100644 --- a/sharing/nearby_sharing_service_impl.h +++ b/sharing/nearby_sharing_service_impl.h @@ -175,7 +175,8 @@ class NearbySharingServiceImpl std::function status_codes_callback) override; bool DidLocalUserCancelTransfer(int64_t share_target_id) override; - void Open(const ShareTarget& share_target, + void Open(ShareTarget share_target, + std::unique_ptr attachment_container, std::function status_codes_callback) override; void OpenUrl(const ::nearby::network::Url& url) override; diff --git a/sharing/nearby_sharing_service_impl_test.cc b/sharing/nearby_sharing_service_impl_test.cc index 83b63509..3654c158 100644 --- a/sharing/nearby_sharing_service_impl_test.cc +++ b/sharing/nearby_sharing_service_impl_test.cc @@ -4494,14 +4494,30 @@ TEST_F(NearbySharingServiceImplTest, OpenSharedTarget) { TextAttachment(TextMetadata::TEXT, "body", "title", "mime")); NearbySharingService::StatusCodes result; absl::Notification notification; - service_->Open(share_target, + service_->Open( + share_target, + std::make_unique(share_target.attachment_container), + [&](NearbySharingService::StatusCodes status_code) { + result = status_code; + notification.Notify(); + }); + + ASSERT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); + EXPECT_EQ(result, NearbySharingService::StatusCodes::kOk); +} + +TEST_F(NearbySharingServiceImplTest, OpenSharedTargetWithEmptyAttachments) { + ShareTarget share_target; + NearbySharingService::StatusCodes result; + absl::Notification notification; + service_->Open(share_target, std::make_unique(), [&](NearbySharingService::StatusCodes status_code) { result = status_code; notification.Notify(); }); ASSERT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); - EXPECT_EQ(result, NearbySharingService::StatusCodes::kOk); + EXPECT_EQ(result, NearbySharingService::StatusCodes::kInvalidArgument); } TEST_F(NearbySharingServiceImplTest,