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