1 /*
2  * Copyright (c) 2021-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_JS_FRONTEND_FRONTEND_DELEGATE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_FRONTEND_DELEGATE_H
18 
19 #include <vector>
20 
21 #include "base/geometry/size.h"
22 #include "base/json/json_util.h"
23 #include "base/memory/ace_type.h"
24 #include "base/utils/measure_util.h"
25 #include "base/utils/noncopyable.h"
26 #include "core/common/router_recover_record.h"
27 #include "core/components_ng/pattern/overlay/overlay_manager.h"
28 #include "core/components_ng/pattern/toast/toast_layout_property.h"
29 #include "core/components_ng/render/snapshot_param.h"
30 #include "core/event/ace_event_helper.h"
31 #include "core/pipeline/pipeline_base.h"
32 #include "frameworks/bridge/common/media_query/media_query_info.h"
33 #include "frameworks/bridge/common/utils/componentInfo.h"
34 #include "frameworks/bridge/js_frontend/engine/common/group_js_bridge.h"
35 #include "frameworks/bridge/js_frontend/engine/common/js_constants.h"
36 #include "interfaces/inner_api/ace/constants.h"
37 
38 namespace OHOS::Ace::Framework {
39 enum class AlertState { USER_CANCEL = 0, USER_CONFIRM, RECOVERY };
40 
41 typedef struct RouterStateInfo {
42     int32_t index = -1;
43     std::string name;
44     std::string path;
45     std::string params;
46 } StateInfo;
47 
48 class JsAcePage;
49 
50 // A virtual interface which must be implemented as a backing for
51 // FrontendDelegateImpl instances.
52 //
53 // This is the primary interface by which the JsFrontend delegates
54 // FrontendDelegateImpl behavior out to QjsEngine for js to native calls.
55 class FrontendDelegate : public AceType {
56     DECLARE_ACE_TYPE(FrontendDelegate, AceType);
57 
58 public:
59     FrontendDelegate() = default;
60     ~FrontendDelegate() override = default;
61 
62     virtual void AttachPipelineContext(const RefPtr<PipelineBase>& context) = 0;
SetAssetManager(const RefPtr<AssetManager>& assetManager)63     ACE_EXPORT void SetAssetManager(const RefPtr<AssetManager>& assetManager)
64     {
65         assetManager_ = assetManager;
66     }
67 
68     // ----------------
69     // system.router
70     // ----------------
71     // Jump to the specified page.
72     virtual void Push(const std::string& uri, const std::string& params) = 0;
PushWithMode(const std::string& uri, const std::string& params, uint32_t routerMode)73     virtual void PushWithMode(const std::string& uri, const std::string& params, uint32_t routerMode) {}
PushWithCallback(const std::string& uri, const std::string& params, bool recoverable, const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0)74     virtual void PushWithCallback(const std::string& uri, const std::string& params, bool recoverable,
75         const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0)
76     {}
PushNamedRoute(const std::string& uri, const std::string& params, bool recoverable, const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0)77     virtual void PushNamedRoute(const std::string& uri, const std::string& params, bool recoverable,
78         const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0)
79     {}
80     // Jump to the specified page, but current page will be removed from the stack.
81     virtual void Replace(const std::string& uri, const std::string& params) = 0;
ReplaceWithMode(const std::string& uri, const std::string& params, uint32_t routerMode)82     virtual void ReplaceWithMode(const std::string& uri, const std::string& params, uint32_t routerMode) {}
ReplaceWithCallback(const std::string& uri, const std::string& params, bool recoverable, const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0)83     virtual void ReplaceWithCallback(const std::string& uri, const std::string& params, bool recoverable,
84         const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0)
85     {}
ReplaceNamedRoute(const std::string& uri, const std::string& params, bool recoverable, const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0)86     virtual void ReplaceNamedRoute(const std::string& uri, const std::string& params, bool recoverable,
87         const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0)
88     {}
89     // Back to specified page or the previous page if url not set.
90     virtual void Back(const std::string& uri, const std::string& params = "") = 0;
91     // Back to specified page or the previous page if Index not set.
BackToIndex(int32_t index, const std::string& params = �)92     virtual void BackToIndex(int32_t index, const std::string& params = "")
93     {}
94     // Postpone page transition after Begin called, usually to wait some async operation
95     virtual void PostponePageTransition() = 0;
96     // Begin page transition after Postpone called, usually to wait some async operation
97     virtual void LaunchPageTransition() = 0;
98     // Clear all the pages in stack except the top page, that is current page.
99     virtual void Clear() = 0;
100     // Gets the number of pages in the page stack.
101     virtual int32_t GetStackSize() const = 0;
102     // Gets the index of current page, only used for PagePattern::OnAttachToMainTree.
GetCurrentPageIndex() const103     virtual int32_t GetCurrentPageIndex() const
104     {
105         return GetStackSize();
106     }
107     // Gets current page's states
108     virtual void GetState(int32_t& index, std::string& name, std::string& path) = 0;
109     // Gets page's states by index.
GetRouterStateByIndex(int32_t& index, std::string& name, std::string& path, std::string& params)110     virtual void GetRouterStateByIndex(int32_t& index, std::string& name, std::string& path, std::string& params)
111     {}
112     // Gets page's states by url.
GetRouterStateByUrl(std::string& url, std::vector<StateInfo>& stateArray)113     virtual void GetRouterStateByUrl(std::string& url, std::vector<StateInfo>& stateArray)
114     {}
115     // Gets current page's components count
116     virtual size_t GetComponentsCount() = 0;
117     // Gets page's index by url
GetIndexByUrl(const std::string& url)118     virtual int32_t GetIndexByUrl(const std::string& url)
119     {
120         return -1;
121     }
122     // Gets current page's params
GetParams()123     virtual std::string GetParams()
124     {
125         return "";
126     }
127     virtual void GetRectangleById(const std::string& key, NG::Rectangle& rectangle);
128 
129     virtual void ResetFocus();
130 
131     virtual bool RequestFocus(const std::string& value, bool isSyncRequest);
132 
133     virtual void SetRequestFocusCallback(std::function<void(NG::RequestFocusResult result)> callback);
134 
135     virtual void ResetRequestFocusCallback();
136 
137     virtual bool Activate(bool isActive, bool autoInactive = true);
138 
139     // restore
RestoreRouterStack( const std::string& contentInfo, ContentInfoType type)140     virtual std::pair<RouterRecoverRecord, UIContentErrorCode> RestoreRouterStack(
141         const std::string& contentInfo, ContentInfoType type)
142     {
143         return std::make_pair(RouterRecoverRecord(), UIContentErrorCode::NO_ERRORS);
144     }
GetContentInfo(ContentInfoType type)145     virtual std::string GetContentInfo(ContentInfoType type)
146     {
147         return "";
148     }
149 
150     virtual void TriggerPageUpdate(int32_t pageId, bool directExecute = false) = 0;
151 
152     // posting js task from jsengine
153     virtual void PostJsTask(std::function<void()>&& task, const std::string& name) = 0;
PostUITask(std::function<void()>&& task, const std::string& name)154     virtual void PostUITask(std::function<void()>&& task, const std::string& name) {}
155 
156     // ----------------
157     // system.app
158     // ----------------
159     virtual const std::string& GetAppID() const = 0;
160     virtual const std::string& GetAppName() const = 0;
161     virtual const std::string& GetVersionName() const = 0;
162     virtual int32_t GetVersionCode() const = 0;
163 
164     // ----------------
165     // system.measure
166     // ----------------
167     virtual double MeasureText(MeasureContext context) = 0;
168     virtual Size MeasureTextSize(MeasureContext context) = 0;
169 
170     // ----------------
171     // system.prompt
172     // ----------------
173     virtual void ShowToast(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback) = 0;
CloseToast(const int32_t toastId, std::function<void(int32_t)>&& callback)174     virtual void CloseToast(const int32_t toastId, std::function<void(int32_t)>&& callback) {};
SetToastStopListenerCallback(std::function<void()>&& stopCallback)175     virtual void SetToastStopListenerCallback(std::function<void()>&& stopCallback) {};
176     virtual void ShowDialog(const std::string& title, const std::string& message,
177         const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
178         const std::set<std::string>& callbacks) = 0;
ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons, std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks)179     virtual void ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
180         std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks) {};
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)181     virtual void ShowDialog(const std::string& title, const std::string& message,
182         const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
183         const std::set<std::string>& callbacks, std::function<void(bool)>&& onStatusChanged) {};
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)184     virtual void ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
185         std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks,
186         std::function<void(bool)>&& onStatusChanged) {};
RemoveCustomDialog(int32_t instanceId)187     virtual void RemoveCustomDialog(int32_t instanceId) {};
OpenCustomDialog(const PromptDialogAttr &dialogAttr, std::function<void(int32_t)> &&callback)188     virtual void OpenCustomDialog(const PromptDialogAttr &dialogAttr, std::function<void(int32_t)> &&callback) {};
CloseCustomDialog(const int32_t dialogId)189     virtual void CloseCustomDialog(const int32_t dialogId) {};
CloseCustomDialog(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)> &&callback)190     virtual void CloseCustomDialog(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)> &&callback) {};
UpdateCustomDialog(const WeakPtr<NG::UINode>& node, const PromptDialogAttr &dialogAttr, std::function<void(int32_t)> &&callback)191     virtual void UpdateCustomDialog(const WeakPtr<NG::UINode>& node, const PromptDialogAttr &dialogAttr,
192         std::function<void(int32_t)> &&callback) {};
193 
GetTransitionEffect(void* value)194     virtual RefPtr<NG::ChainedTransitionEffect> GetTransitionEffect(void* value)
195     {
196         return nullptr;
197     }
198 
199     virtual void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback) = 0;
200     virtual void DisableAlertBeforeBackPage() = 0;
201 
202     virtual void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
203         std::function<void(int32_t, int32_t)>&& callback) = 0;
ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, std::function<void(int32_t, int32_t)>&& callback, std::function<void(bool)>&& onStatusChanged)204     virtual void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
205         std::function<void(int32_t, int32_t)>&& callback, std::function<void(bool)>&& onStatusChanged) {};
ShowActionMenu(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons, std::function<void(int32_t, int32_t)>&& callback)206     virtual void ShowActionMenu(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
207         std::function<void(int32_t, int32_t)>&& callback) {};
208     virtual Rect GetBoundingRectData(NodeId nodeId) = 0;
209 
210     virtual std::string GetInspector(NodeId nodeId) = 0;
211 
212     virtual void PushJsCallbackToRenderNode(NodeId id, double ratio, std::function<void(bool, double)>&& callback) = 0;
213 
214     // ----------------
215     // system.configuration
216     // ----------------
217     virtual void ChangeLocale(const std::string& language, const std::string& countryOrRegion) = 0;
218 
219     // ----------------
220     // system.image
221     // ----------------
222     virtual void HandleImage(const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback) = 0;
223     // ----------------
224     // internal.jsResult
225     // ----------------
226     virtual void SetCallBackResult(const std::string& callBackId, const std::string& result) = 0;
227 
228     // ----------------
229     // system.animation
230     // ----------------
231     virtual void RequestAnimationFrame(const std::string& callbackId) = 0;
232     virtual void CancelAnimationFrame(const std::string& callbackId) = 0;
233 
GetSnapshot(const std::string& componentId, std::function<void(std::shared_ptr<Media::PixelMap>, int32_t, std::function<void()>)>&& callback, const NG::SnapshotOptions& options)234     virtual void GetSnapshot(const std::string& componentId,
235         std::function<void(std::shared_ptr<Media::PixelMap>, int32_t, std::function<void()>)>&& callback,
236         const NG::SnapshotOptions& options)
237     {}
CreateSnapshot(std::function<void()>&& customBuilder, std::function<void(std::shared_ptr<Media::PixelMap>, int32_t, std::function<void()>)>&& callback, bool enableInspector, const NG::SnapshotParam& param)238     virtual void CreateSnapshot(std::function<void()>&& customBuilder,
239         std::function<void(std::shared_ptr<Media::PixelMap>, int32_t, std::function<void()>)>&& callback,
240         bool enableInspector, const NG::SnapshotParam& param)
241     {}
242 
GetSyncSnapshot(const std::string& componentId, const NG::SnapshotOptions& options)243     virtual std::pair<int32_t, std::shared_ptr<Media::PixelMap>> GetSyncSnapshot(const std::string& componentId,
244         const NG::SnapshotOptions& options)
245     {
246         return {};
247     }
248 
249     virtual bool GetAssetContent(const std::string& url, std::string& content) = 0;
250     virtual bool GetAssetContent(const std::string& url, std::vector<uint8_t>& content) = 0;
251     virtual std::string GetAssetPath(const std::string& url) = 0;
252 
253     virtual void WaitTimer(const std::string& callbackId, const std::string& delay, bool isInterval, bool isFirst) = 0;
254     virtual void ClearTimer(const std::string& callbackId) = 0;
255 
256     virtual void PostSyncTaskToPage(std::function<void()>&& task, const std::string& name) = 0;
257 
258     virtual void AddTaskObserver(std::function<void()>&& task) = 0;
259 
260     virtual void RemoveTaskObserver() = 0;
261 
262     virtual void GetI18nData(std::unique_ptr<JsonValue>& json) = 0;
263 
264     virtual void GetResourceConfiguration(std::unique_ptr<JsonValue>& json) = 0;
265 
266     virtual void GetConfigurationCommon(const std::string& filePath, std::unique_ptr<JsonValue>& data) = 0;
267 
268     virtual const RefPtr<MediaQueryInfo>& GetMediaQueryInfoInstance() = 0;
269 
270     virtual void OnMediaQueryUpdate(bool isSynchronous = false) = 0;
271 
272     virtual void RegisterFont(const std::string& familyName, const std::string& familySrc,
273         const std::string& bundleName = "", const std::string& moduleName = "") = 0;
274 
275     virtual void GetSystemFontList(std::vector<std::string>& fontList) = 0;
276 
277     virtual bool GetSystemFont(const std::string& fontName, FontInfo& fontInfo) = 0;
278 
GetUIFontConfig(FontConfigJsonInfo& fontConfigJsonInfo)279     virtual void GetUIFontConfig(FontConfigJsonInfo& fontConfigJsonInfo) {}
280 
AddFrameNodeToOverlay( const RefPtr<NG::FrameNode>& node, std::optional<int32_t> index = std::nullopt)281     virtual void AddFrameNodeToOverlay(
282         const RefPtr<NG::FrameNode>& node, std::optional<int32_t> index = std::nullopt) {}
RemoveFrameNodeOnOverlay(const RefPtr<NG::FrameNode>& node)283     virtual void RemoveFrameNodeOnOverlay(const RefPtr<NG::FrameNode>& node) {}
ShowNodeOnOverlay(const RefPtr<NG::FrameNode>& node)284     virtual void ShowNodeOnOverlay(const RefPtr<NG::FrameNode>& node) {}
HideNodeOnOverlay(const RefPtr<NG::FrameNode>& node)285     virtual void HideNodeOnOverlay(const RefPtr<NG::FrameNode>& node) {}
ShowAllNodesOnOverlay()286     virtual void ShowAllNodesOnOverlay() {}
HideAllNodesOnOverlay()287     virtual void HideAllNodesOnOverlay() {}
288 
289     virtual SingleTaskExecutor GetAnimationJsTask() = 0;
290 
291     virtual SingleTaskExecutor GetUiTask() = 0;
292 
293     virtual RefPtr<PipelineBase> GetPipelineContext() = 0;
294 
295     virtual const RefPtr<GroupJsBridge>& GetGroupJsBridge() = 0;
296 
297     virtual RefPtr<JsAcePage> GetPage(int32_t pageId) const = 0;
298 
299     virtual int32_t GetMinPlatformVersion() = 0;
300 
301     template<typename T>
302     bool ACE_EXPORT GetResourceData(const std::string& fileUri, T& content);
303 
304     template<typename T>
305     bool ACE_EXPORT GetResourceData(const std::string& fileUri, T& content, std::string& ami);
306 
307     template<typename T>
308     ACE_EXPORT static bool GetResourceData(const std::string& fileUri, const RefPtr<AssetManager>& assetManager,
309         T& content);
310 
GetAssetManager() const311     ACE_EXPORT RefPtr<AssetManager> GetAssetManager() const
312     {
313         return assetManager_;
314     }
315 
316     virtual void LoadResourceConfiguration(std::map<std::string, std::string>& sortedResourcePath,
317         std::unique_ptr<JsonValue>& currentResourceData) = 0;
318 
DisallowPopLastPage()319     void DisallowPopLastPage()
320     {
321         disallowPopLastPage_ = true;
322     }
323 
CallNativeHandler(const std::string& event, const std::string& params)324     virtual void CallNativeHandler(const std::string& event, const std::string& params) {}
325 
326 protected:
327     RefPtr<AssetManager> assetManager_;
328     bool disallowPopLastPage_ = false;
329     bool isFirstNotifyShow_ = true;
330 
331     ACE_DISALLOW_COPY_AND_MOVE(FrontendDelegate);
332 };
333 
334 } // namespace OHOS::Ace::Framework
335 
336 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_FRONTEND_DELEGATE_H
337