• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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.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     void DumpLocalProfile(std::string& result);
66 
67 private:
68     bool WaitKvDataService();
69     void RestoreServiceItemLocked(const std::string& value);
70     void RegisterCallbacks();
71     void FlushProfileItems();
72     std::string GenerateKey(const std::string& udid, const std::string& key, KeyType keyType);
73 
74     bool CheckSyncOption(const SyncOptions& syncOptions);
75     int32_t NotifySyncStart(const sptr<IRemoteObject>& profileEventNotifier);
76     void SetServiceType(const std::string& udid, const std::string& serviceId, ServiceCharacteristicProfile& profile);
77     void ReportBehaviorEvent(const std::string& event);
78     void ReportFaultEvent(const std::string& event, const std::string& key, const int32_t result);
79 
80 private:
81     std::mutex serviceLock_;
82     std::mutex callbackLock_;
83     nlohmann::json servicesJson_;
84     std::shared_ptr<DeviceProfileStorage> onlineSyncTbl_;
85     std::shared_ptr<AppExecFwk::EventHandler> storageHandler_;
86     sptr<IRemoteObject::DeathRecipient> kvStoreDeathRecipient_;
87     std::string localUdid_;
88     std::map<std::string, std::string> profileItems_;
89     std::atomic<bool> kvDataServiceFailed_ {false};
90     std::atomic<bool> inited_ {false};
91     std::shared_ptr<DistributedKv::KvStoreObserver> kvStoreObserver_;
92     std::shared_ptr<DistributedKv::KvStoreSyncCallback> kvStoreSyncCallback_;
93     std::mutex profileSyncLock_;
94     sptr<IRemoteObject> syncEventNotifier_;
95     std::atomic<bool> isSync_ {false};
96 };
97 } // namespace DeviceProfile
98 } // namespace OHOS
99 #endif // OHOS_DISTRIBUTED_DEVICE_PROFILE_STORAGE_MANAGER_H