Commit fb578abb authored by Richard Knoll's avatar Richard Knoll Committed by Commit Bot

[Nearby] Refactor NearbySharingService Send* methods

Instead of SendText and SendFiles methods this now has one unified
SendAttachments methods to make it easier to handle Attachments coming
in from the UI. Also hooks it up through the DiscoveryManager all the
way to NearbySharingService. File URLs coming from the ShareSheet on
ChromeOS are converted to FileAttachments.

Bug: 1085067
Change-Id: I61ce93842920ffa303ee71f4373c237b113948bc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2378026Reviewed-by: default avatarAlex Chau <alexchau@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Commit-Queue: Richard Knoll <knollr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#802242}
parent fdb8dc55
......@@ -18,6 +18,7 @@ source_set("share_target") {
"//base",
"//chrome/services/sharing/public/mojom",
"//chrome/services/sharing/public/mojom:nearby_share_target_types",
"//net",
"//url",
]
}
......@@ -23,6 +23,10 @@ Attachment::Attachment(int64_t id, Family family, int64_t size)
Attachment::Attachment(const Attachment&) = default;
Attachment::Attachment(Attachment&&) = default;
Attachment& Attachment::operator=(const Attachment&) = default;
Attachment& Attachment::operator=(Attachment&&) = default;
Attachment::~Attachment() = default;
......@@ -7,6 +7,8 @@
#include <stdint.h>
struct ShareTarget;
// A single attachment to be sent by / received from a ShareTarget, can be
// either a file or text.
class Attachment {
......@@ -16,7 +18,9 @@ class Attachment {
Attachment(Family family, int64_t size);
Attachment(int64_t id, Family family, int64_t size);
Attachment(const Attachment&);
Attachment(Attachment&&);
Attachment& operator=(const Attachment&);
Attachment& operator=(Attachment&&);
virtual ~Attachment();
int64_t id() const { return id_; }
......@@ -24,6 +28,8 @@ class Attachment {
int64_t size() const { return size_; }
void set_size(int64_t size) { size_ = size; }
virtual void MoveToShareTarget(ShareTarget& share_target) = 0;
private:
int64_t id_;
Family family_;
......
......@@ -2,33 +2,68 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nearby_sharing/file_attachment.h"
#include <utility>
#include "chrome/browser/nearby_sharing/file_attachment.h"
#include "base/strings/string_util.h"
#include "chrome/browser/nearby_sharing/share_target.h"
#include "net/base/mime_util.h"
FileAttachment::FileAttachment(std::string file_name,
Type type,
int64_t size,
base::Optional<base::FilePath> file_path,
std::string mime_type)
: Attachment(Attachment::Family::kFile, size),
file_name_(std::move(file_name)),
type_(type),
file_path_(std::move(file_path)),
mime_type_(std::move(mime_type)) {}
namespace {
FileAttachment::Type FileAttachmentTypeFromMimeType(
const std::string& mime_type) {
if (base::StartsWith(mime_type, "image/"))
return FileAttachment::Type::kImage;
if (base::StartsWith(mime_type, "video/"))
return FileAttachment::Type::kVideo;
if (base::StartsWith(mime_type, "audio/"))
return FileAttachment::Type::kAudio;
return FileAttachment::Type::kUnknown;
}
std::string MimeTypeFromPath(const base::FilePath& path) {
std::string mime_type = "application/octet-stream";
base::FilePath::StringType ext = path.Extension();
if (!ext.empty())
net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type);
return mime_type;
}
} // namespace
FileAttachment::FileAttachment(base::FilePath file_path)
: Attachment(Attachment::Family::kFile, /*size=*/0),
file_name_(file_path.BaseName().AsUTF8Unsafe()),
mime_type_(MimeTypeFromPath(file_path)),
type_(FileAttachmentTypeFromMimeType(mime_type_)),
file_path_(std::move(file_path)) {}
FileAttachment::FileAttachment(int64_t id,
std::string file_name,
Type type,
int64_t size,
std::string mime_type)
std::string file_name,
std::string mime_type,
Type type)
: Attachment(id, Attachment::Family::kFile, size),
file_name_(std::move(file_name)),
type_(type),
mime_type_(std::move(mime_type)) {}
mime_type_(std::move(mime_type)),
type_(type) {}
FileAttachment::FileAttachment(const FileAttachment&) = default;
FileAttachment::FileAttachment(FileAttachment&&) = default;
FileAttachment& FileAttachment::operator=(const FileAttachment&) = default;
FileAttachment& FileAttachment::operator=(FileAttachment&&) = default;
FileAttachment::~FileAttachment() = default;
void FileAttachment::MoveToShareTarget(ShareTarget& share_target) {
share_target.file_attachments.push_back(std::move(*this));
}
......@@ -18,30 +18,31 @@ class FileAttachment : public Attachment {
public:
using Type = sharing::mojom::FileMetadata::Type;
FileAttachment(std::string file_name,
Type type,
int64_t size,
base::Optional<base::FilePath> file_path,
std::string mime_type);
explicit FileAttachment(base::FilePath file_path);
FileAttachment(int64_t id,
std::string file_name,
Type type,
int64_t size,
std::string mime_type);
std::string file_name,
std::string mime_type,
Type type);
FileAttachment(const FileAttachment&);
FileAttachment(FileAttachment&&);
FileAttachment& operator=(const FileAttachment&);
FileAttachment& operator=(FileAttachment&&);
~FileAttachment() override;
const std::string& file_name() const { return file_name_; }
const std::string& mime_type() const { return mime_type_; }
Type type() const { return type_; }
const base::Optional<base::FilePath>& file_path() const { return file_path_; }
const std::string& mime_type() const { return mime_type_; }
// Attachment:
void MoveToShareTarget(ShareTarget& share_target) override;
private:
std::string file_name_;
std::string mime_type_;
Type type_;
base::Optional<base::FilePath> file_path_;
std::string mime_type_;
};
#endif // CHROME_BROWSER_NEARBY_SHARING_FILE_ATTACHMENT_H_
......@@ -33,12 +33,8 @@ class MockNearbySharingService : public NearbySharingService {
(TransferUpdateCallback*),
(override));
MOCK_METHOD(StatusCodes,
SendText,
(const ShareTarget&, std::string),
(override));
MOCK_METHOD(StatusCodes,
SendFiles,
(const ShareTarget&, const std::vector<base::FilePath>&),
SendAttachments,
(const ShareTarget&, std::vector<std::unique_ptr<Attachment>>),
(override));
MOCK_METHOD(void,
Accept,
......
......@@ -58,10 +58,8 @@ TextAttachment CreateTextAttachment(TextAttachment::Type type) {
}
FileAttachment CreateFileAttachment(FileAttachment::Type type) {
return FileAttachment(/*file_name=*/"file.jpg", type,
/*size=*/10,
/*file_path=*/base::nullopt,
/*mime_type=*/"example");
return FileAttachment(/*id=*/0, /*size=*/10, /*file_name=*/"file.jpg",
/*mime_type=*/"example", type);
}
std::unique_ptr<KeyedService> CreateMockNearbySharingService(
......
......@@ -39,8 +39,10 @@ base::Optional<nearby_share::mojom::TransferStatus> GetTransferStatus(
} // namespace
NearbyPerSessionDiscoveryManager::NearbyPerSessionDiscoveryManager(
NearbySharingService* nearby_sharing_service)
: nearby_sharing_service_(nearby_sharing_service) {}
NearbySharingService* nearby_sharing_service,
std::vector<std::unique_ptr<Attachment>> attachments)
: nearby_sharing_service_(nearby_sharing_service),
attachments_(std::move(attachments)) {}
NearbyPerSessionDiscoveryManager::~NearbyPerSessionDiscoveryManager() {
UnregisterSendSurface();
......@@ -113,9 +115,9 @@ void NearbyPerSessionDiscoveryManager::SelectShareTarget(
mojo::PendingReceiver<nearby_share::mojom::TransferUpdateListener> receiver =
transfer_update_listener_.BindNewPipeAndPassReceiver();
// TODO(crbug.com/1099710): Call correct method and pass attachments.
NearbySharingService::StatusCodes status =
nearby_sharing_service_->SendText(iter->second, "Example Text");
nearby_sharing_service_->SendAttachments(iter->second,
std::move(attachments_));
// If the send call succeeded, we expect OnTransferUpdate() to be called next.
if (status == NearbySharingService::StatusCodes::kOk) {
......
......@@ -5,9 +5,13 @@
#ifndef CHROME_BROWSER_NEARBY_SHARING_NEARBY_PER_SESSION_DISCOVERY_MANAGER_H_
#define CHROME_BROWSER_NEARBY_SHARING_NEARBY_PER_SESSION_DISCOVERY_MANAGER_H_
#include <memory>
#include <vector>
#include "base/containers/flat_map.h"
#include "base/memory/weak_ptr.h"
#include "base/unguessable_token.h"
#include "chrome/browser/nearby_sharing/attachment.h"
#include "chrome/browser/nearby_sharing/nearby_sharing_service.h"
#include "chrome/browser/nearby_sharing/share_target_discovered_callback.h"
#include "chrome/browser/nearby_sharing/transfer_update_callback.h"
......@@ -23,8 +27,9 @@ class NearbyPerSessionDiscoveryManager
public ShareTargetDiscoveredCallback,
public nearby_share::mojom::DiscoveryManager {
public:
explicit NearbyPerSessionDiscoveryManager(
NearbySharingService* nearby_sharing_service);
NearbyPerSessionDiscoveryManager(
NearbySharingService* nearby_sharing_service,
std::vector<std::unique_ptr<Attachment>> attachments);
~NearbyPerSessionDiscoveryManager() override;
// TransferUpdateCallback:
......@@ -47,6 +52,7 @@ class NearbyPerSessionDiscoveryManager
void UnregisterSendSurface();
NearbySharingService* nearby_sharing_service_;
std::vector<std::unique_ptr<Attachment>> attachments_;
mojo::Remote<nearby_share::mojom::ShareTargetListener> share_target_listener_;
mojo::Remote<nearby_share::mojom::TransferUpdateListener>
transfer_update_listener_;
......
......@@ -28,6 +28,25 @@ MATCHER_P(MatchesTarget, target, "") {
return arg.id == target.id;
}
const char kTextAttachmentBody[] = "Test text payload";
std::vector<std::unique_ptr<Attachment>> CreateAttachments() {
std::vector<std::unique_ptr<Attachment>> attachments;
attachments.push_back(std::make_unique<TextAttachment>(
TextAttachment::Type::kText, kTextAttachmentBody));
return attachments;
}
void ExpectTextAttachment(
const std::string& text_body,
const std::vector<std::unique_ptr<Attachment>>& attachments) {
ASSERT_EQ(1u, attachments.size());
ASSERT_TRUE(attachments[0]);
ASSERT_EQ(Attachment::Family::kText, attachments[0]->family());
auto* text_attachment = static_cast<TextAttachment*>(attachments[0].get());
EXPECT_EQ(text_body, text_attachment->text_body());
}
class MockShareTargetListener
: public nearby_share::mojom::ShareTargetListener {
public:
......@@ -84,7 +103,8 @@ class NearbyPerSessionDiscoveryManagerTest : public testing::Test {
private:
content::BrowserTaskEnvironment task_environment_;
MockNearbySharingService sharing_service_;
NearbyPerSessionDiscoveryManager manager_{&sharing_service_};
NearbyPerSessionDiscoveryManager manager_{&sharing_service_,
CreateAttachments()};
};
} // namespace
......@@ -95,7 +115,8 @@ TEST_F(NearbyPerSessionDiscoveryManagerTest, CreateDestroyWithoutRegistering) {
EXPECT_CALL(sharing_service(), UnregisterSendSurface(&manager(), &manager()))
.Times(0);
{
NearbyPerSessionDiscoveryManager manager(&sharing_service());
NearbyPerSessionDiscoveryManager manager(&sharing_service(),
CreateAttachments());
// Creating and destroying an instance should not register itself with the
// NearbySharingService.
}
......@@ -202,11 +223,13 @@ TEST_F(NearbyPerSessionDiscoveryManagerTest, SelectShareTarget_SendSuccess) {
EXPECT_CALL(callback, Run(nearby_share::mojom::SelectShareTargetResult::kOk,
testing::IsTrue(), testing::IsTrue()));
// TODO(crbug.com/1099710): Call correct method and pass attachments.
EXPECT_CALL(sharing_service(), SendText(_, _))
EXPECT_CALL(sharing_service(), SendAttachments(_, _))
.WillOnce(testing::Invoke(
[&share_target](const ShareTarget& target, std::string text) {
[&share_target](
const ShareTarget& target,
std::vector<std::unique_ptr<Attachment>> attachments) {
EXPECT_EQ(share_target.id, target.id);
ExpectTextAttachment(kTextAttachmentBody, attachments);
return NearbySharingService::StatusCodes::kOk;
}));
......@@ -230,11 +253,13 @@ TEST_F(NearbyPerSessionDiscoveryManagerTest, SelectShareTarget_SendError) {
Run(nearby_share::mojom::SelectShareTargetResult::kError,
testing::IsFalse(), testing::IsFalse()));
// TODO(crbug.com/1099710): Call correct method and pass attachments.
EXPECT_CALL(sharing_service(), SendText(_, _))
EXPECT_CALL(sharing_service(), SendAttachments(_, _))
.WillOnce(testing::Invoke(
[&share_target](const ShareTarget& target, std::string text) {
[&share_target](
const ShareTarget& target,
std::vector<std::unique_ptr<Attachment>> attachments) {
EXPECT_EQ(share_target.id, target.id);
ExpectTextAttachment(kTextAttachmentBody, attachments);
return NearbySharingService::StatusCodes::kError;
}));
......@@ -276,8 +301,7 @@ TEST_F(NearbyPerSessionDiscoveryManagerTest, OnTransferUpdate_WaitRemote) {
mojo::PendingRemote<nearby_share::mojom::ConfirmationManager>
manager) { transfer_listener.Bind(std::move(listener)); }));
// TODO(crbug.com/1099710): Call correct method and pass attachments.
EXPECT_CALL(sharing_service(), SendText(_, _))
EXPECT_CALL(sharing_service(), SendAttachments(_, _))
.WillOnce(testing::Return(NearbySharingService::StatusCodes::kOk));
manager().SelectShareTarget(share_target.id, callback.Get());
......@@ -328,8 +352,7 @@ TEST_F(NearbyPerSessionDiscoveryManagerTest, OnTransferUpdate_WaitLocal) {
mojo::PendingRemote<nearby_share::mojom::ConfirmationManager>
manager) { transfer_listener.Bind(std::move(listener)); }));
// TODO(crbug.com/1099710): Call correct method and pass attachments.
EXPECT_CALL(sharing_service(), SendText(_, _))
EXPECT_CALL(sharing_service(), SendAttachments(_, _))
.WillOnce(testing::Return(NearbySharingService::StatusCodes::kOk));
manager().SelectShareTarget(share_target.id, callback.Get());
......
......@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARING_SERVICE_H_
#define CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARING_SERVICE_H_
#include <memory>
#include <string>
#include <vector>
......@@ -87,13 +88,10 @@ class NearbySharingService : public KeyedService {
virtual StatusCodes UnregisterReceiveSurface(
TransferUpdateCallback* transfer_callback) = 0;
// Sends text to the remote |share_target|.
virtual StatusCodes SendText(const ShareTarget& share_target,
std::string text) = 0;
// Sends files to the remote |share_target|.
virtual StatusCodes SendFiles(const ShareTarget& share_target,
const std::vector<base::FilePath>& files) = 0;
// Sends |attachments| to the remote |share_target|.
virtual StatusCodes SendAttachments(
const ShareTarget& share_target,
std::vector<std::unique_ptr<Attachment>> attachments) = 0;
// Accepts incoming share from the remote |share_target|.
virtual void Accept(const ShareTarget& share_target,
......
......@@ -10,7 +10,6 @@
#include "base/files/file.h"
#include "base/logging.h"
#include "base/numerics/checked_math.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/system/sys_info.h"
#include "base/task/post_task.h"
......@@ -40,7 +39,6 @@
#include "content/public/browser/storage_partition.h"
#include "crypto/random.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "net/base/mime_util.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/base/idle/idle.h"
#include "url/gurl.h"
......@@ -159,41 +157,6 @@ int64_t GeneratePayloadId() {
return payload_id;
}
TextAttachment TextAttachmentFromText(const std::string& text) {
return TextAttachment(TextAttachment::Type::kText, text);
}
FileAttachment::Type FileAttachmentTypeFromMimeType(
const std::string& mime_type) {
if (base::StartsWith(mime_type, "image/"))
return FileAttachment::Type::kImage;
if (base::StartsWith(mime_type, "video/"))
return FileAttachment::Type::kVideo;
if (base::StartsWith(mime_type, "audio/"))
return FileAttachment::Type::kAudio;
return FileAttachment::Type::kUnknown;
}
std::string MimeTypeFromPath(const base::FilePath path) {
std::string mime_type = "application/octet-stream";
base::FilePath::StringType ext = path.Extension();
if (!ext.empty())
net::GetWellKnownMimeTypeFromExtension(ext.substr(1), &mime_type);
return mime_type;
}
FileAttachment FileAttachmentFromPath(const base::FilePath path) {
std::string mime_type = MimeTypeFromPath(path);
// Size will be updated later asynchronously, see OnOpenFiles().
return FileAttachment(path.BaseName().AsUTF8Unsafe(),
FileAttachmentTypeFromMimeType(mime_type),
/*size=*/0, path, mime_type);
}
// Wraps a call to OnTransferUpdate() to filter any updates after receiving a
// final status.
class TransferUpdateDecorator : public TransferUpdateCallback {
......@@ -475,25 +438,6 @@ NearbySharingServiceImpl::UnregisterReceiveSurface(
return StatusCodes::kOk;
}
NearbySharingServiceImpl::StatusCodes NearbySharingServiceImpl::SendText(
const ShareTarget& share_target,
std::string text) {
ShareTarget share_target_copy = share_target;
share_target_copy.text_attachments.push_back(TextAttachmentFromText(text));
return SendAttachments(std::move(share_target_copy));
}
NearbySharingServiceImpl::StatusCodes NearbySharingServiceImpl::SendFiles(
const ShareTarget& share_target,
const std::vector<base::FilePath>& files) {
ShareTarget share_target_copy = share_target;
for (const base::FilePath& path : files)
share_target_copy.file_attachments.push_back(FileAttachmentFromPath(path));
return SendAttachments(std::move(share_target_copy));
}
void NearbySharingServiceImpl::Accept(
const ShareTarget& share_target,
StatusCodesCallback status_codes_callback) {
......@@ -1480,11 +1424,11 @@ NearbySharingService::StatusCodes NearbySharingServiceImpl::SendPayloads(
}
NearbySharingService::StatusCodes NearbySharingServiceImpl::SendAttachments(
ShareTarget share_target) {
const ShareTarget& share_target,
std::vector<std::unique_ptr<Attachment>> attachments) {
if (!is_scanning_) {
NS_LOG(WARNING)
<< __func__
<< ": Failed to send file to remote ShareTarget. Not scanning.";
NS_LOG(WARNING) << __func__
<< ": Failed to send attachments. Not scanning.";
return StatusCodes::kError;
}
......@@ -1497,9 +1441,19 @@ NearbySharingService::StatusCodes NearbySharingServiceImpl::SendAttachments(
ShareTargetInfo* info = GetShareTargetInfo(share_target);
if (!info || !info->endpoint_id()) {
// TODO(crbug.com/1119276): Support scanning for unknown share targets.
NS_LOG(WARNING)
<< __func__
<< ": Failed to send file to remote ShareTarget. Unknown ShareTarget.";
NS_LOG(WARNING) << __func__
<< ": Failed to send attachments. Unknown ShareTarget.";
return StatusCodes::kError;
}
ShareTarget share_target_copy = share_target;
for (std::unique_ptr<Attachment>& attachment : attachments) {
DCHECK(attachment);
attachment->MoveToShareTarget(share_target_copy);
}
if (!share_target_copy.has_attachments()) {
NS_LOG(WARNING) << __func__ << ": No attachments to send.";
return StatusCodes::kError;
}
......@@ -1523,11 +1477,11 @@ NearbySharingService::StatusCodes NearbySharingServiceImpl::SendAttachments(
// Send process initialized successfully, from now on status updated will be
// sent out via OnOutgoingTransferUpdate().
info->transfer_update_callback()->OnTransferUpdate(
share_target, TransferMetadataBuilder()
.set_status(TransferMetadata::Status::kConnecting)
.build());
share_target_copy, TransferMetadataBuilder()
.set_status(TransferMetadata::Status::kConnecting)
.build());
CreatePayloads(std::move(share_target),
CreatePayloads(std::move(share_target_copy),
base::BindOnce(&NearbySharingServiceImpl::OnCreatePayloads,
weak_ptr_factory_.GetWeakPtr(),
std::move(*endpoint_info)));
......@@ -2181,8 +2135,8 @@ void NearbySharingServiceImpl::OnReceivedIntroduction(
NS_LOG(VERBOSE) << __func__ << "Found file attachment " << file->name
<< " of type " << file->type << " with mimeType "
<< file->mime_type;
FileAttachment attachment(file->id, file->name, file->type, file->size,
file->mime_type);
FileAttachment attachment(file->id, file->size, file->name, file->mime_type,
file->type);
SetAttachmentPayloadId(attachment, file->payload_id);
share_target.file_attachments.push_back(std::move(attachment));
......
......@@ -79,10 +79,9 @@ class NearbySharingServiceImpl
ReceiveSurfaceState state) override;
StatusCodes UnregisterReceiveSurface(
TransferUpdateCallback* transfer_callback) override;
StatusCodes SendText(const ShareTarget& share_target,
std::string text) override;
StatusCodes SendFiles(const ShareTarget& share_target,
const std::vector<base::FilePath>& files) override;
StatusCodes SendAttachments(
const ShareTarget& share_target,
std::vector<std::unique_ptr<Attachment>> attachments) override;
void Accept(const ShareTarget& share_target,
StatusCodesCallback status_codes_callback) override;
void Reject(const ShareTarget& share_target,
......@@ -166,7 +165,6 @@ class NearbySharingServiceImpl
StatusCodes ReceivePayloads(const ShareTarget& share_target);
StatusCodes SendPayloads(const ShareTarget& share_target);
StatusCodes SendAttachments(ShareTarget share_target);
void OnOutgoingConnection(const ShareTarget& share_target,
NearbyConnection* connection);
void SendIntroduction(const ShareTarget& share_target,
......
......@@ -268,6 +268,26 @@ int64_t GetFreeSpaceInDownloadPath(Profile* profile) {
return free_space;
}
std::vector<std::unique_ptr<Attachment>> CreateTextAttachments(
std::vector<std::string> texts) {
std::vector<std::unique_ptr<Attachment>> attachments;
for (auto& text : texts) {
attachments.push_back(std::make_unique<TextAttachment>(
TextAttachment::Type::kText, std::move(text)));
}
return attachments;
}
std::vector<std::unique_ptr<Attachment>> CreateFileAttachments(
std::vector<base::FilePath> file_paths) {
std::vector<std::unique_ptr<Attachment>> attachments;
for (auto& file_path : file_paths) {
attachments.push_back(
std::make_unique<FileAttachment>(std::move(file_path)));
}
return attachments;
}
class NearbySharingServiceImplTest : public testing::Test {
public:
NearbySharingServiceImplTest()
......@@ -2340,8 +2360,9 @@ TEST_F(NearbySharingServiceImplTest, RegisterReceiveSurfaceWhileSending) {
TransferMetadata::Status::kAwaitingLocalConfirmation,
TransferMetadata::Status::kAwaitingRemoteAcceptance},
run_loop.QuitClosure());
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendText(target, kTextPayload));
EXPECT_EQ(
NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendAttachments(target, CreateTextAttachments({kTextPayload})));
run_loop.Run();
NearbySharingService::StatusCodes result = service_->RegisterReceiveSurface(
......@@ -2353,6 +2374,17 @@ TEST_F(NearbySharingServiceImplTest, RegisterReceiveSurfaceWhileSending) {
service_->UnregisterSendSurface(&transfer_callback, &discovery_callback);
}
TEST_F(NearbySharingServiceImplTest, SendAttachments_WithoutAttachments) {
MockTransferUpdateCallback transfer_callback;
MockShareTargetDiscoveredCallback discovery_callback;
ShareTarget target =
DiscoverShareTarget(transfer_callback, discovery_callback);
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kError,
service_->SendAttachments(target, /*attachments=*/{}));
service_->UnregisterSendSurface(&transfer_callback, &discovery_callback);
}
TEST_F(NearbySharingServiceImplTest, SendText_AlreadySending) {
MockTransferUpdateCallback transfer_callback;
MockShareTargetDiscoveredCallback discovery_callback;
......@@ -2365,21 +2397,24 @@ TEST_F(NearbySharingServiceImplTest, SendText_AlreadySending) {
TransferMetadata::Status::kAwaitingLocalConfirmation,
TransferMetadata::Status::kAwaitingRemoteAcceptance},
run_loop.QuitClosure());
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendText(target, kTextPayload));
EXPECT_EQ(
NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendAttachments(target, CreateTextAttachments({kTextPayload})));
run_loop.Run();
// We're now in the sending state, try to send again should fail
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kError,
service_->SendText(target, kTextPayload));
EXPECT_EQ(
NearbySharingServiceImpl::StatusCodes::kError,
service_->SendAttachments(target, CreateTextAttachments({kTextPayload})));
service_->UnregisterSendSurface(&transfer_callback, &discovery_callback);
}
TEST_F(NearbySharingServiceImplTest, SendText_WithoutScanning) {
ShareTarget target;
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kError,
service_->SendText(target, kTextPayload));
EXPECT_EQ(
NearbySharingServiceImpl::StatusCodes::kError,
service_->SendAttachments(target, CreateTextAttachments({kTextPayload})));
}
TEST_F(NearbySharingServiceImplTest, SendText_UnknownTarget) {
......@@ -2388,8 +2423,9 @@ TEST_F(NearbySharingServiceImplTest, SendText_UnknownTarget) {
DiscoverShareTarget(transfer_callback, discovery_callback);
ShareTarget target;
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kError,
service_->SendText(target, kTextPayload));
EXPECT_EQ(
NearbySharingServiceImpl::StatusCodes::kError,
service_->SendAttachments(target, CreateTextAttachments({kTextPayload})));
service_->UnregisterSendSurface(&transfer_callback, &discovery_callback);
}
......@@ -2402,8 +2438,9 @@ TEST_F(NearbySharingServiceImplTest, SendText_FailedCreateEndpointInfo) {
ShareTarget target =
DiscoverShareTarget(transfer_callback, discovery_callback);
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kError,
service_->SendText(target, kTextPayload));
EXPECT_EQ(
NearbySharingServiceImpl::StatusCodes::kError,
service_->SendAttachments(target, CreateTextAttachments({kTextPayload})));
service_->UnregisterSendSurface(&transfer_callback, &discovery_callback);
}
......@@ -2422,8 +2459,9 @@ TEST_F(NearbySharingServiceImplTest, SendText_FailedToConnect) {
TransferMetadata::Status::kFailed},
run_loop.QuitClosure());
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendText(target, kTextPayload));
EXPECT_EQ(
NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendAttachments(target, CreateTextAttachments({kTextPayload})));
run_loop.Run();
service_->UnregisterSendSurface(&transfer_callback, &discovery_callback);
......@@ -2447,8 +2485,9 @@ TEST_F(NearbySharingServiceImplTest, SendText_FailedKeyVerification) {
kToken);
fake_nearby_connections_manager_->set_nearby_connection(&connection_);
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendText(target, kTextPayload));
EXPECT_EQ(
NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendAttachments(target, CreateTextAttachments({kTextPayload})));
run_loop.Run();
service_->UnregisterSendSurface(&transfer_callback, &discovery_callback);
......@@ -2473,8 +2512,9 @@ TEST_F(NearbySharingServiceImplTest, SendText_UnableToVerifyKey) {
kToken);
fake_nearby_connections_manager_->set_nearby_connection(&connection_);
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendText(target, kTextPayload));
EXPECT_EQ(
NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendAttachments(target, CreateTextAttachments({kTextPayload})));
run_loop.Run();
service_->UnregisterSendSurface(&transfer_callback, &discovery_callback);
......@@ -2493,8 +2533,9 @@ TEST_P(NearbySharingServiceImplSendFailureTest, SendText_RemoteFailure) {
TransferMetadata::Status::kAwaitingRemoteAcceptance},
introduction_run_loop.QuitClosure());
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendText(target, kTextPayload));
EXPECT_EQ(
NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendAttachments(target, CreateTextAttachments({kTextPayload})));
introduction_run_loop.Run();
// Verify data sent to the remote device so far.
......@@ -2533,7 +2574,7 @@ TEST_P(NearbySharingServiceImplSendFailureTest, SendFiles_RemoteFailure) {
introduction_run_loop.QuitClosure());
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendFiles(target, {path}));
service_->SendAttachments(target, CreateFileAttachments({path})));
introduction_run_loop.Run();
// Verify data sent to the remote device so far.
......@@ -2572,8 +2613,9 @@ TEST_F(NearbySharingServiceImplTest, SendText_Success) {
TransferMetadata::Status::kAwaitingRemoteAcceptance},
introduction_run_loop.QuitClosure());
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendText(target, kTextPayload));
EXPECT_EQ(
NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendAttachments(target, CreateTextAttachments({kTextPayload})));
introduction_run_loop.Run();
// Verify data sent to the remote device so far.
......@@ -2647,7 +2689,7 @@ TEST_F(NearbySharingServiceImplTest, SendFiles_Success) {
introduction_run_loop.QuitClosure());
EXPECT_EQ(NearbySharingServiceImpl::StatusCodes::kOk,
service_->SendFiles(target, {path}));
service_->SendAttachments(target, CreateFileAttachments({path})));
introduction_run_loop.Run();
// Verify data sent to the remote device so far.
......
......@@ -6,6 +6,7 @@
#include <memory>
#include "base/files/file_path.h"
#include "base/test/mock_callback.h"
#include "chrome/browser/nearby_sharing/constants.h"
#include "chrome/browser/nearby_sharing/transfer_metadata_builder.h"
......@@ -37,10 +38,8 @@ class PayloadTrackerTest : public testing::Test {
void SetUp() override {
for (int i = 0; i < kAttachmentCount / 2; i++) {
FileAttachment file(/*file_name=*/"file.jpg",
FileAttachment::Type::kImage, kTotalSize,
/*file_path=*/base::nullopt,
/*mime_type=*/"example");
FileAttachment file(base::FilePath(FILE_PATH_LITERAL("file.jpg")));
file.set_size(kTotalSize);
AttachmentInfo info;
info.payload_id = i;
......
......@@ -4,19 +4,66 @@
#include "chrome/browser/nearby_sharing/sharesheet/nearby_share_action.h"
#include <memory>
#include <vector>
#include "base/files/file_path.h"
#include "base/logging.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/chromeos/file_manager/app_id.h"
#include "chrome/browser/chromeos/file_manager/fileapi_util.h"
#include "chrome/browser/nearby_sharing/attachment.h"
#include "chrome/browser/nearby_sharing/file_attachment.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/sharesheet/sharesheet_types.h"
#include "chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_url.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/controls/webview/webview.h"
#include "url/gurl.h"
namespace {
std::vector<base::FilePath> ResolveFileUrls(
Profile* profile,
const std::vector<GURL>& file_urls) {
std::vector<base::FilePath> file_paths;
storage::FileSystemContext* fs_context =
file_manager::util::GetFileSystemContextForExtensionId(
profile, file_manager::kFileManagerAppId);
for (const auto& file_url : file_urls) {
storage::FileSystemURL fs_url = fs_context->CrackURL(file_url);
file_paths.push_back(fs_url.path());
}
return file_paths;
}
std::vector<std::unique_ptr<Attachment>> CreateAttachmentsFromIntent(
Profile* profile,
apps::mojom::IntentPtr intent) {
std::vector<std::unique_ptr<Attachment>> attachments;
// TODO(knollr): Support other attachment types.
if (intent->file_urls) {
std::vector<base::FilePath> file_paths =
ResolveFileUrls(profile, *intent->file_urls);
for (auto& file_path : file_paths) {
attachments.push_back(
std::make_unique<FileAttachment>(std::move(file_path)));
}
}
return attachments;
}
} // namespace
namespace {
......@@ -72,7 +119,8 @@ void NearbyShareAction::LaunchAction(
DCHECK(nearby_ui_ != nullptr);
nearby_ui_->AddObserver(this);
nearby_ui_->SetShareIntent(std::move(intent));
nearby_ui_->SetAttachments(
CreateAttachmentsFromIntent(profile, std::move(intent)));
}
void NearbyShareAction::OnClose() {
......
......@@ -6,6 +6,7 @@
#include <utility>
#include "base/strings/strcat.h"
#include "chrome/browser/nearby_sharing/share_target.h"
#include "chrome/browser/nearby_sharing/text_attachment.h"
#include "url/gurl.h"
......@@ -109,6 +110,14 @@ TextAttachment::TextAttachment(int64_t id,
TextAttachment::TextAttachment(const TextAttachment&) = default;
TextAttachment::TextAttachment(TextAttachment&&) = default;
TextAttachment& TextAttachment::operator=(const TextAttachment&) = default;
TextAttachment& TextAttachment::operator=(TextAttachment&&) = default;
TextAttachment::~TextAttachment() = default;
void TextAttachment::MoveToShareTarget(ShareTarget& share_target) {
share_target.text_attachments.push_back(std::move(*this));
}
......@@ -19,13 +19,18 @@ class TextAttachment : public Attachment {
TextAttachment(Type type, std::string text_body);
TextAttachment(int64_t id, Type type, std::string text_title, int64_t size);
TextAttachment(const TextAttachment&);
TextAttachment(TextAttachment&&);
TextAttachment& operator=(const TextAttachment&);
TextAttachment& operator=(TextAttachment&&);
~TextAttachment() override;
const std::string& text_body() const { return text_body_; }
const std::string& text_title() const { return text_title_; }
Type type() const { return type_; }
// Attachment:
void MoveToShareTarget(ShareTarget& share_target) override;
private:
Type type_;
std::string text_title_;
......
......@@ -1503,6 +1503,7 @@ static_library("ui") {
"//chrome/browser:theme_properties",
"//chrome/browser/media/kaleidoscope/mojom",
"//chrome/browser/media/router",
"//chrome/browser/nearby_sharing:share_target",
"//chrome/browser/nearby_sharing/certificates",
"//chrome/browser/nearby_sharing/client",
"//chrome/browser/nearby_sharing/contacts",
......
......@@ -4,10 +4,15 @@
#include "chrome/browser/ui/webui/nearby_internals/nearby_internals_ui_trigger_handler.h"
#include <memory>
#include <vector>
#include "base/bind.h"
#include "base/time/time.h"
#include "chrome/browser/nearby_sharing/attachment.h"
#include "chrome/browser/nearby_sharing/logging/logging.h"
#include "chrome/browser/nearby_sharing/nearby_sharing_service_factory.h"
#include "chrome/browser/nearby_sharing/text_attachment.h"
#include "chrome/services/sharing/public/mojom/nearby_share_target_types.mojom.h"
namespace {
......@@ -414,11 +419,16 @@ void NearbyInternalsUiTriggerHandler::SendText(const base::ListValue* args) {
return;
}
std::vector<std::unique_ptr<Attachment>> attachments;
attachments.push_back(std::make_unique<TextAttachment>(
TextAttachment::Type::kText, kPayloadExample));
const base::Value& callback_id = args->GetList()[0];
ResolveJavascriptCallback(
callback_id,
StatusCodeToDictionary(service_->SendText(it->second, kPayloadExample),
TriggerEvent::kSendText));
StatusCodeToDictionary(
service_->SendAttachments(it->second, std::move(attachments)),
TriggerEvent::kSendText));
}
void NearbyInternalsUiTriggerHandler::Accept(const base::ListValue* args) {
......
......@@ -72,16 +72,19 @@ void NearbyShareDialogUI::RemoveObserver(
observers_.RemoveObserver(observer);
}
void NearbyShareDialogUI::SetShareIntent(apps::mojom::IntentPtr intent) {
intent_ = std::move(intent);
void NearbyShareDialogUI::SetAttachments(
std::vector<std::unique_ptr<Attachment>> attachments) {
attachments_ = std::move(attachments);
}
void NearbyShareDialogUI::BindInterface(
mojo::PendingReceiver<mojom::DiscoveryManager> manager) {
mojo::MakeSelfOwnedReceiver(
std::make_unique<NearbyPerSessionDiscoveryManager>(nearby_service_),
std::make_unique<NearbyPerSessionDiscoveryManager>(
nearby_service_, std::move(attachments_)),
std::move(manager));
}
void NearbyShareDialogUI::BindInterface(
mojo::PendingReceiver<mojom::NearbyShareSettings> receiver) {
NearbySharingService* nearby_sharing_service =
......
......@@ -5,11 +5,14 @@
#ifndef CHROME_BROWSER_UI_WEBUI_NEARBY_SHARE_NEARBY_SHARE_DIALOG_UI_H_
#define CHROME_BROWSER_UI_WEBUI_NEARBY_SHARE_NEARBY_SHARE_DIALOG_UI_H_
#include <memory>
#include <vector>
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "chrome/browser/nearby_sharing/attachment.h"
#include "chrome/browser/ui/webui/nearby_share/nearby_share.mojom.h"
#include "chrome/browser/ui/webui/nearby_share/public/mojom/nearby_share_settings.mojom.h"
#include "components/services/app_service/public/mojom/types.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "ui/webui/mojo_web_ui_controller.h"
......@@ -32,7 +35,7 @@ class NearbyShareDialogUI : public ui::MojoWebUIController {
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
void SetShareIntent(apps::mojom::IntentPtr intent);
void SetAttachments(std::vector<std::unique_ptr<Attachment>> attachments);
// Instantiates the implementor of the mojom::DiscoveryManager mojo
// interface passing the pending receiver that will be internally bound.
......@@ -43,7 +46,7 @@ class NearbyShareDialogUI : public ui::MojoWebUIController {
private:
void HandleClose(const base::ListValue* args);
apps::mojom::IntentPtr intent_;
std::vector<std::unique_ptr<Attachment>> attachments_;
base::ObserverList<Observer> observers_;
NearbySharingService* nearby_service_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment