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 "js_color_space_utils.h"
17
18 namespace OHOS {
19 namespace ColorManager {
CreateJsError(NativeEngine & engine,int32_t errCode,const std::string & message)20 NativeValue* CreateJsError(NativeEngine& engine, int32_t errCode, const std::string& message)
21 {
22 return engine.CreateError(CreateJsValue(engine, errCode), CreateJsValue(engine, message));
23 }
24
BindNativeFunction(NativeEngine & engine,NativeObject & object,const char * name,const char * moduleName,NativeCallback func)25 void BindNativeFunction(NativeEngine& engine, NativeObject& object, const char* name,
26 const char* moduleName, NativeCallback func)
27 {
28 std::string fullName(moduleName);
29 fullName += ".";
30 fullName += name;
31 object.SetProperty(name, engine.CreateFunction(fullName.c_str(), fullName.length(), func, nullptr));
32 }
33
CheckParamMinimumValid(NativeEngine & engine,const size_t paramNum,const size_t minNum)34 bool CheckParamMinimumValid(NativeEngine& engine, const size_t paramNum, const size_t minNum)
35 {
36 if (paramNum <= minNum) {
37 CMLOGE("[NAPI]Argc is invalid: %{public}zu", paramNum);
38 engine.Throw(CreateJsError(engine,
39 static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_INVALID_PARAM)),
40 "Parameter check fails. The number of parameter(s) must not be less than " + std::to_string(minNum)));
41 return false;
42 }
43 return true;
44 }
45
ColorSpaceTypeInit(NativeEngine * engine)46 NativeValue* ColorSpaceTypeInit(NativeEngine* engine)
47 {
48 if (engine == nullptr) {
49 CMLOGE("[NAPI]Engine is nullptr");
50 return nullptr;
51 }
52
53 NativeValue *objValue = engine->CreateObject();
54 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
55 if (object == nullptr) {
56 CMLOGE("[NAPI]Failed to get object");
57 return nullptr;
58 }
59
60 object->SetProperty("UNKNOWN", CreateJsValue(*engine, static_cast<int32_t>(ApiColorSpaceType::UNKNOWN)));
61 object->SetProperty("ADOBE_RGB_1998",
62 CreateJsValue(*engine, static_cast<int32_t>(ApiColorSpaceType::ADOBE_RGB_1998)));
63 object->SetProperty("DCI_P3", CreateJsValue(*engine, static_cast<int32_t>(ApiColorSpaceType::DCI_P3)));
64 object->SetProperty("DISPLAY_P3", CreateJsValue(*engine, static_cast<int32_t>(ApiColorSpaceType::DISPLAY_P3)));
65 object->SetProperty("SRGB", CreateJsValue(*engine, static_cast<int32_t>(ApiColorSpaceType::SRGB)));
66 object->SetProperty("CUSTOM", CreateJsValue(*engine, static_cast<int32_t>(ApiColorSpaceType::CUSTOM)));
67 return objValue;
68 }
69
CMErrorInit(NativeEngine * engine)70 NativeValue* CMErrorInit(NativeEngine* engine)
71 {
72 if (engine == nullptr) {
73 CMLOGE("[NAPI]Engine is nullptr");
74 return nullptr;
75 }
76
77 NativeValue *objValue = engine->CreateObject();
78 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
79 if (object == nullptr) {
80 CMLOGE("[NAPI]Failed to get object");
81 return nullptr;
82 }
83
84 object->SetProperty("CM_ERROR_NULLPTR", CreateJsValue(*engine, static_cast<int32_t>(CMError::CM_ERROR_NULLPTR)));
85 object->SetProperty("CM_ERROR_INVALID_PARAM",
86 CreateJsValue(*engine, static_cast<int32_t>(CMError::CM_ERROR_INVALID_PARAM)));
87 object->SetProperty("CM_ERROR_INVALID_ENUM_USAGE",
88 CreateJsValue(*engine, static_cast<int32_t>(CMError::CM_ERROR_INVALID_ENUM_USAGE)));
89 return objValue;
90 }
91
CMErrorCodeInit(NativeEngine * engine)92 NativeValue* CMErrorCodeInit(NativeEngine* engine)
93 {
94 if (engine == nullptr) {
95 CMLOGE("[NAPI]Engine is nullptr");
96 return nullptr;
97 }
98
99 NativeValue *objValue = engine->CreateObject();
100 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
101 if (object == nullptr) {
102 CMLOGE("[NAPI]Failed to get object");
103 return nullptr;
104 }
105
106 object->SetProperty("CM_ERROR_NO_PERMISSION",
107 CreateJsValue(*engine, static_cast<int32_t>(CMErrorCode::CM_ERROR_NO_PERMISSION)));
108 object->SetProperty("CM_ERROR_INVALID_PARAM",
109 CreateJsValue(*engine, static_cast<int32_t>(CMErrorCode::CM_ERROR_INVALID_PARAM)));
110 object->SetProperty("CM_ERROR_DEVICE_NOT_SUPPORT",
111 CreateJsValue(*engine, static_cast<int32_t>(CMErrorCode::CM_ERROR_DEVICE_NOT_SUPPORT)));
112 object->SetProperty("CM_ERROR_ABNORMAL_PARAM_VALUE",
113 CreateJsValue(*engine, static_cast<int32_t>(CMErrorCode::CM_ERROR_ABNORMAL_PARAM_VALUE)));
114 return objValue;
115 }
116
ParseJsDoubleValue(NativeObject * jsObject,NativeEngine & engine,const std::string & name,double & data)117 bool ParseJsDoubleValue(NativeObject* jsObject, NativeEngine& engine, const std::string& name, double& data)
118 {
119 NativeValue* value = jsObject->GetProperty(name.c_str());
120 if (value->TypeOf() != NATIVE_UNDEFINED) {
121 if (!ConvertFromJsValue(engine, value, data)) {
122 CMLOGE("[NAPI]Failed to convert parameter to data: %{public}s", name.c_str());
123 return false;
124 }
125 } else {
126 CMLOGI("[NAPI]no property with: %{public}s", name.c_str());
127 return false;
128 }
129 return true;
130 }
131 } // namespace ColorManager
132 } // namespace OHOS
133