1 /*
2  * Copyright (c) 2021-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_DEVICE_PROFILE_STORAGE_H
17 #define OHOS_DISTRIBUTED_DEVICE_PROFILE_STORAGE_H
18 
19 #include <atomic>
20 #include <shared_mutex>
21 
22 #include "sync_options.h"
23 
24 #include "distributed_kv_data_manager.h"
25 #include "event_handler.h"
26 #include "kvstore_observer.h"
27 #include "kvstore_sync_callback.h"
28 
29 namespace OHOS {
30 namespace DeviceProfile {
31 enum class StorageInitStatus {
32     UNINITED = 1,
33     INIT_FAILED = 2,
34     INIT_SUCCEED = 3
35 };
36 
37 class DeviceProfileStorage {
38 public:
39     using KvStoreInitCallback = std::function<void()>;
40 
41     DeviceProfileStorage(const std::string& appId, const std::string& storeId);
42     virtual ~DeviceProfileStorage() = default;
43 
44     virtual void Init();
45     virtual int32_t GetDeviceProfile(const std::string& key, std::string& value);
46     virtual int32_t GetDeviceProfile(const std::string& udid, const std::string& key, std::string& value);
47     virtual int32_t DeleteDeviceProfile(const std::string& key);
48     virtual int32_t PutDeviceProfile(const std::string& key, const std::string& value);
49     virtual int32_t PutDeviceProfileBatch(const std::vector<std::string>& keys,
50         const std::vector<std::string>& values);
51     virtual int32_t SyncDeviceProfile(const std::vector<std::string>& deviceIdList,
52         SyncMode syncMode);
53     virtual int32_t RemoveDeviceData(const std::string networkId);
54     virtual int32_t RegisterSyncCallback(const std::shared_ptr<DistributedKv::KvStoreSyncCallback>& sycnCb);
55     virtual int32_t UnRegisterSyncCallback();
56 
57     void SetOptions(const DistributedKv::Options& options);
58     StorageInitStatus GetInitStatus();
59     bool RegisterKvStoreInitCallback(const KvStoreInitCallback& callback);
60     int32_t SubscribeKvStore(const std::shared_ptr<DistributedKv::KvStoreObserver>& observer);
61     int32_t UnSubscribeKvStore(const std::shared_ptr<DistributedKv::KvStoreObserver>& observer);
62 
63 private:
64     bool TryGetKvStore();
65     bool InitKvStore();
66     DistributedKv::Status GetKvStore();
67     void DeleteKvStore();
68     void SubscribeDeviceProfileKvStore();
69     bool CheckTrustGroup(const std::vector<std::string>& deviceIdList);
70     std::vector<std::string> CheckTrustDeviceList(const std::vector<std::string> &deviceIdList);
71     std::vector<std::string> GetOnlineDevices();
72 
73 private:
74     DistributedKv::Options options_;
75     DistributedKv::AppId appId_;
76     DistributedKv::StoreId storeId_;
77     DistributedKv::DistributedKvDataManager dataManager_;
78 
79     std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_;
80     std::atomic<StorageInitStatus> initStatus_ {StorageInitStatus::UNINITED};
81     KvStoreInitCallback kvStoreInitCallback_;
82     std::shared_mutex storageLock_;
83 };
84 } // namespace DeviceProfile
85 } // namespace OHOS
86 #endif // OHOS_DISTRIBUTED_DEVICE_PROFILE_STORAGE_H
87