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