1 /*
2  * Copyright (c) 2022-2023 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 "js_color_space_utils.h"
17 
18 namespace OHOS {
19 namespace ColorManager {
CreateJsError(napi_env env,int32_t errCode,const std::string & message)20 napi_value CreateJsError(napi_env env, int32_t errCode, const std::string& message)
21 {
22     napi_value result = nullptr;
23     NAPI_CALL_DEFAULT(napi_create_error(env, CreateJsValue(env, errCode),
24         CreateJsValue(env, message), &result));
25     return result;
26 }
27 
BindNativeFunction(napi_env env,napi_value object,const char * name,const char * moduleName,napi_callback func)28 napi_status BindNativeFunction(napi_env env, napi_value object, const char* name,
29     const char* moduleName, napi_callback func)
30 {
31     std::string fullName;
32     if (moduleName) {
33         fullName = moduleName;
34         fullName += '.';
35     }
36     fullName += name;
37     napi_value funcValue = nullptr;
38     napi_status status = napi_ok;
39     status = napi_create_function(env, fullName.c_str(), fullName.size(), func, nullptr, &funcValue);
40     if (status != napi_ok) {
41         return status;
42     }
43     status = napi_set_named_property(env, object, fullName.c_str(), funcValue);
44     if (status != napi_ok) {
45         return status;
46     }
47     return status;
48 }
49 
CheckParamMinimumValid(napi_env env,const size_t paramNum,const size_t minNum)50 bool CheckParamMinimumValid(napi_env env, const size_t paramNum, const size_t minNum)
51 {
52     if (paramNum <= minNum) {
53         CMLOGE("[NAPI]Argc is invalid: %{public}zu", paramNum);
54         std::string errMsg = "BusinessError 401: Parameter error, the type of paramNum must be larger than" +
55             std::to_string(minNum);
56         napi_throw(env,
57             CreateJsError(env, static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_INVALID_PARAM)),
58             errMsg));
59         return false;
60     }
61     return true;
62 }
63 
ColorSpaceTypeInit(napi_env env)64 napi_value ColorSpaceTypeInit(napi_env env)
65 {
66     if (env == nullptr) {
67         CMLOGE("[NAPI]Engine is nullptr");
68         return nullptr;
69     }
70     napi_value object = nullptr;
71     NAPI_CALL_DEFAULT(napi_create_object(env, &object));
72     if (object == nullptr) {
73         CMLOGE("[NAPI]Failed to get object");
74         return nullptr;
75     }
76 
77     for (auto& [colorSpaceName, colorSpace] : STRING_TO_JS_MAP) {
78         napi_value value = CreateJsValue(env, static_cast<int32_t>(colorSpace));
79         NAPI_CALL_DEFAULT(napi_set_named_property(env, object, colorSpaceName.c_str(), value));
80     }
81     return object;
82 }
83 
CMErrorInit(napi_env env)84 napi_value CMErrorInit(napi_env env)
85 {
86     if (env == nullptr) {
87         CMLOGE("[NAPI]Engine is nullptr");
88         return nullptr;
89     }
90 
91     napi_value object = nullptr;
92     NAPI_CALL_DEFAULT(napi_create_object(env, &object));
93     if (object == nullptr) {
94         CMLOGE("[NAPI]Failed to get object");
95         return nullptr;
96     }
97 
98     napi_value valueErrorNullptr = CreateJsValue(env, static_cast<int32_t>(CMError::CM_ERROR_NULLPTR));
99     napi_value valueErrorInvalidPara = CreateJsValue(env, static_cast<int32_t>(CMError::CM_ERROR_INVALID_PARAM));
100     napi_value valueErrorInvalidEnum = CreateJsValue(env, static_cast<int32_t>(CMError::CM_ERROR_INVALID_ENUM_USAGE));
101     NAPI_CALL_DEFAULT(napi_set_named_property(env, object, "CM_ERROR_NULLPTR", valueErrorNullptr));
102     NAPI_CALL_DEFAULT(
103         napi_set_named_property(env, object, "CM_ERROR_INVALID_PARAM", valueErrorInvalidPara));
104     NAPI_CALL_DEFAULT(
105         napi_set_named_property(env, object, "CM_ERROR_INVALID_ENUM_USAGE", valueErrorInvalidEnum));
106     return object;
107 }
108 
CMErrorCodeInit(napi_env env)109 napi_value CMErrorCodeInit(napi_env env)
110 {
111     if (env == nullptr) {
112         CMLOGE("[NAPI]Engine is nullptr");
113         return nullptr;
114     }
115 
116     napi_value object = nullptr;
117     NAPI_CALL_DEFAULT(napi_create_object(env, &object));
118     if (object == nullptr) {
119         CMLOGE("[NAPI]Failed to get object");
120         return nullptr;
121     }
122 
123     napi_value valueErrorNoPermission = CreateJsValue(env, static_cast<int32_t>(CMErrorCode::CM_ERROR_NO_PERMISSION));
124     napi_value valueErrorInvalidPara = CreateJsValue(env, static_cast<int32_t>(CMErrorCode::CM_ERROR_INVALID_PARAM));
125     napi_value valueErrorDevNotSupport = CreateJsValue(env,
126         static_cast<int32_t>(CMErrorCode::CM_ERROR_DEVICE_NOT_SUPPORT));
127     napi_value valueErrorAbnormalpara = CreateJsValue(env,
128         static_cast<int32_t>(CMErrorCode::CM_ERROR_ABNORMAL_PARAM_VALUE));
129     NAPI_CALL_DEFAULT(
130         napi_set_named_property(env, object, "CM_ERROR_NO_PERMISSION", valueErrorNoPermission));
131     NAPI_CALL_DEFAULT(
132         napi_set_named_property(env, object, "CM_ERROR_INVALID_PARAM", valueErrorInvalidPara));
133     NAPI_CALL_DEFAULT(
134         napi_set_named_property(env, object, "CM_ERROR_DEVICE_NOT_SUPPORT", valueErrorDevNotSupport));
135     NAPI_CALL_DEFAULT(
136         napi_set_named_property(env, object, "CM_ERROR_ABNORMAL_PARAM_VALUE", valueErrorAbnormalpara));
137     return object;
138 }
139 
ParseJsDoubleValue(napi_value jsObject,napi_env env,const std::string & name,double & data)140 bool ParseJsDoubleValue(napi_value jsObject, napi_env env, const std::string& name, double& data)
141 {
142     napi_value value = nullptr;
143     if (napi_get_named_property(env, jsObject, name.c_str(), &value) != napi_ok) {
144         CMLOGE("[NAPI]Failed to get property: %{public}s", name.c_str());
145         return false;
146     };
147     napi_valuetype valueType = napi_undefined;
148     if (napi_typeof(env, value, &valueType) != napi_ok) {
149         CMLOGE("[NAPI]data type is invalied: %{public}s", name.c_str());
150         return false;
151     };
152     if (valueType != napi_undefined) {
153         if (napi_get_value_double(env, value, &data) != napi_ok) {
154             CMLOGE("[NAPI]Failed to convert parameter to data: %{public}s", name.c_str());
155             return false;
156         }
157     } else {
158         CMLOGI("[NAPI]no property with: %{public}s", name.c_str());
159         return false;
160     }
161     return true;
162 }
163 }  // namespace ColorManager
164 }  // namespace OHOS
165