1 /*
2 * Copyright (c) 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 "core/components_ng/pattern/patternlock/patternlock_napi.h"
17
18 #include "base/utils/utils.h"
19 #include "core/components_ng/base/view_abstract.h"
20 #include "core/components_ng/common_napi_utils/common_napi_utils.h"
21 #include "core/components_ng/pattern/patternlock/pattern_lock_controller.h"
22 #include "core/components_ng/pattern/patternlock/patternlock_model_ng.h"
23 #include "core/components_v2/pattern_lock/pattern_lock_component.h"
24 #include "core/components_v2/pattern_lock/pattern_lock_theme.h"
25
26 extern const char _binary_arkui_patternlock_js_start[];
27 extern const char _binary_arkui_patternlock_abc_start[];
28 #if !defined(IOS_PLATFORM)
29 extern const char _binary_arkui_patternlock_js_end[];
30 extern const char _binary_arkui_patternlock_abc_end[];
31 #else
32 extern const char* _binary_arkui_patternlock_js_end;
33 extern const char* _binary_arkui_patternlock_abc_end;
34 #endif
35
36 namespace OHOS::Ace {
37 namespace {
38 static constexpr const size_t MAX_ARG_NUM = 10;
39 } // namespace
40 std::unique_ptr<PatternLockModel> PatternLockModel::instance_ = nullptr;
41 std::mutex PatternLockModel::mutex_;
42
43 template<typename T>
GetTheme()44 RefPtr<T> GetTheme()
45 {
46 auto pipelineContext = PipelineBase::GetCurrentContext();
47 CHECK_NULL_RETURN(pipelineContext, nullptr);
48 auto themeManager = pipelineContext->GetThemeManager();
49 CHECK_NULL_RETURN(themeManager, nullptr);
50 return themeManager->GetTheme<T>();
51 }
52
GetInstance()53 PatternLockModel* PatternLockModel::GetInstance()
54 {
55 if (!instance_) {
56 std::lock_guard<std::mutex> lock(mutex_);
57 if (!instance_) {
58 instance_.reset(new NG::PatternLockModelNG());
59 }
60 }
61 return instance_.get();
62 }
63
AutoResetResult(napi_env env,napi_value value)64 bool AutoResetResult(napi_env env, napi_value value)
65 {
66 bool autoReset = true;
67 napi_valuetype valueType = CommonNapiUtils::GetValueType(env, value);
68 if (valueType == napi_boolean) {
69 autoReset = CommonNapiUtils::GetBool(env, value);
70 }
71
72 return autoReset;
73 }
74
Create(napi_env env,napi_callback_info info)75 napi_value Create(napi_env env, napi_callback_info info)
76 {
77 size_t argc = MAX_ARG_NUM;
78 napi_value argv[MAX_ARG_NUM] = { nullptr };
79 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
80 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
81 PatternLockController* wrapper = nullptr;
82 napi_unwrap(env, argv[0], (void**)&wrapper);
83 if (wrapper) {
84 auto controller = PatternLockModel::GetInstance()->Create();
85 if (controller) {
86 wrapper->SetController(controller);
87 }
88 }
89 return CommonNapiUtils::CreateNull(env);
90 }
91
SideLength(napi_env env,napi_callback_info info)92 napi_value SideLength(napi_env env, napi_callback_info info)
93 {
94 size_t argc = MAX_ARG_NUM;
95 napi_value thisVal = nullptr;
96 napi_value argv[MAX_ARG_NUM] = { nullptr };
97 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
98 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
99
100 CalcDimension sideLength;
101 if (!CommonNapiUtils::GetDimensionResult(env, argv[0], sideLength)) {
102 RefPtr<V2::PatternLockTheme> patternLockTheme = GetTheme<V2::PatternLockTheme>();
103 CHECK_NULL_RETURN(patternLockTheme, CommonNapiUtils::CreateNull(env));
104 sideLength = patternLockTheme->GetSideLength();
105 }
106 PatternLockModel::GetInstance()->SetSideLength(sideLength);
107 return CommonNapiUtils::CreateNull(env);
108 }
109
CircleRadius(napi_env env,napi_callback_info info)110 napi_value CircleRadius(napi_env env, napi_callback_info info)
111 {
112 size_t argc = MAX_ARG_NUM;
113 napi_value thisVal = nullptr;
114 napi_value argv[MAX_ARG_NUM] = { nullptr };
115 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
116 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
117
118 CalcDimension radius;
119 if (!CommonNapiUtils::GetDimensionResult(env, argv[0], radius) || radius.IsNonPositive()) {
120 RefPtr<V2::PatternLockTheme> patternLockTheme = GetTheme<V2::PatternLockTheme>();
121 CHECK_NULL_RETURN(patternLockTheme, CommonNapiUtils::CreateNull(env));
122 radius = patternLockTheme->GetCircleRadius();
123 }
124 PatternLockModel::GetInstance()->SetCircleRadius(radius);
125 return CommonNapiUtils::CreateNull(env);
126 }
127
PathStrokeWidth(napi_env env,napi_callback_info info)128 napi_value PathStrokeWidth(napi_env env, napi_callback_info info)
129 {
130 size_t argc = MAX_ARG_NUM;
131 napi_value thisVal = nullptr;
132 napi_value argv[MAX_ARG_NUM] = { nullptr };
133 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
134 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
135
136 CalcDimension lineWidth;
137 if (!CommonNapiUtils::GetDimensionResult(env, argv[0], lineWidth)) {
138 RefPtr<V2::PatternLockTheme> patternLockTheme = GetTheme<V2::PatternLockTheme>();
139 CHECK_NULL_RETURN(patternLockTheme, CommonNapiUtils::CreateNull(env));
140 lineWidth = patternLockTheme->GetPathStrokeWidth();
141 }
142 PatternLockModel::GetInstance()->SetStrokeWidth(lineWidth);
143 return CommonNapiUtils::CreateNull(env);
144 }
145
ActiveColor(napi_env env,napi_callback_info info)146 napi_value ActiveColor(napi_env env, napi_callback_info info)
147 {
148 size_t argc = MAX_ARG_NUM;
149 napi_value thisVal = nullptr;
150 napi_value argv[MAX_ARG_NUM] = { nullptr };
151 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
152 NAPI_ASSERT(env, argc >= 1, "PatternLockModelNapi activeColor wrong number of arguments");
153
154 Color activeColor;
155 if (!CommonNapiUtils::ParseColor(env, argv[0], activeColor)) {
156 RefPtr<V2::PatternLockTheme> patternLockTheme = GetTheme<V2::PatternLockTheme>();
157 CHECK_NULL_RETURN(patternLockTheme, CommonNapiUtils::CreateNull(env));
158 activeColor = patternLockTheme->GetActiveColor();
159 }
160 PatternLockModel::GetInstance()->SetActiveColor(activeColor);
161 return CommonNapiUtils::CreateNull(env);
162 }
163
SelectedColor(napi_env env,napi_callback_info info)164 napi_value SelectedColor(napi_env env, napi_callback_info info)
165 {
166 size_t argc = MAX_ARG_NUM;
167 napi_value thisVal = nullptr;
168 napi_value argv[MAX_ARG_NUM] = { nullptr };
169 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
170 NAPI_ASSERT(env, argc >= 1, "PatternLockModelNapi selectedColor Wrong number of arguments");
171
172 Color selectedColor;
173 if (!CommonNapiUtils::ParseColor(env, argv[0], selectedColor)) {
174 RefPtr<V2::PatternLockTheme> patternLockTheme = GetTheme<V2::PatternLockTheme>();
175 CHECK_NULL_RETURN(patternLockTheme, CommonNapiUtils::CreateNull(env));
176 selectedColor = patternLockTheme->GetSelectedColor();
177 }
178 PatternLockModel::GetInstance()->SetSelectedColor(selectedColor);
179 return CommonNapiUtils::CreateNull(env);
180 }
181
PathColor(napi_env env,napi_callback_info info)182 napi_value PathColor(napi_env env, napi_callback_info info)
183 {
184 size_t argc = MAX_ARG_NUM;
185 napi_value thisVal = nullptr;
186 napi_value argv[MAX_ARG_NUM] = { nullptr };
187 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
188 NAPI_ASSERT(env, argc >= 1, "PatternLockModelNapi pathColor Wrong number of arguments");
189
190 Color pathColor;
191 if (!CommonNapiUtils::ParseColor(env, argv[0], pathColor)) {
192 RefPtr<V2::PatternLockTheme> patternLockTheme = GetTheme<V2::PatternLockTheme>();
193 CHECK_NULL_RETURN(patternLockTheme, CommonNapiUtils::CreateNull(env));
194 pathColor = patternLockTheme->GetPathColor();
195 }
196 PatternLockModel::GetInstance()->SetPathColor(pathColor);
197 return CommonNapiUtils::CreateNull(env);
198 }
199
RegularColor(napi_env env,napi_callback_info info)200 napi_value RegularColor(napi_env env, napi_callback_info info)
201 {
202 size_t argc = MAX_ARG_NUM;
203 napi_value thisVal = nullptr;
204 napi_value argv[MAX_ARG_NUM] = { nullptr };
205 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
206 NAPI_ASSERT(env, argc >= 1, "PatternLockModelNapi regularColor Wrong number of arguments");
207 Color regularColor;
208 if (!CommonNapiUtils::ParseColor(env, argv[0], regularColor)) {
209 RefPtr<V2::PatternLockTheme> patternLockTheme = GetTheme<V2::PatternLockTheme>();
210 CHECK_NULL_RETURN(patternLockTheme, CommonNapiUtils::CreateNull(env));
211 regularColor = patternLockTheme->GetRegularColor();
212 }
213 PatternLockModel::GetInstance()->SetRegularColor(regularColor);
214 return CommonNapiUtils::CreateNull(env);
215 }
216
AutoReset(napi_env env,napi_callback_info info)217 napi_value AutoReset(napi_env env, napi_callback_info info)
218 {
219 size_t argc = MAX_ARG_NUM;
220 napi_value thisVal = nullptr;
221 napi_value argv[MAX_ARG_NUM] = { nullptr };
222 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
223 NAPI_ASSERT(env, argc >= 1, "PatternLockModelNapi autoReset Wrong number of arguments");
224
225 bool autoReset = AutoResetResult(env, argv[0]);
226 PatternLockModel::GetInstance()->SetAutoReset(autoReset);
227 return CommonNapiUtils::CreateNull(env);
228 }
229
OnPatternComplete(napi_env env,napi_callback_info info)230 napi_value OnPatternComplete(napi_env env, napi_callback_info info)
231 {
232 size_t argc = MAX_ARG_NUM;
233 napi_value thisVal = nullptr;
234 napi_value argv[MAX_ARG_NUM] = { nullptr };
235 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, nullptr));
236 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
237 napi_ref callback = nullptr;
238 napi_valuetype valuetype;
239 NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
240 NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected.");
241 auto asyncEvent = std::make_shared<NapiAsyncEvnet>(env, argv[0]);
242 auto onComplete = [asyncEvent](const BaseEventInfo* info) {
243 napi_value arrayValue;
244 const auto* eventInfo = TypeInfoHelper::DynamicCast<V2::PatternCompleteEvent>(info);
245 CHECK_NULL_VOID(eventInfo);
246 arrayValue = CommonNapiUtils::CreateArray(asyncEvent->GetEnv());
247 std::vector<int> arr = eventInfo->GetInput();
248 for (size_t i = 0; i < arr.size(); i++) {
249 CommonNapiUtils::SetSelementToArray(
250 asyncEvent->GetEnv(), arrayValue, i, CommonNapiUtils::CreateInt32(asyncEvent->GetEnv(), arr.at(i)));
251 }
252 napi_value argv[1] = { arrayValue };
253 asyncEvent->Call(1, argv);
254 };
255 PatternLockModel::GetInstance()->SetPatternComplete(std::move(onComplete));
256 return CommonNapiUtils::CreateNull(env);
257 }
258
Reset(napi_env env,napi_callback_info info)259 napi_value Reset(napi_env env, napi_callback_info info)
260 {
261 napi_value thisVar = nullptr;
262 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, NULL));
263 PatternLockController* wrapper = nullptr;
264 napi_unwrap(env, thisVar, (void**)&wrapper);
265 if (wrapper == nullptr) {
266 return CommonNapiUtils::CreateNull(env);
267 }
268 wrapper->Reset();
269 return CommonNapiUtils::CreateNull(env);
270 }
271
PatternLockControllerConstructor(napi_env env,napi_callback_info info)272 napi_value PatternLockControllerConstructor(napi_env env, napi_callback_info info)
273 {
274 napi_value thisVar = nullptr;
275 napi_status status;
276 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
277 auto wrapper = new (std::nothrow) PatternLockController();
278 if (wrapper == nullptr) {
279 return CommonNapiUtils::CreateNull(env);
280 }
281 status = napi_wrap(
282 env, thisVar, wrapper,
283 [](napi_env env, void* data, void* hint) {
284 auto* wrapper = reinterpret_cast<PatternLockController*>(data);
285 delete wrapper;
286 wrapper = nullptr;
287 },
288 nullptr, nullptr);
289 if (status != napi_ok) {
290 delete wrapper;
291 return CommonNapiUtils::CreateNull(env);
292 }
293 return thisVar;
294 }
295
Init(napi_env env,napi_value exports)296 napi_value Init(napi_env env, napi_value exports)
297 {
298 static napi_property_descriptor desc[] = { DECLARE_NAPI_FUNCTION("create", Create),
299 DECLARE_NAPI_FUNCTION("sideLength", SideLength), DECLARE_NAPI_FUNCTION("circleRadius", CircleRadius),
300 DECLARE_NAPI_FUNCTION("pathStrokeWidth", PathStrokeWidth), DECLARE_NAPI_FUNCTION("activeColor", ActiveColor),
301 DECLARE_NAPI_FUNCTION("selectedColor", SelectedColor), DECLARE_NAPI_FUNCTION("pathColor", PathColor),
302 DECLARE_NAPI_FUNCTION("regularColor", RegularColor), DECLARE_NAPI_FUNCTION("autoReset", AutoReset),
303 DECLARE_NAPI_FUNCTION("onPatternComplete", OnPatternComplete) };
304 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
305 return exports;
306 }
307
InitController(napi_env env,napi_value exports)308 napi_value InitController(napi_env env, napi_value exports)
309 {
310 napi_value patternLockControllerClass = nullptr;
311 napi_property_descriptor properties[] = {
312 DECLARE_NAPI_FUNCTION("reset", Reset),
313 };
314 NAPI_CALL(env, napi_define_class(env, "PatternLockController", NAPI_AUTO_LENGTH, PatternLockControllerConstructor,
315 nullptr, sizeof(properties) / sizeof(*properties), properties, &patternLockControllerClass));
316 NAPI_CALL(env, napi_set_named_property(env, exports, "PatternLockController", patternLockControllerClass));
317 return exports;
318 }
319 } // namespace OHOS::Ace
320
NAPI_arkui_patternlock_GetJSCode(const char ** buf,int * bufLen)321 extern "C" __attribute__((visibility("default"))) void NAPI_arkui_patternlock_GetJSCode(const char** buf, int* bufLen)
322 {
323 if (buf != nullptr) {
324 *buf = _binary_arkui_patternlock_js_start;
325 }
326
327 if (bufLen != nullptr) {
328 *bufLen = _binary_arkui_patternlock_js_end - _binary_arkui_patternlock_js_start;
329 }
330 }
331
332 // arkui_patternlock JS register
NAPI_arkui_patternlock_GetABCCode(const char ** buf,int * buflen)333 extern "C" __attribute__((visibility("default"))) void NAPI_arkui_patternlock_GetABCCode(const char** buf, int* buflen)
334 {
335 if (buf != nullptr) {
336 *buf = _binary_arkui_patternlock_abc_start;
337 }
338 if (buflen != nullptr) {
339 *buflen = _binary_arkui_patternlock_abc_end - _binary_arkui_patternlock_abc_start;
340 }
341 }
342
343 static napi_module_with_js modules = {
344 .nm_filename = "arkui/libpatternlock.z.so/arkui_patternlock.js",
345 .nm_version = 1,
346 .nm_flags = 0,
347 .nm_register_func = OHOS::Ace::Init,
348 .nm_modname = "arkui.patternlock",
349 .nm_priv = ((void*)0),
350 .nm_get_abc_code = NAPI_arkui_patternlock_GetABCCode,
351 .nm_get_js_code = NAPI_arkui_patternlock_GetJSCode,
352 };
353
354 static napi_module patternLockControllerModule = {
355 .nm_version = 1,
356 .nm_flags = 0,
357 .nm_filename = nullptr,
358 .nm_register_func = OHOS::Ace::InitController,
359 .nm_modname = "arkui.patternlockcontroller",
360 .nm_priv = ((void*)0),
361 .reserved = { 0 },
362 };
363
RegisterModule()364 extern "C" __attribute__((constructor)) void RegisterModule()
365 {
366 napi_module_register(&patternLockControllerModule);
367 napi_module_with_js_register(&modules);
368 }
369