1 /* 2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_H 17 #define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_H 18 19 #include <condition_variable> 20 #include <map> 21 #include <set> 22 23 #include "kvstore_observer.h" 24 25 #include "db_adapter.h" 26 #include "event_handler.h" 27 #include "impl_utils.h" 28 #include "single_instance.h" 29 #include "version_info.h" 30 31 class DBAdapter; 32 namespace OHOS { 33 namespace DistributedHardware { 34 class VersionInfoManager : public std::enable_shared_from_this<VersionInfoManager>, 35 public DistributedKv::KvStoreObserver { 36 public: 37 VersionInfoManager(const VersionInfoManager &) = delete; 38 VersionInfoManager &operator = (const VersionInfoManager &) = delete; 39 VersionInfoManager(VersionInfoManager &&) = delete; 40 VersionInfoManager &operator = (VersionInfoManager &&) = delete; 41 static std::shared_ptr<VersionInfoManager> GetInstance(); 42 VersionInfoManager(); 43 virtual ~VersionInfoManager(); 44 45 int32_t Init(); 46 int32_t UnInit(); 47 48 int32_t AddVersion(const VersionInfo &versionInfo); 49 int32_t GetVersionInfoByDeviceId(const std::string &deviceId, VersionInfo &versionInfo); 50 int32_t RemoveVersionInfoByDeviceId(const std::string &deviceId); 51 int32_t SyncVersionInfoFromDB(const std::string &deviceId); 52 int32_t SyncRemoteVersionInfos(); 53 54 void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; 55 class VersionInfoManagerEventHandler : public AppExecFwk::EventHandler { 56 public: 57 VersionInfoManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner, 58 std::shared_ptr<VersionInfoManager> versionInfoMgrPtr); 59 ~VersionInfoManagerEventHandler() override = default; 60 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 61 private: 62 std::weak_ptr<VersionInfoManager> versionInfoMgrWPtr_; 63 }; 64 std::shared_ptr<VersionInfoManager::VersionInfoManagerEventHandler> GetEventHandler(); 65 66 private: 67 void UpdateVersionCache(const VersionInfo &versionInfo); 68 void HandleVersionAddChange(const std::vector<DistributedKv::Entry> &insertRecords); 69 void HandleVersionUpdateChange(const std::vector<DistributedKv::Entry> &updateRecords); 70 void HandleVersionDeleteChange(const std::vector<DistributedKv::Entry> &deleteRecords); 71 72 private: 73 mutable std::mutex verInfoMgrMutex_; 74 std::shared_ptr<DBAdapter> dbAdapterPtr_; 75 std::shared_ptr<VersionInfoManager::VersionInfoManagerEventHandler> eventHandler_; 76 }; 77 } // namespace DistributedHardware 78 } // namespace OHOS 79 #endif 80