• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "scan_option_descriptor_helper.h"
17 #include "scan_range_helper.h"
18 #include "napi_scan_utils.h"
19 #include "scan_log.h"
20 
21 namespace OHOS::Scan {
22 static constexpr const char *PARAM_OPTION_NAME = "optionName";
23 static constexpr const char *PARAM_OPTION_INDEX = "optionIndex";
24 static constexpr const char *PARAM_OPTION_TITLE = "optionTitle";
25 static constexpr const char *PARAM_OPTION_DESC = "optionDesc";
26 static constexpr const char *PARAM_OPTION_TYPE = "optionType";
27 static constexpr const char *PARAM_OPTION_UNIT = "optionUnit";
28 static constexpr const char *PARAM_OPTION_CONSTRAINT_TYPE = "optionConstraintType";
29 static constexpr const char *PARAM_OPTION_CONSTRAINT_STRING = "optionConstraintString";
30 static constexpr const char *PARAM_OPTION_CONSTRAINT_NUMBER = "optionConstraintInt";
31 static constexpr const char *PARAM_OPTION_CONSTRAINT_RANGE = "optionConstraintRange";
32 
MakeJsObject(napi_env env,const ScanOptionDescriptor & desc)33 napi_value ScanOptionDescriptorHelper::MakeJsObject(napi_env env, const ScanOptionDescriptor &desc)
34 {
35     napi_value jsObj = nullptr;
36 
37     if (napi_create_object(env, &jsObj) != napi_ok) {
38         SCAN_HILOGE("Failed to create JavaScript object");
39         return nullptr;
40     }
41     NapiScanUtils::SetStringPropertyUtf8(env, jsObj, PARAM_OPTION_NAME, desc.GetOptionName());
42     NapiScanUtils::SetUint32Property(env, jsObj, PARAM_OPTION_INDEX, desc.GetOptionIndex());
43     NapiScanUtils::SetStringPropertyUtf8(env, jsObj, PARAM_OPTION_TITLE, desc.GetOptionTitle());
44     NapiScanUtils::SetStringPropertyUtf8(env, jsObj, PARAM_OPTION_DESC, desc.GetOptionDesc());
45     NapiScanUtils::SetUint32Property(env, jsObj, PARAM_OPTION_TYPE, desc.GetOptionType());
46     NapiScanUtils::SetUint32Property(env, jsObj, PARAM_OPTION_UNIT, desc.GetOptionUnit());
47     NapiScanUtils::SetUint32Property(env, jsObj, PARAM_OPTION_CONSTRAINT_TYPE, desc.GetOptionConstraintType());
48     if (desc.GetOptionConstraintType() == SCAN_CONSTRAINT_STRING_LIST) {
49         std::vector<std::string> optionConstraintString;
50         desc.GetOptionConstraintString(optionConstraintString);
51         napi_value arrOptionConstraintString = nullptr;
52         SCAN_CALL(env, napi_create_array(env, &arrOptionConstraintString));
53         uint32_t arrOptionConstraintStringLength = optionConstraintString.size();
54         for (uint32_t i = 0; i < arrOptionConstraintStringLength; i++) {
55             napi_value value = NapiScanUtils::CreateStringUtf8(env, optionConstraintString[i]);
56             SCAN_CALL(env, napi_set_element(env, arrOptionConstraintString, i, value));
57         }
58         SCAN_CALL(env, napi_set_named_property(env, jsObj, PARAM_OPTION_CONSTRAINT_STRING, arrOptionConstraintString));
59     } else if (desc.GetOptionConstraintType() == SCAN_CONSTRAINT_WORD_LIST) {
60         std::vector<int32_t> optionConstraintNumber;
61         desc.GetOptionConstraintNumber(optionConstraintNumber);
62         napi_value arrOptionConstraintNumber = nullptr;
63         SCAN_CALL(env, napi_create_array(env, &arrOptionConstraintNumber));
64         uint32_t arrOptionConstraintNumberLength = optionConstraintNumber.size();
65         for (uint32_t i = 0; i < arrOptionConstraintNumberLength; i++) {
66             napi_value value;
67             SCAN_CALL(env, napi_create_int32(env, optionConstraintNumber[i], &value));
68             SCAN_CALL(env, napi_set_element(env, arrOptionConstraintNumber, i, value));
69         }
70         SCAN_CALL(env, napi_set_named_property(env, jsObj, PARAM_OPTION_CONSTRAINT_NUMBER, arrOptionConstraintNumber));
71     } else if (desc.GetOptionConstraintType() == SCAN_CONSTRAINT_RANGE) {
72         ScanRange range;
73         desc.GetOptionConstraintRange(range);
74         SCAN_CALL(env, napi_set_named_property(env, jsObj, PARAM_OPTION_CONSTRAINT_RANGE,
75             ScanRangeHelper::MakeJsObject(env, range)));
76     } else {
77         SCAN_HILOGD("type (%{public}u) does not meet the format requirements", desc.GetOptionConstraintType());
78     }
79     return jsObj;
80 }
81 
GetValueFromJs(napi_env env,napi_value jsValue,std::shared_ptr<ScanOptionDescriptor> & nativeObj)82 napi_value ScanOptionDescriptorHelper::GetValueFromJs(napi_env env, napi_value jsValue,
83                                                       std::shared_ptr<ScanOptionDescriptor> &nativeObj)
84 {
85     std::string optionName = NapiScanUtils::GetStringPropertyUtf8(env, jsValue, PARAM_OPTION_NAME);
86     nativeObj->SetOptionName(optionName);
87 
88     uint32_t optionIndex = NapiScanUtils::GetUint32Property(env, jsValue, PARAM_OPTION_INDEX);
89     nativeObj->SetOptionIndex(optionIndex);
90 
91     std::string optionTitle = NapiScanUtils::GetStringPropertyUtf8(env, jsValue, PARAM_OPTION_TITLE);
92     nativeObj->SetOptionTitle(optionTitle);
93 
94     std::string optionDesc = NapiScanUtils::GetStringPropertyUtf8(env, jsValue, PARAM_OPTION_DESC);
95     nativeObj->SetOptionDesc(optionDesc);
96 
97     uint32_t optionType = NapiScanUtils::GetUint32Property(env, jsValue, PARAM_OPTION_TYPE);
98     nativeObj->SetOptionType(optionType);
99 
100     uint32_t optionUnit = NapiScanUtils::GetUint32Property(env, jsValue, PARAM_OPTION_UNIT);
101     nativeObj->SetOptionUnit(optionUnit);
102 
103     uint32_t optionConstraintType = NapiScanUtils::GetUint32Property(env, jsValue, PARAM_OPTION_CONSTRAINT_TYPE);
104     nativeObj->SetOptionConstraintType(optionConstraintType);
105     return nullptr;
106 }
107 
ObjSetOptionConstraintString(napi_env env,napi_value jsValue,std::shared_ptr<ScanOptionDescriptor> & nativeObj)108 napi_value ScanOptionDescriptorHelper::ObjSetOptionConstraintString(napi_env env, napi_value jsValue,
109                                                                     std::shared_ptr<ScanOptionDescriptor> &nativeObj)
110 {
111     napi_value jsOptionConstraintString = NapiScanUtils::GetNamedProperty(env, jsValue, PARAM_OPTION_CONSTRAINT_STRING);
112     bool isArray = false;
113     if (jsOptionConstraintString != nullptr) {
114         std::vector<std::string> optionConstraintString;
115         SCAN_CALL(env, napi_is_array(env, jsOptionConstraintString, &isArray));
116         if (!isArray) {
117             SCAN_HILOGE("Invalid list of option constraint string");
118             return nullptr;
119         }
120         uint32_t arrayLength = 0;
121         SCAN_CALL(env, napi_get_array_length(env, jsOptionConstraintString, &arrayLength));
122         for (uint32_t index = 0; index < arrayLength; index++) {
123             napi_value jsConstraintString;
124             std::string constraintString;
125             SCAN_CALL(env, napi_get_element(env, jsOptionConstraintString, index, &jsConstraintString));
126             constraintString = NapiScanUtils::GetValueString(env, jsConstraintString);
127             optionConstraintString.emplace_back(constraintString);
128         }
129         nativeObj->SetOptionConstraintString(optionConstraintString);
130     }
131     return nullptr;
132 }
133 
ObjSetOptionConstraintNumber(napi_env env,napi_value jsValue,std::shared_ptr<ScanOptionDescriptor> & nativeObj)134 napi_value ScanOptionDescriptorHelper::ObjSetOptionConstraintNumber(napi_env env, napi_value jsValue,
135                                                                     std::shared_ptr<ScanOptionDescriptor> &nativeObj)
136 {
137     napi_value jsOptionConstraintNumber = NapiScanUtils::GetNamedProperty(env, jsValue, PARAM_OPTION_CONSTRAINT_NUMBER);
138     bool isArray = false;
139     if (jsOptionConstraintNumber != nullptr) {
140         std::vector<int32_t> optionConstraintNumber;
141         SCAN_CALL(env, napi_is_array(env, jsOptionConstraintNumber, &isArray));
142         if (!isArray) {
143             SCAN_HILOGE("Invalid list of option constraint number");
144             return nullptr;
145         }
146         uint32_t arrayLength = 0;
147         SCAN_CALL(env, napi_get_array_length(env, jsOptionConstraintNumber, &arrayLength));
148         for (uint32_t index = 0; index < arrayLength; index++) {
149             napi_value jsConstraintNumber;
150             int32_t constraintNumber;
151             SCAN_CALL(env, napi_get_element(env, jsOptionConstraintNumber, index, &jsConstraintNumber));
152             SCAN_CALL(env, napi_get_value_int32(env, jsConstraintNumber, &constraintNumber));
153             optionConstraintNumber.emplace_back(constraintNumber);
154         }
155         nativeObj->SetOptionConstraintNumber(optionConstraintNumber);
156     }
157     return nullptr;
158 }
159 
BuildFromJs(napi_env env,napi_value jsValue)160 std::shared_ptr<ScanOptionDescriptor> ScanOptionDescriptorHelper::BuildFromJs(napi_env env, napi_value jsValue)
161 {
162     auto nativeObj = std::make_shared<ScanOptionDescriptor>();
163 
164     auto names = NapiScanUtils::GetPropertyNames(env, jsValue);
165     for (const auto& name : names) {
166         SCAN_HILOGD("Property: %{public}s", name.c_str());
167     }
168 
169     GetValueFromJs(env, jsValue, nativeObj);
170     ObjSetOptionConstraintString(env, jsValue, nativeObj);
171     ObjSetOptionConstraintNumber(env, jsValue, nativeObj);
172     return nativeObj;
173 }
174 }  // namespace OHOS::Scan
175