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 #include "color_space_object_convertor.h"
16
17 #include "native_engine/native_reference.h"
18
19 #include "js_color_space.h"
20 #include "js_color_space_utils.h"
21
22 namespace OHOS {
23 namespace ColorManager {
CreateJsColorSpaceObject(napi_env env,std::shared_ptr<ColorSpace> & colorSpace)24 napi_value CreateJsColorSpaceObject(napi_env env, std::shared_ptr<ColorSpace>& colorSpace)
25 {
26 if (colorSpace == nullptr) {
27 CMLOGE("[NAPI]colorSpace is nullptr");
28 napi_throw(env,
29 CreateJsError(env, static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_INVALID_PARAM)),
30 "[NAPI]colorSpace is nullptr"));
31 return nullptr;
32 }
33 napi_value object = nullptr;
34 napi_create_object(env, &object);
35 if (object == nullptr) {
36 CMLOGE("[NAPI]Fail to convert to js object");
37 napi_throw(env,
38 CreateJsError(env, static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_INVALID_PARAM)),
39 "[NAPI]Fail to convert to js object"));
40 napi_get_undefined(env, &object);
41 return object;
42 }
43
44 std::unique_ptr<JsColorSpace> jsColorSpace = std::make_unique<JsColorSpace>(colorSpace);
45 napi_wrap(env, object, jsColorSpace.release(), JsColorSpace::Finalizer, nullptr, nullptr);
46 BindFunctions(env, object);
47
48 std::shared_ptr<NativeReference> jsColorSpaceNativeRef;
49 napi_ref jsColorSpaceRef = nullptr;
50 napi_create_reference(env, object, 1, &jsColorSpaceRef);
51 jsColorSpaceNativeRef.reset(reinterpret_cast<NativeReference*>(jsColorSpaceRef));
52 return object;
53 }
54
GetColorSpaceByJSObject(napi_env env,napi_value object)55 std::shared_ptr<ColorSpace> GetColorSpaceByJSObject(napi_env env, napi_value object)
56 {
57 if (object == nullptr) {
58 CMLOGE("[NAPI]GetColorSpaceByJSObject::jsObject is nullptr");
59 return nullptr;
60 }
61 JsColorSpace* jsColorSpace = nullptr;
62 napi_unwrap(env, object, (void **)&jsColorSpace);
63 if (jsColorSpace == nullptr) {
64 CMLOGE("[NAPI]GetColorSpaceByJSObject::jsColorSpace is nullptr");
65 return nullptr;
66 }
67 return jsColorSpace->GetColorSpaceToken();
68 }
69 } // namespace ColorManager
70 } // namespace OHOS
71