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_MANAGER_H
17 #define OHOS_DISTRIBUTED_DEVICE_PROFILE_STORAGE_MANAGER_H
18 
19 #include <atomic>
20 #include <map>
21 
22 #include "device_profile_storage.h"
23 #include "event_handler.h"
24 #include "kvstore_death_recipient_dp.h"
25 #include "online_sync_table.h"
26 #include "service_characteristic_profile.h"
27 #include "single_instance.h"
28 
29 #include "dp_device_manager.h"
30 #include "sync_options.h"
31 
32 #include "nlohmann/json.hpp"
33 
34 namespace OHOS {
35 namespace DeviceProfile {
36 enum class KeyType : int8_t {
37     UNKNOWN = -1,
38     SERVICE = 0,
39     SERVICE_LIST = 1,
40     DEVICE_INFO = 2
41 };
42 
43 class DeviceProfileStorageManager {
44     DECLARE_SINGLE_INSTANCE(DeviceProfileStorageManager);
45     friend class DeviceProfile::DpDeviceManager;
46 
47 public:
48     bool Init();
49     void OnKvStoreInitDone();
50 
51     int32_t PutDeviceProfile(const ServiceCharacteristicProfile& profile);
52     int32_t DeleteDeviceProfile(const std::string& serviceId);
53     int32_t GetDeviceProfile(const std::string& udid, const std::string& serviceId,
54         ServiceCharacteristicProfile& profile);
55     int32_t SyncDeviceProfile(const SyncOptions& syncOptions,
56         const sptr<IRemoteObject>& profileEventNotifier);
57 
58     int32_t SubscribeKvStore(const std::shared_ptr<DistributedKv::KvStoreObserver>& observer);
59     int32_t UnSubscribeKvStore(const std::shared_ptr<DistributedKv::KvStoreObserver>& observer);
60     int32_t RegisterSyncCallback(const std::shared_ptr<DistributedKv::KvStoreSyncCallback>& sycnCb);
61     int32_t UnRegisterSyncCallback();
62     void NotifySyncCompleted();
63     void NotifySubscriberDied(const sptr<IRemoteObject>& profileEventNotifier);
64     int32_t RemoveUnBoundDeviceProfile(const std::string& udid);
65     int32_t RemoveRemoteDeviceProfile();
66     void DumpLocalProfile(std::string& result);
67 
68 private:
69     bool WaitKvDataService();
70     void RestoreServiceItemLocked(const std::string& value);
71     void RegisterCallbacks();
72     void FlushProfileItems();
73     std::string GenerateKey(const std::string& udid, const std::string& key, KeyType keyType);
74 
75     bool CheckSyncOption(const SyncOptions& syncOptions);
76     int32_t NotifySyncStart(const sptr<IRemoteObject>& profileEventNotifier);
77     void SetServiceType(const std::string& udid, const std::string& serviceId, ServiceCharacteristicProfile& profile);
78     void ReportBehaviorEvent(const std::string& event);
79     void ReportFaultEvent(const std::string& event, const std::string& key, const int32_t result);
80 
81 private:
82     std::mutex serviceLock_;
83     std::mutex callbackLock_;
84     std::mutex initLock_;
85     nlohmann::json servicesJson_;
86     std::shared_ptr<DeviceProfileStorage> onlineSyncTbl_;
87     std::shared_ptr<AppExecFwk::EventHandler> storageHandler_;
88     sptr<IRemoteObject::DeathRecipient> kvStoreDeathRecipientDp_;
89     std::string localUdid_;
90     std::map<std::string, std::string> profileItems_;
91     std::atomic<bool> kvDataServiceFailed_ {false};
92     std::atomic<bool> inited_ {false};
93     std::shared_ptr<DistributedKv::KvStoreObserver> kvStoreObserver_;
94     std::shared_ptr<DistributedKv::KvStoreSyncCallback> kvStoreSyncCallback_;
95     std::mutex profileSyncLock_;
96     sptr<IRemoteObject> syncEventNotifier_;
97     std::atomic<bool> isSync_ {false};
98 };
99 } // namespace DeviceProfile
100 } // namespace OHOS
101 #endif // OHOS_DISTRIBUTED_DEVICE_PROFILE_STORAGE_MANAGER_H