Commit aa6970c7 authored by Himanshu Jaju's avatar Himanshu Jaju Committed by Commit Bot

Implement IsOutOfStorage for incoming attachments in Nearby Share

- Checks if incoming files are store-able in the Downloads section
- Adds a test to get Fail incoming connection due to storage crunch
- PRD for decision on storing in Downloads (go/cros-nearby)

Bug: 1085068
Change-Id: Ibee41fce67d0da5b904112b4b9970215ae874b60
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2336033
Commit-Queue: Himanshu Jaju <himanshujaju@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Reviewed-by: default avatarRichard Knoll <knollr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799096}
parent e445f27f
...@@ -8,10 +8,13 @@ ...@@ -8,10 +8,13 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/numerics/checked_math.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/system/sys_info.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/task_runner_util.h" #include "base/task_runner_util.h"
#include "base/threading/sequenced_task_runner_handle.h" #include "base/threading/sequenced_task_runner_handle.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_certificate_manager_impl.h" #include "chrome/browser/nearby_sharing/certificates/nearby_share_certificate_manager_impl.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_encrypted_metadata_key.h" #include "chrome/browser/nearby_sharing/certificates/nearby_share_encrypted_metadata_key.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_client_impl.h" #include "chrome/browser/nearby_sharing/client/nearby_share_client_impl.h"
...@@ -28,6 +31,7 @@ ...@@ -28,6 +31,7 @@
#include "chrome/services/sharing/public/cpp/advertisement.h" #include "chrome/services/sharing/public/cpp/advertisement.h"
#include "chrome/services/sharing/public/mojom/nearby_connections_types.mojom.h" #include "chrome/services/sharing/public/mojom/nearby_connections_types.mojom.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "content/public/browser/download_manager.h"
#include "device/bluetooth/bluetooth_adapter_factory.h" #include "device/bluetooth/bluetooth_adapter_factory.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"
...@@ -110,6 +114,11 @@ std::string ToFourDigitString(const std::vector<uint8_t>& bytes) { ...@@ -110,6 +114,11 @@ std::string ToFourDigitString(const std::vector<uint8_t>& bytes) {
return base::StringPrintf("%04d", std::abs(hash)); return base::StringPrintf("%04d", std::abs(hash));
} }
bool IsOutOfStorage(base::FilePath file_path, int64_t storage_required) {
int64_t free_space = base::SysInfo::AmountOfFreeDiskSpace(file_path);
return free_space < storage_required;
}
} // namespace } // namespace
NearbySharingServiceImpl::NearbySharingServiceImpl( NearbySharingServiceImpl::NearbySharingServiceImpl(
...@@ -1491,6 +1500,8 @@ void NearbySharingServiceImpl::OnReceivedIntroduction( ...@@ -1491,6 +1500,8 @@ void NearbySharingServiceImpl::OnReceivedIntroduction(
NS_LOG(INFO) << __func__ << ": Successfully read the introduction frame."; NS_LOG(INFO) << __func__ << ": Successfully read the introduction frame.";
base::CheckedNumeric<int64_t> file_size_sum(0);
sharing::mojom::IntroductionFramePtr introduction_frame = sharing::mojom::IntroductionFramePtr introduction_frame =
std::move((*frame)->get_introduction()); std::move((*frame)->get_introduction());
for (const auto& file : introduction_frame->file_metadata) { for (const auto& file : introduction_frame->file_metadata) {
...@@ -1509,6 +1520,15 @@ void NearbySharingServiceImpl::OnReceivedIntroduction( ...@@ -1509,6 +1520,15 @@ void NearbySharingServiceImpl::OnReceivedIntroduction(
file->mime_type); file->mime_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));
file_size_sum += file->size;
if (!file_size_sum.IsValid()) {
Fail(share_target, TransferMetadata::Status::kNotEnoughSpace);
NS_LOG(WARNING) << __func__
<< ": Ignoring introduction, total file size overflowed "
"64 bit integer.";
return;
}
} }
for (const auto& text : introduction_frame->text_metadata) { for (const auto& text : introduction_frame->text_metadata) {
...@@ -1541,7 +1561,31 @@ void NearbySharingServiceImpl::OnReceivedIntroduction( ...@@ -1541,7 +1561,31 @@ void NearbySharingServiceImpl::OnReceivedIntroduction(
return; return;
} }
if (IsOutOfStorage(share_target)) { if (file_size_sum.ValueOrDie() == 0) {
OnStorageCheckCompleted(std::move(share_target), std::move(token),
/*is_out_of_storage=*/false);
return;
}
base::FilePath download_path =
DownloadPrefs::FromDownloadManager(
content::BrowserContext::GetDownloadManager(profile_))
->DownloadPath();
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindOnce(&IsOutOfStorage, std::move(download_path),
file_size_sum.ValueOrDie()),
base::BindOnce(&NearbySharingServiceImpl::OnStorageCheckCompleted,
weak_ptr_factory_.GetWeakPtr(), std::move(share_target),
std::move(token)));
}
void NearbySharingServiceImpl::OnStorageCheckCompleted(
ShareTarget share_target,
base::Optional<std::string> token,
bool is_out_of_storage) {
if (is_out_of_storage) {
Fail(share_target, TransferMetadata::Status::kNotEnoughSpace); Fail(share_target, TransferMetadata::Status::kNotEnoughSpace);
NS_LOG(WARNING) << __func__ NS_LOG(WARNING) << __func__
<< ": Not enough space on the receiver. We have informed " << ": Not enough space on the receiver. We have informed "
...@@ -1549,6 +1593,13 @@ void NearbySharingServiceImpl::OnReceivedIntroduction( ...@@ -1549,6 +1593,13 @@ void NearbySharingServiceImpl::OnReceivedIntroduction(
return; return;
} }
NearbyConnection* connection = GetIncomingConnection(share_target);
if (!connection) {
NS_LOG(WARNING) << __func__ << ": Invalid connection for share target - "
<< share_target.device_name;
return;
}
mutual_acceptance_timeout_alarm_.Reset(base::BindOnce( mutual_acceptance_timeout_alarm_.Reset(base::BindOnce(
&NearbySharingServiceImpl::OnIncomingMutualAcceptanceTimeout, &NearbySharingServiceImpl::OnIncomingMutualAcceptanceTimeout,
weak_ptr_factory_.GetWeakPtr(), share_target)); weak_ptr_factory_.GetWeakPtr(), share_target));
...@@ -1646,11 +1697,6 @@ void NearbySharingServiceImpl::OnIncomingConnectionDisconnected( ...@@ -1646,11 +1697,6 @@ void NearbySharingServiceImpl::OnIncomingConnectionDisconnected(
UnregisterShareTarget(share_target); UnregisterShareTarget(share_target);
} }
bool NearbySharingServiceImpl::IsOutOfStorage(const ShareTarget& share_target) {
// TODO(himanshujaju) - Check storage space based on file path.
return false;
}
void NearbySharingServiceImpl::OnIncomingMutualAcceptanceTimeout( void NearbySharingServiceImpl::OnIncomingMutualAcceptanceTimeout(
const ShareTarget& share_target) { const ShareTarget& share_target) {
DCHECK(share_target.is_incoming); DCHECK(share_target.is_incoming);
......
...@@ -194,13 +194,15 @@ class NearbySharingServiceImpl ...@@ -194,13 +194,15 @@ class NearbySharingServiceImpl
void OnReceivedIntroduction(ShareTarget share_target, void OnReceivedIntroduction(ShareTarget share_target,
base::Optional<std::string> token, base::Optional<std::string> token,
base::Optional<sharing::mojom::V1FramePtr> frame); base::Optional<sharing::mojom::V1FramePtr> frame);
void OnStorageCheckCompleted(ShareTarget share_target,
base::Optional<std::string> token,
bool is_out_of_storage);
void OnFrameRead(ShareTarget share_target, void OnFrameRead(ShareTarget share_target,
base::Optional<sharing::mojom::V1FramePtr> frame); base::Optional<sharing::mojom::V1FramePtr> frame);
void HandleCertificateInfoFrame( void HandleCertificateInfoFrame(
const sharing::mojom::CertificateInfoFramePtr& certificate_frame); const sharing::mojom::CertificateInfoFramePtr& certificate_frame);
void OnIncomingConnectionDisconnected(const ShareTarget& share_target); void OnIncomingConnectionDisconnected(const ShareTarget& share_target);
bool IsOutOfStorage(const ShareTarget& share_target);
void OnIncomingMutualAcceptanceTimeout(const ShareTarget& share_target); void OnIncomingMutualAcceptanceTimeout(const ShareTarget& share_target);
base::Optional<ShareTarget> CreateShareTarget( base::Optional<ShareTarget> CreateShareTarget(
......
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