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