diff --git a/sharing/BUILD b/sharing/BUILD index da0faaeb..6aadfd1e 100644 --- a/sharing/BUILD +++ b/sharing/BUILD @@ -232,6 +232,8 @@ cc_library( "//internal/base:file_path", "//internal/base:files", "//internal/platform:types", + "//location/nearby/sharing/lib/sync:sync_config_prefs_cc_proto", + "//location/nearby/sharing/lib/sync:sync_manager", "//proto:sharing_enums_cc_proto", "//sharing/analytics", "//sharing/certificates", @@ -384,6 +386,7 @@ cc_library( "//internal/platform/implementation:types", "//location/nearby/sharing/lib/rpc:grpc_async_client_factory", "//location/nearby/sharing/lib/rpc:sharing_rpc_client", + "//location/nearby/sharing/lib/sync:sync_manager", "//proto:sharing_enums_cc_proto", "//sharing/analytics", "//sharing/certificates", diff --git a/sharing/incoming_share_session.cc b/sharing/incoming_share_session.cc index 2cb7fd91..870c7787 100644 --- a/sharing/incoming_share_session.cc +++ b/sharing/incoming_share_session.cc @@ -24,6 +24,8 @@ #include #include +#include "location/nearby/sharing/lib/sync/sync_config_prefs.pb.h" +#include "location/nearby/sharing/lib/sync/sync_manager.h" #include "absl/container/flat_hash_map.h" #include "absl/functional/any_invocable.h" #include "absl/time/time.h" @@ -38,7 +40,6 @@ #include "sharing/nearby_connection.h" #include "sharing/nearby_connections_manager.h" #include "sharing/nearby_connections_types.h" -#include "sharing/paired_key_verification_runner.h" #include "sharing/payload_tracker.h" #include "sharing/proto/wire_format.pb.h" #include "sharing/share_session.h" @@ -56,9 +57,12 @@ using ::location::nearby::proto::sharing::OSType; using ::location::nearby::proto::sharing::ResponseToIntroduction; using ::nearby::sharing::service::proto::AppMetadata; using ::nearby::sharing::service::proto::ConnectionResponseFrame; +using ::nearby::sharing::service::proto::Frame; using ::nearby::sharing::service::proto::IntroductionFrame; +using ::nearby::sharing::service::proto::SyncConfig; using ::nearby::sharing::service::proto::V1Frame; using ::nearby::sharing::service::proto::WifiCredentials; +using ::nearby::sharing::sync::SyncConfigPrefs; } // namespace @@ -85,6 +89,7 @@ void IncomingShareSession::InvokeTransferUpdateCallback( std::optional IncomingShareSession::ProcessIntroduction( const IntroductionFrame& introduction_frame) { + session_phase_ = SessionPhase::kTransfer; int64_t file_size_sum = 0; int app_file_count = 0; for (const AppMetadata& apk : introduction_frame.app_metadata()) { @@ -494,4 +499,43 @@ void IncomingShareSession::PushPayloadTransferUpdateForTest( payload_updates_queue()->Queue(std::move(update)); } +void IncomingShareSession::ProcessSyncFrame( + SyncManager& sync_manager, + const nearby::sharing::service::proto::SyncFrame& sync_frame) { + if (session_phase_ != SessionPhase::kUninitialized) { + LOG(WARNING) << "Ignore SyncFrame received in unexpected session phase: " + << static_cast(session_phase_); + return; + } + // TODO: b/485304482 - Check that the connected device is authenticated and is + // part of a sync pairing. + if (!certificate().has_value()) { + LOG(WARNING) << "Ignore SyncFrame received from unauthenticated device."; + return; + } + if (false && + !sync_manager.IsFileSyncBinding(certificate()->binding_id())) { + LOG(WARNING) << "Ignore SyncFrame received in unexpected binding id: " + << certificate()->binding_id(); + return; + } + session_phase_ = SessionPhase::kSync; + if (sync_frame.has_handshake()) { + VLOG(1) << __func__ << ": Received FileSync Handshake"; + WriteSyncConfigFrame( + sync_manager.GetSyncConfig(certificate()->binding_id()) + .value_or(SyncConfigPrefs()) + .sync_config()); + } +} + +void IncomingShareSession::WriteSyncConfigFrame(const SyncConfig& config) { + Frame frame; + frame.set_version(Frame::V1); + V1Frame* v1_frame = frame.mutable_v1(); + v1_frame->set_type(V1Frame::FILE_SYNC); + *v1_frame->mutable_file_sync()->mutable_config() = config; + WriteFrame(frame); +} + } // namespace nearby::sharing diff --git a/sharing/incoming_share_session.h b/sharing/incoming_share_session.h index 1ec0e519..5a9d91ee 100644 --- a/sharing/incoming_share_session.h +++ b/sharing/incoming_share_session.h @@ -21,6 +21,7 @@ #include #include +#include "location/nearby/sharing/lib/sync/sync_manager.h" #include "absl/functional/any_invocable.h" #include "internal/base/file_path.h" #include "internal/platform/clock.h" @@ -29,7 +30,6 @@ #include "sharing/nearby_connection.h" #include "sharing/nearby_connections_manager.h" #include "sharing/nearby_connections_types.h" -#include "sharing/paired_key_verification_runner.h" #include "sharing/proto/wire_format.pb.h" #include "sharing/share_session.h" #include "sharing/share_target.h" @@ -104,10 +104,19 @@ class IncomingShareSession : public ShareSession { // Called when an incoming connection is established. void OnConnected(NearbyConnection* connection); + void ProcessSyncFrame(nearby::sharing::SyncManager& sync_manager, + const nearby::sharing::service::proto::SyncFrame& sync_frame); + protected: void InvokeTransferUpdateCallback(const TransferMetadata& metadata) override; private: + enum class SessionPhase { + kUninitialized, + kTransfer, + kSync, + }; + // Update file attachment paths with payload paths. bool UpdateFilePayloadPaths(); @@ -119,6 +128,9 @@ class IncomingShareSession : public ShareSession { // Returns true if all payloads were successfully finalized. bool FinalizePayloads(); + void WriteSyncConfigFrame( + const nearby::sharing::service::proto::SyncConfig& config); + std::function transfer_update_callback_; @@ -127,6 +139,8 @@ class IncomingShareSession : public ShareSession { // This alarm is used to disconnect the sharing connection if both sides do // not press accept within the timeout. std::unique_ptr mutual_acceptance_timeout_; + + SessionPhase session_phase_ = SessionPhase::kUninitialized; }; } // namespace nearby::sharing diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index 7c08717f..7749dfda 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -273,7 +273,8 @@ NearbySharingServiceImpl::NearbySharingServiceImpl( absl::bind_front(&NearbySharingServiceImpl::NotifyShareTargetLost, this), absl::bind_front(&NearbySharingServiceImpl::OnOutgoingTransferUpdate, - this)) { + this)), + sync_manager_(&preference_manager_) { CHECK(nearby_connections_manager_); CHECK(analytics_recorder); @@ -2479,6 +2480,12 @@ void NearbySharingServiceImpl::OnIncomingSessionFrameRead( OnReceivedIntroduction(*session, frame->introduction()); // OnReceivedIntroduction will schedule the next ReadFrame. return; + case service::proto::V1Frame::FILE_SYNC: + if (NearbyFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_sharing_feature::kEnableFileSync)) { + session->ProcessSyncFrame(sync_manager_, frame->file_sync()); + } + break; default: LOG(ERROR) << __func__ << ": Discarding unknown frame of type: " << static_cast(frame->type()); diff --git a/sharing/nearby_sharing_service_impl.h b/sharing/nearby_sharing_service_impl.h index bdf0b34b..7b17c0e8 100644 --- a/sharing/nearby_sharing_service_impl.h +++ b/sharing/nearby_sharing_service_impl.h @@ -28,6 +28,7 @@ #include #include "location/nearby/sharing/lib/rpc/sharing_rpc_client.h" +#include "location/nearby/sharing/lib/sync/sync_manager.h" #include "absl/base/nullability.h" #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" @@ -529,6 +530,7 @@ class NearbySharingServiceImpl // If true, a new endpoint id will be generated at the next advertisement. bool force_new_endpoint_id_ = false; OutgoingTargetsManager outgoing_targets_manager_; + nearby::sharing::SyncManager sync_manager_; }; } // namespace nearby::sharing