[Nearby Presence] Refactor PresenceService to use Impl model

Refactors PresenceService to have a pure virtual base class and an
implemnetation.

PiperOrigin-RevId: 539125804
This commit is contained in:
Juliet Levesque
2023-06-09 10:59:40 -07:00
committed by Copybara-Service
parent 589c126725
commit 3a49499de4
8 changed files with 418 additions and 53 deletions
+6 -2
View File
@@ -19,13 +19,14 @@ cc_library(
name = "presence",
srcs = [
"presence_client_impl.cc",
"presence_service.cc",
"presence_service_impl.cc",
],
hdrs = [
"presence_client.h",
"presence_client_impl.h",
"presence_device_provider.h",
"presence_service.h",
"presence_service_impl.h",
],
deps = [
":types",
@@ -43,15 +44,18 @@ cc_library(
testonly = 1,
srcs = [
"fake_presence_client.cc",
"fake_presence_service.cc",
],
hdrs = [
"fake_presence_client.h",
"fake_presence_service.h",
],
compatible_with = ["//buildenv/target:non_prod"],
deps = [
":presence",
":types",
"@com_google_absl//absl/status",
"//internal/platform:types",
"//internal/proto:metadata_cc_proto",
"@com_google_absl//absl/status:statusor",
],
)
+100
View File
@@ -0,0 +1,100 @@
// Copyright 2020 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.
#include "presence/fake_presence_service.h"
#include <memory>
#include <utility>
#include <vector>
#include "internal/platform/borrowable.h"
#include "presence/fake_presence_client.h"
namespace nearby {
namespace presence {
FakePresenceService::FakePresenceService() = default;
std::unique_ptr<PresenceClient> FakePresenceService::CreatePresenceClient() {
auto fake = std::make_unique<FakePresenceClient>();
most_recent_fake_presence_client_ = fake.get();
return std::move(fake);
}
// Not implemented.
absl::StatusOr<ScanSessionId> FakePresenceService::StartScan(
ScanRequest scan_request, ScanCallback callback) {
return absl::Status(absl::StatusCode::kCancelled,
"StartScan not implemented yet");
}
// Not implemented.
void FakePresenceService::StopScan(ScanSessionId session_id) {}
// Not implemented.
absl::StatusOr<BroadcastSessionId> FakePresenceService::StartBroadcast(
BroadcastRequest broadcast_request, BroadcastCallback callback) {
return absl::Status(absl::StatusCode::kCancelled,
"StartBroadcast not implemented yet");
}
// Not implemented.
void FakePresenceService::StopBroadcast(BroadcastSessionId session_id) {}
void FakePresenceService::UpdateLocalDeviceMetadata(
const ::nearby::internal::Metadata& metadata, bool regen_credentials,
absl::string_view manager_app_id,
const std::vector<nearby::internal::IdentityType>& identity_types,
int credential_life_cycle_days, int contiguous_copy_of_credentials,
GenerateCredentialsResultCallback credentials_generated_cb) {
metadata_ = metadata;
if (!regen_credentials) {
// No need to call back on credentials_generated_cb.
return;
}
if (gen_credentials_status_.ok()) {
std::move(credentials_generated_cb.credentials_generated_cb)(
shared_credentials_);
} else {
std::move(credentials_generated_cb.credentials_generated_cb)(
gen_credentials_status_);
}
}
// Not implemented.
PresenceDeviceProvider* FakePresenceService::GetLocalDeviceProvider() {
return nullptr;
}
// Not implemented.
void FakePresenceService::GetLocalPublicCredentials(
const CredentialSelector& credential_selector,
GetPublicCredentialsResultCallback callback) {}
void FakePresenceService::UpdateRemotePublicCredentials(
absl::string_view manager_app_id, absl::string_view account_name,
const std::vector<nearby::internal::SharedCredential>& remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb) {
if (update_remote_public_credentials_status_.ok()) {
remote_shared_credentials_ = remote_public_creds;
}
std::move(credentials_updated_cb.credentials_updated_cb)(
update_remote_public_credentials_status_);
}
} // namespace presence
} // namespace nearby
+105
View File
@@ -0,0 +1,105 @@
// 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_PRESENCE_FAKE_PRESENCE_SERVICE_H_
#define THIRD_PARTY_NEARBY_PRESENCE_FAKE_PRESENCE_SERVICE_H_
#include "internal/platform/borrowable.h"
#include "internal/proto/metadata.pb.h"
#include "presence/data_types.h"
#include "presence/presence_client.h"
#include "presence/presence_device_provider.h"
#include "presence/presence_service.h"
namespace nearby {
namespace presence {
class FakePresenceClient;
class FakePresenceService : public PresenceService {
public:
FakePresenceService();
~FakePresenceService() override { lender_.Release(); }
// PresenceService:
std::unique_ptr<PresenceClient> CreatePresenceClient() override;
absl::StatusOr<ScanSessionId> StartScan(ScanRequest scan_request,
ScanCallback callback) override;
void StopScan(ScanSessionId session_id) override;
absl::StatusOr<BroadcastSessionId> StartBroadcast(
BroadcastRequest broadcast_request, BroadcastCallback callback) override;
void StopBroadcast(BroadcastSessionId session_id) override;
void UpdateLocalDeviceMetadata(
const ::nearby::internal::Metadata& metadata, bool regen_credentials,
absl::string_view manager_app_id,
const std::vector<nearby::internal::IdentityType>& identity_types,
int credential_life_cycle_days, int contiguous_copy_of_credentials,
GenerateCredentialsResultCallback credentials_generated_cb) override;
PresenceDeviceProvider* GetLocalDeviceProvider() override;
::nearby::internal::Metadata GetLocalDeviceMetadata() override {
return metadata_;
}
void GetLocalPublicCredentials(
const CredentialSelector& credential_selector,
GetPublicCredentialsResultCallback callback) override;
void UpdateRemotePublicCredentials(
absl::string_view manager_app_id, absl::string_view account_name,
const std::vector<nearby::internal::SharedCredential>&
remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb) override;
// Use for testing. Call this to set the response to
// `UpdateLocalDeviceMetadata`.
void SetUpdateLocalDeviceMetadataResponse(
absl::Status status,
std::vector<nearby::internal::SharedCredential> shared_credentials) {
shared_credentials_ = shared_credentials;
gen_credentials_status_ = status;
}
FakePresenceClient* GetMostRecentFakePresenceClient() {
return most_recent_fake_presence_client_;
}
// Used for testing to verify the remote credentials set.
std::vector<nearby::internal::SharedCredential> GetRemoteSharedCredentials() {
return remote_shared_credentials_;
}
void SetUpdateRemoteSharedCredentialsResult(absl::Status status) {
update_remote_public_credentials_status_ = status;
}
private:
FakePresenceClient* most_recent_fake_presence_client_ = nullptr;
std::vector<nearby::internal::SharedCredential> shared_credentials_;
std::vector<nearby::internal::SharedCredential> remote_shared_credentials_;
absl::Status gen_credentials_status_;
absl::Status update_remote_public_credentials_status_;
::nearby::internal::Metadata metadata_;
::nearby::Lender<PresenceService*> lender_{this};
};
} // namespace presence
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_PRESENCE_FAKE_PRESENCE_SERVICE_H_
+7 -7
View File
@@ -21,7 +21,7 @@
#include "internal/platform/medium_environment.h"
#include "presence/data_types.h"
#include "presence/presence_device.h"
#include "presence/presence_service.h"
#include "presence/presence_service_impl.h"
namespace nearby {
namespace presence {
@@ -32,10 +32,10 @@ using ::testing::status::StatusIs;
constexpr absl::string_view kMacAddr = "\x4C\x8B\x1D\xCE\xBA\xD1";
// Creates a PresenceClient and destroys PresenceService that was used to create
// it.
// Creates a PresenceClient and destroys PresenceServiceImpl that was used to
// create it.
std::unique_ptr<PresenceClient> CreateDefunctPresenceClient() {
PresenceService presence_service;
PresenceServiceImpl presence_service;
return presence_service.CreatePresenceClient();
}
@@ -59,7 +59,7 @@ TEST_F(PresenceClientTest, StartBroadcastWithDefaultConstructor) {
env_.Start();
absl::Status broadcast_result;
PresenceService presence_service;
PresenceServiceImpl presence_service;
std::unique_ptr<PresenceClient> presence_client =
presence_service.CreatePresenceClient();
auto unused = presence_client->StartBroadcast(
@@ -95,7 +95,7 @@ TEST_F(PresenceClientTest, StartScanWithDefaultConstructor) {
.start_scan_cb = [&](absl::Status status) { scan_result.Set(status); },
};
PresenceService presence_service;
PresenceServiceImpl presence_service;
std::unique_ptr<PresenceClient> presence_client =
presence_service.CreatePresenceClient();
EXPECT_OK(presence_client->StartScan({}, std::move(scan_callback)));
@@ -122,7 +122,7 @@ TEST_F(PresenceClientTest, StartScanFailsWhenPresenceServiceIsGone) {
}
TEST_F(PresenceClientTest, GettingDeviceWorks) {
PresenceService presence_service;
PresenceServiceImpl presence_service;
std::unique_ptr<PresenceClient> presence_client =
presence_service.CreatePresenceClient();
presence_service.UpdateLocalDeviceMetadata(CreateTestMetadata(), false, "",
+17 -38
View File
@@ -16,73 +16,52 @@
#define THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_SERVICE_H_
#include <memory>
#include <utility>
#include <vector>
#include "internal/platform/borrowable.h"
#include "internal/proto/metadata.pb.h"
#include "presence/data_types.h"
#include "presence/implementation/service_controller.h"
#include "presence/presence_client.h"
#include "presence/presence_device_provider.h"
namespace nearby {
namespace presence {
/*
* PresenceService hosts presence functions by routing invokes to the unique
* {@code ServiceController}. PresenceService should be initialized once and
* only once in the process that hosting presence functions.
*/
class PresenceService {
public:
PresenceService();
~PresenceService() { lender_.Release(); }
virtual ~PresenceService() = default;
std::unique_ptr<PresenceClient> CreatePresenceClient();
virtual std::unique_ptr<PresenceClient> CreatePresenceClient() = 0;
absl::StatusOr<ScanSessionId> StartScan(ScanRequest scan_request,
ScanCallback callback);
void StopScan(ScanSessionId session_id);
virtual absl::StatusOr<ScanSessionId> StartScan(ScanRequest scan_request,
ScanCallback callback) = 0;
virtual void StopScan(ScanSessionId session_id) = 0;
absl::StatusOr<BroadcastSessionId> StartBroadcast(
BroadcastRequest broadcast_request, BroadcastCallback callback);
virtual absl::StatusOr<BroadcastSessionId> StartBroadcast(
BroadcastRequest broadcast_request, BroadcastCallback callback) = 0;
void StopBroadcast(BroadcastSessionId session_id);
virtual void StopBroadcast(BroadcastSessionId session_id) = 0;
void UpdateLocalDeviceMetadata(
virtual void UpdateLocalDeviceMetadata(
const ::nearby::internal::Metadata& metadata, bool regen_credentials,
absl::string_view manager_app_id,
const std::vector<nearby::internal::IdentityType>& identity_types,
int credential_life_cycle_days, int contiguous_copy_of_credentials,
GenerateCredentialsResultCallback credentials_generated_cb) {
provider_->UpdateMetadata(metadata);
service_controller_->UpdateLocalDeviceMetadata(
metadata, regen_credentials, manager_app_id, identity_types,
credential_life_cycle_days, contiguous_copy_of_credentials,
std::move(credentials_generated_cb));
}
GenerateCredentialsResultCallback credentials_generated_cb) = 0;
PresenceDeviceProvider* GetLocalDeviceProvider() { return provider_.get(); }
virtual PresenceDeviceProvider* GetLocalDeviceProvider() = 0;
void GetLocalPublicCredentials(const CredentialSelector& credential_selector,
GetPublicCredentialsResultCallback callback);
virtual void GetLocalPublicCredentials(
const CredentialSelector& credential_selector,
GetPublicCredentialsResultCallback callback) = 0;
void UpdateRemotePublicCredentials(
virtual void UpdateRemotePublicCredentials(
absl::string_view manager_app_id, absl::string_view account_name,
const std::vector<nearby::internal::SharedCredential>&
remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb);
UpdateRemotePublicCredentialsCallback credentials_updated_cb) = 0;
// Testing only.
::nearby::internal::Metadata GetLocalDeviceMetadata() {
return service_controller_->GetLocalDeviceMetadata();
}
private:
std::unique_ptr<ServiceController> service_controller_;
::nearby::Lender<PresenceService *> lender_{this};
std::unique_ptr<PresenceDeviceProvider> provider_;
virtual ::nearby::internal::Metadata GetLocalDeviceMetadata() = 0;
};
} // namespace presence
+88
View File
@@ -0,0 +1,88 @@
// Copyright 2020 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.
#include "presence/presence_service_impl.h"
#include <memory>
#include <utility>
#include <vector>
#include "internal/platform/borrowable.h"
#include "presence/data_types.h"
#include "presence/implementation/service_controller_impl.h"
#include "presence/presence_client_impl.h"
namespace nearby {
namespace presence {
PresenceServiceImpl::PresenceServiceImpl() {
service_controller_ = std::make_unique<ServiceControllerImpl>();
provider_ = std::make_unique<PresenceDeviceProvider>(
service_controller_->GetLocalDeviceMetadata());
}
std::unique_ptr<PresenceClient> PresenceServiceImpl::CreatePresenceClient() {
return PresenceClientImpl::Factory::Create(lender_.GetBorrowable());
}
absl::StatusOr<ScanSessionId> PresenceServiceImpl::StartScan(
ScanRequest scan_request, ScanCallback callback) {
return service_controller_->StartScan(scan_request, std::move(callback));
}
void PresenceServiceImpl::StopScan(ScanSessionId id) {
service_controller_->StopScan(id);
}
absl::StatusOr<BroadcastSessionId> PresenceServiceImpl::StartBroadcast(
BroadcastRequest broadcast_request, BroadcastCallback callback) {
return service_controller_->StartBroadcast(broadcast_request,
std::move(callback));
}
void PresenceServiceImpl::StopBroadcast(BroadcastSessionId session) {
service_controller_->StopBroadcast(session);
}
void PresenceServiceImpl::UpdateLocalDeviceMetadata(
const ::nearby::internal::Metadata& metadata, bool regen_credentials,
absl::string_view manager_app_id,
const std::vector<nearby::internal::IdentityType>& identity_types,
int credential_life_cycle_days, int contiguous_copy_of_credentials,
GenerateCredentialsResultCallback credentials_generated_cb) {
provider_->UpdateMetadata(metadata);
service_controller_->UpdateLocalDeviceMetadata(
metadata, regen_credentials, manager_app_id, identity_types,
credential_life_cycle_days, contiguous_copy_of_credentials,
std::move(credentials_generated_cb));
}
void PresenceServiceImpl::GetLocalPublicCredentials(
const CredentialSelector& credential_selector,
GetPublicCredentialsResultCallback callback) {
service_controller_->GetLocalPublicCredentials(credential_selector,
std::move(callback));
}
void PresenceServiceImpl::UpdateRemotePublicCredentials(
absl::string_view manager_app_id, absl::string_view account_name,
const std::vector<nearby::internal::SharedCredential>& remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb) {
service_controller_->UpdateRemotePublicCredentials(
manager_app_id, account_name, remote_public_creds,
std::move(credentials_updated_cb));
}
} // namespace presence
} // namespace nearby
+88
View File
@@ -0,0 +1,88 @@
// 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_PRESENCE_PRESENCE_SERVICE_IMPL_H_
#define THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_SERVICE_IMPL_H_
#include <memory>
#include <utility>
#include <vector>
#include "internal/platform/borrowable.h"
#include "internal/proto/metadata.pb.h"
#include "presence/data_types.h"
#include "presence/implementation/service_controller.h"
#include "presence/presence_client.h"
#include "presence/presence_device_provider.h"
#include "presence/presence_service.h"
namespace nearby {
namespace presence {
/*
* PresenceService hosts presence functions by routing invokes to the unique
* {@code ServiceController}. PresenceService should be initialized once and
* only once in the process that hosting presence functions.
*/
class PresenceServiceImpl : public PresenceService {
public:
PresenceServiceImpl();
~PresenceServiceImpl() override { lender_.Release(); }
std::unique_ptr<PresenceClient> CreatePresenceClient() override;
absl::StatusOr<ScanSessionId> StartScan(ScanRequest scan_request,
ScanCallback callback) override;
void StopScan(ScanSessionId session_id) override;
absl::StatusOr<BroadcastSessionId> StartBroadcast(
BroadcastRequest broadcast_request, BroadcastCallback callback) override;
void StopBroadcast(BroadcastSessionId session_id) override;
void UpdateLocalDeviceMetadata(
const ::nearby::internal::Metadata& metadata, bool regen_credentials,
absl::string_view manager_app_id,
const std::vector<nearby::internal::IdentityType>& identity_types,
int credential_life_cycle_days, int contiguous_copy_of_credentials,
GenerateCredentialsResultCallback credentials_generated_cb) override;
PresenceDeviceProvider* GetLocalDeviceProvider() override {
return provider_.get();
}
::nearby::internal::Metadata GetLocalDeviceMetadata() override {
return service_controller_->GetLocalDeviceMetadata();
}
void GetLocalPublicCredentials(
const CredentialSelector& credential_selector,
GetPublicCredentialsResultCallback callback) override;
void UpdateRemotePublicCredentials(
absl::string_view manager_app_id, absl::string_view account_name,
const std::vector<nearby::internal::SharedCredential>&
remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb) override;
private:
std::unique_ptr<ServiceController> service_controller_;
::nearby::Lender<PresenceService*> lender_{this};
std::unique_ptr<PresenceDeviceProvider> provider_;
};
} // namespace presence
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_SERVICE_IMPL_H_
+7 -6
View File
@@ -25,6 +25,7 @@
#include "internal/platform/count_down_latch.h"
#include "internal/platform/medium_environment.h"
#include "presence/presence_client.h"
#include "presence/presence_service_impl.h"
namespace nearby {
namespace presence {
@@ -58,7 +59,7 @@ CredentialSelector BuildDefaultCredentialSelector() {
}
TEST_F(PresenceServiceTest, DefaultConstructorWorks) {
PresenceService presence_service;
PresenceServiceImpl presence_service;
}
TEST_F(PresenceServiceTest, StartThenStopScan) {
@@ -67,7 +68,7 @@ TEST_F(PresenceServiceTest, StartThenStopScan) {
ScanCallback scan_callback = {
.start_scan_cb = [&](absl::Status status) { scan_result = status; },
};
PresenceService presence_service;
PresenceServiceImpl presence_service;
std::unique_ptr<PresenceClient> client =
presence_service.CreatePresenceClient();
@@ -89,7 +90,7 @@ TEST_F(PresenceServiceTest, StartThenStopScan) {
}
TEST_F(PresenceServiceTest, UpdatingLocalMetadataWorks) {
PresenceService presence_service;
PresenceServiceImpl presence_service;
presence_service.UpdateLocalDeviceMetadata(CreateTestMetadata("Test account"),
false, "Test app", {}, 3, 1, {});
EXPECT_EQ(presence_service.GetLocalDeviceMetadata().SerializeAsString(),
@@ -97,12 +98,12 @@ TEST_F(PresenceServiceTest, UpdatingLocalMetadataWorks) {
}
TEST_F(PresenceServiceTest, TestGetDeviceProvider) {
PresenceService presence_service;
PresenceServiceImpl presence_service;
EXPECT_NE(presence_service.GetLocalDeviceProvider(), nullptr);
}
TEST_F(PresenceServiceTest, TestGetPublicCredentials) {
PresenceService presence_service;
PresenceServiceImpl presence_service;
CredentialSelector selector = BuildDefaultCredentialSelector();
absl::Status status;
nearby::CountDownLatch fetched_latch(1);
@@ -120,7 +121,7 @@ TEST_F(PresenceServiceTest, TestGetPublicCredentials) {
}
TEST_F(PresenceServiceTest, TestUpdateRemotePublicCredentials) {
PresenceService presence_service;
PresenceServiceImpl presence_service;
internal::SharedCredential public_credential_for_test;
public_credential_for_test.set_identity_type(
internal::IdentityType::IDENTITY_TYPE_TRUSTED);