From b6f817274e1581e332809bfc1a0fcd4e95658032 Mon Sep 17 00:00:00 2001 From: hais Date: Mon, 11 Jul 2022 14:24:06 -0700 Subject: [PATCH] Initial header files showing class dependency and ownership. No detailed function signatures nor impl included yet. PiperOrigin-RevId: 460300058 --- internal/platform/BUILD | 3 + internal/platform/credential.h | 131 ++++++++++++++++++ internal/platform/credential_storage.h | 39 ++++++ internal/platform/implementation/BUILD | 1 + .../implementation/credential_storage.h | 89 ++++++++++++ presence/BUILD | 4 + presence/implementation/BUILD | 35 +++++ presence/implementation/broadcast_manager.h | 43 ++++++ presence/implementation/credential_manager.h | 82 +++++++++++ presence/implementation/mediums/BUILD | 30 ++++ presence/implementation/mediums/ble.h | 41 ++++++ presence/implementation/mediums/mediums.h | 42 ++++++ .../implementation/mock_service_controller.h | 37 +++++ presence/implementation/scan_manager.h | 43 ++++++ presence/implementation/service_controller.h | 34 +++++ .../implementation/service_controller_impl.h | 42 ++++++ presence/presence_service.h | 42 ++++++ presence/presence_service_test.cc | 29 ++++ 18 files changed, 767 insertions(+) create mode 100644 internal/platform/credential.h create mode 100644 internal/platform/credential_storage.h create mode 100644 internal/platform/implementation/credential_storage.h create mode 100644 presence/implementation/BUILD create mode 100644 presence/implementation/broadcast_manager.h create mode 100644 presence/implementation/credential_manager.h create mode 100644 presence/implementation/mediums/BUILD create mode 100644 presence/implementation/mediums/ble.h create mode 100644 presence/implementation/mediums/mediums.h create mode 100644 presence/implementation/mock_service_controller.h create mode 100644 presence/implementation/scan_manager.h create mode 100644 presence/implementation/service_controller.h create mode 100644 presence/implementation/service_controller_impl.h create mode 100644 presence/presence_service.h create mode 100644 presence/presence_service_test.cc diff --git a/internal/platform/BUILD b/internal/platform/BUILD index 154b09d7..2bce56ea 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -30,6 +30,7 @@ cc_library( "bluetooth_utils.h", "byte_array.h", "callable.h", + "credential.h", "exception.h", "feature_flags.h", "input_stream.h", @@ -343,6 +344,7 @@ cc_library( "ble_v2.h", "bluetooth_adapter.h", "bluetooth_classic.h", + "credential_storage.h", "wifi.h", "wifi_hotspot.h", "wifi_lan.h", @@ -356,6 +358,7 @@ cc_library( "//internal/analytics:__subpackages__", "//internal/platform:__pkg__", "//internal/platform/implementation:__subpackages__", + "//third_party/nearby/presence:__subpackages__", ], deps = [ ":logging", diff --git a/internal/platform/credential.h b/internal/platform/credential.h new file mode 100644 index 00000000..a4d43d30 --- /dev/null +++ b/internal/platform/credential.h @@ -0,0 +1,131 @@ +// 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. + +#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_CREDENTIAL_H_ +#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_CREDENTIAL_H_ + +#include +#include +#include + +namespace location { +namespace nearby { + +enum class TrustType { + kUnspecified = 0, + + // The same user itself. + kPrivate = 1, + + // The user selects the contact from its contact list. + kTrusted = 2, + + // For offline credentials and without dependence on Gaia account + kProvisioned = 3, + + // For offline public identities and without dependence on Gaia account + kPublic = 4, +}; + +enum class DeviceType { + kUnspecified = 0, + kPhone = 1, + kTablet = 2, + kDisplay = 3, + kLaptop = 4, + kTV = 5, + kWatch = 6, +}; + +struct DeviceMetadata { + // Stable device identifier that does not rotate for a few months. + std::string stable_device_id; + // The account name which created the certificate. + std::string account_name; + // The name of the local device when the certificate is created. + std::string device_name; + // The icon url of the user whose device created the certificate. + std::string icon_url; + // The Bluetooth MAC address of the device which created the certificate. + std::string bluetooth_mac_address; + DeviceType device_type; +}; + +struct PrivateCredential { + TrustType trust_type; + + // The unique id of (and hashed based on) a pair of secret + // key (PrivateCredential.verification_key) and X509Certificate's public + // key (PublicCredential.verification_key). + std::vector secret_id; + + // The aes key to encrypt personal fields in public certificates. + // A bytes representation of a Secret Key owned by contact, to decrypt the + // encrypted DeviceMetadata bytes stored within the advertisement. + std::vector authenticity_key; + + // Bytes representation of a public key of X509Certificate, used in + // handshake during contact verification phase. + std::vector verification_key; + + // The time in millis from epoch when this credential becomes effective. + int start_time; + + // The time in millis from epoch when this credential expires. + int end_time; + + // The set of 2-byte salts already used to encrypt the metadata key. + std::set> consumed_salts; + + // The aes key to encrypt DeviceMetadata in public credential. + std::vector metadata_encryption_key; + + DeviceMetadata device_metadata; +}; + +struct PublicCredential { + TrustType trust_type; + + // The unique id of (and hashed based on) a pair of secret + // key (PrivateCredential.verification_key) and X509Certificate's public + // key (PublicCredential.verification_key). + std::vector secret_id; + + // Bytes representation of a Secret Key owned by contact, to decrypt the + // metadata_key stored within the advertisement. + std::vector authenticity_key; + + // Bytes representation of a public key of X509Certificate, used in + // handshake during contact verification phase. + std::vector verification_key; + + // The time in millis from epoch when this credential becomes effective. + int start_time; + + // The time in millis from epoch when this credential expires. + int end_time; + + // The encrypted DeviceMetadata in bytes, contains personal information of the + // device/user who created this certificate. Needs to be decrypted into bytes, + // and converted back to DeviceMetadata instance to access fields. + std::vector encrypted_metadata_bytes; + + // The tag for verifying metadata_encryption_key. + std::vector metadata_encryption_key_tag; +}; + +} // namespace nearby +} // namespace location + +#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_CREDENTIAL_H_ diff --git a/internal/platform/credential_storage.h b/internal/platform/credential_storage.h new file mode 100644 index 00000000..4142fb51 --- /dev/null +++ b/internal/platform/credential_storage.h @@ -0,0 +1,39 @@ +// 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. + +#ifndef THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_CREDENTIAL_STORAGE_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_CREDENTIAL_STORAGE_H_ + +#include "internal/platform/implementation/credential_storage.h" + +namespace location { +namespace nearby { +/* + * The instance of CredentialStorage is owned by {@code CredentialManager}. + * It's a wrapper on top of implementation/credential_storage.h to providing + * credential storage operations for Nearby logic layer to invoke. + */ +class CredentialStorage { + public: + CredentialStorage() = default; + ~CredentialStorage() = default; + + private: + api::CredentialStorage credential_storage_; +}; + +} // namespace nearby +} // namespace location + +#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_CREDENTIAL_STORAGE_H_ diff --git a/internal/platform/implementation/BUILD b/internal/platform/implementation/BUILD index d0d30b8f..6f6e3fd8 100644 --- a/internal/platform/implementation/BUILD +++ b/internal/platform/implementation/BUILD @@ -57,6 +57,7 @@ cc_library( "ble_v2.h", "bluetooth_adapter.h", "bluetooth_classic.h", + "credential_storage.h", "server_sync.h", "wifi.h", "wifi_hotspot.h", diff --git a/internal/platform/implementation/credential_storage.h b/internal/platform/implementation/credential_storage.h new file mode 100644 index 00000000..e219b4fb --- /dev/null +++ b/internal/platform/implementation/credential_storage.h @@ -0,0 +1,89 @@ +// 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. + +#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_CREDENTIAL_STORAGE_H_ +#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_CREDENTIAL_STORAGE_H_ + +#include +#include +#include +#include + +#include "internal/platform/credential.h" +#include "internal/platform/exception.h" + +namespace location { +namespace nearby { +namespace api { + +enum class CredentialOperationStatus { + kFailed = 0, + kSucceeded = 1, +}; + +struct CredentialSelector { + std::string account_name; + TrustType trust_type; +}; + +struct SaveCredentialCallback { + std::function credentials_saved_cb; +}; + +struct GetPrivateCredentialCallback { + std::function>)> + credentials_fetched_cb; +}; + +struct GetPublicCredentialCallback { + std::function>)> + credentials_fetched_cb; +}; + +/* + * This class specifies the virtual functions for native platforms to implement. + */ +class CredentialStorage { + public: + CredentialStorage() = default; + virtual ~CredentialStorage() = default; + // Used for + // 1. Save/update private creds after (re)generate credentials invoked by + // manager app. (public_credentials will be empty for this case); Or + // 2. Update remote public creds after manager app downloaded a new batch of + // remote public creds and save to local storage. (private_credentials will + // be empty for this case). + // account_name will be used as the key in both options, and both would + // overwrite the previous credentials if there already exists credentials for + // that given account_name. + virtual void SaveCredentials( + std::string account_name, + std::vector private_credentials, + std::vector public_credentials, + SaveCredentialCallback callback); + + // Used to fetch private creds when broadcasting. + virtual void GetPrivateCredentials(CredentialSelector credential_selector, + GetPrivateCredentialCallback callback); + + // Used to fetch remote public creds when scanning. + virtual void GetPublicCredentials(CredentialSelector credential_selector, + GetPublicCredentialCallback callback); +}; + +} // namespace api +} // namespace nearby +} // namespace location + +#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_CREDENTIAL_STORAGE_H_ diff --git a/presence/BUILD b/presence/BUILD index 8bd7c245..6fd6dcbc 100644 --- a/presence/BUILD +++ b/presence/BUILD @@ -20,12 +20,14 @@ cc_library( ], hdrs = [ "presence_client.h", + "presence_service.h", ], visibility = [ "//third_party/nearby:__subpackages__", ], deps = [ ":types", + "//third_party/nearby/presence/implementation:internal", # build_cleaner: keep ], ) @@ -146,10 +148,12 @@ cc_test( size = "small", srcs = [ "presence_client_test.cc", + "presence_service_test.cc", ], shard_count = 6, deps = [ ":presence", + "//internal/platform/implementation/g3", "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_googletest//:gtest_main", ], diff --git a/presence/implementation/BUILD b/presence/implementation/BUILD new file mode 100644 index 00000000..5aca64aa --- /dev/null +++ b/presence/implementation/BUILD @@ -0,0 +1,35 @@ +# 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. +licenses(["notice"]) + +cc_library( + name = "internal", + srcs = [], + hdrs = [ + "broadcast_manager.h", + "credential_manager.h", + "mock_service_controller.h", + "scan_manager.h", + "service_controller.h", + "service_controller_impl.h", + ], + visibility = [ + "//third_party/nearby/presence:__subpackages__", + ], + deps = [ + "//internal/platform:base", + "//internal/platform:comm", + "//third_party/nearby/presence/implementation/mediums", + ], +) diff --git a/presence/implementation/broadcast_manager.h b/presence/implementation/broadcast_manager.h new file mode 100644 index 00000000..8567293d --- /dev/null +++ b/presence/implementation/broadcast_manager.h @@ -0,0 +1,43 @@ +// 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. + +#ifndef THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_BROADCAST_MANAGER_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_BROADCAST_MANAGER_H_ + +#include "third_party/nearby/presence/implementation/credential_manager.h" +#include "third_party/nearby/presence/implementation/mediums/mediums.h" + +namespace nearby { +namespace presence { + +/* + * The instance of BroadcastManager is owned by {@code ServiceControllerImpl}. + * Helping service controller to manage broadcast requests and callbacks. + */ +class BroadcastManager { + public: + BroadcastManager(Mediums& mediums, CredentialManager& credential_manager) { + mediums_ = &mediums, credential_manager_ = &credential_manager; + } + ~BroadcastManager() = default; + + private: + Mediums* mediums_; + CredentialManager* credential_manager_; +}; + +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_BROADCAST_MANAGER_H_ diff --git a/presence/implementation/credential_manager.h b/presence/implementation/credential_manager.h new file mode 100644 index 00000000..68ebe735 --- /dev/null +++ b/presence/implementation/credential_manager.h @@ -0,0 +1,82 @@ +// 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. + +#ifndef THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_CREDENTIAL_MANAGER_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_CREDENTIAL_MANAGER_H_ + +#include +#include +#include + +#include "internal/platform/credential.h" +#include "internal/platform/credential_storage.h" + +namespace nearby { +namespace presence { + +struct GenerateCredentialsCallback { + std::function)> + credentials_generated_cb; +}; + +struct UpdateRemotePublicCredentialsCallback { + std::function + credentials_updated_cb; +}; + +/* + * The instance of CredentialManager is owned by {@code ServiceControllerImpl}. + * Helping service controller to manage local credentials and coordinate with + * downloaded remote credentials. + */ +class CredentialManager { + public: + CredentialManager() = default; + ~CredentialManager() = default; + + // Used to (re)generate user’s private and public credentials. + // The generated private credentials will be saved to creds storage. + // The generated public credentials will be returned inside the + // credentials_generated_cb for manager app to upload to web. + // The user’s own public credentials won’t be saved on local credential + // storage. + void GenerateCredentials( + location::nearby::DeviceMetadata device_metadata, + std::vector trust_types, + GenerateCredentialsCallback credentials_generated_cb); + + // Update remote public credentials. + void UpdateRemotePublicCredentials( + std::string account_name, + std::vector remote_public_creds, + UpdateRemotePublicCredentialsCallback credentials_updated_cb); + + // Used to fetch private creds when broadcasting. + void GetPrivateCredentials( + location::nearby::api::CredentialSelector credential_selector, + location::nearby::api::GetPrivateCredentialCallback callback); + + // Used to fetch remote public creds when scanning. + void GetPublicCredentials( + location::nearby::api::CredentialSelector credential_selector, + location::nearby::api::GetPublicCredentialCallback callback); + + private: + location::nearby::CredentialStorage* credential_storage_; +}; + +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_CREDENTIAL_MANAGER_H_ diff --git a/presence/implementation/mediums/BUILD b/presence/implementation/mediums/BUILD new file mode 100644 index 00000000..597db6e4 --- /dev/null +++ b/presence/implementation/mediums/BUILD @@ -0,0 +1,30 @@ +# 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. +licenses(["notice"]) + +cc_library( + name = "mediums", + srcs = [], + hdrs = [ + "ble.h", + "mediums.h", + ], + visibility = [ + "//third_party/nearby/presence/implementation:__subpackages__", + ], + deps = [ + "//internal/platform:base", + "//internal/platform:comm", + ], +) diff --git a/presence/implementation/mediums/ble.h b/presence/implementation/mediums/ble.h new file mode 100644 index 00000000..1a14fcbe --- /dev/null +++ b/presence/implementation/mediums/ble.h @@ -0,0 +1,41 @@ +// 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. + +#ifndef THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MEDIUMS_BLE_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MEDIUMS_BLE_H_ + +#include "internal/platform/ble_v2.h" + +namespace nearby { +namespace presence { + +/* + * This Ble class utilizes platform/ble_v2 BleV2Medium, provides ble functions + * for presence logic layer to invoke. + * This class would have states like if ble is available or not, if it's doing + * broadcast/scan. + */ +class Ble { + public: + Ble() = default; + ~Ble() = default; + // TODO(hais): add expected function set. + private: + location::nearby::BleV2Medium medium_; +}; + +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MEDIUMS_BLE_H_ diff --git a/presence/implementation/mediums/mediums.h b/presence/implementation/mediums/mediums.h new file mode 100644 index 00000000..92fc2b4f --- /dev/null +++ b/presence/implementation/mediums/mediums.h @@ -0,0 +1,42 @@ +// 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. + +#ifndef THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MEDIUMS_MEDIUMS_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MEDIUMS_MEDIUMS_H_ + +#include "third_party/nearby/presence/implementation/mediums/ble.h" + +namespace nearby { +namespace presence { + +/* + * This class owns medium instance like Ble and etc. And the instance of + * this class will be owned in {@code ServiceControllerImpl}. + */ +class Mediums { + public: + Mediums() = default; + ~Mediums() = default; + + // Returns a handle to the Ble medium. + Ble& GetBle(); + + private: + Ble ble_; +}; + +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MEDIUMS_MEDIUMS_H_ diff --git a/presence/implementation/mock_service_controller.h b/presence/implementation/mock_service_controller.h new file mode 100644 index 00000000..65eeae0e --- /dev/null +++ b/presence/implementation/mock_service_controller.h @@ -0,0 +1,37 @@ +// 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. + +#ifndef THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MOCK_SERVICE_CONTROLLER_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MOCK_SERVICE_CONTROLLER_H_ + +#include "third_party/nearby/presence/implementation/service_controller.h" + +namespace nearby { +namespace presence { + +/* + * This class is for unit test, mocking {@code ServiceController} functions. + */ +class MockServiceController : public ServiceController { + public: + MockServiceController() = default; + ~MockServiceController() override = default; + + private: +}; + +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MOCK_SERVICE_CONTROLLER_H_ diff --git a/presence/implementation/scan_manager.h b/presence/implementation/scan_manager.h new file mode 100644 index 00000000..753f75cb --- /dev/null +++ b/presence/implementation/scan_manager.h @@ -0,0 +1,43 @@ +// 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. + +#ifndef THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SCAN_MANAGER_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SCAN_MANAGER_H_ + +#include "third_party/nearby/presence/implementation/credential_manager.h" +#include "third_party/nearby/presence/implementation/mediums/mediums.h" + +namespace nearby { +namespace presence { + +/* + * The instance of ScanManager is owned by {@code ServiceControllerImpl}. + * Helping service controller to manage scan requests and callbacks. + */ +class ScanManager { + public: + ScanManager(Mediums& mediums, CredentialManager& credential_manager) { + mediums_ = &mediums, credential_manager_ = &credential_manager; + } + ~ScanManager() = default; + + private: + Mediums* mediums_; + CredentialManager* credential_manager_; +}; + +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SCAN_MANAGER_H_ diff --git a/presence/implementation/service_controller.h b/presence/implementation/service_controller.h new file mode 100644 index 00000000..4d5fc312 --- /dev/null +++ b/presence/implementation/service_controller.h @@ -0,0 +1,34 @@ +// 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. + +#ifndef THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SERVICE_CONTROLLER_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SERVICE_CONTROLLER_H_ + +namespace nearby { +namespace presence { + +/* + * This class is owned in {@code PresenceService}. It specifies the function + * signatures. {@code ServiceControllerImpl} and {@code MockServiceController} + * inherit this class and provides real implementation and mock impl for tests. + */ +class ServiceController { + public: + virtual ~ServiceController() = default; +}; + +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SERVICE_CONTROLLER_H_ diff --git a/presence/implementation/service_controller_impl.h b/presence/implementation/service_controller_impl.h new file mode 100644 index 00000000..f664079a --- /dev/null +++ b/presence/implementation/service_controller_impl.h @@ -0,0 +1,42 @@ +// 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. + +#ifndef THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SERVICE_CONTROLLER_IMPL_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SERVICE_CONTROLLER_IMPL_H_ + +#include "third_party/nearby/presence/implementation/credential_manager.h" +#include "third_party/nearby/presence/implementation/mediums/mediums.h" +#include "third_party/nearby/presence/implementation/service_controller.h" + +/* + * This class implements {@code ServiceController} functions. Owns mediums and + * other managers instances. + */ +namespace nearby { +namespace presence { + +class ServiceControllerImpl : public ServiceController { + public: + ServiceControllerImpl() = default; + ~ServiceControllerImpl() override = default; + + private: + Mediums mediums_; // NOLINT: further impl will use it. + CredentialManager credential_manager_; // NOLINT: further impl will use it. +}; + +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_SERVICE_CONTROLLER_IMPL_H_ diff --git a/presence/presence_service.h b/presence/presence_service.h new file mode 100644 index 00000000..86142571 --- /dev/null +++ b/presence/presence_service.h @@ -0,0 +1,42 @@ +// 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. + +#ifndef THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_SERVICE_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_SERVICE_H_ + +#include + +#include "third_party/nearby/presence/implementation/service_controller.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() = default; + ~PresenceService() = default; + + private: + std::unique_ptr service_controller_; +}; + +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_SERVICE_H_ diff --git a/presence/presence_service_test.cc b/presence/presence_service_test.cc new file mode 100644 index 00000000..f32e4312 --- /dev/null +++ b/presence/presence_service_test.cc @@ -0,0 +1,29 @@ +// 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 "third_party/nearby/presence/presence_service.h" + +#include "gtest/gtest.h" + +namespace nearby { +namespace presence { +namespace { + +TEST(PresenceServiceTest, DefaultConstructorWorks) { + PresenceService presence_service; +} + +} // namespace +} // namespace presence +} // namespace nearby