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