diff --git a/connections/implementation/BUILD b/connections/implementation/BUILD index 308741e1..2e08cc45 100644 --- a/connections/implementation/BUILD +++ b/connections/implementation/BUILD @@ -1,6 +1,3 @@ -load("@rules_cc//cc:cc_library.bzl", "cc_library") -load("@rules_cc//cc:cc_test.bzl", "cc_test") - # Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,6 +11,10 @@ load("@rules_cc//cc:cc_test.bzl", "cc_test") # 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. + +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("@rules_cc//cc:cc_test.bzl", "cc_test") + licenses(["notice"]) cc_library( @@ -164,6 +165,7 @@ cc_library( "//connections/implementation/proto:offline_wire_formats_cc_proto", "//connections/v3:v3_types", "//internal/analytics:event_logger", + "//internal/base:file_path", "//internal/base:masker", "//internal/flags:nearby_flags", "//internal/interop:authentication_status", diff --git a/connections/implementation/client_proxy.cc b/connections/implementation/client_proxy.cc index 78d37928..22d20b6b 100644 --- a/connections/implementation/client_proxy.cc +++ b/connections/implementation/client_proxy.cc @@ -52,11 +52,13 @@ #include "connections/v3/connections_device_provider.h" #include "connections/v3/listeners.h" #include "internal/analytics/event_logger.h" +#include "internal/base/file_path.h" #include "internal/flags/nearby_flags.h" #include "internal/interop/device.h" #include "internal/platform/byte_array.h" #include "internal/platform/cancelable_alarm.h" #include "internal/platform/cancellation_flag.h" +#include "internal/platform/device_info_impl.h" #include "internal/platform/error_code_params.h" #include "internal/platform/error_code_recorder.h" #include "internal/platform/feature_flags.h" @@ -81,6 +83,8 @@ constexpr char kEndpointIdChars[] = { 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; +constexpr absl::string_view kPreferencesFilePath = "Google/Nearby/Connections"; + bool IsFeatureUseStableEndpointIdEnabled() { return NearbyFlags::GetInstance().GetBoolFlag( connections::config_package_nearby::nearby_connections_feature:: @@ -91,6 +95,12 @@ bool IsFeatureUseStableEndpointIdEnabled() { ClientProxy::ClientProxy(::nearby::analytics::EventLogger* event_logger) : client_id_(Prng().NextInt64()) { VLOG(1) << "ClientProxy ctor event_logger=" << event_logger; + if (NearbyFlags::GetInstance().GetBoolFlag( + config_package_nearby::nearby_connections_feature:: + kEnableNearbyConnectionsPreferences)) { + InitializePreferencesManager(); + } + is_dct_enabled_ = NearbyFlags::GetInstance().GetBoolFlag( config_package_nearby::nearby_connections_feature::kEnableDct); analytics_recorder_ = @@ -172,8 +182,8 @@ std::optional ClientProxy::GetBluetoothMacAddress( return std::nullopt; } -void ClientProxy::SetBluetoothMacAddress( - const std::string& endpoint_id, MacAddress bluetooth_mac_address) { +void ClientProxy::SetBluetoothMacAddress(const std::string& endpoint_id, + MacAddress bluetooth_mac_address) { bluetooth_mac_addresses_[endpoint_id] = bluetooth_mac_address; } @@ -1188,9 +1198,7 @@ void ClientProxy::ScheduleClearCachedEndpointIdAlarm() { << GetClientId() << "; cached_endpoint_id_=" << cached_endpoint_id_; cached_endpoint_id_alarm_ = std::make_unique( "clear_high_power_endpoint_id_cache", - [this]() { - ClearCachedLocalEndpointId(); - }, + [this]() { ClearCachedLocalEndpointId(); }, kHighPowerAdvertisementEndpointIdCacheTimeout, &single_thread_executor_); } @@ -1348,6 +1356,22 @@ std::optional ClientProxy::GetEndpointIdForDct() const { return dct_endpoint_id_; } +void ClientProxy::InitializePreferencesManager() { + LOG(INFO) << "ClientProxy [InitializePreferencesManager]: client=" + << GetClientId(); + auto device_info_ = std::make_unique(); + preferences_manager_ = api::ImplementationPlatform::CreatePreferencesManager( + device_info_->GetAppDataPath() + .append(FilePath(kPreferencesFilePath)) + .ToString()); + + if (preferences_manager_ == nullptr) { + LOG(ERROR) << "ClientProxy [Failed to initialize preferences manager]: " + "client=" + << GetClientId(); + } +} + std::string ClientProxy::ToString(PayloadProgressInfo::Status status) const { switch (status) { case PayloadProgressInfo::Status::kSuccess: diff --git a/connections/implementation/client_proxy.h b/connections/implementation/client_proxy.h index 30183ec8..aa004633 100644 --- a/connections/implementation/client_proxy.h +++ b/connections/implementation/client_proxy.h @@ -45,6 +45,7 @@ #include "internal/platform/cancelable_alarm.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/error_code_recorder.h" +#include "internal/platform/implementation/preferences_manager.h" #include "internal/platform/mac_address.h" #include "internal/platform/mutex.h" // Prefer using absl:: versions of a set and a map; they tend to be more @@ -458,6 +459,8 @@ class ClientProxy final { std::optional GetEndpointIdForDct() const; + void InitializePreferencesManager(); + // The device name used for DCT advertising. std::string dct_device_name_; // The dedup value used for DCT advertising. @@ -534,6 +537,9 @@ class ClientProxy final { std::unique_ptr default_cancellation_flag_ = std::make_unique(true); + // A preferences manager for storing client-specific preferences. + std::unique_ptr preferences_manager_; + // An analytics logger with |EventLogger| provided by client, which is default // nullptr as no-op. std::unique_ptr analytics_recorder_;