// 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_PREFERENCE_MANAGER_H_ #define THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_PREFERENCE_MANAGER_H_ #include #include #include #include #include #include #if defined(__has_include) #if __has_include("location/nearby/sharing/lib/sync/sync_binding_prefs.pb.h") #include "location/nearby/sharing/lib/sync/sync_binding_prefs.pb.h" #define NEARBY_HAS_SYNC_BINDING_PREFS_PROTO 1 #endif // __has_include("location/nearby/sharing/lib/sync/sync_binding_prefs.pb.h") #if __has_include("location/nearby/sharing/lib/sync/sync_config_prefs.pb.h") #include "location/nearby/sharing/lib/sync/sync_config_prefs.pb.h" #define NEARBY_HAS_SYNC_CONFIG_PREFS_PROTO 1 #endif // __has_include("location/nearby/sharing/lib/sync/sync_config_prefs.pb.h") #endif // defined(__has_include) #include "sharing/proto/wire_format.pb.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "absl/types/span.h" #include "sharing/internal/api/private_certificate_data.h" #ifndef NEARBY_HAS_SYNC_CONFIG_PREFS_PROTO namespace nearby::sharing::sync { class SyncConfigPrefs { public: bool ParseFromString(const std::string& serialized) { return sync_config_.ParseFromString(serialized); } std::string SerializeAsString() const { return sync_config_.SerializeAsString(); } const nearby::sharing::service::proto::SyncConfig& sync_config() const { return sync_config_; } nearby::sharing::service::proto::SyncConfig* mutable_sync_config() { return &sync_config_; } private: nearby::sharing::service::proto::SyncConfig sync_config_; }; } // namespace nearby::sharing::sync #endif // NEARBY_HAS_SYNC_CONFIG_PREFS_PROTO #ifndef NEARBY_HAS_SYNC_BINDING_PREFS_PROTO namespace nearby::sharing::sync { class SyncBindingPrefs { public: bool ParseFromString(const std::string&) { return false; } std::string SerializeAsString() const { return {}; } }; } // namespace nearby::sharing::sync #endif // NEARBY_HAS_SYNC_BINDING_PREFS_PROTO namespace nearby::sharing::api { class PreferenceManager { public: virtual ~PreferenceManager() = default; virtual void SetBoolean(absl::string_view key, bool value) = 0; virtual void SetInteger(absl::string_view key, int value) = 0; virtual void SetInt64(absl::string_view key, int64_t value) = 0; virtual void SetString(absl::string_view key, absl::string_view value) = 0; virtual void SetTime(absl::string_view key, absl::Time value) = 0; // Array operations, where key contains an array. virtual void SetBooleanArray(absl::string_view key, absl::Span value) = 0; virtual void SetIntegerArray(absl::string_view key, absl::Span value) = 0; virtual void SetInt64Array(absl::string_view key, absl::Span value) = 0; virtual void SetStringArray(absl::string_view key, absl::Span value) = 0; virtual void SetPrivateCertificateArray( absl::string_view key, absl::Span value) = 0; // Expiration data is a std::pair containing the certificate ID base64 encoded // according to RFC 4648 section 5 and the expiration time in nanos since Unix // Epoch. virtual void SetCertificateExpirationArray( absl::string_view key, absl::Span> value) = 0; // Dictionary operations, where key contains a dictionary, and dictionary_item // is modified. // If key is empty, a new dictionary containing the new dictionary_item is // created. // If key exists and does not contain a dictionary, the operation fails // silently. virtual void SetDictionaryBooleanValue(absl::string_view key, absl::string_view dictionary_item, bool value) = 0; virtual void SetDictionaryIntegerValue(absl::string_view key, absl::string_view dictionary_item, int value) = 0; virtual void SetDictionaryInt64Value(absl::string_view key, absl::string_view dictionary_item, int64_t value) = 0; virtual void SetDictionaryStringValue(absl::string_view key, absl::string_view dictionary_item, std::string value) = 0; virtual void RemoveDictionaryItem(absl::string_view key, absl::string_view dictionary_item) = 0; virtual void SetSyncConfigValue( absl::string_view binding_id, const nearby::sharing::sync::SyncConfigPrefs& value) = 0; virtual void SetSyncBindingValue( const nearby::sharing::sync::SyncBindingPrefs& value) = 0; // Gets values virtual bool GetBoolean(absl::string_view key, bool default_value) const = 0; virtual int GetInteger(absl::string_view key, int default_value) const = 0; virtual int64_t GetInt64(absl::string_view key, int64_t default_value) const = 0; virtual std::string GetString(absl::string_view key, const std::string& default_value) const = 0; virtual absl::Time GetTime(absl::string_view key, absl::Time default_value) const = 0; virtual std::vector GetBooleanArray( absl::string_view key, absl::Span default_value) const = 0; virtual std::vector GetIntegerArray( absl::string_view key, absl::Span default_value) const = 0; virtual std::vector GetInt64Array( absl::string_view key, absl::Span default_value) const = 0; virtual std::vector GetStringArray( absl::string_view key, absl::Span default_value) const = 0; virtual std::vector GetPrivateCertificateArray( absl::string_view key) const = 0; // Expiration data is a std::pair containing the certificate ID base64 encoded // according to RFC 4648 section 5 and the expiration time in nanos since Unix // Epoch. virtual std::vector> GetCertificateExpirationArray(absl::string_view key) const = 0; // Dictionary operations, where key contains a dictionary. // If key does not exist, does not contain a dictionary, or the dictionary // does not contain dictionary_item, std::nullopt is returned. virtual std::optional GetDictionaryBooleanValue( absl::string_view key, absl::string_view dictionary_item) const = 0; virtual std::optional GetDictionaryIntegerValue( absl::string_view key, absl::string_view dictionary_item) const = 0; virtual std::optional GetDictionaryInt64Value( absl::string_view key, absl::string_view dictionary_item) const = 0; virtual std::optional GetDictionaryStringValue( absl::string_view key, absl::string_view dictionary_item) const = 0; virtual std::optional GetSyncConfigValue(absl::string_view binding_id) const = 0; virtual std::optional GetSyncBindingValue() const = 0; // Removes preferences virtual void Remove(absl::string_view key) = 0; // Removes all sync configs. // Observers are not notified for each removed config. virtual void RemoveAllSyncConfigs() = 0; // Removes all binding configs. // Observers are not notified for each removed config. virtual void RemoveAllBindingConfigs() = 0; // Adds preference observer virtual void AddObserver( absl::string_view name, std::function observer) = 0; virtual void RemoveObserver(absl::string_view name) = 0; }; } // namespace nearby::sharing::api #endif // THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_PREFERENCE_MANAGER_H_