1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_FRONTEND_DELEGATE_DECLARATIVE_NG_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_FRONTEND_DELEGATE_DECLARATIVE_NG_H
18 
19 #include "base/memory/ace_type.h"
20 #include "base/thread/cancelable_callback.h"
21 #include "base/utils/measure_util.h"
22 #include "core/components_ng/render/snapshot_param.h"
23 #include "frameworks/bridge/common/accessibility/accessibility_node_manager.h"
24 #include "frameworks/bridge/common/manifest/manifest_parser.h"
25 #include "frameworks/bridge/common/utils/pipeline_context_holder.h"
26 #include "frameworks/bridge/declarative_frontend/ng/page_router_manager.h"
27 #include "frameworks/bridge/js_frontend/engine/common/group_js_bridge.h"
28 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
29 #include "frameworks/bridge/js_frontend/frontend_delegate.h"
30 #include "frameworks/bridge/js_frontend/frontend_delegate_impl.h"
31 #include "frameworks/bridge/js_frontend/js_ace_page.h"
32 
33 namespace OHOS::Ace::Framework {
34 
35 class FrontendDelegateDeclarativeNG : public FrontendDelegate {
36     DECLARE_ACE_TYPE(FrontendDelegateDeclarativeNG, FrontendDelegate);
37 
38 public:
39     FrontendDelegateDeclarativeNG(const RefPtr<TaskExecutor>& taskExecutor);
40     ~FrontendDelegateDeclarativeNG() override = default;
41 
42     void AttachPipelineContext(const RefPtr<PipelineBase>& context) override;
43     void AttachSubPipelineContext(const RefPtr<PipelineBase>& context);
44     // restore
RestoreRouterStack(const std::string & contentInfo,ContentInfoType type)45     std::pair<RouterRecoverRecord, UIContentErrorCode> RestoreRouterStack(
46         const std::string& contentInfo, ContentInfoType type) override
47     {
48         return std::make_pair(RouterRecoverRecord(), UIContentErrorCode::NO_ERRORS);
49     }
50     std::string GetContentInfo(ContentInfoType type) override;
51     // JSFrontend delegate NG functions.
52     void RunPage(const std::string& url, const std::string& params,
53         const std::string& profile, bool isNamedRouter = false);
54     void RunPage(const std::shared_ptr<std::vector<uint8_t>>& content,
55         const std::string& params, const std::string& profile);
56     void OnConfigurationUpdated(const std::string& data);
57     bool OnStartContinuation();
58     void OnCompleteContinuation(int32_t code);
59     void OnSaveData(std::string& data);
60     bool OnRestoreData(const std::string& data);
61     void OnRemoteTerminated();
62     void SetColorMode(ColorMode colorMode);
63     void CallPopPage();
64     void OnApplicationDestroy(const std::string& packageName);
65     void UpdateApplicationState(const std::string& packageName, Frontend::State state);
66     void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data);
67     void NotifyAppStorage(
68         const WeakPtr<Framework::JsEngine>& jsEngineWeak, const std::string& key, const std::string& value);
69 
70     // set callback
71     void SetMediaQueryCallback(MediaQueryCallback&& mediaQueryCallback);
72     void SetLayoutInspectorCallback(const LayoutInspectorCallback& layoutInspectorCallback);
73     void SetDrawInspectorCallback(const DrawInspectorCallback& drawInspectorCallback);
74     void SetOnStartContinuationCallBack(OnStartContinuationCallBack&& onStartContinuationCallBack);
75     void SetOnCompleteContinuationCallBack(OnCompleteContinuationCallBack&& onCompleteContinuationCallBack);
76     void SetOnSaveDataCallBack(OnSaveDataCallBack&& onSaveDataCallBack);
77     void SetOnRemoteTerminatedCallBack(OnRemoteTerminatedCallBack&& onRemoteTerminatedCallBack);
78     void SetOnRestoreDataCallBack(OnRestoreDataCallBack&& onRestoreDataCallBack);
79     void SetDestroyApplicationCallback(DestroyApplicationCallback&& destroyApplicationCallback);
80     void SetUpdateApplicationStateCallback(UpdateApplicationStateCallback&& updateApplicationStateCallback);
81     void SetOnWindowDisplayModeChangedCallback(
82         OnWindowDisplayModeChangedCallBack&& onWindowDisplayModeChangedCallBack);
83     void SetExternalEventCallback(ExternalEventCallback&& externalEventCallback);
84     void SetTimerCallback(TimerCallback&& timerCallback);
85 
GetJSAccessibilityManager()86     RefPtr<Framework::AccessibilityNodeManager> GetJSAccessibilityManager() const
87     {
88         return jsAccessibilityManager_;
89     }
90     void FireAccessibilityEvent(const AccessibilityEvent& accessibilityEvent);
91     void InitializeAccessibilityCallback();
92 
93     void OnSurfaceChanged();
94     void OnMediaQueryUpdate(bool isSynchronous = false) override;
95     void OnLayoutCompleted(const std::string& componentId);
96     void OnDrawCompleted(const std::string& componentId);
97     void FireExternalEvent(const std::string& eventId, const std::string& componentId, uint32_t nodeId, bool isDestroy);
98 
99     // FrontendDelegate overrides.
100     void Push(const std::string& uri, const std::string& params) override;
101     void PushWithMode(const std::string& uri, const std::string& params, uint32_t routerMode) override;
102     void PushWithCallback(const std::string& uri, const std::string& params, bool recoverable,
103         const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override;
104     void PushNamedRoute(const std::string& uri, const std::string& params, bool recoverable,
105         const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override;
106     void Replace(const std::string& uri, const std::string& params) override;
107     void ReplaceWithMode(const std::string& uri, const std::string& params, uint32_t routerMode) override;
108     void ReplaceWithCallback(const std::string& uri, const std::string& params, bool recoverable,
109         const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override;
110     void ReplaceNamedRoute(const std::string& uri, const std::string& params, bool recoverable,
111         const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override;
112     void Back(const std::string& uri, const std::string& params) override;
113     void BackToIndex(int32_t index, const std::string& params) override;
114     void PostponePageTransition() override;
115     void LaunchPageTransition() override;
116     void Clear() override;
117     int32_t GetStackSize() const override;
118     void GetState(int32_t& index, std::string& name, std::string& path) override;
119     void GetRouterStateByIndex(int32_t& index, std::string& name, std::string& path, std::string& params) override;
120     void GetRouterStateByUrl(std::string& url, std::vector<StateInfo>& stateArray) override;
GetPage(int32_t pageId)121     RefPtr<JsAcePage> GetPage(int32_t pageId) const override
122     {
123         return nullptr;
124     }
125     size_t GetComponentsCount() override;
126     std::string GetParams() override;
127     void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params);
128 
129     void TriggerPageUpdate(int32_t pageId, bool directExecute = false) override {}
130 
131     void PostJsTask(std::function<void()>&& task, const std::string& name) override;
132     const std::string& GetAppID() const override;
133     const std::string& GetAppName() const override;
134     const std::string& GetVersionName() const override;
135     int32_t GetVersionCode() const override;
136 
137     double MeasureText(MeasureContext context) override;
138     Size MeasureTextSize(MeasureContext context) override;
139     void ShowToast(const NG::ToastInfo& toastInfo) override;
140     void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons,
141         bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
142         const std::set<std::string>& callbacks) override;
143     void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons,
144         bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks,
145         std::function<void(bool)>&& onStatusChanged) override;
146     void ShowDialog(const PromptDialogAttr &dialogAttr, const std::vector<ButtonInfo> &buttons,
147         std::function<void(int32_t, int32_t)> &&callback, const std::set<std::string> &callbacks) override;
148     void ShowDialog(const PromptDialogAttr &dialogAttr, const std::vector<ButtonInfo> &buttons,
149         std::function<void(int32_t, int32_t)> &&callback, const std::set<std::string> &callbacks,
150         std::function<void(bool)>&& onStatusChanged) override;
151     void OpenCustomDialog(const PromptDialogAttr &dialogAttr, std::function<void(int32_t)> &&callback) override;
152     void CloseCustomDialog(const int32_t dialogId) override;
153     void CloseCustomDialog(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)> &&callback) override;
154     void UpdateCustomDialog(const WeakPtr<NG::UINode>& node, const PromptDialogAttr &dialogAttr,
155         std::function<void(int32_t)> &&callback) override;
156 
157     void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback) override;
158 
159     void DisableAlertBeforeBackPage() override;
160 
161     void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
162         std::function<void(int32_t, int32_t)>&& callback) override;
163 
164     void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
165         std::function<void(int32_t, int32_t)>&& callback, std::function<void(bool)>&& onStatusChanged) override;
166 
167     void ShowActionMenuInner(DialogProperties& dialogProperties, const std::vector<ButtonInfo>& button,
168         std::function<void(int32_t, int32_t)>&& callback);
169 
170     void WaitTimer(const std::string& callbackId, const std::string& delay, bool isInterval, bool isFirst) override;
171     void ClearTimer(const std::string& callbackId) override;
AddTaskObserver(std::function<void ()> && task)172     void AddTaskObserver(std::function<void()>&& task) override {}
RemoveTaskObserver()173     void RemoveTaskObserver() override {}
PushJsCallbackToRenderNode(NodeId id,double ratio,std::function<void (bool,double)> && callback)174     void PushJsCallbackToRenderNode(NodeId id, double ratio, std::function<void(bool, double)>&& callback) override {}
175     void PostSyncTaskToPage(std::function<void()>&& task, const std::string& name) override;
SetCallBackResult(const std::string & callBackId,const std::string & result)176     void SetCallBackResult(const std::string& callBackId, const std::string& result) override {}
177     bool GetAssetContent(const std::string& url, std::string& content) override;
178     bool GetAssetContent(const std::string& url, std::vector<uint8_t>& content) override;
179     std::string GetAssetPath(const std::string& url) override;
180     // i18n
GetI18nData(std::unique_ptr<JsonValue> & json)181     void GetI18nData(std::unique_ptr<JsonValue>& json) override {}
182 
GetResourceConfiguration(std::unique_ptr<JsonValue> & json)183     void GetResourceConfiguration(std::unique_ptr<JsonValue>& json) override {}
184 
GetConfigurationCommon(const std::string & filePath,std::unique_ptr<JsonValue> & data)185     void GetConfigurationCommon(const std::string& filePath, std::unique_ptr<JsonValue>& data) override {}
GetBoundingRectData(NodeId nodeId)186     Rect GetBoundingRectData(NodeId nodeId) override
187     {
188         return Rect();
189     }
190 
GetInspector(NodeId nodeId)191     std::string GetInspector(NodeId nodeId) override
192     {
193         return "";
194     }
GetMinPlatformVersion()195     int32_t GetMinPlatformVersion() override
196     {
197         return manifestParser_->GetMinPlatformVersion();
198     }
199 
LoadResourceConfiguration(std::map<std::string,std::string> & mediaResourceFileMap,std::unique_ptr<JsonValue> & currentResourceData)200     void LoadResourceConfiguration(std::map<std::string, std::string>& mediaResourceFileMap,
201         std::unique_ptr<JsonValue>& currentResourceData) override
202     {}
203 
204     void ChangeLocale(const std::string& language, const std::string& countryOrRegion) override;
205 
206     void RegisterFont(const std::string& familyName, const std::string& familySrc, const std::string& bundleName = "",
207         const std::string& moduleName = "") override;
208 
209     void GetSystemFontList(std::vector<std::string>& fontList) override;
210 
211     bool GetSystemFont(const std::string& fontName, FontInfo& fontInfo) override;
212 
213     void GetUIFontConfig(FontConfigJsonInfo& fontConfigJsonInfo) override;
214 
HandleImage(const std::string & src,std::function<void (bool,int32_t,int32_t)> && callback)215     void HandleImage(const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback) override {}
216 
217     void GetSnapshot(const std::string& componentId,
218         std::function<void(std::shared_ptr<Media::PixelMap>, int32_t, std::function<void()>)>&& callback,
219         const NG::SnapshotOptions& options) override;
220 
221     void CreateSnapshot(std::function<void()>&& customBuilder,
222         std::function<void(std::shared_ptr<Media::PixelMap>, int32_t, std::function<void()>)>&& callback,
223         bool enableInspector, const NG::SnapshotParam& param) override;
224 
225     std::pair<int32_t, std::shared_ptr<Media::PixelMap>> GetSyncSnapshot(const std::string& componentId,
226         const NG::SnapshotOptions& options) override;
227 
228     void AddFrameNodeToOverlay(
229         const RefPtr<NG::FrameNode>& node, std::optional<int32_t> index = std::nullopt) override;
230     void RemoveFrameNodeOnOverlay(const RefPtr<NG::FrameNode>& node) override;
231     void ShowNodeOnOverlay(const RefPtr<NG::FrameNode>& node) override;
232     void HideNodeOnOverlay(const RefPtr<NG::FrameNode>& node) override;
233     void ShowAllNodesOnOverlay() override;
234     void HideAllNodesOnOverlay() override;
235 
RequestAnimationFrame(const std::string & callbackId)236     void RequestAnimationFrame(const std::string& callbackId) override {}
237 
CancelAnimationFrame(const std::string & callbackId)238     void CancelAnimationFrame(const std::string& callbackId) override {}
239     SingleTaskExecutor GetAnimationJsTask() override;
240 
241     SingleTaskExecutor GetUiTask() override;
242 
GetMediaQueryInfoInstance()243     const RefPtr<MediaQueryInfo>& GetMediaQueryInfoInstance() override
244     {
245         return mediaQueryInfo_;
246     }
247 
SetGroupJsBridge(const RefPtr<GroupJsBridge> & groupJsBridge)248     void SetGroupJsBridge(const RefPtr<GroupJsBridge>& groupJsBridge)
249     {
250         groupJsBridge_ = groupJsBridge;
251     }
GetGroupJsBridge()252     const RefPtr<GroupJsBridge>& GetGroupJsBridge() override
253     {
254         return groupJsBridge_;
255     }
256 
257     RefPtr<PipelineBase> GetPipelineContext() override;
258 
SetPageRouterManager(const RefPtr<NG::PageRouterManager> & routerMgr)259     void SetPageRouterManager(const RefPtr<NG::PageRouterManager>& routerMgr)
260     {
261         pageRouterManager_ = routerMgr;
262     }
GetPageRouterManager()263     const RefPtr<NG::PageRouterManager>& GetPageRouterManager() const
264     {
265         return pageRouterManager_;
266     }
267 
GetWindowConfig()268     WindowConfig& GetWindowConfig()
269     {
270         return manifestParser_->GetWindowConfig();
271     }
272 
GetManifestParser()273     const RefPtr<Framework::ManifestParser>& GetManifestParser() const
274     {
275         return manifestParser_;
276     }
277 
GetTaskExecutor()278     const RefPtr<TaskExecutor>& GetTaskExecutor() const
279     {
280         return taskExecutor_;
281     }
282 
283     bool OnPageBackPress();
284 
285     void OnPageShow();
286 
287     void OnPageHide();
288     void OnForeground();
289 
290     std::string GetCurrentPageUrl();
291 
292     // Get the currently running JS page information in NG structure.
293     RefPtr<RevSourceMap> GetCurrentPageSourceMap();
294 
295     // Get the currently running JS page information in NG structure.
296     RefPtr<RevSourceMap> GetFaAppSourceMap();
297 
298     void GetStageSourceMap(std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap);
299     void ShowDialogInner(DialogProperties& dialogProperties, std::function<void(int32_t, int32_t)>&& callback,
300         const std::set<std::string>& callbacks);
301 
302     void RebuildAllPages();
303 
304 private:
305     DialogProperties ParsePropertiesFromAttr(const PromptDialogAttr &dialogAttr);
306 
307     PipelineContextHolder pipelineContextHolder_;
308     RefPtr<TaskExecutor> taskExecutor_;
309     RefPtr<NG::PageRouterManager> pageRouterManager_;
310     RefPtr<Framework::ManifestParser> manifestParser_;
311     RefPtr<MediaQueryInfo> mediaQueryInfo_;
312     RefPtr<GroupJsBridge> groupJsBridge_;
313 
314     RefPtr<Framework::AccessibilityNodeManager> jsAccessibilityManager_;
315     RefPtr<RevSourceMap> appSourceMap_;
316     std::queue<std::string> animationFrameTaskIds_;
317     std::unordered_map<std::string, CancelableCallback<void()>> animationFrameTaskMap_;
318 
319     std::unordered_map<std::string, CancelableCallback<void()>> timeoutTaskMap_;
320 
321     MediaQueryCallback mediaQueryCallback_;
322     LayoutInspectorCallback layoutInspectorCallback_;
323     DrawInspectorCallback drawInspectorCallback_;
324     OnStartContinuationCallBack onStartContinuationCallBack_;
325     OnCompleteContinuationCallBack onCompleteContinuationCallBack_;
326     OnSaveDataCallBack onSaveDataCallBack_;
327     OnRemoteTerminatedCallBack onRemoteTerminatedCallBack_;
328     OnRestoreDataCallBack onRestoreDataCallBack_;
329     DestroyApplicationCallback destroyApplication_;
330     UpdateApplicationStateCallback updateApplicationState_;
331     OnWindowDisplayModeChangedCallBack onWindowDisplayModeChanged_;
332     ExternalEventCallback externalEvent_;
333     TimerCallback timer_;
334 
335     mutable std::mutex mutex_;
336 };
337 
338 } // namespace OHOS::Ace::Framework
339 
340 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_FRONTEND_DELEGATE_DECLARATIVE_NG_H
341