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 virtual void RemoveVisibleChangeNode(NodeId id) = 0; 115 116 // ---------------- 117 // system.app 118 // ---------------- 119 virtual const std::string& GetAppID() const = 0; 120 virtual const std::string& GetAppName() const = 0; 121 virtual const std::string& GetVersionName() const = 0; 122 virtual int32_t GetVersionCode() const = 0; 123 124 // ---------------- 125 // system.measure 126 // ---------------- 127 virtual double MeasureText(const MeasureContext& context) = 0; 128 virtual Size MeasureTextSize(const MeasureContext& context) = 0; 129 130 // ---------------- 131 // system.prompt 132 // ---------------- 133 virtual void ShowToast(const std::string& message, int32_t duration, const std::string& bottom) = 0; SetToastStopListenerCallback(std::function<void ()> && stopCallback)134 virtual void SetToastStopListenerCallback(std::function<void()>&& stopCallback) {}; 135 virtual void ShowDialog(const std::string& title, const std::string& message, 136 const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, 137 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)138 virtual void ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons, 139 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)140 virtual void ShowDialog(const std::string& title, const std::string& message, 141 const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, 142 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)143 virtual void ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons, 144 std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks, 145 std::function<void(bool)>&& onStatusChanged) {}; 146 147 virtual void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback) = 0; 148 virtual void DisableAlertBeforeBackPage() = 0; 149 150 virtual void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 151 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)152 virtual void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 153 std::function<void(int32_t, int32_t)>&& callback, std::function<void(bool)>&& onStatusChanged) {}; 154 155 virtual Rect GetBoundingRectData(NodeId nodeId) = 0; 156 157 virtual std::string GetInspector(NodeId nodeId) = 0; 158 159 virtual void PushJsCallbackToRenderNode(NodeId id, double ratio, std::function<void(bool, double)>&& callback) = 0; 160 161 // ---------------- 162 // system.configuration 163 // ---------------- 164 virtual void ChangeLocale(const std::string& language, const std::string& countryOrRegion) = 0; 165 166 // ---------------- 167 // system.image 168 // ---------------- 169 virtual void HandleImage(const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback) = 0; 170 // ---------------- 171 // internal.jsResult 172 // ---------------- 173 virtual void SetCallBackResult(const std::string& callBackId, const std::string& result) = 0; 174 175 // ---------------- 176 // system.animation 177 // ---------------- 178 virtual void RequestAnimationFrame(const std::string& callbackId) = 0; 179 virtual void CancelAnimationFrame(const std::string& callbackId) = 0; 180 GetSnapshot(const std::string & componentId,std::function<void (std::shared_ptr<Media::PixelMap>,int32_t)> && callback)181 virtual void GetSnapshot( 182 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)183 virtual void CreateSnapshot(std::function<void()>&& customBuilder, 184 std::function<void(std::shared_ptr<Media::PixelMap>, int32_t)>&& callback) {} 185 186 virtual bool GetAssetContent(const std::string& url, std::string& content) = 0; 187 virtual bool GetAssetContent(const std::string& url, std::vector<uint8_t>& content) = 0; 188 virtual std::string GetAssetPath(const std::string& url) = 0; 189 190 virtual void WaitTimer(const std::string& callbackId, const std::string& delay, bool isInterval, bool isFirst) = 0; 191 virtual void ClearTimer(const std::string& callbackId) = 0; 192 193 virtual void PostSyncTaskToPage(std::function<void()>&& task) = 0; 194 195 virtual void AddTaskObserver(std::function<void()>&& task) = 0; 196 197 virtual void RemoveTaskObserver() = 0; 198 199 virtual void GetI18nData(std::unique_ptr<JsonValue>& json) = 0; 200 201 virtual void GetResourceConfiguration(std::unique_ptr<JsonValue>& json) = 0; 202 203 virtual void GetConfigurationCommon(const std::string& filePath, std::unique_ptr<JsonValue>& data) = 0; 204 205 virtual const RefPtr<MediaQueryInfo>& GetMediaQueryInfoInstance() = 0; 206 207 virtual void OnMediaQueryUpdate(bool isSynchronous = false) = 0; 208 209 virtual void RegisterFont(const std::string& familyName, const std::string& familySrc) = 0; 210 211 virtual void GetSystemFontList(std::vector<std::string>& fontList) = 0; 212 213 virtual bool GetSystemFont(const std::string& fontName, FontInfo& fontInfo) = 0; 214 215 virtual SingleTaskExecutor GetAnimationJsTask() = 0; 216 217 virtual SingleTaskExecutor GetUiTask() = 0; 218 219 virtual RefPtr<PipelineBase> GetPipelineContext() = 0; 220 221 virtual const RefPtr<GroupJsBridge>& GetGroupJsBridge() = 0; 222 223 virtual RefPtr<JsAcePage> GetPage(int32_t pageId) const = 0; 224 225 virtual int32_t GetMinPlatformVersion() = 0; 226 227 template<typename T> 228 bool ACE_EXPORT GetResourceData(const std::string& fileUri, T& content); 229 230 template<typename T> 231 bool ACE_EXPORT GetResourceData(const std::string& fileUri, T& content, std::string& ami); 232 233 template<typename T> 234 ACE_EXPORT static bool GetResourceData(const std::string& fileUri, const RefPtr<AssetManager>& assetManager, 235 T& content); 236 GetAssetManager()237 ACE_EXPORT RefPtr<AssetManager> GetAssetManager() const 238 { 239 return assetManager_; 240 } 241 242 virtual void LoadResourceConfiguration(std::map<std::string, std::string>& sortedResourcePath, 243 std::unique_ptr<JsonValue>& currentResourceData) = 0; 244 DisallowPopLastPage()245 void DisallowPopLastPage() 246 { 247 disallowPopLastPage_ = true; 248 } 249 CallNativeHandler(const std::string & event,const std::string & params)250 virtual void CallNativeHandler(const std::string& event, const std::string& params) {} 251 252 protected: 253 RefPtr<AssetManager> assetManager_; 254 bool disallowPopLastPage_ = false; 255 bool isFirstNotifyShow_ = true; 256 257 ACE_DISALLOW_COPY_AND_MOVE(FrontendDelegate); 258 }; 259 260 } // namespace OHOS::Ace::Framework 261 262 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_FRONTEND_DELEGATE_H 263