// 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. #include "internal/platform/implementation/g3/preferences_repository.h" #include #include #include "absl/synchronization/mutex.h" #include "nlohmann/json.hpp" #include "nlohmann/json_fwd.hpp" #include "internal/base/file_path.h" #include "internal/base/files.h" namespace nearby { namespace g3 { namespace { using json = nlohmann::json; } // namespace json PreferencesRepository::LoadPreferences() { absl::MutexLock lock(mutex_); // Emulate Windows implementation try { if (!Files::FileExists(file_path_)) { return value_; } std::ifstream preferences_file(file_path_.GetPath()); if (!preferences_file.good()) { return value_; } json preferences = json::parse(preferences_file, nullptr, false); preferences_file.close(); if (preferences.is_discarded()) { return value_; } value_ = preferences; } catch (...) { return value_; } return value_; } bool PreferencesRepository::SavePreferences(json preferences) { absl::MutexLock lock(mutex_); value_ = preferences; std::ofstream preferences_file(file_path_.GetPath(), std::ios_base::trunc); preferences_file << preferences; preferences_file.close(); return true; } } // namespace g3 } // namespace nearby