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