1 /*
2  * Copyright (c) 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_META_INFO_MANAGER_H
17 #define OHOS_DISTRIBUTED_HARDWARE_META_INFO_MANAGER_H
18 
19 #include <condition_variable>
20 #include <map>
21 
22 #include "kvstore_observer.h"
23 
24 #include "db_adapter.h"
25 #include "event_handler.h"
26 #include "meta_capability_info.h"
27 
28 class DBAdapter;
29 namespace OHOS {
30 namespace DistributedHardware {
31 class MetaInfoManager : public std::enable_shared_from_this<MetaInfoManager>,
32                         public DistributedKv::KvStoreObserver {
33 public:
34     MetaInfoManager(const MetaInfoManager &) = delete;
35     MetaInfoManager &operator = (const MetaInfoManager &) = delete;
36     MetaInfoManager(MetaInfoManager &&) = delete;
37     MetaInfoManager &operator = (MetaInfoManager &&) = delete;
38     static std::shared_ptr<MetaInfoManager> GetInstance();
39     MetaInfoManager();
40     virtual ~MetaInfoManager();
41     int32_t Init();
42     int32_t UnInit();
43     int32_t AddMetaCapInfos(const std::vector<std::shared_ptr<MetaCapabilityInfo>> &meatCapInfos);
44     int32_t SyncMetaInfoFromDB(const std::string &udidHash);
45     int32_t SyncRemoteMetaInfos();
46     int32_t GetDataByKeyPrefix(const std::string &keyPrefix, MetaCapInfoMap &metaCapMap);
47     int32_t RemoveMetaInfoByKey(const std::string &key);
48     int32_t GetMetaCapInfo(const std::string &udidHash, const std::string &dhId,
49         std::shared_ptr<MetaCapabilityInfo> &metaCapPtr);
50     void GetMetaCapInfosByUdidHash(const std::string &udidHash,
51         std::vector<std::shared_ptr<MetaCapabilityInfo>> &metaCapInfos);
52     int32_t GetMetaDataByDHType(const DHType dhType, MetaCapInfoMap &metaInfoMap);
53     int32_t SyncDataByNetworkId(const std::string &networkId);
54     /* Database data changes callback */
55     virtual void OnChange(const DistributedKv::ChangeNotification &changeNotification) override;
56     /* Cloud data changes callback */
57     void OnChange(const DistributedKv::DataOrigin &origin, Keys &&keys) override;
58 
59     class MetaInfoManagerEventHandler : public AppExecFwk::EventHandler {
60         public:
61             MetaInfoManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner,
62                 std::shared_ptr<MetaInfoManager> metaInfoMgrPtr);
63             ~MetaInfoManagerEventHandler() override = default;
64             void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
65         private:
66             std::weak_ptr<MetaInfoManager> metaInfoMgrWPtr_;
67     };
68     std::shared_ptr<MetaInfoManager::MetaInfoManagerEventHandler> GetEventHandler();
69 
70 private:
71     int32_t GetMetaCapByValue(const std::string &value, std::shared_ptr<MetaCapabilityInfo> &metaCapPtr);
72     void HandleMetaCapabilityAddChange(const std::vector<DistributedKv::Entry> &insertRecords);
73     void HandleMetaCapabilityUpdateChange(const std::vector<DistributedKv::Entry> &updateRecords);
74     void HandleMetaCapabilityDeleteChange(const std::vector<DistributedKv::Entry> &deleteRecords);
75     std::vector<DistributedKv::Entry> GetEntriesByKeys(const std::vector<std::string> &keys);
76 private:
77     mutable std::mutex metaInfoMgrMutex_;
78     std::shared_ptr<DBAdapter> dbAdapterPtr_;
79     MetaCapInfoMap globalMetaInfoMap_;
80 
81     std::shared_ptr<MetaInfoManager::MetaInfoManagerEventHandler> eventHandler_;
82 };
83 } // namespace DistributedHardware
84 } // namespace OHOS
85 #endif