1 /*
2 * Copyright (c) 2022-2024 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/frontend_delegate_declarative_ng.h"
17
18 #include "base/i18n/localization.h"
19 #include "base/log/ace_trace.h"
20 #include "base/log/event_report.h"
21 #include "base/resource/ace_res_config.h"
22 #include "base/subwindow/subwindow_manager.h"
23 #include "base/thread/background_task_executor.h"
24 #include "base/utils/measure_util.h"
25 #include "base/utils/utils.h"
26 #include "core/common/ace_application_info.h"
27 #include "core/common/container.h"
28 #include "core/common/thread_checker.h"
29 #include "core/components_ng/base/view_stack_model.h"
30 #include "core/components_ng/pattern/overlay/overlay_manager.h"
31 #include "core/components_ng/pattern/stage/page_pattern.h"
32 #include "core/components_ng/render/adapter/component_snapshot.h"
33 #include "core/pipeline_ng/pipeline_context.h"
34 #include "frameworks/bridge/common/utils/utils.h"
35 #include "frameworks/core/common/ace_engine.h"
36
37 namespace OHOS::Ace::Framework {
38
39 namespace {
40
41 const char MANIFEST_JSON[] = "manifest.json";
42 const char PAGES_JSON[] = "main_pages.json";
43 constexpr int32_t TOAST_TIME_MAX = 10000; // ms
44 constexpr int32_t TOAST_TIME_DEFAULT = 1500; // ms
45 constexpr int32_t CALLBACK_ERRORCODE_CANCEL = 1;
46 constexpr int32_t CALLBACK_DATACODE_ZERO = 0;
47
48 // helper function to run OverlayManager task
49 // ensures that the task runs in subwindow instead of main Window
MainWindowOverlay(std::function<void (RefPtr<NG::OverlayManager>)> && task,const std::string & name)50 void MainWindowOverlay(std::function<void(RefPtr<NG::OverlayManager>)>&& task, const std::string& name)
51 {
52 auto currentId = Container::CurrentId();
53 ContainerScope scope(currentId);
54 auto context = NG::PipelineContext::GetCurrentContext();
55 CHECK_NULL_VOID(context);
56 auto overlayManager = context->GetOverlayManager();
57 context->GetTaskExecutor()->PostTask(
58 [task = std::move(task), weak = WeakPtr<NG::OverlayManager>(overlayManager)] {
59 auto overlayManager = weak.Upgrade();
60 task(overlayManager);
61 },
62 TaskExecutor::TaskType::UI, name);
63 }
64 } // namespace
65
FrontendDelegateDeclarativeNG(const RefPtr<TaskExecutor> & taskExecutor)66 FrontendDelegateDeclarativeNG::FrontendDelegateDeclarativeNG(const RefPtr<TaskExecutor>& taskExecutor)
67 : taskExecutor_(taskExecutor), manifestParser_(AceType::MakeRefPtr<Framework::ManifestParser>()),
68 mediaQueryInfo_(AceType::MakeRefPtr<MediaQueryInfo>()),
69 jsAccessibilityManager_(AccessibilityNodeManager::Create())
70 {}
71
SetMediaQueryCallback(MediaQueryCallback && mediaQueryCallback)72 void FrontendDelegateDeclarativeNG::SetMediaQueryCallback(MediaQueryCallback&& mediaQueryCallback)
73 {
74 mediaQueryCallback_ = mediaQueryCallback;
75 }
76
SetLayoutInspectorCallback(const LayoutInspectorCallback & layoutInspectorCallback)77 void FrontendDelegateDeclarativeNG::SetLayoutInspectorCallback(const LayoutInspectorCallback& layoutInspectorCallback)
78 {
79 layoutInspectorCallback_ = layoutInspectorCallback;
80 }
81
SetDrawInspectorCallback(const DrawInspectorCallback & drawInspectorCallback)82 void FrontendDelegateDeclarativeNG::SetDrawInspectorCallback(const DrawInspectorCallback& drawInspectorCallback)
83 {
84 drawInspectorCallback_ = drawInspectorCallback;
85 }
86
SetOnStartContinuationCallBack(OnStartContinuationCallBack && onStartContinuationCallBack)87 void FrontendDelegateDeclarativeNG::SetOnStartContinuationCallBack(
88 OnStartContinuationCallBack&& onStartContinuationCallBack)
89 {
90 onStartContinuationCallBack_ = onStartContinuationCallBack;
91 }
92
SetOnCompleteContinuationCallBack(OnCompleteContinuationCallBack && onCompleteContinuationCallBack)93 void FrontendDelegateDeclarativeNG::SetOnCompleteContinuationCallBack(
94 OnCompleteContinuationCallBack&& onCompleteContinuationCallBack)
95 {
96 onCompleteContinuationCallBack_ = onCompleteContinuationCallBack;
97 }
98
SetOnSaveDataCallBack(OnSaveDataCallBack && onSaveDataCallBack)99 void FrontendDelegateDeclarativeNG::SetOnSaveDataCallBack(OnSaveDataCallBack&& onSaveDataCallBack)
100 {
101 onSaveDataCallBack_ = onSaveDataCallBack;
102 }
103
SetOnRemoteTerminatedCallBack(OnRemoteTerminatedCallBack && onRemoteTerminatedCallBack)104 void FrontendDelegateDeclarativeNG::SetOnRemoteTerminatedCallBack(
105 OnRemoteTerminatedCallBack&& onRemoteTerminatedCallBack)
106 {
107 onRemoteTerminatedCallBack_ = onRemoteTerminatedCallBack;
108 }
109
SetOnRestoreDataCallBack(OnRestoreDataCallBack && onRestoreDataCallBack)110 void FrontendDelegateDeclarativeNG::SetOnRestoreDataCallBack(OnRestoreDataCallBack&& onRestoreDataCallBack)
111 {
112 onRestoreDataCallBack_ = onRestoreDataCallBack;
113 }
114
SetDestroyApplicationCallback(DestroyApplicationCallback && destroyApplicationCallback)115 void FrontendDelegateDeclarativeNG::SetDestroyApplicationCallback(
116 DestroyApplicationCallback&& destroyApplicationCallback)
117 {
118 destroyApplication_ = destroyApplicationCallback;
119 }
120
SetUpdateApplicationStateCallback(UpdateApplicationStateCallback && updateApplicationStateCallback)121 void FrontendDelegateDeclarativeNG::SetUpdateApplicationStateCallback(
122 UpdateApplicationStateCallback&& updateApplicationStateCallback)
123 {
124 updateApplicationState_ = updateApplicationStateCallback;
125 }
126
SetOnWindowDisplayModeChangedCallback(OnWindowDisplayModeChangedCallBack && onWindowDisplayModeChangedCallBack)127 void FrontendDelegateDeclarativeNG::SetOnWindowDisplayModeChangedCallback(
128 OnWindowDisplayModeChangedCallBack&& onWindowDisplayModeChangedCallBack)
129 {
130 onWindowDisplayModeChanged_ = onWindowDisplayModeChangedCallBack;
131 }
132
SetExternalEventCallback(ExternalEventCallback && externalEventCallback)133 void FrontendDelegateDeclarativeNG::SetExternalEventCallback(ExternalEventCallback&& externalEventCallback)
134 {
135 externalEvent_ = externalEventCallback;
136 }
137
SetTimerCallback(TimerCallback && timerCallback)138 void FrontendDelegateDeclarativeNG::SetTimerCallback(TimerCallback&& timerCallback)
139 {
140 timer_ = timerCallback;
141 }
142
AttachPipelineContext(const RefPtr<PipelineBase> & context)143 void FrontendDelegateDeclarativeNG::AttachPipelineContext(const RefPtr<PipelineBase>& context)
144 {
145 if (!context) {
146 return;
147 }
148 context->SetOnPageShow([weak = AceType::WeakClaim(this)] {
149 auto delegate = weak.Upgrade();
150 if (delegate) {
151 delegate->OnPageShow();
152 }
153 });
154
155 pipelineContextHolder_.Attach(context);
156 jsAccessibilityManager_->SetPipelineContext(context);
157 jsAccessibilityManager_->InitializeCallback();
158 }
159
AttachSubPipelineContext(const RefPtr<PipelineBase> & context)160 void FrontendDelegateDeclarativeNG::AttachSubPipelineContext(const RefPtr<PipelineBase>& context)
161 {
162 if (!context) {
163 return;
164 }
165 jsAccessibilityManager_->AddSubPipelineContext(context);
166 jsAccessibilityManager_->RegisterSubWindowInteractionOperation(context->GetWindowId());
167 }
168
RunPage(const std::string & url,const std::string & params,const std::string & profile,bool isNamedRouter)169 void FrontendDelegateDeclarativeNG::RunPage(
170 const std::string& url, const std::string& params, const std::string& profile, bool isNamedRouter)
171 {
172 ACE_SCOPED_TRACE("FrontendDelegateDeclarativeNG::RunPage");
173
174 LOGI("Frontend delegate declarative run page, url=%{public}s", url.c_str());
175 std::string jsonContent;
176 if (GetAssetContent(MANIFEST_JSON, jsonContent)) {
177 manifestParser_->Parse(jsonContent);
178 manifestParser_->Printer();
179 } else if (!profile.empty() && GetAssetContent(profile, jsonContent)) {
180 manifestParser_->Parse(jsonContent);
181 } else if (GetAssetContent(PAGES_JSON, jsonContent)) {
182 manifestParser_->Parse(jsonContent);
183 }
184 std::string mainPagePath;
185 if (!url.empty()) {
186 mainPagePath = manifestParser_->GetRouter()->GetPagePath(url);
187 } else {
188 mainPagePath = manifestParser_->GetRouter()->GetEntry();
189 }
190 taskExecutor_->PostTask(
191 [manifestParser = manifestParser_, delegate = Claim(this),
192 weakPtr = WeakPtr<NG::PageRouterManager>(pageRouterManager_), url, params, isNamedRouter]() {
193 auto pageRouterManager = weakPtr.Upgrade();
194 CHECK_NULL_VOID(pageRouterManager);
195 pageRouterManager->SetManifestParser(manifestParser);
196 if (isNamedRouter) {
197 pageRouterManager->RunPageByNamedRouter(url, params);
198 } else {
199 pageRouterManager->RunPage(url, params);
200 }
201 auto pipeline = delegate->GetPipelineContext();
202 // TODO: get platform version from context, and should stored in AceApplicationInfo.
203 if (manifestParser->GetMinPlatformVersion() > 0) {
204 pipeline->SetMinPlatformVersion(manifestParser->GetMinPlatformVersion());
205 }
206 },
207 TaskExecutor::TaskType::JS, "ArkUIRunPageUrl");
208 }
209
RunPage(const std::shared_ptr<std::vector<uint8_t>> & content,const std::string & params,const std::string & profile)210 void FrontendDelegateDeclarativeNG::RunPage(
211 const std::shared_ptr<std::vector<uint8_t>>& content, const std::string& params, const std::string& profile)
212 {
213 ACE_SCOPED_TRACE("FrontendDelegateDeclarativeNG::RunPage %zu", content->size());
214 taskExecutor_->PostTask(
215 [delegate = Claim(this), weakPtr = WeakPtr<NG::PageRouterManager>(pageRouterManager_), content, params]() {
216 auto pageRouterManager = weakPtr.Upgrade();
217 CHECK_NULL_VOID(pageRouterManager);
218 pageRouterManager->RunPage(content, params);
219 auto pipeline = delegate->GetPipelineContext();
220 },
221 TaskExecutor::TaskType::JS, "ArkUIRunPageContent");
222 }
223
OnConfigurationUpdated(const std::string & data)224 void FrontendDelegateDeclarativeNG::OnConfigurationUpdated(const std::string& data)
225 {
226 // only support mediaQueryUpdate
227 OnMediaQueryUpdate();
228 }
229
OnStartContinuation()230 bool FrontendDelegateDeclarativeNG::OnStartContinuation()
231 {
232 bool ret = false;
233 taskExecutor_->PostSyncTask(
234 [weak = AceType::WeakClaim(this), &ret] {
235 auto delegate = weak.Upgrade();
236 if (delegate && delegate->onStartContinuationCallBack_) {
237 ret = delegate->onStartContinuationCallBack_();
238 }
239 },
240 TaskExecutor::TaskType::JS, "ArkUIStartContinuation");
241 return ret;
242 }
243
OnCompleteContinuation(int32_t code)244 void FrontendDelegateDeclarativeNG::OnCompleteContinuation(int32_t code)
245 {
246 taskExecutor_->PostSyncTask(
247 [weak = AceType::WeakClaim(this), code] {
248 auto delegate = weak.Upgrade();
249 if (delegate && delegate->onCompleteContinuationCallBack_) {
250 delegate->onCompleteContinuationCallBack_(code);
251 }
252 },
253 TaskExecutor::TaskType::JS, "ArkUICompleteContinuation");
254 }
255
OnRemoteTerminated()256 void FrontendDelegateDeclarativeNG::OnRemoteTerminated()
257 {
258 taskExecutor_->PostSyncTask(
259 [weak = AceType::WeakClaim(this)] {
260 auto delegate = weak.Upgrade();
261 if (delegate && delegate->onRemoteTerminatedCallBack_) {
262 delegate->onRemoteTerminatedCallBack_();
263 }
264 },
265 TaskExecutor::TaskType::JS, "ArkUIRemoteTerminated");
266 }
267
OnSaveData(std::string & data)268 void FrontendDelegateDeclarativeNG::OnSaveData(std::string& data)
269 {
270 std::string savedData;
271 taskExecutor_->PostSyncTask(
272 [weak = AceType::WeakClaim(this), &savedData] {
273 auto delegate = weak.Upgrade();
274 if (delegate && delegate->onSaveDataCallBack_) {
275 delegate->onSaveDataCallBack_(savedData);
276 }
277 },
278 TaskExecutor::TaskType::JS, "ArkUISaveData");
279 std::string pageUri = GetCurrentPageUrl();
280 data = std::string("{\"url\":\"").append(pageUri).append("\",\"__remoteData\":").append(savedData).append("}");
281 }
282
OnRestoreData(const std::string & data)283 bool FrontendDelegateDeclarativeNG::OnRestoreData(const std::string& data)
284 {
285 bool ret = false;
286 taskExecutor_->PostSyncTask(
287 [weak = AceType::WeakClaim(this), &data, &ret] {
288 auto delegate = weak.Upgrade();
289 if (delegate && delegate->onRestoreDataCallBack_) {
290 ret = delegate->onRestoreDataCallBack_(data);
291 }
292 },
293 TaskExecutor::TaskType::JS, "ArkUIRestoreData");
294 return ret;
295 }
296
OnApplicationDestroy(const std::string & packageName)297 void FrontendDelegateDeclarativeNG::OnApplicationDestroy(const std::string& packageName)
298 {
299 taskExecutor_->PostSyncTask(
300 [destroyApplication = destroyApplication_, packageName] { destroyApplication(packageName); },
301 TaskExecutor::TaskType::JS, "ArkUIApplicationDestroy");
302 }
303
UpdateApplicationState(const std::string & packageName,Frontend::State state)304 void FrontendDelegateDeclarativeNG::UpdateApplicationState(const std::string& packageName, Frontend::State state)
305 {
306 taskExecutor_->PostTask([updateApplicationState = updateApplicationState_, packageName,
307 state] { updateApplicationState(packageName, state); },
308 TaskExecutor::TaskType::JS, "ArkUIUpdateApplicationState");
309 }
310
OnWindowDisplayModeChanged(bool isShownInMultiWindow,const std::string & data)311 void FrontendDelegateDeclarativeNG::OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data)
312 {
313 taskExecutor_->PostTask([onWindowDisplayModeChanged = onWindowDisplayModeChanged_, isShownInMultiWindow,
314 data] { onWindowDisplayModeChanged(isShownInMultiWindow, data); },
315 TaskExecutor::TaskType::JS, "ArkUIWindowDisplayModeChanged");
316 }
317
NotifyAppStorage(const WeakPtr<Framework::JsEngine> & jsEngineWeak,const std::string & key,const std::string & value)318 void FrontendDelegateDeclarativeNG::NotifyAppStorage(
319 const WeakPtr<Framework::JsEngine>& jsEngineWeak, const std::string& key, const std::string& value)
320 {
321 taskExecutor_->PostTask(
322 [jsEngineWeak, key, value] {
323 auto jsEngine = jsEngineWeak.Upgrade();
324 if (!jsEngine) {
325 return;
326 }
327 jsEngine->NotifyAppStorage(key, value);
328 },
329 TaskExecutor::TaskType::JS, "ArkUINotifyAppStorage");
330 }
331
FireAccessibilityEvent(const AccessibilityEvent & accessibilityEvent)332 void FrontendDelegateDeclarativeNG::FireAccessibilityEvent(const AccessibilityEvent& accessibilityEvent)
333 {
334 jsAccessibilityManager_->SendAccessibilityAsyncEvent(accessibilityEvent);
335 }
336
InitializeAccessibilityCallback()337 void FrontendDelegateDeclarativeNG::InitializeAccessibilityCallback()
338 {
339 jsAccessibilityManager_->InitializeCallback();
340 }
341
FireExternalEvent(const std::string &,const std::string & componentId,const uint32_t nodeId,const bool isDestroy)342 void FrontendDelegateDeclarativeNG::FireExternalEvent(
343 const std::string& /* eventId */, const std::string& componentId, const uint32_t nodeId, const bool isDestroy)
344 {
345 taskExecutor_->PostSyncTask(
346 [weak = AceType::WeakClaim(this), componentId, nodeId, isDestroy] {
347 auto delegate = weak.Upgrade();
348 if (delegate) {
349 delegate->externalEvent_(componentId, nodeId, isDestroy);
350 }
351 },
352 TaskExecutor::TaskType::JS, "ArkUIFireExternalEvent");
353 }
354
WaitTimer(const std::string & callbackId,const std::string & delay,bool isInterval,bool isFirst)355 void FrontendDelegateDeclarativeNG::WaitTimer(
356 const std::string& callbackId, const std::string& delay, bool isInterval, bool isFirst)
357 {
358 if (!isFirst) {
359 auto timeoutTaskIter = timeoutTaskMap_.find(callbackId);
360 // If not find the callbackId in map, means this timer already was removed,
361 // no need create a new cancelableTimer again.
362 if (timeoutTaskIter == timeoutTaskMap_.end()) {
363 return;
364 }
365 }
366
367 int32_t delayTime = StringToInt(delay);
368 // CancelableCallback class can only be executed once.
369 CancelableCallback<void()> cancelableTimer;
370 cancelableTimer.Reset([callbackId, delay, isInterval, call = timer_] { call(callbackId, delay, isInterval); });
371 auto result = timeoutTaskMap_.try_emplace(callbackId, cancelableTimer);
372 if (!result.second) {
373 result.first->second = cancelableTimer;
374 }
375 taskExecutor_->PostDelayedTask(cancelableTimer, TaskExecutor::TaskType::JS, delayTime, "ArkUIWaitTimer");
376 }
377
ClearTimer(const std::string & callbackId)378 void FrontendDelegateDeclarativeNG::ClearTimer(const std::string& callbackId)
379 {
380 auto timeoutTaskIter = timeoutTaskMap_.find(callbackId);
381 if (timeoutTaskIter != timeoutTaskMap_.end()) {
382 timeoutTaskIter->second.Cancel();
383 timeoutTaskMap_.erase(timeoutTaskIter);
384 }
385 }
386
Push(const std::string & uri,const std::string & params)387 void FrontendDelegateDeclarativeNG::Push(const std::string& uri, const std::string& params)
388 {
389 CHECK_NULL_VOID(pageRouterManager_);
390 pageRouterManager_->Push(NG::RouterPageInfo({ uri, params, true }));
391 OnMediaQueryUpdate();
392 }
393
PushWithMode(const std::string & uri,const std::string & params,uint32_t routerMode)394 void FrontendDelegateDeclarativeNG::PushWithMode(const std::string& uri, const std::string& params, uint32_t routerMode)
395 {
396 CHECK_NULL_VOID(pageRouterManager_);
397 pageRouterManager_->Push(NG::RouterPageInfo({ uri, params, true, static_cast<NG::RouterMode>(routerMode) }));
398 OnMediaQueryUpdate();
399 }
400
PushWithCallback(const std::string & uri,const std::string & params,bool recoverable,const std::function<void (const std::string &,int32_t)> & errorCallback,uint32_t routerMode)401 void FrontendDelegateDeclarativeNG::PushWithCallback(const std::string& uri, const std::string& params,
402 bool recoverable, const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode)
403 {
404 CHECK_NULL_VOID(pageRouterManager_);
405 pageRouterManager_->Push(
406 NG::RouterPageInfo({ uri, params, recoverable, static_cast<NG::RouterMode>(routerMode), errorCallback }));
407 OnMediaQueryUpdate();
408 }
409
PushNamedRoute(const std::string & uri,const std::string & params,bool recoverable,const std::function<void (const std::string &,int32_t)> & errorCallback,uint32_t routerMode)410 void FrontendDelegateDeclarativeNG::PushNamedRoute(const std::string& uri, const std::string& params,
411 bool recoverable, const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode)
412 {
413 CHECK_NULL_VOID(pageRouterManager_);
414 pageRouterManager_->PushNamedRoute(
415 NG::RouterPageInfo({ uri, params, recoverable, static_cast<NG::RouterMode>(routerMode), errorCallback }));
416 OnMediaQueryUpdate();
417 }
418
Replace(const std::string & uri,const std::string & params)419 void FrontendDelegateDeclarativeNG::Replace(const std::string& uri, const std::string& params)
420 {
421 CHECK_NULL_VOID(pageRouterManager_);
422 pageRouterManager_->Replace(NG::RouterPageInfo({ uri, params, true }));
423 }
424
ReplaceWithMode(const std::string & uri,const std::string & params,uint32_t routerMode)425 void FrontendDelegateDeclarativeNG::ReplaceWithMode(
426 const std::string& uri, const std::string& params, uint32_t routerMode)
427 {
428 CHECK_NULL_VOID(pageRouterManager_);
429 pageRouterManager_->Replace(
430 NG::RouterPageInfo({ uri, params, true, static_cast<NG::RouterMode>(routerMode) }));
431 OnMediaQueryUpdate();
432 }
433
ReplaceWithCallback(const std::string & uri,const std::string & params,bool recoverable,const std::function<void (const std::string &,int32_t)> & errorCallback,uint32_t routerMode)434 void FrontendDelegateDeclarativeNG::ReplaceWithCallback(const std::string& uri, const std::string& params,
435 bool recoverable, const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode)
436 {
437 CHECK_NULL_VOID(pageRouterManager_);
438 pageRouterManager_->Replace(
439 NG::RouterPageInfo({ uri, params, recoverable, static_cast<NG::RouterMode>(routerMode), errorCallback }));
440 OnMediaQueryUpdate();
441 }
442
ReplaceNamedRoute(const std::string & uri,const std::string & params,bool recoverable,const std::function<void (const std::string &,int32_t)> & errorCallback,uint32_t routerMode)443 void FrontendDelegateDeclarativeNG::ReplaceNamedRoute(const std::string& uri, const std::string& params,
444 bool recoverable, const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode)
445 {
446 CHECK_NULL_VOID(pageRouterManager_);
447 pageRouterManager_->ReplaceNamedRoute(
448 NG::RouterPageInfo({ uri, params, recoverable, static_cast<NG::RouterMode>(routerMode), errorCallback }));
449 OnMediaQueryUpdate();
450 }
451
Back(const std::string & uri,const std::string & params)452 void FrontendDelegateDeclarativeNG::Back(const std::string& uri, const std::string& params)
453 {
454 CHECK_NULL_VOID(pageRouterManager_);
455 pageRouterManager_->BackWithTarget(NG::RouterPageInfo({ uri, params }));
456 }
457
BackToIndex(int32_t index,const std::string & params)458 void FrontendDelegateDeclarativeNG::BackToIndex(int32_t index, const std::string& params)
459 {
460 CHECK_NULL_VOID(pageRouterManager_);
461 pageRouterManager_->BackToIndexWithTarget(index, params);
462 }
463
Clear()464 void FrontendDelegateDeclarativeNG::Clear()
465 {
466 CHECK_NULL_VOID(pageRouterManager_);
467 pageRouterManager_->Clear();
468 }
469
GetStackSize() const470 int32_t FrontendDelegateDeclarativeNG::GetStackSize() const
471 {
472 CHECK_NULL_RETURN(pageRouterManager_, 0);
473 return pageRouterManager_->GetStackSize();
474 }
475
GetState(int32_t & index,std::string & name,std::string & path)476 void FrontendDelegateDeclarativeNG::GetState(int32_t& index, std::string& name, std::string& path)
477 {
478 CHECK_NULL_VOID(pageRouterManager_);
479 pageRouterManager_->GetState(index, name, path);
480 }
481
GetRouterStateByIndex(int32_t & index,std::string & name,std::string & path,std::string & params)482 void FrontendDelegateDeclarativeNG::GetRouterStateByIndex(int32_t& index, std::string& name,
483 std::string& path, std::string& params)
484 {
485 CHECK_NULL_VOID(pageRouterManager_);
486 pageRouterManager_->GetStateByIndex(index, name, path, params);
487 }
488
GetRouterStateByUrl(std::string & url,std::vector<StateInfo> & stateArray)489 void FrontendDelegateDeclarativeNG::GetRouterStateByUrl(std::string& url, std::vector<StateInfo>& stateArray)
490 {
491 CHECK_NULL_VOID(pageRouterManager_);
492 pageRouterManager_->GetStateByUrl(url, stateArray);
493 }
494
GetParams()495 std::string FrontendDelegateDeclarativeNG::GetParams()
496 {
497 CHECK_NULL_RETURN(pageRouterManager_, "");
498 return pageRouterManager_->GetParams();
499 }
500
NavigatePage(uint8_t type,const PageTarget & target,const std::string & params)501 void FrontendDelegateDeclarativeNG::NavigatePage(uint8_t type, const PageTarget& target, const std::string& params)
502 {
503 switch (static_cast<NavigatorType>(type)) {
504 case NavigatorType::PUSH:
505 Push(target.url, params);
506 break;
507 case NavigatorType::REPLACE:
508 Replace(target.url, params);
509 break;
510 case NavigatorType::BACK:
511 Back(target.url, params);
512 break;
513 default:
514 Back(target.url, params);
515 }
516 }
517
PostJsTask(std::function<void ()> && task,const std::string & name)518 void FrontendDelegateDeclarativeNG::PostJsTask(std::function<void()>&& task, const std::string& name)
519 {
520 taskExecutor_->PostTask(task, TaskExecutor::TaskType::JS, name);
521 }
522
GetAppID() const523 const std::string& FrontendDelegateDeclarativeNG::GetAppID() const
524 {
525 return manifestParser_->GetAppInfo()->GetAppID();
526 }
527
GetAppName() const528 const std::string& FrontendDelegateDeclarativeNG::GetAppName() const
529 {
530 return manifestParser_->GetAppInfo()->GetAppName();
531 }
532
GetVersionName() const533 const std::string& FrontendDelegateDeclarativeNG::GetVersionName() const
534 {
535 return manifestParser_->GetAppInfo()->GetVersionName();
536 }
537
GetVersionCode() const538 int32_t FrontendDelegateDeclarativeNG::GetVersionCode() const
539 {
540 return manifestParser_->GetAppInfo()->GetVersionCode();
541 }
542
PostSyncTaskToPage(std::function<void ()> && task,const std::string & name)543 void FrontendDelegateDeclarativeNG::PostSyncTaskToPage(std::function<void()>&& task, const std::string& name)
544 {
545 pipelineContextHolder_.Get(); // Wait until Pipeline Context is attached.
546 taskExecutor_->PostSyncTask(task, TaskExecutor::TaskType::UI, name);
547 }
548
GetAssetContent(const std::string & url,std::string & content)549 bool FrontendDelegateDeclarativeNG::GetAssetContent(const std::string& url, std::string& content)
550 {
551 return GetAssetContentImpl(assetManager_, url, content);
552 }
553
GetAssetContent(const std::string & url,std::vector<uint8_t> & content)554 bool FrontendDelegateDeclarativeNG::GetAssetContent(const std::string& url, std::vector<uint8_t>& content)
555 {
556 return GetAssetContentImpl(assetManager_, url, content);
557 }
558
GetAssetPath(const std::string & url)559 std::string FrontendDelegateDeclarativeNG::GetAssetPath(const std::string& url)
560 {
561 return GetAssetPathImpl(assetManager_, url);
562 }
563
ChangeLocale(const std::string & language,const std::string & countryOrRegion)564 void FrontendDelegateDeclarativeNG::ChangeLocale(const std::string& language, const std::string& countryOrRegion)
565 {
566 taskExecutor_->PostTask(
567 [language, countryOrRegion]() { AceApplicationInfo::GetInstance().ChangeLocale(language, countryOrRegion); },
568 TaskExecutor::TaskType::PLATFORM, "ArkUIChangeLocale");
569 }
570
RegisterFont(const std::string & familyName,const std::string & familySrc,const std::string & bundleName,const std::string & moduleName)571 void FrontendDelegateDeclarativeNG::RegisterFont(const std::string& familyName, const std::string& familySrc,
572 const std::string& bundleName, const std::string& moduleName)
573 {
574 pipelineContextHolder_.Get()->RegisterFont(familyName, familySrc, bundleName, moduleName);
575 }
576
GetSystemFontList(std::vector<std::string> & fontList)577 void FrontendDelegateDeclarativeNG::GetSystemFontList(std::vector<std::string>& fontList)
578 {
579 pipelineContextHolder_.Get()->GetSystemFontList(fontList);
580 }
581
GetSystemFont(const std::string & fontName,FontInfo & fontInfo)582 bool FrontendDelegateDeclarativeNG::GetSystemFont(const std::string& fontName, FontInfo& fontInfo)
583 {
584 return pipelineContextHolder_.Get()->GetSystemFont(fontName, fontInfo);
585 }
586
GetUIFontConfig(FontConfigJsonInfo & fontConfigJsonInfo)587 void FrontendDelegateDeclarativeNG::GetUIFontConfig(FontConfigJsonInfo& fontConfigJsonInfo)
588 {
589 pipelineContextHolder_.Get()->GetUIFontConfig(fontConfigJsonInfo);
590 }
591
MeasureText(MeasureContext context)592 double FrontendDelegateDeclarativeNG::MeasureText(MeasureContext context)
593 {
594 if (context.isFontSizeUseDefaultUnit && context.fontSize.has_value() &&
595 !AceApplicationInfo::GetInstance().GreatOrEqualTargetAPIVersion(PlatformVersion::VERSION_TWELVE)) {
596 context.fontSize = Dimension(context.fontSize->Value(), DimensionUnit::VP);
597 }
598 return MeasureUtil::MeasureText(context);
599 }
600
MeasureTextSize(MeasureContext context)601 Size FrontendDelegateDeclarativeNG::MeasureTextSize(MeasureContext context)
602 {
603 if (context.isFontSizeUseDefaultUnit && context.fontSize.has_value() &&
604 !AceApplicationInfo::GetInstance().GreatOrEqualTargetAPIVersion(PlatformVersion::VERSION_TWELVE)) {
605 context.fontSize = Dimension(context.fontSize->Value(), DimensionUnit::VP);
606 }
607 return MeasureUtil::MeasureTextSize(context);
608 }
609
GetAnimationJsTask()610 SingleTaskExecutor FrontendDelegateDeclarativeNG::GetAnimationJsTask()
611 {
612 return SingleTaskExecutor::Make(taskExecutor_, TaskExecutor::TaskType::JS);
613 }
614
GetUiTask()615 SingleTaskExecutor FrontendDelegateDeclarativeNG::GetUiTask()
616 {
617 return SingleTaskExecutor::Make(taskExecutor_, TaskExecutor::TaskType::UI);
618 }
619
GetPipelineContext()620 RefPtr<PipelineBase> FrontendDelegateDeclarativeNG::GetPipelineContext()
621 {
622 return pipelineContextHolder_.Get();
623 }
624
OnPageBackPress()625 bool FrontendDelegateDeclarativeNG::OnPageBackPress()
626 {
627 CHECK_NULL_RETURN(pageRouterManager_, false);
628 auto pageNode = pageRouterManager_->GetCurrentPageNode();
629 CHECK_NULL_RETURN(pageNode, false);
630 auto pagePattern = pageNode->GetPattern<NG::PagePattern>();
631 CHECK_NULL_RETURN(pagePattern, false);
632 if (pagePattern->OnBackPressed()) {
633 return true;
634 }
635 return pageRouterManager_->Pop();
636 }
637
OnSurfaceChanged()638 void FrontendDelegateDeclarativeNG::OnSurfaceChanged()
639 {
640 if (mediaQueryInfo_->GetIsInit()) {
641 mediaQueryInfo_->SetIsInit(false);
642 }
643 mediaQueryInfo_->EnsureListenerIdValid();
644 OnMediaQueryUpdate();
645 }
646
ShowDialog(const std::string & title,const std::string & message,const std::vector<ButtonInfo> & buttons,bool autoCancel,std::function<void (int32_t,int32_t)> && callback,const std::set<std::string> & callbacks)647 void FrontendDelegateDeclarativeNG::ShowDialog(const std::string& title, const std::string& message,
648 const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
649 const std::set<std::string>& callbacks)
650 {
651 TAG_LOGD(AceLogTag::ACE_OVERLAY, "show dialog enter");
652 DialogProperties dialogProperties = {
653 .type = DialogType::ALERT_DIALOG,
654 .title = title,
655 .content = message,
656 .autoCancel = autoCancel,
657 .buttons = buttons,
658 };
659 ShowDialogInner(dialogProperties, std::move(callback), callbacks);
660 }
661
ShowDialog(const std::string & title,const std::string & message,const std::vector<ButtonInfo> & buttons,bool autoCancel,std::function<void (int32_t,int32_t)> && callback,const std::set<std::string> & callbacks,std::function<void (bool)> && onStatusChanged)662 void FrontendDelegateDeclarativeNG::ShowDialog(const std::string& title, const std::string& message,
663 const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
664 const std::set<std::string>& callbacks, std::function<void(bool)>&& onStatusChanged)
665 {
666 TAG_LOGD(AceLogTag::ACE_OVERLAY, "show dialog enter");
667 DialogProperties dialogProperties = {
668 .type = DialogType::ALERT_DIALOG,
669 .title = title,
670 .content = message,
671 .autoCancel = autoCancel,
672 .buttons = buttons,
673 .onStatusChanged = std::move(onStatusChanged),
674 };
675 ShowDialogInner(dialogProperties, std::move(callback), callbacks);
676 }
677
ShowDialog(const PromptDialogAttr & dialogAttr,const std::vector<ButtonInfo> & buttons,std::function<void (int32_t,int32_t)> && callback,const std::set<std::string> & callbacks)678 void FrontendDelegateDeclarativeNG::ShowDialog(const PromptDialogAttr& dialogAttr,
679 const std::vector<ButtonInfo>& buttons, std::function<void(int32_t, int32_t)>&& callback,
680 const std::set<std::string>& callbacks)
681 {
682 TAG_LOGD(AceLogTag::ACE_OVERLAY, "show dialog enter");
683 DialogProperties dialogProperties = {
684 .type = DialogType::ALERT_DIALOG,
685 .title = dialogAttr.title,
686 .content = dialogAttr.message,
687 .autoCancel = dialogAttr.autoCancel,
688 .buttons = buttons,
689 .isShowInSubWindow = dialogAttr.showInSubWindow,
690 .isModal = dialogAttr.isModal,
691 .enableHoverMode = dialogAttr.enableHoverMode,
692 .maskRect = dialogAttr.maskRect,
693 };
694 if (dialogAttr.alignment.has_value()) {
695 dialogProperties.alignment = dialogAttr.alignment.value();
696 }
697 if (dialogAttr.offset.has_value()) {
698 dialogProperties.offset = dialogAttr.offset.value();
699 }
700 if (dialogAttr.shadow.has_value()) {
701 dialogProperties.shadow = dialogAttr.shadow.value();
702 }
703 if (dialogAttr.backgroundColor.has_value()) {
704 dialogProperties.backgroundColor = dialogAttr.backgroundColor.value();
705 }
706 if (dialogAttr.backgroundBlurStyle.has_value()) {
707 dialogProperties.backgroundBlurStyle = dialogAttr.backgroundBlurStyle.value();
708 }
709 if (dialogAttr.hoverModeArea.has_value()) {
710 dialogProperties.hoverModeArea = dialogAttr.hoverModeArea.value();
711 }
712 ShowDialogInner(dialogProperties, std::move(callback), callbacks);
713 }
714
ShowDialog(const PromptDialogAttr & dialogAttr,const std::vector<ButtonInfo> & buttons,std::function<void (int32_t,int32_t)> && callback,const std::set<std::string> & callbacks,std::function<void (bool)> && onStatusChanged)715 void FrontendDelegateDeclarativeNG::ShowDialog(const PromptDialogAttr& dialogAttr,
716 const std::vector<ButtonInfo>& buttons, std::function<void(int32_t, int32_t)>&& callback,
717 const std::set<std::string>& callbacks, std::function<void(bool)>&& onStatusChanged)
718 {
719 TAG_LOGD(AceLogTag::ACE_OVERLAY, "show dialog enter");
720 DialogProperties dialogProperties = {
721 .type = DialogType::ALERT_DIALOG,
722 .title = dialogAttr.title,
723 .content = dialogAttr.message,
724 .autoCancel = dialogAttr.autoCancel,
725 .buttons = buttons,
726 .isShowInSubWindow = dialogAttr.showInSubWindow,
727 .isModal = dialogAttr.isModal,
728 .onStatusChanged = std::move(onStatusChanged),
729 .maskRect = dialogAttr.maskRect,
730 };
731 if (dialogAttr.alignment.has_value()) {
732 dialogProperties.alignment = dialogAttr.alignment.value();
733 }
734 if (dialogAttr.offset.has_value()) {
735 dialogProperties.offset = dialogAttr.offset.value();
736 }
737 ShowDialogInner(dialogProperties, std::move(callback), callbacks);
738 }
739
ParsePropertiesFromAttr(const PromptDialogAttr & dialogAttr)740 DialogProperties FrontendDelegateDeclarativeNG::ParsePropertiesFromAttr(const PromptDialogAttr &dialogAttr)
741 {
742 DialogProperties dialogProperties = { .autoCancel = dialogAttr.autoCancel,
743 .customStyle = dialogAttr.customStyle,
744 .onWillDismiss = dialogAttr.customOnWillDismiss,
745 .maskColor = dialogAttr.maskColor,
746 .backgroundColor = dialogAttr.backgroundColor,
747 .borderRadius = dialogAttr.borderRadius,
748 .isShowInSubWindow = dialogAttr.showInSubWindow,
749 .isModal = dialogAttr.isModal,
750 .enableHoverMode = dialogAttr.enableHoverMode,
751 .customBuilder = dialogAttr.customBuilder,
752 .borderWidth = dialogAttr.borderWidth,
753 .borderColor = dialogAttr.borderColor,
754 .borderStyle = dialogAttr.borderStyle,
755 .shadow = dialogAttr.shadow,
756 .width = dialogAttr.width,
757 .height = dialogAttr.height,
758 .maskRect = dialogAttr.maskRect,
759 .transitionEffect = dialogAttr.transitionEffect,
760 .contentNode = dialogAttr.contentNode,
761 .onDidAppear = dialogAttr.onDidAppear,
762 .onDidDisappear = dialogAttr.onDidDisappear,
763 .onWillAppear = dialogAttr.onWillAppear,
764 .onWillDisappear = dialogAttr.onWillDisappear,
765 .keyboardAvoidMode = dialogAttr.keyboardAvoidMode };
766 #if defined(PREVIEW)
767 if (dialogProperties.isShowInSubWindow) {
768 LOGW("[Engine Log] Unable to use the SubWindow in the Previewer. Perform this operation on the "
769 "emulator or a real device instead.");
770 dialogProperties.isShowInSubWindow = false;
771 }
772 #endif
773 if (dialogAttr.alignment.has_value()) {
774 dialogProperties.alignment = dialogAttr.alignment.value();
775 }
776 if (dialogAttr.offset.has_value()) {
777 dialogProperties.offset = dialogAttr.offset.value();
778 }
779 if (dialogAttr.hoverModeArea.has_value()) {
780 dialogProperties.hoverModeArea = dialogAttr.hoverModeArea.value();
781 }
782 if (Container::LessThanAPIVersion(PlatformVersion::VERSION_TWELVE)) {
783 dialogProperties.isSysBlurStyle = false;
784 } else {
785 if (dialogAttr.backgroundBlurStyle.has_value()) {
786 dialogProperties.backgroundBlurStyle = dialogAttr.backgroundBlurStyle.value();
787 }
788 }
789 return dialogProperties;
790 }
791
OpenCustomDialog(const PromptDialogAttr & dialogAttr,std::function<void (int32_t)> && callback)792 void FrontendDelegateDeclarativeNG::OpenCustomDialog(const PromptDialogAttr &dialogAttr,
793 std::function<void(int32_t)> &&callback)
794 {
795 DialogProperties dialogProperties = ParsePropertiesFromAttr(dialogAttr);
796 if (Container::IsCurrentUseNewPipeline()) {
797 auto task = [dialogAttr, dialogProperties, callback](const RefPtr<NG::OverlayManager>& overlayManager) mutable {
798 CHECK_NULL_VOID(overlayManager);
799 TAG_LOGD(AceLogTag::ACE_OVERLAY, "Begin to open custom dialog ");
800 if (dialogProperties.isShowInSubWindow) {
801 SubwindowManager::GetInstance()->OpenCustomDialogNG(dialogProperties, std::move(callback));
802 if (dialogProperties.isModal) {
803 TAG_LOGW(AceLogTag::ACE_OVERLAY, "temporary not support isShowInSubWindow and isModal");
804 }
805 } else {
806 overlayManager->OpenCustomDialog(dialogProperties, std::move(callback));
807 }
808 };
809 MainWindowOverlay(std::move(task), "ArkUIOverlayOpenCustomDialog");
810 return;
811 } else {
812 TAG_LOGW(AceLogTag::ACE_OVERLAY, "not support old pipeline");
813 }
814 }
815
CloseCustomDialog(const int32_t dialogId)816 void FrontendDelegateDeclarativeNG::CloseCustomDialog(const int32_t dialogId)
817 {
818 auto task = [dialogId](const RefPtr<NG::OverlayManager>& overlayManager) {
819 CHECK_NULL_VOID(overlayManager);
820 TAG_LOGD(AceLogTag::ACE_OVERLAY, "begin to close custom dialog.");
821 overlayManager->CloseCustomDialog(dialogId);
822 SubwindowManager::GetInstance()->CloseCustomDialogNG(dialogId);
823 };
824 MainWindowOverlay(std::move(task), "ArkUIOverlayCloseCustomDialog");
825 return;
826 }
827
CloseCustomDialog(const WeakPtr<NG::UINode> & node,std::function<void (int32_t)> && callback)828 void FrontendDelegateDeclarativeNG::CloseCustomDialog(const WeakPtr<NG::UINode>& node,
829 std::function<void(int32_t)> &&callback)
830 {
831 auto task = [node, callback](const RefPtr<NG::OverlayManager>& overlayManager) mutable {
832 CHECK_NULL_VOID(overlayManager);
833 TAG_LOGD(AceLogTag::ACE_OVERLAY, "begin to close custom dialog.");
834 overlayManager->CloseCustomDialog(node, std::move(callback));
835 SubwindowManager::GetInstance()->CloseCustomDialogNG(node, std::move(callback));
836 };
837 MainWindowOverlay(std::move(task), "ArkUIOverlayCloseCustomDialog");
838 return;
839 }
840
UpdateCustomDialog(const WeakPtr<NG::UINode> & node,const PromptDialogAttr & dialogAttr,std::function<void (int32_t)> && callback)841 void FrontendDelegateDeclarativeNG::UpdateCustomDialog(
842 const WeakPtr<NG::UINode>& node, const PromptDialogAttr &dialogAttr, std::function<void(int32_t)> &&callback)
843 {
844 DialogProperties dialogProperties = {
845 .autoCancel = dialogAttr.autoCancel,
846 .maskColor = dialogAttr.maskColor,
847 .isSysBlurStyle = false
848 };
849 if (dialogAttr.alignment.has_value()) {
850 dialogProperties.alignment = dialogAttr.alignment.value();
851 }
852 if (dialogAttr.offset.has_value()) {
853 dialogProperties.offset = dialogAttr.offset.value();
854 }
855 auto task = [dialogAttr, dialogProperties, node, callback]
856 (const RefPtr<NG::OverlayManager>& overlayManager) mutable {
857 CHECK_NULL_VOID(overlayManager);
858 TAG_LOGD(AceLogTag::ACE_OVERLAY, "begin to update custom dialog.");
859 overlayManager->UpdateCustomDialog(node, dialogProperties, std::move(callback));
860 SubwindowManager::GetInstance()->UpdateCustomDialogNG(node, dialogAttr, std::move(callback));
861 };
862 MainWindowOverlay(std::move(task), "ArkUIOverlayUpdateCustomDialog");
863 return;
864 }
865
ShowActionMenu(const std::string & title,const std::vector<ButtonInfo> & button,std::function<void (int32_t,int32_t)> && callback)866 void FrontendDelegateDeclarativeNG::ShowActionMenu(
867 const std::string& title, const std::vector<ButtonInfo>& button, std::function<void(int32_t, int32_t)>&& callback)
868 {
869 TAG_LOGD(AceLogTag::ACE_OVERLAY, "show action menu enter");
870 DialogProperties dialogProperties = {
871 .title = title,
872 .autoCancel = true,
873 .isMenu = true,
874 .buttons = button,
875 };
876 ShowActionMenuInner(dialogProperties, button, std::move(callback));
877 }
878
ShowActionMenu(const std::string & title,const std::vector<ButtonInfo> & button,std::function<void (int32_t,int32_t)> && callback,std::function<void (bool)> && onStatusChanged)879 void FrontendDelegateDeclarativeNG::ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
880 std::function<void(int32_t, int32_t)>&& callback, std::function<void(bool)>&& onStatusChanged)
881 {
882 TAG_LOGD(AceLogTag::ACE_OVERLAY, "show action menu enter");
883 DialogProperties dialogProperties = {
884 .title = title,
885 .autoCancel = true,
886 .isMenu = true,
887 .buttons = button,
888 .onStatusChanged = std::move(onStatusChanged),
889 };
890 ShowActionMenuInner(dialogProperties, button, std::move(callback));
891 }
892
OnMediaQueryUpdate(bool isSynchronous)893 void FrontendDelegateDeclarativeNG::OnMediaQueryUpdate(bool isSynchronous)
894 {
895 auto containerId = Container::CurrentId();
896 if (containerId < 0) {
897 auto container = Container::GetActive();
898 if (container) {
899 containerId = container->GetInstanceId();
900 }
901 }
902 bool isInSubwindow = containerId >= 1000000;
903 if (isInSubwindow) {
904 return;
905 }
906 if (mediaQueryInfo_->GetIsInit()) {
907 return;
908 }
909
910 taskExecutor_->PostTask(
911 [weak = AceType::WeakClaim(this)] {
912 auto delegate = weak.Upgrade();
913 if (!delegate) {
914 return;
915 }
916 const auto& info = delegate->mediaQueryInfo_->GetMediaQueryInfo();
917
918 // request js media query
919 const auto& listenerId = delegate->mediaQueryInfo_->GetListenerId();
920 delegate->mediaQueryCallback_(listenerId, info);
921 delegate->mediaQueryInfo_->ResetListenerId();
922 },
923 TaskExecutor::TaskType::JS, "ArkUIMediaQueryUpdate");
924 }
925
OnLayoutCompleted(const std::string & componentId)926 void FrontendDelegateDeclarativeNG::OnLayoutCompleted(const std::string& componentId)
927 {
928 taskExecutor_->PostTask(
929 [weak = AceType::WeakClaim(this), componentId] {
930 auto delegate = weak.Upgrade();
931 if (!delegate) {
932 return;
933 }
934 delegate->layoutInspectorCallback_(componentId);
935 },
936 TaskExecutor::TaskType::JS, "ArkUILayoutCompleted");
937 }
938
OnDrawCompleted(const std::string & componentId)939 void FrontendDelegateDeclarativeNG::OnDrawCompleted(const std::string& componentId)
940 {
941 taskExecutor_->PostTask(
942 [weak = AceType::WeakClaim(this), componentId] {
943 auto delegate = weak.Upgrade();
944 if (!delegate) {
945 return;
946 }
947 delegate->drawInspectorCallback_(componentId);
948 },
949 TaskExecutor::TaskType::JS, "ArkUIDrawCompleted");
950 }
951
SetColorMode(ColorMode colorMode)952 void FrontendDelegateDeclarativeNG::SetColorMode(ColorMode colorMode)
953 {
954 OnMediaQueryUpdate();
955 }
956
OnForeground()957 void FrontendDelegateDeclarativeNG::OnForeground()
958 {
959 if (!isFirstNotifyShow_) {
960 OnPageShow();
961 }
962 isFirstNotifyShow_ = false;
963 }
964
GetCurrentPageUrl()965 std::string FrontendDelegateDeclarativeNG::GetCurrentPageUrl()
966 {
967 CHECK_NULL_RETURN(pageRouterManager_, "");
968 return pageRouterManager_->GetCurrentPageUrl();
969 }
970
971 // Get the currently running JS page information in NG structure.
GetCurrentPageSourceMap()972 RefPtr<RevSourceMap> FrontendDelegateDeclarativeNG::GetCurrentPageSourceMap()
973 {
974 CHECK_NULL_RETURN(pageRouterManager_, nullptr);
975 return pageRouterManager_->GetCurrentPageSourceMap(assetManager_);
976 }
977
978 // Get the currently running JS page information in NG structure.
GetFaAppSourceMap()979 RefPtr<RevSourceMap> FrontendDelegateDeclarativeNG::GetFaAppSourceMap()
980 {
981 if (appSourceMap_) {
982 return appSourceMap_;
983 }
984 std::string appMap;
985 if (GetAssetContent("app.js.map", appMap)) {
986 appSourceMap_ = AceType::MakeRefPtr<RevSourceMap>();
987 appSourceMap_->Init(appMap);
988 }
989 return appSourceMap_;
990 }
991
GetStageSourceMap(std::unordered_map<std::string,RefPtr<Framework::RevSourceMap>> & sourceMaps)992 void FrontendDelegateDeclarativeNG::GetStageSourceMap(
993 std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMaps)
994 {
995 std::string maps;
996 if (GetAssetContent(MERGE_SOURCEMAPS_PATH, maps)) {
997 auto SourceMap = AceType::MakeRefPtr<RevSourceMap>();
998 SourceMap->StageModeSourceMapSplit(maps, sourceMaps);
999 }
1000 }
1001
CallPopPage()1002 void FrontendDelegateDeclarativeNG::CallPopPage()
1003 {
1004 Back("", "");
1005 }
1006
PostponePageTransition()1007 void FrontendDelegateDeclarativeNG::PostponePageTransition()
1008 {
1009 taskExecutor_->PostTask(
1010 [weak = AceType::WeakClaim(this)] {
1011 auto delegate = weak.Upgrade();
1012 if (!delegate) {
1013 return;
1014 }
1015 auto pipelineContext = delegate->pipelineContextHolder_.Get();
1016 pipelineContext->PostponePageTransition();
1017 },
1018 TaskExecutor::TaskType::UI, "ArkUIPostponePageTransition");
1019 }
1020
LaunchPageTransition()1021 void FrontendDelegateDeclarativeNG::LaunchPageTransition()
1022 {
1023 taskExecutor_->PostTask(
1024 [weak = AceType::WeakClaim(this)] {
1025 auto delegate = weak.Upgrade();
1026 if (!delegate) {
1027 return;
1028 }
1029 auto pipelineContext = delegate->pipelineContextHolder_.Get();
1030 pipelineContext->LaunchPageTransition();
1031 },
1032 TaskExecutor::TaskType::UI, "ArkUILaunchPageTransition");
1033 }
1034
GetComponentsCount()1035 size_t FrontendDelegateDeclarativeNG::GetComponentsCount()
1036 {
1037 CHECK_NULL_RETURN(pageRouterManager_, 0);
1038 auto pageNode = pageRouterManager_->GetCurrentPageNode();
1039 CHECK_NULL_RETURN(pageNode, 0);
1040 return pageNode->GetAllDepthChildrenCount();
1041 }
1042
ShowToast(const NG::ToastInfo & toastInfo)1043 void FrontendDelegateDeclarativeNG::ShowToast(const NG::ToastInfo& toastInfo)
1044 {
1045 TAG_LOGD(AceLogTag::ACE_OVERLAY, "show toast enter");
1046 NG::ToastInfo updatedToastInfo = toastInfo;
1047 updatedToastInfo.duration = std::clamp(toastInfo.duration, TOAST_TIME_DEFAULT, TOAST_TIME_MAX);
1048 updatedToastInfo.isRightToLeft = AceApplicationInfo::GetInstance().IsRightToLeft();
1049 auto task = [updatedToastInfo, containerId = Container::CurrentId()](
1050 const RefPtr<NG::OverlayManager>& overlayManager) {
1051 CHECK_NULL_VOID(overlayManager);
1052 ContainerScope scope(containerId);
1053 overlayManager->ShowToast(updatedToastInfo);
1054 };
1055 MainWindowOverlay(std::move(task), "ArkUIOverlayShowToast");
1056 }
1057
ShowDialogInner(DialogProperties & dialogProperties,std::function<void (int32_t,int32_t)> && callback,const std::set<std::string> & callbacks)1058 void FrontendDelegateDeclarativeNG::ShowDialogInner(DialogProperties& dialogProperties,
1059 std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks)
1060 {
1061 TAG_LOGD(AceLogTag::ACE_OVERLAY, "show dialog inner enter");
1062 dialogProperties.onSuccess = std::move(callback);
1063 dialogProperties.onCancel = [callback, taskExecutor = taskExecutor_] {
1064 taskExecutor->PostTask(
1065 [callback]() { callback(CALLBACK_ERRORCODE_CANCEL, CALLBACK_DATACODE_ZERO); },
1066 TaskExecutor::TaskType::JS, "ArkUIOverlayShowDialogCancel");
1067 };
1068 auto task = [dialogProperties](const RefPtr<NG::OverlayManager>& overlayManager) {
1069 LOGI("Begin to show dialog ");
1070 CHECK_NULL_VOID(overlayManager);
1071 auto container = Container::Current();
1072 CHECK_NULL_VOID(container);
1073 if (container->IsSubContainer()) {
1074 auto currentId = SubwindowManager::GetInstance()->GetParentContainerId(Container::CurrentId());
1075 container = AceEngine::Get().GetContainer(currentId);
1076 CHECK_NULL_VOID(container);
1077 }
1078 RefPtr<NG::FrameNode> dialog;
1079 if (dialogProperties.isShowInSubWindow) {
1080 dialog = SubwindowManager::GetInstance()->ShowDialogNG(dialogProperties, nullptr);
1081 CHECK_NULL_VOID(dialog);
1082 if (dialogProperties.isModal && !container->IsUIExtensionWindow()) {
1083 DialogProperties Maskarg;
1084 Maskarg.isMask = true;
1085 Maskarg.autoCancel = dialogProperties.autoCancel;
1086 auto mask = overlayManager->ShowDialog(Maskarg, nullptr, false);
1087 CHECK_NULL_VOID(mask);
1088 overlayManager->SetMaskNodeId(dialog->GetId(), mask->GetId());
1089 }
1090 } else {
1091 dialog = overlayManager->ShowDialog(
1092 dialogProperties, nullptr, AceApplicationInfo::GetInstance().IsRightToLeft());
1093 CHECK_NULL_VOID(dialog);
1094 }
1095 };
1096 MainWindowOverlay(std::move(task), "ArkUIOverlayShowDialog");
1097 return;
1098 }
1099
ShowActionMenuInner(DialogProperties & dialogProperties,const std::vector<ButtonInfo> & button,std::function<void (int32_t,int32_t)> && callback)1100 void FrontendDelegateDeclarativeNG::ShowActionMenuInner(DialogProperties& dialogProperties,
1101 const std::vector<ButtonInfo>& button, std::function<void(int32_t, int32_t)>&& callback)
1102 {
1103 TAG_LOGD(AceLogTag::ACE_OVERLAY, "show action menu inner enter");
1104 dialogProperties.onSuccess = std::move(callback);
1105 dialogProperties.onCancel = [callback, taskExecutor = taskExecutor_] {
1106 taskExecutor->PostTask(
1107 [callback]() { callback(CALLBACK_ERRORCODE_CANCEL, CALLBACK_DATACODE_ZERO); },
1108 TaskExecutor::TaskType::JS, "ArkUIOverlayShowActionMenuCancel");
1109 };
1110 auto context = DynamicCast<NG::PipelineContext>(pipelineContextHolder_.Get());
1111 auto overlayManager = context ? context->GetOverlayManager() : nullptr;
1112 taskExecutor_->PostTask(
1113 [dialogProperties, weak = WeakPtr<NG::OverlayManager>(overlayManager)] {
1114 auto overlayManager = weak.Upgrade();
1115 CHECK_NULL_VOID(overlayManager);
1116 overlayManager->ShowDialog(dialogProperties, nullptr, AceApplicationInfo::GetInstance().IsRightToLeft());
1117 },
1118 TaskExecutor::TaskType::UI, "ArkUIOverlayShowActionMenu");
1119 return;
1120 }
1121
EnableAlertBeforeBackPage(const std::string & message,std::function<void (int32_t)> && callback)1122 void FrontendDelegateDeclarativeNG::EnableAlertBeforeBackPage(
1123 const std::string& message, std::function<void(int32_t)>&& callback)
1124 {
1125 CHECK_NULL_VOID(pageRouterManager_);
1126 pageRouterManager_->EnableAlertBeforeBackPage(message, std::move(callback));
1127 return;
1128 }
1129
DisableAlertBeforeBackPage()1130 void FrontendDelegateDeclarativeNG::DisableAlertBeforeBackPage()
1131 {
1132 CHECK_NULL_VOID(pageRouterManager_);
1133 pageRouterManager_->DisableAlertBeforeBackPage();
1134 return;
1135 }
1136
RebuildAllPages()1137 void FrontendDelegateDeclarativeNG::RebuildAllPages()
1138 {
1139 CHECK_NULL_VOID(pageRouterManager_);
1140 auto url = pageRouterManager_->GetCurrentPageUrl();
1141 pageRouterManager_->Clear();
1142 pageRouterManager_->RunPage(url, "");
1143 return;
1144 }
1145
OnPageShow()1146 void FrontendDelegateDeclarativeNG::OnPageShow()
1147 {
1148 CHECK_NULL_VOID(pageRouterManager_);
1149 auto pageNode = pageRouterManager_->GetCurrentPageNode();
1150 CHECK_NULL_VOID(pageNode);
1151 auto pagePattern = pageNode->GetPattern<NG::PagePattern>();
1152 CHECK_NULL_VOID(pagePattern);
1153 pagePattern->OnShow();
1154 }
1155
OnPageHide()1156 void FrontendDelegateDeclarativeNG::OnPageHide()
1157 {
1158 CHECK_NULL_VOID(pageRouterManager_);
1159 auto pageNode = pageRouterManager_->GetCurrentPageNode();
1160 CHECK_NULL_VOID(pageNode);
1161 auto pagePattern = pageNode->GetPattern<NG::PagePattern>();
1162 CHECK_NULL_VOID(pagePattern);
1163 pagePattern->OnHide();
1164 }
1165
GetSnapshot(const std::string & componentId,NG::ComponentSnapshot::JsCallback && callback,const NG::SnapshotOptions & options)1166 void FrontendDelegateDeclarativeNG::GetSnapshot(
1167 const std::string& componentId, NG::ComponentSnapshot::JsCallback&& callback, const NG::SnapshotOptions& options)
1168 {
1169 #ifdef ENABLE_ROSEN_BACKEND
1170 NG::ComponentSnapshot::Get(componentId, std::move(callback), options);
1171 #endif
1172 }
1173
GetSyncSnapshot(const std::string & componentId,const NG::SnapshotOptions & options)1174 std::pair<int32_t, std::shared_ptr<Media::PixelMap>> FrontendDelegateDeclarativeNG::GetSyncSnapshot(
1175 const std::string& componentId, const NG::SnapshotOptions& options)
1176 {
1177 #ifdef ENABLE_ROSEN_BACKEND
1178 return NG::ComponentSnapshot::GetSync(componentId, options);
1179 #endif
1180 return {ERROR_CODE_INTERNAL_ERROR, nullptr};
1181 }
1182
GetContentInfo(ContentInfoType type)1183 std::string FrontendDelegateDeclarativeNG::GetContentInfo(ContentInfoType type)
1184 {
1185 auto jsonContentInfo = JsonUtil::Create(true);
1186
1187 CHECK_NULL_RETURN(pageRouterManager_, "");
1188 jsonContentInfo->Put("stackInfo", pageRouterManager_->GetStackInfo(type));
1189 if (type == ContentInfoType::RESOURCESCHEDULE_RECOVERY) {
1190 auto namedRouterInfo = pageRouterManager_->GetNamedRouterInfo();
1191 if (namedRouterInfo) {
1192 jsonContentInfo->Put("namedRouterInfo", std::move(namedRouterInfo));
1193 }
1194 }
1195
1196 if (type == ContentInfoType::CONTINUATION || type == ContentInfoType::APP_RECOVERY) {
1197 auto pipelineContext = pipelineContextHolder_.Get();
1198 CHECK_NULL_RETURN(pipelineContext, jsonContentInfo->ToString());
1199 jsonContentInfo->Put("nodeInfo", pipelineContext->GetStoredNodeInfo());
1200 }
1201
1202 return jsonContentInfo->ToString();
1203 }
1204
CreateSnapshot(std::function<void ()> && customBuilder,NG::ComponentSnapshot::JsCallback && callback,bool enableInspector,const NG::SnapshotParam & param)1205 void FrontendDelegateDeclarativeNG::CreateSnapshot(
1206 std::function<void()>&& customBuilder, NG::ComponentSnapshot::JsCallback&& callback, bool enableInspector,
1207 const NG::SnapshotParam& param)
1208 {
1209 #ifdef ENABLE_ROSEN_BACKEND
1210 ViewStackModel::GetInstance()->NewScope();
1211 CHECK_NULL_VOID(customBuilder);
1212 customBuilder();
1213 auto customNode = ViewStackModel::GetInstance()->Finish();
1214
1215 NG::ComponentSnapshot::Create(customNode, std::move(callback), enableInspector, param);
1216 #endif
1217 }
1218
AddFrameNodeToOverlay(const RefPtr<NG::FrameNode> & node,std::optional<int32_t> index)1219 void FrontendDelegateDeclarativeNG::AddFrameNodeToOverlay(
1220 const RefPtr<NG::FrameNode>& node, std::optional<int32_t> index)
1221 {
1222 auto task = [node, index, containerId = Container::CurrentId()](const RefPtr<NG::OverlayManager>& overlayManager) {
1223 CHECK_NULL_VOID(overlayManager);
1224 ContainerScope scope(containerId);
1225 overlayManager->AddFrameNodeToOverlay(node, index);
1226 };
1227 MainWindowOverlay(std::move(task), "ArkUIOverlayAddFrameNode");
1228 }
1229
RemoveFrameNodeOnOverlay(const RefPtr<NG::FrameNode> & node)1230 void FrontendDelegateDeclarativeNG::RemoveFrameNodeOnOverlay(const RefPtr<NG::FrameNode>& node)
1231 {
1232 auto task = [node, containerId = Container::CurrentId()](const RefPtr<NG::OverlayManager>& overlayManager) {
1233 CHECK_NULL_VOID(overlayManager);
1234 ContainerScope scope(containerId);
1235 overlayManager->RemoveFrameNodeOnOverlay(node);
1236 };
1237 MainWindowOverlay(std::move(task), "ArkUIOverlayRemoveFrameNode");
1238 }
1239
ShowNodeOnOverlay(const RefPtr<NG::FrameNode> & node)1240 void FrontendDelegateDeclarativeNG::ShowNodeOnOverlay(const RefPtr<NG::FrameNode>& node)
1241 {
1242 auto task = [node, containerId = Container::CurrentId()](const RefPtr<NG::OverlayManager>& overlayManager) {
1243 CHECK_NULL_VOID(overlayManager);
1244 ContainerScope scope(containerId);
1245 overlayManager->ShowNodeOnOverlay(node);
1246 };
1247 MainWindowOverlay(std::move(task), "ArkUIOverlayShowNode");
1248 }
1249
HideNodeOnOverlay(const RefPtr<NG::FrameNode> & node)1250 void FrontendDelegateDeclarativeNG::HideNodeOnOverlay(const RefPtr<NG::FrameNode>& node)
1251 {
1252 auto task = [node, containerId = Container::CurrentId()](const RefPtr<NG::OverlayManager>& overlayManager) {
1253 CHECK_NULL_VOID(overlayManager);
1254 ContainerScope scope(containerId);
1255 overlayManager->HideNodeOnOverlay(node);
1256 };
1257 MainWindowOverlay(std::move(task), "ArkUIOverlayHideNode");
1258 }
1259
ShowAllNodesOnOverlay()1260 void FrontendDelegateDeclarativeNG::ShowAllNodesOnOverlay()
1261 {
1262 auto task = [containerId = Container::CurrentId()](const RefPtr<NG::OverlayManager>& overlayManager) {
1263 CHECK_NULL_VOID(overlayManager);
1264 ContainerScope scope(containerId);
1265 overlayManager->ShowAllNodesOnOverlay();
1266 };
1267 MainWindowOverlay(std::move(task), "ArkUIOverlayShowAllNodes");
1268 }
1269
HideAllNodesOnOverlay()1270 void FrontendDelegateDeclarativeNG::HideAllNodesOnOverlay()
1271 {
1272 auto task = [containerId = Container::CurrentId()](const RefPtr<NG::OverlayManager>& overlayManager) {
1273 CHECK_NULL_VOID(overlayManager);
1274 ContainerScope scope(containerId);
1275 overlayManager->HideAllNodesOnOverlay();
1276 };
1277 MainWindowOverlay(std::move(task), "ArkUIOverlayHideAllNodes");
1278 }
1279 } // namespace OHOS::Ace::Framework
1280