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