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_range_helper.h"
17 #include "napi_scan_utils.h"
18 #include "scan_constant.h"
19 #include "scan_log.h"
20 
21 namespace OHOS::Scan {
22 static constexpr const char *PARAM_RANGE_MINVALUE = "minValue";
23 static constexpr const char *PARAM_RANGE_MAXVALUE = "maxValue";
24 static constexpr const char *PARAM_RANGE_QUANTVALUE = "quantValue";
25 
MakeJsObject(napi_env env,const ScanRange & range)26 napi_value ScanRangeHelper::MakeJsObject(napi_env env, const ScanRange &range)
27 {
28     napi_value jsObj = nullptr;
29     SCAN_CALL(env, napi_create_object(env, &jsObj));
30 
31     NapiScanUtils::SetInt32Property(env, jsObj, PARAM_RANGE_MINVALUE, range.GetMinValue());
32     NapiScanUtils::SetInt32Property(env, jsObj, PARAM_RANGE_MAXVALUE, range.GetMaxValue());
33     NapiScanUtils::SetInt32Property(env, jsObj, PARAM_RANGE_QUANTVALUE, range.GetQuantValue());
34 
35     return jsObj;
36 }
37 
BuildFromJs(napi_env env,napi_value jsValue)38 std::shared_ptr<ScanRange> ScanRangeHelper::BuildFromJs(napi_env env, napi_value jsValue)
39 {
40     auto nativeObj = std::make_shared<ScanRange>();
41 
42     if (!ValidateProperty(env, jsValue)) {
43         SCAN_HILOGE("Invalid property of scan range");
44         return nullptr;
45     }
46 
47     auto jsMinValue = NapiScanUtils::GetNamedProperty(env, jsValue, PARAM_RANGE_MINVALUE);
48     if (jsMinValue != nullptr) {
49         nativeObj->SetMinValue(NapiScanUtils::GetInt32FromValue(env, jsMinValue));
50     }
51 
52     auto jsMaxValue = NapiScanUtils::GetNamedProperty(env, jsValue, PARAM_RANGE_MAXVALUE);
53     if (jsMaxValue != nullptr) {
54         nativeObj->SetMaxValue(NapiScanUtils::GetInt32FromValue(env, jsMaxValue));
55     }
56 
57     auto jsQuantValue = NapiScanUtils::GetNamedProperty(env, jsValue, PARAM_RANGE_QUANTVALUE);
58     if (jsQuantValue != nullptr) {
59         nativeObj->SetQuantValue(NapiScanUtils::GetInt32FromValue(env, jsQuantValue));
60     }
61 
62     SCAN_HILOGE("Build Scan Range succeed");
63     return nativeObj;
64 }
65 
ValidateProperty(napi_env env,napi_value object)66 bool ScanRangeHelper::ValidateProperty(napi_env env, napi_value object)
67 {
68     std::map<std::string, ScanParamStatus> propertyList = {
69         {PARAM_RANGE_MINVALUE, SCAN_PARAM_OPT},
70         {PARAM_RANGE_MAXVALUE, SCAN_PARAM_OPT},
71         {PARAM_RANGE_QUANTVALUE, SCAN_PARAM_OPT},
72     };
73 
74     auto names = NapiScanUtils::GetPropertyNames(env, object);
75     for (auto name : names) {
76         if (propertyList.find(name) == propertyList.end()) {
77             SCAN_HILOGE("Invalid property: %{public}s", name.c_str());
78             return false;
79         }
80         propertyList[name] = SCAN_PARAM_SET;
81     }
82     bool hasMinValue = propertyList[PARAM_RANGE_MINVALUE] == SCAN_PARAM_SET;
83     bool hasMaxValue = propertyList[PARAM_RANGE_MAXVALUE] == SCAN_PARAM_SET;
84     bool hasQuantValue = propertyList[PARAM_RANGE_QUANTVALUE] == SCAN_PARAM_SET;
85     if (!hasMinValue || !hasMaxValue || !hasQuantValue) {
86         return false;
87     }
88     return true;
89 }
90 } // namespace OHOS::Scan
91