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 "native_node_napi.h"
17
18 #include "napi/native_node_api.h"
19 #include "node/node_extened.h"
20 #include "node/node_model.h"
21
22 #include "base/error/error_code.h"
23 #include "core/components_ng/base/frame_node.h"
24
25 extern "C" {
26
OH_ArkUI_GetNodeHandleFromNapiValue(napi_env env,napi_value value,ArkUI_NodeHandle * handle)27 int32_t OH_ArkUI_GetNodeHandleFromNapiValue(napi_env env, napi_value value, ArkUI_NodeHandle* handle)
28 {
29 bool hasProperty = false;
30 auto result = napi_has_named_property(env, value, "nodePtr_", &hasProperty);
31 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
32 if (result == napi_ok && hasProperty) {
33 napi_value frameNodePtr = nullptr;
34 auto result = napi_get_named_property(env, value, "nodePtr_", &frameNodePtr);
35 if (result != napi_ok) {
36 LOGE("fail to get nodePtr");
37 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
38 }
39 // BuilderNode case.
40 void* nativePtr = nullptr;
41 result = napi_get_value_external(env, frameNodePtr, &nativePtr);
42 if (result != napi_ok || nativePtr == nullptr) {
43 LOGE("fail to get nodePtr external value");
44 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
45 }
46 auto* uiNodePtr = reinterpret_cast<OHOS::Ace::NG::UINode*>(nativePtr);
47 uiNodePtr->IncRefCount();
48 // check whether it is bind to native XComponent.
49 bool isBindNativeXComponent = impl && impl->getNodeModifiers()->getXComponentModifier()
50 ->getXComponentIsBindNative(reinterpret_cast<ArkUINodeHandle>(nativePtr));
51 *handle = new ArkUI_Node({ .type = -1,
52 .uiNodeHandle = reinterpret_cast<ArkUINodeHandle>(nativePtr),
53 .cNode = false,
54 .buildNode = true });
55 if (isBindNativeXComponent) {
56 OHOS::Ace::NodeModel::RegisterBindNativeNode(*handle);
57 (*handle)->isBindNative = true;
58 }
59 if (impl) {
60 impl->getExtendedAPI()->setAttachNodePtr((*handle)->uiNodeHandle, reinterpret_cast<void*>(*handle));
61 }
62 return OHOS::Ace::ERROR_CODE_NO_ERROR;
63 }
64 result = napi_has_named_property(env, value, "builderNode_", &hasProperty);
65 if (result == napi_ok && hasProperty) {
66 // Component Content case.
67 napi_value builderNode = nullptr;
68 auto result = napi_get_named_property(env, value, "builderNode_", &builderNode);
69 if (result != napi_ok) {
70 LOGE("fail to get builderNode");
71 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
72 }
73 napi_value nodePtr = nullptr;
74 result = napi_get_named_property(env, builderNode, "nodePtr_", &nodePtr);
75 if (result != napi_ok) {
76 LOGE("fail to get nodePtr in builderNode");
77 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
78 }
79 void* nativePtr = nullptr;
80 result = napi_get_value_external(env, nodePtr, &nativePtr);
81 if (result != napi_ok) {
82 LOGE("fail to get nodePtr external value in builderNode");
83 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
84 }
85 auto* uiNode = reinterpret_cast<OHOS::Ace::NG::UINode*>(nativePtr);
86 OHOS::Ace::NG::FrameNode* frameNode = OHOS::Ace::AceType::DynamicCast<OHOS::Ace::NG::FrameNode>(uiNode);
87 if (frameNode == nullptr) {
88 LOGE("fail to get frameNode value in builderNode");
89 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
90 }
91 if (frameNode->GetTag() == "BuilderProxyNode") {
92 // need to get the really frameNode.
93 if (!impl) {
94 return OHOS::Ace::ERROR_CODE_NATIVE_IMPL_LIBRARY_NOT_FOUND;
95 }
96 auto* child = impl->getNodeModifiers()->getFrameNodeModifier()->getChild(
97 reinterpret_cast<ArkUINodeHandle>(frameNode), 0, true);
98 if (!child) {
99 LOGE("fail to get child in BuilderProxyNode");
100 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
101 }
102 frameNode = reinterpret_cast<OHOS::Ace::NG::FrameNode*>(child);
103 }
104 frameNode->IncRefCount();
105 *handle = new ArkUI_Node({ .type = -1,
106 .uiNodeHandle = reinterpret_cast<ArkUINodeHandle>(frameNode),
107 .cNode = false,
108 .buildNode = true });
109 if (impl) {
110 impl->getExtendedAPI()->setAttachNodePtr((*handle)->uiNodeHandle, reinterpret_cast<void*>(*handle));
111 }
112 return OHOS::Ace::ERROR_CODE_NO_ERROR;
113 }
114 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
115 }
116
OH_ArkUI_GetContextFromNapiValue(napi_env env,napi_value value,ArkUI_ContextHandle * context)117 int32_t OH_ArkUI_GetContextFromNapiValue(napi_env env, napi_value value, ArkUI_ContextHandle* context)
118 {
119 bool hasProperty = false;
120 auto result = napi_has_named_property(env, value, "instanceId_", &hasProperty);
121 if (result != napi_ok || !hasProperty) {
122 LOGE("fail to get Context value");
123 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
124 }
125
126 napi_value contextPtr = nullptr;
127 result = napi_get_named_property(env, value, "instanceId_", &contextPtr);
128 if (result != napi_ok) {
129 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
130 }
131
132 napi_valuetype valuetype;
133 if (napi_typeof(env, contextPtr, &valuetype) != napi_ok) {
134 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
135 }
136 if (valuetype != napi_number) {
137 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
138 }
139 int32_t instanceId = -1;
140 result = napi_get_value_int32(env, contextPtr, &instanceId);
141 if (result != napi_ok) {
142 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
143 }
144 *context = new ArkUI_Context({ .id = instanceId });
145 return OHOS::Ace::ERROR_CODE_NO_ERROR;
146 }
147
OH_ArkUI_GetNodeContentFromNapiValue(napi_env env,napi_value value,ArkUI_NodeContentHandle * content)148 int32_t OH_ArkUI_GetNodeContentFromNapiValue(napi_env env, napi_value value, ArkUI_NodeContentHandle* content)
149 {
150 bool hasProperty = false;
151 auto result = napi_has_named_property(env, value, "nativePtr_", &hasProperty);
152 if (result != napi_ok || !hasProperty) {
153 LOGE("fail to get native content value");
154 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
155 }
156 napi_value nativeContent = nullptr;
157 result = napi_get_named_property(env, value, "nativePtr_", &nativeContent);
158 if (result != napi_ok) {
159 LOGE("fail to get native content");
160 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
161 }
162 void* nativePtr = nullptr;
163 result = napi_get_value_external(env, nativeContent, &nativePtr);
164 if (result != napi_ok) {
165 LOGE("fail to get native content ptr");
166 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
167 }
168 *content = reinterpret_cast<ArkUI_NodeContentHandle>(nativePtr);
169 return OHOS::Ace::ERROR_CODE_NO_ERROR;
170 }
171
OH_ArkUI_InitModuleForArkTSEnv(napi_env env)172 ArkUI_ErrorCode OH_ArkUI_InitModuleForArkTSEnv(napi_env env)
173 {
174 CHECK_NULL_RETURN(env, ARKUI_ERROR_CODE_PARAM_INVALID);
175 CHECK_NULL_RETURN(OHOS::Ace::NodeModel::InitialFullImpl(), ARKUI_ERROR_CODE_CAPI_INIT_ERROR);
176 auto callback = [](const char* moduleName) -> bool {
177 const char* allowedModules[] = { "arkui.node", "arkui.modifier", "measure", "arkui.UIContext",
178 "arkui.observer", "arkui.inspector", "font", "arkui.uicontext" };
179 for (const char* allowedModule : allowedModules) {
180 if (std::strcmp(moduleName, allowedModule) == 0) {
181 return true;
182 }
183 }
184 return false;
185 };
186 // This function is guaranteed to be called only from a single thread,
187 // so there is no need for synchronization or thread-safety mechanisms.
188 static std::once_flag set_callback_flag;
189 static napi_status ret = napi_ok;
190 std::call_once(set_callback_flag, [callback]() {
191 ret = napi_set_module_validate_callback(callback);
192 });
193 if (ret != napi_ok) {
194 LOGE("fail to set module validate callback");
195 return ARKUI_ERROR_CODE_PARAM_INVALID;
196 }
197 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
198 impl->getRuntimeInit()->registerViews(reinterpret_cast<void*>(env));
199 return ARKUI_ERROR_CODE_NO_ERROR;
200 }
201
OH_ArkUI_NotifyArkTSEnvDestroy(napi_env env)202 void OH_ArkUI_NotifyArkTSEnvDestroy(napi_env env)
203 {
204 CHECK_NULL_VOID(env);
205 CHECK_NULL_VOID(OHOS::Ace::NodeModel::InitialFullImpl());
206 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
207 CHECK_NULL_VOID(impl);
208 impl->getRuntimeInit()->notifyArkTSEnvDestroy(reinterpret_cast<void*>(env));
209 }
210
OH_ArkUI_GetDrawableDescriptorFromNapiValue(napi_env env,napi_value value,ArkUI_DrawableDescriptor ** drawableDescriptor)211 int32_t OH_ArkUI_GetDrawableDescriptorFromNapiValue(
212 napi_env env, napi_value value, ArkUI_DrawableDescriptor** drawableDescriptor)
213 {
214 void* objectNapi = nullptr;
215 napi_unwrap(env, value, &objectNapi);
216 if (!objectNapi) {
217 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
218 }
219 ArkUI_DrawableDescriptor* drawable =
220 new ArkUI_DrawableDescriptor { nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr };
221 auto* descriptor = reinterpret_cast<OHOS::Ace::Napi::DrawableDescriptor*>(objectNapi);
222 if (!descriptor) {
223 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
224 }
225 auto drawableType = descriptor->GetDrawableType();
226 if (drawableType == OHOS::Ace::Napi::DrawableDescriptor::DrawableType::ANIMATED) {
227 auto* animatedDrawable = static_cast<OHOS::Ace::Napi::AnimatedDrawableDescriptor*>(descriptor);
228 if (!animatedDrawable) {
229 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
230 }
231 int32_t duration = animatedDrawable->GetDuration();
232 int32_t iteration = animatedDrawable->GetIterations();
233 drawable->animatedDrawableDescriptor = std::make_shared<OHOS::Ace::Napi::AnimatedDrawableDescriptor>(
234 animatedDrawable->GetPixelMapList(), duration, iteration);
235 *drawableDescriptor = drawable;
236 return OHOS::Ace::ERROR_CODE_NO_ERROR;
237 }
238 drawable->drawableDescriptor = std::make_shared<OHOS::Ace::Napi::DrawableDescriptor>(descriptor->GetPixelMap());
239 *drawableDescriptor = drawable;
240 return OHOS::Ace::ERROR_CODE_NO_ERROR;
241 }
242
OH_ArkUI_GetDrawableDescriptorFromResourceNapiValue(napi_env env,napi_value value,ArkUI_DrawableDescriptor ** drawableDescriptor)243 int32_t OH_ArkUI_GetDrawableDescriptorFromResourceNapiValue(
244 napi_env env, napi_value value, ArkUI_DrawableDescriptor** drawableDescriptor)
245 {
246 auto parseApi = reinterpret_cast<void (*)(void*, void*)>(OHOS::Ace::NodeModel::GetParseJsMedia());
247 if (!parseApi) {
248 return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
249 }
250
251 ArkUI_DrawableDescriptor* drawable =
252 new ArkUI_DrawableDescriptor { nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr };
253 drawable->resource = std::make_shared<ArkUI_Resource>();
254 parseApi(value, drawable->resource.get());
255 *drawableDescriptor = drawable;
256 return OHOS::Ace::ERROR_CODE_NO_ERROR;
257 }
258
OH_ArkUI_GetNavigationId(ArkUI_NodeHandle node,char * buffer,int32_t bufferSize,int32_t * writeLength)259 ArkUI_ErrorCode OH_ArkUI_GetNavigationId(
260 ArkUI_NodeHandle node, char* buffer, int32_t bufferSize, int32_t* writeLength)
261 {
262 CHECK_NULL_RETURN(node, ARKUI_ERROR_CODE_PARAM_INVALID);
263 CHECK_NULL_RETURN(buffer, ARKUI_ERROR_CODE_PARAM_INVALID);
264 CHECK_NULL_RETURN(writeLength, ARKUI_ERROR_CODE_PARAM_INVALID);
265 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
266 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_GET_INFO_FAILED);
267 auto navigationAPI = fullImpl->getNavigation();
268 CHECK_NULL_RETURN(navigationAPI, ARKUI_ERROR_CODE_GET_INFO_FAILED);
269 auto ret =
270 navigationAPI->getNavigationId(node->uiNodeHandle, buffer, bufferSize, writeLength);
271 return static_cast<ArkUI_ErrorCode>(ret);
272 }
273
OH_ArkUI_GetNavDestinationName(ArkUI_NodeHandle node,char * buffer,int32_t bufferSize,int32_t * writeLength)274 ArkUI_ErrorCode OH_ArkUI_GetNavDestinationName(
275 ArkUI_NodeHandle node, char* buffer, int32_t bufferSize, int32_t* writeLength)
276 {
277 CHECK_NULL_RETURN(node, ARKUI_ERROR_CODE_PARAM_INVALID);
278 CHECK_NULL_RETURN(buffer, ARKUI_ERROR_CODE_PARAM_INVALID);
279 CHECK_NULL_RETURN(writeLength, ARKUI_ERROR_CODE_PARAM_INVALID);
280 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
281 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_GET_INFO_FAILED);
282 auto navigationAPI = fullImpl->getNavigation();
283 CHECK_NULL_RETURN(navigationAPI, ARKUI_ERROR_CODE_GET_INFO_FAILED);
284 auto ret =
285 navigationAPI->getNavDestinationName(node->uiNodeHandle, buffer, bufferSize, writeLength);
286 return static_cast<ArkUI_ErrorCode>(ret);
287 }
288
OH_ArkUI_GetNavStackLength(ArkUI_NodeHandle node,int32_t * length)289 ArkUI_ErrorCode OH_ArkUI_GetNavStackLength(ArkUI_NodeHandle node, int32_t* length)
290 {
291 CHECK_NULL_RETURN(node, ARKUI_ERROR_CODE_PARAM_INVALID);
292 CHECK_NULL_RETURN(length, ARKUI_ERROR_CODE_PARAM_INVALID);
293 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
294 auto stacklength = fullImpl->getNavigation()->getStackLength(node->uiNodeHandle);
295 if (stacklength < 0) {
296 return ARKUI_ERROR_CODE_GET_INFO_FAILED;
297 }
298 *length = stacklength;
299 return ARKUI_ERROR_CODE_NO_ERROR;
300 }
301
OH_ArkUI_GetNavDestinationNameByIndex(ArkUI_NodeHandle node,int32_t index,char * buffer,int32_t bufferSize,int32_t * writeLength)302 ArkUI_ErrorCode OH_ArkUI_GetNavDestinationNameByIndex(
303 ArkUI_NodeHandle node, int32_t index, char* buffer, int32_t bufferSize, int32_t* writeLength)
304 {
305 CHECK_NULL_RETURN(node, ARKUI_ERROR_CODE_PARAM_INVALID);
306 CHECK_NULL_RETURN(buffer, ARKUI_ERROR_CODE_PARAM_INVALID);
307 CHECK_NULL_RETURN(writeLength, ARKUI_ERROR_CODE_PARAM_INVALID);
308 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
309 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_GET_INFO_FAILED);
310 auto navigationAPI = fullImpl->getNavigation();
311 CHECK_NULL_RETURN(navigationAPI, ARKUI_ERROR_CODE_GET_INFO_FAILED);
312 auto ret =
313 navigationAPI->getNavDesNameByIndex(node->uiNodeHandle, index, buffer, bufferSize, writeLength);
314 return static_cast<ArkUI_ErrorCode>(ret);
315 }
316
OH_ArkUI_GetNavDestinationId(ArkUI_NodeHandle node,char * buffer,int32_t bufferSize,int32_t * writeLength)317 ArkUI_ErrorCode OH_ArkUI_GetNavDestinationId(
318 ArkUI_NodeHandle node, char* buffer, int32_t bufferSize, int32_t* writeLength)
319 {
320 CHECK_NULL_RETURN(node, ARKUI_ERROR_CODE_PARAM_INVALID);
321 CHECK_NULL_RETURN(buffer, ARKUI_ERROR_CODE_PARAM_INVALID);
322 CHECK_NULL_RETURN(writeLength, ARKUI_ERROR_CODE_PARAM_INVALID);
323 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
324 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_GET_INFO_FAILED);
325 auto navigationAPI = fullImpl->getNavigation();
326 CHECK_NULL_RETURN(navigationAPI, ARKUI_ERROR_CODE_GET_INFO_FAILED);
327 auto ret =
328 navigationAPI->getNavDestinationId(node->uiNodeHandle, buffer, bufferSize, writeLength);
329 return static_cast<ArkUI_ErrorCode>(ret);
330 }
331
OH_ArkUI_GetNavDestinationState(ArkUI_NodeHandle node,ArkUI_NavDestinationState * state)332 ArkUI_ErrorCode OH_ArkUI_GetNavDestinationState(ArkUI_NodeHandle node, ArkUI_NavDestinationState* state)
333 {
334 CHECK_NULL_RETURN(node, ARKUI_ERROR_CODE_PARAM_INVALID);
335 CHECK_NULL_RETURN(state, ARKUI_ERROR_CODE_PARAM_INVALID);
336 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
337 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_GET_INFO_FAILED);
338 auto navigationAPI = fullImpl->getNavigation();
339 CHECK_NULL_RETURN(navigationAPI, ARKUI_ERROR_CODE_GET_INFO_FAILED);
340
341 auto navDestinationState = navigationAPI->getNavDestinationState(node->uiNodeHandle);
342 if (navDestinationState < 0) {
343 return ARKUI_ERROR_CODE_GET_INFO_FAILED;
344 }
345 *state = static_cast<ArkUI_NavDestinationState>(navDestinationState);
346 return ARKUI_ERROR_CODE_NO_ERROR;
347 }
348
OH_ArkUI_GetNavDestinationIndex(ArkUI_NodeHandle node,int32_t * index)349 ArkUI_ErrorCode OH_ArkUI_GetNavDestinationIndex(ArkUI_NodeHandle node, int32_t* index)
350 {
351 CHECK_NULL_RETURN(node, ARKUI_ERROR_CODE_PARAM_INVALID);
352 CHECK_NULL_RETURN(index, ARKUI_ERROR_CODE_PARAM_INVALID);
353 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
354 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_GET_INFO_FAILED);
355 auto navigationAPI = fullImpl->getNavigation();
356 CHECK_NULL_RETURN(navigationAPI, ARKUI_ERROR_CODE_GET_INFO_FAILED);
357
358 auto retIndex = navigationAPI->getNavDestinationIndex(node->uiNodeHandle);
359 if (retIndex < 0) {
360 return ARKUI_ERROR_CODE_GET_INFO_FAILED;
361 }
362 *index = retIndex;
363 return ARKUI_ERROR_CODE_NO_ERROR;
364 }
365
OH_ArkUI_GetNavDestinationParam(ArkUI_NodeHandle node)366 napi_value OH_ArkUI_GetNavDestinationParam(ArkUI_NodeHandle node)
367 {
368 CHECK_NULL_RETURN(node, nullptr);
369 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
370 CHECK_NULL_RETURN(fullImpl, nullptr);
371 auto navigationAPI = fullImpl->getNavigation();
372 CHECK_NULL_RETURN(navigationAPI, nullptr);
373 return reinterpret_cast<napi_value>(navigationAPI->getNavDestinationParam(node->uiNodeHandle));
374 }
375
OH_ArkUI_GetRouterPageIndex(ArkUI_NodeHandle node,int32_t * index)376 ArkUI_ErrorCode OH_ArkUI_GetRouterPageIndex(ArkUI_NodeHandle node, int32_t* index)
377 {
378 CHECK_NULL_RETURN(node, ARKUI_ERROR_CODE_PARAM_INVALID);
379 CHECK_NULL_RETURN(index, ARKUI_ERROR_CODE_PARAM_INVALID);
380 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
381 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_GET_INFO_FAILED);
382 auto navigationAPI = fullImpl->getNavigation();
383 CHECK_NULL_RETURN(navigationAPI, ARKUI_ERROR_CODE_GET_INFO_FAILED);
384
385 auto retIndex = navigationAPI->getRouterPageIndex(node->uiNodeHandle);
386 if (retIndex < 0) {
387 return ARKUI_ERROR_CODE_GET_INFO_FAILED;
388 }
389 *index = retIndex;
390 return ARKUI_ERROR_CODE_NO_ERROR;
391 }
392
OH_ArkUI_GetRouterPageName(ArkUI_NodeHandle node,char * buffer,int32_t bufferSize,int32_t * writeLength)393 ArkUI_ErrorCode OH_ArkUI_GetRouterPageName(
394 ArkUI_NodeHandle node, char* buffer, int32_t bufferSize, int32_t* writeLength)
395 {
396 CHECK_NULL_RETURN(node, ARKUI_ERROR_CODE_PARAM_INVALID);
397 CHECK_NULL_RETURN(buffer, ARKUI_ERROR_CODE_PARAM_INVALID);
398 CHECK_NULL_RETURN(writeLength, ARKUI_ERROR_CODE_PARAM_INVALID);
399 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
400 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_GET_INFO_FAILED);
401 auto navigationAPI = fullImpl->getNavigation();
402 CHECK_NULL_RETURN(navigationAPI, ARKUI_ERROR_CODE_GET_INFO_FAILED);
403 auto ret =
404 navigationAPI->getRouterPageName(node->uiNodeHandle, buffer, bufferSize, writeLength);
405 return static_cast<ArkUI_ErrorCode>(ret);
406 }
407
OH_ArkUI_GetRouterPagePath(ArkUI_NodeHandle node,char * buffer,int32_t bufferSize,int32_t * writeLength)408 ArkUI_ErrorCode OH_ArkUI_GetRouterPagePath(
409 ArkUI_NodeHandle node, char* buffer, int32_t bufferSize, int32_t* writeLength)
410 {
411 CHECK_NULL_RETURN(node, ARKUI_ERROR_CODE_PARAM_INVALID);
412 CHECK_NULL_RETURN(buffer, ARKUI_ERROR_CODE_PARAM_INVALID);
413 CHECK_NULL_RETURN(writeLength, ARKUI_ERROR_CODE_PARAM_INVALID);
414 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
415 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_GET_INFO_FAILED);
416 auto navigationAPI = fullImpl->getNavigation();
417 CHECK_NULL_RETURN(navigationAPI, ARKUI_ERROR_CODE_GET_INFO_FAILED);
418 auto ret =
419 navigationAPI->getRouterPagePath(node->uiNodeHandle, buffer, bufferSize, writeLength);
420 return static_cast<ArkUI_ErrorCode>(ret);
421 }
422
OH_ArkUI_GetRouterPageState(ArkUI_NodeHandle node,ArkUI_RouterPageState * state)423 ArkUI_ErrorCode OH_ArkUI_GetRouterPageState(ArkUI_NodeHandle node, ArkUI_RouterPageState* state)
424 {
425 CHECK_NULL_RETURN(node, ARKUI_ERROR_CODE_PARAM_INVALID);
426 CHECK_NULL_RETURN(state, ARKUI_ERROR_CODE_PARAM_INVALID);
427 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
428 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_GET_INFO_FAILED);
429 auto navigationAPI = fullImpl->getNavigation();
430 CHECK_NULL_RETURN(navigationAPI, ARKUI_ERROR_CODE_GET_INFO_FAILED);
431
432 auto routerPageState = navigationAPI->getRouterPageState(node->uiNodeHandle);
433 if (routerPageState < 0) {
434 return ARKUI_ERROR_CODE_GET_INFO_FAILED;
435 }
436 *state = static_cast<ArkUI_RouterPageState>(routerPageState);
437 return ARKUI_ERROR_CODE_NO_ERROR;
438 }
439
OH_ArkUI_GetRouterPageId(ArkUI_NodeHandle node,char * buffer,int32_t bufferSize,int32_t * writeLength)440 ArkUI_ErrorCode OH_ArkUI_GetRouterPageId(
441 ArkUI_NodeHandle node, char* buffer, int32_t bufferSize, int32_t* writeLength)
442 {
443 CHECK_NULL_RETURN(node, ARKUI_ERROR_CODE_PARAM_INVALID);
444 CHECK_NULL_RETURN(buffer, ARKUI_ERROR_CODE_PARAM_INVALID);
445 CHECK_NULL_RETURN(writeLength, ARKUI_ERROR_CODE_PARAM_INVALID);
446 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
447 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_GET_INFO_FAILED);
448 auto navigationAPI = fullImpl->getNavigation();
449 CHECK_NULL_RETURN(navigationAPI, ARKUI_ERROR_CODE_GET_INFO_FAILED);
450 auto ret =
451 navigationAPI->getRouterPageId(node->uiNodeHandle, buffer, bufferSize, writeLength);
452 return static_cast<ArkUI_ErrorCode>(ret);
453 }
454
OH_ArkUI_PostFrameCallback(ArkUI_ContextHandle uiContext,void * userData,void (* callback)(uint64_t nanoTimestamp,uint32_t frameCount,void * userData))455 int32_t OH_ArkUI_PostFrameCallback(ArkUI_ContextHandle uiContext, void* userData,
456 void (*callback)(uint64_t nanoTimestamp, uint32_t frameCount, void* userData))
457 {
458 CHECK_NULL_RETURN(uiContext, ARKUI_ERROR_CODE_UI_CONTEXT_INVALID);
459 CHECK_NULL_RETURN(callback, ARKUI_ERROR_CODE_CALLBACK_INVALID);
460 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
461 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_CAPI_INIT_ERROR);
462 auto basicAPI = fullImpl->getBasicAPI();
463 CHECK_NULL_RETURN(basicAPI, ARKUI_ERROR_CODE_CAPI_INIT_ERROR);
464 auto* context = reinterpret_cast<ArkUI_Context*>(uiContext);
465 auto id = context->id;
466 auto ret = basicAPI->postFrameCallback(id, userData, callback);
467 if (ret == OHOS::Ace::ERROR_CODE_NATIVE_IMPL_NOT_MAIN_THREAD) {
468 LOGF_ABORT("OH_ArkUI_PostFrameCallback doesn't run on UI thread!");
469 }
470 return static_cast<ArkUI_ErrorCode>(ret);
471 }
472
OH_ArkUI_PostIdleCallback(ArkUI_ContextHandle uiContext,void * userData,void (* callback)(uint64_t nanoTimeLeft,uint32_t frameCount,void * userData))473 int32_t OH_ArkUI_PostIdleCallback(ArkUI_ContextHandle uiContext, void* userData,
474 void (*callback)(uint64_t nanoTimeLeft, uint32_t frameCount, void* userData))
475 {
476 CHECK_NULL_RETURN(uiContext, ARKUI_ERROR_CODE_UI_CONTEXT_INVALID);
477 CHECK_NULL_RETURN(callback, ARKUI_ERROR_CODE_CALLBACK_INVALID);
478 auto* fullImpl = OHOS::Ace::NodeModel::GetFullImpl();
479 CHECK_NULL_RETURN(fullImpl, ARKUI_ERROR_CODE_CAPI_INIT_ERROR);
480 auto basicAPI = fullImpl->getBasicAPI();
481 CHECK_NULL_RETURN(basicAPI, ARKUI_ERROR_CODE_CAPI_INIT_ERROR);
482 auto* context = reinterpret_cast<ArkUI_Context*>(uiContext);
483 auto id = context->id;
484 auto ret = basicAPI->postIdleCallback(id, userData, callback);
485 if (ret == OHOS::Ace::ERROR_CODE_NATIVE_IMPL_NOT_MAIN_THREAD) {
486 LOGF_ABORT("OH_ArkUI_PostIdleCallback doesn't run on UI thread!");
487 }
488 return static_cast<ArkUI_ErrorCode>(ret);
489 }
490 }
491