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 #include "color_space_object_convertor.h"
16
17 #include "js_color_space.h"
18 #include "js_color_space_utils.h"
19
20 namespace OHOS {
21 namespace ColorManager {
CreateJsColorSpaceObject(NativeEngine & engine,std::shared_ptr<ColorSpace> & colorSpace)22 NativeValue* CreateJsColorSpaceObject(NativeEngine& engine, std::shared_ptr<ColorSpace>& colorSpace)
23 {
24 if (colorSpace == nullptr) {
25 CMLOGE("[NAPI]colorSpace is nullptr");
26 engine.Throw(CreateJsError(engine,
27 static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_INVALID_PARAM))));
28 return nullptr;
29 }
30 NativeValue* objValue = engine.CreateObject();
31 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
32 if (object == nullptr) {
33 CMLOGE("[NAPI]Fail to convert to js object");
34 engine.Throw(CreateJsError(engine, static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR))));
35 return engine.CreateUndefined();
36 }
37
38 std::unique_ptr<JsColorSpace> jsColorSpace = std::make_unique<JsColorSpace>(colorSpace);
39 object->SetNativePointer(jsColorSpace.release(), JsColorSpace::Finalizer, nullptr);
40 BindFunctions(engine, object);
41
42 std::shared_ptr<NativeReference> jsColorSpaceRef;
43 jsColorSpaceRef.reset(engine.CreateReference(objValue, 1));
44 return objValue;
45 }
46
GetColorSpaceByJSObject(NativeObject * object)47 std::shared_ptr<ColorSpace> GetColorSpaceByJSObject(NativeObject* object)
48 {
49 {
50 if (object == nullptr) {
51 CMLOGE("[NAPI]GetColorSpaceByJSObject::jsObject is nullptr");
52 return nullptr;
53 }
54 auto jsColorSpace = reinterpret_cast<JsColorSpace*>(object->GetNativePointer());
55 if (jsColorSpace == nullptr) {
56 CMLOGE("[NAPI]GetColorSpaceByJSObject::jsColorSpace is nullptr");
57 return nullptr;
58 }
59 return jsColorSpace->GetColorSpaceToken();
60 }
61 }
62 } // namespace ColorManager
63 } // namespace OHOS
64