• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 DeleteDeviceProfile(const std::string& key);
47     virtual int32_t PutDeviceProfile(const std::string& key, const std::string& value);
48     virtual int32_t PutDeviceProfileBatch(const std::vector<std::string>& keys,
49         const std::vector<std::string>& values);
50     virtual int32_t SyncDeviceProfile(const std::vector<std::string>& deviceIdList,
51         SyncMode syncMode);
52     virtual int32_t RemoveDeviceData(const std::string networkId);
53     virtual int32_t RegisterSyncCallback(const std::shared_ptr<DistributedKv::KvStoreSyncCallback>& sycnCb);
54     virtual int32_t UnRegisterSyncCallback();
55 
56     void SetOptions(const DistributedKv::Options& options);
57     StorageInitStatus GetInitStatus();
58     bool RegisterKvStoreInitCallback(const KvStoreInitCallback& callback);
59     int32_t SubscribeKvStore(const std::shared_ptr<DistributedKv::KvStoreObserver>& observer);
60     int32_t UnSubscribeKvStore(const std::shared_ptr<DistributedKv::KvStoreObserver>& observer);
61 
62 private:
63     bool TryGetKvStore();
64     bool InitKvStore();
65     DistributedKv::Status GetKvStore();
66     void DeleteKvStore();
67     void SubscribeDeviceProfileKvStore();
68     bool CheckTrustGroup(const std::vector<std::string>& deviceIdList);
69 
70 private:
71     DistributedKv::Options options_;
72     DistributedKv::AppId appId_;
73     DistributedKv::StoreId storeId_;
74     DistributedKv::DistributedKvDataManager dataManager_;
75 
76     std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_;
77     std::atomic<StorageInitStatus> initStatus_ {StorageInitStatus::UNINITED};
78     KvStoreInitCallback kvStoreInitCallback_;
79     std::shared_mutex storageLock_;
80 };
81 } // namespace DeviceProfile
82 } // namespace OHOS
83 #endif // OHOS_DISTRIBUTED_DEVICE_PROFILE_STORAGE_H
84