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