1 /*
2  * Copyright (c) 2024 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_sampling_options.h"
17 #include "js_drawing_utils.h"
18 #include "js_common.h"
19 #include "native_value.h"
20 
21 namespace OHOS::Rosen {
22 namespace Drawing {
23 thread_local napi_ref JsSamplingOptions::constructor_ = nullptr;
24 const std::string CLASS_NAME = "SamplingOptions";
Init(napi_env env,napi_value exportObj)25 napi_value JsSamplingOptions::Init(napi_env env, napi_value exportObj)
26 {
27     napi_property_descriptor properties[] = {
28     };
29 
30     napi_value constructor = nullptr;
31     napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr,
32                                            sizeof(properties) / sizeof(properties[0]), properties, &constructor);
33     if (status != napi_ok) {
34         ROSEN_LOGE("JsSamplingOptions::Init failed to define SamplingOptions class");
35         return nullptr;
36     }
37 
38     status = napi_create_reference(env, constructor, 1, &constructor_);
39     if (status != napi_ok) {
40         ROSEN_LOGE("JsSamplingOptions::Init failed to create reference of constructor");
41         return nullptr;
42     }
43 
44     status = napi_set_named_property(env, exportObj, CLASS_NAME.c_str(), constructor);
45     if (status != napi_ok) {
46         ROSEN_LOGE("JsSamplingOptions::Init failed to set constructor");
47         return nullptr;
48     }
49 
50     return exportObj;
51 }
52 
Constructor(napi_env env,napi_callback_info info)53 napi_value JsSamplingOptions::Constructor(napi_env env, napi_callback_info info)
54 {
55     size_t argCount = ARGC_ONE;
56     napi_value argv[ARGC_ONE] = {nullptr};
57     napi_value jsThis = nullptr;
58     napi_status status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
59     if (status != napi_ok) {
60         ROSEN_LOGE("JsSamplingOptions::Constructor failed to napi_get_cb_info");
61         return nullptr;
62     }
63     JsSamplingOptions* jsSamplingOptions = nullptr;
64     std::shared_ptr<SamplingOptions> samplingOptions = nullptr;
65     if (argCount == ARGC_ZERO) {
66         samplingOptions = std::make_shared<SamplingOptions>();
67         jsSamplingOptions = new JsSamplingOptions(samplingOptions);
68     } else {
69         int32_t jsFilterMode = 0;
70         GET_INT32_CHECK_GE_ZERO_PARAM(ARGC_ZERO, jsFilterMode);
71         samplingOptions = std::make_shared<SamplingOptions>(FilterMode(jsFilterMode));
72         jsSamplingOptions = new JsSamplingOptions(samplingOptions);
73     }
74 
75     status = napi_wrap(env, jsThis, jsSamplingOptions,
76                        JsSamplingOptions::Destructor, nullptr, nullptr);
77     if (status != napi_ok) {
78         delete jsSamplingOptions;
79         ROSEN_LOGE("JsSamplingOptions::Constructor failed to wrap native instance");
80         return nullptr;
81     }
82 
83     return jsThis;
84 }
85 
Destructor(napi_env env,void * nativeObject,void * finalize)86 void JsSamplingOptions::Destructor(napi_env env, void* nativeObject, void* finalize)
87 {
88     (void)finalize;
89     if (nativeObject != nullptr) {
90         JsSamplingOptions* napi = reinterpret_cast<JsSamplingOptions*>(nativeObject);
91         delete napi;
92     }
93 }
94 
~JsSamplingOptions()95 JsSamplingOptions::~JsSamplingOptions()
96 {
97     m_samplingOptions = nullptr;
98 }
99 
GetSamplingOptions()100 std::shared_ptr<SamplingOptions> JsSamplingOptions::GetSamplingOptions()
101 {
102     return m_samplingOptions;
103 }
104 
105 } // namespace Drawing
106 } // namespace OHOS::Rosen
107