• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "profile_data_manager.h"
17 
18 #include "device_profile_manager.h"
19 #include "device_icon_info_dao.h"
20 #include "distributed_device_profile_constants.h"
21 #include "distributed_device_profile_errors.h"
22 #include "distributed_device_profile_log.h"
23 #include "multi_user_manager.h"
24 #include "product_info_dao.h"
25 #include "profile_cache.h"
26 #include "profile_utils.h"
27 #include "content_sensor_manager_utils.h"
28 
29 namespace OHOS {
30 namespace DistributedDeviceProfile {
31 IMPLEMENT_SINGLE_INSTANCE(ProfileDataManager)
32 
33 namespace {
34 const std::string TAG = "ProfileDataManager";
35 }
36 
Init()37 int32_t ProfileDataManager::Init()
38 {
39     if (DeviceProfileDao::GetInstance().Init() != DP_SUCCESS) {
40         HILOGE("DeviceProfileDao init failed");
41         return DP_DEVICE_PROFILE_DAO_INIT_FAIL;
42     }
43     if (ProductInfoDao::GetInstance().Init() != DP_SUCCESS) {
44         HILOGE("ProductInfoDao init failed");
45         return DP_PRODUCT_INFO_DAO_INIT_FAIL;
46     }
47     if (DeviceIconInfoDao::GetInstance().Init() != DP_SUCCESS) {
48         HILOGE("DeviceIconInfoDao init failed");
49         return DP_DEVICE_ICON_INFO_DAO_INIT_FAIL;
50     }
51     return DP_SUCCESS;
52 }
53 
UnInit()54 int32_t ProfileDataManager::UnInit()
55 {
56     if (DeviceProfileDao::GetInstance().UnInit() != DP_SUCCESS) {
57         HILOGE("DeviceProfileDao unInit failed");
58         return DP_DEVICE_PROFILE_DAO_UNINIT_FAIL;
59     }
60     if (ProductInfoDao::GetInstance().UnInit() != DP_SUCCESS) {
61         HILOGE("ProductInfoDao unInit failed");
62         return DP_PRODUCT_INFO_DAO_UNINIT_FAIL;
63     }
64     if (DeviceIconInfoDao::GetInstance().UnInit() != DP_SUCCESS) {
65         HILOGE("DeviceIconInfoDao unInit failed");
66         return DP_DEVICE_ICON_INFO_DAO_UNINIT_FAIL;
67     }
68     return 0;
69 }
70 
PutDeviceProfile(DeviceProfile deviceProfile)71 int32_t ProfileDataManager::PutDeviceProfile(DeviceProfile deviceProfile)
72 {
73     if (deviceProfile.GetDeviceId().empty() || ProfileDataManager::FilterInvaildSymbol(deviceProfile.GetDeviceId())) {
74         HILOGE("deviceId invaild,deviceId=%{public}s",
75             ProfileUtils::GetAnonyString(deviceProfile.GetDeviceId()).c_str());
76         return DP_INVALID_PARAMS;
77     }
78     if (deviceProfile.GetUserId() == DEFAULT_USER_ID) {
79         deviceProfile.SetUserId(MultiUserManager::GetInstance().GetCurrentForegroundUserID());
80     }
81     DeviceProfileFilterOptions filterOptions;
82     filterOptions.AddDeviceIds(deviceProfile.GetDeviceId());
83     filterOptions.SetUserId(deviceProfile.GetUserId());
84     std::vector<DeviceProfile> oldDeviceProfiles;
85     int32_t ret = RET_INIT;
86     ret = DeviceProfileDao::GetInstance().GetDeviceProfiles(filterOptions, oldDeviceProfiles);
87     if ((ret != DP_SUCCESS) && (ret != DP_NOT_FIND_DATA)) {
88         HILOGE("GetDeviceProfiles failed,ret=%{public}d", ret);
89         return ret;
90     }
91     if (oldDeviceProfiles.empty()) {
92         HILOGI("oldDeviceProfiles is mepty");
93         deviceProfile.SetId(DEFAULT_ID);
94     } else {
95         DeviceProfile oldDeviceProfile = oldDeviceProfiles[0];
96         deviceProfile.SetId(oldDeviceProfile.GetId());
97     }
98     if (deviceProfile.GetId() <= 0) {
99         ret = DeviceProfileDao::GetInstance().PutDeviceProfile(deviceProfile);
100     } else {
101         ret = DeviceProfileDao::GetInstance().UpdateDeviceProfile(deviceProfile);
102     }
103     if (ret != DP_SUCCESS) {
104         HILOGE("PutDeviceProfile failed,ret=%{public}d", ret);
105         return ret;
106     }
107     return DP_SUCCESS;
108 }
109 
DeleteDeviceProfileBatch(std::vector<DeviceProfile> & deviceProfiles)110 int32_t ProfileDataManager::DeleteDeviceProfileBatch(std::vector<DeviceProfile>& deviceProfiles)
111 {
112     HILOGI("deviceProfiles.size:%{public}zu", deviceProfiles.size());
113     if (deviceProfiles.empty()) {
114         HILOGE("deviceProfiles is empty");
115         return DP_INVALID_PARAM;
116     }
117     DeviceProfileFilterOptions filterOptions;
118     filterOptions.SetUserId(deviceProfiles[0].GetUserId());
119     for (auto const &item : deviceProfiles) {
120         filterOptions.AddDeviceIds(item.GetDeviceId());
121     }
122     std::vector<DeviceProfile> oldDeviceProfiles;
123     int32_t ret = GetDeviceProfiles(filterOptions, oldDeviceProfiles);
124     if (ret != DP_SUCCESS) {
125         HILOGE("DeleteDeviceProfile failed,ret=%{public}d", ret);
126         return ret;
127     }
128     HILOGI("oldDeviceProfiles.size:%{public}zu", oldDeviceProfiles.size());
129     for (auto dp : oldDeviceProfiles) {
130         int32_t delRet = DeviceProfileDao::GetInstance().DeleteDeviceProfile(dp);
131         if (delRet != DP_SUCCESS) {
132             HILOGE("DeleteDeviceProfile failed, delRet=%{public}d, dp:%{public}s", delRet, dp.dump().c_str());
133             ret = delRet;
134         }
135     }
136     if (ret != DP_SUCCESS) {
137         HILOGE("DeleteDeviceProfile failed, ret=%{public}d", ret);
138         return ret;
139     }
140     return DP_SUCCESS;
141 }
142 
FilterInvaildSymbol(std::string str)143 bool ProfileDataManager::FilterInvaildSymbol(std::string str)
144 {
145     if (str.length() == 0 || str.length() > MAX_STRING_LEN) {
146         return false;
147     }
148     size_t found = str.find(SEPARATOR);
149     if (found == std::string::npos) {
150         return false;
151     }
152     size_t foundSlashes = str.find(SLASHES);
153     if (foundSlashes == std::string::npos) {
154         return false;
155     }
156     return true;
157 }
158 
PutDeviceProfileBatch(std::vector<DeviceProfile> & deviceProfiles)159 int32_t ProfileDataManager::PutDeviceProfileBatch(std::vector<DeviceProfile>& deviceProfiles)
160 {
161     if (deviceProfiles.empty()) {
162         HILOGE("deviceProfiles is empty");
163         return DP_INVALID_PARAM;
164     }
165     int32_t ret = RET_INIT;
166     for (auto deviceProfile : deviceProfiles) {
167         int32_t putRet = PutDeviceProfile(deviceProfile);
168         if (ret != DP_SUCCESS) {
169             ret = putRet;
170         }
171     }
172     if (ret != DP_SUCCESS) {
173         HILOGE("PutDeviceProfiles failed,ret=%{public}d", ret);
174         return ret;
175     }
176     return DP_SUCCESS;
177 }
178 
GetDeviceProfiles(DeviceProfileFilterOptions & options,std::vector<DeviceProfile> & deviceProfiles)179 int32_t ProfileDataManager::GetDeviceProfiles(DeviceProfileFilterOptions& options,
180     std::vector<DeviceProfile>& deviceProfiles)
181 {
182     if (options.GetUserId() == DEFAULT_USER_ID) {
183         options.SetUserId(MultiUserManager::GetInstance().GetCurrentForegroundUserID());
184     }
185     int32_t ret = DeviceProfileDao::GetInstance().GetDeviceProfiles(options, deviceProfiles);
186     if (ret != DP_SUCCESS) {
187         HILOGE("GetDeviceProfile failed,ret=%{public}d", ret);
188         return ret;
189     }
190     if (deviceProfiles.empty()) {
191         HILOGE("GetDeviceProfile failed,deviceProfiles is empty");
192         return DP_DEVICE_PROFILE_NOT_FOUND;
193     }
194     return DP_SUCCESS;
195 }
196 
GetDeviceIconInfos(const DeviceIconInfoFilterOptions & filterOptions,std::vector<DeviceIconInfo> & deviceIconInfos)197 int32_t ProfileDataManager::GetDeviceIconInfos(const DeviceIconInfoFilterOptions& filterOptions,
198     std::vector<DeviceIconInfo>& deviceIconInfos)
199 {
200     if (filterOptions.GetProductIds().empty() || filterOptions.GetImageType().empty() ||
201         filterOptions.GetSpecName().empty()) {
202         HILOGE("Invalid parameter");
203         return DP_INVALID_PARAM;
204     }
205     int32_t ret = DeviceIconInfoDao::GetInstance().GetDeviceIconInfos(filterOptions, deviceIconInfos);
206     if (ret != DP_SUCCESS) {
207         HILOGE("GetDeviceIconInfos failed,ret=%{public}d", ret);
208         return ret;
209     }
210     return DP_SUCCESS;
211 }
212 
PutDeviceIconInfoBatch(const std::vector<DeviceIconInfo> & deviceIconInfos)213 int32_t ProfileDataManager::PutDeviceIconInfoBatch(const std::vector<DeviceIconInfo>& deviceIconInfos)
214 {
215     if (deviceIconInfos.empty()) {
216         HILOGE("ProductInfos is empty");
217         return DP_INVALID_PARAM;
218     }
219     for (auto &deviceIconInfo : deviceIconInfos) {
220         int32_t ret = PutDeviceIconInfo(deviceIconInfo);
221         if (ret != DP_SUCCESS) {
222             HILOGE("PutDeviceIconInfo is failed,ret=%{public}d", ret);
223             return ret;
224         }
225     }
226     return DP_SUCCESS;
227 }
228 
PutDeviceIconInfo(const DeviceIconInfo & deviceIconInfo)229 int32_t ProfileDataManager::PutDeviceIconInfo(const DeviceIconInfo& deviceIconInfo)
230 {
231     DeviceIconInfoFilterOptions filterOptions;
232     std::vector<std::string> productIds;
233     productIds.emplace_back(deviceIconInfo.GetProductId());
234     filterOptions.SetProductIds(productIds);
235     filterOptions.SetSubProductId(deviceIconInfo.GetSubProductId());
236     filterOptions.SetImageType(deviceIconInfo.GetImageType());
237     filterOptions.SetSpecName(deviceIconInfo.GetSpecName());
238     std::vector<DeviceIconInfo> deviceIconInfos;
239     int32_t ret = DeviceIconInfoDao::GetInstance().GetDeviceIconInfos(filterOptions, deviceIconInfos);
240     if ((ret != DP_SUCCESS) && (ret != DP_NOT_FIND_DATA)) {
241         HILOGE("GetDeviceIconInfos failed,ret=%{public}d", ret);
242         return ret;
243     }
244     if (deviceIconInfos.empty()) {
245         ret = DeviceIconInfoDao::GetInstance().PutDeviceIconInfo(deviceIconInfo);
246     } else {
247         DeviceIconInfo updateDeviceIconInfo = deviceIconInfo;
248         updateDeviceIconInfo.SetId(deviceIconInfos[0].GetId());
249         ret = DeviceIconInfoDao::GetInstance().UpdateDeviceIconInfo(updateDeviceIconInfo);
250     }
251     if (ret != DP_SUCCESS) {
252         HILOGE("PutDeviceIconInfo failed,ret=%{public}d", ret);
253         return ret;
254     }
255     return DP_SUCCESS;
256 }
257 
PutProductInfoBatch(const std::vector<ProductInfo> & productInfos)258 int32_t ProfileDataManager::PutProductInfoBatch(const std::vector<ProductInfo>& productInfos)
259 {
260     if (productInfos.empty()) {
261         HILOGE("ProductInfos is empty");
262         return DP_INVALID_PARAM;
263     }
264     for (auto &productInfo : productInfos) {
265         int32_t ret = PutProductInfo(productInfo);
266         if (ret != DP_SUCCESS) {
267             HILOGE("PutProductInfo is failed,ret=%{public}d", ret);
268             return ret;
269         }
270     }
271     return DP_SUCCESS;
272 }
273 
PutProductInfo(const ProductInfo & productInfo)274 int32_t ProfileDataManager::PutProductInfo(const ProductInfo& productInfo)
275 {
276     if (productInfo.GetProductId().empty()) {
277         HILOGE("ProductId is nullptr");
278         return DP_INVALID_PARAM;
279     }
280     std::vector<std::string> productIds;
281     productIds.emplace_back(productInfo.GetProductId());
282     std::vector<ProductInfo> productInfos;
283     int32_t ret = ProductInfoDao::GetInstance().GetProductInfos(productIds, productInfos);
284     if ((ret != DP_SUCCESS) && (ret != DP_NOT_FIND_DATA)) {
285         HILOGE("GetProductInfos failed,ret=%{public}d", ret);
286         return ret;
287     }
288     if (productInfos.empty()) {
289         ret = ProductInfoDao::GetInstance().PutProductInfo(productInfo);
290     } else {
291         ret = ProductInfoDao::GetInstance().UpdateProductInfo(productInfo);
292     }
293     if (ret != DP_SUCCESS) {
294         HILOGE("PutProductInfo failed,ret=%{public}d", ret);
295         return ret;
296     }
297     return DP_SUCCESS;
298 }
299 } // namespace DistributedDeviceProfile
300 } // namespace OHOS
301