• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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/declarative_frontend/ng/declarative_frontend_ng.h"
17 
18 #include "base/log/dump_log.h"
19 #include "core/common/recorder/node_data_cache.h"
20 #include "frameworks/bridge/declarative_frontend/ng/page_router_manager_factory.h"
21 
22 namespace OHOS::Ace {
23 
~DeclarativeFrontendNG()24 DeclarativeFrontendNG::~DeclarativeFrontendNG() noexcept
25 {
26     LOGI("DeclarativeFrontend destroyed");
27 }
28 
Destroy()29 void DeclarativeFrontendNG::Destroy()
30 {
31     // The call doesn't change the page pop status
32     Recorder::NodeDataCache::Get().OnBeforePagePop(true);
33     CHECK_RUN_ON(JS);
34     // To guarantee the jsEngine_ and delegate_ released in js thread
35     delegate_.Reset();
36     jsEngine_->Destroy();
37     jsEngine_.Reset();
38 }
39 
Initialize(FrontendType type,const RefPtr<TaskExecutor> & taskExecutor)40 bool DeclarativeFrontendNG::Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor)
41 {
42     type_ = type;
43     taskExecutor_ = taskExecutor;
44     ACE_DCHECK(type_ == FrontendType::DECLARATIVE_JS);
45     InitializeDelegate(taskExecutor);
46     bool needPostJsTask = true;
47     auto container = Container::Current();
48     if (container) {
49         const auto& setting = container->GetSettings();
50         needPostJsTask = !(setting.usePlatformAsUIThread && setting.useUIAsJSThread);
51     }
52     auto initJSEngineTask = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_), delegate = delegate_] {
53         auto jsEngine = weakEngine.Upgrade();
54         if (!jsEngine) {
55             return;
56         }
57         jsEngine->Initialize(delegate);
58     };
59     if (needPostJsTask) {
60         taskExecutor->PostTask(initJSEngineTask, TaskExecutor::TaskType::JS, "ArkUIInitJsEngine");
61     } else {
62         initJSEngineTask();
63     }
64     return true;
65 }
66 
AttachPipelineContext(const RefPtr<PipelineBase> & context)67 void DeclarativeFrontendNG::AttachPipelineContext(const RefPtr<PipelineBase>& context)
68 {
69     if (delegate_) {
70         delegate_->AttachPipelineContext(context);
71     }
72 }
73 
AttachSubPipelineContext(const RefPtr<PipelineBase> & context)74 void DeclarativeFrontendNG::AttachSubPipelineContext(const RefPtr<PipelineBase>& context)
75 {
76     if (!delegate_) {
77         return;
78     }
79     delegate_->AttachSubPipelineContext(context);
80 }
81 
SetAssetManager(const RefPtr<AssetManager> & assetManager)82 void DeclarativeFrontendNG::SetAssetManager(const RefPtr<AssetManager>& assetManager)
83 {
84     if (delegate_) {
85         delegate_->SetAssetManager(assetManager);
86     }
87 }
88 
InitializeDelegate(const RefPtr<TaskExecutor> & taskExecutor)89 void DeclarativeFrontendNG::InitializeDelegate(const RefPtr<TaskExecutor>& taskExecutor)
90 {
91     auto pageRouterManager = NG::PageRouterManagerFactory::CreateManager();
92     auto loadPageCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](const std::string& url,
93                                 const std::function<void(const std::string&, int32_t)>& errorCallback) {
94         auto jsEngine = weakEngine.Upgrade();
95         if (!jsEngine) {
96             return false;
97         }
98         return jsEngine->LoadPageSource(url, errorCallback);
99     };
100 
101     auto loadPageByBufferCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
102                                         const std::shared_ptr<std::vector<uint8_t>>& content,
103                                         const std::function<void(const std::string&, int32_t)>& errorCallback,
104                                         const std::string& contentName) {
105         auto jsEngine = weakEngine.Upgrade();
106         if (!jsEngine) {
107             return false;
108         }
109         return jsEngine->LoadPageSource(content, errorCallback, contentName);
110     };
111 
112     auto mediaQueryCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
113                                          const std::string& callbackId, const std::string& args) {
114         auto jsEngine = weakEngine.Upgrade();
115         if (!jsEngine) {
116             return;
117         }
118         jsEngine->MediaQueryCallback(callbackId, args);
119     };
120 
121     auto layoutInspectorCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
122                                        const std::string& componentId) {
123         auto jsEngine = weakEngine.Upgrade();
124         if (!jsEngine) {
125             return;
126         }
127         jsEngine->LayoutInspectorCallback(componentId);
128     };
129 
130     auto drawInspectorCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
131                                      const std::string& componentId) {
132         auto jsEngine = weakEngine.Upgrade();
133         if (!jsEngine) {
134             return;
135         }
136         jsEngine->DrawInspectorCallback(componentId);
137     };
138 
139     auto drawChildrenInspectorCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
140         const std::string& componentId) {
141             auto jsEngine = weakEngine.Upgrade();
142             if (!jsEngine) {
143                 return;
144             }
145             jsEngine->DrawChildrenInspectorCallback(componentId);
146     };
147 
148     auto isDrawChildrenCallBackFuncExist = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
149         const std::string& componentId) {
150             auto jsEngine = weakEngine.Upgrade();
151             if (!jsEngine) {
152                 return false;
153             }
154             return jsEngine->IsDrawChildrenCallbackFuncExist(componentId);
155     };
156 
157     auto onStartContinuationCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() -> bool {
158         auto jsEngine = weakEngine.Upgrade();
159         if (!jsEngine) {
160             return false;
161         }
162         return jsEngine->OnStartContinuation();
163     };
164 
165     auto onCompleteContinuationCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](int32_t code) {
166         auto jsEngine = weakEngine.Upgrade();
167         if (!jsEngine) {
168             return;
169         }
170         jsEngine->OnCompleteContinuation(code);
171     };
172 
173     auto onSaveDataCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](std::string& savedData) {
174         auto jsEngine = weakEngine.Upgrade();
175         if (!jsEngine) {
176             return;
177         }
178         jsEngine->OnSaveData(savedData);
179     };
180 
181     auto onRemoteTerminatedCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() {
182         auto jsEngine = weakEngine.Upgrade();
183         if (!jsEngine) {
184             return;
185         }
186         jsEngine->OnRemoteTerminated();
187     };
188 
189     auto onRestoreDataCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
190                                             const std::string& data) -> bool {
191         auto jsEngine = weakEngine.Upgrade();
192         if (!jsEngine) {
193             return false;
194         }
195         return jsEngine->OnRestoreData(data);
196     };
197 
198     auto destroyApplicationCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
199                                                  const std::string& packageName) {
200         auto jsEngine = weakEngine.Upgrade();
201         if (!jsEngine) {
202             return;
203         }
204         jsEngine->DestroyApplication(packageName);
205     };
206 
207     auto updateApplicationStateCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
208                                                      const std::string& packageName, Frontend::State state) {
209         auto jsEngine = weakEngine.Upgrade();
210         if (!jsEngine) {
211             return;
212         }
213         jsEngine->UpdateApplicationState(packageName, state);
214     };
215 
216     auto onWindowDisplayModeChangedCallBack = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
217                                                          bool isShownInMultiWindow, const std::string& data) {
218         auto jsEngine = weakEngine.Upgrade();
219         if (!jsEngine) {
220             return;
221         }
222         jsEngine->OnWindowDisplayModeChanged(isShownInMultiWindow, data);
223     };
224 
225     auto externalEventCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
226                                             const std::string& componentId, const uint32_t nodeId,
227                                             const bool isDestroy) {
228         auto jsEngine = weakEngine.Upgrade();
229         if (!jsEngine) {
230             return;
231         }
232         jsEngine->FireExternalEvent(componentId, nodeId, isDestroy);
233     };
234 
235     auto timerCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
236                                     const std::string& callbackId, const std::string& delay, bool isInterval) {
237         auto jsEngine = weakEngine.Upgrade();
238         if (!jsEngine) {
239             return;
240         }
241         jsEngine->TimerCallback(callbackId, delay, isInterval);
242     };
243 
244     auto loadNamedRouterCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
245                                        const std::string& namedRouter, bool isTriggeredByJs) {
246         auto jsEngine = weakEngine.Upgrade();
247         if (!jsEngine) {
248             return false;
249         }
250         return jsEngine->LoadNamedRouterSource(namedRouter, isTriggeredByJs);
251     };
252 
253     auto updateRootComponentCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() {
254         auto jsEngine = weakEngine.Upgrade();
255         if (!jsEngine) {
256             return false;
257         }
258         return jsEngine->UpdateRootComponent();
259     };
260 
261     auto generateIntentPageCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
262         const std::string& bundleName, const std::string& moduleName, const std::string& pagePath) {
263             auto jsEngine = weakEngine.Upgrade();
264             if (!jsEngine) {
265                 return false;
266             }
267             return jsEngine->GeneratePageByIntent(bundleName, moduleName, pagePath);
268     };
269 
270     pageRouterManager->SetLoadJsCallback(std::move(loadPageCallback));
271     pageRouterManager->SetLoadJsByBufferCallback(std::move(loadPageByBufferCallback));
272     pageRouterManager->SetLoadNamedRouterCallback(std::move(loadNamedRouterCallback));
273     pageRouterManager->SetUpdateRootComponentCallback(std::move(updateRootComponentCallback));
274     pageRouterManager->SetGenerateIntentPageCallback(std::move(generateIntentPageCallback));
275 
276     delegate_ = AceType::MakeRefPtr<Framework::FrontendDelegateDeclarativeNG>(taskExecutor);
277     delegate_->SetMediaQueryCallback(std::move(mediaQueryCallback));
278     delegate_->SetLayoutInspectorCallback(std::move(layoutInspectorCallback));
279     delegate_->SetDrawInspectorCallback(std::move(drawInspectorCallback));
280     delegate_->SetDrawChildrenInspectorCallback(std::move(drawChildrenInspectorCallback));
281     delegate_->SetIsDrawChildrenCallbackFuncExistCallback(std::move(isDrawChildrenCallBackFuncExist));
282     delegate_->SetOnStartContinuationCallBack(std::move(onStartContinuationCallBack));
283     delegate_->SetOnCompleteContinuationCallBack(std::move(onCompleteContinuationCallBack));
284     delegate_->SetOnSaveDataCallBack(std::move(onSaveDataCallBack));
285     delegate_->SetOnRemoteTerminatedCallBack(std::move(onRemoteTerminatedCallBack));
286     delegate_->SetOnRestoreDataCallBack(std::move(onRestoreDataCallBack));
287     delegate_->SetDestroyApplicationCallback(std::move(destroyApplicationCallback));
288     delegate_->SetUpdateApplicationStateCallback(std::move(updateApplicationStateCallback));
289     delegate_->SetOnWindowDisplayModeChangedCallback(std::move(onWindowDisplayModeChangedCallBack));
290     delegate_->SetExternalEventCallback(std::move(externalEventCallback));
291     delegate_->SetTimerCallback(std::move(timerCallback));
292 
293     delegate_->SetPageRouterManager(pageRouterManager);
294     if (jsEngine_) {
295         delegate_->SetGroupJsBridge(jsEngine_->GetGroupJsBridge());
296     }
297     auto moduleNamecallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](const std::string& pageName)->
298     std::string {
299         auto jsEngine = weakEngine.Upgrade();
300         if (!jsEngine) {
301             return "";
302         }
303         return jsEngine->SearchRouterRegisterMap(pageName);
304     };
305     auto navigationLoadCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
306         const std::string bundleName, const std::string& moduleName, const std::string& pageSourceFile,
307         bool isSingleton) -> int32_t {
308         auto jsEngine = weakEngine.Upgrade();
309         if (!jsEngine) {
310             return -1;
311         }
312         return jsEngine->LoadNavDestinationSource(bundleName, moduleName, pageSourceFile, isSingleton);
313     };
314     auto container = Container::Current();
315     if (container) {
316         auto pageUrlChecker = container->GetPageUrlChecker();
317         // ArkTSCard container no SetPageUrlChecker
318         if (pageUrlChecker != nullptr) {
319             pageUrlChecker->SetModuleNameCallback(std::move(moduleNamecallback));
320         }
321         auto navigationRoute = container->GetNavigationRoute();
322         if (navigationRoute) {
323             navigationRoute->SetLoadPageCallback(std::move(navigationLoadCallback));
324         }
325     }
326 }
327 
GetPageRouterManager() const328 RefPtr<NG::PageRouterManager> DeclarativeFrontendNG::GetPageRouterManager() const
329 {
330     CHECK_NULL_RETURN(delegate_, nullptr);
331     return delegate_->GetPageRouterManager();
332 }
333 
UpdateState(Frontend::State state)334 void DeclarativeFrontendNG::UpdateState(Frontend::State state)
335 {
336     if (!delegate_ || state == Frontend::State::ON_CREATE) {
337         return;
338     }
339     bool needPostJsTask = true;
340     auto container = Container::Current();
341     CHECK_NULL_VOID(container);
342     const auto& setting = container->GetSettings();
343     needPostJsTask = !(setting.usePlatformAsUIThread && setting.useUIAsJSThread);
344     if (needPostJsTask) {
345         delegate_->UpdateApplicationState(delegate_->GetAppID(), state);
346         return;
347     }
348     if (jsEngine_) {
349         jsEngine_->UpdateApplicationState(delegate_->GetAppID(), state);
350     }
351 }
352 
OnConfigurationUpdated(const std::string & data)353 void DeclarativeFrontendNG::OnConfigurationUpdated(const std::string& data)
354 {
355     if (delegate_) {
356         delegate_->OnConfigurationUpdated(data);
357     }
358 }
359 
OnActive()360 void DeclarativeFrontendNG::OnActive()
361 {
362     if (delegate_) {
363         foregroundFrontend_ = true;
364         delegate_->InitializeAccessibilityCallback();
365     }
366 }
367 
OnCompleteContinuation(int32_t code)368 void DeclarativeFrontendNG::OnCompleteContinuation(int32_t code)
369 {
370     if (delegate_) {
371         delegate_->OnCompleteContinuation(code);
372     }
373 }
374 
OnSaveData(std::string & data)375 void DeclarativeFrontendNG::OnSaveData(std::string& data)
376 {
377     if (delegate_) {
378         delegate_->OnSaveData(data);
379     }
380 }
381 
OnRemoteTerminated()382 void DeclarativeFrontendNG::OnRemoteTerminated()
383 {
384     if (delegate_) {
385         delegate_->OnRemoteTerminated();
386     }
387 }
388 
NotifyAppStorage(const std::string & key,const std::string & value)389 void DeclarativeFrontendNG::NotifyAppStorage(const std::string& key, const std::string& value)
390 {
391     if (!delegate_) {
392         return;
393     }
394     delegate_->NotifyAppStorage(jsEngine_, key, value);
395 }
396 
GetRouterSize() const397 int32_t DeclarativeFrontendNG::GetRouterSize() const
398 {
399     if (delegate_) {
400         return delegate_->GetStackSize();
401     }
402     return -1;
403 }
404 
OnStartContinuation()405 bool DeclarativeFrontendNG::OnStartContinuation()
406 {
407     if (!delegate_) {
408         return false;
409     }
410     return delegate_->OnStartContinuation();
411 }
412 
OnRestoreData(const std::string & data)413 bool DeclarativeFrontendNG::OnRestoreData(const std::string& data)
414 {
415     if (!delegate_) {
416         return false;
417     }
418     return delegate_->OnRestoreData(data);
419 }
420 
RunPage(const std::string & url,const std::string & params)421 UIContentErrorCode DeclarativeFrontendNG::RunPage(const std::string& url, const std::string& params)
422 {
423     auto container = Container::Current();
424     auto isStageModel = container ? container->IsUseStageModel() : false;
425     if (!isStageModel) {
426         // In NG structure and fa mode, first load app.js
427         auto taskExecutor = container ? container->GetTaskExecutor() : nullptr;
428         CHECK_NULL_RETURN(taskExecutor, UIContentErrorCode::NULL_POINTER);
429         taskExecutor->PostTask(
430             [weak = AceType::WeakClaim(this)]() {
431                 auto frontend = weak.Upgrade();
432                 CHECK_NULL_VOID(frontend);
433                 CHECK_NULL_VOID(frontend->jsEngine_);
434                 frontend->jsEngine_->LoadFaAppSource();
435             },
436             TaskExecutor::TaskType::JS, "ArkUILoadFaAppSource");
437     }
438     // Not use this pageId from backend, manage it in FrontendDelegateDeclarativeNg.
439     if (delegate_) {
440         delegate_->RunPage(url, params, pageProfile_);
441         return UIContentErrorCode::NO_ERRORS;
442     }
443 
444     return UIContentErrorCode::NULL_POINTER;
445 }
446 
RunPage(const std::shared_ptr<std::vector<uint8_t>> & content,const std::string & params)447 UIContentErrorCode DeclarativeFrontendNG::RunPage(
448     const std::shared_ptr<std::vector<uint8_t>>& content, const std::string& params)
449 {
450     auto container = Container::Current();
451     auto isStageModel = container ? container->IsUseStageModel() : false;
452     if (!isStageModel) {
453         LOGE("RunPage by buffer must be run under stage model.");
454         return UIContentErrorCode::NO_STAGE;
455     }
456 
457     if (delegate_) {
458         delegate_->RunPage(content, params, pageProfile_);
459         return UIContentErrorCode::NO_ERRORS;
460     }
461 
462     return UIContentErrorCode::NULL_POINTER;
463 }
464 
RunPageByNamedRouter(const std::string & name,const std::string & params)465 UIContentErrorCode DeclarativeFrontendNG::RunPageByNamedRouter(const std::string& name, const std::string& params)
466 {
467     if (delegate_) {
468         delegate_->RunPage(name, params, pageProfile_, true);
469         return UIContentErrorCode::NO_ERRORS;
470     }
471     return UIContentErrorCode::NULL_POINTER;
472 }
473 
RunIntentPage()474 UIContentErrorCode DeclarativeFrontendNG::RunIntentPage()
475 {
476     if (delegate_) {
477         delegate_->RunIntentPage();
478         return UIContentErrorCode::NO_ERRORS;
479     }
480     return UIContentErrorCode::NULL_POINTER;
481 }
482 
SetRouterIntentInfo(const std::string & intentInfoSerialized,bool isColdStart,const std::function<void ()> && loadPageCallback)483 UIContentErrorCode DeclarativeFrontendNG::SetRouterIntentInfo(const std::string& intentInfoSerialized,
484     bool isColdStart, const std::function<void()>&& loadPageCallback)
485 {
486     if (delegate_) {
487         delegate_->SetRouterIntentInfo(intentInfoSerialized, isColdStart, std::move(loadPageCallback));
488         return UIContentErrorCode::NO_ERRORS;
489     }
490     return UIContentErrorCode::NULL_POINTER;
491 }
492 
GetTopNavDestinationInfo(bool onlyFullScreen,bool needParam)493 std::string DeclarativeFrontendNG::GetTopNavDestinationInfo(bool onlyFullScreen, bool needParam)
494 {
495     if (delegate_) {
496         return delegate_->GetTopNavDestinationInfo(onlyFullScreen, needParam);
497     }
498     return "{}";
499 }
500 
ReplacePage(const std::string & url,const std::string & params)501 void DeclarativeFrontendNG::ReplacePage(const std::string& url, const std::string& params)
502 {
503     if (delegate_) {
504         delegate_->Replace(url, params);
505     }
506 }
507 
PushPage(const std::string & url,const std::string & params)508 void DeclarativeFrontendNG::PushPage(const std::string& url, const std::string& params)
509 {
510     if (delegate_) {
511         delegate_->Push(url, params);
512     }
513 }
514 
515 
GetContextValue()516 napi_value DeclarativeFrontendNG::GetContextValue()
517 {
518     return jsEngine_->GetContextValue();
519 }
520 
BuilderNodeFunc(std::string functionName,const std::vector<int32_t> & nodeIds)521 bool DeclarativeFrontendNG::BuilderNodeFunc(std::string functionName, const std::vector<int32_t>& nodeIds)
522 {
523     CHECK_NULL_RETURN(jsEngine_, false);
524     return jsEngine_->BuilderNodeFunc(functionName, nodeIds);
525 }
526 
GetFrameNodeValueByNodeId(int32_t nodeId)527 napi_value DeclarativeFrontendNG::GetFrameNodeValueByNodeId(int32_t nodeId)
528 {
529     return jsEngine_->GetFrameNodeValueByNodeId(nodeId);
530 }
531 
NavigatePage(uint8_t type,const PageTarget & target,const std::string & params)532 void DeclarativeFrontendNG::NavigatePage(uint8_t type, const PageTarget& target, const std::string& params)
533 {
534     if (delegate_) {
535         delegate_->NavigatePage(type, target, params);
536     }
537 }
538 
OnWindowDisplayModeChanged(bool isShownInMultiWindow,const std::string & data)539 void DeclarativeFrontendNG::OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data)
540 {
541     delegate_->OnWindowDisplayModeChanged(isShownInMultiWindow, data);
542 }
543 
GetAccessibilityManager() const544 RefPtr<AccessibilityManager> DeclarativeFrontendNG::GetAccessibilityManager() const
545 {
546     if (!delegate_) {
547         return nullptr;
548     }
549     return delegate_->GetJSAccessibilityManager();
550 }
551 
GetWindowConfig()552 WindowConfig& DeclarativeFrontendNG::GetWindowConfig()
553 {
554     if (!delegate_) {
555         static WindowConfig windowConfig;
556         return windowConfig;
557     }
558     return delegate_->GetWindowConfig();
559 }
560 
OnBackPressed()561 bool DeclarativeFrontendNG::OnBackPressed()
562 {
563     CHECK_NULL_RETURN(delegate_, false);
564     return delegate_->OnPageBackPress();
565 }
566 
OnShow()567 void DeclarativeFrontendNG::OnShow()
568 {
569     foregroundFrontend_ = true;
570     CHECK_NULL_VOID(delegate_);
571     delegate_->OnPageShow();
572 }
573 
OnHide()574 void DeclarativeFrontendNG::OnHide()
575 {
576     foregroundFrontend_ = false;
577     CHECK_NULL_VOID(delegate_);
578     delegate_->OnPageHide();
579 }
580 
CallRouterBack()581 void DeclarativeFrontendNG::CallRouterBack()
582 {
583     if (delegate_) {
584         if (delegate_->GetStackSize() == 1 && isSubWindow_) {
585             LOGW("Can't back because this is the last page of sub window!");
586             return;
587         }
588         delegate_->Back("", "");
589     }
590 }
591 
OnSurfaceChanged(int32_t width,int32_t height)592 void DeclarativeFrontendNG::OnSurfaceChanged(int32_t width, int32_t height)
593 {
594     // TODO: update media query infos
595     if (delegate_) {
596         delegate_->OnSurfaceChanged();
597     }
598 }
599 
OnLayoutCompleted(const std::string & componentId)600 void DeclarativeFrontendNG::OnLayoutCompleted(const std::string& componentId)
601 {
602     if (delegate_) {
603         delegate_->OnLayoutCompleted(componentId);
604     }
605 }
606 
OnDrawCompleted(const std::string & componentId)607 void DeclarativeFrontendNG::OnDrawCompleted(const std::string& componentId)
608 {
609     if (delegate_) {
610         delegate_->OnDrawCompleted(componentId);
611     }
612 }
613 
OnDrawChildrenCompleted(const std::string & componentId)614 void DeclarativeFrontendNG::OnDrawChildrenCompleted(const std::string& componentId)
615 {
616     if (delegate_) {
617         delegate_->OnDrawChildrenCompleted(componentId);
618     }
619 }
620 
IsDrawChildrenCallbackFuncExist(const std::string & componentId)621 bool DeclarativeFrontendNG::IsDrawChildrenCallbackFuncExist(const std::string& componentId)
622 {
623     if (delegate_) {
624         return delegate_->IsDrawChildrenCallbackFuncExist(componentId);
625     }
626     return false;
627 }
628 
DumpFrontend() const629 void DeclarativeFrontendNG::DumpFrontend() const
630 {
631     if (!delegate_) {
632         return;
633     }
634     int32_t routerIndex = 0;
635     std::string routerName;
636     std::string routerPath;
637     delegate_->GetState(routerIndex, routerName, routerPath);
638 
639     if (DumpLog::GetInstance().GetDumpFile()) {
640         DumpLog::GetInstance().AddDesc("Components: " + std::to_string(delegate_->GetComponentsCount()));
641         DumpLog::GetInstance().AddDesc("Path: " + routerPath);
642         DumpLog::GetInstance().AddDesc("Length: " + std::to_string(routerIndex));
643         DumpLog::GetInstance().Print(0, routerName, 0);
644     }
645 }
646 
GetPagePath() const647 std::string DeclarativeFrontendNG::GetPagePath() const
648 {
649     if (!delegate_) {
650         return "";
651     }
652     int32_t routerIndex = 0;
653     std::string routerName;
654     std::string routerPath;
655     delegate_->GetState(routerIndex, routerName, routerPath);
656     return routerPath + routerName;
657 }
658 
TriggerGarbageCollection()659 void DeclarativeFrontendNG::TriggerGarbageCollection()
660 {
661     if (jsEngine_) {
662         jsEngine_->RunGarbageCollection();
663     }
664 }
665 
DumpHeapSnapshot(bool isPrivate)666 void DeclarativeFrontendNG::DumpHeapSnapshot(bool isPrivate)
667 {
668     if (jsEngine_) {
669         jsEngine_->DumpHeapSnapshot(isPrivate);
670     }
671 }
672 
NotifyUIIdle()673 void DeclarativeFrontendNG::NotifyUIIdle()
674 {
675     if (jsEngine_) {
676         jsEngine_->NotifyUIIdle();
677     }
678 }
679 
RestoreRouterStack(const std::string & contentInfo,ContentInfoType type)680 std::pair<RouterRecoverRecord, UIContentErrorCode> DeclarativeFrontendNG::RestoreRouterStack(
681     const std::string& contentInfo, ContentInfoType type)
682 {
683     if (delegate_) {
684         return delegate_->RestoreRouterStack(contentInfo, type);
685     }
686     return std::make_pair(RouterRecoverRecord(), UIContentErrorCode::NULL_POINTER);
687 }
688 
GetContentInfo(ContentInfoType type) const689 std::string DeclarativeFrontendNG::GetContentInfo(ContentInfoType type) const
690 {
691     if (delegate_) {
692         return delegate_->GetContentInfo(type);
693     }
694     return "";
695 }
696 
SetColorMode(ColorMode colorMode)697 void DeclarativeFrontendNG::SetColorMode(ColorMode colorMode)
698 {
699     // TODO: update media query infos
700     if (delegate_) {
701         delegate_->SetColorMode(colorMode);
702     }
703 }
704 
RebuildAllPages()705 void DeclarativeFrontendNG::RebuildAllPages()
706 {
707     if (delegate_) {
708         delegate_->RebuildAllPages();
709     }
710 }
711 
FlushReload()712 void DeclarativeFrontendNG::FlushReload()
713 {
714     if (jsEngine_) {
715         jsEngine_->FlushReload();
716     }
717 }
718 
HotReload()719 void DeclarativeFrontendNG::HotReload()
720 {
721     auto manager = GetPageRouterManager();
722     CHECK_NULL_VOID(manager);
723     manager->FlushFrontend();
724 }
725 
GetCurrentPageUrl() const726 std::string DeclarativeFrontendNG::GetCurrentPageUrl() const
727 {
728     auto pageRouterManager = GetPageRouterManager();
729     if (pageRouterManager) {
730         return pageRouterManager->GetCurrentPageUrl();
731     }
732     return "";
733 }
734 } // namespace OHOS::Ace
735