• 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 #define LOG_TAG "DataParamsConversion"
16 #include "data_params_conversion.h"
17 #include "logger.h"
18 #include "ndk_data_conversion.h"
19 #include "udmf_client.h"
20 #include "udmf_utils.h"
21 
22 namespace OHOS::UDMF {
GetInnerDataParams(OH_UdmfGetDataParams & ndkDataParams,QueryOption & query,GetDataParams & dataParams)23 Status DataParamsConversion::GetInnerDataParams(OH_UdmfGetDataParams &ndkDataParams, QueryOption &query,
24     GetDataParams &dataParams)
25 {
26     if (ndkDataParams.dataProgressListener == nullptr || query.key.size() == 0) {
27         return Status::E_INVALID_PARAMETERS;
28     }
29     dataParams.query = query;
30     if (ndkDataParams.destUri.size() > 0) {
31         dataParams.destUri = ndkDataParams.destUri;
32     }
33     dataParams.fileConflictOptions = static_cast<FileConflictOptions>(ndkDataParams.fileConflictOptions);
34     dataParams.progressIndicator = static_cast<ProgressIndicator>(ndkDataParams.progressIndicator);
35     dataParams.progressListener = [ndkDataParams](ProgressInfo progressInfo, std::shared_ptr<UnifiedData> data) {
36         OH_Udmf_ProgressInfo ndkProgrssInfo;
37         ndkProgrssInfo.progress = progressInfo.progress;
38         ndkProgrssInfo.status = progressInfo.progressStatus;
39         if (data == nullptr) {
40             ndkDataParams.dataProgressListener(&ndkProgrssInfo, nullptr);
41             return;
42         }
43         OH_UdmfData *ndkData = OH_UdmfData_Create();
44         NdkDataConversion::GetNdkUnifiedData(data, ndkData);
45         ndkDataParams.dataProgressListener(&ndkProgrssInfo, ndkData);
46         OH_UdmfData_Destroy(ndkData);
47     };
48     std::set<std::string> types;
49     for (size_t i = 0; i < ndkDataParams.acceptableInfo.typesCount; i++) {
50         types.insert(ndkDataParams.acceptableInfo.typesArray[i]);
51     }
52     dataParams.acceptableInfo = {
53         .types = std::move(types),
54         .recordCount = ndkDataParams.acceptableInfo.recordsCount,
55     };
56     return Status::E_OK;
57 }
58 
GetDataLoaderParams(const OH_UdmfDataLoadParams & ndkDataParams,DataLoadParams & dataLoadParams)59 Status DataParamsConversion::GetDataLoaderParams(const OH_UdmfDataLoadParams &ndkDataParams,
60     DataLoadParams &dataLoadParams)
61 {
62     if (ndkDataParams.dataLoadHandler == nullptr) {
63         LOG_ERROR(UDMF_CAPI, "DataLoadHandler is null");
64         return Status::E_INVALID_PARAMETERS;
65     }
66     std::set<std::string> types;
67     for (size_t i = 0; i < ndkDataParams.dataLoadInfo.typesCount; i++) {
68         types.insert(ndkDataParams.dataLoadInfo.typesArray[i]);
69     }
70     dataLoadParams.dataLoadInfo = {
71         .sequenceKey = UTILS::GenerateId(),
72         .types = std::move(types),
73         .recordCount = ndkDataParams.dataLoadInfo.recordsCount,
74     };
75     dataLoadParams.loadHandler = [ndkDataParams](const std::string &udKey, const DataLoadInfo &dataLoadInfo) {
76         std::vector<std::string> types(dataLoadInfo.types.begin(), dataLoadInfo.types.end());
77         OH_UdmfDataLoadInfo ndkDataLoadInfo {
78             .typesArray = NdkDataConversion::StrVectorToTypesArray(types),
79             .typesCount = types.size(),
80             .recordsCount = dataLoadInfo.recordCount,
81         };
82         OH_UdmfData *data = ndkDataParams.dataLoadHandler(&ndkDataLoadInfo);
83         if (data == nullptr || data->unifiedData_ == nullptr) {
84             LOG_ERROR(UDMF_CAPI, "Data is null");
85             return;
86         }
87         auto status = UdmfClient::GetInstance().PushDelayData(udKey, *data->unifiedData_);
88         if (status != Status::E_OK) {
89             LOG_ERROR(UDMF_CAPI, "Set delay data failed, status = %{public}d", status);
90         }
91     };
92     return Status::E_OK;
93 }
94 }
95