• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) {}
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     {}
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) {}
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     {}
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.
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()103     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 void SetAutoFocusTransfer(bool autoFocusTransfer);
138     virtual void SetKeyProcessingMode(int32_t keyProcessingMode);
139     virtual bool Activate(bool isActive, bool autoInactive = true);
140 
141     // restore
RestoreRouterStack(const std::string & contentInfo,ContentInfoType type)142     virtual std::pair<RouterRecoverRecord, UIContentErrorCode> RestoreRouterStack(
143         const std::string& contentInfo, ContentInfoType type)
144     {
145         return std::make_pair(RouterRecoverRecord(), UIContentErrorCode::NO_ERRORS);
146     }
GetContentInfo(ContentInfoType type)147     virtual std::string GetContentInfo(ContentInfoType type)
148     {
149         return "";
150     }
151 
152     virtual void TriggerPageUpdate(int32_t pageId, bool directExecute = false) = 0;
153 
154     // posting js task from jsengine
155     virtual void PostJsTask(std::function<void()>&& task, const std::string& name) = 0;
PostUITask(std::function<void ()> && task,const std::string & name)156     virtual void PostUITask(std::function<void()>&& task, const std::string& name) {}
157 
158     // ----------------
159     // system.app
160     // ----------------
161     virtual const std::string& GetAppID() const = 0;
162     virtual const std::string& GetAppName() const = 0;
163     virtual const std::string& GetVersionName() const = 0;
164     virtual int32_t GetVersionCode() const = 0;
165 
166     // ----------------
167     // system.measure
168     // ----------------
169     virtual double MeasureText(MeasureContext context) = 0;
170     virtual Size MeasureTextSize(MeasureContext context) = 0;
171 
172     // ----------------
173     // system.prompt
174     // ----------------
175     virtual void ShowToast(const NG::ToastInfo& toastInfo) = 0;
SetToastStopListenerCallback(std::function<void ()> && stopCallback)176     virtual void SetToastStopListenerCallback(std::function<void()>&& stopCallback) {};
177     virtual void ShowDialog(const std::string& title, const std::string& message,
178         const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
179         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)180     virtual void ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
181         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)182     virtual void ShowDialog(const std::string& title, const std::string& message,
183         const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
184         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)185     virtual void ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
186         std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks,
187         std::function<void(bool)>&& onStatusChanged) {};
RemoveCustomDialog()188     virtual void RemoveCustomDialog() {};
OpenCustomDialog(const PromptDialogAttr & dialogAttr,std::function<void (int32_t)> && callback)189     virtual void OpenCustomDialog(const PromptDialogAttr &dialogAttr, std::function<void(int32_t)> &&callback) {};
CloseCustomDialog(const int32_t dialogId)190     virtual void CloseCustomDialog(const int32_t dialogId) {};
CloseCustomDialog(const WeakPtr<NG::UINode> & node,std::function<void (int32_t)> && callback)191     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)192     virtual void UpdateCustomDialog(const WeakPtr<NG::UINode>& node, const PromptDialogAttr &dialogAttr,
193         std::function<void(int32_t)> &&callback) {};
194 
GetTransitionEffect(void * value)195     virtual RefPtr<NG::ChainedTransitionEffect> GetTransitionEffect(void* value)
196     {
197         return nullptr;
198     }
199 
200     virtual void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback) = 0;
201     virtual void DisableAlertBeforeBackPage() = 0;
202 
203     virtual void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
204         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)205     virtual void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
206         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)207     virtual void ShowActionMenu(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons,
208         std::function<void(int32_t, int32_t)>&& callback) {};
209     virtual Rect GetBoundingRectData(NodeId nodeId) = 0;
210 
211     virtual std::string GetInspector(NodeId nodeId) = 0;
212 
213     virtual void PushJsCallbackToRenderNode(NodeId id, double ratio, std::function<void(bool, double)>&& callback) = 0;
214 
215     // ----------------
216     // system.configuration
217     // ----------------
218     virtual void ChangeLocale(const std::string& language, const std::string& countryOrRegion) = 0;
219 
220     // ----------------
221     // system.image
222     // ----------------
223     virtual void HandleImage(const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback) = 0;
224     // ----------------
225     // internal.jsResult
226     // ----------------
227     virtual void SetCallBackResult(const std::string& callBackId, const std::string& result) = 0;
228 
229     // ----------------
230     // system.animation
231     // ----------------
232     virtual void RequestAnimationFrame(const std::string& callbackId) = 0;
233     virtual void CancelAnimationFrame(const std::string& callbackId) = 0;
234 
GetSnapshot(const std::string & componentId,std::function<void (std::shared_ptr<Media::PixelMap>,int32_t,std::function<void ()>)> && callback,const NG::SnapshotOptions & options)235     virtual void GetSnapshot(const std::string& componentId,
236         std::function<void(std::shared_ptr<Media::PixelMap>, int32_t, std::function<void()>)>&& callback,
237         const NG::SnapshotOptions& options)
238     {}
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)239     virtual void CreateSnapshot(std::function<void()>&& customBuilder,
240         std::function<void(std::shared_ptr<Media::PixelMap>, int32_t, std::function<void()>)>&& callback,
241         bool enableInspector, const NG::SnapshotParam& param)
242     {}
243 
GetSyncSnapshot(RefPtr<NG::FrameNode> & node,const NG::SnapshotOptions & options)244     virtual std::pair<int32_t, std::shared_ptr<Media::PixelMap>> GetSyncSnapshot(
245         RefPtr<NG::FrameNode>& node, const NG::SnapshotOptions& options)
246     {
247         return {};
248     }
249 
GetSyncSnapshot(const std::string & componentId,const NG::SnapshotOptions & options)250     virtual std::pair<int32_t, std::shared_ptr<Media::PixelMap>> GetSyncSnapshot(const std::string& componentId,
251         const NG::SnapshotOptions& options)
252     {
253         return {};
254     }
255 
GetSnapshotByUniqueId(int32_t uniqueId,std::function<void (std::shared_ptr<Media::PixelMap>,int32_t,std::function<void ()>)> && callback,const NG::SnapshotOptions & options)256     virtual void GetSnapshotByUniqueId(int32_t uniqueId,
257         std::function<void(std::shared_ptr<Media::PixelMap>, int32_t, std::function<void()>)>&& callback,
258         const NG::SnapshotOptions& options)
259     {}
260 
GetSyncSnapshotByUniqueId(int32_t uniqueId,const NG::SnapshotOptions & options)261     virtual std::pair<int32_t, std::shared_ptr<Media::PixelMap>> GetSyncSnapshotByUniqueId(int32_t uniqueId,
262         const NG::SnapshotOptions& options)
263     {
264         return {};
265     }
266 
267     virtual bool GetAssetContent(const std::string& url, std::string& content) = 0;
268     virtual bool GetAssetContent(const std::string& url, std::vector<uint8_t>& content) = 0;
269     virtual std::string GetAssetPath(const std::string& url) = 0;
270 
271     virtual void WaitTimer(const std::string& callbackId, const std::string& delay, bool isInterval, bool isFirst) = 0;
272     virtual void ClearTimer(const std::string& callbackId) = 0;
273 
274     virtual void PostSyncTaskToPage(std::function<void()>&& task, const std::string& name) = 0;
275 
276     virtual void AddTaskObserver(std::function<void()>&& task) = 0;
277 
278     virtual void RemoveTaskObserver() = 0;
279 
280     virtual void GetI18nData(std::unique_ptr<JsonValue>& json) = 0;
281 
282     virtual void GetResourceConfiguration(std::unique_ptr<JsonValue>& json) = 0;
283 
284     virtual void GetConfigurationCommon(const std::string& filePath, std::unique_ptr<JsonValue>& data) = 0;
285 
286     virtual const RefPtr<MediaQueryInfo>& GetMediaQueryInfoInstance() = 0;
287 
288     virtual void OnMediaQueryUpdate(bool isSynchronous = false) = 0;
289 
290     virtual void RegisterFont(const std::string& familyName, const std::string& familySrc,
291         const std::string& bundleName = "", const std::string& moduleName = "") = 0;
292 
293     virtual void GetSystemFontList(std::vector<std::string>& fontList) = 0;
294 
295     virtual bool GetSystemFont(const std::string& fontName, FontInfo& fontInfo) = 0;
296 
GetUIFontConfig(FontConfigJsonInfo & fontConfigJsonInfo)297     virtual void GetUIFontConfig(FontConfigJsonInfo& fontConfigJsonInfo) {}
298 
299     virtual void AddFrameNodeToOverlay(
300         const RefPtr<NG::FrameNode>& node, std::optional<int32_t> index = std::nullopt) {}
RemoveFrameNodeOnOverlay(const RefPtr<NG::FrameNode> & node)301     virtual void RemoveFrameNodeOnOverlay(const RefPtr<NG::FrameNode>& node) {}
ShowNodeOnOverlay(const RefPtr<NG::FrameNode> & node)302     virtual void ShowNodeOnOverlay(const RefPtr<NG::FrameNode>& node) {}
HideNodeOnOverlay(const RefPtr<NG::FrameNode> & node)303     virtual void HideNodeOnOverlay(const RefPtr<NG::FrameNode>& node) {}
ShowAllNodesOnOverlay()304     virtual void ShowAllNodesOnOverlay() {}
HideAllNodesOnOverlay()305     virtual void HideAllNodesOnOverlay() {}
SetOverlayManagerOptions(const NG::OverlayManagerInfo & overlayInfo)306     virtual bool SetOverlayManagerOptions(const NG::OverlayManagerInfo& overlayInfo)
307     {
308         return false;
309     }
GetOverlayManagerOptions()310     virtual std::optional<NG::OverlayManagerInfo> GetOverlayManagerOptions()
311     {
312         return std::nullopt;
313     }
314 
315     virtual SingleTaskExecutor GetAnimationJsTask() = 0;
316 
317     virtual SingleTaskExecutor GetUiTask() = 0;
318 
319     virtual RefPtr<PipelineBase> GetPipelineContext() = 0;
320 
321     virtual const RefPtr<GroupJsBridge>& GetGroupJsBridge() = 0;
322 
323     virtual RefPtr<JsAcePage> GetPage(int32_t pageId) const = 0;
324 
325     virtual int32_t GetMinPlatformVersion() = 0;
326 
327     template<typename T>
328     bool ACE_EXPORT GetResourceData(const std::string& fileUri, T& content);
329 
330     template<typename T>
331     bool ACE_EXPORT GetResourceData(const std::string& fileUri, T& content, std::string& ami);
332 
333     template<typename T>
334     ACE_EXPORT static bool GetResourceData(const std::string& fileUri, const RefPtr<AssetManager>& assetManager,
335         T& content);
336 
GetAssetManager()337     ACE_EXPORT RefPtr<AssetManager> GetAssetManager() const
338     {
339         return assetManager_;
340     }
341 
342     virtual void LoadResourceConfiguration(std::map<std::string, std::string>& sortedResourcePath,
343         std::unique_ptr<JsonValue>& currentResourceData) = 0;
344 
DisallowPopLastPage()345     void DisallowPopLastPage()
346     {
347         disallowPopLastPage_ = true;
348     }
349 
CallNativeHandler(const std::string & event,const std::string & params)350     virtual void CallNativeHandler(const std::string& event, const std::string& params) {}
351 
352 protected:
353     RefPtr<AssetManager> assetManager_;
354     bool disallowPopLastPage_ = false;
355     bool isFirstNotifyShow_ = true;
356 
357     ACE_DISALLOW_COPY_AND_MOVE(FrontendDelegate);
358 };
359 
360 } // namespace OHOS::Ace::Framework
361 
362 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_FRONTEND_DELEGATE_H
363