diff --git a/sharing/BUILD b/sharing/BUILD index 1ee9b122..0f76cf00 100644 --- a/sharing/BUILD +++ b/sharing/BUILD @@ -136,7 +136,6 @@ cc_library( "//internal/base:bluetooth_address", "//internal/flags:flag_reader", "//internal/flags:nearby_flags", - "//internal/network:nearby_http_client", "//internal/network:types", "//internal/platform:base", "//internal/platform:types", @@ -196,6 +195,7 @@ cc_library( ":types", "//internal/base", "//sharing/common:enum", + "//sharing/internal/api:platform", "//sharing/internal/public:logging", "//sharing/local_device_data", "//sharing/proto:share_cc_proto", diff --git a/sharing/fake_nearby_sharing_service.cc b/sharing/fake_nearby_sharing_service.cc index 5f1070cf..7fa29042 100644 --- a/sharing/fake_nearby_sharing_service.cc +++ b/sharing/fake_nearby_sharing_service.cc @@ -22,6 +22,7 @@ #include "absl/strings/string_view.h" #include "internal/base/observer_list.h" #include "sharing/attachment.h" +#include "sharing/internal/api/sharing_rpc_notifier.h" #include "sharing/local_device_data/nearby_share_local_device_data_manager.h" #include "sharing/nearby_sharing_service.h" #include "sharing/nearby_sharing_settings.h" @@ -33,6 +34,8 @@ namespace nearby { namespace sharing { +using ::nearby::sharing::api::SharingRpcNotifier; + void FakeNearbySharingService::AddObserver(Observer* observer) { observers_.AddObserver(observer); } @@ -184,7 +187,7 @@ std::string FakeNearbySharingService::Dump() const { return ""; } NearbyShareSettings* FakeNearbySharingService::GetSettings() { return nullptr; } -NearbyShareHttpNotifier* FakeNearbySharingService::GetHttpNotifier() { +SharingRpcNotifier* FakeNearbySharingService::GetRpcNotifier() { return nullptr; } diff --git a/sharing/fake_nearby_sharing_service.h b/sharing/fake_nearby_sharing_service.h index 8c3affc8..55824984 100644 --- a/sharing/fake_nearby_sharing_service.h +++ b/sharing/fake_nearby_sharing_service.h @@ -23,6 +23,7 @@ #include "absl/strings/string_view.h" #include "internal/base/observer_list.h" #include "sharing/attachment.h" +#include "sharing/internal/api/sharing_rpc_notifier.h" #include "sharing/local_device_data/nearby_share_local_device_data_manager.h" #include "sharing/nearby_sharing_service.h" #include "sharing/nearby_sharing_settings.h" @@ -134,7 +135,7 @@ class FakeNearbySharingService : public NearbySharingService { std::string Dump() const override; NearbyShareSettings* GetSettings() override; - NearbyShareHttpNotifier* GetHttpNotifier() override; + nearby::sharing::api::SharingRpcNotifier* GetRpcNotifier() override; NearbyShareLocalDeviceDataManager* GetLocalDeviceDataManager() override; NearbyShareContactManager* GetContactManager() override; NearbyShareCertificateManager* GetCertificateManager() override; diff --git a/sharing/internal/api/BUILD b/sharing/internal/api/BUILD index 795111af..1894e88d 100644 --- a/sharing/internal/api/BUILD +++ b/sharing/internal/api/BUILD @@ -26,6 +26,8 @@ cc_library( "private_certificate_data.h", "public_certificate_database.h", "sharing_platform.h", + "sharing_rpc_client.h", + "sharing_rpc_notifier.h", "shell.h", "system_info.h", "wifi_adapter.h", @@ -42,6 +44,7 @@ cc_library( "//sharing/proto:share_cc_proto", "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings:string_view", "@com_google_absl//absl/time", "@com_google_absl//absl/types:span", diff --git a/sharing/internal/api/sharing_rpc_client.h b/sharing/internal/api/sharing_rpc_client.h new file mode 100644 index 00000000..09b27d98 --- /dev/null +++ b/sharing/internal/api/sharing_rpc_client.h @@ -0,0 +1,72 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_CLIENT_H_ +#define THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_CLIENT_H_ + +#include + +#include "absl/functional/any_invocable.h" +#include "absl/status/statusor.h" +#include "sharing/internal/api/sharing_rpc_notifier.h" +#include "sharing/proto/certificate_rpc.pb.h" +#include "sharing/proto/contact_rpc.pb.h" +#include "sharing/proto/device_rpc.pb.h" + +namespace nearby::sharing::api { + +// SharingRpcClient is used to access Nearby Share backend APIs. +class SharingRpcClient { + public: + SharingRpcClient() = default; + virtual ~SharingRpcClient() = default; + + // Updates device data. + virtual void UpdateDevice( + const proto::UpdateDeviceRequest& request, + absl::AnyInvocable< + void(const absl::StatusOr& response) &&> + callback) = 0; + + // NearbyShareService v1: ListContactPeople + virtual void ListContactPeople( + const proto::ListContactPeopleRequest& request, + absl::AnyInvocable& response) &&> + callback) = 0; + + // NearbyShareService v1: ListPublicCertificates + virtual void ListPublicCertificates( + const proto::ListPublicCertificatesRequest& request, + absl::AnyInvocable< + void(const absl::StatusOr& + response) &&> + callback) = 0; +}; + +// Interface for creating SharingRpcClient instances. Because each +// SharingRpcClient instance can only be used for one API call, a factory +// makes it easier to make multiple requests in sequence or in parallel. +class SharingRpcClientFactory { + public: + SharingRpcClientFactory() = default; + virtual ~SharingRpcClientFactory() = default; + + virtual std::unique_ptr CreateInstance() = 0; + virtual api::SharingRpcNotifier* GetRpcNotifier() const = 0; +}; + +} // namespace nearby::sharing::api + +#endif // THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_CLIENT_H_ diff --git a/sharing/internal/api/sharing_rpc_notifier.h b/sharing/internal/api/sharing_rpc_notifier.h new file mode 100644 index 00000000..0ca97f40 --- /dev/null +++ b/sharing/internal/api/sharing_rpc_notifier.h @@ -0,0 +1,60 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_NOTIFIER_H_ +#define THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_NOTIFIER_H_ + +#include "sharing/proto/certificate_rpc.pb.h" +#include "sharing/proto/contact_rpc.pb.h" +#include "sharing/proto/device_rpc.pb.h" + +namespace nearby::sharing::api { + +// Interface for passing RPC Responses/Requests to observers, by passing +// instances of this class to each RPC Client. +class SharingRpcNotifier { + public: + class Observer { + public: + virtual ~Observer() = default; + + // Called when HTTP RPC is made for request and responses. + virtual void OnUpdateDeviceRequest( + const nearby::sharing::proto::UpdateDeviceRequest& request) = 0; + virtual void OnUpdateDeviceResponse( + const nearby::sharing::proto::UpdateDeviceResponse& response) = 0; + virtual void OnListContactPeopleRequest( + const nearby::sharing::proto::ListContactPeopleRequest& request) = 0; + virtual void OnListContactPeopleResponse( + const nearby::sharing::proto::ListContactPeopleResponse& response) = 0; + virtual void OnListPublicCertificatesRequest( + const nearby::sharing::proto::ListPublicCertificatesRequest& + request) = 0; + virtual void OnListPublicCertificatesResponse( + const nearby::sharing::proto::ListPublicCertificatesResponse& + response) = 0; + }; + + virtual ~SharingRpcNotifier() = default; + + virtual void AddObserver(Observer* observer) = 0; + virtual void RemoveObserver(Observer* observer) = 0; + + protected: + SharingRpcNotifier() = default; +}; + +} // namespace nearby::sharing::api + +#endif // THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_NOTIFIER_H_ diff --git a/sharing/nearby_sharing_service.h b/sharing/nearby_sharing_service.h index 9be2f794..add4e08d 100644 --- a/sharing/nearby_sharing_service.h +++ b/sharing/nearby_sharing_service.h @@ -23,6 +23,7 @@ #include "absl/strings/string_view.h" #include "internal/network/url.h" #include "sharing/attachment.h" +#include "sharing/internal/api/sharing_rpc_notifier.h" #include "sharing/local_device_data/nearby_share_local_device_data_manager.h" #include "sharing/nearby_sharing_settings.h" #include "sharing/share_target.h" @@ -239,7 +240,7 @@ class NearbySharingService { virtual void UpdateFilePathsInProgress(bool update_file_paths) = 0; virtual NearbyShareSettings* GetSettings() = 0; - virtual NearbyShareHttpNotifier* GetHttpNotifier() = 0; + virtual nearby::sharing::api::SharingRpcNotifier* GetRpcNotifier() = 0; virtual NearbyShareLocalDeviceDataManager* GetLocalDeviceDataManager() = 0; virtual NearbyShareContactManager* GetContactManager() = 0; virtual NearbyShareCertificateManager* GetCertificateManager() = 0; diff --git a/sharing/nearby_sharing_service_factory.cc b/sharing/nearby_sharing_service_factory.cc index 3bf7ea69..13d27201 100644 --- a/sharing/nearby_sharing_service_factory.cc +++ b/sharing/nearby_sharing_service_factory.cc @@ -18,7 +18,6 @@ #include #include "internal/analytics/event_logger.h" -#include "internal/network/http_client_factory_impl.h" #include "sharing/internal/api/sharing_platform.h" #include "sharing/internal/public/context_impl.h" #include "sharing/nearby_connections_manager_factory.h" @@ -52,8 +51,6 @@ NearbySharingService* NearbySharingServiceFactory::CreateSharingService( sharing_platform.GetPreferenceManager(), std::move(event_logger)); decoder_ = std::make_unique(); - http_client_factory_ = - std::make_unique(); nearby_connections_manager_ = NearbyConnectionsManagerFactory::CreateConnectionsManager( link_type, context_.get(), sharing_platform.GetDeviceInfo(), @@ -61,8 +58,7 @@ NearbySharingService* NearbySharingServiceFactory::CreateSharingService( nearby_sharing_service_ = std::make_unique( context_.get(), sharing_platform, decoder_.get(), - http_client_factory_.get(), std::move(nearby_connections_manager_), - event_logger_.get()); + std::move(nearby_connections_manager_), event_logger_.get()); return nearby_sharing_service_.get(); } diff --git a/sharing/nearby_sharing_service_factory.h b/sharing/nearby_sharing_service_factory.h index 1dc72961..85b4ef4c 100644 --- a/sharing/nearby_sharing_service_factory.h +++ b/sharing/nearby_sharing_service_factory.h @@ -18,7 +18,6 @@ #include #include "internal/analytics/event_logger.h" -#include "internal/network/http_client_factory.h" #include "sharing/internal/api/sharing_platform.h" #include "sharing/internal/public/context.h" #include "sharing/nearby_connections_manager.h" @@ -46,7 +45,6 @@ class NearbySharingServiceFactory { std::unique_ptr context_; std::unique_ptr<::nearby::analytics::EventLogger> event_logger_; std::unique_ptr decoder_; - std::unique_ptr http_client_factory_; std::unique_ptr nearby_connections_manager_; std::unique_ptr nearby_sharing_service_; }; diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index 24d01c87..39da5e6d 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -41,7 +41,6 @@ #include "internal/base/bluetooth_address.h" #include "internal/base/observer_list.h" #include "internal/flags/nearby_flags.h" -#include "internal/network/http_client_factory.h" #include "internal/network/url.h" #include "internal/platform/device_info.h" #include "internal/platform/implementation/account_manager.h" @@ -58,7 +57,6 @@ #include "sharing/certificates/nearby_share_decrypted_public_certificate.h" #include "sharing/certificates/nearby_share_encrypted_metadata_key.h" #include "sharing/client/nearby_share_client_impl.h" -#include "sharing/client/nearby_share_http_notifier.h" #include "sharing/common/compatible_u8_string.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/common/nearby_share_prefs.h" @@ -73,6 +71,7 @@ #include "sharing/incoming_share_target_info.h" #include "sharing/internal/api/bluetooth_adapter.h" #include "sharing/internal/api/sharing_platform.h" +#include "sharing/internal/api/sharing_rpc_notifier.h" #include "sharing/internal/api/wifi_adapter.h" #include "sharing/internal/base/encode.h" #include "sharing/internal/public/connectivity_manager.h" @@ -188,7 +187,6 @@ class TransferUpdateDecorator : public TransferUpdateCallback { NearbySharingServiceImpl::NearbySharingServiceImpl( Context* context, SharingPlatform& sharing_platform, NearbySharingDecoder* decoder, - nearby::network::HttpClientFactory* http_client_factory, std::unique_ptr nearby_connections_manager, nearby::analytics::EventLogger* event_logger) : context_(context), @@ -199,8 +197,7 @@ NearbySharingServiceImpl::NearbySharingServiceImpl( nearby_connections_manager_(std::move(nearby_connections_manager)), nearby_share_client_factory_( std::make_unique( - device_info_.GetOsType(), account_manager_, http_client_factory, - &nearby_share_http_notifier_, event_logger)), + device_info_.GetOsType(), account_manager_, event_logger)), profile_info_provider_( std::make_unique( device_info_, account_manager_)), @@ -1031,8 +1028,9 @@ NearbyShareSettings* NearbySharingServiceImpl::GetSettings() { return settings_.get(); } -NearbyShareHttpNotifier* NearbySharingServiceImpl::GetHttpNotifier() { - return &nearby_share_http_notifier_; +nearby::sharing::api::SharingRpcNotifier* +NearbySharingServiceImpl::GetRpcNotifier() { + return nearby_share_client_factory_->GetRpcNotifier(); } NearbyShareLocalDeviceDataManager* diff --git a/sharing/nearby_sharing_service_impl.h b/sharing/nearby_sharing_service_impl.h index 18abba9a..3d6e3d00 100644 --- a/sharing/nearby_sharing_service_impl.h +++ b/sharing/nearby_sharing_service_impl.h @@ -34,7 +34,6 @@ #include "absl/types/span.h" #include "internal/analytics/event_logger.h" #include "internal/base/observer_list.h" -#include "internal/network/http_client_factory.h" #include "internal/network/url.h" #include "internal/platform/device_info.h" #include "internal/platform/implementation/account_manager.h" @@ -48,9 +47,6 @@ #include "sharing/certificates/nearby_share_certificate_manager.h" #include "sharing/certificates/nearby_share_decrypted_public_certificate.h" #include "sharing/certificates/nearby_share_private_certificate.h" -#include "sharing/client/nearby_share_client.h" -#include "sharing/client/nearby_share_client_impl.h" -#include "sharing/client/nearby_share_http_notifier.h" #include "sharing/common/nearby_share_enums.h" #include "sharing/common/nearby_share_profile_info_provider.h" #include "sharing/fast_initiation/nearby_fast_initiation.h" @@ -58,6 +54,8 @@ #include "sharing/internal/api/bluetooth_adapter.h" #include "sharing/internal/api/preference_manager.h" #include "sharing/internal/api/sharing_platform.h" +#include "sharing/internal/api/sharing_rpc_client.h" +#include "sharing/internal/api/sharing_rpc_notifier.h" #include "sharing/internal/api/wifi_adapter.h" #include "sharing/internal/public/connectivity_manager.h" #include "sharing/internal/public/context.h" @@ -109,7 +107,6 @@ class NearbySharingServiceImpl NearbySharingServiceImpl( Context* context, nearby::sharing::api::SharingPlatform& sharing_platform, NearbySharingDecoder* decoder, - nearby::network::HttpClientFactory* http_client_factory, std::unique_ptr nearby_connections_manager, nearby::analytics::EventLogger* event_logger = nullptr); ~NearbySharingServiceImpl() override; @@ -172,7 +169,7 @@ class NearbySharingServiceImpl absl::string_view password) override; void SetArcTransferCleanupCallback(std::function callback) override; NearbyShareSettings* GetSettings() override; - NearbyShareHttpNotifier* GetHttpNotifier() override; + nearby::sharing::api::SharingRpcNotifier* GetRpcNotifier() override; NearbyShareLocalDeviceDataManager* GetLocalDeviceDataManager() override; NearbyShareContactManager* GetContactManager() override; NearbyShareCertificateManager* GetCertificateManager() override; @@ -502,8 +499,8 @@ class NearbySharingServiceImpl std::unique_ptr nearby_connections_manager_; // Scanner which is non-null when we are performing a background scan for // remote devices that are attempting to share. - NearbyShareHttpNotifier nearby_share_http_notifier_; - std::unique_ptr nearby_share_client_factory_; + std::unique_ptr + nearby_share_client_factory_; std::unique_ptr profile_info_provider_; std::unique_ptr local_device_data_manager_; std::unique_ptr contact_manager_; diff --git a/sharing/nearby_sharing_service_impl_test.cc b/sharing/nearby_sharing_service_impl_test.cc index 0fe95d61..41081c10 100644 --- a/sharing/nearby_sharing_service_impl_test.cc +++ b/sharing/nearby_sharing_service_impl_test.cc @@ -45,7 +45,6 @@ #include "internal/network/http_client_factory.h" #include "internal/test/fake_account_manager.h" #include "internal/test/fake_device_info.h" -#include "internal/test/fake_http_client_factory.h" #include "internal/test/fake_task_runner.h" #include "sharing/advertisement.h" #include "sharing/attachment.h" @@ -423,7 +422,6 @@ class NearbySharingServiceImplTest : public testing::Test { fake_nearby_connections_manager_ = new FakeNearbyConnectionsManager(); auto service = std::make_unique( &fake_context_, mock_sharing_platform_, &fake_decoder_, - fake_http_client_.get(), absl::WrapUnique(fake_nearby_connections_manager_)); return service; @@ -1253,8 +1251,6 @@ class NearbySharingServiceImplTest : public testing::Test { nearby_fast_initiation_factory_; FakeNearbyConnection connection_; MockNearbySharingDecoder fake_decoder_; - std::unique_ptr fake_http_client_ = - std::make_unique(); std::unique_ptr service_; int expect_transfer_updates_count_ = 0; std::function expect_transfer_updates_callback_;