1 /* 2 * Copyright (c) 2022-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_DECLARATIVE_FRONTEND_FRONTEND_DELEGATE_DECLARATIVE_NG_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_FRONTEND_DELEGATE_DECLARATIVE_NG_H 18 19 #include "base/memory/ace_type.h" 20 #include "base/thread/cancelable_callback.h" 21 #include "base/utils/measure_util.h" 22 #include "frameworks/bridge/common/accessibility/accessibility_node_manager.h" 23 #include "frameworks/bridge/common/manifest/manifest_parser.h" 24 #include "frameworks/bridge/common/utils/pipeline_context_holder.h" 25 #include "frameworks/bridge/declarative_frontend/ng/page_router_manager.h" 26 #include "frameworks/bridge/js_frontend/engine/common/group_js_bridge.h" 27 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h" 28 #include "frameworks/bridge/js_frontend/frontend_delegate.h" 29 #include "frameworks/bridge/js_frontend/frontend_delegate_impl.h" 30 #include "frameworks/bridge/js_frontend/js_ace_page.h" 31 32 namespace OHOS::Ace::Framework { 33 34 class FrontendDelegateDeclarativeNG : public FrontendDelegate { 35 DECLARE_ACE_TYPE(FrontendDelegateDeclarativeNG, FrontendDelegate); 36 37 public: 38 FrontendDelegateDeclarativeNG(const RefPtr<TaskExecutor>& taskExecutor); 39 ~FrontendDelegateDeclarativeNG() override = default; 40 41 void AttachPipelineContext(const RefPtr<PipelineBase>& context) override; 42 void AttachSubPipelineContext(const RefPtr<PipelineBase>& context); 43 // distribute RestoreRouterStack(const std::string & contentInfo)44 std::string RestoreRouterStack(const std::string& contentInfo) override 45 { 46 return ""; 47 } 48 std::string GetContentInfo() override; 49 // JSFrontend delegate NG functions. 50 void RunPage(const std::string& url, const std::string& params, const std::string& profile); 51 void OnConfigurationUpdated(const std::string& data); 52 bool OnStartContinuation(); 53 void OnCompleteContinuation(int32_t code); 54 void OnSaveData(std::string& data); 55 bool OnRestoreData(const std::string& data); 56 void OnRemoteTerminated(); 57 void SetColorMode(ColorMode colorMode); 58 void CallPopPage(); 59 void OnApplicationDestroy(const std::string& packageName); 60 void UpdateApplicationState(const std::string& packageName, Frontend::State state); 61 void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data); 62 void NotifyAppStorage( 63 const WeakPtr<Framework::JsEngine>& jsEngineWeak, const std::string& key, const std::string& value); 64 65 // set callback 66 void SetMediaQueryCallback(MediaQueryCallback&& mediaQueryCallback); 67 void SetLayoutInspectorCallback(const LayoutInspectorCallback& layoutInspectorCallback); 68 void SetDrawInspectorCallback(const DrawInspectorCallback& drawInspectorCallback); 69 void SetOnStartContinuationCallBack(OnStartContinuationCallBack&& onStartContinuationCallBack); 70 void SetOnCompleteContinuationCallBack(OnCompleteContinuationCallBack&& onCompleteContinuationCallBack); 71 void SetOnSaveDataCallBack(OnSaveDataCallBack&& onSaveDataCallBack); 72 void SetOnRemoteTerminatedCallBack(OnRemoteTerminatedCallBack&& onRemoteTerminatedCallBack); 73 void SetOnRestoreDataCallBack(OnRestoreDataCallBack&& onRestoreDataCallBack); 74 void SetDestroyApplicationCallback(DestroyApplicationCallback&& destroyApplicationCallback); 75 void SetUpdateApplicationStateCallback(UpdateApplicationStateCallback&& updateApplicationStateCallback); 76 void SetOnWindowDisplayModeChangedCallback( 77 OnWindowDisplayModeChangedCallBack&& onWindowDisplayModeChangedCallBack); 78 void SetExternalEventCallback(ExternalEventCallback&& externalEventCallback); 79 void SetTimerCallback(TimerCallback&& timerCallback); 80 GetJSAccessibilityManager()81 RefPtr<Framework::AccessibilityNodeManager> GetJSAccessibilityManager() const 82 { 83 return jsAccessibilityManager_; 84 } 85 void FireAccessibilityEvent(const AccessibilityEvent& accessibilityEvent); 86 void InitializeAccessibilityCallback(); 87 88 void OnSurfaceChanged(); 89 void OnMediaQueryUpdate(bool isSynchronous = false) override; 90 void OnLayoutCompleted(const std::string& componentId); 91 void OnDrawCompleted(const std::string& componentId); 92 void FireExternalEvent(const std::string& eventId, const std::string& componentId, uint32_t nodeId, bool isDestroy); 93 94 // FrontendDelegate overrides. 95 void Push(const std::string& uri, const std::string& params) override; 96 void PushWithMode(const std::string& uri, const std::string& params, uint32_t routerMode) override; 97 void PushWithCallback(const std::string& uri, const std::string& params, 98 const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override; 99 void PushNamedRoute(const std::string& uri, const std::string& params, 100 const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override; 101 void Replace(const std::string& uri, const std::string& params) override; 102 void ReplaceWithMode(const std::string& uri, const std::string& params, uint32_t routerMode) override; 103 void ReplaceWithCallback(const std::string& uri, const std::string& params, 104 const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override; 105 void ReplaceNamedRoute(const std::string& uri, const std::string& params, 106 const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override; 107 void Back(const std::string& uri, const std::string& params) override; 108 void PostponePageTransition() override; 109 void LaunchPageTransition() override; 110 void Clear() override; 111 int32_t GetStackSize() const override; 112 void GetState(int32_t& index, std::string& name, std::string& path) override; GetPage(int32_t pageId)113 RefPtr<JsAcePage> GetPage(int32_t pageId) const override 114 { 115 return nullptr; 116 } 117 size_t GetComponentsCount() override; 118 std::string GetParams() override; 119 void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params); 120 121 void TriggerPageUpdate(int32_t pageId, bool directExecute = false) override {} 122 123 void PostJsTask(std::function<void()>&& task) override; 124 const std::string& GetAppID() const override; 125 const std::string& GetAppName() const override; 126 const std::string& GetVersionName() const override; 127 int32_t GetVersionCode() const override; 128 129 double MeasureText(const MeasureContext& context) override; 130 Size MeasureTextSize(const MeasureContext& context) override; 131 void ShowToast(const std::string& message, int32_t duration, const std::string& bottom) override; 132 void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons, 133 bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, 134 const std::set<std::string>& callbacks) override; 135 void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons, 136 bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks, 137 std::function<void(bool)>&& onStatusChanged) override; 138 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) override; 140 void ShowDialog(const PromptDialogAttr &dialogAttr, const std::vector<ButtonInfo> &buttons, 141 std::function<void(int32_t, int32_t)> &&callback, const std::set<std::string> &callbacks, 142 std::function<void(bool)>&& onStatusChanged) override; 143 144 void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback) override; 145 146 void DisableAlertBeforeBackPage() override; 147 148 void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 149 std::function<void(int32_t, int32_t)>&& callback) override; 150 151 void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button, 152 std::function<void(int32_t, int32_t)>&& callback, std::function<void(bool)>&& onStatusChanged) override; 153 154 void ShowActionMenuInner(DialogProperties& dialogProperties, const std::vector<ButtonInfo>& button, 155 std::function<void(int32_t, int32_t)>&& callback); 156 157 void WaitTimer(const std::string& callbackId, const std::string& delay, bool isInterval, bool isFirst) override; 158 void ClearTimer(const std::string& callbackId) override; AddTaskObserver(std::function<void ()> && task)159 void AddTaskObserver(std::function<void()>&& task) override {} RemoveTaskObserver()160 void RemoveTaskObserver() override {} PushJsCallbackToRenderNode(NodeId id,double ratio,std::function<void (bool,double)> && callback)161 void PushJsCallbackToRenderNode(NodeId id, double ratio, std::function<void(bool, double)>&& callback) override {} 162 void PostSyncTaskToPage(std::function<void()>&& task) override; SetCallBackResult(const std::string & callBackId,const std::string & result)163 void SetCallBackResult(const std::string& callBackId, const std::string& result) override {} 164 bool GetAssetContent(const std::string& url, std::string& content) override; 165 bool GetAssetContent(const std::string& url, std::vector<uint8_t>& content) override; 166 std::string GetAssetPath(const std::string& url) override; 167 // i18n GetI18nData(std::unique_ptr<JsonValue> & json)168 void GetI18nData(std::unique_ptr<JsonValue>& json) override {} 169 GetResourceConfiguration(std::unique_ptr<JsonValue> & json)170 void GetResourceConfiguration(std::unique_ptr<JsonValue>& json) override {} 171 GetConfigurationCommon(const std::string & filePath,std::unique_ptr<JsonValue> & data)172 void GetConfigurationCommon(const std::string& filePath, std::unique_ptr<JsonValue>& data) override {} GetBoundingRectData(NodeId nodeId)173 Rect GetBoundingRectData(NodeId nodeId) override 174 { 175 return Rect(); 176 } 177 GetInspector(NodeId nodeId)178 std::string GetInspector(NodeId nodeId) override 179 { 180 return ""; 181 } GetMinPlatformVersion()182 int32_t GetMinPlatformVersion() override 183 { 184 return manifestParser_->GetMinPlatformVersion(); 185 } 186 LoadResourceConfiguration(std::map<std::string,std::string> & mediaResourceFileMap,std::unique_ptr<JsonValue> & currentResourceData)187 void LoadResourceConfiguration(std::map<std::string, std::string>& mediaResourceFileMap, 188 std::unique_ptr<JsonValue>& currentResourceData) override 189 {} 190 191 void ChangeLocale(const std::string& language, const std::string& countryOrRegion) override; 192 193 void RegisterFont(const std::string& familyName, const std::string& familySrc) override; 194 195 void GetSystemFontList(std::vector<std::string>& fontList) override; 196 197 bool GetSystemFont(const std::string& fontName, FontInfo& fontInfo) override; 198 HandleImage(const std::string & src,std::function<void (bool,int32_t,int32_t)> && callback)199 void HandleImage(const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback) override {} 200 201 void GetSnapshot(const std::string& componentId, 202 std::function<void(std::shared_ptr<Media::PixelMap>, int32_t)>&& callback) override; 203 204 void CreateSnapshot(std::function<void()>&& customBuilder, 205 std::function<void(std::shared_ptr<Media::PixelMap>, int32_t)>&& callback) override; RequestAnimationFrame(const std::string & callbackId)206 void RequestAnimationFrame(const std::string& callbackId) override {} 207 CancelAnimationFrame(const std::string & callbackId)208 void CancelAnimationFrame(const std::string& callbackId) override {} 209 SingleTaskExecutor GetAnimationJsTask() override; 210 211 SingleTaskExecutor GetUiTask() override; 212 GetMediaQueryInfoInstance()213 const RefPtr<MediaQueryInfo>& GetMediaQueryInfoInstance() override 214 { 215 return mediaQueryInfo_; 216 } 217 SetGroupJsBridge(const RefPtr<GroupJsBridge> & groupJsBridge)218 void SetGroupJsBridge(const RefPtr<GroupJsBridge>& groupJsBridge) 219 { 220 groupJsBridge_ = groupJsBridge; 221 } GetGroupJsBridge()222 const RefPtr<GroupJsBridge>& GetGroupJsBridge() override 223 { 224 return groupJsBridge_; 225 } 226 227 RefPtr<PipelineBase> GetPipelineContext() override; 228 SetPageRouterManager(const RefPtr<NG::PageRouterManager> & routerMgr)229 void SetPageRouterManager(const RefPtr<NG::PageRouterManager>& routerMgr) 230 { 231 pageRouterManager_ = routerMgr; 232 } GetPageRouterManager()233 const RefPtr<NG::PageRouterManager>& GetPageRouterManager() const 234 { 235 return pageRouterManager_; 236 } 237 GetWindowConfig()238 WindowConfig& GetWindowConfig() 239 { 240 return manifestParser_->GetWindowConfig(); 241 } 242 GetManifestParser()243 const RefPtr<Framework::ManifestParser>& GetManifestParser() const 244 { 245 return manifestParser_; 246 } 247 GetTaskExecutor()248 const RefPtr<TaskExecutor>& GetTaskExecutor() const 249 { 250 return taskExecutor_; 251 } 252 253 bool OnPageBackPress(); 254 255 void OnPageShow(); 256 257 void OnPageHide(); 258 void OnForeground(); 259 260 std::string GetCurrentPageUrl(); 261 262 // Get the currently running JS page information in NG structure. 263 RefPtr<RevSourceMap> GetCurrentPageSourceMap(); 264 265 // Get the currently running JS page information in NG structure. 266 RefPtr<RevSourceMap> GetFaAppSourceMap(); 267 268 void GetStageSourceMap(std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap); 269 void ShowDialogInner(DialogProperties& dialogProperties, std::function<void(int32_t, int32_t)>&& callback, 270 const std::set<std::string>& callbacks); 271 272 void RebuildAllPages(); 273 274 private: 275 PipelineContextHolder pipelineContextHolder_; 276 RefPtr<TaskExecutor> taskExecutor_; 277 RefPtr<NG::PageRouterManager> pageRouterManager_; 278 RefPtr<Framework::ManifestParser> manifestParser_; 279 RefPtr<MediaQueryInfo> mediaQueryInfo_; 280 RefPtr<GroupJsBridge> groupJsBridge_; 281 282 RefPtr<Framework::AccessibilityNodeManager> jsAccessibilityManager_; 283 RefPtr<RevSourceMap> appSourceMap_; 284 std::queue<std::string> animationFrameTaskIds_; 285 std::unordered_map<std::string, CancelableCallback<void()>> animationFrameTaskMap_; 286 287 std::unordered_map<std::string, CancelableCallback<void()>> timeoutTaskMap_; 288 289 MediaQueryCallback mediaQueryCallback_; 290 LayoutInspectorCallback layoutInspectorCallback_; 291 DrawInspectorCallback drawInspectorCallback_; 292 OnStartContinuationCallBack onStartContinuationCallBack_; 293 OnCompleteContinuationCallBack onCompleteContinuationCallBack_; 294 OnSaveDataCallBack onSaveDataCallBack_; 295 OnRemoteTerminatedCallBack onRemoteTerminatedCallBack_; 296 OnRestoreDataCallBack onRestoreDataCallBack_; 297 DestroyApplicationCallback destroyApplication_; 298 UpdateApplicationStateCallback updateApplicationState_; 299 OnWindowDisplayModeChangedCallBack onWindowDisplayModeChanged_; 300 ExternalEventCallback externalEvent_; 301 TimerCallback timer_; 302 303 mutable std::mutex mutex_; 304 }; 305 306 } // namespace OHOS::Ace::Framework 307 308 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_FRONTEND_DELEGATE_DECLARATIVE_NG_H 309