Set file save path on a per session basis.

PiperOrigin-RevId: 866512037
This commit is contained in:
Francis Tsui
2026-02-06 10:16:09 -08:00
committed by Copybara-Service
parent 23fe8106b8
commit 183fdacb03
18 changed files with 113 additions and 18 deletions
@@ -196,6 +196,27 @@ std::string ClientProxy::GetConnectionToken(const std::string& endpoint_id) {
return {};
}
bool ClientProxy::OverrideSavePath(absl::string_view endpoint_id,
absl::string_view path) {
MutexLock lock(&mutex_);
ConnectionPair* item = LookupConnection(endpoint_id);
if (item != nullptr) {
item->first.save_path = path;
return true;
}
return false;
}
std::string ClientProxy::GetSavePath(
absl::string_view endpoint_id) const {
MutexLock lock(&mutex_);
const ConnectionPair* item = LookupConnection(endpoint_id);
if (item != nullptr) {
return item->first.save_path;
}
return "";
}
std::optional<MacAddress> ClientProxy::GetBluetoothMacAddress(
const std::string& endpoint_id) {
auto item = bluetooth_mac_addresses_.find(endpoint_id);
@@ -77,6 +77,13 @@ class ClientProxy final {
std::string GetLocalEndpointId();
std::string GetLocalEndpointInfo() { return local_endpoint_info_; }
// Override the base for received file attachments from a specific endpoint.
// Returns true if the endpoint is found and the path is overridden.
bool OverrideSavePath(absl::string_view endpoint_id, absl::string_view path);
// Get the save path for a specific endpoint. Returns empty string if
// not set.
std::string GetSavePath(absl::string_view endpoint_id) const;
analytics::AnalyticsRecorder& GetAnalyticsRecorder() const {
return *analytics_recorder_;
}
@@ -394,6 +401,7 @@ class ClientProxy final {
std::optional<location::nearby::connections::OsInfo> os_info;
std::int32_t safe_to_disconnect_version;
std::int32_t remote_multiplex_socket_bitmask;
std::string save_path;
};
using ConnectionPair = std::pair<Connection, PayloadListener>;
@@ -21,7 +21,6 @@
#include <utility>
#include <vector>
#include "base/casts.h"
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
@@ -65,6 +64,7 @@ using ::location::nearby::connections::OsInfo;
using ::location::nearby::proto::connections::CLIENT_SESSION;
using ::location::nearby::proto::connections::START_CLIENT_SESSION;
using ::location::nearby::proto::connections::STOP_CLIENT_SESSION;
using ::testing::IsEmpty;
using ::testing::MockFunction;
using ::testing::StrictMock;
@@ -1400,9 +1400,7 @@ TEST_F(ClientProxyTest, GetLocalDeviceWorksWithDeviceProvider) {
MockDeviceProvider provider;
client1()->RegisterDeviceProvider(&provider);
ASSERT_NE(client1()->GetLocalDeviceProvider(), nullptr);
EXPECT_CALL(*(absl::down_cast<MockDeviceProvider*>(
client1()->GetLocalDeviceProvider())),
GetLocalDevice);
EXPECT_CALL(provider, GetLocalDevice);
client1()->GetLocalDevice();
}
@@ -1612,6 +1610,23 @@ TEST_F(ClientProxyTest, NotLoadClientInfoFromPreferencesOnExpired) {
false);
}
TEST_F(ClientProxyTest, OverrideSavePath) {
Endpoint advertising_endpoint =
StartAdvertising(client1(), advertising_connection_listener_);
OnAdvertisingConnectionInitiated(client1(), advertising_endpoint);
client1()->OverrideSavePath(advertising_endpoint.id, "/tmp/test_path");
EXPECT_EQ(client1()->GetSavePath(advertising_endpoint.id), "/tmp/test_path");
}
TEST_F(ClientProxyTest, GetSavePathDefaultsToEmpty) {
Endpoint advertising_endpoint =
StartAdvertising(client1(), advertising_connection_listener_);
OnAdvertisingConnectionInitiated(client1(), advertising_endpoint);
EXPECT_THAT(client1()->GetSavePath(advertising_endpoint.id), IsEmpty());
}
} // namespace
} // namespace connections
} // namespace nearby
@@ -792,9 +792,12 @@ PayloadTransferFrame::PayloadChunk PayloadManager::CreatePayloadChunk(
ErrorOr<PayloadManager::PendingPayloadHandle>
PayloadManager::CreateIncomingPayload(const PayloadTransferFrame& frame,
const std::string& endpoint_id) {
const std::string& endpoint_id,
const std::string& save_path) {
ErrorOr<std::unique_ptr<InternalPayload>> result =
CreateIncomingInternalPayload(frame, custom_save_path_);
CreateIncomingInternalPayload(frame, save_path.empty()
? custom_save_path_
: save_path);
if (result.has_error()) {
return {result.error()};
}
@@ -1340,7 +1343,8 @@ void PayloadManager::ProcessDataPacket(
});
ErrorOr<PendingPayloadHandle> result =
CreateIncomingPayload(payload_transfer_frame, from_endpoint_id);
CreateIncomingPayload(payload_transfer_frame, from_endpoint_id,
to_client->GetSavePath(from_endpoint_id));
if (result.has_error()) {
LOG(WARNING) << "PayloadManager failed to create InternalPayload from "
"PayloadTransferFrame with payload_id="
+5 -1
View File
@@ -323,9 +323,13 @@ class PayloadManager : public EndpointManager::FrameProcessor {
LAST_CHUNK) != 0);
}
// Creates an incoming payload and returns a handle to it.
// If `save_path` is empty, the payload will be saved to the default save
// path set in `SetCustomSavePath()`.
ErrorOr<PendingPayloadHandle> CreateIncomingPayload(
const location::nearby::connections::PayloadTransferFrame& frame,
const std::string& endpoint_id) ABSL_LOCKS_EXCLUDED(mutex_);
const std::string& endpoint_id,
const std::string& save_path) ABSL_LOCKS_EXCLUDED(mutex_);
Payload::Id CreateOutgoingPayload(Payload payload,
const EndpointIds& endpoint_ids)