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