• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_create_error(env, CreateJsValue(env, errCode), CreateJsValue(env, message), &result);
24     return result;
25 }
26 
BindNativeFunction(napi_env env,napi_value object,const char * name,const char * moduleName,napi_callback func)27 void BindNativeFunction(napi_env env, napi_value object, const char* name, const char* moduleName, napi_callback func)
28 {
29     std::string fullName;
30     if (moduleName) {
31         fullName = moduleName;
32         fullName += '.';
33     }
34     fullName += name;
35     napi_value funcValue = nullptr;
36     napi_create_function(env, fullName.c_str(), fullName.size(), func, nullptr, &funcValue);
37     napi_set_named_property(env, object, fullName.c_str(), funcValue);
38 }
39 
CheckParamMinimumValid(napi_env env,const size_t paramNum,const size_t minNum)40 bool CheckParamMinimumValid(napi_env env, const size_t paramNum, const size_t minNum)
41 {
42     if (paramNum <= minNum) {
43         CMLOGE("[NAPI]Argc is invalid: %{public}zu", paramNum);
44         std::string errMsg = "Parameter check fails. The number of parameter(s) must not be less than " +
45             std::to_string(minNum);
46         napi_throw(env,
47             CreateJsError(env, static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_INVALID_PARAM)),
48             errMsg));
49         return false;
50     }
51     return true;
52 }
53 
ColorSpaceTypeInit(napi_env env)54 napi_value ColorSpaceTypeInit(napi_env env)
55 {
56     if (env == nullptr) {
57         CMLOGE("[NAPI]Engine is nullptr");
58         return nullptr;
59     }
60     napi_value object = nullptr;
61     napi_create_object(env, &object);
62     if (object == nullptr) {
63         CMLOGE("[NAPI]Failed to get object");
64         return nullptr;
65     }
66 
67     for (auto& [colorSpaceName, colorSpace] : STRING_TO_JS_MAP) {
68         napi_value value = CreateJsValue(env, static_cast<int32_t>(colorSpace));
69         napi_set_named_property(env, object, colorSpaceName.c_str(), value);
70     }
71     return object;
72 }
73 
CMErrorInit(napi_env env)74 napi_value CMErrorInit(napi_env env)
75 {
76     if (env == nullptr) {
77         CMLOGE("[NAPI]Engine is nullptr");
78         return nullptr;
79     }
80 
81     napi_value object = nullptr;
82     napi_create_object(env, &object);
83     if (object == nullptr) {
84         CMLOGE("[NAPI]Failed to get object");
85         return nullptr;
86     }
87 
88     napi_value valueErrorNullptr = CreateJsValue(env, static_cast<int32_t>(CMError::CM_ERROR_NULLPTR));
89     napi_value valueErrorInvalidPara = CreateJsValue(env, static_cast<int32_t>(CMError::CM_ERROR_INVALID_PARAM));
90     napi_value valueErrorInvalidEnum = CreateJsValue(env, static_cast<int32_t>(CMError::CM_ERROR_INVALID_ENUM_USAGE));
91     napi_set_named_property(env, object, "CM_ERROR_NULLPTR", valueErrorNullptr);
92     napi_set_named_property(env, object, "CM_ERROR_INVALID_PARAM", valueErrorInvalidPara);
93     napi_set_named_property(env, object, "CM_ERROR_INVALID_ENUM_USAGE", valueErrorInvalidEnum);
94     return object;
95 }
96 
CMErrorCodeInit(napi_env env)97 napi_value CMErrorCodeInit(napi_env env)
98 {
99     if (env == nullptr) {
100         CMLOGE("[NAPI]Engine is nullptr");
101         return nullptr;
102     }
103 
104     napi_value object = nullptr;
105     napi_create_object(env, &object);
106     if (object == nullptr) {
107         CMLOGE("[NAPI]Failed to get object");
108         return nullptr;
109     }
110 
111     napi_value valueErrorNoPermission = CreateJsValue(env, static_cast<int32_t>(CMErrorCode::CM_ERROR_NO_PERMISSION));
112     napi_value valueErrorInvalidPara = CreateJsValue(env, static_cast<int32_t>(CMErrorCode::CM_ERROR_INVALID_PARAM));
113     napi_value valueErrorDevNotSupport = CreateJsValue(env,
114         static_cast<int32_t>(CMErrorCode::CM_ERROR_DEVICE_NOT_SUPPORT));
115     napi_value valueErrorAbnormalpara = CreateJsValue(env,
116         static_cast<int32_t>(CMErrorCode::CM_ERROR_ABNORMAL_PARAM_VALUE));
117     napi_set_named_property(env, object, "CM_ERROR_NO_PERMISSION", valueErrorNoPermission);
118     napi_set_named_property(env, object, "CM_ERROR_INVALID_PARAM", valueErrorInvalidPara);
119     napi_set_named_property(env, object, "CM_ERROR_DEVICE_NOT_SUPPORT", valueErrorDevNotSupport);
120     napi_set_named_property(env, object, "CM_ERROR_ABNORMAL_PARAM_VALUE", valueErrorAbnormalpara);
121     return object;
122 }
123 
ParseJsDoubleValue(napi_value jsObject,napi_env env,const std::string & name,double & data)124 bool ParseJsDoubleValue(napi_value jsObject, napi_env env, const std::string& name, double& data)
125 {
126     napi_value value = nullptr;
127     napi_get_named_property(env, jsObject, name.c_str(), &value);
128     napi_valuetype valueType = napi_undefined;
129     napi_typeof(env, value, &valueType);
130     if (valueType != napi_undefined) {
131         if (napi_get_value_double(env, value, &data) != napi_ok) {
132             CMLOGE("[NAPI]Failed to convert parameter to data: %{public}s", name.c_str());
133             return false;
134         }
135     } else {
136         CMLOGI("[NAPI]no property with: %{public}s", name.c_str());
137         return false;
138     }
139     return true;
140 }
141 }  // namespace ColorManager
142 }  // namespace OHOS
143