Refactor RpcClient interfaces.

PiperOrigin-RevId: 600912194
This commit is contained in:
Francis Tsui
2024-01-23 14:44:37 -08:00
committed by Copybara-Service
parent 5fb7f69830
commit e2aa37ab04
12 changed files with 155 additions and 30 deletions
+1 -1
View File
@@ -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",
+4 -1
View File
@@ -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;
}
+2 -1
View File
@@ -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;
+3
View File
@@ -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",
+72
View File
@@ -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 <memory>
#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<proto::UpdateDeviceResponse>& response) &&>
callback) = 0;
// NearbyShareService v1: ListContactPeople
virtual void ListContactPeople(
const proto::ListContactPeopleRequest& request,
absl::AnyInvocable<void(const absl::StatusOr<
proto::ListContactPeopleResponse>& response) &&>
callback) = 0;
// NearbyShareService v1: ListPublicCertificates
virtual void ListPublicCertificates(
const proto::ListPublicCertificatesRequest& request,
absl::AnyInvocable<
void(const absl::StatusOr<proto::ListPublicCertificatesResponse>&
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<SharingRpcClient> CreateInstance() = 0;
virtual api::SharingRpcNotifier* GetRpcNotifier() const = 0;
};
} // namespace nearby::sharing::api
#endif // THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_SHARING_RPC_CLIENT_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_
+2 -1
View File
@@ -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;
+1 -5
View File
@@ -18,7 +18,6 @@
#include <utility>
#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<NearbySharingDecoderImpl>();
http_client_factory_ =
std::make_unique<nearby::network::HttpClientFactoryImpl>();
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<NearbySharingServiceImpl>(
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();
}
-2
View File
@@ -18,7 +18,6 @@
#include <memory>
#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> context_;
std::unique_ptr<::nearby::analytics::EventLogger> event_logger_;
std::unique_ptr<NearbySharingDecoder> decoder_;
std::unique_ptr<nearby::network::HttpClientFactory> http_client_factory_;
std::unique_ptr<NearbyConnectionsManager> nearby_connections_manager_;
std::unique_ptr<NearbySharingService> nearby_sharing_service_;
};
+5 -7
View File
@@ -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<NearbyConnectionsManager> 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<NearbyShareClientFactoryImpl>(
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<NearbyShareProfileInfoProviderImpl>(
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*
+5 -8
View File
@@ -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<NearbyConnectionsManager> 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<void()> 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<NearbyConnectionsManager> 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<NearbyShareClientFactory> nearby_share_client_factory_;
std::unique_ptr<nearby::sharing::api::SharingRpcClientFactory>
nearby_share_client_factory_;
std::unique_ptr<NearbyShareProfileInfoProvider> profile_info_provider_;
std::unique_ptr<NearbyShareLocalDeviceDataManager> local_device_data_manager_;
std::unique_ptr<NearbyShareContactManager> contact_manager_;
@@ -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<NearbySharingServiceImpl>(
&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<nearby::network::HttpClientFactory> fake_http_client_ =
std::make_unique<nearby::network::FakeHttpClientFactory>();
std::unique_ptr<NearbySharingServiceImpl> service_;
int expect_transfer_updates_count_ = 0;
std::function<void()> expect_transfer_updates_callback_;