• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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/plugin_frontend/plugin_frontend.h"
17 
18 #include "base/log/dump_log.h"
19 #include "base/log/event_report.h"
20 
21 namespace OHOS::Ace {
22 namespace {
23 /*
24  * NOTE:
25  * This function is needed to copy the values from BaseEventInfo
26  * It is observed, that the owner of BaseEventInfo will delete the pointer before it is ultimately
27  * processed by the EventMarker callback. In order to avoid this, a copy of all data needs to be made.
28  */
CopyEventInfo(const BaseEventInfo & info)29 std::shared_ptr<BaseEventInfo> CopyEventInfo(const BaseEventInfo& info)
30 {
31     const auto* touchInfo = TypeInfoHelper::DynamicCast<TouchEventInfo>(&info);
32     if (touchInfo != nullptr) {
33         return std::make_shared<TouchEventInfo>(*touchInfo);
34     }
35 
36     const auto* dragStartInfo = TypeInfoHelper::DynamicCast<DragStartInfo>(&info);
37     if (dragStartInfo != nullptr) {
38         return std::make_shared<DragStartInfo>(*dragStartInfo);
39     }
40 
41     const auto* dragUpdateInfo = TypeInfoHelper::DynamicCast<DragUpdateInfo>(&info);
42     if (dragUpdateInfo != nullptr) {
43         return std::make_shared<DragUpdateInfo>(*dragUpdateInfo);
44     }
45 
46     const auto* dragEndInfo = TypeInfoHelper::DynamicCast<DragEndInfo>(&info);
47     if (dragEndInfo != nullptr) {
48         return std::make_shared<DragEndInfo>(*dragEndInfo);
49     }
50 
51     const auto* clickInfo = TypeInfoHelper::DynamicCast<ClickInfo>(&info);
52     if (clickInfo != nullptr) {
53         return std::make_shared<ClickInfo>(*clickInfo);
54     }
55     return nullptr;
56 }
57 
TouchInfoToString(const BaseEventInfo & info,std::string & eventParam)58 void TouchInfoToString(const BaseEventInfo& info, std::string& eventParam)
59 {
60     eventParam.append("{\"touches\":[{");
61     const auto touchInfo = TypeInfoHelper::DynamicCast<TouchEventInfo>(&info);
62     if (touchInfo) {
63         auto touchList = touchInfo->GetTouches();
64         for (const auto& location : touchList) {
65             auto globalLocation = location.GetGlobalLocation();
66             eventParam.append("\"globalX\":")
67                 .append(std::to_string(globalLocation.GetX()))
68                 .append(",\"globalY\":")
69                 .append(std::to_string(globalLocation.GetY()))
70                 .append(",");
71             auto localLocation = location.GetLocalLocation();
72             eventParam.append("\"localX\":")
73                 .append(std::to_string(localLocation.GetX()))
74                 .append(",\"localY\":")
75                 .append(std::to_string(localLocation.GetY()))
76                 .append(",");
77             eventParam.append("\"size\":").append(std::to_string(location.GetSize())).append(",");
78         }
79         if (eventParam.back() == ',') {
80             eventParam.pop_back();
81         }
82         eventParam.append("}],\"changedTouches\":[{");
83         auto changeTouch = touchInfo->GetChangedTouches();
84         for (const auto& change : changeTouch) {
85             auto globalLocation = change.GetGlobalLocation();
86             eventParam.append("\"globalX\":")
87                 .append(std::to_string(globalLocation.GetX()))
88                 .append(",\"globalY\":")
89                 .append(std::to_string(globalLocation.GetY()))
90                 .append(",");
91             auto localLocation = change.GetLocalLocation();
92             eventParam.append("\"localX\":")
93                 .append(std::to_string(localLocation.GetX()))
94                 .append(",\"localY\":")
95                 .append(std::to_string(localLocation.GetY()))
96                 .append(",");
97             eventParam.append("\"size\":").append(std::to_string(change.GetSize())).append(",");
98         }
99         if (eventParam.back() == ',') {
100             eventParam.pop_back();
101         }
102     }
103     eventParam.append("}]}");
104 }
105 
MouseInfoToString(const BaseEventInfo & info,std::string & eventParam)106 void MouseInfoToString(const BaseEventInfo& info, std::string& eventParam)
107 {
108     const auto mouseInfo = TypeInfoHelper::DynamicCast<MouseEventInfo>(&info);
109     eventParam.append("{\"mouse\":{");
110     if (mouseInfo) {
111         auto globalMouse = mouseInfo->GetGlobalMouse();
112         eventParam.append("\"globalX\":")
113             .append(std::to_string(globalMouse.x))
114             .append(",\"globalY\":")
115             .append(std::to_string(globalMouse.y))
116             .append(",\"globalZ\":")
117             .append(std::to_string(globalMouse.z))
118             .append(",\"localX\":")
119             .append(std::to_string(globalMouse.x))
120             .append(",\"localY\":")
121             .append(std::to_string(globalMouse.y))
122             .append(",\"localZ\":")
123             .append(std::to_string(globalMouse.z))
124             .append(",\"deltaX\":")
125             .append(std::to_string(globalMouse.deltaX))
126             .append(",\"deltaY\":")
127             .append(std::to_string(globalMouse.deltaY))
128             .append(",\"deltaZ\":")
129             .append(std::to_string(globalMouse.deltaZ))
130             .append(",\"scrollX\":")
131             .append(std::to_string(globalMouse.scrollX))
132             .append(",\"scrollY\":")
133             .append(std::to_string(globalMouse.scrollY))
134             .append(",\"scrollZ\":")
135             .append(std::to_string(globalMouse.scrollZ))
136             .append(",\"action\":")
137             .append(std::to_string(static_cast<int32_t>(globalMouse.action)))
138             .append(",\"button\":")
139             .append(std::to_string(static_cast<int32_t>(globalMouse.button)))
140             .append(",\"pressedButtons\":")
141             .append(std::to_string(globalMouse.pressedButtons));
142     }
143     eventParam.append("}}");
144 }
145 
SwipeInfoToString(const BaseEventInfo & info,std::string & eventParam)146 void SwipeInfoToString(const BaseEventInfo& info, std::string& eventParam)
147 {
148     const auto& swipeInfo = TypeInfoHelper::DynamicCast<SwipeEventInfo>(&info);
149     CHECK_NULL_VOID(swipeInfo);
150     eventParam = swipeInfo->ToJsonParamInfo();
151 }
152 } // namespace
153 
~PluginFrontend()154 PluginFrontend::~PluginFrontend() noexcept
155 {
156     Destroy();
157     TAG_LOGI(AceLogTag::ACE_PLUGIN_COMPONENT, "Plugin frontend destroyed");
158 }
159 
Destroy()160 void PluginFrontend::Destroy()
161 {
162     CHECK_RUN_ON(JS);
163     // To guarantee the jsEngine_ and delegate_ released in js thread
164     if (jsEngine_) {
165         jsEngine_->Destroy();
166         jsEngine_.Reset();
167     }
168     delegate_.Reset();
169     handler_.Reset();
170 }
171 
Initialize(FrontendType type,const RefPtr<TaskExecutor> & taskExecutor)172 bool PluginFrontend::Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor)
173 {
174     type_ = type;
175     taskExecutor_ = taskExecutor;
176     ACE_DCHECK(type_ == FrontendType::JS_PLUGIN);
177     InitializeFrontendDelegate(taskExecutor);
178     taskExecutor->PostSyncTask(
179         [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_), delegate = delegate_] {
180             auto jsEngine = weakEngine.Upgrade();
181             CHECK_NULL_VOID(jsEngine);
182             jsEngine->Initialize(delegate);
183         },
184         TaskExecutor::TaskType::JS, "ArkUIPluginInitialize");
185 
186     return true;
187 }
188 
AttachPipelineContext(const RefPtr<PipelineBase> & context)189 void PluginFrontend::AttachPipelineContext(const RefPtr<PipelineBase>& context)
190 {
191     CHECK_NULL_VOID(delegate_);
192     handler_ = AceType::MakeRefPtr<PluginEventHandler>(delegate_);
193     auto pipelineContext = AceType::DynamicCast<PipelineContext>(context);
194     if (pipelineContext) {
195         pipelineContext->RegisterEventHandler(handler_);
196     }
197     delegate_->AttachPipelineContext(context);
198 }
199 
SetAssetManager(const RefPtr<AssetManager> & assetManager)200 void PluginFrontend::SetAssetManager(const RefPtr<AssetManager>& assetManager)
201 {
202     if (delegate_) {
203         delegate_->SetAssetManager(assetManager);
204     }
205 }
206 
InitializeFrontendDelegate(const RefPtr<TaskExecutor> & taskExecutor)207 void PluginFrontend::InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor)
208 {
209     const auto& loadCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](const std::string& url,
210                                    const RefPtr<Framework::JsAcePage>& jsPage, bool isMainPage) {
211         auto jsEngine = weakEngine.Upgrade();
212         CHECK_NULL_VOID(jsEngine);
213         jsEngine->LoadPluginComponent(url, jsPage, isMainPage);
214     };
215 
216     const auto& setPluginMessageTransferCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
217                                                        const RefPtr<JsMessageDispatcher>& dispatcher) {
218         auto jsEngine = weakEngine.Upgrade();
219         CHECK_NULL_VOID(jsEngine);
220         jsEngine->SetJsMessageDispatcher(dispatcher);
221     };
222 
223     const auto& asyncEventCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
224                                          const std::string& eventId, const std::string& param) {
225         auto jsEngine = weakEngine.Upgrade();
226         CHECK_NULL_VOID(jsEngine);
227         jsEngine->FireAsyncEvent(eventId, param);
228     };
229 
230     const auto& syncEventCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
231                                         const std::string& eventId, const std::string& param) {
232         auto jsEngine = weakEngine.Upgrade();
233         CHECK_NULL_VOID(jsEngine);
234         jsEngine->FireSyncEvent(eventId, param);
235     };
236 
237     const auto& updatePageCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
238                                          const RefPtr<Framework::JsAcePage>& jsPage) {
239         auto jsEngine = weakEngine.Upgrade();
240         CHECK_NULL_VOID(jsEngine);
241         jsEngine->UpdateRunningPage(jsPage);
242         jsEngine->UpdateStagingPage(jsPage);
243     };
244 
245     const auto& resetStagingPageCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() {
246         auto jsEngine = weakEngine.Upgrade();
247         CHECK_NULL_VOID(jsEngine);
248         jsEngine->ResetStagingPage();
249     };
250 
251     const auto& destroyPageCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](int32_t pageId) {
252         auto jsEngine = weakEngine.Upgrade();
253         CHECK_NULL_VOID(jsEngine);
254         jsEngine->DestroyPageInstance(pageId);
255     };
256 
257     const auto& destroyApplicationCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
258                                                  const std::string& packageName) {
259         auto jsEngine = weakEngine.Upgrade();
260         CHECK_NULL_VOID(jsEngine);
261         jsEngine->DestroyApplication(packageName);
262     };
263 
264     const auto& updateApplicationStateCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
265                                                      const std::string& packageName, Frontend::State state) {
266         auto jsEngine = weakEngine.Upgrade();
267         CHECK_NULL_VOID(jsEngine);
268         jsEngine->UpdateApplicationState(packageName, state);
269     };
270 
271     const auto& onWindowDisplayModeChangedCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
272                                                          bool isShownInMultiWindow, const std::string& data) {
273         auto jsEngine = weakEngine.Upgrade();
274         CHECK_NULL_VOID(jsEngine);
275         jsEngine->OnWindowDisplayModeChanged(isShownInMultiWindow, data);
276     };
277 
278     const auto& onSaveAbilityStateCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](std::string& data) {
279         auto jsEngine = weakEngine.Upgrade();
280         CHECK_NULL_VOID(jsEngine);
281         jsEngine->OnSaveAbilityState(data);
282     };
283     const auto& onRestoreAbilityStateCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
284                                                     const std::string& data) {
285         auto jsEngine = weakEngine.Upgrade();
286         CHECK_NULL_VOID(jsEngine);
287         jsEngine->OnRestoreAbilityState(data);
288     };
289 
290     const auto& onNewWantCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](const std::string& data) {
291         auto jsEngine = weakEngine.Upgrade();
292         CHECK_NULL_VOID(jsEngine);
293         jsEngine->OnNewWant(data);
294     };
295     const auto& onActiveCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() {
296         auto jsEngine = weakEngine.Upgrade();
297         CHECK_NULL_VOID(jsEngine);
298         jsEngine->OnActive();
299     };
300 
301     const auto& onInactiveCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() {
302         auto jsEngine = weakEngine.Upgrade();
303         CHECK_NULL_VOID(jsEngine);
304         jsEngine->OnInactive();
305     };
306 
307     const auto& onConfigurationUpdatedCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
308                                                      const std::string& data) {
309         auto jsEngine = weakEngine.Upgrade();
310         CHECK_NULL_VOID(jsEngine);
311         jsEngine->OnConfigurationUpdated(data);
312     };
313 
314     const auto& timerCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
315                                     const std::string& callbackId, const std::string& delay, bool isInterval) {
316         auto jsEngine = weakEngine.Upgrade();
317         CHECK_NULL_VOID(jsEngine);
318         jsEngine->TimerCallback(callbackId, delay, isInterval);
319     };
320 
321     const auto& mediaQueryCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
322                                          const std::string& callbackId, const std::string& args) {
323         auto jsEngine = weakEngine.Upgrade();
324         CHECK_NULL_VOID(jsEngine);
325         jsEngine->MediaQueryCallback(callbackId, args);
326     };
327 
328     const auto& requestAnimationCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
329                                                const std::string& callbackId, uint64_t timeStamp) {
330         auto jsEngine = weakEngine.Upgrade();
331         CHECK_NULL_VOID(jsEngine);
332         jsEngine->RequestAnimationCallback(callbackId, timeStamp);
333     };
334 
335     const auto& jsCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
336                                  const std::string& callbackId, const std::string& args) {
337         auto jsEngine = weakEngine.Upgrade();
338         CHECK_NULL_VOID(jsEngine);
339         jsEngine->JsCallback(callbackId, args);
340     };
341 
342     const auto& onMemoryLevelCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](const int32_t level) {
343         auto jsEngine = weakEngine.Upgrade();
344         CHECK_NULL_VOID(jsEngine);
345         jsEngine->OnMemoryLevel(level);
346     };
347 
348     const auto& onStartContinuationCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() -> bool {
349         auto jsEngine = weakEngine.Upgrade();
350         CHECK_NULL_RETURN(jsEngine, false);
351         return jsEngine->OnStartContinuation();
352     };
353     const auto& onCompleteContinuationCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](int32_t code) {
354         auto jsEngine = weakEngine.Upgrade();
355         CHECK_NULL_VOID(jsEngine);
356         jsEngine->OnCompleteContinuation(code);
357     };
358     const auto& onRemoteTerminatedCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() {
359         auto jsEngine = weakEngine.Upgrade();
360         CHECK_NULL_VOID(jsEngine);
361         jsEngine->OnRemoteTerminated();
362     };
363     const auto& onSaveDataCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](std::string& savedData) {
364         auto jsEngine = weakEngine.Upgrade();
365         CHECK_NULL_VOID(jsEngine);
366         jsEngine->OnSaveData(savedData);
367     };
368     const auto& onRestoreDataCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
369                                             const std::string& data) -> bool {
370         auto jsEngine = weakEngine.Upgrade();
371         CHECK_NULL_RETURN(jsEngine, false);
372         return jsEngine->OnRestoreData(data);
373     };
374     delegate_ = AceType::MakeRefPtr<Framework::PluginFrontendDelegate>(taskExecutor, loadCallback,
375         setPluginMessageTransferCallback, asyncEventCallback, syncEventCallback, updatePageCallback,
376         resetStagingPageCallback, destroyPageCallback, destroyApplicationCallback, updateApplicationStateCallback,
377         timerCallback, mediaQueryCallback, requestAnimationCallback, jsCallback, onWindowDisplayModeChangedCallBack,
378         onConfigurationUpdatedCallBack, onSaveAbilityStateCallBack, onRestoreAbilityStateCallBack, onNewWantCallBack,
379         onActiveCallBack, onInactiveCallBack, onMemoryLevelCallBack, onStartContinuationCallBack,
380         onCompleteContinuationCallBack, onRemoteTerminatedCallBack, onSaveDataCallBack, onRestoreDataCallBack);
381     if (disallowPopLastPage_) {
382         delegate_->DisallowPopLastPage();
383     }
384     if (jsEngine_) {
385         delegate_->SetGroupJsBridge(jsEngine_->GetGroupJsBridge());
386     } else {
387         LOGE("the js engine is nullptr");
388         EventReport::SendAppStartException(AppStartExcepType::JS_ENGINE_CREATE_ERR);
389     }
390 }
391 
RunPage(const std::string & url,const std::string & params)392 UIContentErrorCode PluginFrontend::RunPage(const std::string& url, const std::string& params)
393 {
394     // Not use this pageId from backend, manage it in PluginFrontendDelegate.
395     CHECK_NULL_RETURN(delegate_, UIContentErrorCode::NULL_POINTER);
396     return delegate_->RunPage(url, params);
397 }
398 
ReplacePage(const std::string & url,const std::string & params)399 void PluginFrontend::ReplacePage(const std::string& url, const std::string& params)
400 {
401     CHECK_NULL_VOID(delegate_);
402     delegate_->Replace(url, params);
403 }
404 
PushPage(const std::string & url,const std::string & params)405 void PluginFrontend::PushPage(const std::string& url, const std::string& params)
406 {
407     CHECK_NULL_VOID(delegate_);
408     delegate_->Push(url, params);
409 }
410 
411 // navigator component call router
NavigatePage(uint8_t type,const PageTarget & target,const std::string & params)412 void PluginFrontend::NavigatePage(uint8_t type, const PageTarget& target, const std::string& params)
413 {
414     CHECK_NULL_VOID(delegate_);
415     switch (static_cast<NavigatorType>(type)) {
416         case NavigatorType::PUSH:
417             delegate_->Push(target, params);
418             break;
419         case NavigatorType::REPLACE:
420             delegate_->Replace(target, params);
421             break;
422         case NavigatorType::BACK:
423             delegate_->BackWithTarget(target, params);
424             break;
425         default:
426             LOGE("Navigator type is invalid!");
427             delegate_->BackWithTarget(target, params);
428     }
429 }
430 
SendCallbackMessage(const std::string & callbackId,const std::string & data) const431 void PluginFrontend::SendCallbackMessage(const std::string& callbackId, const std::string& data) const
432 {
433     CHECK_NULL_VOID(delegate_);
434     delegate_->OnJSCallback(callbackId, data);
435 }
436 
SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher> & dispatcher) const437 void PluginFrontend::SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const
438 {
439     CHECK_NULL_VOID(delegate_);
440     delegate_->SetJsMessageDispatcher(dispatcher);
441 }
442 
TransferComponentResponseData(int callbackId,int32_t code,std::vector<uint8_t> && data) const443 void PluginFrontend::TransferComponentResponseData(int callbackId, int32_t code, std::vector<uint8_t>&& data) const
444 {
445     CHECK_NULL_VOID(delegate_);
446     delegate_->TransferComponentResponseData(callbackId, code, std::move(data));
447 }
448 
TransferJsResponseData(int callbackId,int32_t code,std::vector<uint8_t> && data) const449 void PluginFrontend::TransferJsResponseData(int callbackId, int32_t code, std::vector<uint8_t>&& data) const
450 {
451     CHECK_NULL_VOID(delegate_);
452     delegate_->TransferJsResponseData(callbackId, code, std::move(data));
453 }
454 
455 #if defined(PREVIEW)
TransferJsResponseDataPreview(int callbackId,int32_t code,ResponseData responseData) const456 void PluginFrontend::TransferJsResponseDataPreview(int callbackId, int32_t code, ResponseData responseData) const
457 {
458     delegate_->TransferJsResponseDataPreview(callbackId, code, responseData);
459 }
460 #endif
461 
TransferJsPluginGetError(int callbackId,int32_t errorCode,std::string && errorMessage) const462 void PluginFrontend::TransferJsPluginGetError(int callbackId, int32_t errorCode, std::string&& errorMessage) const
463 {
464     CHECK_NULL_VOID(delegate_);
465     delegate_->TransferJsPluginGetError(callbackId, errorCode, std::move(errorMessage));
466 }
467 
TransferJsEventData(int32_t callbackId,int32_t code,std::vector<uint8_t> && data) const468 void PluginFrontend::TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const
469 {
470     CHECK_NULL_VOID(delegate_);
471     delegate_->TransferJsEventData(callbackId, code, std::move(data));
472 }
473 
LoadPluginJsCode(std::string && jsCode) const474 void PluginFrontend::LoadPluginJsCode(std::string&& jsCode) const
475 {
476     CHECK_NULL_VOID(delegate_);
477     delegate_->LoadPluginJsCode(std::move(jsCode));
478 }
479 
LoadPluginJsByteCode(std::vector<uint8_t> && jsCode,std::vector<int32_t> && jsCodeLen) const480 void PluginFrontend::LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const
481 {
482     CHECK_NULL_VOID(delegate_);
483     delegate_->LoadPluginJsByteCode(std::move(jsCode), std::move(jsCodeLen));
484 }
485 
UpdateState(Frontend::State state)486 void PluginFrontend::UpdateState(Frontend::State state)
487 {
488     CHECK_NULL_VOID(delegate_);
489     switch (state) {
490         case Frontend::State::ON_CREATE:
491             break;
492         case Frontend::State::ON_SHOW:
493         case Frontend::State::ON_HIDE:
494             delegate_->UpdateApplicationState(delegate_->GetAppID(), state);
495             break;
496         case Frontend::State::ON_DESTROY:
497             delegate_->OnApplicationDestroy(delegate_->GetAppID());
498             break;
499         default:
500             LOGE("error State: %d", state);
501             break;
502     }
503 }
504 
OnWindowDisplayModeChanged(bool isShownInMultiWindow,const std::string & data)505 void PluginFrontend::OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data)
506 {
507     CHECK_NULL_VOID(delegate_);
508     delegate_->OnWindowDisplayModeChanged(isShownInMultiWindow, data);
509 }
510 
OnSaveAbilityState(std::string & data)511 void PluginFrontend::OnSaveAbilityState(std::string& data)
512 {
513     CHECK_NULL_VOID(delegate_);
514     delegate_->OnSaveAbilityState(data);
515 }
516 
OnRestoreAbilityState(const std::string & data)517 void PluginFrontend::OnRestoreAbilityState(const std::string& data)
518 {
519     CHECK_NULL_VOID(delegate_);
520     delegate_->OnRestoreAbilityState(data);
521 }
522 
OnNewWant(const std::string & data)523 void PluginFrontend::OnNewWant(const std::string& data)
524 {
525     CHECK_NULL_VOID(delegate_);
526     delegate_->OnNewWant(data);
527 }
528 
GetAccessibilityManager() const529 RefPtr<AccessibilityManager> PluginFrontend::GetAccessibilityManager() const
530 {
531     CHECK_NULL_RETURN(delegate_, nullptr);
532     return delegate_->GetJSAccessibilityManager();
533 }
534 
GetWindowConfig()535 WindowConfig& PluginFrontend::GetWindowConfig()
536 {
537     if (!delegate_) {
538         static WindowConfig windowConfig;
539         LOGW("delegate is null, return default config");
540         return windowConfig;
541     }
542     return delegate_->GetWindowConfig();
543 }
544 
OnBackPressed()545 bool PluginFrontend::OnBackPressed()
546 {
547     CHECK_NULL_RETURN(delegate_, false);
548     return delegate_->OnPageBackPress();
549 }
550 
OnShow()551 void PluginFrontend::OnShow()
552 {
553     CHECK_NULL_VOID(delegate_);
554     delegate_->OnForeground();
555 }
556 
OnHide()557 void PluginFrontend::OnHide()
558 {
559     CHECK_NULL_VOID(delegate_);
560     delegate_->OnBackGround();
561     foregroundFrontend_ = false;
562 }
563 
OnConfigurationUpdated(const std::string & data)564 void PluginFrontend::OnConfigurationUpdated(const std::string& data)
565 {
566     CHECK_NULL_VOID(delegate_);
567     delegate_->OnConfigurationUpdated(data);
568 }
569 
OnActive()570 void PluginFrontend::OnActive()
571 {
572     CHECK_NULL_VOID(delegate_);
573     foregroundFrontend_ = true;
574     delegate_->OnActive();
575     delegate_->InitializeAccessibilityCallback();
576 }
577 
OnInactive()578 void PluginFrontend::OnInactive()
579 {
580     CHECK_NULL_VOID(delegate_);
581     delegate_->OnInactive();
582     delegate_->OnSuspended();
583 }
584 
OnStartContinuation()585 bool PluginFrontend::OnStartContinuation()
586 {
587     CHECK_NULL_RETURN(delegate_, false);
588     return delegate_->OnStartContinuation();
589 }
590 
OnCompleteContinuation(int32_t code)591 void PluginFrontend::OnCompleteContinuation(int32_t code)
592 {
593     CHECK_NULL_VOID(delegate_);
594     delegate_->OnCompleteContinuation(code);
595 }
596 
OnMemoryLevel(const int32_t level)597 void PluginFrontend::OnMemoryLevel(const int32_t level)
598 {
599     CHECK_NULL_VOID(delegate_);
600     delegate_->OnMemoryLevel(level);
601 }
602 
OnSaveData(std::string & data)603 void PluginFrontend::OnSaveData(std::string& data)
604 {
605     CHECK_NULL_VOID(delegate_);
606     delegate_->OnSaveData(data);
607 }
608 
GetPluginsUsed(std::string & data)609 void PluginFrontend::GetPluginsUsed(std::string& data)
610 {
611     CHECK_NULL_VOID(delegate_);
612     delegate_->GetPluginsUsed(data);
613 }
614 
OnRestoreData(const std::string & data)615 bool PluginFrontend::OnRestoreData(const std::string& data)
616 {
617     CHECK_NULL_RETURN(delegate_, false);
618     return delegate_->OnRestoreData(data);
619 }
620 
OnRemoteTerminated()621 void PluginFrontend::OnRemoteTerminated()
622 {
623     CHECK_NULL_VOID(delegate_);
624     delegate_->OnRemoteTerminated();
625 }
626 
OnNewRequest(const std::string & data)627 void PluginFrontend::OnNewRequest(const std::string& data)
628 {
629     CHECK_NULL_VOID(delegate_);
630     delegate_->OnNewRequest(data);
631 }
632 
CallRouterBack()633 void PluginFrontend::CallRouterBack()
634 {
635     CHECK_NULL_VOID(delegate_);
636     if (delegate_->GetStackSize() == 1 && isSubWindow_) {
637         LOGW("Can't back because this is the last page of sub window!");
638         return;
639     }
640     delegate_->CallPopPage();
641 }
642 
OnSurfaceChanged(int32_t width,int32_t height)643 void PluginFrontend::OnSurfaceChanged(int32_t width, int32_t height)
644 {
645     CHECK_NULL_VOID(delegate_);
646     delegate_->OnSurfaceChanged();
647 }
648 
OnLayoutCompleted(const std::string & componentId)649 void PluginFrontend::OnLayoutCompleted(const std::string& componentId)
650 {
651     CHECK_NULL_VOID(delegate_);
652 }
653 
OnDrawCompleted(const std::string & componentId)654 void PluginFrontend::OnDrawCompleted(const std::string& componentId)
655 {
656     CHECK_NULL_VOID(delegate_);
657 }
658 
OnDrawChildrenCompleted(const std::string & componentId)659 void PluginFrontend::OnDrawChildrenCompleted(const std::string& componentId)
660 {
661     CHECK_NULL_VOID(delegate_);
662 }
663 
IsDrawChildrenCallbackFuncExist(const std::string & componentId)664 bool PluginFrontend::IsDrawChildrenCallbackFuncExist(const std::string& componentId)
665 {
666     return false;
667 }
668 
DumpFrontend() const669 void PluginFrontend::DumpFrontend() const
670 {
671     CHECK_NULL_VOID(delegate_);
672     int32_t routerIndex = 0;
673     std::string routerName;
674     std::string routerPath;
675     delegate_->GetState(routerIndex, routerName, routerPath);
676 
677     if (DumpLog::GetInstance().GetDumpFile()) {
678         DumpLog::GetInstance().AddDesc("Components: " + std::to_string(delegate_->GetComponentsCount()));
679         DumpLog::GetInstance().AddDesc("Path: " + routerPath);
680         DumpLog::GetInstance().AddDesc("Length: " + std::to_string(routerIndex));
681         DumpLog::GetInstance().Print(0, routerName, 0);
682     }
683 }
684 
GetPagePath() const685 std::string PluginFrontend::GetPagePath() const
686 {
687     CHECK_NULL_RETURN(delegate_, "");
688     int32_t routerIndex = 0;
689     std::string routerName;
690     std::string routerPath;
691     delegate_->GetState(routerIndex, routerName, routerPath);
692     return routerPath + routerName;
693 }
694 
TriggerGarbageCollection()695 void PluginFrontend::TriggerGarbageCollection()
696 {
697     CHECK_NULL_VOID(delegate_);
698     jsEngine_->RunGarbageCollection();
699 }
700 
SetColorMode(ColorMode colorMode)701 void PluginFrontend::SetColorMode(ColorMode colorMode)
702 {
703     CHECK_NULL_VOID(delegate_);
704     delegate_->SetColorMode(colorMode);
705 }
706 
RebuildAllPages()707 void PluginFrontend::RebuildAllPages()
708 {
709     CHECK_NULL_VOID(delegate_);
710     delegate_->RebuildAllPages();
711 }
712 
NotifyAppStorage(const std::string & key,const std::string & value)713 void PluginFrontend::NotifyAppStorage(const std::string& key, const std::string& value)
714 {
715     CHECK_NULL_VOID(delegate_);
716     delegate_->NotifyAppStorage(jsEngine_, key, value);
717 }
718 
UpdatePlugin(const std::string & content)719 void PluginFrontend::UpdatePlugin(const std::string& content)
720 {
721     CHECK_NULL_VOID(delegate_);
722     delegate_->UpdatePlugin(content);
723 }
724 
HandleAsyncEvent(const EventMarker & eventMarker)725 void PluginEventHandler::HandleAsyncEvent(const EventMarker& eventMarker)
726 {
727     std::string param = eventMarker.GetData().GetEventParam();
728     if (eventMarker.GetData().isDeclarativeUi) {
729         if (delegate_) {
730             delegate_->GetUiTask().PostTask(
731                 [eventMarker] { eventMarker.CallUiFunction(); }, "ArkUIPluginCallUiFunction");
732         }
733     } else {
734         delegate_->FireAsyncEvent(eventMarker.GetData().eventId, param.append("null"), std::string(""));
735     }
736 
737     AccessibilityEvent accessibilityEvent;
738     accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
739     accessibilityEvent.eventType = eventMarker.GetData().eventType;
740     delegate_->FireAccessibilityEvent(accessibilityEvent);
741 }
742 
HandleAsyncEvent(const EventMarker & eventMarker,const BaseEventInfo & info)743 void PluginEventHandler::HandleAsyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info)
744 {
745     std::string eventParam;
746     if (eventMarker.GetData().eventType.find("touch") != std::string::npos) {
747         TouchInfoToString(info, eventParam);
748     } else if (eventMarker.GetData().eventType.find("mouse") != std::string::npos) {
749         MouseInfoToString(info, eventParam);
750     } else if (eventMarker.GetData().eventType == "swipe") {
751         SwipeInfoToString(info, eventParam);
752     }
753 
754     std::string param = eventMarker.GetData().GetEventParam();
755     if (eventParam.empty()) {
756         param.append("null");
757     } else {
758         param.append(eventParam);
759     }
760 
761     if (eventMarker.GetData().isDeclarativeUi) {
762         if (delegate_) {
763             auto cinfo = CopyEventInfo(info);
764             delegate_->GetUiTask().PostTask(
765                 [eventMarker, cinfo] { eventMarker.CallUiArgFunction(cinfo.get()); }, "ArkUIPluginCallUiArgFunction");
766         }
767     } else {
768         delegate_->FireAsyncEvent(eventMarker.GetData().eventId, param, "");
769     }
770 
771     AccessibilityEvent accessibilityEvent;
772     accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
773     accessibilityEvent.eventType = eventMarker.GetData().eventType;
774     delegate_->FireAccessibilityEvent(accessibilityEvent);
775 }
776 
HandleAsyncEvent(const EventMarker & eventMarker,const std::shared_ptr<BaseEventInfo> & info)777 void PluginEventHandler::HandleAsyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info)
778 {
779     if (eventMarker.GetData().isDeclarativeUi) {
780         if (delegate_) {
781             delegate_->GetUiTask().PostTask(
782                 [eventMarker, info] { eventMarker.CallUiArgFunction(info.get()); }, "ArkUIPluginCallUiArgFunction");
783         }
784     }
785 }
786 
HandleSyncEvent(const EventMarker & eventMarker,const KeyEvent & info,bool & result)787 void PluginEventHandler::HandleSyncEvent(const EventMarker& eventMarker, const KeyEvent& info, bool& result)
788 {
789     std::string param = std::string("\"")
790                             .append(eventMarker.GetData().eventType)
791                             .append("\",{\"code\":")
792                             .append(std::to_string(static_cast<int32_t>(info.code)))
793                             .append(",\"action\":")
794                             .append(std::to_string(static_cast<int32_t>(info.action)))
795                             .append(",\"repeatCount\":")
796                             .append(std::to_string(static_cast<int32_t>(info.repeatTime)))
797                             .append(",\"timestamp\":")
798                             .append(std::to_string(static_cast<int32_t>(info.timeStamp.time_since_epoch().count())))
799                             .append(",\"key\":\"")
800                             .append(info.key)
801                             .append("\"},");
802 
803     result = delegate_->FireSyncEvent(eventMarker.GetData().eventId, param, "");
804 
805     AccessibilityEvent accessibilityEvent;
806     accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
807     accessibilityEvent.eventType = std::to_string(static_cast<int32_t>(info.code));
808     delegate_->FireAccessibilityEvent(accessibilityEvent);
809 }
810 
HandleAsyncEvent(const EventMarker & eventMarker,int32_t param)811 void PluginEventHandler::HandleAsyncEvent(const EventMarker& eventMarker, int32_t param)
812 {
813     LOGW("js event handler does not support this event type!");
814     AccessibilityEvent accessibilityEvent;
815     accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
816     accessibilityEvent.eventType = eventMarker.GetData().eventType;
817     delegate_->FireAccessibilityEvent(accessibilityEvent);
818 }
819 
HandleAsyncEvent(const EventMarker & eventMarker,const KeyEvent & info)820 void PluginEventHandler::HandleAsyncEvent(const EventMarker& eventMarker, const KeyEvent& info)
821 {
822     LOGW("js event handler does not support this event type!");
823     AccessibilityEvent accessibilityEvent;
824     accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
825     accessibilityEvent.eventType = eventMarker.GetData().eventType;
826     delegate_->FireAccessibilityEvent(accessibilityEvent);
827 }
828 
HandleAsyncEvent(const EventMarker & eventMarker,const std::string & param)829 void PluginEventHandler::HandleAsyncEvent(const EventMarker& eventMarker, const std::string& param)
830 {
831     if (eventMarker.GetData().isDeclarativeUi) {
832         std::string fixParam(param);
833         std::string::size_type startPos = param.find_first_of("{");
834         std::string::size_type endPos = param.find_last_of("}");
835         if (startPos != std::string::npos && endPos != std::string::npos && startPos < endPos) {
836             fixParam = fixParam.substr(startPos, endPos - startPos + 1);
837         }
838         if (delegate_) {
839             delegate_->GetUiTask().PostTask(
840                 [eventMarker, fixParam] { eventMarker.CallUiStrFunction(fixParam); }, "ArkUIPluginCallUiStrFunction");
841         }
842     } else {
843         delegate_->FireAsyncEvent(eventMarker.GetData().eventId, param, "");
844     }
845 
846     AccessibilityEvent accessibilityEvent;
847     accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
848     accessibilityEvent.eventType = eventMarker.GetData().eventType;
849     delegate_->FireAccessibilityEvent(accessibilityEvent);
850 }
851 
HandleSyncEvent(const EventMarker & eventMarker,bool & result)852 void PluginEventHandler::HandleSyncEvent(const EventMarker& eventMarker, bool& result)
853 {
854     LOGW("js event handler does not support this event type!");
855     AccessibilityEvent accessibilityEvent;
856     accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
857     accessibilityEvent.eventType = eventMarker.GetData().eventType;
858     delegate_->FireAccessibilityEvent(accessibilityEvent);
859 }
860 
HandleSyncEvent(const EventMarker & eventMarker,const std::shared_ptr<BaseEventInfo> & info)861 void PluginEventHandler::HandleSyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info)
862 {
863     CHECK_NULL_VOID(delegate_);
864     delegate_->GetUiTask().PostSyncTask(
865         [eventMarker, info] { eventMarker.CallUiArgFunction(info.get()); }, "ArkUIPluginCallUiArgFunction");
866 }
867 
HandleSyncEvent(const EventMarker & eventMarker,const BaseEventInfo & info,bool & result)868 void PluginEventHandler::HandleSyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info, bool& result)
869 {
870     LOGW("js event handler does not support this event type!");
871     AccessibilityEvent accessibilityEvent;
872     accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
873     accessibilityEvent.eventType = eventMarker.GetData().eventType;
874     delegate_->FireAccessibilityEvent(accessibilityEvent);
875 }
876 
HandleSyncEvent(const EventMarker & eventMarker,const std::string & param,std::string & result)877 void PluginEventHandler::HandleSyncEvent(const EventMarker& eventMarker, const std::string& param, std::string& result)
878 {
879     LOGW("js event handler does not support this event type!");
880     AccessibilityEvent accessibilityEvent;
881     accessibilityEvent.nodeId = StringUtils::StringToInt(eventMarker.GetData().eventId);
882     accessibilityEvent.eventType = eventMarker.GetData().eventType;
883     delegate_->FireAccessibilityEvent(accessibilityEvent);
884     delegate_->FireSyncEvent(eventMarker.GetData().eventId, param, std::string(""), result);
885 }
886 
HandleSyncEvent(const EventMarker & eventMarker,const std::string & componentId,const int32_t nodeId,const bool isDestroy)887 void PluginEventHandler::HandleSyncEvent(
888     const EventMarker& eventMarker, const std::string& componentId, const int32_t nodeId, const bool isDestroy)
889 {
890     LOGW("js event handler does not support this event type!");
891 }
892 } // namespace OHOS::Ace
893