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