1 /*
2 * Copyright (c) 2021-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 "frameworks/bridge/declarative_frontend/declarative_frontend.h"
17
18 #include "base/log/dump_log.h"
19 #include "base/log/event_report.h"
20 #include "core/common/recorder/node_data_cache.h"
21 #include "frameworks/bridge/card_frontend/form_frontend_delegate_declarative.h"
22 #include "frameworks/bridge/declarative_frontend/ng/page_router_manager_factory.h"
23
24 namespace OHOS::Ace {
25 namespace {
26
27 /*
28 * NOTE:
29 * This function is needed to copy the values from BaseEventInfo
30 * It is observed, that the owner of BaseEventInfo will delete the pointer before it is ultimately
31 * processed by the EventMarker callback. In order to avoid this, a copy of all data needs to be made.
32 */
CopyEventInfo(const BaseEventInfo & info)33 std::shared_ptr<BaseEventInfo> CopyEventInfo(const BaseEventInfo& info)
34 {
35 const auto* touchInfo = TypeInfoHelper::DynamicCast<TouchEventInfo>(&info);
36 if (touchInfo != nullptr) {
37 return std::make_shared<TouchEventInfo>(*touchInfo);
38 }
39
40 const auto* dragStartInfo = TypeInfoHelper::DynamicCast<DragStartInfo>(&info);
41 if (dragStartInfo != nullptr) {
42 return std::make_shared<DragStartInfo>(*dragStartInfo);
43 }
44
45 const auto* dragUpdateInfo = TypeInfoHelper::DynamicCast<DragUpdateInfo>(&info);
46 if (dragUpdateInfo != nullptr) {
47 return std::make_shared<DragUpdateInfo>(*dragUpdateInfo);
48 }
49
50 const auto* dragEndInfo = TypeInfoHelper::DynamicCast<DragEndInfo>(&info);
51 if (dragEndInfo != nullptr) {
52 return std::make_shared<DragEndInfo>(*dragEndInfo);
53 }
54
55 const auto* clickInfo = TypeInfoHelper::DynamicCast<ClickInfo>(&info);
56 if (clickInfo != nullptr) {
57 return std::make_shared<ClickInfo>(*clickInfo);
58 }
59 return nullptr;
60 }
61
TouchInfoToString(const BaseEventInfo & info,std::string & eventParam)62 void TouchInfoToString(const BaseEventInfo& info, std::string& eventParam)
63 {
64 eventParam.append("{\"touches\":[{");
65 const auto touchInfo = TypeInfoHelper::DynamicCast<TouchEventInfo>(&info);
66 if (touchInfo) {
67 auto touchList = touchInfo->GetTouches();
68 for (const auto& location : touchList) {
69 auto globalLocation = location.GetGlobalLocation();
70 eventParam.append("\"globalX\":")
71 .append(std::to_string(globalLocation.GetX()))
72 .append(",\"globalY\":")
73 .append(std::to_string(globalLocation.GetY()))
74 .append(",");
75 auto localLocation = location.GetLocalLocation();
76 eventParam.append("\"localX\":")
77 .append(std::to_string(localLocation.GetX()))
78 .append(",\"localY\":")
79 .append(std::to_string(localLocation.GetY()))
80 .append(",");
81 eventParam.append("\"size\":").append(std::to_string(location.GetSize())).append(",");
82 }
83 if (eventParam.back() == ',') {
84 eventParam.pop_back();
85 }
86 eventParam.append("}],\"changedTouches\":[{");
87 auto changeTouch = touchInfo->GetChangedTouches();
88 for (const auto& change : changeTouch) {
89 auto globalLocation = change.GetGlobalLocation();
90 eventParam.append("\"globalX\":")
91 .append(std::to_string(globalLocation.GetX()))
92 .append(",\"globalY\":")
93 .append(std::to_string(globalLocation.GetY()))
94 .append(",");
95 auto localLocation = change.GetLocalLocation();
96 eventParam.append("\"localX\":")
97 .append(std::to_string(localLocation.GetX()))
98 .append(",\"localY\":")
99 .append(std::to_string(localLocation.GetY()))
100 .append(",");
101 eventParam.append("\"size\":").append(std::to_string(change.GetSize())).append(",");
102 }
103 if (eventParam.back() == ',') {
104 eventParam.pop_back();
105 }
106 }
107 eventParam.append("}]}");
108 }
109
MouseInfoToString(const BaseEventInfo & info,std::string & eventParam)110 void MouseInfoToString(const BaseEventInfo& info, std::string& eventParam)
111 {
112 const auto mouseInfo = TypeInfoHelper::DynamicCast<MouseEventInfo>(&info);
113 eventParam.append("{\"mouse\":{");
114 if (mouseInfo) {
115 auto globalMouse = mouseInfo->GetGlobalMouse();
116 eventParam.append("\"globalX\":")
117 .append(std::to_string(globalMouse.x))
118 .append(",\"globalY\":")
119 .append(std::to_string(globalMouse.y))
120 .append(",\"globalZ\":")
121 .append(std::to_string(globalMouse.z))
122 .append(",\"localX\":")
123 .append(std::to_string(globalMouse.x))
124 .append(",\"localY\":")
125 .append(std::to_string(globalMouse.y))
126 .append(",\"localZ\":")
127 .append(std::to_string(globalMouse.z))
128 .append(",\"deltaX\":")
129 .append(std::to_string(globalMouse.deltaX))
130 .append(",\"deltaY\":")
131 .append(std::to_string(globalMouse.deltaY))
132 .append(",\"deltaZ\":")
133 .append(std::to_string(globalMouse.deltaZ))
134 .append(",\"scrollX\":")
135 .append(std::to_string(globalMouse.scrollX))
136 .append(",\"scrollY\":")
137 .append(std::to_string(globalMouse.scrollY))
138 .append(",\"scrollZ\":")
139 .append(std::to_string(globalMouse.scrollZ))
140 .append(",\"action\":")
141 .append(std::to_string(static_cast<int32_t>(globalMouse.action)))
142 .append(",\"button\":")
143 .append(std::to_string(static_cast<int32_t>(globalMouse.button)))
144 .append(",\"pressedButtons\":")
145 .append(std::to_string(globalMouse.pressedButtons));
146 }
147 eventParam.append("}}");
148 }
149
SwipeInfoToString(const BaseEventInfo & info,std::string & eventParam)150 void SwipeInfoToString(const BaseEventInfo& info, std::string& eventParam)
151 {
152 const auto& swipeInfo = TypeInfoHelper::DynamicCast<SwipeEventInfo>(&info);
153 if (swipeInfo != nullptr) {
154 eventParam = swipeInfo->ToJsonParamInfo();
155 }
156 }
157
158 } // namespace
159
~DeclarativeFrontend()160 DeclarativeFrontend::~DeclarativeFrontend() noexcept
161 {
162 LOGI("DeclarativeFrontend destroyed");
163 }
164
Destroy()165 void DeclarativeFrontend::Destroy()
166 {
167 // The call doesn't change the page pop status
168 Recorder::NodeDataCache::Get().OnBeforePagePop(true);
169 CHECK_RUN_ON(JS);
170 LOGI("DeclarativeFrontend Destroy begin.");
171 // To guarantee the jsEngine_ and delegate_ released in js thread
172 delegate_.Reset();
173 handler_.Reset();
174 if (jsEngine_) {
175 jsEngine_->Destroy();
176 }
177 jsEngine_.Reset();
178 LOGI("DeclarativeFrontend Destroy end.");
179 }
180
Initialize(FrontendType type,const RefPtr<TaskExecutor> & taskExecutor)181 bool DeclarativeFrontend::Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor)
182 {
183 type_ = type;
184 taskExecutor_ = taskExecutor;
185 ACE_DCHECK(type_ == FrontendType::DECLARATIVE_JS);
186 InitializeFrontendDelegate(taskExecutor);
187
188 bool needPostJsTask = true;
189 auto container = Container::Current();
190 if (container) {
191 const auto& setting = container->GetSettings();
192 needPostJsTask = !(setting.usePlatformAsUIThread && setting.useUIAsJSThread);
193 }
194
195 #if defined(PREVIEW)
196 auto initJSEngineTask = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_), delegate = delegate_,
197 pkgNameMap = pkgNameMap_, pkgAliasMap = pkgAliasMap_, pkgContextInfoMap = pkgContextInfoMap_] {
198 auto jsEngine = weakEngine.Upgrade();
199 if (!jsEngine) {
200 return;
201 }
202 jsEngine->SetPkgNameList(pkgNameMap);
203 jsEngine->SetPkgAliasList(pkgAliasMap);
204 jsEngine->SetpkgContextInfoList(pkgContextInfoMap);
205 #else
206 auto initJSEngineTask = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_), delegate = delegate_] {
207 auto jsEngine = weakEngine.Upgrade();
208 if (!jsEngine) {
209 return;
210 }
211 #endif
212 jsEngine->Initialize(delegate);
213 };
214 if (needPostJsTask) {
215 taskExecutor->PostTask(initJSEngineTask, TaskExecutor::TaskType::JS, "ArkUIInitJsEngine");
216 } else {
217 initJSEngineTask();
218 }
219
220 return true;
221 }
222
223 void DeclarativeFrontend::AttachPipelineContext(const RefPtr<PipelineBase>& context)
224 {
225 if (!delegate_) {
226 return;
227 }
228 handler_ = AceType::MakeRefPtr<DeclarativeEventHandler>(delegate_);
229 auto pipelineContext = AceType::DynamicCast<PipelineContext>(context);
230 if (pipelineContext) {
231 pipelineContext->RegisterEventHandler(handler_);
232 }
233 delegate_->AttachPipelineContext(context);
234 }
235
236 void DeclarativeFrontend::AttachSubPipelineContext(const RefPtr<PipelineBase>& context)
237 {
238 if (!context) {
239 return;
240 }
241 auto pipelineContext = AceType::DynamicCast<PipelineContext>(context);
242 if (pipelineContext) {
243 pipelineContext->RegisterEventHandler(handler_);
244 }
245 if (!delegate_) {
246 return;
247 }
248 delegate_->AttachSubPipelineContext(context);
249 }
250
251 void DeclarativeFrontend::SetAssetManager(const RefPtr<AssetManager>& assetManager)
252 {
253 if (delegate_) {
254 delegate_->SetAssetManager(assetManager);
255 }
256 }
257
258 void DeclarativeFrontend::InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor)
259 {
260 const auto& loadCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](const std::string& url,
261 const RefPtr<Framework::JsAcePage>& jsPage, bool isMainPage) {
262 auto jsEngine = weakEngine.Upgrade();
263 if (!jsEngine) {
264 return;
265 }
266 jsEngine->LoadJs(url, jsPage, isMainPage);
267 jsEngine->UpdateRootComponent();
268 };
269
270 const auto& setPluginMessageTransferCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
271 const RefPtr<JsMessageDispatcher>& dispatcher) {
272 auto jsEngine = weakEngine.Upgrade();
273 if (!jsEngine) {
274 return;
275 }
276 jsEngine->SetJsMessageDispatcher(dispatcher);
277 };
278
279 const auto& asyncEventCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
280 const std::string& eventId, const std::string& param) {
281 auto jsEngine = weakEngine.Upgrade();
282 if (!jsEngine) {
283 return;
284 }
285 jsEngine->FireAsyncEvent(eventId, param);
286 };
287
288 const auto& syncEventCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
289 const std::string& eventId, const std::string& param) {
290 auto jsEngine = weakEngine.Upgrade();
291 if (!jsEngine) {
292 return;
293 }
294 jsEngine->FireSyncEvent(eventId, param);
295 };
296
297 const auto& updatePageCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
298 const RefPtr<Framework::JsAcePage>& jsPage) {
299 auto jsEngine = weakEngine.Upgrade();
300 if (!jsEngine) {
301 return;
302 }
303 jsEngine->UpdateRunningPage(jsPage);
304 jsEngine->UpdateStagingPage(jsPage);
305 };
306
307 const auto& resetStagingPageCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() {
308 auto jsEngine = weakEngine.Upgrade();
309 if (!jsEngine) {
310 return;
311 }
312 jsEngine->ResetStagingPage();
313 };
314
315 const auto& destroyPageCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](int32_t pageId) {
316 auto jsEngine = weakEngine.Upgrade();
317 if (!jsEngine) {
318 return;
319 }
320 jsEngine->DestroyPageInstance(pageId);
321 };
322
323 const auto& destroyApplicationCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
324 const std::string& packageName) {
325 auto jsEngine = weakEngine.Upgrade();
326 if (!jsEngine) {
327 return;
328 }
329 jsEngine->DestroyApplication(packageName);
330 };
331
332 const auto& updateApplicationStateCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
333 const std::string& packageName, Frontend::State state) {
334 auto jsEngine = weakEngine.Upgrade();
335 if (!jsEngine) {
336 return;
337 }
338 jsEngine->UpdateApplicationState(packageName, state);
339 };
340
341 const auto& onWindowDisplayModeChangedCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
342 bool isShownInMultiWindow, const std::string& data) {
343 auto jsEngine = weakEngine.Upgrade();
344 if (!jsEngine) {
345 return;
346 }
347 jsEngine->OnWindowDisplayModeChanged(isShownInMultiWindow, data);
348 };
349
350 const auto& onSaveAbilityStateCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](std::string& data) {
351 auto jsEngine = weakEngine.Upgrade();
352 if (!jsEngine) {
353 return;
354 }
355 jsEngine->OnSaveAbilityState(data);
356 };
357 const auto& onRestoreAbilityStateCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
358 const std::string& data) {
359 auto jsEngine = weakEngine.Upgrade();
360 if (!jsEngine) {
361 return;
362 }
363 jsEngine->OnRestoreAbilityState(data);
364 };
365
366 const auto& onNewWantCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](const std::string& data) {
367 auto jsEngine = weakEngine.Upgrade();
368 if (!jsEngine) {
369 return;
370 }
371 jsEngine->OnNewWant(data);
372 };
373
374 const auto& onConfigurationUpdatedCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
375 const std::string& data) {
376 auto jsEngine = weakEngine.Upgrade();
377 if (!jsEngine) {
378 return;
379 }
380 jsEngine->OnConfigurationUpdated(data);
381 };
382
383 const auto& timerCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
384 const std::string& callbackId, const std::string& delay, bool isInterval) {
385 auto jsEngine = weakEngine.Upgrade();
386 if (!jsEngine) {
387 return;
388 }
389 jsEngine->TimerCallback(callbackId, delay, isInterval);
390 };
391
392 const auto& mediaQueryCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
393 const std::string& callbackId, const std::string& args) {
394 auto jsEngine = weakEngine.Upgrade();
395 if (!jsEngine) {
396 return;
397 }
398 jsEngine->MediaQueryCallback(callbackId, args);
399 };
400
401 const auto& layoutInspectorCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
402 const std::string& componentId) {
403 auto jsEngine = weakEngine.Upgrade();
404 if (!jsEngine) {
405 return;
406 }
407 jsEngine->LayoutInspectorCallback(componentId);
408 };
409
410 const auto& drawInspectorCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
411 const std::string& componentId) {
412 auto jsEngine = weakEngine.Upgrade();
413 if (!jsEngine) {
414 return;
415 }
416 jsEngine->DrawInspectorCallback(componentId);
417 };
418
419 const auto& requestAnimationCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
420 const std::string& callbackId, uint64_t timeStamp) {
421 auto jsEngine = weakEngine.Upgrade();
422 if (!jsEngine) {
423 return;
424 }
425 jsEngine->RequestAnimationCallback(callbackId, timeStamp);
426 };
427
428 const auto& jsCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
429 const std::string& callbackId, const std::string& args) {
430 auto jsEngine = weakEngine.Upgrade();
431 if (!jsEngine) {
432 return;
433 }
434 jsEngine->JsCallback(callbackId, args);
435 };
436
437 const auto& onMemoryLevelCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](const int32_t level) {
438 auto jsEngine = weakEngine.Upgrade();
439 if (!jsEngine) {
440 return;
441 }
442 jsEngine->OnMemoryLevel(level);
443 };
444
445 const auto& onStartContinuationCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() -> bool {
446 auto jsEngine = weakEngine.Upgrade();
447 if (!jsEngine) {
448 return false;
449 }
450 return jsEngine->OnStartContinuation();
451 };
452 const auto& onCompleteContinuationCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](int32_t code) {
453 auto jsEngine = weakEngine.Upgrade();
454 if (!jsEngine) {
455 return;
456 }
457 jsEngine->OnCompleteContinuation(code);
458 };
459 const auto& onRemoteTerminatedCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() {
460 auto jsEngine = weakEngine.Upgrade();
461 if (!jsEngine) {
462 return;
463 }
464 jsEngine->OnRemoteTerminated();
465 };
466 const auto& onSaveDataCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](std::string& savedData) {
467 auto jsEngine = weakEngine.Upgrade();
468 if (!jsEngine) {
469 return;
470 }
471 jsEngine->OnSaveData(savedData);
472 };
473 const auto& onRestoreDataCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
474 const std::string& data) -> bool {
475 auto jsEngine = weakEngine.Upgrade();
476 if (!jsEngine) {
477 return false;
478 }
479 return jsEngine->OnRestoreData(data);
480 };
481
482 const auto& externalEventCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
483 const std::string& componentId, const uint32_t nodeId,
484 const bool isDestroy) {
485 auto jsEngine = weakEngine.Upgrade();
486 if (!jsEngine) {
487 return;
488 }
489 jsEngine->FireExternalEvent(componentId, nodeId, isDestroy);
490 };
491
492 if (isFormRender_) {
493 delegate_ = AceType::MakeRefPtr<Framework::FormFrontendDelegateDeclarative>(taskExecutor, loadCallback,
494 setPluginMessageTransferCallback, asyncEventCallback, syncEventCallback, updatePageCallback,
495 resetStagingPageCallback, destroyPageCallback, destroyApplicationCallback, updateApplicationStateCallback,
496 timerCallback, mediaQueryCallback, layoutInspectorCallback, drawInspectorCallback, requestAnimationCallback,
497 jsCallback, onWindowDisplayModeChangedCallBack, onConfigurationUpdatedCallBack, onSaveAbilityStateCallBack,
498 onRestoreAbilityStateCallBack, onNewWantCallBack, onMemoryLevelCallBack, onStartContinuationCallBack,
499 onCompleteContinuationCallBack, onRemoteTerminatedCallBack, onSaveDataCallBack, onRestoreDataCallBack,
500 externalEventCallback);
501 } else {
502 delegate_ = AceType::MakeRefPtr<Framework::FrontendDelegateDeclarative>(taskExecutor, loadCallback,
503 setPluginMessageTransferCallback, asyncEventCallback, syncEventCallback, updatePageCallback,
504 resetStagingPageCallback, destroyPageCallback, destroyApplicationCallback, updateApplicationStateCallback,
505 timerCallback, mediaQueryCallback, layoutInspectorCallback, drawInspectorCallback, requestAnimationCallback,
506 jsCallback, onWindowDisplayModeChangedCallBack, onConfigurationUpdatedCallBack, onSaveAbilityStateCallBack,
507 onRestoreAbilityStateCallBack, onNewWantCallBack, onMemoryLevelCallBack, onStartContinuationCallBack,
508 onCompleteContinuationCallBack, onRemoteTerminatedCallBack, onSaveDataCallBack, onRestoreDataCallBack,
509 externalEventCallback);
510 }
511
512 if (disallowPopLastPage_) {
513 delegate_->DisallowPopLastPage();
514 }
515 if (!jsEngine_) {
516 EventReport::SendAppStartException(AppStartExcepType::JS_ENGINE_CREATE_ERR);
517 return;
518 }
519 delegate_->SetGroupJsBridge(jsEngine_->GetGroupJsBridge());
520 if (Container::IsCurrentUseNewPipeline()) {
521 auto loadPageCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](const std::string& url,
522 const std::function<void(const std::string&, int32_t)>& errorCallback) {
523 auto jsEngine = weakEngine.Upgrade();
524 if (!jsEngine) {
525 return false;
526 }
527 return jsEngine->LoadPageSource(url, errorCallback);
528 };
529 auto loadPageByBufferCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
530 const std::shared_ptr<std::vector<uint8_t>>& content,
531 const std::function<void(const std::string&, int32_t)>& errorCallback,
532 const std::string& contentName) {
533 auto jsEngine = weakEngine.Upgrade();
534 if (!jsEngine) {
535 return false;
536 }
537 return jsEngine->LoadPageSource(content, errorCallback, contentName);
538 };
539 auto loadNamedRouterCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
540 const std::string& namedRouter, bool isTriggeredByJs) {
541 auto jsEngine = weakEngine.Upgrade();
542 if (!jsEngine) {
543 return false;
544 }
545 return jsEngine->LoadNamedRouterSource(namedRouter, isTriggeredByJs);
546 };
547 auto updateRootComponentCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() {
548 auto jsEngine = weakEngine.Upgrade();
549 if (!jsEngine) {
550 return false;
551 }
552 return jsEngine->UpdateRootComponent();
553 };
554 auto getFullPathInfoCallback =
555 [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() -> std::unique_ptr<JsonValue> {
556 auto jsEngine = weakEngine.Upgrade();
557 if (!jsEngine) {
558 return nullptr;
559 }
560 return jsEngine->GetFullPathInfo();
561 };
562 auto restoreFullPathInfoCallback =
563 [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](std::unique_ptr<JsonValue> fullPathInfo) {
564 auto jsEngine = weakEngine.Upgrade();
565 if (!jsEngine) {
566 return;
567 }
568 return jsEngine->RestoreFullPathInfo(std::move(fullPathInfo));
569 };
570 auto getNamedRouterInfoCallback =
571 [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() -> std::unique_ptr<JsonValue> {
572 auto jsEngine = weakEngine.Upgrade();
573 if (!jsEngine) {
574 return nullptr;
575 }
576 return jsEngine->GetNamedRouterInfo();
577 };
578 auto restoreNamedRouterInfoCallback =
579 [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](std::unique_ptr<JsonValue> namedRouterInfo) {
580 auto jsEngine = weakEngine.Upgrade();
581 if (!jsEngine) {
582 return;
583 }
584 return jsEngine->RestoreNamedRouterInfo(std::move(namedRouterInfo));
585 };
586 auto isNamedRouterNeedPreloadCallback =
587 [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](const std::string& name) -> bool {
588 auto jsEngine = weakEngine.Upgrade();
589 if (!jsEngine) {
590 return false;
591 }
592 return jsEngine->IsNamedRouterNeedPreload(name);
593 };
594
595 auto preloadNamedRouterCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
596 const std::string& name, std::function<void(bool)>&& loadFinishCallback) {
597 auto jsEngine = weakEngine.Upgrade();
598 if (!jsEngine) {
599 return;
600 }
601 return jsEngine->PreloadNamedRouter(name, std::move(loadFinishCallback));
602 };
603
604 auto pageRouterManager = NG::PageRouterManagerFactory::CreateManager();
605 pageRouterManager->SetLoadJsCallback(std::move(loadPageCallback));
606 pageRouterManager->SetLoadJsByBufferCallback(std::move(loadPageByBufferCallback));
607 pageRouterManager->SetLoadNamedRouterCallback(std::move(loadNamedRouterCallback));
608 pageRouterManager->SetUpdateRootComponentCallback(std::move(updateRootComponentCallback));
609 pageRouterManager->SetGetFullPathInfoCallback(std::move(getFullPathInfoCallback));
610 pageRouterManager->SetRestoreFullPathInfoCallback(std::move(restoreFullPathInfoCallback));
611 pageRouterManager->SetGetNamedRouterInfoCallback(std::move(getNamedRouterInfoCallback));
612 pageRouterManager->SetRestoreNamedRouterInfoCallback(std::move(restoreNamedRouterInfoCallback));
613 pageRouterManager->SetIsNamedRouterNeedPreloadCallback(std::move(isNamedRouterNeedPreloadCallback));
614 pageRouterManager->SetPreloadNamedRouterCallback(std::move(preloadNamedRouterCallback));
615 delegate_->SetPageRouterManager(pageRouterManager);
616
617 #if defined(PREVIEW)
618 auto isComponentPreviewCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() {
619 auto jsEngine = weakEngine.Upgrade();
620 if (!jsEngine) {
621 return false;
622 }
623 return jsEngine->IsComponentPreview();
624 };
625 delegate_->SetIsComponentPreview(isComponentPreviewCallback);
626 #endif
627
628 auto moduleNamecallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](const std::string& pageName)->
629 std::string {
630 auto jsEngine = weakEngine.Upgrade();
631 if (!jsEngine) {
632 return "";
633 }
634 return jsEngine->SearchRouterRegisterMap(pageName);
635 };
636 auto navigationLoadCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
637 const std::string bundleName, const std::string& moduleName, const std::string& pageSourceFile,
638 bool isSingleton) -> int32_t {
639 auto jsEngine = weakEngine.Upgrade();
640 if (!jsEngine) {
641 return -1;
642 }
643 return jsEngine->LoadNavDestinationSource(bundleName, moduleName, pageSourceFile, isSingleton);
644 };
645 auto container = Container::Current();
646 if (container) {
647 auto pageUrlChecker = container->GetPageUrlChecker();
648 // ArkTSCard container no SetPageUrlChecker
649 if (pageUrlChecker != nullptr) {
650 pageUrlChecker->SetModuleNameCallback(std::move(moduleNamecallback));
651 }
652 auto navigationRoute = container->GetNavigationRoute();
653 if (navigationRoute) {
654 navigationRoute->SetLoadPageCallback(std::move(navigationLoadCallback));
655 }
656 }
657 }
658 }
659
660 UIContentErrorCode DeclarativeFrontend::RunPage(const std::string& url, const std::string& params)
661 {
662 auto container = Container::Current();
663 auto isStageModel = container ? container->IsUseStageModel() : false;
664 if (!isStageModel && Container::IsCurrentUseNewPipeline()) {
665 // In NG structure and fa mode, first load app.js
666 auto taskExecutor = container ? container->GetTaskExecutor() : nullptr;
667 CHECK_NULL_RETURN(taskExecutor, UIContentErrorCode::NULL_POINTER);
668 taskExecutor->PostTask(
669 [weak = AceType::WeakClaim(this)]() {
670 auto frontend = weak.Upgrade();
671 CHECK_NULL_VOID(frontend);
672 CHECK_NULL_VOID(frontend->jsEngine_);
673 frontend->jsEngine_->LoadFaAppSource();
674 },
675 TaskExecutor::TaskType::JS, "ArkUILoadFaAppSource");
676 }
677
678 if (delegate_) {
679 if (isFormRender_) {
680 auto delegate = AceType::DynamicCast<Framework::FormFrontendDelegateDeclarative>(delegate_);
681 return delegate->RunCard(url, params, pageProfile_, 0);
682 } else {
683 delegate_->RunPage(url, params, pageProfile_);
684 return UIContentErrorCode::NO_ERRORS;
685 }
686 }
687
688 return UIContentErrorCode::NULL_POINTER;
689 }
690
691 UIContentErrorCode DeclarativeFrontend::RunPage(
692 const std::shared_ptr<std::vector<uint8_t>>& content, const std::string& params)
693 {
694 auto container = Container::Current();
695 auto isStageModel = container ? container->IsUseStageModel() : false;
696 if (!isStageModel) {
697 LOGE("RunPage by buffer must be run under stage model.");
698 return UIContentErrorCode::NO_STAGE;
699 }
700
701 if (delegate_) {
702 delegate_->RunPage(content, params, pageProfile_);
703 return UIContentErrorCode::NO_ERRORS;
704 }
705
706 return UIContentErrorCode::NULL_POINTER;
707 }
708
709 UIContentErrorCode DeclarativeFrontend::RunPageByNamedRouter(const std::string& name, const std::string& params)
710 {
711 if (delegate_) {
712 return delegate_->RunPage(name, params, pageProfile_, true);
713 }
714
715 return UIContentErrorCode::NULL_POINTER;
716 }
717
718 void DeclarativeFrontend::ReplacePage(const std::string& url, const std::string& params)
719 {
720 if (delegate_) {
721 delegate_->Replace(url, params);
722 }
723 }
724
725 void DeclarativeFrontend::PushPage(const std::string& url, const std::string& params)
726 {
727 if (delegate_) {
728 delegate_->Push(url, params);
729 }
730 }
731
732 void DeclarativeFrontend::NavigatePage(uint8_t type, const PageTarget& target, const std::string& params)
733 {
734 if (!delegate_) {
735 return;
736 }
737 switch (static_cast<NavigatorType>(type)) {
738 case NavigatorType::PUSH:
739 delegate_->Push(target, params);
740 break;
741 case NavigatorType::REPLACE:
742 delegate_->Replace(target, params);
743 break;
744 case NavigatorType::BACK:
745 delegate_->BackWithTarget(target, params);
746 break;
747 default:
748 delegate_->BackWithTarget(target, params);
749 break;
750 }
751 }
752
753 std::string DeclarativeFrontend::GetCurrentPageUrl() const
754 {
755 CHECK_NULL_RETURN(delegate_, "");
756 return delegate_->GetCurrentPageUrl();
757 }
758
759 // Get the currently running JS page information in NG structure.
760 RefPtr<Framework::RevSourceMap> DeclarativeFrontend::GetCurrentPageSourceMap() const
761 {
762 CHECK_NULL_RETURN(delegate_, nullptr);
763 return delegate_->GetCurrentPageSourceMap();
764 }
765
766 // Get the currently running JS page information in NG structure.
767 RefPtr<Framework::RevSourceMap> DeclarativeFrontend::GetFaAppSourceMap() const
768 {
769 CHECK_NULL_RETURN(delegate_, nullptr);
770 return delegate_->GetFaAppSourceMap();
771 }
772
773 void DeclarativeFrontend::GetStageSourceMap(
774 std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap) const
775 {
776 if (delegate_) {
777 delegate_->GetStageSourceMap(sourceMap);
778 }
779 }
780
781 RefPtr<NG::PageRouterManager> DeclarativeFrontend::GetPageRouterManager() const
782 {
783 CHECK_NULL_RETURN(delegate_, nullptr);
784 return delegate_->GetPageRouterManager();
785 }
786
787 std::pair<RouterRecoverRecord, UIContentErrorCode> DeclarativeFrontend::RestoreRouterStack(
788 const std::string& contentInfo, ContentInfoType type)
789 {
790 if (delegate_) {
791 return delegate_->RestoreRouterStack(contentInfo, type);
792 }
793 return std::make_pair(RouterRecoverRecord(), UIContentErrorCode::NULL_POINTER);
794 }
795
796 std::string DeclarativeFrontend::GetContentInfo(ContentInfoType type) const
797 {
798 if (delegate_) {
799 return delegate_->GetContentInfo(type);
800 }
801 return "";
802 }
803
804 int32_t DeclarativeFrontend::GetRouterSize() const
805 {
806 if (delegate_) {
807 return delegate_->GetStackSize();
808 }
809 return -1;
810 }
811
812 void DeclarativeFrontend::SendCallbackMessage(const std::string& callbackId, const std::string& data) const
813 {
814 if (delegate_) {
815 delegate_->OnJSCallback(callbackId, data);
816 }
817 }
818
819 void DeclarativeFrontend::SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const
820 {
821 if (delegate_) {
822 delegate_->SetJsMessageDispatcher(dispatcher);
823 }
824 }
825
826 void DeclarativeFrontend::TransferComponentResponseData(int callbackId, int32_t code, std::vector<uint8_t>&& data) const
827 {
828 if (delegate_) {
829 delegate_->TransferComponentResponseData(callbackId, code, std::move(data));
830 }
831 }
832
833 void DeclarativeFrontend::TransferJsResponseData(int callbackId, int32_t code, std::vector<uint8_t>&& data) const
834 {
835 if (delegate_) {
836 delegate_->TransferJsResponseData(callbackId, code, std::move(data));
837 }
838 }
839
840 napi_value DeclarativeFrontend::GetContextValue()
841 {
842 if (jsEngine_) {
843 return jsEngine_->GetContextValue();
844 }
845 return nullptr;
846 }
847
848 napi_value DeclarativeFrontend::GetFrameNodeValueByNodeId(int32_t nodeId)
849 {
850 if (jsEngine_) {
851 return jsEngine_->GetFrameNodeValueByNodeId(nodeId);
852 }
853 return nullptr;
854 }
855
856 #if defined(PREVIEW)
857 void DeclarativeFrontend::TransferJsResponseDataPreview(int callbackId, int32_t code, ResponseData responseData) const
858 {
859 delegate_->TransferJsResponseDataPreview(callbackId, code, responseData);
860 }
861
862 RefPtr<Component> DeclarativeFrontend::GetNewComponentWithJsCode(const std::string& jsCode, const std::string& viewID)
863 {
864 if (jsEngine_) {
865 return jsEngine_->GetNewComponentWithJsCode(jsCode, viewID);
866 }
867 return nullptr;
868 }
869 #endif
870
871 void DeclarativeFrontend::TransferJsPluginGetError(int callbackId, int32_t errorCode, std::string&& errorMessage) const
872 {
873 if (delegate_) {
874 delegate_->TransferJsPluginGetError(callbackId, errorCode, std::move(errorMessage));
875 }
876 }
877
878 void DeclarativeFrontend::TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const
879 {
880 if (delegate_) {
881 delegate_->TransferJsEventData(callbackId, code, std::move(data));
882 }
883 }
884
885 void DeclarativeFrontend::LoadPluginJsCode(std::string&& jsCode) const
886 {
887 if (delegate_) {
888 delegate_->LoadPluginJsCode(std::move(jsCode));
889 }
890 }
891
892 void DeclarativeFrontend::LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const
893 {
894 if (delegate_) {
895 delegate_->LoadPluginJsByteCode(std::move(jsCode), std::move(jsCodeLen));
896 }
897 }
898
899 void DeclarativeFrontend::UpdateState(Frontend::State state)
900 {
901 if (!delegate_ || state == Frontend::State::ON_CREATE) {
902 return;
903 }
904 bool needPostJsTask = true;
905 auto container = Container::Current();
906 CHECK_NULL_VOID(container);
907 const auto& setting = container->GetSettings();
908 needPostJsTask = !(setting.usePlatformAsUIThread && setting.useUIAsJSThread);
909 if (needPostJsTask) {
910 delegate_->UpdateApplicationState(delegate_->GetAppID(), state);
911 return;
912 }
913 if (jsEngine_) {
914 jsEngine_->UpdateApplicationState(delegate_->GetAppID(), state);
915 }
916 }
917
918 void DeclarativeFrontend::OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data)
919 {
920 delegate_->OnWindowDisplayModeChanged(isShownInMultiWindow, data);
921 }
922
923 void DeclarativeFrontend::OnSaveAbilityState(std::string& data)
924 {
925 if (delegate_) {
926 delegate_->OnSaveAbilityState(data);
927 }
928 }
929
930 void DeclarativeFrontend::OnRestoreAbilityState(const std::string& data)
931 {
932 if (delegate_) {
933 delegate_->OnRestoreAbilityState(data);
934 }
935 }
936
937 void DeclarativeFrontend::OnNewWant(const std::string& data)
938 {
939 if (delegate_) {
940 delegate_->OnNewWant(data);
941 }
942 }
943
944 RefPtr<AccessibilityManager> DeclarativeFrontend::GetAccessibilityManager() const
945 {
946 if (!delegate_) {
947 return nullptr;
948 }
949 return delegate_->GetJSAccessibilityManager();
950 }
951
952 WindowConfig& DeclarativeFrontend::GetWindowConfig()
953 {
954 if (!delegate_) {
955 static WindowConfig windowConfig;
956 return windowConfig;
957 }
958 return delegate_->GetWindowConfig();
959 }
960
961 bool DeclarativeFrontend::OnBackPressed()
962 {
963 if (!delegate_) {
964 return false;
965 }
966 return delegate_->OnPageBackPress();
967 }
968
969 void DeclarativeFrontend::OnShow()
970 {
971 if (delegate_) {
972 foregroundFrontend_ = true;
973 delegate_->OnForeground();
974 }
975 }
976
977 void DeclarativeFrontend::OnHide()
978 {
979 if (delegate_) {
980 delegate_->OnBackGround();
981 foregroundFrontend_ = false;
982 }
983 }
984
985 void DeclarativeFrontend::OnConfigurationUpdated(const std::string& data)
986 {
987 if (delegate_) {
988 delegate_->OnConfigurationUpdated(data);
989 }
990 }
991
992 void DeclarativeFrontend::OnActive()
993 {
994 if (delegate_) {
995 foregroundFrontend_ = true;
996 delegate_->InitializeAccessibilityCallback();
997 }
998 }
999
1000 void DeclarativeFrontend::OnInactive() {}
1001
1002 bool DeclarativeFrontend::OnStartContinuation()
1003 {
1004 if (!delegate_) {
1005 return false;
1006 }
1007 return delegate_->OnStartContinuation();
1008 }
1009
1010 void DeclarativeFrontend::OnCompleteContinuation(int32_t code)
1011 {
1012 if (delegate_) {
1013 delegate_->OnCompleteContinuation(code);
1014 }
1015 }
1016
1017 void DeclarativeFrontend::OnMemoryLevel(const int32_t level)
1018 {
1019 if (delegate_) {
1020 delegate_->OnMemoryLevel(level);
1021 }
1022 }
1023
1024 void DeclarativeFrontend::OnSaveData(std::string& data)
1025 {
1026 if (delegate_) {
1027 delegate_->OnSaveData(data);
1028 }
1029 }
1030
1031 void DeclarativeFrontend::GetPluginsUsed(std::string& data)
1032 {
1033 if (delegate_) {
1034 delegate_->GetPluginsUsed(data);
1035 }
1036 }
1037
1038 bool DeclarativeFrontend::OnRestoreData(const std::string& data)
1039 {
1040 if (!delegate_) {
1041 return false;
1042 }
1043 return delegate_->OnRestoreData(data);
1044 }
1045
1046 void DeclarativeFrontend::OnRemoteTerminated()
1047 {
1048 if (delegate_) {
1049 delegate_->OnRemoteTerminated();
1050 }
1051 }
1052
1053 void DeclarativeFrontend::OnNewRequest(const std::string& data)
1054 {
1055 if (delegate_) {
1056 delegate_->OnNewRequest(data);
1057 }
1058 }
1059
1060 void DeclarativeFrontend::CallRouterBack()
1061 {
1062 if (delegate_) {
1063 if (delegate_->GetStackSize() == 1 && isSubWindow_) {
1064 return;
1065 }
1066 delegate_->CallPopPage();
1067 }
1068 }
1069
1070 void DeclarativeFrontend::OnSurfaceChanged(int32_t width, int32_t height)
1071 {
1072 if (delegate_) {
1073 delegate_->OnSurfaceChanged();
1074 }
1075 }
1076
1077 void DeclarativeFrontend::OnLayoutCompleted(const std::string& componentId)
1078 {
1079 if (delegate_) {
1080 delegate_->OnLayoutCompleted(componentId);
1081 }
1082 }
1083
1084 void DeclarativeFrontend::OnDrawCompleted(const std::string& componentId)
1085 {
1086 if (delegate_) {
1087 delegate_->OnDrawCompleted(componentId);
1088 }
1089 }
1090
1091 void DeclarativeFrontend::HotReload()
1092 {
1093 auto manager = GetPageRouterManager();
1094 CHECK_NULL_VOID(manager);
1095 manager->FlushFrontend();
1096 }
1097
1098 void DeclarativeFrontend::FlushReload()
1099 {
1100 if (jsEngine_) {
1101 jsEngine_->FlushReload();
1102 }
1103 }
1104
1105 void DeclarativeFrontend::DumpFrontend() const
1106 {
1107 if (!delegate_ || !DumpLog::GetInstance().GetDumpFile()) {
1108 return;
1109 }
1110
1111 bool unrestore = false;
1112 std::string name;
1113 std::string path;
1114 std::string params;
1115 int32_t stackSize = delegate_->GetStackSize();
1116 DumpLog::GetInstance().Print(0, "Router stack size " + std::to_string(stackSize));
1117 for (int32_t i = 1; i <= stackSize; ++i) {
1118 delegate_->GetRouterStateByIndex(i, name, path, params);
1119 unrestore = delegate_->IsUnrestoreByIndex(i);
1120 DumpLog::GetInstance().Print(1, "Page[" + std::to_string(i) + "], name: " + name);
1121 DumpLog::GetInstance().Print(2, "Path: " + path);
1122 DumpLog::GetInstance().Print(2, "Params: " + params);
1123 DumpLog::GetInstance().Print(2, "Unrestore: " + std::string(unrestore ? "yes" : "no"));
1124 if (i != stackSize) {
1125 continue;
1126 }
1127 DumpLog::GetInstance().Print(2, "Components: " + std::to_string(delegate_->GetComponentsCount()));
1128 }
1129 }
1130
1131 std::string DeclarativeFrontend::GetPagePath() const
1132 {
1133 if (!delegate_) {
1134 return "";
1135 }
1136 int32_t routerIndex = 0;
1137 std::string routerName;
1138 std::string routerPath;
1139 delegate_->GetState(routerIndex, routerName, routerPath);
1140 return routerPath + routerName;
1141 }
1142
1143 void DeclarativeFrontend::TriggerGarbageCollection()
1144 {
1145 if (jsEngine_) {
1146 jsEngine_->RunGarbageCollection();
1147 }
1148 }
1149
1150 void DeclarativeFrontend::DumpHeapSnapshot(bool isPrivate)
1151 {
1152 if (jsEngine_) {
1153 jsEngine_->DumpHeapSnapshot(isPrivate);
1154 }
1155 }
1156
1157 void DeclarativeFrontend::NotifyUIIdle()
1158 {
1159 if (jsEngine_) {
1160 jsEngine_->NotifyUIIdle();
1161 }
1162 }
1163
1164 void DeclarativeFrontend::SetColorMode(ColorMode colorMode)
1165 {
1166 if (delegate_) {
1167 delegate_->SetColorMode(colorMode);
1168 }
1169 }
1170
1171 void DeclarativeFrontend::RebuildAllPages()
1172 {
1173 if (delegate_) {
1174 delegate_->RebuildAllPages();
1175 }
1176 }
1177
1178 void DeclarativeFrontend::NotifyAppStorage(const std::string& key, const std::string& value)
1179 {
1180 if (!delegate_) {
1181 return;
1182 }
1183 delegate_->NotifyAppStorage(jsEngine_, key, value);
1184 }
1185
1186 std::string DeclarativeFrontend::GetPagePathByUrl(const std::string& url) const
1187 {
1188 if (!delegate_) {
1189 return "";
1190 }
1191 return delegate_->GetPagePathByUrl(url);
1192 }
1193
1194 void DeclarativeEventHandler::HandleAsyncEvent(const EventMarker& eventMarker)
1195 {
1196 LOGI("HandleAsyncEvent pageId: %{private}d, eventId: %{private}s, eventType: %{private}s",
1197 eventMarker.GetData().pageId, eventMarker.GetData().eventId.c_str(), eventMarker.GetData().eventType.c_str());
1198 std::string param = eventMarker.GetData().GetEventParam();
1199 if (eventMarker.GetData().isDeclarativeUi) {
1200 if (delegate_) {
1201 delegate_->GetUiTask().PostTask([eventMarker] { eventMarker.CallUiFunction(); }, "ArkUICallUiFunction");
1202 }
1203 } else {
1204 delegate_->FireAsyncEvent(eventMarker.GetData().eventId, param.append("null"), std::string(""));
1205 }
1206
1207 AccessibilityEvent accessibilityEvent;
1208 accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
1209 accessibilityEvent.eventType = eventMarker.GetData().eventType;
1210 delegate_->FireAccessibilityEvent(accessibilityEvent);
1211 }
1212
1213 void DeclarativeEventHandler::HandleAsyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info)
1214 {
1215 std::string eventParam;
1216 if (eventMarker.GetData().eventType.find("touch") != std::string::npos) {
1217 TouchInfoToString(info, eventParam);
1218 } else if (eventMarker.GetData().eventType.find("mouse") != std::string::npos) {
1219 MouseInfoToString(info, eventParam);
1220 } else if (eventMarker.GetData().eventType == "swipe") {
1221 SwipeInfoToString(info, eventParam);
1222 }
1223 std::string param = eventMarker.GetData().GetEventParam();
1224 if (eventParam.empty()) {
1225 param.append("null");
1226 } else {
1227 param.append(eventParam);
1228 }
1229
1230 if (eventMarker.GetData().isDeclarativeUi) {
1231 if (delegate_) {
1232 auto cinfo = CopyEventInfo(info);
1233 delegate_->GetUiTask().PostTask(
1234 [eventMarker, cinfo] { eventMarker.CallUiArgFunction(cinfo.get()); }, "ArkUICallUiArgFunction");
1235 }
1236 } else {
1237 delegate_->FireAsyncEvent(eventMarker.GetData().eventId, param, "");
1238 }
1239
1240 AccessibilityEvent accessibilityEvent;
1241 accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
1242 accessibilityEvent.eventType = eventMarker.GetData().eventType;
1243 delegate_->FireAccessibilityEvent(accessibilityEvent);
1244 }
1245
1246 void DeclarativeEventHandler::HandleAsyncEvent(
1247 const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info)
1248 {
1249 if (eventMarker.GetData().isDeclarativeUi) {
1250 if (delegate_) {
1251 delegate_->GetUiTask().PostTask(
1252 [eventMarker, info] { eventMarker.CallUiArgFunction(info.get()); }, "ArkUICallUiArgFunction");
1253 }
1254 }
1255 }
1256
1257 void DeclarativeEventHandler::HandleSyncEvent(const EventMarker& eventMarker, const KeyEvent& info, bool& result)
1258 {
1259 std::string param = std::string("\"")
1260 .append(eventMarker.GetData().eventType)
1261 .append("\",{\"code\":")
1262 .append(std::to_string(static_cast<int32_t>(info.code)))
1263 .append(",\"action\":")
1264 .append(std::to_string(static_cast<int32_t>(info.action)))
1265 .append(",\"repeatCount\":")
1266 .append(std::to_string(static_cast<int32_t>(info.repeatTime)))
1267 .append(",\"timestamp\":")
1268 .append(std::to_string(info.timeStamp.time_since_epoch().count()))
1269 .append(",\"key\":\"")
1270 .append(info.key)
1271 .append("\"},");
1272
1273 result = delegate_->FireSyncEvent(eventMarker.GetData().eventId, param, "");
1274
1275 AccessibilityEvent accessibilityEvent;
1276 accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
1277 accessibilityEvent.eventType = std::to_string(static_cast<int32_t>(info.code));
1278 delegate_->FireAccessibilityEvent(accessibilityEvent);
1279 }
1280
1281 void DeclarativeEventHandler::HandleAsyncEvent(const EventMarker& eventMarker, int32_t param)
1282 {
1283 AccessibilityEvent accessibilityEvent;
1284 accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
1285 accessibilityEvent.eventType = eventMarker.GetData().eventType;
1286 delegate_->FireAccessibilityEvent(accessibilityEvent);
1287 }
1288
1289 void DeclarativeEventHandler::HandleAsyncEvent(const EventMarker& eventMarker, const KeyEvent& info)
1290 {
1291 AccessibilityEvent accessibilityEvent;
1292 accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
1293 accessibilityEvent.eventType = eventMarker.GetData().eventType;
1294 delegate_->FireAccessibilityEvent(accessibilityEvent);
1295 }
1296
1297 void DeclarativeEventHandler::HandleAsyncEvent(const EventMarker& eventMarker, const std::string& param)
1298 {
1299 if (eventMarker.GetData().isDeclarativeUi) {
1300 std::string fixParam(param);
1301 std::string::size_type startPos = param.find_first_of("{");
1302 std::string::size_type endPos = param.find_last_of("}");
1303 if (startPos != std::string::npos && endPos != std::string::npos && startPos < endPos) {
1304 fixParam = fixParam.substr(startPos, endPos - startPos + 1);
1305 }
1306 if (delegate_) {
1307 delegate_->GetUiTask().PostTask(
1308 [eventMarker, fixParam] { eventMarker.CallUiStrFunction(fixParam); }, "ArkUICallUiStrFunction");
1309 }
1310 } else {
1311 delegate_->FireAsyncEvent(eventMarker.GetData().eventId, param, "");
1312 }
1313
1314 AccessibilityEvent accessibilityEvent;
1315 accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
1316 accessibilityEvent.eventType = eventMarker.GetData().eventType;
1317 delegate_->FireAccessibilityEvent(accessibilityEvent);
1318 }
1319
1320 void DeclarativeEventHandler::HandleSyncEvent(const EventMarker& eventMarker, bool& result)
1321 {
1322 AccessibilityEvent accessibilityEvent;
1323 accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
1324 accessibilityEvent.eventType = eventMarker.GetData().eventType;
1325 delegate_->FireAccessibilityEvent(accessibilityEvent);
1326 }
1327
1328 void DeclarativeEventHandler::HandleSyncEvent(
1329 const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info)
1330 {
1331 if (delegate_) {
1332 delegate_->GetUiTask().PostSyncTask(
1333 [eventMarker, info] { eventMarker.CallUiArgFunction(info.get()); }, "ArkUICallUiArgFunction");
1334 }
1335 }
1336
1337 void DeclarativeEventHandler::HandleSyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info, bool& result)
1338 {
1339 AccessibilityEvent accessibilityEvent;
1340 accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
1341 accessibilityEvent.eventType = eventMarker.GetData().eventType;
1342 delegate_->FireAccessibilityEvent(accessibilityEvent);
1343 }
1344
1345 void DeclarativeEventHandler::HandleSyncEvent(
1346 const EventMarker& eventMarker, const std::string& param, std::string& result)
1347 {
1348 AccessibilityEvent accessibilityEvent;
1349 accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
1350 accessibilityEvent.eventType = eventMarker.GetData().eventType;
1351 delegate_->FireAccessibilityEvent(accessibilityEvent);
1352 delegate_->FireSyncEvent(eventMarker.GetData().eventId, param, std::string(""), result);
1353 }
1354
1355 void DeclarativeEventHandler::HandleSyncEvent(
1356 const EventMarker& eventMarker, const std::string& componentId, const int32_t nodeId, const bool isDestroy)
1357 {
1358 if (delegate_) {
1359 delegate_->FireExternalEvent(eventMarker.GetData().eventId, componentId, nodeId, isDestroy);
1360 }
1361 }
1362
1363 } // namespace OHOS::Ace
1364