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_DP_DEVICE_PROFILE_MANAGER_H 17 #define OHOS_DP_DEVICE_PROFILE_MANAGER_H 18 19 #include <atomic> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <vector> 24 25 #include "dm_device_info.h" 26 #include "iremote_object.h" 27 28 #include "characteristic_profile.h" 29 #include "device_profile.h" 30 #include "dp_sync_options.h" 31 #include "i_dp_sync_adapter.h" 32 #include "i_sync_completed_callback.h" 33 #include "ikv_adapter.h" 34 #include "kv_data_change_listener.h" 35 #include "kv_store_death_recipient.h" 36 #include "kv_sync_completed_listener.h" 37 #include "service_profile.h" 38 #include "single_instance.h" 39 #include "trusted_device_info.h" 40 41 namespace OHOS { 42 namespace DistributedDeviceProfile { 43 class DeviceProfileManager { 44 DECLARE_SINGLE_INSTANCE(DeviceProfileManager); 45 46 public: 47 int32_t Init(); 48 int32_t UnInit(); 49 int32_t ReInit(); 50 int32_t PutDeviceProfile(const DeviceProfile& deviceProfile); 51 int32_t PutServiceProfile(const ServiceProfile& serviceProfile); 52 int32_t PutServiceProfileBatch(const std::vector<ServiceProfile>& serviceProfiles); 53 int32_t PutCharacteristicProfile(const CharacteristicProfile& charProfile); 54 int32_t PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile>& charProfiles); 55 int32_t GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile); 56 int32_t GetServiceProfile(const std::string& deviceId, const std::string& serviceName, 57 ServiceProfile& serviceProfile); 58 int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, 59 const std::string& characteristicKey, CharacteristicProfile& charProfile); 60 int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName, bool isMultiUser = false, 61 int32_t userId = DEFAULT_USER_ID); 62 int32_t DeleteCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, 63 const std::string& characteristicKey, bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID); 64 int32_t DeleteRemovedUserData(int32_t userId); 65 int32_t GetAllDeviceProfile(std::vector<DeviceProfile>& deviceProfiles); 66 int32_t GetAllServiceProfile(std::vector<ServiceProfile>& serviceProfiles); 67 int32_t GetAllCharacteristicProfile(std::vector<CharacteristicProfile>& charProfiles); 68 int32_t SyncDeviceProfile(const DistributedDeviceProfile::DpSyncOptions& syncOptions, 69 sptr<IRemoteObject> syncCompletedCallback); 70 std::vector<DistributedKv::Entry> GetEntriesByKeys(const std::vector<std::string>& keys); 71 int32_t SavePutTempCache(std::map<std::string, std::string>& entries); 72 bool IsFirstInitDB(); 73 void ResetFirst(); 74 void OnDeviceOnline(const TrustedDeviceInfo& trustedDeviceInfo); 75 void OnUserChange(int32_t lastUserId, int32_t curUserId); 76 77 private: 78 bool LoadDpSyncAdapter(); 79 void UnloadDpSyncAdapter(); 80 int32_t RunloadedFunction(const std::string& deviceId, sptr<IRemoteObject> syncCompletedCallback); 81 int32_t SyncWithNotOHBasedDevice(const std::vector<std::string>& notOHBasedDevices, 82 const std::string& callerDescriptor, sptr<IRemoteObject> syncCompletedCallback); 83 void SyncWithNotOHBasedDeviceFailed(const std::vector<std::string>& notOHBasedDevices, 84 sptr<IRemoteObject> syncCompletedCallback); 85 void AddToPutTempCache(const std::map<std::string, std::string>& values); 86 void FixDataOnDeviceOnline(const TrustedDeviceInfo& deviceInfo); 87 int32_t DeleteBatchByKeys(const std::vector<std::string>& delKeys); 88 int32_t GetProfilesByOwner(const std::string& uuid, std::map<std::string, std::string>& entries); 89 int32_t GetProfilesByKeyPrefix(const std::string& udid, std::map<std::string, std::string>& entries); 90 // Clean data that does not belong to the local. 91 void FixLocalData(const std::string& localUdid, const std::map<std::string, std::string>& localDataByOwner); 92 // Clean ohbase data when the peer is non-ohbase 93 void FixRemoteDataWhenPeerIsNonOH(const std::string& remoteUdid); 94 // Clean non-ohbase data when the peer is ohbase 95 void FixRemoteDataWhenPeerIsOHBase(const std::string& remoteUdid, 96 const std::map<std::string, std::string>& localDataByOwner); 97 void NotifyNotOHBaseOnline(const TrustedDeviceInfo& deviceInfo); 98 void E2ESyncDynamicProfile(const TrustedDeviceInfo& deviceInfo); 99 int32_t SaveBatchByKeys(const std::map<std::string, std::string>& entries); 100 void FixDiffProfiles(); 101 template <typename T> 102 int32_t IsMultiUserValid(const T& profile); 103 bool IsSameAccount(const std::string deviceId, const int32_t userId); 104 bool HasTrustP2PRelation(const std::string deviceId, const int32_t userId); 105 bool isAdapterSoLoaded_ = false; 106 std::mutex isAdapterLoadLock_; 107 std::mutex dynamicStoreMutex_; 108 std::shared_ptr<IKVAdapter> deviceProfileStore_ = nullptr; 109 std::shared_ptr<IDPSyncAdapter> dpSyncAdapter_; 110 std::atomic<bool> isFirst_{true}; 111 std::mutex putTempCacheMutex_; 112 std::map<std::string, std::string> putTempCache_; 113 }; 114 } // namespace DeviceProfile 115 } // namespace OHOS 116 #endif // OHOS_DP_DEVICE_PROFILE_MANAGER_H 117