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